blob: d24d1eb745af9ab6771cbda521cadd6885156631 [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 );
399 TEST_ASSERT( 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 );
404 TEST_ASSERT( 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,
560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_ASSERT( output_length <=
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
570 TEST_ASSERT( output_length <=
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Neil Armstrong4766f992022-02-28 16:23:59 +0100592/*!
593 * \brief Internal Function for MAC multipart tests.
594 * \param key_type_arg Type of key passed in
595 * \param key_data The encryption / decryption key data
596 * \param alg_arg The type of algorithm used
597 * \param input_data Data to encrypt / decrypt
598 * \param data_part_len_arg If not -1, the length of chunks to feed
599 * the data in to be encrypted / decrypted. If
600 * -1, no chunking
601 * \param expected_output Expected output
602 * \param is_verify If non-zero this is an verify operation.
603 * \param do_zero_parts If non-zero, interleave zero length chunks
604 * with normal length chunks.
605 * \return int Zero on failure, non-zero on success.
606 */
607static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
608 int alg_arg,
609 data_t *input_data,
610 int data_part_len_arg,
611 data_t *expected_output,
612 int is_verify,
613 int do_zero_parts )
614{
615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
616 psa_key_type_t key_type = key_type_arg;
617 psa_algorithm_t alg = alg_arg;
618 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
619 unsigned char mac[PSA_MAC_MAX_SIZE];
620 size_t part_offset = 0;
621 size_t part_length = 0;
622 size_t data_part_len = 0;
623 size_t mac_len = 0;
624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
625 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
626
627 int test_ok = 0;
628 size_t part_count = 0;
629
Neil Armstrongfd4c2592022-03-07 10:11:11 +0100630 PSA_INIT( );
Neil Armstrong4766f992022-02-28 16:23:59 +0100631
632 if( is_verify )
633 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
634 else
635 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
636
637 psa_set_key_algorithm( &attributes, alg );
638 psa_set_key_type( &attributes, key_type );
639
640 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
641 &key ) );
642
643 if( is_verify )
644 status = psa_mac_verify_setup( &operation, key, alg );
645 else
646 status = psa_mac_sign_setup( &operation, key, alg );
647
648 PSA_ASSERT( status );
649
650 if( data_part_len_arg != -1 )
651 {
652 /* Pass data in parts */
653 data_part_len = ( size_t ) data_part_len_arg;
654
655 for( part_offset = 0, part_count = 0;
656 part_offset < input_data->len;
657 part_offset += part_length, part_count++ )
658 {
659 if( do_zero_parts && ( part_count & 0x01 ) )
660 {
661 part_length = 0;
662 }
663 else if( ( input_data->len - part_offset ) < data_part_len )
664 {
665 part_length = ( input_data->len - part_offset );
666 }
667 else
668 {
669 part_length = data_part_len;
670 }
671
672 PSA_ASSERT( psa_mac_update( &operation,
673 ( input_data->x + part_offset ),
674 part_length ) );
675 }
676 }
677 else
678 {
679 /* Pass all data in one go. */
680 PSA_ASSERT( psa_mac_update( &operation, input_data->x,
681 input_data->len ) );
682 }
683
684 if( is_verify )
685 {
686 PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
687 expected_output->len ) );
688 }
689 else
690 {
691 PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
692 PSA_MAC_MAX_SIZE, &mac_len ) );
693
694 ASSERT_COMPARE( expected_output->x, expected_output->len,
695 mac, mac_len );
696 }
697
698 test_ok = 1;
699
700exit:
701 psa_destroy_key( key );
702 psa_mac_abort( &operation );
703 PSA_DONE( );
704
705 return( test_ok );
706}
707
Gilles Peskinee59236f2018-01-27 23:32:46 +0100708/* END_HEADER */
709
710/* BEGIN_DEPENDENCIES
711 * depends_on:MBEDTLS_PSA_CRYPTO_C
712 * END_DEPENDENCIES
713 */
714
715/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200716void static_checks( )
717{
718 size_t max_truncated_mac_size =
719 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
720
721 /* Check that the length for a truncated MAC always fits in the algorithm
722 * encoding. The shifted mask is the maximum truncated value. The
723 * untruncated algorithm may be one byte larger. */
724 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
725}
726/* END_CASE */
727
728/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200729void import_with_policy( int type_arg,
730 int usage_arg, int alg_arg,
731 int expected_status_arg )
732{
733 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
734 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200735 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200736 psa_key_type_t type = type_arg;
737 psa_key_usage_t usage = usage_arg;
738 psa_algorithm_t alg = alg_arg;
739 psa_status_t expected_status = expected_status_arg;
740 const uint8_t key_material[16] = {0};
741 psa_status_t status;
742
743 PSA_ASSERT( psa_crypto_init( ) );
744
745 psa_set_key_type( &attributes, type );
746 psa_set_key_usage_flags( &attributes, usage );
747 psa_set_key_algorithm( &attributes, alg );
748
749 status = psa_import_key( &attributes,
750 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200751 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200752 TEST_EQUAL( status, expected_status );
753 if( status != PSA_SUCCESS )
754 goto exit;
755
Ronald Cron5425a212020-08-04 14:58:35 +0200756 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200757 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200758 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200759 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200760 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200761 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200762
Ronald Cron5425a212020-08-04 14:58:35 +0200763 PSA_ASSERT( psa_destroy_key( key ) );
764 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200765
766exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100767 /*
768 * Key attributes may have been returned by psa_get_key_attributes()
769 * thus reset them as required.
770 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200771 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100772
773 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200774 PSA_DONE( );
775}
776/* END_CASE */
777
778/* BEGIN_CASE */
779void import_with_data( data_t *data, int type_arg,
780 int attr_bits_arg,
781 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200782{
783 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
784 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200786 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200787 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200788 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790
Gilles Peskine8817f612018-12-18 00:18:46 +0100791 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100792
Gilles Peskine4747d192019-04-17 15:05:45 +0200793 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200794 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200795
Ronald Cron5425a212020-08-04 14:58:35 +0200796 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100797 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200798 if( status != PSA_SUCCESS )
799 goto exit;
800
Ronald Cron5425a212020-08-04 14:58:35 +0200801 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200802 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200803 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200804 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200805 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200806
Ronald Cron5425a212020-08-04 14:58:35 +0200807 PSA_ASSERT( psa_destroy_key( key ) );
808 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100809
810exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100811 /*
812 * Key attributes may have been returned by psa_get_key_attributes()
813 * thus reset them as required.
814 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200815 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100816
817 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200818 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819}
820/* END_CASE */
821
822/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200823void import_large_key( int type_arg, int byte_size_arg,
824 int expected_status_arg )
825{
826 psa_key_type_t type = type_arg;
827 size_t byte_size = byte_size_arg;
828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
829 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200831 psa_status_t status;
832 uint8_t *buffer = NULL;
833 size_t buffer_size = byte_size + 1;
834 size_t n;
835
Steven Cooreman69967ce2021-01-18 18:01:08 +0100836 /* Skip the test case if the target running the test cannot
837 * accomodate large keys due to heap size constraints */
838 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200839 memset( buffer, 'K', byte_size );
840
841 PSA_ASSERT( psa_crypto_init( ) );
842
843 /* Try importing the key */
844 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
845 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200846 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100847 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200848 TEST_EQUAL( status, expected_status );
849
850 if( status == PSA_SUCCESS )
851 {
Ronald Cron5425a212020-08-04 14:58:35 +0200852 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200853 TEST_EQUAL( psa_get_key_type( &attributes ), type );
854 TEST_EQUAL( psa_get_key_bits( &attributes ),
855 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200856 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200857 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200858 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200859 for( n = 0; n < byte_size; n++ )
860 TEST_EQUAL( buffer[n], 'K' );
861 for( n = byte_size; n < buffer_size; n++ )
862 TEST_EQUAL( buffer[n], 0 );
863 }
864
865exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100866 /*
867 * Key attributes may have been returned by psa_get_key_attributes()
868 * thus reset them as required.
869 */
870 psa_reset_key_attributes( &attributes );
871
Ronald Cron5425a212020-08-04 14:58:35 +0200872 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200873 PSA_DONE( );
874 mbedtls_free( buffer );
875}
876/* END_CASE */
877
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100878/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200879void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
880{
Ronald Cron5425a212020-08-04 14:58:35 +0200881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200882 size_t bits = bits_arg;
883 psa_status_t expected_status = expected_status_arg;
884 psa_status_t status;
885 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200886 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200887 size_t buffer_size = /* Slight overapproximations */
888 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200889 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200890 unsigned char *p;
891 int ret;
892 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200894
Gilles Peskine8817f612018-12-18 00:18:46 +0100895 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200896 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200897
898 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
899 bits, keypair ) ) >= 0 );
900 length = ret;
901
902 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200903 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200904 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100905 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200906
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200907 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200908 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200909
910exit:
911 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200912 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200913}
914/* END_CASE */
915
916/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300917void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300918 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200919 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530920 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100921 int expected_bits,
922 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200923 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100924 int canonical_input )
925{
Ronald Cron5425a212020-08-04 14:58:35 +0200926 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100927 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200928 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200929 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100930 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530931 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100932 unsigned char *exported = NULL;
933 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100934 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100935 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100936 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200938 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939
Moran Pekercb088e72018-07-17 17:36:59 +0300940 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200941 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100942 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200943 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100944 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945
Archana4d7ae1d2021-07-07 02:50:22 +0530946 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200947 psa_set_key_usage_flags( &attributes, usage_arg );
948 psa_set_key_algorithm( &attributes, alg );
949 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700950
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100951 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200952 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953
954 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200955 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200956 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
957 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200958 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100959
960 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200961 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100962 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100963
964 /* The exported length must be set by psa_export_key() to a value between 0
965 * and export_size. On errors, the exported length must be 0. */
966 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
967 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
968 TEST_ASSERT( exported_length <= export_size );
969
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200970 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200971 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100972 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200973 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100974 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200976 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977
Gilles Peskineea38a922021-02-13 00:05:16 +0100978 /* Run sanity checks on the exported key. For non-canonical inputs,
979 * this validates the canonical representations. For canonical inputs,
980 * this doesn't directly validate the implementation, but it still helps
981 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530982 if( !psa_key_lifetime_is_external( lifetime ) )
983 {
984 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
985 goto exit;
986 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200987
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200989 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990 else
991 {
Ronald Cron5425a212020-08-04 14:58:35 +0200992 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200993 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200994 &key2 ) );
995 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100996 reexported,
997 export_size,
998 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200999 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301000 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001001 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 }
gabor-mezei-armceface22021-01-21 12:26:17 +01001003 TEST_ASSERT( exported_length <=
Archana4d7ae1d2021-07-07 02:50:22 +05301004 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301005 psa_get_key_bits( &got_attributes ) ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01001006 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007
1008destroy:
1009 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001010 PSA_ASSERT( psa_destroy_key( key ) );
1011 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012
1013exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001014 /*
1015 * Key attributes may have been returned by psa_get_key_attributes()
1016 * thus reset them as required.
1017 */
1018 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301019 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001020 mbedtls_free( exported );
1021 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001022 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023}
1024/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001025
Moran Pekerf709f4a2018-06-06 17:26:04 +03001026/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001027void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001028 int type_arg,
1029 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301030 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001031 int export_size_delta,
1032 int expected_export_status_arg,
1033 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001034{
Ronald Cron5425a212020-08-04 14:58:35 +02001035 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001036 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001037 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001038 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001039 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301040 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001041 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001042 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001043 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001044 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001045
Gilles Peskine8817f612018-12-18 00:18:46 +01001046 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001047
Archana4d7ae1d2021-07-07 02:50:22 +05301048 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001049 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1050 psa_set_key_algorithm( &attributes, alg );
1051 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001052
1053 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001054 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001055
Gilles Peskine49c25912018-10-29 15:15:31 +01001056 /* Export the public key */
1057 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001058 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001059 exported, export_size,
1060 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001061 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001062 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001063 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001064 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001065 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001066 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001067 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001068 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001069 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01001070 TEST_ASSERT( expected_public_key->len <=
1071 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1072 TEST_ASSERT( expected_public_key->len <=
1073 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001074 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1075 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001076 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001077exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001078 /*
1079 * Key attributes may have been returned by psa_get_key_attributes()
1080 * thus reset them as required.
1081 */
1082 psa_reset_key_attributes( &attributes );
1083
itayzafrir3e02b3b2018-06-12 17:06:52 +03001084 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001085 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001086 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001087}
1088/* END_CASE */
1089
Gilles Peskine20035e32018-02-03 22:44:14 +01001090/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001091void import_and_exercise_key( data_t *data,
1092 int type_arg,
1093 int bits_arg,
1094 int alg_arg )
1095{
Ronald Cron5425a212020-08-04 14:58:35 +02001096 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001097 psa_key_type_t type = type_arg;
1098 size_t bits = bits_arg;
1099 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001100 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001102 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001103
Gilles Peskine8817f612018-12-18 00:18:46 +01001104 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001105
Gilles Peskine4747d192019-04-17 15:05:45 +02001106 psa_set_key_usage_flags( &attributes, usage );
1107 psa_set_key_algorithm( &attributes, alg );
1108 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001109
1110 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001111 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001112
1113 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001114 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001115 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1116 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001117
1118 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001119 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001120 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001121
Ronald Cron5425a212020-08-04 14:58:35 +02001122 PSA_ASSERT( psa_destroy_key( key ) );
1123 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001124
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001125exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001126 /*
1127 * Key attributes may have been returned by psa_get_key_attributes()
1128 * thus reset them as required.
1129 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001130 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001131
1132 psa_reset_key_attributes( &attributes );
1133 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001134 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001135}
1136/* END_CASE */
1137
1138/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001139void effective_key_attributes( int type_arg, int expected_type_arg,
1140 int bits_arg, int expected_bits_arg,
1141 int usage_arg, int expected_usage_arg,
1142 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001143{
Ronald Cron5425a212020-08-04 14:58:35 +02001144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001145 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001146 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001147 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001148 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001149 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001150 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001151 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001152 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001154
Gilles Peskine8817f612018-12-18 00:18:46 +01001155 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001156
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001157 psa_set_key_usage_flags( &attributes, usage );
1158 psa_set_key_algorithm( &attributes, alg );
1159 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001160 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001161
Ronald Cron5425a212020-08-04 14:58:35 +02001162 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001163 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001164
Ronald Cron5425a212020-08-04 14:58:35 +02001165 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001166 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1167 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1168 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1169 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001170
1171exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001172 /*
1173 * Key attributes may have been returned by psa_get_key_attributes()
1174 * thus reset them as required.
1175 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001176 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001177
1178 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001179 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001180}
1181/* END_CASE */
1182
1183/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001184void check_key_policy( int type_arg, int bits_arg,
1185 int usage_arg, int alg_arg )
1186{
1187 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001188 usage_arg,
1189 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001190 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001191 goto exit;
1192}
1193/* END_CASE */
1194
1195/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001196void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001197{
1198 /* Test each valid way of initializing the object, except for `= {0}`, as
1199 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1200 * though it's OK by the C standard. We could test for this, but we'd need
1201 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001202 psa_key_attributes_t func = psa_key_attributes_init( );
1203 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1204 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001205
1206 memset( &zero, 0, sizeof( zero ) );
1207
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001208 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1209 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1210 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001211
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001212 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1213 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1214 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1215
1216 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1217 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1218 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1219
1220 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1221 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1222 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1223
1224 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1225 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1226 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001227}
1228/* END_CASE */
1229
1230/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001231void mac_key_policy( int policy_usage_arg,
1232 int policy_alg_arg,
1233 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001234 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001235 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001236 int expected_status_sign_arg,
1237 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001238{
Ronald Cron5425a212020-08-04 14:58:35 +02001239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001241 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001242 psa_key_type_t key_type = key_type_arg;
1243 psa_algorithm_t policy_alg = policy_alg_arg;
1244 psa_algorithm_t exercise_alg = exercise_alg_arg;
1245 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001246 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001247 psa_status_t expected_status_sign = expected_status_sign_arg;
1248 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001249 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001250
Gilles Peskine8817f612018-12-18 00:18:46 +01001251 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001252
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001253 psa_set_key_usage_flags( &attributes, policy_usage );
1254 psa_set_key_algorithm( &attributes, policy_alg );
1255 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001256
Gilles Peskine049c7532019-05-15 20:22:09 +02001257 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001258 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001259
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001260 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1261 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001262
Ronald Cron5425a212020-08-04 14:58:35 +02001263 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001264 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001265
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001266 /* Calculate the MAC, one-shot case. */
1267 uint8_t input[128] = {0};
1268 size_t mac_len;
1269 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1270 input, 128,
1271 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1272 expected_status_sign );
1273
Neil Armstrong3af9b972022-02-07 12:20:21 +01001274 /* Calculate the MAC, multi-part case. */
1275 PSA_ASSERT( psa_mac_abort( &operation ) );
1276 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1277 if( status == PSA_SUCCESS )
1278 {
1279 status = psa_mac_update( &operation, input, 128 );
1280 if( status == PSA_SUCCESS )
1281 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1282 &mac_len ),
1283 expected_status_sign );
1284 else
1285 TEST_EQUAL( status, expected_status_sign );
1286 }
1287 else
1288 {
1289 TEST_EQUAL( status, expected_status_sign );
1290 }
1291 PSA_ASSERT( psa_mac_abort( &operation ) );
1292
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001293 /* Verify correct MAC, one-shot case. */
1294 status = psa_mac_verify( key, exercise_alg, input, 128,
1295 mac, mac_len );
1296
1297 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1298 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001299 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001300 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001301
Neil Armstrong3af9b972022-02-07 12:20:21 +01001302 /* Verify correct MAC, multi-part case. */
1303 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1304 if( status == PSA_SUCCESS )
1305 {
1306 status = psa_mac_update( &operation, input, 128 );
1307 if( status == PSA_SUCCESS )
1308 {
1309 status = psa_mac_verify_finish( &operation, mac, mac_len );
1310 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1311 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1312 else
1313 TEST_EQUAL( status, expected_status_verify );
1314 }
1315 else
1316 {
1317 TEST_EQUAL( status, expected_status_verify );
1318 }
1319 }
1320 else
1321 {
1322 TEST_EQUAL( status, expected_status_verify );
1323 }
1324
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001325 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001326
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001327 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001328 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001329 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001330
1331exit:
1332 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001333 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001334 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001335}
1336/* END_CASE */
1337
1338/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001339void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001340 int policy_alg,
1341 int key_type,
1342 data_t *key_data,
1343 int exercise_alg )
1344{
Ronald Cron5425a212020-08-04 14:58:35 +02001345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001347 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001348 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001349 size_t output_buffer_size = 0;
1350 size_t input_buffer_size = 0;
1351 size_t output_length = 0;
1352 uint8_t *output = NULL;
1353 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354 psa_status_t status;
1355
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001356 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1357 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1358 input_buffer_size );
1359
1360 ASSERT_ALLOC( input, input_buffer_size );
1361 ASSERT_ALLOC( output, output_buffer_size );
1362
Gilles Peskine8817f612018-12-18 00:18:46 +01001363 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001365 psa_set_key_usage_flags( &attributes, policy_usage );
1366 psa_set_key_algorithm( &attributes, policy_alg );
1367 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001368
Gilles Peskine049c7532019-05-15 20:22:09 +02001369 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001370 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001371
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001372 /* Check if no key usage flag implication is done */
1373 TEST_EQUAL( policy_usage,
1374 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001375
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001376 /* Encrypt check, one-shot */
1377 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1378 output, output_buffer_size,
1379 &output_length);
1380 if( policy_alg == exercise_alg &&
1381 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1382 PSA_ASSERT( status );
1383 else
1384 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1385
1386 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001387 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001388 if( policy_alg == exercise_alg &&
1389 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001391 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001392 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001393 psa_cipher_abort( &operation );
1394
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001395 /* Decrypt check, one-shot */
1396 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1397 input, input_buffer_size,
1398 &output_length);
1399 if( policy_alg == exercise_alg &&
1400 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1401 PSA_ASSERT( status );
1402 else
1403 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1404
1405 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001406 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001407 if( policy_alg == exercise_alg &&
1408 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001409 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001410 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001411 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001412
1413exit:
1414 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001415 mbedtls_free( input );
1416 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001417 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001418 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001419}
1420/* END_CASE */
1421
1422/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001423void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001424 int policy_alg,
1425 int key_type,
1426 data_t *key_data,
1427 int nonce_length_arg,
1428 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001429 int exercise_alg,
1430 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001431{
Ronald Cron5425a212020-08-04 14:58:35 +02001432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001434 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001435 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001436 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001437 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001438 unsigned char nonce[16] = {0};
1439 size_t nonce_length = nonce_length_arg;
1440 unsigned char tag[16];
1441 size_t tag_length = tag_length_arg;
1442 size_t output_length;
1443
1444 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1445 TEST_ASSERT( tag_length <= sizeof( tag ) );
1446
Gilles Peskine8817f612018-12-18 00:18:46 +01001447 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001448
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001449 psa_set_key_usage_flags( &attributes, policy_usage );
1450 psa_set_key_algorithm( &attributes, policy_alg );
1451 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001452
Gilles Peskine049c7532019-05-15 20:22:09 +02001453 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001454 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001455
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001456 /* Check if no key usage implication is done */
1457 TEST_EQUAL( policy_usage,
1458 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001459
Neil Armstrong752d8112022-02-07 14:51:11 +01001460 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02001461 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001462 nonce, nonce_length,
1463 NULL, 0,
1464 NULL, 0,
1465 tag, tag_length,
1466 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001467 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1468 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001469 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001470 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001471
Neil Armstrong752d8112022-02-07 14:51:11 +01001472 /* Encrypt check, multi-part */
1473 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
1474 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1475 TEST_EQUAL( status, expected_status );
1476 else
1477 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1478
1479 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001480 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001481 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001482 nonce, nonce_length,
1483 NULL, 0,
1484 tag, tag_length,
1485 NULL, 0,
1486 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001487 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1488 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1489 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001490 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001491 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001492 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001493
Neil Armstrong752d8112022-02-07 14:51:11 +01001494 /* Decrypt check, multi-part */
1495 PSA_ASSERT( psa_aead_abort( &operation ) );
1496 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
1497 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1498 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1499 else
1500 TEST_EQUAL( status, expected_status );
1501
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001502exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01001503 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001504 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001505 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001506}
1507/* END_CASE */
1508
1509/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001510void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001511 int policy_alg,
1512 int key_type,
1513 data_t *key_data,
1514 int exercise_alg )
1515{
Ronald Cron5425a212020-08-04 14:58:35 +02001516 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001517 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001518 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001519 psa_status_t status;
1520 size_t key_bits;
1521 size_t buffer_length;
1522 unsigned char *buffer = NULL;
1523 size_t output_length;
1524
Gilles Peskine8817f612018-12-18 00:18:46 +01001525 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001526
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001527 psa_set_key_usage_flags( &attributes, policy_usage );
1528 psa_set_key_algorithm( &attributes, policy_alg );
1529 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001530
Gilles Peskine049c7532019-05-15 20:22:09 +02001531 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001532 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001533
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001534 /* Check if no key usage implication is done */
1535 TEST_EQUAL( policy_usage,
1536 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001537
Ronald Cron5425a212020-08-04 14:58:35 +02001538 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001539 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001540 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1541 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001542 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001543
Ronald Cron5425a212020-08-04 14:58:35 +02001544 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001545 NULL, 0,
1546 NULL, 0,
1547 buffer, buffer_length,
1548 &output_length );
1549 if( policy_alg == exercise_alg &&
1550 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001551 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001552 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001553 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001554
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001555 if( buffer_length != 0 )
1556 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001557 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001558 buffer, buffer_length,
1559 NULL, 0,
1560 buffer, buffer_length,
1561 &output_length );
1562 if( policy_alg == exercise_alg &&
1563 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001564 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001565 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001566 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001567
1568exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001569 /*
1570 * Key attributes may have been returned by psa_get_key_attributes()
1571 * thus reset them as required.
1572 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001573 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001574
1575 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001576 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 mbedtls_free( buffer );
1578}
1579/* END_CASE */
1580
1581/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001582void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001583 int policy_alg,
1584 int key_type,
1585 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001586 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001587 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001588 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001589{
Ronald Cron5425a212020-08-04 14:58:35 +02001590 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001592 psa_key_usage_t policy_usage = policy_usage_arg;
1593 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001594 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001595 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1596 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1597 * compatible with the policy and `payload_length_arg` is supposed to be
1598 * a valid input length to sign. If `payload_length_arg <= 0`,
1599 * `exercise_alg` is supposed to be forbidden by the policy. */
1600 int compatible_alg = payload_length_arg > 0;
1601 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001602 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 size_t signature_length;
1604
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001605 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001606 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001607 TEST_EQUAL( expected_usage,
1608 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001609
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001611
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001612 psa_set_key_usage_flags( &attributes, policy_usage );
1613 psa_set_key_algorithm( &attributes, policy_alg );
1614 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001615
Gilles Peskine049c7532019-05-15 20:22:09 +02001616 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001617 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001618
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001619 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1620
Ronald Cron5425a212020-08-04 14:58:35 +02001621 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001622 payload, payload_length,
1623 signature, sizeof( signature ),
1624 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001625 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001626 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001627 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001628 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001629
1630 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001631 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001632 payload, payload_length,
1633 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001634 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001635 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001636 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001637 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001638
Gilles Peskinef7b41372021-09-22 16:15:05 +02001639 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001640 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001641 {
1642 status = psa_sign_message( key, exercise_alg,
1643 payload, payload_length,
1644 signature, sizeof( signature ),
1645 &signature_length );
1646 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1647 PSA_ASSERT( status );
1648 else
1649 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1650
1651 memset( signature, 0, sizeof( signature ) );
1652 status = psa_verify_message( key, exercise_alg,
1653 payload, payload_length,
1654 signature, sizeof( signature ) );
1655 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1656 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1657 else
1658 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1659 }
1660
Gilles Peskined5b33222018-06-18 22:20:03 +02001661exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001662 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001663 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001664}
1665/* END_CASE */
1666
Janos Follathba3fab92019-06-11 14:50:16 +01001667/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001668void derive_key_policy( int policy_usage,
1669 int policy_alg,
1670 int key_type,
1671 data_t *key_data,
1672 int exercise_alg )
1673{
Ronald Cron5425a212020-08-04 14:58:35 +02001674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001676 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001677 psa_status_t status;
1678
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001680
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001681 psa_set_key_usage_flags( &attributes, policy_usage );
1682 psa_set_key_algorithm( &attributes, policy_alg );
1683 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001684
Gilles Peskine049c7532019-05-15 20:22:09 +02001685 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001686 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001687
Janos Follathba3fab92019-06-11 14:50:16 +01001688 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1689
1690 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1691 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001692 {
Janos Follathba3fab92019-06-11 14:50:16 +01001693 PSA_ASSERT( psa_key_derivation_input_bytes(
1694 &operation,
1695 PSA_KEY_DERIVATION_INPUT_SEED,
1696 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001697 }
Janos Follathba3fab92019-06-11 14:50:16 +01001698
1699 status = psa_key_derivation_input_key( &operation,
1700 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001701 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001702
Gilles Peskineea0fb492018-07-12 17:17:20 +02001703 if( policy_alg == exercise_alg &&
1704 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001706 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001707 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001708
1709exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001710 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001711 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001712 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001713}
1714/* END_CASE */
1715
1716/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001717void agreement_key_policy( int policy_usage,
1718 int policy_alg,
1719 int key_type_arg,
1720 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001721 int exercise_alg,
1722 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001723{
Ronald Cron5425a212020-08-04 14:58:35 +02001724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001726 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001727 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001728 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001729 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001730
Gilles Peskine8817f612018-12-18 00:18:46 +01001731 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001732
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001733 psa_set_key_usage_flags( &attributes, policy_usage );
1734 psa_set_key_algorithm( &attributes, policy_alg );
1735 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001736
Gilles Peskine049c7532019-05-15 20:22:09 +02001737 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001738 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001739
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001740 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001741 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001742
Steven Cooremance48e852020-10-05 16:02:45 +02001743 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001744
1745exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001746 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001747 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001748 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001749}
1750/* END_CASE */
1751
1752/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001753void key_policy_alg2( int key_type_arg, data_t *key_data,
1754 int usage_arg, int alg_arg, int alg2_arg )
1755{
Ronald Cron5425a212020-08-04 14:58:35 +02001756 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001757 psa_key_type_t key_type = key_type_arg;
1758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1759 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1760 psa_key_usage_t usage = usage_arg;
1761 psa_algorithm_t alg = alg_arg;
1762 psa_algorithm_t alg2 = alg2_arg;
1763
1764 PSA_ASSERT( psa_crypto_init( ) );
1765
1766 psa_set_key_usage_flags( &attributes, usage );
1767 psa_set_key_algorithm( &attributes, alg );
1768 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1769 psa_set_key_type( &attributes, key_type );
1770 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001771 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001772
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001773 /* Update the usage flags to obtain implicit usage flags */
1774 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001775 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001776 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1777 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1778 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1779
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001780 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001781 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001782 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001783 goto exit;
1784
1785exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001786 /*
1787 * Key attributes may have been returned by psa_get_key_attributes()
1788 * thus reset them as required.
1789 */
1790 psa_reset_key_attributes( &got_attributes );
1791
Ronald Cron5425a212020-08-04 14:58:35 +02001792 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001793 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001794}
1795/* END_CASE */
1796
1797/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001798void raw_agreement_key_policy( int policy_usage,
1799 int policy_alg,
1800 int key_type_arg,
1801 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001802 int exercise_alg,
1803 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001804{
Ronald Cron5425a212020-08-04 14:58:35 +02001805 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001807 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001808 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001809 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001810 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001811
1812 PSA_ASSERT( psa_crypto_init( ) );
1813
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001814 psa_set_key_usage_flags( &attributes, policy_usage );
1815 psa_set_key_algorithm( &attributes, policy_alg );
1816 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001817
Gilles Peskine049c7532019-05-15 20:22:09 +02001818 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001819 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001820
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001821 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001822
Steven Cooremance48e852020-10-05 16:02:45 +02001823 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001824
1825exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001826 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001827 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001828 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001829}
1830/* END_CASE */
1831
1832/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001833void copy_success( int source_usage_arg,
1834 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301835 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001836 int type_arg, data_t *material,
1837 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001838 int target_usage_arg,
1839 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301840 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001841 int expected_usage_arg,
1842 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001843{
Gilles Peskineca25db92019-04-19 11:43:08 +02001844 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1845 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001846 psa_key_usage_t expected_usage = expected_usage_arg;
1847 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001848 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301849 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1850 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001851 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1852 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001853 uint8_t *export_buffer = NULL;
1854
Gilles Peskine57ab7212019-01-28 13:03:09 +01001855 PSA_ASSERT( psa_crypto_init( ) );
1856
Gilles Peskineca25db92019-04-19 11:43:08 +02001857 /* Prepare the source key. */
1858 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1859 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001860 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001861 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301862 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001863 PSA_ASSERT( psa_import_key( &source_attributes,
1864 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001865 &source_key ) );
1866 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001867
Gilles Peskineca25db92019-04-19 11:43:08 +02001868 /* Prepare the target attributes. */
1869 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001870 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001871 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001872 }
Archana8a180362021-07-05 02:18:48 +05301873 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001874
Gilles Peskineca25db92019-04-19 11:43:08 +02001875 if( target_usage_arg != -1 )
1876 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1877 if( target_alg_arg != -1 )
1878 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001879 if( target_alg2_arg != -1 )
1880 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001881
Archana8a180362021-07-05 02:18:48 +05301882
Gilles Peskine57ab7212019-01-28 13:03:09 +01001883 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001884 PSA_ASSERT( psa_copy_key( source_key,
1885 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001886
1887 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001888 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001889
1890 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001891 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001892 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1893 psa_get_key_type( &target_attributes ) );
1894 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1895 psa_get_key_bits( &target_attributes ) );
1896 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1897 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001898 TEST_EQUAL( expected_alg2,
1899 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001900 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1901 {
1902 size_t length;
1903 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001904 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001905 material->len, &length ) );
1906 ASSERT_COMPARE( material->x, material->len,
1907 export_buffer, length );
1908 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001909
Archana8a180362021-07-05 02:18:48 +05301910 if( !psa_key_lifetime_is_external( target_lifetime ) )
1911 {
1912 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1913 goto exit;
1914 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1915 goto exit;
1916 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001917
Ronald Cron5425a212020-08-04 14:58:35 +02001918 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001919
1920exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001921 /*
1922 * Source and target key attributes may have been returned by
1923 * psa_get_key_attributes() thus reset them as required.
1924 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001925 psa_reset_key_attributes( &source_attributes );
1926 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001927
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001928 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001929 mbedtls_free( export_buffer );
1930}
1931/* END_CASE */
1932
1933/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001934void copy_fail( int source_usage_arg,
1935 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301936 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001937 int type_arg, data_t *material,
1938 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001939 int target_usage_arg,
1940 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001941 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001942 int expected_status_arg )
1943{
1944 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1945 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001946 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1947 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001948 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001949
1950 PSA_ASSERT( psa_crypto_init( ) );
1951
1952 /* Prepare the source key. */
1953 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1954 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001955 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001956 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301957 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001958 PSA_ASSERT( psa_import_key( &source_attributes,
1959 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001960 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001961
1962 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001963 psa_set_key_id( &target_attributes, key_id );
1964 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001965 psa_set_key_type( &target_attributes, target_type_arg );
1966 psa_set_key_bits( &target_attributes, target_bits_arg );
1967 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1968 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001969 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001970
1971 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001972 TEST_EQUAL( psa_copy_key( source_key,
1973 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001974 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001975
Ronald Cron5425a212020-08-04 14:58:35 +02001976 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001977
Gilles Peskine4a644642019-05-03 17:14:08 +02001978exit:
1979 psa_reset_key_attributes( &source_attributes );
1980 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001981 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001982}
1983/* END_CASE */
1984
1985/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001986void hash_operation_init( )
1987{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001988 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001989 /* Test each valid way of initializing the object, except for `= {0}`, as
1990 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1991 * though it's OK by the C standard. We could test for this, but we'd need
1992 * to supress the Clang warning for the test. */
1993 psa_hash_operation_t func = psa_hash_operation_init( );
1994 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1995 psa_hash_operation_t zero;
1996
1997 memset( &zero, 0, sizeof( zero ) );
1998
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001999 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002000 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2001 PSA_ERROR_BAD_STATE );
2002 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2003 PSA_ERROR_BAD_STATE );
2004 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2005 PSA_ERROR_BAD_STATE );
2006
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002007 /* A default hash operation should be abortable without error. */
2008 PSA_ASSERT( psa_hash_abort( &func ) );
2009 PSA_ASSERT( psa_hash_abort( &init ) );
2010 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002011}
2012/* END_CASE */
2013
2014/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002015void hash_setup( int alg_arg,
2016 int expected_status_arg )
2017{
2018 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002019 uint8_t *output = NULL;
2020 size_t output_size = 0;
2021 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002022 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002023 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002024 psa_status_t status;
2025
Gilles Peskine8817f612018-12-18 00:18:46 +01002026 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002027
Neil Armstrongedb20862022-02-07 15:47:44 +01002028 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002029 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002030 ASSERT_ALLOC( output, output_size );
2031
2032 status = psa_hash_compute( alg, NULL, 0,
2033 output, output_size, &output_length );
2034 TEST_EQUAL( status, expected_status );
2035
2036 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002037 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002038 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002039
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002040 /* Whether setup succeeded or failed, abort must succeed. */
2041 PSA_ASSERT( psa_hash_abort( &operation ) );
2042
2043 /* If setup failed, reproduce the failure, so as to
2044 * test the resulting state of the operation object. */
2045 if( status != PSA_SUCCESS )
2046 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2047
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002048 /* Now the operation object should be reusable. */
2049#if defined(KNOWN_SUPPORTED_HASH_ALG)
2050 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2051 PSA_ASSERT( psa_hash_abort( &operation ) );
2052#endif
2053
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002054exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002055 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002056 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002061void hash_compute_fail( int alg_arg, data_t *input,
2062 int output_size_arg, int expected_status_arg )
2063{
2064 psa_algorithm_t alg = alg_arg;
2065 uint8_t *output = NULL;
2066 size_t output_size = output_size_arg;
2067 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002068 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002069 psa_status_t expected_status = expected_status_arg;
2070 psa_status_t status;
2071
2072 ASSERT_ALLOC( output, output_size );
2073
2074 PSA_ASSERT( psa_crypto_init( ) );
2075
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002076 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002077 status = psa_hash_compute( alg, input->x, input->len,
2078 output, output_size, &output_length );
2079 TEST_EQUAL( status, expected_status );
2080 TEST_ASSERT( output_length <= output_size );
2081
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002082 /* Hash Compute, multi-part */
2083 status = psa_hash_setup( &operation, alg );
2084 if( status == PSA_SUCCESS )
2085 {
2086 status = psa_hash_update( &operation, input->x, input->len );
2087 if( status == PSA_SUCCESS )
2088 {
2089 status = psa_hash_finish( &operation, output, output_size,
2090 &output_length );
2091 if( status == PSA_SUCCESS )
2092 TEST_ASSERT( output_length <= output_size );
2093 else
2094 TEST_EQUAL( status, expected_status );
2095 }
2096 else
2097 {
2098 TEST_EQUAL( status, expected_status );
2099 }
2100 }
2101 else
2102 {
2103 TEST_EQUAL( status, expected_status );
2104 }
2105
Gilles Peskine0a749c82019-11-28 19:33:58 +01002106exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002107 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002108 mbedtls_free( output );
2109 PSA_DONE( );
2110}
2111/* END_CASE */
2112
2113/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002114void hash_compare_fail( int alg_arg, data_t *input,
2115 data_t *reference_hash,
2116 int expected_status_arg )
2117{
2118 psa_algorithm_t alg = alg_arg;
2119 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002120 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002121 psa_status_t status;
2122
2123 PSA_ASSERT( psa_crypto_init( ) );
2124
Neil Armstrong55a1be12022-02-07 11:23:20 +01002125 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002126 status = psa_hash_compare( alg, input->x, input->len,
2127 reference_hash->x, reference_hash->len );
2128 TEST_EQUAL( status, expected_status );
2129
Neil Armstrong55a1be12022-02-07 11:23:20 +01002130 /* Hash Compare, multi-part */
2131 status = psa_hash_setup( &operation, alg );
2132 if( status == PSA_SUCCESS )
2133 {
2134 status = psa_hash_update( &operation, input->x, input->len );
2135 if( status == PSA_SUCCESS )
2136 {
2137 status = psa_hash_verify( &operation, reference_hash->x,
2138 reference_hash->len );
2139 TEST_EQUAL( status, expected_status );
2140 }
2141 else
2142 {
2143 TEST_EQUAL( status, expected_status );
2144 }
2145 }
2146 else
2147 {
2148 TEST_EQUAL( status, expected_status );
2149 }
2150
Gilles Peskine88e08462020-01-28 20:43:00 +01002151exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002152 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002153 PSA_DONE( );
2154}
2155/* END_CASE */
2156
2157/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002158void hash_compute_compare( int alg_arg, data_t *input,
2159 data_t *expected_output )
2160{
2161 psa_algorithm_t alg = alg_arg;
2162 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2163 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002164 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002165 size_t i;
2166
2167 PSA_ASSERT( psa_crypto_init( ) );
2168
Neil Armstrongca30a002022-02-07 11:40:23 +01002169 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002170 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002171 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002172 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002173 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002174 ASSERT_COMPARE( output, output_length,
2175 expected_output->x, expected_output->len );
2176
Neil Armstrongca30a002022-02-07 11:40:23 +01002177 /* Compute with tight buffer, multi-part */
2178 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2179 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2180 PSA_ASSERT( psa_hash_finish( &operation, output,
2181 PSA_HASH_LENGTH( alg ),
2182 &output_length ) );
2183 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2184 ASSERT_COMPARE( output, output_length,
2185 expected_output->x, expected_output->len );
2186
2187 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002188 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2189 output, sizeof( output ),
2190 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002191 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002192 ASSERT_COMPARE( output, output_length,
2193 expected_output->x, expected_output->len );
2194
Neil Armstrongca30a002022-02-07 11:40:23 +01002195 /* Compute with larger buffer, multi-part */
2196 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2197 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2198 PSA_ASSERT( psa_hash_finish( &operation, output,
2199 sizeof( output ), &output_length ) );
2200 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2201 ASSERT_COMPARE( output, output_length,
2202 expected_output->x, expected_output->len );
2203
2204 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002205 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2206 output, output_length ) );
2207
Neil Armstrongca30a002022-02-07 11:40:23 +01002208 /* Compare with correct hash, multi-part */
2209 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2210 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2211 PSA_ASSERT( psa_hash_verify( &operation, output,
2212 output_length ) );
2213
2214 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002215 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2216 output, output_length + 1 ),
2217 PSA_ERROR_INVALID_SIGNATURE );
2218
Neil Armstrongca30a002022-02-07 11:40:23 +01002219 /* Compare with trailing garbage, multi-part */
2220 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2221 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2222 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2223 PSA_ERROR_INVALID_SIGNATURE );
2224
2225 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002226 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2227 output, output_length - 1 ),
2228 PSA_ERROR_INVALID_SIGNATURE );
2229
Neil Armstrongca30a002022-02-07 11:40:23 +01002230 /* Compare with truncated hash, multi-part */
2231 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2232 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2233 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2234 PSA_ERROR_INVALID_SIGNATURE );
2235
Gilles Peskine0a749c82019-11-28 19:33:58 +01002236 /* Compare with corrupted value */
2237 for( i = 0; i < output_length; i++ )
2238 {
Chris Jones9634bb12021-01-20 15:56:42 +00002239 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002240 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002241
2242 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002243 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2244 output, output_length ),
2245 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002246
2247 /* Multi-Part */
2248 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2249 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2250 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2251 PSA_ERROR_INVALID_SIGNATURE );
2252
Gilles Peskine0a749c82019-11-28 19:33:58 +01002253 output[i] ^= 1;
2254 }
2255
2256exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002257 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002258 PSA_DONE( );
2259}
2260/* END_CASE */
2261
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002262/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002263void hash_bad_order( )
2264{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002265 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002266 unsigned char input[] = "";
2267 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002268 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002269 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2270 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2271 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002272 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002273 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002274 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002275
Gilles Peskine8817f612018-12-18 00:18:46 +01002276 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002277
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002278 /* Call setup twice in a row. */
2279 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002280 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002281 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2282 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002283 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002284 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002285 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002286
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002287 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002288 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002289 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002290 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002291
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002292 /* Check that update calls abort on error. */
2293 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002294 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002295 ASSERT_OPERATION_IS_ACTIVE( operation );
2296 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2297 PSA_ERROR_BAD_STATE );
2298 ASSERT_OPERATION_IS_INACTIVE( operation );
2299 PSA_ASSERT( psa_hash_abort( &operation ) );
2300 ASSERT_OPERATION_IS_INACTIVE( operation );
2301
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002302 /* Call update after finish. */
2303 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2304 PSA_ASSERT( psa_hash_finish( &operation,
2305 hash, sizeof( hash ), &hash_len ) );
2306 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002307 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002308 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002309
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002310 /* Call verify without calling setup beforehand. */
2311 TEST_EQUAL( psa_hash_verify( &operation,
2312 valid_hash, sizeof( valid_hash ) ),
2313 PSA_ERROR_BAD_STATE );
2314 PSA_ASSERT( psa_hash_abort( &operation ) );
2315
2316 /* Call verify after finish. */
2317 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2318 PSA_ASSERT( psa_hash_finish( &operation,
2319 hash, sizeof( hash ), &hash_len ) );
2320 TEST_EQUAL( psa_hash_verify( &operation,
2321 valid_hash, sizeof( valid_hash ) ),
2322 PSA_ERROR_BAD_STATE );
2323 PSA_ASSERT( psa_hash_abort( &operation ) );
2324
2325 /* Call verify twice in a row. */
2326 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002327 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002328 PSA_ASSERT( psa_hash_verify( &operation,
2329 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002330 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002331 TEST_EQUAL( psa_hash_verify( &operation,
2332 valid_hash, sizeof( valid_hash ) ),
2333 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002334 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002335 PSA_ASSERT( psa_hash_abort( &operation ) );
2336
2337 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002338 TEST_EQUAL( psa_hash_finish( &operation,
2339 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002340 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002341 PSA_ASSERT( psa_hash_abort( &operation ) );
2342
2343 /* Call finish twice in a row. */
2344 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2345 PSA_ASSERT( psa_hash_finish( &operation,
2346 hash, sizeof( hash ), &hash_len ) );
2347 TEST_EQUAL( psa_hash_finish( &operation,
2348 hash, sizeof( hash ), &hash_len ),
2349 PSA_ERROR_BAD_STATE );
2350 PSA_ASSERT( psa_hash_abort( &operation ) );
2351
2352 /* Call finish after calling verify. */
2353 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2354 PSA_ASSERT( psa_hash_verify( &operation,
2355 valid_hash, sizeof( valid_hash ) ) );
2356 TEST_EQUAL( psa_hash_finish( &operation,
2357 hash, sizeof( hash ), &hash_len ),
2358 PSA_ERROR_BAD_STATE );
2359 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002360
2361exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002362 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002363}
2364/* END_CASE */
2365
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002366/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002367void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002368{
2369 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002370 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2371 * appended to it */
2372 unsigned char hash[] = {
2373 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2374 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2375 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002376 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002377 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002378
Gilles Peskine8817f612018-12-18 00:18:46 +01002379 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002380
itayzafrir27e69452018-11-01 14:26:34 +02002381 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002382 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002383 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002384 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002385 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002386 ASSERT_OPERATION_IS_INACTIVE( operation );
2387 PSA_ASSERT( psa_hash_abort( &operation ) );
2388 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002389
itayzafrir27e69452018-11-01 14:26:34 +02002390 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002391 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002392 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002393 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002394
itayzafrir27e69452018-11-01 14:26:34 +02002395 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002396 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002397 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002398 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002399
itayzafrirec93d302018-10-18 18:01:10 +03002400exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002401 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002402}
2403/* END_CASE */
2404
Ronald Cronee414c72021-03-18 18:50:08 +01002405/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002406void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002407{
2408 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002409 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002410 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002411 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002412 size_t hash_len;
2413
Gilles Peskine8817f612018-12-18 00:18:46 +01002414 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002415
itayzafrir58028322018-10-25 10:22:01 +03002416 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002417 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002418 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002419 hash, expected_size - 1, &hash_len ),
2420 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002421
2422exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002423 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002424}
2425/* END_CASE */
2426
Ronald Cronee414c72021-03-18 18:50:08 +01002427/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002428void hash_clone_source_state( )
2429{
2430 psa_algorithm_t alg = PSA_ALG_SHA_256;
2431 unsigned char hash[PSA_HASH_MAX_SIZE];
2432 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2433 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2434 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2435 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2436 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2437 size_t hash_len;
2438
2439 PSA_ASSERT( psa_crypto_init( ) );
2440 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2441
2442 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2443 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2444 PSA_ASSERT( psa_hash_finish( &op_finished,
2445 hash, sizeof( hash ), &hash_len ) );
2446 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2447 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2448
2449 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2450 PSA_ERROR_BAD_STATE );
2451
2452 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2453 PSA_ASSERT( psa_hash_finish( &op_init,
2454 hash, sizeof( hash ), &hash_len ) );
2455 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2456 PSA_ASSERT( psa_hash_finish( &op_finished,
2457 hash, sizeof( hash ), &hash_len ) );
2458 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2459 PSA_ASSERT( psa_hash_finish( &op_aborted,
2460 hash, sizeof( hash ), &hash_len ) );
2461
2462exit:
2463 psa_hash_abort( &op_source );
2464 psa_hash_abort( &op_init );
2465 psa_hash_abort( &op_setup );
2466 psa_hash_abort( &op_finished );
2467 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002468 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002469}
2470/* END_CASE */
2471
Ronald Cronee414c72021-03-18 18:50:08 +01002472/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002473void hash_clone_target_state( )
2474{
2475 psa_algorithm_t alg = PSA_ALG_SHA_256;
2476 unsigned char hash[PSA_HASH_MAX_SIZE];
2477 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2478 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2479 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2480 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2481 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2482 size_t hash_len;
2483
2484 PSA_ASSERT( psa_crypto_init( ) );
2485
2486 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2487 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2488 PSA_ASSERT( psa_hash_finish( &op_finished,
2489 hash, sizeof( hash ), &hash_len ) );
2490 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2491 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2492
2493 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2494 PSA_ASSERT( psa_hash_finish( &op_target,
2495 hash, sizeof( hash ), &hash_len ) );
2496
2497 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2498 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2499 PSA_ERROR_BAD_STATE );
2500 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2501 PSA_ERROR_BAD_STATE );
2502
2503exit:
2504 psa_hash_abort( &op_target );
2505 psa_hash_abort( &op_init );
2506 psa_hash_abort( &op_setup );
2507 psa_hash_abort( &op_finished );
2508 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002509 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002510}
2511/* END_CASE */
2512
itayzafrir58028322018-10-25 10:22:01 +03002513/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002514void mac_operation_init( )
2515{
Jaeden Amero252ef282019-02-15 14:05:35 +00002516 const uint8_t input[1] = { 0 };
2517
Jaeden Amero769ce272019-01-04 11:48:03 +00002518 /* Test each valid way of initializing the object, except for `= {0}`, as
2519 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2520 * though it's OK by the C standard. We could test for this, but we'd need
2521 * to supress the Clang warning for the test. */
2522 psa_mac_operation_t func = psa_mac_operation_init( );
2523 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2524 psa_mac_operation_t zero;
2525
2526 memset( &zero, 0, sizeof( zero ) );
2527
Jaeden Amero252ef282019-02-15 14:05:35 +00002528 /* A freshly-initialized MAC operation should not be usable. */
2529 TEST_EQUAL( psa_mac_update( &func,
2530 input, sizeof( input ) ),
2531 PSA_ERROR_BAD_STATE );
2532 TEST_EQUAL( psa_mac_update( &init,
2533 input, sizeof( input ) ),
2534 PSA_ERROR_BAD_STATE );
2535 TEST_EQUAL( psa_mac_update( &zero,
2536 input, sizeof( input ) ),
2537 PSA_ERROR_BAD_STATE );
2538
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002539 /* A default MAC operation should be abortable without error. */
2540 PSA_ASSERT( psa_mac_abort( &func ) );
2541 PSA_ASSERT( psa_mac_abort( &init ) );
2542 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002543}
2544/* END_CASE */
2545
2546/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002547void mac_setup( int key_type_arg,
2548 data_t *key,
2549 int alg_arg,
2550 int expected_status_arg )
2551{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002552 psa_key_type_t key_type = key_type_arg;
2553 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002554 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002555 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002556 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2557#if defined(KNOWN_SUPPORTED_MAC_ALG)
2558 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2559#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002560
Gilles Peskine8817f612018-12-18 00:18:46 +01002561 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002562
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002563 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2564 &operation, &status ) )
2565 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002566 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002567
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002568 /* The operation object should be reusable. */
2569#if defined(KNOWN_SUPPORTED_MAC_ALG)
2570 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2571 smoke_test_key_data,
2572 sizeof( smoke_test_key_data ),
2573 KNOWN_SUPPORTED_MAC_ALG,
2574 &operation, &status ) )
2575 goto exit;
2576 TEST_EQUAL( status, PSA_SUCCESS );
2577#endif
2578
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002579exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002580 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002581}
2582/* END_CASE */
2583
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002584/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00002585void mac_bad_order( )
2586{
Ronald Cron5425a212020-08-04 14:58:35 +02002587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002588 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2589 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002590 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002591 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2592 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2593 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002594 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002595 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2596 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2597 size_t sign_mac_length = 0;
2598 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2599 const uint8_t verify_mac[] = {
2600 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2601 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2602 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2603
2604 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002605 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002606 psa_set_key_algorithm( &attributes, alg );
2607 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002608
Ronald Cron5425a212020-08-04 14:58:35 +02002609 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2610 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002611
Jaeden Amero252ef282019-02-15 14:05:35 +00002612 /* Call update without calling setup beforehand. */
2613 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2614 PSA_ERROR_BAD_STATE );
2615 PSA_ASSERT( psa_mac_abort( &operation ) );
2616
2617 /* Call sign finish without calling setup beforehand. */
2618 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2619 &sign_mac_length),
2620 PSA_ERROR_BAD_STATE );
2621 PSA_ASSERT( psa_mac_abort( &operation ) );
2622
2623 /* Call verify finish without calling setup beforehand. */
2624 TEST_EQUAL( psa_mac_verify_finish( &operation,
2625 verify_mac, sizeof( verify_mac ) ),
2626 PSA_ERROR_BAD_STATE );
2627 PSA_ASSERT( psa_mac_abort( &operation ) );
2628
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002629 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002630 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002631 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002632 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002633 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002634 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002635 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002636 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002637
Jaeden Amero252ef282019-02-15 14:05:35 +00002638 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002639 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002640 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2641 PSA_ASSERT( psa_mac_sign_finish( &operation,
2642 sign_mac, sizeof( sign_mac ),
2643 &sign_mac_length ) );
2644 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2645 PSA_ERROR_BAD_STATE );
2646 PSA_ASSERT( psa_mac_abort( &operation ) );
2647
2648 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002649 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002650 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2651 PSA_ASSERT( psa_mac_verify_finish( &operation,
2652 verify_mac, sizeof( verify_mac ) ) );
2653 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2654 PSA_ERROR_BAD_STATE );
2655 PSA_ASSERT( psa_mac_abort( &operation ) );
2656
2657 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002658 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002659 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2660 PSA_ASSERT( psa_mac_sign_finish( &operation,
2661 sign_mac, sizeof( sign_mac ),
2662 &sign_mac_length ) );
2663 TEST_EQUAL( psa_mac_sign_finish( &operation,
2664 sign_mac, sizeof( sign_mac ),
2665 &sign_mac_length ),
2666 PSA_ERROR_BAD_STATE );
2667 PSA_ASSERT( psa_mac_abort( &operation ) );
2668
2669 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002670 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002671 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2672 PSA_ASSERT( psa_mac_verify_finish( &operation,
2673 verify_mac, sizeof( verify_mac ) ) );
2674 TEST_EQUAL( psa_mac_verify_finish( &operation,
2675 verify_mac, sizeof( verify_mac ) ),
2676 PSA_ERROR_BAD_STATE );
2677 PSA_ASSERT( psa_mac_abort( &operation ) );
2678
2679 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002680 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002681 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002682 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002683 TEST_EQUAL( psa_mac_verify_finish( &operation,
2684 verify_mac, sizeof( verify_mac ) ),
2685 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002686 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002687 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002688 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002689
2690 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002691 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002692 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002693 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002694 TEST_EQUAL( psa_mac_sign_finish( &operation,
2695 sign_mac, sizeof( sign_mac ),
2696 &sign_mac_length ),
2697 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002698 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002699 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002700 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002701
Ronald Cron5425a212020-08-04 14:58:35 +02002702 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002703
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002704exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002705 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002706}
2707/* END_CASE */
2708
2709/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01002710void mac_sign_verify_multi( int key_type_arg,
2711 data_t *key_data,
2712 int alg_arg,
2713 data_t *input,
2714 int is_verify,
2715 data_t *expected_mac )
2716{
2717 size_t data_part_len = 0;
2718
2719 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
2720 {
2721 /* Split data into length(data_part_len) parts. */
2722 mbedtls_test_set_step( 2000 + data_part_len );
2723
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01002724 if( mac_multipart_internal_func( key_type_arg, key_data,
2725 alg_arg,
2726 input, data_part_len,
2727 expected_mac,
2728 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01002729 break;
2730
2731 /* length(0) part, length(data_part_len) part, length(0) part... */
2732 mbedtls_test_set_step( 3000 + data_part_len );
2733
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01002734 if( mac_multipart_internal_func( key_type_arg, key_data,
2735 alg_arg,
2736 input, data_part_len,
2737 expected_mac,
2738 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01002739 break;
2740 }
2741
2742 /* Goto is required to silence warnings about unused labels, as we
2743 * don't actually do any test assertions in this function. */
2744 goto exit;
2745}
2746/* END_CASE */
2747
2748/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002749void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002750 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002751 int alg_arg,
2752 data_t *input,
2753 data_t *expected_mac )
2754{
Ronald Cron5425a212020-08-04 14:58:35 +02002755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002756 psa_key_type_t key_type = key_type_arg;
2757 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002758 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002760 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002761 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002762 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002763 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002764 const size_t output_sizes_to_test[] = {
2765 0,
2766 1,
2767 expected_mac->len - 1,
2768 expected_mac->len,
2769 expected_mac->len + 1,
2770 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002771
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002772 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002773 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002774 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002775
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002777
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002779 psa_set_key_algorithm( &attributes, alg );
2780 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002781
Ronald Cron5425a212020-08-04 14:58:35 +02002782 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2783 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002784
Gilles Peskine8b356b52020-08-25 23:44:59 +02002785 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2786 {
2787 const size_t output_size = output_sizes_to_test[i];
2788 psa_status_t expected_status =
2789 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2790 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002791
Chris Jones9634bb12021-01-20 15:56:42 +00002792 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002793 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002794
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002795 /* Calculate the MAC, one-shot case. */
2796 TEST_EQUAL( psa_mac_compute( key, alg,
2797 input->x, input->len,
2798 actual_mac, output_size, &mac_length ),
2799 expected_status );
2800 if( expected_status == PSA_SUCCESS )
2801 {
2802 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2803 actual_mac, mac_length );
2804 }
2805
2806 if( output_size > 0 )
2807 memset( actual_mac, 0, output_size );
2808
2809 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002810 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002811 PSA_ASSERT( psa_mac_update( &operation,
2812 input->x, input->len ) );
2813 TEST_EQUAL( psa_mac_sign_finish( &operation,
2814 actual_mac, output_size,
2815 &mac_length ),
2816 expected_status );
2817 PSA_ASSERT( psa_mac_abort( &operation ) );
2818
2819 if( expected_status == PSA_SUCCESS )
2820 {
2821 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2822 actual_mac, mac_length );
2823 }
2824 mbedtls_free( actual_mac );
2825 actual_mac = NULL;
2826 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002827
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002828exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002829 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002830 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002831 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002832 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002833}
2834/* END_CASE */
2835
2836/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002837void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002838 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002839 int alg_arg,
2840 data_t *input,
2841 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002842{
Ronald Cron5425a212020-08-04 14:58:35 +02002843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844 psa_key_type_t key_type = key_type_arg;
2845 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002846 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002848 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002849
Gilles Peskine69c12672018-06-28 00:07:19 +02002850 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2851
Gilles Peskine8817f612018-12-18 00:18:46 +01002852 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002854 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002855 psa_set_key_algorithm( &attributes, alg );
2856 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002857
Ronald Cron5425a212020-08-04 14:58:35 +02002858 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2859 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002860
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002861 /* Verify correct MAC, one-shot case. */
2862 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2863 expected_mac->x, expected_mac->len ) );
2864
2865 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002866 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002867 PSA_ASSERT( psa_mac_update( &operation,
2868 input->x, input->len ) );
2869 PSA_ASSERT( psa_mac_verify_finish( &operation,
2870 expected_mac->x,
2871 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002873 /* Test a MAC that's too short, one-shot case. */
2874 TEST_EQUAL( psa_mac_verify( key, alg,
2875 input->x, input->len,
2876 expected_mac->x,
2877 expected_mac->len - 1 ),
2878 PSA_ERROR_INVALID_SIGNATURE );
2879
2880 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002881 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002882 PSA_ASSERT( psa_mac_update( &operation,
2883 input->x, input->len ) );
2884 TEST_EQUAL( psa_mac_verify_finish( &operation,
2885 expected_mac->x,
2886 expected_mac->len - 1 ),
2887 PSA_ERROR_INVALID_SIGNATURE );
2888
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002889 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002890 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2891 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002892 TEST_EQUAL( psa_mac_verify( key, alg,
2893 input->x, input->len,
2894 perturbed_mac, expected_mac->len + 1 ),
2895 PSA_ERROR_INVALID_SIGNATURE );
2896
2897 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002898 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002899 PSA_ASSERT( psa_mac_update( &operation,
2900 input->x, input->len ) );
2901 TEST_EQUAL( psa_mac_verify_finish( &operation,
2902 perturbed_mac,
2903 expected_mac->len + 1 ),
2904 PSA_ERROR_INVALID_SIGNATURE );
2905
2906 /* Test changing one byte. */
2907 for( size_t i = 0; i < expected_mac->len; i++ )
2908 {
Chris Jones9634bb12021-01-20 15:56:42 +00002909 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002910 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002911
2912 TEST_EQUAL( psa_mac_verify( key, alg,
2913 input->x, input->len,
2914 perturbed_mac, expected_mac->len ),
2915 PSA_ERROR_INVALID_SIGNATURE );
2916
Ronald Cron5425a212020-08-04 14:58:35 +02002917 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002918 PSA_ASSERT( psa_mac_update( &operation,
2919 input->x, input->len ) );
2920 TEST_EQUAL( psa_mac_verify_finish( &operation,
2921 perturbed_mac,
2922 expected_mac->len ),
2923 PSA_ERROR_INVALID_SIGNATURE );
2924 perturbed_mac[i] ^= 1;
2925 }
2926
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002928 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002929 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002930 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002931 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002932}
2933/* END_CASE */
2934
2935/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002936void cipher_operation_init( )
2937{
Jaeden Ameroab439972019-02-15 14:12:05 +00002938 const uint8_t input[1] = { 0 };
2939 unsigned char output[1] = { 0 };
2940 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002941 /* Test each valid way of initializing the object, except for `= {0}`, as
2942 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2943 * though it's OK by the C standard. We could test for this, but we'd need
2944 * to supress the Clang warning for the test. */
2945 psa_cipher_operation_t func = psa_cipher_operation_init( );
2946 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2947 psa_cipher_operation_t zero;
2948
2949 memset( &zero, 0, sizeof( zero ) );
2950
Jaeden Ameroab439972019-02-15 14:12:05 +00002951 /* A freshly-initialized cipher operation should not be usable. */
2952 TEST_EQUAL( psa_cipher_update( &func,
2953 input, sizeof( input ),
2954 output, sizeof( output ),
2955 &output_length ),
2956 PSA_ERROR_BAD_STATE );
2957 TEST_EQUAL( psa_cipher_update( &init,
2958 input, sizeof( input ),
2959 output, sizeof( output ),
2960 &output_length ),
2961 PSA_ERROR_BAD_STATE );
2962 TEST_EQUAL( psa_cipher_update( &zero,
2963 input, sizeof( input ),
2964 output, sizeof( output ),
2965 &output_length ),
2966 PSA_ERROR_BAD_STATE );
2967
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002968 /* A default cipher operation should be abortable without error. */
2969 PSA_ASSERT( psa_cipher_abort( &func ) );
2970 PSA_ASSERT( psa_cipher_abort( &init ) );
2971 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002972}
2973/* END_CASE */
2974
2975/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002976void cipher_setup( int key_type_arg,
2977 data_t *key,
2978 int alg_arg,
2979 int expected_status_arg )
2980{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002981 psa_key_type_t key_type = key_type_arg;
2982 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002983 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002984 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002985 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002986#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002987 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2988#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002989
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002991
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002992 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2993 &operation, &status ) )
2994 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002995 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002996
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002997 /* The operation object should be reusable. */
2998#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2999 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3000 smoke_test_key_data,
3001 sizeof( smoke_test_key_data ),
3002 KNOWN_SUPPORTED_CIPHER_ALG,
3003 &operation, &status ) )
3004 goto exit;
3005 TEST_EQUAL( status, PSA_SUCCESS );
3006#endif
3007
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003008exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003009 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003010 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003011}
3012/* END_CASE */
3013
Ronald Cronee414c72021-03-18 18:50:08 +01003014/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003015void cipher_bad_order( )
3016{
Ronald Cron5425a212020-08-04 14:58:35 +02003017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003018 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3019 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003021 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003022 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003023 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003024 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3025 0xaa, 0xaa, 0xaa, 0xaa };
3026 const uint8_t text[] = {
3027 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3028 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003029 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003030 size_t length = 0;
3031
3032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3034 psa_set_key_algorithm( &attributes, alg );
3035 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003036 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3037 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003038
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003039 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003040 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003041 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003042 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003043 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003044 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003045 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003046 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003047
3048 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003049 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003050 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003051 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003052 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003053 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003054 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003055 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003056
Jaeden Ameroab439972019-02-15 14:12:05 +00003057 /* Generate an IV without calling setup beforehand. */
3058 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3059 buffer, sizeof( buffer ),
3060 &length ),
3061 PSA_ERROR_BAD_STATE );
3062 PSA_ASSERT( psa_cipher_abort( &operation ) );
3063
3064 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003065 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003066 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3067 buffer, sizeof( buffer ),
3068 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003069 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003070 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3071 buffer, sizeof( buffer ),
3072 &length ),
3073 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003074 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003075 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003076 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003077
3078 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003079 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003080 PSA_ASSERT( psa_cipher_set_iv( &operation,
3081 iv, sizeof( iv ) ) );
3082 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3083 buffer, sizeof( buffer ),
3084 &length ),
3085 PSA_ERROR_BAD_STATE );
3086 PSA_ASSERT( psa_cipher_abort( &operation ) );
3087
3088 /* Set an IV without calling setup beforehand. */
3089 TEST_EQUAL( psa_cipher_set_iv( &operation,
3090 iv, sizeof( iv ) ),
3091 PSA_ERROR_BAD_STATE );
3092 PSA_ASSERT( psa_cipher_abort( &operation ) );
3093
3094 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003095 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003096 PSA_ASSERT( psa_cipher_set_iv( &operation,
3097 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003098 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003099 TEST_EQUAL( psa_cipher_set_iv( &operation,
3100 iv, sizeof( iv ) ),
3101 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003102 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003103 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003104 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003105
3106 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003107 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003108 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3109 buffer, sizeof( buffer ),
3110 &length ) );
3111 TEST_EQUAL( psa_cipher_set_iv( &operation,
3112 iv, sizeof( iv ) ),
3113 PSA_ERROR_BAD_STATE );
3114 PSA_ASSERT( psa_cipher_abort( &operation ) );
3115
3116 /* Call update without calling setup beforehand. */
3117 TEST_EQUAL( psa_cipher_update( &operation,
3118 text, sizeof( text ),
3119 buffer, sizeof( buffer ),
3120 &length ),
3121 PSA_ERROR_BAD_STATE );
3122 PSA_ASSERT( psa_cipher_abort( &operation ) );
3123
3124 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003125 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003126 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003127 TEST_EQUAL( psa_cipher_update( &operation,
3128 text, sizeof( text ),
3129 buffer, sizeof( buffer ),
3130 &length ),
3131 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003132 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003133 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003134 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003135
3136 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003137 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003138 PSA_ASSERT( psa_cipher_set_iv( &operation,
3139 iv, sizeof( iv ) ) );
3140 PSA_ASSERT( psa_cipher_finish( &operation,
3141 buffer, sizeof( buffer ), &length ) );
3142 TEST_EQUAL( psa_cipher_update( &operation,
3143 text, sizeof( text ),
3144 buffer, sizeof( buffer ),
3145 &length ),
3146 PSA_ERROR_BAD_STATE );
3147 PSA_ASSERT( psa_cipher_abort( &operation ) );
3148
3149 /* Call finish without calling setup beforehand. */
3150 TEST_EQUAL( psa_cipher_finish( &operation,
3151 buffer, sizeof( buffer ), &length ),
3152 PSA_ERROR_BAD_STATE );
3153 PSA_ASSERT( psa_cipher_abort( &operation ) );
3154
3155 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003156 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003157 /* Not calling update means we are encrypting an empty buffer, which is OK
3158 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003159 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003160 TEST_EQUAL( psa_cipher_finish( &operation,
3161 buffer, sizeof( buffer ), &length ),
3162 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003163 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003164 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003165 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003166
3167 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003168 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003169 PSA_ASSERT( psa_cipher_set_iv( &operation,
3170 iv, sizeof( iv ) ) );
3171 PSA_ASSERT( psa_cipher_finish( &operation,
3172 buffer, sizeof( buffer ), &length ) );
3173 TEST_EQUAL( psa_cipher_finish( &operation,
3174 buffer, sizeof( buffer ), &length ),
3175 PSA_ERROR_BAD_STATE );
3176 PSA_ASSERT( psa_cipher_abort( &operation ) );
3177
Ronald Cron5425a212020-08-04 14:58:35 +02003178 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003179
Jaeden Ameroab439972019-02-15 14:12:05 +00003180exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003181 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003182 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003183}
3184/* END_CASE */
3185
3186/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003187void cipher_encrypt_fail( int alg_arg,
3188 int key_type_arg,
3189 data_t *key_data,
3190 data_t *input,
3191 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003192{
Ronald Cron5425a212020-08-04 14:58:35 +02003193 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003194 psa_status_t status;
3195 psa_key_type_t key_type = key_type_arg;
3196 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003197 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003198 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3199 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3200 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003201 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003202 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003203 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003204 size_t function_output_length;
3205 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3207
3208 if ( PSA_ERROR_BAD_STATE != expected_status )
3209 {
3210 PSA_ASSERT( psa_crypto_init( ) );
3211
3212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3213 psa_set_key_algorithm( &attributes, alg );
3214 psa_set_key_type( &attributes, key_type );
3215
3216 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3217 input->len );
3218 ASSERT_ALLOC( output, output_buffer_size );
3219
3220 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3221 &key ) );
3222 }
3223
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003224 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003225 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3226 output_buffer_size, &output_length );
3227
3228 TEST_EQUAL( status, expected_status );
3229
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003230 /* Encrypt, multi-part */
3231 status = psa_cipher_encrypt_setup( &operation, key, alg );
3232 if( status == PSA_SUCCESS )
3233 {
3234 if( alg != PSA_ALG_ECB_NO_PADDING )
3235 {
3236 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3237 iv, iv_size,
3238 &iv_length ) );
3239 }
3240
3241 status = psa_cipher_update( &operation, input->x, input->len,
3242 output, output_buffer_size,
3243 &function_output_length );
3244 if( status == PSA_SUCCESS )
3245 {
3246 output_length += function_output_length;
3247
3248 status = psa_cipher_finish( &operation, output + output_length,
3249 output_buffer_size - output_length,
3250 &function_output_length );
3251
3252 TEST_EQUAL( status, expected_status );
3253 }
3254 else
3255 {
3256 TEST_EQUAL( status, expected_status );
3257 }
3258 }
3259 else
3260 {
3261 TEST_EQUAL( status, expected_status );
3262 }
3263
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003264exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003265 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003266 mbedtls_free( output );
3267 psa_destroy_key( key );
3268 PSA_DONE( );
3269}
3270/* END_CASE */
3271
3272/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003273void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3274 data_t *input, int iv_length,
3275 int expected_result )
3276{
3277 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3278 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3280 size_t output_buffer_size = 0;
3281 unsigned char *output = NULL;
3282
3283 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3284 ASSERT_ALLOC( output, output_buffer_size );
3285
3286 PSA_ASSERT( psa_crypto_init( ) );
3287
3288 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3289 psa_set_key_algorithm( &attributes, alg );
3290 psa_set_key_type( &attributes, key_type );
3291
3292 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3293 &key ) );
3294 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3295 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3296 iv_length ) );
3297
3298exit:
3299 psa_cipher_abort( &operation );
3300 mbedtls_free( output );
3301 psa_destroy_key( key );
3302 PSA_DONE( );
3303}
3304/* END_CASE */
3305
3306/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003307void cipher_encrypt_alg_without_iv( int alg_arg,
3308 int key_type_arg,
3309 data_t *key_data,
3310 data_t *input,
3311 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003312{
3313 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3314 psa_key_type_t key_type = key_type_arg;
3315 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003316 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3317 uint8_t iv[1] = { 0x5a };
3318 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003319 unsigned char *output = NULL;
3320 size_t output_buffer_size = 0;
3321 size_t output_length = 0;
3322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3323
3324 PSA_ASSERT( psa_crypto_init( ) );
3325
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003326 /* Validate size macros */
3327 TEST_ASSERT( expected_output->len <=
3328 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3329 TEST_ASSERT( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) <=
3330 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3331
3332 /* Set up key and output buffer */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003333 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3334 psa_set_key_algorithm( &attributes, alg );
3335 psa_set_key_type( &attributes, key_type );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003336 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3337 &key ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003338 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3339 ASSERT_ALLOC( output, output_buffer_size );
3340
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003341 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003342 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3343 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3344 PSA_ERROR_BAD_STATE );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003345
3346 /* generate_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003347 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3348 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
3349 &iv_length ),
3350 PSA_ERROR_BAD_STATE );
3351
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003352 /* One-shot encryption */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003353 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
3354 output_buffer_size, &output_length ) );
3355 TEST_ASSERT( output_length <=
3356 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3357 TEST_ASSERT( output_length <=
3358 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3359
3360 ASSERT_COMPARE( expected_output->x, expected_output->len,
3361 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003362
3363 /* Encrypt, multi-part */
3364 PSA_ASSERT( psa_cipher_abort( &operation ) );
3365 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3366
3367 PSA_ASSERT( psa_cipher_update( &operation, input->x, input->len,
3368 output, output_buffer_size,
3369 &output_length) );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003370
3371 ASSERT_COMPARE( expected_output->x, expected_output->len,
3372 output, output_length );
3373
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003374exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003375 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003376 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003377 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003378 psa_destroy_key( key );
3379 PSA_DONE( );
3380}
3381/* END_CASE */
3382
3383/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003384void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3385{
3386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3387 psa_algorithm_t alg = alg_arg;
3388 psa_key_type_t key_type = key_type_arg;
3389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3390 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3391 psa_status_t status;
3392
3393 PSA_ASSERT( psa_crypto_init( ) );
3394
3395 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3396 psa_set_key_algorithm( &attributes, alg );
3397 psa_set_key_type( &attributes, key_type );
3398
3399 /* Usage of either of these two size macros would cause divide by zero
3400 * with incorrect key types previously. Input length should be irrelevant
3401 * here. */
3402 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3403 0 );
3404 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3405
3406
3407 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3408 &key ) );
3409
3410 /* Should fail due to invalid alg type (to support invalid key type).
3411 * Encrypt or decrypt will end up in the same place. */
3412 status = psa_cipher_encrypt_setup( &operation, key, alg );
3413
3414 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3415
3416exit:
3417 psa_cipher_abort( &operation );
3418 psa_destroy_key( key );
3419 PSA_DONE( );
3420}
3421/* END_CASE */
3422
3423/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003424void cipher_encrypt_validation( int alg_arg,
3425 int key_type_arg,
3426 data_t *key_data,
3427 data_t *input )
3428{
3429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3430 psa_key_type_t key_type = key_type_arg;
3431 psa_algorithm_t alg = alg_arg;
3432 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3433 unsigned char *output1 = NULL;
3434 size_t output1_buffer_size = 0;
3435 size_t output1_length = 0;
3436 unsigned char *output2 = NULL;
3437 size_t output2_buffer_size = 0;
3438 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003439 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003440 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003442
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003444
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003445 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3446 psa_set_key_algorithm( &attributes, alg );
3447 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003448
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003449 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3450 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3451 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3452 ASSERT_ALLOC( output1, output1_buffer_size );
3453 ASSERT_ALLOC( output2, output2_buffer_size );
3454
Ronald Cron5425a212020-08-04 14:58:35 +02003455 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3456 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003457
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003458 /* The one-shot cipher encryption uses generated iv so validating
3459 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003460 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3461 output1_buffer_size, &output1_length ) );
3462 TEST_ASSERT( output1_length <=
3463 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3464 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003465 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003466
3467 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3468 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003469
Gilles Peskine8817f612018-12-18 00:18:46 +01003470 PSA_ASSERT( psa_cipher_update( &operation,
3471 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003472 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003473 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003474 TEST_ASSERT( function_output_length <=
3475 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3476 TEST_ASSERT( function_output_length <=
3477 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003478 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003479
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003480 PSA_ASSERT( psa_cipher_finish( &operation,
3481 output2 + output2_length,
3482 output2_buffer_size - output2_length,
3483 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003484 TEST_ASSERT( function_output_length <=
3485 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3486 TEST_ASSERT( function_output_length <=
3487 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003488 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003489
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003490 PSA_ASSERT( psa_cipher_abort( &operation ) );
3491 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3492 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003493
Gilles Peskine50e586b2018-06-08 14:28:46 +02003494exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003495 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003496 mbedtls_free( output1 );
3497 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003498 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003499 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003500}
3501/* END_CASE */
3502
3503/* BEGIN_CASE */
3504void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003505 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003506 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003507 int first_part_size_arg,
3508 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003509 data_t *expected_output,
3510 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003511{
Ronald Cron5425a212020-08-04 14:58:35 +02003512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003513 psa_key_type_t key_type = key_type_arg;
3514 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003515 psa_status_t status;
3516 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003517 size_t first_part_size = first_part_size_arg;
3518 size_t output1_length = output1_length_arg;
3519 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003520 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003521 size_t output_buffer_size = 0;
3522 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003523 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003524 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003525 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003526
Gilles Peskine8817f612018-12-18 00:18:46 +01003527 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003528
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003529 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3530 psa_set_key_algorithm( &attributes, alg );
3531 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003532
Ronald Cron5425a212020-08-04 14:58:35 +02003533 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3534 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003535
Ronald Cron5425a212020-08-04 14:58:35 +02003536 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003537
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003538 if( iv->len > 0 )
3539 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003540 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003541 }
3542
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003543 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3544 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003545 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003546
Gilles Peskinee0866522019-02-19 19:44:00 +01003547 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003548 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3549 output, output_buffer_size,
3550 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003551 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003552 TEST_ASSERT( function_output_length <=
3553 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3554 TEST_ASSERT( function_output_length <=
3555 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003556 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003557
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003558 if( first_part_size < input->len )
3559 {
3560 PSA_ASSERT( psa_cipher_update( &operation,
3561 input->x + first_part_size,
3562 input->len - first_part_size,
3563 ( output_buffer_size == 0 ? NULL :
3564 output + total_output_length ),
3565 output_buffer_size - total_output_length,
3566 &function_output_length ) );
3567 TEST_ASSERT( function_output_length == output2_length );
3568 TEST_ASSERT( function_output_length <=
3569 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3570 alg,
3571 input->len - first_part_size ) );
3572 TEST_ASSERT( function_output_length <=
3573 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3574 total_output_length += function_output_length;
3575 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003576
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003577 status = psa_cipher_finish( &operation,
3578 ( output_buffer_size == 0 ? NULL :
3579 output + total_output_length ),
3580 output_buffer_size - total_output_length,
3581 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003582 TEST_ASSERT( function_output_length <=
3583 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3584 TEST_ASSERT( function_output_length <=
3585 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003586 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003587 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003588
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003589 if( expected_status == PSA_SUCCESS )
3590 {
3591 PSA_ASSERT( psa_cipher_abort( &operation ) );
3592
3593 ASSERT_COMPARE( expected_output->x, expected_output->len,
3594 output, total_output_length );
3595 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003596
3597exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003598 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003599 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003600 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003601 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003602}
3603/* END_CASE */
3604
3605/* BEGIN_CASE */
3606void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003607 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003608 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003609 int first_part_size_arg,
3610 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003611 data_t *expected_output,
3612 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003613{
Ronald Cron5425a212020-08-04 14:58:35 +02003614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003615 psa_key_type_t key_type = key_type_arg;
3616 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003617 psa_status_t status;
3618 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003619 size_t first_part_size = first_part_size_arg;
3620 size_t output1_length = output1_length_arg;
3621 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003622 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003623 size_t output_buffer_size = 0;
3624 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003625 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003626 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003627 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003628
Gilles Peskine8817f612018-12-18 00:18:46 +01003629 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003630
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003631 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3632 psa_set_key_algorithm( &attributes, alg );
3633 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003634
Ronald Cron5425a212020-08-04 14:58:35 +02003635 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3636 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003637
Ronald Cron5425a212020-08-04 14:58:35 +02003638 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003639
Steven Cooreman177deba2020-09-07 17:14:14 +02003640 if( iv->len > 0 )
3641 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003642 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003643 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003644
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003645 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3646 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003647 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003648
Gilles Peskinee0866522019-02-19 19:44:00 +01003649 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003650 PSA_ASSERT( psa_cipher_update( &operation,
3651 input->x, first_part_size,
3652 output, output_buffer_size,
3653 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003654 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003655 TEST_ASSERT( function_output_length <=
3656 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3657 TEST_ASSERT( function_output_length <=
3658 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003659 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003660
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003661 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003662 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003663 PSA_ASSERT( psa_cipher_update( &operation,
3664 input->x + first_part_size,
3665 input->len - first_part_size,
3666 ( output_buffer_size == 0 ? NULL :
3667 output + total_output_length ),
3668 output_buffer_size - total_output_length,
3669 &function_output_length ) );
3670 TEST_ASSERT( function_output_length == output2_length );
3671 TEST_ASSERT( function_output_length <=
3672 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3673 alg,
3674 input->len - first_part_size ) );
3675 TEST_ASSERT( function_output_length <=
3676 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3677 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003678 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003679
Gilles Peskine50e586b2018-06-08 14:28:46 +02003680 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003681 ( output_buffer_size == 0 ? NULL :
3682 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003683 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003684 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003685 TEST_ASSERT( function_output_length <=
3686 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3687 TEST_ASSERT( function_output_length <=
3688 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003689 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003690 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003691
3692 if( expected_status == PSA_SUCCESS )
3693 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003694 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003695
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003696 ASSERT_COMPARE( expected_output->x, expected_output->len,
3697 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003698 }
3699
Gilles Peskine50e586b2018-06-08 14:28:46 +02003700exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003701 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003702 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003703 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003704 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003705}
3706/* END_CASE */
3707
Gilles Peskine50e586b2018-06-08 14:28:46 +02003708/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003709void cipher_decrypt_fail( int alg_arg,
3710 int key_type_arg,
3711 data_t *key_data,
3712 data_t *iv,
3713 data_t *input_arg,
3714 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003715{
3716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3717 psa_status_t status;
3718 psa_key_type_t key_type = key_type_arg;
3719 psa_algorithm_t alg = alg_arg;
3720 psa_status_t expected_status = expected_status_arg;
3721 unsigned char *input = NULL;
3722 size_t input_buffer_size = 0;
3723 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01003724 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003725 size_t output_buffer_size = 0;
3726 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01003727 size_t function_output_length;
3728 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003729 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3730
3731 if ( PSA_ERROR_BAD_STATE != expected_status )
3732 {
3733 PSA_ASSERT( psa_crypto_init( ) );
3734
3735 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3736 psa_set_key_algorithm( &attributes, alg );
3737 psa_set_key_type( &attributes, key_type );
3738
3739 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3740 &key ) );
3741 }
3742
3743 /* Allocate input buffer and copy the iv and the plaintext */
3744 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3745 if ( input_buffer_size > 0 )
3746 {
3747 ASSERT_ALLOC( input, input_buffer_size );
3748 memcpy( input, iv->x, iv->len );
3749 memcpy( input + iv->len, input_arg->x, input_arg->len );
3750 }
3751
3752 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3753 ASSERT_ALLOC( output, output_buffer_size );
3754
Neil Armstrong66a479f2022-02-07 15:41:19 +01003755 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003756 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3757 output_buffer_size, &output_length );
3758 TEST_EQUAL( status, expected_status );
3759
Neil Armstrong66a479f2022-02-07 15:41:19 +01003760 /* Decrypt, multi-part */
3761 status = psa_cipher_decrypt_setup( &operation, key, alg );
3762 if( status == PSA_SUCCESS )
3763 {
3764 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
3765 input_arg->len ) +
3766 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3767 ASSERT_ALLOC( output_multi, output_buffer_size );
3768
3769 if( iv->len > 0 )
3770 {
3771 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
3772
3773 if( status != PSA_SUCCESS )
3774 TEST_EQUAL( status, expected_status );
3775 }
3776
3777 if( status == PSA_SUCCESS )
3778 {
3779 status = psa_cipher_update( &operation,
3780 input_arg->x, input_arg->len,
3781 output_multi, output_buffer_size,
3782 &function_output_length );
3783 if( status == PSA_SUCCESS )
3784 {
3785 output_length = function_output_length;
3786
3787 status = psa_cipher_finish( &operation,
3788 output_multi + output_length,
3789 output_buffer_size - output_length,
3790 &function_output_length );
3791
3792 TEST_EQUAL( status, expected_status );
3793 }
3794 else
3795 {
3796 TEST_EQUAL( status, expected_status );
3797 }
3798 }
3799 else
3800 {
3801 TEST_EQUAL( status, expected_status );
3802 }
3803 }
3804 else
3805 {
3806 TEST_EQUAL( status, expected_status );
3807 }
3808
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003809exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01003810 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003811 mbedtls_free( input );
3812 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01003813 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003814 psa_destroy_key( key );
3815 PSA_DONE( );
3816}
3817/* END_CASE */
3818
3819/* BEGIN_CASE */
3820void cipher_decrypt( int alg_arg,
3821 int key_type_arg,
3822 data_t *key_data,
3823 data_t *iv,
3824 data_t *input_arg,
3825 data_t *expected_output )
3826{
3827 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3828 psa_key_type_t key_type = key_type_arg;
3829 psa_algorithm_t alg = alg_arg;
3830 unsigned char *input = NULL;
3831 size_t input_buffer_size = 0;
3832 unsigned char *output = NULL;
3833 size_t output_buffer_size = 0;
3834 size_t output_length = 0;
3835 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3836
3837 PSA_ASSERT( psa_crypto_init( ) );
3838
3839 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3840 psa_set_key_algorithm( &attributes, alg );
3841 psa_set_key_type( &attributes, key_type );
3842
3843 /* Allocate input buffer and copy the iv and the plaintext */
3844 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3845 if ( input_buffer_size > 0 )
3846 {
3847 ASSERT_ALLOC( input, input_buffer_size );
3848 memcpy( input, iv->x, iv->len );
3849 memcpy( input + iv->len, input_arg->x, input_arg->len );
3850 }
3851
3852 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3853 ASSERT_ALLOC( output, output_buffer_size );
3854
3855 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3856 &key ) );
3857
3858 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3859 output_buffer_size, &output_length ) );
3860 TEST_ASSERT( output_length <=
3861 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3862 TEST_ASSERT( output_length <=
3863 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3864
3865 ASSERT_COMPARE( expected_output->x, expected_output->len,
3866 output, output_length );
3867exit:
3868 mbedtls_free( input );
3869 mbedtls_free( output );
3870 psa_destroy_key( key );
3871 PSA_DONE( );
3872}
3873/* END_CASE */
3874
3875/* BEGIN_CASE */
3876void cipher_verify_output( int alg_arg,
3877 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003878 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003879 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003880{
Ronald Cron5425a212020-08-04 14:58:35 +02003881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003882 psa_key_type_t key_type = key_type_arg;
3883 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003884 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003885 size_t output1_size = 0;
3886 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003887 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003888 size_t output2_size = 0;
3889 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003890 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003891
Gilles Peskine8817f612018-12-18 00:18:46 +01003892 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003893
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003894 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3895 psa_set_key_algorithm( &attributes, alg );
3896 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003897
Ronald Cron5425a212020-08-04 14:58:35 +02003898 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3899 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003900 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003901 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003902
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003903 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3904 output1, output1_size,
3905 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003906 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003907 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003908 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003909 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003910
3911 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003912 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003913
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003914 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3915 output2, output2_size,
3916 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003917 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003918 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003919 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003920 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003921
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003922 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003923
3924exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003925 mbedtls_free( output1 );
3926 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003927 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003928 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003929}
3930/* END_CASE */
3931
3932/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003933void cipher_verify_output_multipart( int alg_arg,
3934 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003935 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003936 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003937 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003938{
Ronald Cron5425a212020-08-04 14:58:35 +02003939 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003940 psa_key_type_t key_type = key_type_arg;
3941 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003942 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003943 unsigned char iv[16] = {0};
3944 size_t iv_size = 16;
3945 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003946 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003947 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003948 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003949 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003950 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003951 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003952 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003953 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3954 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003956
Gilles Peskine8817f612018-12-18 00:18:46 +01003957 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003958
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3960 psa_set_key_algorithm( &attributes, alg );
3961 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003962
Ronald Cron5425a212020-08-04 14:58:35 +02003963 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3964 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003965
Ronald Cron5425a212020-08-04 14:58:35 +02003966 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3967 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003968
Steven Cooreman177deba2020-09-07 17:14:14 +02003969 if( alg != PSA_ALG_ECB_NO_PADDING )
3970 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003971 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3972 iv, iv_size,
3973 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003974 }
3975
gabor-mezei-armceface22021-01-21 12:26:17 +01003976 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3977 TEST_ASSERT( output1_buffer_size <=
3978 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003979 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003980
Gilles Peskinee0866522019-02-19 19:44:00 +01003981 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003982
Gilles Peskine8817f612018-12-18 00:18:46 +01003983 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3984 output1, output1_buffer_size,
3985 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003986 TEST_ASSERT( function_output_length <=
3987 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3988 TEST_ASSERT( function_output_length <=
3989 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003990 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003991
Gilles Peskine8817f612018-12-18 00:18:46 +01003992 PSA_ASSERT( psa_cipher_update( &operation1,
3993 input->x + first_part_size,
3994 input->len - first_part_size,
3995 output1, output1_buffer_size,
3996 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003997 TEST_ASSERT( function_output_length <=
3998 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3999 alg,
4000 input->len - first_part_size ) );
4001 TEST_ASSERT( function_output_length <=
4002 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004003 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004004
Gilles Peskine8817f612018-12-18 00:18:46 +01004005 PSA_ASSERT( psa_cipher_finish( &operation1,
4006 output1 + output1_length,
4007 output1_buffer_size - output1_length,
4008 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004009 TEST_ASSERT( function_output_length <=
4010 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4011 TEST_ASSERT( function_output_length <=
4012 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004013 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004014
Gilles Peskine8817f612018-12-18 00:18:46 +01004015 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004016
Gilles Peskine048b7f02018-06-08 14:20:49 +02004017 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004018 TEST_ASSERT( output2_buffer_size <=
4019 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4020 TEST_ASSERT( output2_buffer_size <=
4021 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004022 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004023
Steven Cooreman177deba2020-09-07 17:14:14 +02004024 if( iv_length > 0 )
4025 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004026 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4027 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004028 }
Moran Pekerded84402018-06-06 16:36:50 +03004029
Gilles Peskine8817f612018-12-18 00:18:46 +01004030 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4031 output2, output2_buffer_size,
4032 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004033 TEST_ASSERT( function_output_length <=
4034 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4035 TEST_ASSERT( function_output_length <=
4036 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004037 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004038
Gilles Peskine8817f612018-12-18 00:18:46 +01004039 PSA_ASSERT( psa_cipher_update( &operation2,
4040 output1 + first_part_size,
4041 output1_length - first_part_size,
4042 output2, output2_buffer_size,
4043 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004044 TEST_ASSERT( function_output_length <=
4045 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4046 alg,
4047 output1_length - first_part_size ) );
4048 TEST_ASSERT( function_output_length <=
4049 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004050 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004051
Gilles Peskine8817f612018-12-18 00:18:46 +01004052 PSA_ASSERT( psa_cipher_finish( &operation2,
4053 output2 + output2_length,
4054 output2_buffer_size - output2_length,
4055 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004056 TEST_ASSERT( function_output_length <=
4057 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4058 TEST_ASSERT( function_output_length <=
4059 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004060 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004061
Gilles Peskine8817f612018-12-18 00:18:46 +01004062 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004063
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004064 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004065
4066exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004067 psa_cipher_abort( &operation1 );
4068 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004069 mbedtls_free( output1 );
4070 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004071 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004072 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004073}
4074/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004075
Gilles Peskine20035e32018-02-03 22:44:14 +01004076/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004077void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004078 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004079 data_t *nonce,
4080 data_t *additional_data,
4081 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004082 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004083{
Ronald Cron5425a212020-08-04 14:58:35 +02004084 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004085 psa_key_type_t key_type = key_type_arg;
4086 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004087 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004088 unsigned char *output_data = NULL;
4089 size_t output_size = 0;
4090 size_t output_length = 0;
4091 unsigned char *output_data2 = NULL;
4092 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004093 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004094 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004095 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004096
Gilles Peskine8817f612018-12-18 00:18:46 +01004097 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004098
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004099 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4100 psa_set_key_algorithm( &attributes, alg );
4101 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004102
Gilles Peskine049c7532019-05-15 20:22:09 +02004103 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004104 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004105 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4106 key_bits = psa_get_key_bits( &attributes );
4107
4108 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4109 alg );
4110 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4111 * should be exact. */
4112 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4113 expected_result != PSA_ERROR_NOT_SUPPORTED )
4114 {
4115 TEST_EQUAL( output_size,
4116 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4117 TEST_ASSERT( output_size <=
4118 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4119 }
4120 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004121
Steven Cooremanf49478b2021-02-15 15:19:25 +01004122 status = psa_aead_encrypt( key, alg,
4123 nonce->x, nonce->len,
4124 additional_data->x,
4125 additional_data->len,
4126 input_data->x, input_data->len,
4127 output_data, output_size,
4128 &output_length );
4129
4130 /* If the operation is not supported, just skip and not fail in case the
4131 * encryption involves a common limitation of cryptography hardwares and
4132 * an alternative implementation. */
4133 if( status == PSA_ERROR_NOT_SUPPORTED )
4134 {
4135 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4136 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4137 }
4138
4139 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004140
4141 if( PSA_SUCCESS == expected_result )
4142 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004143 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004144
Gilles Peskine003a4a92019-05-14 16:09:40 +02004145 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4146 * should be exact. */
4147 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004148 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004149
gabor-mezei-armceface22021-01-21 12:26:17 +01004150 TEST_ASSERT( input_data->len <=
4151 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
4152
Ronald Cron5425a212020-08-04 14:58:35 +02004153 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004154 nonce->x, nonce->len,
4155 additional_data->x,
4156 additional_data->len,
4157 output_data, output_length,
4158 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004159 &output_length2 ),
4160 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004161
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004162 ASSERT_COMPARE( input_data->x, input_data->len,
4163 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004164 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004165
Gilles Peskinea1cac842018-06-11 19:33:02 +02004166exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004167 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004168 mbedtls_free( output_data );
4169 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004170 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004171}
4172/* END_CASE */
4173
4174/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004175void aead_encrypt( int key_type_arg, data_t *key_data,
4176 int alg_arg,
4177 data_t *nonce,
4178 data_t *additional_data,
4179 data_t *input_data,
4180 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004181{
Ronald Cron5425a212020-08-04 14:58:35 +02004182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004183 psa_key_type_t key_type = key_type_arg;
4184 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004185 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004186 unsigned char *output_data = NULL;
4187 size_t output_size = 0;
4188 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004190 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004191
Gilles Peskine8817f612018-12-18 00:18:46 +01004192 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004193
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004194 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4195 psa_set_key_algorithm( &attributes, alg );
4196 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004197
Gilles Peskine049c7532019-05-15 20:22:09 +02004198 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004199 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004200 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4201 key_bits = psa_get_key_bits( &attributes );
4202
4203 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4204 alg );
4205 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4206 * should be exact. */
4207 TEST_EQUAL( output_size,
4208 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4209 TEST_ASSERT( output_size <=
4210 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4211 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004212
Steven Cooremand588ea12021-01-11 19:36:04 +01004213 status = psa_aead_encrypt( key, alg,
4214 nonce->x, nonce->len,
4215 additional_data->x, additional_data->len,
4216 input_data->x, input_data->len,
4217 output_data, output_size,
4218 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004219
Ronald Cron28a45ed2021-02-09 20:35:42 +01004220 /* If the operation is not supported, just skip and not fail in case the
4221 * encryption involves a common limitation of cryptography hardwares and
4222 * an alternative implementation. */
4223 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004224 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004225 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4226 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004227 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004228
4229 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004230 ASSERT_COMPARE( expected_result->x, expected_result->len,
4231 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004232
Gilles Peskinea1cac842018-06-11 19:33:02 +02004233exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004234 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004235 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004236 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004237}
4238/* END_CASE */
4239
4240/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004241void aead_decrypt( int key_type_arg, data_t *key_data,
4242 int alg_arg,
4243 data_t *nonce,
4244 data_t *additional_data,
4245 data_t *input_data,
4246 data_t *expected_data,
4247 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004248{
Ronald Cron5425a212020-08-04 14:58:35 +02004249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004250 psa_key_type_t key_type = key_type_arg;
4251 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004252 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004253 unsigned char *output_data = NULL;
4254 size_t output_size = 0;
4255 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004257 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004258 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004259
Gilles Peskine8817f612018-12-18 00:18:46 +01004260 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004261
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004262 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4263 psa_set_key_algorithm( &attributes, alg );
4264 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004265
Gilles Peskine049c7532019-05-15 20:22:09 +02004266 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004267 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004268 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4269 key_bits = psa_get_key_bits( &attributes );
4270
4271 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4272 alg );
4273 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4274 expected_result != PSA_ERROR_NOT_SUPPORTED )
4275 {
4276 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4277 * should be exact. */
4278 TEST_EQUAL( output_size,
4279 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4280 TEST_ASSERT( output_size <=
4281 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4282 }
4283 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004284
Steven Cooremand588ea12021-01-11 19:36:04 +01004285 status = psa_aead_decrypt( key, alg,
4286 nonce->x, nonce->len,
4287 additional_data->x,
4288 additional_data->len,
4289 input_data->x, input_data->len,
4290 output_data, output_size,
4291 &output_length );
4292
Ronald Cron28a45ed2021-02-09 20:35:42 +01004293 /* If the operation is not supported, just skip and not fail in case the
4294 * decryption involves a common limitation of cryptography hardwares and
4295 * an alternative implementation. */
4296 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004297 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004298 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4299 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004300 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004301
4302 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004303
Gilles Peskine2d277862018-06-18 15:41:12 +02004304 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004305 ASSERT_COMPARE( expected_data->x, expected_data->len,
4306 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004307
Gilles Peskinea1cac842018-06-11 19:33:02 +02004308exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004309 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004310 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004311 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004312}
4313/* END_CASE */
4314
4315/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004316void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4317 int alg_arg,
4318 data_t *nonce,
4319 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004320 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004321 int do_set_lengths,
4322 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004323{
Paul Elliottd3f82412021-06-16 16:52:21 +01004324 size_t ad_part_len = 0;
4325 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004326 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004327
Paul Elliott32f46ba2021-09-23 18:24:36 +01004328 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004329 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004330 mbedtls_test_set_step( ad_part_len );
4331
4332 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004333 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004334 if( ad_part_len & 0x01 )
4335 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4336 else
4337 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004338 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004339
4340 /* Split ad into length(ad_part_len) parts. */
4341 if( !aead_multipart_internal_func( key_type_arg, key_data,
4342 alg_arg, nonce,
4343 additional_data,
4344 ad_part_len,
4345 input_data, -1,
4346 set_lengths_method,
4347 expected_output,
4348 1, 0 ) )
4349 break;
4350
4351 /* length(0) part, length(ad_part_len) part, length(0) part... */
4352 mbedtls_test_set_step( 1000 + ad_part_len );
4353
4354 if( !aead_multipart_internal_func( key_type_arg, key_data,
4355 alg_arg, nonce,
4356 additional_data,
4357 ad_part_len,
4358 input_data, -1,
4359 set_lengths_method,
4360 expected_output,
4361 1, 1 ) )
4362 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004363 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004364
Paul Elliott32f46ba2021-09-23 18:24:36 +01004365 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004366 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004367 /* Split data into length(data_part_len) parts. */
4368 mbedtls_test_set_step( 2000 + data_part_len );
4369
4370 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004371 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004372 if( data_part_len & 0x01 )
4373 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4374 else
4375 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004376 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004377
Paul Elliott32f46ba2021-09-23 18:24:36 +01004378 if( !aead_multipart_internal_func( key_type_arg, key_data,
4379 alg_arg, nonce,
4380 additional_data, -1,
4381 input_data, data_part_len,
4382 set_lengths_method,
4383 expected_output,
4384 1, 0 ) )
4385 break;
4386
4387 /* length(0) part, length(data_part_len) part, length(0) part... */
4388 mbedtls_test_set_step( 3000 + data_part_len );
4389
4390 if( !aead_multipart_internal_func( key_type_arg, key_data,
4391 alg_arg, nonce,
4392 additional_data, -1,
4393 input_data, data_part_len,
4394 set_lengths_method,
4395 expected_output,
4396 1, 1 ) )
4397 break;
4398 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004399
Paul Elliott8fc45162021-06-23 16:06:01 +01004400 /* Goto is required to silence warnings about unused labels, as we
4401 * don't actually do any test assertions in this function. */
4402 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004403}
4404/* END_CASE */
4405
4406/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004407void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4408 int alg_arg,
4409 data_t *nonce,
4410 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004411 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004412 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004413 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004414{
Paul Elliottd3f82412021-06-16 16:52:21 +01004415 size_t ad_part_len = 0;
4416 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004417 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004418
Paul Elliott32f46ba2021-09-23 18:24:36 +01004419 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004420 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004421 /* Split ad into length(ad_part_len) parts. */
4422 mbedtls_test_set_step( ad_part_len );
4423
4424 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004425 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004426 if( ad_part_len & 0x01 )
4427 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4428 else
4429 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004430 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004431
4432 if( !aead_multipart_internal_func( key_type_arg, key_data,
4433 alg_arg, nonce,
4434 additional_data,
4435 ad_part_len,
4436 input_data, -1,
4437 set_lengths_method,
4438 expected_output,
4439 0, 0 ) )
4440 break;
4441
4442 /* length(0) part, length(ad_part_len) part, length(0) part... */
4443 mbedtls_test_set_step( 1000 + ad_part_len );
4444
4445 if( !aead_multipart_internal_func( key_type_arg, key_data,
4446 alg_arg, nonce,
4447 additional_data,
4448 ad_part_len,
4449 input_data, -1,
4450 set_lengths_method,
4451 expected_output,
4452 0, 1 ) )
4453 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004454 }
4455
Paul Elliott32f46ba2021-09-23 18:24:36 +01004456 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004457 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004458 /* Split data into length(data_part_len) parts. */
4459 mbedtls_test_set_step( 2000 + data_part_len );
4460
4461 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004462 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004463 if( data_part_len & 0x01 )
4464 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4465 else
4466 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004467 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004468
4469 if( !aead_multipart_internal_func( key_type_arg, key_data,
4470 alg_arg, nonce,
4471 additional_data, -1,
4472 input_data, data_part_len,
4473 set_lengths_method,
4474 expected_output,
4475 0, 0 ) )
4476 break;
4477
4478 /* length(0) part, length(data_part_len) part, length(0) part... */
4479 mbedtls_test_set_step( 3000 + data_part_len );
4480
4481 if( !aead_multipart_internal_func( key_type_arg, key_data,
4482 alg_arg, nonce,
4483 additional_data, -1,
4484 input_data, data_part_len,
4485 set_lengths_method,
4486 expected_output,
4487 0, 1 ) )
4488 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004489 }
4490
Paul Elliott8fc45162021-06-23 16:06:01 +01004491 /* Goto is required to silence warnings about unused labels, as we
4492 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004493 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004494}
4495/* END_CASE */
4496
4497/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004498void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4499 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004500 int nonce_length,
4501 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004502 data_t *additional_data,
4503 data_t *input_data,
4504 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004505{
4506
4507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4508 psa_key_type_t key_type = key_type_arg;
4509 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004510 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004511 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4513 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004514 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004515 size_t actual_nonce_length = 0;
4516 size_t expected_nonce_length = expected_nonce_length_arg;
4517 unsigned char *output = NULL;
4518 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004519 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004520 size_t ciphertext_size = 0;
4521 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004522 size_t tag_length = 0;
4523 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004524
4525 PSA_ASSERT( psa_crypto_init( ) );
4526
4527 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4528 psa_set_key_algorithm( & attributes, alg );
4529 psa_set_key_type( & attributes, key_type );
4530
4531 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4532 &key ) );
4533
4534 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4535
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004536 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4537
Paul Elliottf1277632021-08-24 18:11:37 +01004538 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004539
Paul Elliottf1277632021-08-24 18:11:37 +01004540 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004541
Paul Elliottf1277632021-08-24 18:11:37 +01004542 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004543
Paul Elliottf1277632021-08-24 18:11:37 +01004544 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004545
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004546 status = psa_aead_encrypt_setup( &operation, key, alg );
4547
4548 /* If the operation is not supported, just skip and not fail in case the
4549 * encryption involves a common limitation of cryptography hardwares and
4550 * an alternative implementation. */
4551 if( status == PSA_ERROR_NOT_SUPPORTED )
4552 {
4553 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004554 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004555 }
4556
4557 PSA_ASSERT( status );
4558
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004559 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004560 nonce_length,
4561 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004562
Paul Elliott693bf312021-07-23 17:40:41 +01004563 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004564
Paul Elliottf1277632021-08-24 18:11:37 +01004565 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004566
Paul Elliott88ecbe12021-09-22 17:23:03 +01004567 if( expected_status == PSA_SUCCESS )
4568 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4569 alg ) );
4570
Paul Elliottd79c5c52021-10-06 21:49:41 +01004571 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004572
Paul Elliott693bf312021-07-23 17:40:41 +01004573 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004574 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004575 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004576 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4577 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004578
4579 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4580 additional_data->len ) );
4581
4582 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004583 output, output_size,
4584 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004585
Paul Elliottf1277632021-08-24 18:11:37 +01004586 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4587 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004588 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4589 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004590
4591exit:
4592 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004593 mbedtls_free( output );
4594 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004595 psa_aead_abort( &operation );
4596 PSA_DONE( );
4597}
4598/* END_CASE */
4599
4600/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004601void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4602 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004603 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004604 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004605 data_t *additional_data,
4606 data_t *input_data,
4607 int expected_status_arg )
4608{
4609
4610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4611 psa_key_type_t key_type = key_type_arg;
4612 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004613 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004614 uint8_t *nonce_buffer = NULL;
4615 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4616 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4617 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004618 unsigned char *output = NULL;
4619 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004620 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004621 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004622 size_t ciphertext_size = 0;
4623 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004624 size_t tag_length = 0;
4625 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004626 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004627 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004628
4629 PSA_ASSERT( psa_crypto_init( ) );
4630
4631 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4632 psa_set_key_algorithm( &attributes, alg );
4633 psa_set_key_type( &attributes, key_type );
4634
4635 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4636 &key ) );
4637
4638 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4639
4640 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4641
Paul Elliott6f0e7202021-08-25 12:57:18 +01004642 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004643
Paul Elliott6f0e7202021-08-25 12:57:18 +01004644 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004645
Paul Elliott6f0e7202021-08-25 12:57:18 +01004646 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004647
Paul Elliott6f0e7202021-08-25 12:57:18 +01004648 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004649
Paul Elliott863864a2021-07-23 17:28:31 +01004650 status = psa_aead_encrypt_setup( &operation, key, alg );
4651
4652 /* If the operation is not supported, just skip and not fail in case the
4653 * encryption involves a common limitation of cryptography hardwares and
4654 * an alternative implementation. */
4655 if( status == PSA_ERROR_NOT_SUPPORTED )
4656 {
4657 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004658 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004659 }
4660
4661 PSA_ASSERT( status );
4662
Paul Elliott4023ffd2021-09-10 16:21:22 +01004663 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4664 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004665 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004666 /* Arbitrary size buffer, to test zero length valid buffer. */
4667 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004668 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004669 }
4670 else
4671 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004672 /* If length is zero, then this will return NULL. */
4673 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004674 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004675
Paul Elliott4023ffd2021-09-10 16:21:22 +01004676 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004677 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004678 for( index = 0; index < nonce_length - 1; ++index )
4679 {
4680 nonce_buffer[index] = 'a' + index;
4681 }
Paul Elliott66696b52021-08-16 18:42:41 +01004682 }
Paul Elliott863864a2021-07-23 17:28:31 +01004683 }
4684
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004685 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4686 {
4687 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4688 input_data->len ) );
4689 }
4690
Paul Elliott6f0e7202021-08-25 12:57:18 +01004691 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004692
Paul Elliott693bf312021-07-23 17:40:41 +01004693 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004694
4695 if( expected_status == PSA_SUCCESS )
4696 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004697 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4698 {
4699 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4700 input_data->len ) );
4701 }
4702 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4703 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004704
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004705 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4706 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4707 additional_data->len ),
4708 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004709
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004710 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004711 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004712 &ciphertext_length ),
4713 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004714
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004715 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004716 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004717 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4718 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004719 }
4720
4721exit:
4722 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004723 mbedtls_free( output );
4724 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004725 mbedtls_free( nonce_buffer );
4726 psa_aead_abort( &operation );
4727 PSA_DONE( );
4728}
4729/* END_CASE */
4730
4731/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004732void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4733 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004734 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004735 data_t *nonce,
4736 data_t *additional_data,
4737 data_t *input_data,
4738 int expected_status_arg )
4739{
4740
4741 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4742 psa_key_type_t key_type = key_type_arg;
4743 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004744 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4746 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4747 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004748 unsigned char *output = NULL;
4749 unsigned char *ciphertext = NULL;
4750 size_t output_size = output_size_arg;
4751 size_t ciphertext_size = 0;
4752 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004753 size_t tag_length = 0;
4754 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4755
4756 PSA_ASSERT( psa_crypto_init( ) );
4757
4758 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4759 psa_set_key_algorithm( &attributes, alg );
4760 psa_set_key_type( &attributes, key_type );
4761
4762 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4763 &key ) );
4764
4765 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4766
Paul Elliottc6d11d02021-09-01 12:04:23 +01004767 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004768
Paul Elliottc6d11d02021-09-01 12:04:23 +01004769 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004770
Paul Elliottc6d11d02021-09-01 12:04:23 +01004771 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004772
Paul Elliott43fbda62021-07-23 18:30:59 +01004773 status = psa_aead_encrypt_setup( &operation, key, alg );
4774
4775 /* If the operation is not supported, just skip and not fail in case the
4776 * encryption involves a common limitation of cryptography hardwares and
4777 * an alternative implementation. */
4778 if( status == PSA_ERROR_NOT_SUPPORTED )
4779 {
4780 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4781 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4782 }
4783
4784 PSA_ASSERT( status );
4785
Paul Elliott47b9a142021-10-07 15:04:57 +01004786 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4787 input_data->len ) );
4788
Paul Elliott43fbda62021-07-23 18:30:59 +01004789 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4790
4791 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4792 additional_data->len ) );
4793
4794 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004795 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004796
4797 TEST_EQUAL( status, expected_status );
4798
4799 if( expected_status == PSA_SUCCESS )
4800 {
4801 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004802 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4803 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004804 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4805 }
4806
4807exit:
4808 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004809 mbedtls_free( output );
4810 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004811 psa_aead_abort( &operation );
4812 PSA_DONE( );
4813}
4814/* END_CASE */
4815
Paul Elliott91b021e2021-07-23 18:52:31 +01004816/* BEGIN_CASE */
4817void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4818 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004819 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004820 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004821 data_t *nonce,
4822 data_t *additional_data,
4823 data_t *input_data,
4824 int expected_status_arg )
4825{
4826
4827 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4828 psa_key_type_t key_type = key_type_arg;
4829 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004830 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004831 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4832 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4833 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004834 unsigned char *ciphertext = NULL;
4835 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004836 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004837 size_t ciphertext_size = 0;
4838 size_t ciphertext_length = 0;
4839 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004840 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004841 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004842
4843 PSA_ASSERT( psa_crypto_init( ) );
4844
4845 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4846 psa_set_key_algorithm( &attributes, alg );
4847 psa_set_key_type( &attributes, key_type );
4848
4849 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4850 &key ) );
4851
4852 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4853
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004854 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004855
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004856 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004857
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004858 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004859
Paul Elliott719c1322021-09-13 18:27:22 +01004860 ASSERT_ALLOC( tag_buffer, tag_size );
4861
Paul Elliott91b021e2021-07-23 18:52:31 +01004862 status = psa_aead_encrypt_setup( &operation, key, alg );
4863
4864 /* If the operation is not supported, just skip and not fail in case the
4865 * encryption involves a common limitation of cryptography hardwares and
4866 * an alternative implementation. */
4867 if( status == PSA_ERROR_NOT_SUPPORTED )
4868 {
4869 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4870 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4871 }
4872
4873 PSA_ASSERT( status );
4874
4875 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4876
Paul Elliott76bda482021-10-07 17:07:23 +01004877 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4878 input_data->len ) );
4879
Paul Elliott91b021e2021-07-23 18:52:31 +01004880 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4881 additional_data->len ) );
4882
4883 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004884 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004885
4886 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004887 status = psa_aead_finish( &operation, finish_ciphertext,
4888 finish_ciphertext_size,
4889 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004890 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004891
4892 TEST_EQUAL( status, expected_status );
4893
4894exit:
4895 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004896 mbedtls_free( ciphertext );
4897 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004898 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004899 psa_aead_abort( &operation );
4900 PSA_DONE( );
4901}
4902/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004903
4904/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004905void aead_multipart_verify( int key_type_arg, data_t *key_data,
4906 int alg_arg,
4907 data_t *nonce,
4908 data_t *additional_data,
4909 data_t *input_data,
4910 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004911 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004912 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004913 int expected_status_arg )
4914{
4915 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4916 psa_key_type_t key_type = key_type_arg;
4917 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004918 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4920 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4921 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004922 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004923 unsigned char *plaintext = NULL;
4924 unsigned char *finish_plaintext = NULL;
4925 size_t plaintext_size = 0;
4926 size_t plaintext_length = 0;
4927 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004928 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004929 unsigned char *tag_buffer = NULL;
4930 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004931
4932 PSA_ASSERT( psa_crypto_init( ) );
4933
4934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4935 psa_set_key_algorithm( &attributes, alg );
4936 psa_set_key_type( &attributes, key_type );
4937
4938 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4939 &key ) );
4940
4941 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4942
4943 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4944 input_data->len );
4945
4946 ASSERT_ALLOC( plaintext, plaintext_size );
4947
4948 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4949
4950 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4951
Paul Elliott9961a662021-09-17 19:19:02 +01004952 status = psa_aead_decrypt_setup( &operation, key, alg );
4953
4954 /* If the operation is not supported, just skip and not fail in case the
4955 * encryption involves a common limitation of cryptography hardwares and
4956 * an alternative implementation. */
4957 if( status == PSA_ERROR_NOT_SUPPORTED )
4958 {
4959 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4960 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4961 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004962 TEST_EQUAL( status, expected_setup_status );
4963
4964 if( status != PSA_SUCCESS )
4965 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004966
4967 PSA_ASSERT( status );
4968
4969 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4970
Paul Elliottfec6f372021-10-06 17:15:02 +01004971 status = psa_aead_set_lengths( &operation, additional_data->len,
4972 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004973 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004974
Paul Elliott9961a662021-09-17 19:19:02 +01004975 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4976 additional_data->len ) );
4977
4978 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4979 input_data->len,
4980 plaintext, plaintext_size,
4981 &plaintext_length ) );
4982
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004983 if( tag_usage == USE_GIVEN_TAG )
4984 {
4985 tag_buffer = tag->x;
4986 tag_size = tag->len;
4987 }
4988
Paul Elliott9961a662021-09-17 19:19:02 +01004989 status = psa_aead_verify( &operation, finish_plaintext,
4990 verify_plaintext_size,
4991 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004992 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004993
4994 TEST_EQUAL( status, expected_status );
4995
4996exit:
4997 psa_destroy_key( key );
4998 mbedtls_free( plaintext );
4999 mbedtls_free( finish_plaintext );
5000 psa_aead_abort( &operation );
5001 PSA_DONE( );
5002}
5003/* END_CASE */
5004
Paul Elliott9961a662021-09-17 19:19:02 +01005005/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005006void aead_multipart_setup( int key_type_arg, data_t *key_data,
5007 int alg_arg, int expected_status_arg )
5008{
5009 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5010 psa_key_type_t key_type = key_type_arg;
5011 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005012 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5014 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5015 psa_status_t expected_status = expected_status_arg;
5016
5017 PSA_ASSERT( psa_crypto_init( ) );
5018
5019 psa_set_key_usage_flags( &attributes,
5020 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5021 psa_set_key_algorithm( &attributes, alg );
5022 psa_set_key_type( &attributes, key_type );
5023
5024 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5025 &key ) );
5026
Paul Elliott5221ef62021-09-19 17:33:03 +01005027 status = psa_aead_encrypt_setup( &operation, key, alg );
5028
5029 TEST_EQUAL( status, expected_status );
5030
5031 psa_aead_abort( &operation );
5032
Paul Elliott5221ef62021-09-19 17:33:03 +01005033 status = psa_aead_decrypt_setup( &operation, key, alg );
5034
5035 TEST_EQUAL(status, expected_status );
5036
5037exit:
5038 psa_destroy_key( key );
5039 psa_aead_abort( &operation );
5040 PSA_DONE( );
5041}
5042/* END_CASE */
5043
5044/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005045void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5046 int alg_arg,
5047 data_t *nonce,
5048 data_t *additional_data,
5049 data_t *input_data )
5050{
5051 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5052 psa_key_type_t key_type = key_type_arg;
5053 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005054 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005055 unsigned char *output_data = NULL;
5056 unsigned char *final_data = NULL;
5057 size_t output_size = 0;
5058 size_t finish_output_size = 0;
5059 size_t output_length = 0;
5060 size_t key_bits = 0;
5061 size_t tag_length = 0;
5062 size_t tag_size = 0;
5063 size_t nonce_length = 0;
5064 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5065 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5066 size_t output_part_length = 0;
5067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5068
5069 PSA_ASSERT( psa_crypto_init( ) );
5070
5071 psa_set_key_usage_flags( & attributes,
5072 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5073 psa_set_key_algorithm( & attributes, alg );
5074 psa_set_key_type( & attributes, key_type );
5075
5076 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5077 &key ) );
5078
5079 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5080 key_bits = psa_get_key_bits( &attributes );
5081
5082 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5083
5084 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
5085
5086 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5087
5088 ASSERT_ALLOC( output_data, output_size );
5089
5090 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5091
5092 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
5093
5094 ASSERT_ALLOC( final_data, finish_output_size );
5095
5096 /* Test all operations error without calling setup first. */
5097
Paul Elliottc23a9a02021-06-21 18:32:46 +01005098 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5099 PSA_ERROR_BAD_STATE );
5100
5101 psa_aead_abort( &operation );
5102
Paul Elliottc23a9a02021-06-21 18:32:46 +01005103 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5104 PSA_AEAD_NONCE_MAX_SIZE,
5105 &nonce_length ),
5106 PSA_ERROR_BAD_STATE );
5107
5108 psa_aead_abort( &operation );
5109
Paul Elliott481be342021-07-16 17:38:47 +01005110 /* ------------------------------------------------------- */
5111
Paul Elliottc23a9a02021-06-21 18:32:46 +01005112 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5113 input_data->len ),
5114 PSA_ERROR_BAD_STATE );
5115
5116 psa_aead_abort( &operation );
5117
Paul Elliott481be342021-07-16 17:38:47 +01005118 /* ------------------------------------------------------- */
5119
Paul Elliottc23a9a02021-06-21 18:32:46 +01005120 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5121 additional_data->len ),
5122 PSA_ERROR_BAD_STATE );
5123
5124 psa_aead_abort( &operation );
5125
Paul Elliott481be342021-07-16 17:38:47 +01005126 /* ------------------------------------------------------- */
5127
Paul Elliottc23a9a02021-06-21 18:32:46 +01005128 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5129 input_data->len, output_data,
5130 output_size, &output_length ),
5131 PSA_ERROR_BAD_STATE );
5132
5133 psa_aead_abort( &operation );
5134
Paul Elliott481be342021-07-16 17:38:47 +01005135 /* ------------------------------------------------------- */
5136
Paul Elliottc23a9a02021-06-21 18:32:46 +01005137 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5138 finish_output_size,
5139 &output_part_length,
5140 tag_buffer, tag_length,
5141 &tag_size ),
5142 PSA_ERROR_BAD_STATE );
5143
5144 psa_aead_abort( &operation );
5145
Paul Elliott481be342021-07-16 17:38:47 +01005146 /* ------------------------------------------------------- */
5147
Paul Elliottc23a9a02021-06-21 18:32:46 +01005148 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5149 finish_output_size,
5150 &output_part_length,
5151 tag_buffer,
5152 tag_length ),
5153 PSA_ERROR_BAD_STATE );
5154
5155 psa_aead_abort( &operation );
5156
5157 /* Test for double setups. */
5158
Paul Elliottc23a9a02021-06-21 18:32:46 +01005159 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5160
5161 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5162 PSA_ERROR_BAD_STATE );
5163
5164 psa_aead_abort( &operation );
5165
Paul Elliott481be342021-07-16 17:38:47 +01005166 /* ------------------------------------------------------- */
5167
Paul Elliottc23a9a02021-06-21 18:32:46 +01005168 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5169
5170 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5171 PSA_ERROR_BAD_STATE );
5172
5173 psa_aead_abort( &operation );
5174
Paul Elliott374a2be2021-07-16 17:53:40 +01005175 /* ------------------------------------------------------- */
5176
Paul Elliott374a2be2021-07-16 17:53:40 +01005177 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5178
5179 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5180 PSA_ERROR_BAD_STATE );
5181
5182 psa_aead_abort( &operation );
5183
5184 /* ------------------------------------------------------- */
5185
Paul Elliott374a2be2021-07-16 17:53:40 +01005186 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5187
5188 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5189 PSA_ERROR_BAD_STATE );
5190
5191 psa_aead_abort( &operation );
5192
Paul Elliottc23a9a02021-06-21 18:32:46 +01005193 /* Test for not setting a nonce. */
5194
Paul Elliottc23a9a02021-06-21 18:32:46 +01005195 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5196
5197 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5198 additional_data->len ),
5199 PSA_ERROR_BAD_STATE );
5200
5201 psa_aead_abort( &operation );
5202
Paul Elliott7f628422021-09-01 12:08:29 +01005203 /* ------------------------------------------------------- */
5204
Paul Elliott7f628422021-09-01 12:08:29 +01005205 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5206
5207 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5208 input_data->len, output_data,
5209 output_size, &output_length ),
5210 PSA_ERROR_BAD_STATE );
5211
5212 psa_aead_abort( &operation );
5213
Paul Elliottbdc2c682021-09-21 18:37:10 +01005214 /* ------------------------------------------------------- */
5215
Paul Elliottbdc2c682021-09-21 18:37:10 +01005216 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5217
5218 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5219 finish_output_size,
5220 &output_part_length,
5221 tag_buffer, tag_length,
5222 &tag_size ),
5223 PSA_ERROR_BAD_STATE );
5224
5225 psa_aead_abort( &operation );
5226
5227 /* ------------------------------------------------------- */
5228
Paul Elliottbdc2c682021-09-21 18:37:10 +01005229 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5230
5231 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5232 finish_output_size,
5233 &output_part_length,
5234 tag_buffer,
5235 tag_length ),
5236 PSA_ERROR_BAD_STATE );
5237
5238 psa_aead_abort( &operation );
5239
Paul Elliottc23a9a02021-06-21 18:32:46 +01005240 /* Test for double setting nonce. */
5241
Paul Elliottc23a9a02021-06-21 18:32:46 +01005242 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5243
5244 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5245
5246 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5247 PSA_ERROR_BAD_STATE );
5248
5249 psa_aead_abort( &operation );
5250
Paul Elliott374a2be2021-07-16 17:53:40 +01005251 /* Test for double generating nonce. */
5252
Paul Elliott374a2be2021-07-16 17:53:40 +01005253 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5254
5255 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5256 PSA_AEAD_NONCE_MAX_SIZE,
5257 &nonce_length ) );
5258
5259 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5260 PSA_AEAD_NONCE_MAX_SIZE,
5261 &nonce_length ),
5262 PSA_ERROR_BAD_STATE );
5263
5264
5265 psa_aead_abort( &operation );
5266
5267 /* Test for generate nonce then set and vice versa */
5268
Paul Elliott374a2be2021-07-16 17:53:40 +01005269 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5270
5271 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5272 PSA_AEAD_NONCE_MAX_SIZE,
5273 &nonce_length ) );
5274
5275 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5276 PSA_ERROR_BAD_STATE );
5277
5278 psa_aead_abort( &operation );
5279
Andrzej Kurekad837522021-12-15 15:28:49 +01005280 /* Test for generating nonce after calling set lengths */
5281
5282 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5283
5284 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5285 input_data->len ) );
5286
5287 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5288 PSA_AEAD_NONCE_MAX_SIZE,
5289 &nonce_length ) );
5290
5291 psa_aead_abort( &operation );
5292
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005293 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005294
5295 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5296
5297 if( operation.alg == PSA_ALG_CCM )
5298 {
5299 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5300 input_data->len ),
5301 PSA_ERROR_INVALID_ARGUMENT );
5302 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5303 PSA_AEAD_NONCE_MAX_SIZE,
5304 &nonce_length ),
5305 PSA_ERROR_BAD_STATE );
5306 }
5307 else
5308 {
5309 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5310 input_data->len ) );
5311 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5312 PSA_AEAD_NONCE_MAX_SIZE,
5313 &nonce_length ) );
5314 }
5315
5316 psa_aead_abort( &operation );
5317
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005318 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005319#if SIZE_MAX > UINT32_MAX
5320 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5321
5322 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5323 {
5324 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5325 input_data->len ),
5326 PSA_ERROR_INVALID_ARGUMENT );
5327 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5328 PSA_AEAD_NONCE_MAX_SIZE,
5329 &nonce_length ),
5330 PSA_ERROR_BAD_STATE );
5331 }
5332 else
5333 {
5334 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5335 input_data->len ) );
5336 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5337 PSA_AEAD_NONCE_MAX_SIZE,
5338 &nonce_length ) );
5339 }
5340
5341 psa_aead_abort( &operation );
5342#endif
5343
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005344 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005345
5346 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5347
5348 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5349 PSA_AEAD_NONCE_MAX_SIZE,
5350 &nonce_length ) );
5351
5352 if( operation.alg == PSA_ALG_CCM )
5353 {
5354 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5355 input_data->len ),
5356 PSA_ERROR_INVALID_ARGUMENT );
5357 }
5358 else
5359 {
5360 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5361 input_data->len ) );
5362 }
5363
5364 psa_aead_abort( &operation );
5365
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005366 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005367 /* Test for setting nonce after calling set lengths */
5368
5369 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5370
5371 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5372 input_data->len ) );
5373
5374 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5375
5376 psa_aead_abort( &operation );
5377
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005378 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005379
5380 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5381
5382 if( operation.alg == PSA_ALG_CCM )
5383 {
5384 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5385 input_data->len ),
5386 PSA_ERROR_INVALID_ARGUMENT );
5387 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5388 PSA_ERROR_BAD_STATE );
5389 }
5390 else
5391 {
5392 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5393 input_data->len ) );
5394 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5395 }
5396
5397 psa_aead_abort( &operation );
5398
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005399 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005400#if SIZE_MAX > UINT32_MAX
5401 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5402
5403 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5404 {
5405 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5406 input_data->len ),
5407 PSA_ERROR_INVALID_ARGUMENT );
5408 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5409 PSA_ERROR_BAD_STATE );
5410 }
5411 else
5412 {
5413 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5414 input_data->len ) );
5415 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5416 }
5417
5418 psa_aead_abort( &operation );
5419#endif
5420
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005421 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005422
5423 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5424
5425 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5426
5427 if( operation.alg == PSA_ALG_CCM )
5428 {
5429 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5430 input_data->len ),
5431 PSA_ERROR_INVALID_ARGUMENT );
5432 }
5433 else
5434 {
5435 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5436 input_data->len ) );
5437 }
5438
5439 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005440
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005441 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005442#if SIZE_MAX > UINT32_MAX
5443 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5444
5445 if( operation.alg == PSA_ALG_GCM )
5446 {
5447 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5448 SIZE_MAX ),
5449 PSA_ERROR_INVALID_ARGUMENT );
5450 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5451 PSA_ERROR_BAD_STATE );
5452 }
5453 else if ( operation.alg != PSA_ALG_CCM )
5454 {
5455 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5456 SIZE_MAX ) );
5457 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5458 }
5459
5460 psa_aead_abort( &operation );
5461
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005462 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005463 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5464
5465 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5466
5467 if( operation.alg == PSA_ALG_GCM )
5468 {
5469 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5470 SIZE_MAX ),
5471 PSA_ERROR_INVALID_ARGUMENT );
5472 }
5473 else if ( operation.alg != PSA_ALG_CCM )
5474 {
5475 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5476 SIZE_MAX ) );
5477 }
5478
5479 psa_aead_abort( &operation );
5480#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005481
5482 /* ------------------------------------------------------- */
5483
Paul Elliott374a2be2021-07-16 17:53:40 +01005484 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5485
5486 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5487
5488 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5489 PSA_AEAD_NONCE_MAX_SIZE,
5490 &nonce_length ),
5491 PSA_ERROR_BAD_STATE );
5492
5493 psa_aead_abort( &operation );
5494
Paul Elliott7220cae2021-06-22 17:25:57 +01005495 /* Test for generating nonce in decrypt setup. */
5496
Paul Elliott7220cae2021-06-22 17:25:57 +01005497 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5498
5499 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5500 PSA_AEAD_NONCE_MAX_SIZE,
5501 &nonce_length ),
5502 PSA_ERROR_BAD_STATE );
5503
5504 psa_aead_abort( &operation );
5505
Paul Elliottc23a9a02021-06-21 18:32:46 +01005506 /* Test for setting lengths twice. */
5507
Paul Elliottc23a9a02021-06-21 18:32:46 +01005508 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5509
5510 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5511
5512 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5513 input_data->len ) );
5514
5515 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5516 input_data->len ),
5517 PSA_ERROR_BAD_STATE );
5518
5519 psa_aead_abort( &operation );
5520
Andrzej Kurekad837522021-12-15 15:28:49 +01005521 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005522
Paul Elliottc23a9a02021-06-21 18:32:46 +01005523 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5524
5525 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5526
Andrzej Kurekad837522021-12-15 15:28:49 +01005527 if( operation.alg == PSA_ALG_CCM )
5528 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005529
Andrzej Kurekad837522021-12-15 15:28:49 +01005530 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5531 additional_data->len ),
5532 PSA_ERROR_BAD_STATE );
5533 }
5534 else
5535 {
5536 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5537 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005538
Andrzej Kurekad837522021-12-15 15:28:49 +01005539 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5540 input_data->len ),
5541 PSA_ERROR_BAD_STATE );
5542 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005543 psa_aead_abort( &operation );
5544
5545 /* ------------------------------------------------------- */
5546
Paul Elliottf94bd992021-09-19 18:15:59 +01005547 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5548
5549 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5550
Andrzej Kurekad837522021-12-15 15:28:49 +01005551 if( operation.alg == PSA_ALG_CCM )
5552 {
5553 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5554 input_data->len, output_data,
5555 output_size, &output_length ),
5556 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005557
Andrzej Kurekad837522021-12-15 15:28:49 +01005558 }
5559 else
5560 {
5561 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5562 input_data->len, output_data,
5563 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005564
Andrzej Kurekad837522021-12-15 15:28:49 +01005565 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5566 input_data->len ),
5567 PSA_ERROR_BAD_STATE );
5568 }
5569 psa_aead_abort( &operation );
5570
5571 /* ------------------------------------------------------- */
5572
5573 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5574
5575 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5576
5577 if( operation.alg == PSA_ALG_CCM )
5578 {
5579 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5580 finish_output_size,
5581 &output_part_length,
5582 tag_buffer, tag_length,
5583 &tag_size ) );
5584 }
5585 else
5586 {
5587 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5588 finish_output_size,
5589 &output_part_length,
5590 tag_buffer, tag_length,
5591 &tag_size ) );
5592
5593 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5594 input_data->len ),
5595 PSA_ERROR_BAD_STATE );
5596 }
5597 psa_aead_abort( &operation );
5598
5599 /* Test for setting lengths after generating nonce + already starting data. */
5600
5601 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5602
5603 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5604 PSA_AEAD_NONCE_MAX_SIZE,
5605 &nonce_length ) );
5606 if( operation.alg == PSA_ALG_CCM )
5607 {
5608
5609 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5610 additional_data->len ),
5611 PSA_ERROR_BAD_STATE );
5612 }
5613 else
5614 {
5615 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5616 additional_data->len ) );
5617
5618 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5619 input_data->len ),
5620 PSA_ERROR_BAD_STATE );
5621 }
5622 psa_aead_abort( &operation );
5623
5624 /* ------------------------------------------------------- */
5625
5626 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5627
5628 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5629 PSA_AEAD_NONCE_MAX_SIZE,
5630 &nonce_length ) );
5631 if( operation.alg == PSA_ALG_CCM )
5632 {
5633 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5634 input_data->len, output_data,
5635 output_size, &output_length ),
5636 PSA_ERROR_BAD_STATE );
5637
5638 }
5639 else
5640 {
5641 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5642 input_data->len, output_data,
5643 output_size, &output_length ) );
5644
5645 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5646 input_data->len ),
5647 PSA_ERROR_BAD_STATE );
5648 }
5649 psa_aead_abort( &operation );
5650
5651 /* ------------------------------------------------------- */
5652
5653 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5654
5655 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5656 PSA_AEAD_NONCE_MAX_SIZE,
5657 &nonce_length ) );
5658 if( operation.alg == PSA_ALG_CCM )
5659 {
5660 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5661 finish_output_size,
5662 &output_part_length,
5663 tag_buffer, tag_length,
5664 &tag_size ) );
5665 }
5666 else
5667 {
5668 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5669 finish_output_size,
5670 &output_part_length,
5671 tag_buffer, tag_length,
5672 &tag_size ) );
5673
5674 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5675 input_data->len ),
5676 PSA_ERROR_BAD_STATE );
5677 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005678 psa_aead_abort( &operation );
5679
Paul Elliott243080c2021-07-21 19:01:17 +01005680 /* Test for not sending any additional data or data after setting non zero
5681 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005682
Paul Elliottc23a9a02021-06-21 18:32:46 +01005683 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5684
5685 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5686
5687 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5688 input_data->len ) );
5689
5690 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5691 finish_output_size,
5692 &output_part_length,
5693 tag_buffer, tag_length,
5694 &tag_size ),
5695 PSA_ERROR_INVALID_ARGUMENT );
5696
5697 psa_aead_abort( &operation );
5698
Paul Elliott243080c2021-07-21 19:01:17 +01005699 /* Test for not sending any additional data or data after setting non-zero
5700 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005701
Paul Elliottc23a9a02021-06-21 18:32:46 +01005702 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5703
5704 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5705
5706 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5707 input_data->len ) );
5708
5709 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5710 finish_output_size,
5711 &output_part_length,
5712 tag_buffer,
5713 tag_length ),
5714 PSA_ERROR_INVALID_ARGUMENT );
5715
5716 psa_aead_abort( &operation );
5717
Paul Elliott243080c2021-07-21 19:01:17 +01005718 /* Test for not sending any additional data after setting a non-zero length
5719 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005720
Paul Elliottc23a9a02021-06-21 18:32:46 +01005721 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5722
5723 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5724
5725 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5726 input_data->len ) );
5727
5728 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5729 input_data->len, output_data,
5730 output_size, &output_length ),
5731 PSA_ERROR_INVALID_ARGUMENT );
5732
5733 psa_aead_abort( &operation );
5734
Paul Elliottf94bd992021-09-19 18:15:59 +01005735 /* Test for not sending any data after setting a non-zero length for it.*/
5736
Paul Elliottf94bd992021-09-19 18:15:59 +01005737 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5738
5739 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5740
5741 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5742 input_data->len ) );
5743
5744 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5745 additional_data->len ) );
5746
5747 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5748 finish_output_size,
5749 &output_part_length,
5750 tag_buffer, tag_length,
5751 &tag_size ),
5752 PSA_ERROR_INVALID_ARGUMENT );
5753
5754 psa_aead_abort( &operation );
5755
Paul Elliottb0450fe2021-09-01 15:06:26 +01005756 /* Test for sending too much additional data after setting lengths. */
5757
Paul Elliottb0450fe2021-09-01 15:06:26 +01005758 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5759
5760 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5761
5762 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5763
5764
5765 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5766 additional_data->len ),
5767 PSA_ERROR_INVALID_ARGUMENT );
5768
5769 psa_aead_abort( &operation );
5770
Paul Elliotta2a09b02021-09-22 14:56:40 +01005771 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005772
5773 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5774
5775 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5776
5777 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5778 input_data->len ) );
5779
5780 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5781 additional_data->len ) );
5782
5783 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5784 1 ),
5785 PSA_ERROR_INVALID_ARGUMENT );
5786
5787 psa_aead_abort( &operation );
5788
Paul Elliottb0450fe2021-09-01 15:06:26 +01005789 /* Test for sending too much data after setting lengths. */
5790
Paul Elliottb0450fe2021-09-01 15:06:26 +01005791 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5792
5793 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5794
5795 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5796
5797 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5798 input_data->len, output_data,
5799 output_size, &output_length ),
5800 PSA_ERROR_INVALID_ARGUMENT );
5801
5802 psa_aead_abort( &operation );
5803
Paul Elliotta2a09b02021-09-22 14:56:40 +01005804 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005805
5806 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5807
5808 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5809
5810 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5811 input_data->len ) );
5812
5813 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5814 additional_data->len ) );
5815
5816 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5817 input_data->len, output_data,
5818 output_size, &output_length ) );
5819
5820 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5821 1, output_data,
5822 output_size, &output_length ),
5823 PSA_ERROR_INVALID_ARGUMENT );
5824
5825 psa_aead_abort( &operation );
5826
Paul Elliottc23a9a02021-06-21 18:32:46 +01005827 /* Test sending additional data after data. */
5828
Paul Elliottc23a9a02021-06-21 18:32:46 +01005829 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5830
5831 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5832
Andrzej Kurekad837522021-12-15 15:28:49 +01005833 if( operation.alg != PSA_ALG_CCM )
5834 {
5835 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5836 input_data->len, output_data,
5837 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005838
Andrzej Kurekad837522021-12-15 15:28:49 +01005839 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5840 additional_data->len ),
5841 PSA_ERROR_BAD_STATE );
5842 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005843 psa_aead_abort( &operation );
5844
Paul Elliott534d0b42021-06-22 19:15:20 +01005845 /* Test calling finish on decryption. */
5846
Paul Elliott534d0b42021-06-22 19:15:20 +01005847 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5848
5849 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5850
5851 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5852 finish_output_size,
5853 &output_part_length,
5854 tag_buffer, tag_length,
5855 &tag_size ),
5856 PSA_ERROR_BAD_STATE );
5857
5858 psa_aead_abort( &operation );
5859
5860 /* Test calling verify on encryption. */
5861
Paul Elliott534d0b42021-06-22 19:15:20 +01005862 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5863
5864 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5865
5866 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5867 finish_output_size,
5868 &output_part_length,
5869 tag_buffer,
5870 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005871 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005872
5873 psa_aead_abort( &operation );
5874
5875
Paul Elliottc23a9a02021-06-21 18:32:46 +01005876exit:
5877 psa_destroy_key( key );
5878 psa_aead_abort( &operation );
5879 mbedtls_free( output_data );
5880 mbedtls_free( final_data );
5881 PSA_DONE( );
5882}
5883/* END_CASE */
5884
5885/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005886void signature_size( int type_arg,
5887 int bits,
5888 int alg_arg,
5889 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005890{
5891 psa_key_type_t type = type_arg;
5892 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005893 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005894
Gilles Peskinefe11b722018-12-18 00:24:04 +01005895 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005896
Gilles Peskinee59236f2018-01-27 23:32:46 +01005897exit:
5898 ;
5899}
5900/* END_CASE */
5901
5902/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005903void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5904 int alg_arg, data_t *input_data,
5905 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005906{
Ronald Cron5425a212020-08-04 14:58:35 +02005907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005908 psa_key_type_t key_type = key_type_arg;
5909 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005910 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005911 unsigned char *signature = NULL;
5912 size_t signature_size;
5913 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005915
Gilles Peskine8817f612018-12-18 00:18:46 +01005916 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005917
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005918 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005919 psa_set_key_algorithm( &attributes, alg );
5920 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005921
Gilles Peskine049c7532019-05-15 20:22:09 +02005922 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005923 &key ) );
5924 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005925 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005926
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005927 /* Allocate a buffer which has the size advertized by the
5928 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005929 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005930 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005931 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005932 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005933 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005934
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005935 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005936 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005937 input_data->x, input_data->len,
5938 signature, signature_size,
5939 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005940 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005941 ASSERT_COMPARE( output_data->x, output_data->len,
5942 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005943
5944exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005945 /*
5946 * Key attributes may have been returned by psa_get_key_attributes()
5947 * thus reset them as required.
5948 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005949 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005950
Ronald Cron5425a212020-08-04 14:58:35 +02005951 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005952 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005953 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005954}
5955/* END_CASE */
5956
5957/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005958void sign_hash_fail( int key_type_arg, data_t *key_data,
5959 int alg_arg, data_t *input_data,
5960 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005961{
Ronald Cron5425a212020-08-04 14:58:35 +02005962 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005963 psa_key_type_t key_type = key_type_arg;
5964 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005965 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005966 psa_status_t actual_status;
5967 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005968 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005969 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005970 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005971
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005972 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005973
Gilles Peskine8817f612018-12-18 00:18:46 +01005974 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005975
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005976 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005977 psa_set_key_algorithm( &attributes, alg );
5978 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005979
Gilles Peskine049c7532019-05-15 20:22:09 +02005980 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005981 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005982
Ronald Cron5425a212020-08-04 14:58:35 +02005983 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005984 input_data->x, input_data->len,
5985 signature, signature_size,
5986 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005987 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005988 /* The value of *signature_length is unspecified on error, but
5989 * whatever it is, it should be less than signature_size, so that
5990 * if the caller tries to read *signature_length bytes without
5991 * checking the error code then they don't overflow a buffer. */
5992 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005993
5994exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005995 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005996 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005997 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005998 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005999}
6000/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006001
6002/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006003void sign_verify_hash( int key_type_arg, data_t *key_data,
6004 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006005{
Ronald Cron5425a212020-08-04 14:58:35 +02006006 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006007 psa_key_type_t key_type = key_type_arg;
6008 psa_algorithm_t alg = alg_arg;
6009 size_t key_bits;
6010 unsigned char *signature = NULL;
6011 size_t signature_size;
6012 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006014
Gilles Peskine8817f612018-12-18 00:18:46 +01006015 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006016
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006017 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006018 psa_set_key_algorithm( &attributes, alg );
6019 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006020
Gilles Peskine049c7532019-05-15 20:22:09 +02006021 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006022 &key ) );
6023 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006024 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006025
6026 /* Allocate a buffer which has the size advertized by the
6027 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006028 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006029 key_bits, alg );
6030 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006031 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006032 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006033
6034 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006035 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006036 input_data->x, input_data->len,
6037 signature, signature_size,
6038 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006039 /* Check that the signature length looks sensible. */
6040 TEST_ASSERT( signature_length <= signature_size );
6041 TEST_ASSERT( signature_length > 0 );
6042
6043 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006044 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006045 input_data->x, input_data->len,
6046 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006047
6048 if( input_data->len != 0 )
6049 {
6050 /* Flip a bit in the input and verify that the signature is now
6051 * detected as invalid. Flip a bit at the beginning, not at the end,
6052 * because ECDSA may ignore the last few bits of the input. */
6053 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006054 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006055 input_data->x, input_data->len,
6056 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006057 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006058 }
6059
6060exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006061 /*
6062 * Key attributes may have been returned by psa_get_key_attributes()
6063 * thus reset them as required.
6064 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006065 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006066
Ronald Cron5425a212020-08-04 14:58:35 +02006067 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006068 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006069 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006070}
6071/* END_CASE */
6072
6073/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006074void verify_hash( int key_type_arg, data_t *key_data,
6075 int alg_arg, data_t *hash_data,
6076 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006077{
Ronald Cron5425a212020-08-04 14:58:35 +02006078 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006079 psa_key_type_t key_type = key_type_arg;
6080 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006081 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006082
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006083 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006084
Gilles Peskine8817f612018-12-18 00:18:46 +01006085 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006086
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006087 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006088 psa_set_key_algorithm( &attributes, alg );
6089 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006090
Gilles Peskine049c7532019-05-15 20:22:09 +02006091 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006092 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006093
Ronald Cron5425a212020-08-04 14:58:35 +02006094 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006095 hash_data->x, hash_data->len,
6096 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006097
itayzafrir5c753392018-05-08 11:18:38 +03006098exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006099 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006100 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006101 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006102}
6103/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006104
6105/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006106void verify_hash_fail( int key_type_arg, data_t *key_data,
6107 int alg_arg, data_t *hash_data,
6108 data_t *signature_data,
6109 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006110{
Ronald Cron5425a212020-08-04 14:58:35 +02006111 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006112 psa_key_type_t key_type = key_type_arg;
6113 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006114 psa_status_t actual_status;
6115 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006116 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006117
Gilles Peskine8817f612018-12-18 00:18:46 +01006118 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006119
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006120 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006121 psa_set_key_algorithm( &attributes, alg );
6122 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006123
Gilles Peskine049c7532019-05-15 20:22:09 +02006124 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006125 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006126
Ronald Cron5425a212020-08-04 14:58:35 +02006127 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006128 hash_data->x, hash_data->len,
6129 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006130 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006131
6132exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006133 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006134 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006135 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006136}
6137/* END_CASE */
6138
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006139/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006140void sign_message_deterministic( int key_type_arg,
6141 data_t *key_data,
6142 int alg_arg,
6143 data_t *input_data,
6144 data_t *output_data )
6145{
6146 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6147 psa_key_type_t key_type = key_type_arg;
6148 psa_algorithm_t alg = alg_arg;
6149 size_t key_bits;
6150 unsigned char *signature = NULL;
6151 size_t signature_size;
6152 size_t signature_length = 0xdeadbeef;
6153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6154
6155 PSA_ASSERT( psa_crypto_init( ) );
6156
6157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6158 psa_set_key_algorithm( &attributes, alg );
6159 psa_set_key_type( &attributes, key_type );
6160
6161 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6162 &key ) );
6163 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6164 key_bits = psa_get_key_bits( &attributes );
6165
6166 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6167 TEST_ASSERT( signature_size != 0 );
6168 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
6169 ASSERT_ALLOC( signature, signature_size );
6170
6171 PSA_ASSERT( psa_sign_message( key, alg,
6172 input_data->x, input_data->len,
6173 signature, signature_size,
6174 &signature_length ) );
6175
6176 ASSERT_COMPARE( output_data->x, output_data->len,
6177 signature, signature_length );
6178
6179exit:
6180 psa_reset_key_attributes( &attributes );
6181
6182 psa_destroy_key( key );
6183 mbedtls_free( signature );
6184 PSA_DONE( );
6185
6186}
6187/* END_CASE */
6188
6189/* BEGIN_CASE */
6190void sign_message_fail( int key_type_arg,
6191 data_t *key_data,
6192 int alg_arg,
6193 data_t *input_data,
6194 int signature_size_arg,
6195 int expected_status_arg )
6196{
6197 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6198 psa_key_type_t key_type = key_type_arg;
6199 psa_algorithm_t alg = alg_arg;
6200 size_t signature_size = signature_size_arg;
6201 psa_status_t actual_status;
6202 psa_status_t expected_status = expected_status_arg;
6203 unsigned char *signature = NULL;
6204 size_t signature_length = 0xdeadbeef;
6205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6206
6207 ASSERT_ALLOC( signature, signature_size );
6208
6209 PSA_ASSERT( psa_crypto_init( ) );
6210
6211 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6212 psa_set_key_algorithm( &attributes, alg );
6213 psa_set_key_type( &attributes, key_type );
6214
6215 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6216 &key ) );
6217
6218 actual_status = psa_sign_message( key, alg,
6219 input_data->x, input_data->len,
6220 signature, signature_size,
6221 &signature_length );
6222 TEST_EQUAL( actual_status, expected_status );
6223 /* The value of *signature_length is unspecified on error, but
6224 * whatever it is, it should be less than signature_size, so that
6225 * if the caller tries to read *signature_length bytes without
6226 * checking the error code then they don't overflow a buffer. */
6227 TEST_ASSERT( signature_length <= signature_size );
6228
6229exit:
6230 psa_reset_key_attributes( &attributes );
6231 psa_destroy_key( key );
6232 mbedtls_free( signature );
6233 PSA_DONE( );
6234}
6235/* END_CASE */
6236
6237/* BEGIN_CASE */
6238void sign_verify_message( int key_type_arg,
6239 data_t *key_data,
6240 int alg_arg,
6241 data_t *input_data )
6242{
6243 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6244 psa_key_type_t key_type = key_type_arg;
6245 psa_algorithm_t alg = alg_arg;
6246 size_t key_bits;
6247 unsigned char *signature = NULL;
6248 size_t signature_size;
6249 size_t signature_length = 0xdeadbeef;
6250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6251
6252 PSA_ASSERT( psa_crypto_init( ) );
6253
6254 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6255 PSA_KEY_USAGE_VERIFY_MESSAGE );
6256 psa_set_key_algorithm( &attributes, alg );
6257 psa_set_key_type( &attributes, key_type );
6258
6259 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6260 &key ) );
6261 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6262 key_bits = psa_get_key_bits( &attributes );
6263
6264 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6265 TEST_ASSERT( signature_size != 0 );
6266 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
6267 ASSERT_ALLOC( signature, signature_size );
6268
6269 PSA_ASSERT( psa_sign_message( key, alg,
6270 input_data->x, input_data->len,
6271 signature, signature_size,
6272 &signature_length ) );
6273 TEST_ASSERT( signature_length <= signature_size );
6274 TEST_ASSERT( signature_length > 0 );
6275
6276 PSA_ASSERT( psa_verify_message( key, alg,
6277 input_data->x, input_data->len,
6278 signature, signature_length ) );
6279
6280 if( input_data->len != 0 )
6281 {
6282 /* Flip a bit in the input and verify that the signature is now
6283 * detected as invalid. Flip a bit at the beginning, not at the end,
6284 * because ECDSA may ignore the last few bits of the input. */
6285 input_data->x[0] ^= 1;
6286 TEST_EQUAL( psa_verify_message( key, alg,
6287 input_data->x, input_data->len,
6288 signature, signature_length ),
6289 PSA_ERROR_INVALID_SIGNATURE );
6290 }
6291
6292exit:
6293 psa_reset_key_attributes( &attributes );
6294
6295 psa_destroy_key( key );
6296 mbedtls_free( signature );
6297 PSA_DONE( );
6298}
6299/* END_CASE */
6300
6301/* BEGIN_CASE */
6302void verify_message( int key_type_arg,
6303 data_t *key_data,
6304 int alg_arg,
6305 data_t *input_data,
6306 data_t *signature_data )
6307{
6308 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6309 psa_key_type_t key_type = key_type_arg;
6310 psa_algorithm_t alg = alg_arg;
6311 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6312
6313 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
6314
6315 PSA_ASSERT( psa_crypto_init( ) );
6316
6317 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6318 psa_set_key_algorithm( &attributes, alg );
6319 psa_set_key_type( &attributes, key_type );
6320
6321 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6322 &key ) );
6323
6324 PSA_ASSERT( psa_verify_message( key, alg,
6325 input_data->x, input_data->len,
6326 signature_data->x, signature_data->len ) );
6327
6328exit:
6329 psa_reset_key_attributes( &attributes );
6330 psa_destroy_key( key );
6331 PSA_DONE( );
6332}
6333/* END_CASE */
6334
6335/* BEGIN_CASE */
6336void verify_message_fail( int key_type_arg,
6337 data_t *key_data,
6338 int alg_arg,
6339 data_t *hash_data,
6340 data_t *signature_data,
6341 int expected_status_arg )
6342{
6343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6344 psa_key_type_t key_type = key_type_arg;
6345 psa_algorithm_t alg = alg_arg;
6346 psa_status_t actual_status;
6347 psa_status_t expected_status = expected_status_arg;
6348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6349
6350 PSA_ASSERT( psa_crypto_init( ) );
6351
6352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6353 psa_set_key_algorithm( &attributes, alg );
6354 psa_set_key_type( &attributes, key_type );
6355
6356 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6357 &key ) );
6358
6359 actual_status = psa_verify_message( key, alg,
6360 hash_data->x, hash_data->len,
6361 signature_data->x,
6362 signature_data->len );
6363 TEST_EQUAL( actual_status, expected_status );
6364
6365exit:
6366 psa_reset_key_attributes( &attributes );
6367 psa_destroy_key( key );
6368 PSA_DONE( );
6369}
6370/* END_CASE */
6371
6372/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006373void asymmetric_encrypt( int key_type_arg,
6374 data_t *key_data,
6375 int alg_arg,
6376 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006377 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006378 int expected_output_length_arg,
6379 int expected_status_arg )
6380{
Ronald Cron5425a212020-08-04 14:58:35 +02006381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006382 psa_key_type_t key_type = key_type_arg;
6383 psa_algorithm_t alg = alg_arg;
6384 size_t expected_output_length = expected_output_length_arg;
6385 size_t key_bits;
6386 unsigned char *output = NULL;
6387 size_t output_size;
6388 size_t output_length = ~0;
6389 psa_status_t actual_status;
6390 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006391 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006392
Gilles Peskine8817f612018-12-18 00:18:46 +01006393 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006394
Gilles Peskine656896e2018-06-29 19:12:28 +02006395 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006396 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6397 psa_set_key_algorithm( &attributes, alg );
6398 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006399 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006400 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006401
6402 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006403 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006404 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006405
Gilles Peskine656896e2018-06-29 19:12:28 +02006406 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006407 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006408 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006409
6410 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006411 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006412 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006413 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006414 output, output_size,
6415 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006416 TEST_EQUAL( actual_status, expected_status );
6417 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006418
Gilles Peskine68428122018-06-30 18:42:41 +02006419 /* If the label is empty, the test framework puts a non-null pointer
6420 * in label->x. Test that a null pointer works as well. */
6421 if( label->len == 0 )
6422 {
6423 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006424 if( output_size != 0 )
6425 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006426 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006427 input_data->x, input_data->len,
6428 NULL, label->len,
6429 output, output_size,
6430 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006431 TEST_EQUAL( actual_status, expected_status );
6432 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006433 }
6434
Gilles Peskine656896e2018-06-29 19:12:28 +02006435exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006436 /*
6437 * Key attributes may have been returned by psa_get_key_attributes()
6438 * thus reset them as required.
6439 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006440 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006441
Ronald Cron5425a212020-08-04 14:58:35 +02006442 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006443 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006444 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006445}
6446/* END_CASE */
6447
6448/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006449void asymmetric_encrypt_decrypt( int key_type_arg,
6450 data_t *key_data,
6451 int alg_arg,
6452 data_t *input_data,
6453 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006454{
Ronald Cron5425a212020-08-04 14:58:35 +02006455 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006456 psa_key_type_t key_type = key_type_arg;
6457 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006458 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006459 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006460 size_t output_size;
6461 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006462 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006463 size_t output2_size;
6464 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006466
Gilles Peskine8817f612018-12-18 00:18:46 +01006467 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006468
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006469 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6470 psa_set_key_algorithm( &attributes, alg );
6471 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006472
Gilles Peskine049c7532019-05-15 20:22:09 +02006473 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006474 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006475
6476 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006477 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006478 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006479
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006480 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006481 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006482 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006483
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006484 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006485 TEST_ASSERT( output2_size <=
6486 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6487 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006488 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006489
Gilles Peskineeebd7382018-06-08 18:11:54 +02006490 /* We test encryption by checking that encrypt-then-decrypt gives back
6491 * the original plaintext because of the non-optional random
6492 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006493 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006494 input_data->x, input_data->len,
6495 label->x, label->len,
6496 output, output_size,
6497 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006498 /* We don't know what ciphertext length to expect, but check that
6499 * it looks sensible. */
6500 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006501
Ronald Cron5425a212020-08-04 14:58:35 +02006502 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006503 output, output_length,
6504 label->x, label->len,
6505 output2, output2_size,
6506 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006507 ASSERT_COMPARE( input_data->x, input_data->len,
6508 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006509
6510exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006511 /*
6512 * Key attributes may have been returned by psa_get_key_attributes()
6513 * thus reset them as required.
6514 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006515 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006516
Ronald Cron5425a212020-08-04 14:58:35 +02006517 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006518 mbedtls_free( output );
6519 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006520 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006521}
6522/* END_CASE */
6523
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006524/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006525void asymmetric_decrypt( int key_type_arg,
6526 data_t *key_data,
6527 int alg_arg,
6528 data_t *input_data,
6529 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006530 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006531{
Ronald Cron5425a212020-08-04 14:58:35 +02006532 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006533 psa_key_type_t key_type = key_type_arg;
6534 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006535 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006536 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006537 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006538 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006540
Gilles Peskine8817f612018-12-18 00:18:46 +01006541 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006542
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006543 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6544 psa_set_key_algorithm( &attributes, alg );
6545 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006546
Gilles Peskine049c7532019-05-15 20:22:09 +02006547 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006548 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006549
gabor-mezei-armceface22021-01-21 12:26:17 +01006550 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6551 key_bits = psa_get_key_bits( &attributes );
6552
6553 /* Determine the maximum ciphertext length */
6554 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6555 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6556 ASSERT_ALLOC( output, output_size );
6557
Ronald Cron5425a212020-08-04 14:58:35 +02006558 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006559 input_data->x, input_data->len,
6560 label->x, label->len,
6561 output,
6562 output_size,
6563 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006564 ASSERT_COMPARE( expected_data->x, expected_data->len,
6565 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006566
Gilles Peskine68428122018-06-30 18:42:41 +02006567 /* If the label is empty, the test framework puts a non-null pointer
6568 * in label->x. Test that a null pointer works as well. */
6569 if( label->len == 0 )
6570 {
6571 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006572 if( output_size != 0 )
6573 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006574 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006575 input_data->x, input_data->len,
6576 NULL, label->len,
6577 output,
6578 output_size,
6579 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006580 ASSERT_COMPARE( expected_data->x, expected_data->len,
6581 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006582 }
6583
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006584exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006585 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006586 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006587 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006588 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006589}
6590/* END_CASE */
6591
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006592/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006593void asymmetric_decrypt_fail( int key_type_arg,
6594 data_t *key_data,
6595 int alg_arg,
6596 data_t *input_data,
6597 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006598 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006599 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006600{
Ronald Cron5425a212020-08-04 14:58:35 +02006601 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006602 psa_key_type_t key_type = key_type_arg;
6603 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006604 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006605 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006606 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006607 psa_status_t actual_status;
6608 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006610
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006611 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006612
Gilles Peskine8817f612018-12-18 00:18:46 +01006613 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006614
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006615 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6616 psa_set_key_algorithm( &attributes, alg );
6617 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006618
Gilles Peskine049c7532019-05-15 20:22:09 +02006619 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006620 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006621
Ronald Cron5425a212020-08-04 14:58:35 +02006622 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006623 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006624 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006625 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006626 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006627 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006628 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006629
Gilles Peskine68428122018-06-30 18:42:41 +02006630 /* If the label is empty, the test framework puts a non-null pointer
6631 * in label->x. Test that a null pointer works as well. */
6632 if( label->len == 0 )
6633 {
6634 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006635 if( output_size != 0 )
6636 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006637 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006638 input_data->x, input_data->len,
6639 NULL, label->len,
6640 output, output_size,
6641 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006642 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006643 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006644 }
6645
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006646exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006647 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006648 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006649 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006650 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006651}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006652/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006653
6654/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006655void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006656{
6657 /* Test each valid way of initializing the object, except for `= {0}`, as
6658 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6659 * though it's OK by the C standard. We could test for this, but we'd need
6660 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006661 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006662 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6663 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6664 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006665
6666 memset( &zero, 0, sizeof( zero ) );
6667
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006668 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006669 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006670 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006671 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006672 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006673 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006674 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006675
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006676 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006677 PSA_ASSERT( psa_key_derivation_abort(&func) );
6678 PSA_ASSERT( psa_key_derivation_abort(&init) );
6679 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006680}
6681/* END_CASE */
6682
Janos Follath16de4a42019-06-13 16:32:24 +01006683/* BEGIN_CASE */
6684void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006685{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006686 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006687 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006688 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006689
Gilles Peskine8817f612018-12-18 00:18:46 +01006690 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006691
Janos Follath16de4a42019-06-13 16:32:24 +01006692 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006693 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006694
6695exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006696 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006697 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006698}
6699/* END_CASE */
6700
Janos Follathaf3c2a02019-06-12 12:34:34 +01006701/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006702void derive_set_capacity( int alg_arg, int capacity_arg,
6703 int expected_status_arg )
6704{
6705 psa_algorithm_t alg = alg_arg;
6706 size_t capacity = capacity_arg;
6707 psa_status_t expected_status = expected_status_arg;
6708 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6709
6710 PSA_ASSERT( psa_crypto_init( ) );
6711
6712 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6713
6714 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6715 expected_status );
6716
6717exit:
6718 psa_key_derivation_abort( &operation );
6719 PSA_DONE( );
6720}
6721/* END_CASE */
6722
6723/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006724void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006725 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006726 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006727 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006728 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006729 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006730 int expected_status_arg3,
6731 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006732{
6733 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006734 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6735 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006736 psa_status_t expected_statuses[] = {expected_status_arg1,
6737 expected_status_arg2,
6738 expected_status_arg3};
6739 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006740 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6741 MBEDTLS_SVC_KEY_ID_INIT,
6742 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006743 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6745 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006746 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006747 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006748 psa_status_t expected_output_status = expected_output_status_arg;
6749 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006750
6751 PSA_ASSERT( psa_crypto_init( ) );
6752
6753 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6754 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006755
6756 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6757
6758 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6759 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006760 mbedtls_test_set_step( i );
6761 if( steps[i] == 0 )
6762 {
6763 /* Skip this step */
6764 }
6765 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006766 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006767 psa_set_key_type( &attributes, key_types[i] );
6768 PSA_ASSERT( psa_import_key( &attributes,
6769 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006770 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006771 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6772 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6773 {
6774 // When taking a private key as secret input, use key agreement
6775 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006776 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6777 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006778 expected_statuses[i] );
6779 }
6780 else
6781 {
6782 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006783 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006784 expected_statuses[i] );
6785 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006786 }
6787 else
6788 {
6789 TEST_EQUAL( psa_key_derivation_input_bytes(
6790 &operation, steps[i],
6791 inputs[i]->x, inputs[i]->len ),
6792 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006793 }
6794 }
6795
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006796 if( output_key_type != PSA_KEY_TYPE_NONE )
6797 {
6798 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006799 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006800 psa_set_key_bits( &attributes, 8 );
6801 actual_output_status =
6802 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006803 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006804 }
6805 else
6806 {
6807 uint8_t buffer[1];
6808 actual_output_status =
6809 psa_key_derivation_output_bytes( &operation,
6810 buffer, sizeof( buffer ) );
6811 }
6812 TEST_EQUAL( actual_output_status, expected_output_status );
6813
Janos Follathaf3c2a02019-06-12 12:34:34 +01006814exit:
6815 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006816 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6817 psa_destroy_key( keys[i] );
6818 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006819 PSA_DONE( );
6820}
6821/* END_CASE */
6822
Janos Follathd958bb72019-07-03 15:02:16 +01006823/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006824void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006825{
Janos Follathd958bb72019-07-03 15:02:16 +01006826 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006827 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006828 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006829 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006830 unsigned char input1[] = "Input 1";
6831 size_t input1_length = sizeof( input1 );
6832 unsigned char input2[] = "Input 2";
6833 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006834 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006835 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006836 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6837 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6838 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006839 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006840
Gilles Peskine8817f612018-12-18 00:18:46 +01006841 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006842
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6844 psa_set_key_algorithm( &attributes, alg );
6845 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006846
Gilles Peskine73676cb2019-05-15 20:15:10 +02006847 PSA_ASSERT( psa_import_key( &attributes,
6848 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006849 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006850
6851 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006852 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6853 input1, input1_length,
6854 input2, input2_length,
6855 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006856 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006857
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006858 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006859 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006860 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006861
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006862 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006863
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006864 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006865 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006866
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006867exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006868 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006869 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006870 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006871}
6872/* END_CASE */
6873
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006874/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006875void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006876{
6877 uint8_t output_buffer[16];
6878 size_t buffer_size = 16;
6879 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006880 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006881
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006882 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6883 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006884 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006885
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006886 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006887 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006888
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006889 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006890
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006891 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6892 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006893 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006894
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006895 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006896 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006897
6898exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006899 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006900}
6901/* END_CASE */
6902
6903/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006904void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006905 int step1_arg, data_t *input1,
6906 int step2_arg, data_t *input2,
6907 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006908 int requested_capacity_arg,
6909 data_t *expected_output1,
6910 data_t *expected_output2 )
6911{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006912 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006913 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6914 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006915 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6916 MBEDTLS_SVC_KEY_ID_INIT,
6917 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006918 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006919 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006920 uint8_t *expected_outputs[2] =
6921 {expected_output1->x, expected_output2->x};
6922 size_t output_sizes[2] =
6923 {expected_output1->len, expected_output2->len};
6924 size_t output_buffer_size = 0;
6925 uint8_t *output_buffer = NULL;
6926 size_t expected_capacity;
6927 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006929 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006930 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006931
6932 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6933 {
6934 if( output_sizes[i] > output_buffer_size )
6935 output_buffer_size = output_sizes[i];
6936 if( output_sizes[i] == 0 )
6937 expected_outputs[i] = NULL;
6938 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006939 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006940 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006941
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006942 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6943 psa_set_key_algorithm( &attributes, alg );
6944 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006945
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006946 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006947 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6948 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6949 requested_capacity ) );
6950 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006951 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006952 switch( steps[i] )
6953 {
6954 case 0:
6955 break;
6956 case PSA_KEY_DERIVATION_INPUT_SECRET:
6957 PSA_ASSERT( psa_import_key( &attributes,
6958 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006959 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006960
6961 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6962 {
6963 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6964 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6965 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6966 }
6967
Gilles Peskine1468da72019-05-29 17:35:49 +02006968 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006969 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006970 break;
6971 default:
6972 PSA_ASSERT( psa_key_derivation_input_bytes(
6973 &operation, steps[i],
6974 inputs[i]->x, inputs[i]->len ) );
6975 break;
6976 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006977 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006978
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006979 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006980 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006981 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006982 expected_capacity = requested_capacity;
6983
6984 /* Expansion phase. */
6985 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6986 {
6987 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006988 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006989 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006990 if( expected_capacity == 0 && output_sizes[i] == 0 )
6991 {
6992 /* Reading 0 bytes when 0 bytes are available can go either way. */
6993 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006994 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006995 continue;
6996 }
6997 else if( expected_capacity == 0 ||
6998 output_sizes[i] > expected_capacity )
6999 {
7000 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02007001 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007002 expected_capacity = 0;
7003 continue;
7004 }
7005 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01007006 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007007 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007008 ASSERT_COMPARE( output_buffer, output_sizes[i],
7009 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007010 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007011 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007012 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007013 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007014 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007015 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007016 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007017
7018exit:
7019 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007020 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007021 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7022 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007023 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007024}
7025/* END_CASE */
7026
7027/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007028void derive_full( int alg_arg,
7029 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007030 data_t *input1,
7031 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007032 int requested_capacity_arg )
7033{
Ronald Cron5425a212020-08-04 14:58:35 +02007034 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007035 psa_algorithm_t alg = alg_arg;
7036 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007037 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007038 unsigned char output_buffer[16];
7039 size_t expected_capacity = requested_capacity;
7040 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007041 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007042
Gilles Peskine8817f612018-12-18 00:18:46 +01007043 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007044
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007045 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7046 psa_set_key_algorithm( &attributes, alg );
7047 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007048
Gilles Peskine049c7532019-05-15 20:22:09 +02007049 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007050 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007051
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007052 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7053 input1->x, input1->len,
7054 input2->x, input2->len,
7055 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007056 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007057
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007058 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007059 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007060 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007061
7062 /* Expansion phase. */
7063 while( current_capacity > 0 )
7064 {
7065 size_t read_size = sizeof( output_buffer );
7066 if( read_size > current_capacity )
7067 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007068 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007069 output_buffer,
7070 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007071 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007072 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007073 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007074 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007075 }
7076
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007077 /* Check that the operation refuses to go over capacity. */
7078 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007079 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007080
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007081 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007082
7083exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007084 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007085 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007086 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007087}
7088/* END_CASE */
7089
Janos Follathe60c9052019-07-03 13:51:30 +01007090/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007091void derive_key_exercise( int alg_arg,
7092 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007093 data_t *input1,
7094 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007095 int derived_type_arg,
7096 int derived_bits_arg,
7097 int derived_usage_arg,
7098 int derived_alg_arg )
7099{
Ronald Cron5425a212020-08-04 14:58:35 +02007100 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7101 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007102 psa_algorithm_t alg = alg_arg;
7103 psa_key_type_t derived_type = derived_type_arg;
7104 size_t derived_bits = derived_bits_arg;
7105 psa_key_usage_t derived_usage = derived_usage_arg;
7106 psa_algorithm_t derived_alg = derived_alg_arg;
7107 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007108 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007110 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007111
Gilles Peskine8817f612018-12-18 00:18:46 +01007112 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007113
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007114 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7115 psa_set_key_algorithm( &attributes, alg );
7116 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007117 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007118 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007119
7120 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007121 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7122 input1->x, input1->len,
7123 input2->x, input2->len,
7124 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007125 goto exit;
7126
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007127 psa_set_key_usage_flags( &attributes, derived_usage );
7128 psa_set_key_algorithm( &attributes, derived_alg );
7129 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007130 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007131 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007132 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007133
7134 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007135 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007136 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7137 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007138
7139 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007140 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007141 goto exit;
7142
7143exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007144 /*
7145 * Key attributes may have been returned by psa_get_key_attributes()
7146 * thus reset them as required.
7147 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007148 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007149
7150 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007151 psa_destroy_key( base_key );
7152 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007153 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007154}
7155/* END_CASE */
7156
Janos Follath42fd8882019-07-03 14:17:09 +01007157/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007158void derive_key_export( int alg_arg,
7159 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007160 data_t *input1,
7161 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007162 int bytes1_arg,
7163 int bytes2_arg )
7164{
Ronald Cron5425a212020-08-04 14:58:35 +02007165 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7166 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007167 psa_algorithm_t alg = alg_arg;
7168 size_t bytes1 = bytes1_arg;
7169 size_t bytes2 = bytes2_arg;
7170 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007171 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007172 uint8_t *output_buffer = NULL;
7173 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007174 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7175 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007176 size_t length;
7177
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007178 ASSERT_ALLOC( output_buffer, capacity );
7179 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007180 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007181
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007182 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7183 psa_set_key_algorithm( &base_attributes, alg );
7184 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007185 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007186 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007187
7188 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007189 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7190 input1->x, input1->len,
7191 input2->x, input2->len,
7192 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007193 goto exit;
7194
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007195 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007196 output_buffer,
7197 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007198 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007199
7200 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007201 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7202 input1->x, input1->len,
7203 input2->x, input2->len,
7204 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007205 goto exit;
7206
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007207 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7208 psa_set_key_algorithm( &derived_attributes, 0 );
7209 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007210 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007211 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007212 &derived_key ) );
7213 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007214 export_buffer, bytes1,
7215 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007216 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007217 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007218 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007219 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007220 &derived_key ) );
7221 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007222 export_buffer + bytes1, bytes2,
7223 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007224 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007225
7226 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007227 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7228 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007229
7230exit:
7231 mbedtls_free( output_buffer );
7232 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007233 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007234 psa_destroy_key( base_key );
7235 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007236 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007237}
7238/* END_CASE */
7239
7240/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007241void derive_key_type( int alg_arg,
7242 data_t *key_data,
7243 data_t *input1,
7244 data_t *input2,
7245 int key_type_arg, int bits_arg,
7246 data_t *expected_export )
7247{
7248 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7249 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
7250 const psa_algorithm_t alg = alg_arg;
7251 const psa_key_type_t key_type = key_type_arg;
7252 const size_t bits = bits_arg;
7253 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7254 const size_t export_buffer_size =
7255 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
7256 uint8_t *export_buffer = NULL;
7257 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7258 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7259 size_t export_length;
7260
7261 ASSERT_ALLOC( export_buffer, export_buffer_size );
7262 PSA_ASSERT( psa_crypto_init( ) );
7263
7264 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7265 psa_set_key_algorithm( &base_attributes, alg );
7266 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7267 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
7268 &base_key ) );
7269
Przemek Stekielc85f0912022-03-08 11:37:54 +01007270 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007271 &operation, base_key, alg,
7272 input1->x, input1->len,
7273 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01007274 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007275 goto exit;
7276
7277 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7278 psa_set_key_algorithm( &derived_attributes, 0 );
7279 psa_set_key_type( &derived_attributes, key_type );
7280 psa_set_key_bits( &derived_attributes, bits );
7281 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
7282 &derived_key ) );
7283
7284 PSA_ASSERT( psa_export_key( derived_key,
7285 export_buffer, export_buffer_size,
7286 &export_length ) );
7287 ASSERT_COMPARE( export_buffer, export_length,
7288 expected_export->x, expected_export->len );
7289
7290exit:
7291 mbedtls_free( export_buffer );
7292 psa_key_derivation_abort( &operation );
7293 psa_destroy_key( base_key );
7294 psa_destroy_key( derived_key );
7295 PSA_DONE( );
7296}
7297/* END_CASE */
7298
7299/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007300void derive_key( int alg_arg,
7301 data_t *key_data, data_t *input1, data_t *input2,
7302 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007303 int expected_status_arg,
7304 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02007305{
Ronald Cron5425a212020-08-04 14:58:35 +02007306 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7307 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02007308 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007309 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02007310 size_t bits = bits_arg;
7311 psa_status_t expected_status = expected_status_arg;
7312 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7313 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7314 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7315
7316 PSA_ASSERT( psa_crypto_init( ) );
7317
7318 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7319 psa_set_key_algorithm( &base_attributes, alg );
7320 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7321 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007322 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02007323
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007324 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7325 input1->x, input1->len,
7326 input2->x, input2->len,
7327 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02007328 goto exit;
7329
7330 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7331 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007332 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02007333 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01007334
7335 psa_status_t status =
7336 psa_key_derivation_output_key( &derived_attributes,
7337 &operation,
7338 &derived_key );
7339 if( is_large_output > 0 )
7340 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7341 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02007342
7343exit:
7344 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007345 psa_destroy_key( base_key );
7346 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02007347 PSA_DONE( );
7348}
7349/* END_CASE */
7350
7351/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02007352void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02007353 int our_key_type_arg, int our_key_alg_arg,
7354 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02007355 int expected_status_arg )
7356{
Ronald Cron5425a212020-08-04 14:58:35 +02007357 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007358 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007359 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007360 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007361 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007363 psa_status_t expected_status = expected_status_arg;
7364 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007365
Gilles Peskine8817f612018-12-18 00:18:46 +01007366 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007367
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007368 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007369 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007370 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007371 PSA_ASSERT( psa_import_key( &attributes,
7372 our_key_data->x, our_key_data->len,
7373 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007374
Gilles Peskine77f40d82019-04-11 21:27:06 +02007375 /* The tests currently include inputs that should fail at either step.
7376 * Test cases that fail at the setup step should be changed to call
7377 * key_derivation_setup instead, and this function should be renamed
7378 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007379 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02007380 if( status == PSA_SUCCESS )
7381 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007382 TEST_EQUAL( psa_key_derivation_key_agreement(
7383 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
7384 our_key,
7385 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02007386 expected_status );
7387 }
7388 else
7389 {
7390 TEST_ASSERT( status == expected_status );
7391 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007392
7393exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007394 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007395 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007396 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007397}
7398/* END_CASE */
7399
7400/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007401void raw_key_agreement( int alg_arg,
7402 int our_key_type_arg, data_t *our_key_data,
7403 data_t *peer_key_data,
7404 data_t *expected_output )
7405{
Ronald Cron5425a212020-08-04 14:58:35 +02007406 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007407 psa_algorithm_t alg = alg_arg;
7408 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007410 unsigned char *output = NULL;
7411 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007412 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007413
7414 ASSERT_ALLOC( output, expected_output->len );
7415 PSA_ASSERT( psa_crypto_init( ) );
7416
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007417 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7418 psa_set_key_algorithm( &attributes, alg );
7419 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007420 PSA_ASSERT( psa_import_key( &attributes,
7421 our_key_data->x, our_key_data->len,
7422 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007423
gabor-mezei-armceface22021-01-21 12:26:17 +01007424 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7425 key_bits = psa_get_key_bits( &attributes );
7426
Gilles Peskinebe697d82019-05-16 18:00:41 +02007427 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7428 peer_key_data->x, peer_key_data->len,
7429 output, expected_output->len,
7430 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007431 ASSERT_COMPARE( output, output_length,
7432 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01007433 TEST_ASSERT( output_length <=
7434 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7435 TEST_ASSERT( output_length <=
7436 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007437
7438exit:
7439 mbedtls_free( output );
7440 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007441 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007442}
7443/* END_CASE */
7444
7445/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007446void key_agreement_capacity( int alg_arg,
7447 int our_key_type_arg, data_t *our_key_data,
7448 data_t *peer_key_data,
7449 int expected_capacity_arg )
7450{
Ronald Cron5425a212020-08-04 14:58:35 +02007451 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007452 psa_algorithm_t alg = alg_arg;
7453 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007454 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007456 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007457 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007458
Gilles Peskine8817f612018-12-18 00:18:46 +01007459 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007460
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007461 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7462 psa_set_key_algorithm( &attributes, alg );
7463 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007464 PSA_ASSERT( psa_import_key( &attributes,
7465 our_key_data->x, our_key_data->len,
7466 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007467
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007468 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007469 PSA_ASSERT( psa_key_derivation_key_agreement(
7470 &operation,
7471 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7472 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007473 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7474 {
7475 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007476 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007477 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007478 NULL, 0 ) );
7479 }
Gilles Peskine59685592018-09-18 12:11:34 +02007480
Gilles Peskinebf491972018-10-25 22:36:12 +02007481 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007482 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007483 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007484 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007485
Gilles Peskinebf491972018-10-25 22:36:12 +02007486 /* Test the actual capacity by reading the output. */
7487 while( actual_capacity > sizeof( output ) )
7488 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007489 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007490 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007491 actual_capacity -= sizeof( output );
7492 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007493 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007494 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007495 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007496 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007497
Gilles Peskine59685592018-09-18 12:11:34 +02007498exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007499 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007500 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007501 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007502}
7503/* END_CASE */
7504
7505/* BEGIN_CASE */
7506void key_agreement_output( int alg_arg,
7507 int our_key_type_arg, data_t *our_key_data,
7508 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007509 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007510{
Ronald Cron5425a212020-08-04 14:58:35 +02007511 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007512 psa_algorithm_t alg = alg_arg;
7513 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007514 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007516 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007517
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007518 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7519 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007520
Gilles Peskine8817f612018-12-18 00:18:46 +01007521 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007522
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007523 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7524 psa_set_key_algorithm( &attributes, alg );
7525 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007526 PSA_ASSERT( psa_import_key( &attributes,
7527 our_key_data->x, our_key_data->len,
7528 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007529
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007530 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007531 PSA_ASSERT( psa_key_derivation_key_agreement(
7532 &operation,
7533 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7534 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007535 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7536 {
7537 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007538 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007539 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007540 NULL, 0 ) );
7541 }
Gilles Peskine59685592018-09-18 12:11:34 +02007542
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007543 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007544 actual_output,
7545 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007546 ASSERT_COMPARE( actual_output, expected_output1->len,
7547 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007548 if( expected_output2->len != 0 )
7549 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007550 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007551 actual_output,
7552 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007553 ASSERT_COMPARE( actual_output, expected_output2->len,
7554 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007555 }
Gilles Peskine59685592018-09-18 12:11:34 +02007556
7557exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007558 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007559 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007560 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007561 mbedtls_free( actual_output );
7562}
7563/* END_CASE */
7564
7565/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007566void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007567{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007568 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007569 unsigned char *output = NULL;
7570 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007571 size_t i;
7572 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007573
Simon Butcher49f8e312020-03-03 15:51:50 +00007574 TEST_ASSERT( bytes_arg >= 0 );
7575
Gilles Peskine91892022021-02-08 19:50:26 +01007576 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007577 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007578
Gilles Peskine8817f612018-12-18 00:18:46 +01007579 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007580
Gilles Peskinea50d7392018-06-21 10:22:13 +02007581 /* Run several times, to ensure that every output byte will be
7582 * nonzero at least once with overwhelming probability
7583 * (2^(-8*number_of_runs)). */
7584 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007585 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007586 if( bytes != 0 )
7587 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007588 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007589
Gilles Peskinea50d7392018-06-21 10:22:13 +02007590 for( i = 0; i < bytes; i++ )
7591 {
7592 if( output[i] != 0 )
7593 ++changed[i];
7594 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007595 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007596
7597 /* Check that every byte was changed to nonzero at least once. This
7598 * validates that psa_generate_random is overwriting every byte of
7599 * the output buffer. */
7600 for( i = 0; i < bytes; i++ )
7601 {
7602 TEST_ASSERT( changed[i] != 0 );
7603 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007604
7605exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007606 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007607 mbedtls_free( output );
7608 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007609}
7610/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007611
7612/* BEGIN_CASE */
7613void generate_key( int type_arg,
7614 int bits_arg,
7615 int usage_arg,
7616 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007617 int expected_status_arg,
7618 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007619{
Ronald Cron5425a212020-08-04 14:58:35 +02007620 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007621 psa_key_type_t type = type_arg;
7622 psa_key_usage_t usage = usage_arg;
7623 size_t bits = bits_arg;
7624 psa_algorithm_t alg = alg_arg;
7625 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007627 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007628
Gilles Peskine8817f612018-12-18 00:18:46 +01007629 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007630
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007631 psa_set_key_usage_flags( &attributes, usage );
7632 psa_set_key_algorithm( &attributes, alg );
7633 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007634 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007635
7636 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007637 psa_status_t status = psa_generate_key( &attributes, &key );
7638
7639 if( is_large_key > 0 )
7640 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7641 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007642 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007643 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007644
7645 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007646 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007647 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7648 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007649
Gilles Peskine818ca122018-06-20 18:16:48 +02007650 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007651 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007652 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007653
7654exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007655 /*
7656 * Key attributes may have been returned by psa_get_key_attributes()
7657 * thus reset them as required.
7658 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007659 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007660
Ronald Cron5425a212020-08-04 14:58:35 +02007661 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007662 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007663}
7664/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007665
Ronald Cronee414c72021-03-18 18:50:08 +01007666/* 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 +02007667void generate_key_rsa( int bits_arg,
7668 data_t *e_arg,
7669 int expected_status_arg )
7670{
Ronald Cron5425a212020-08-04 14:58:35 +02007671 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007672 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007673 size_t bits = bits_arg;
7674 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7675 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7676 psa_status_t expected_status = expected_status_arg;
7677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7678 uint8_t *exported = NULL;
7679 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007680 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007681 size_t exported_length = SIZE_MAX;
7682 uint8_t *e_read_buffer = NULL;
7683 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007684 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007685 size_t e_read_length = SIZE_MAX;
7686
7687 if( e_arg->len == 0 ||
7688 ( e_arg->len == 3 &&
7689 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7690 {
7691 is_default_public_exponent = 1;
7692 e_read_size = 0;
7693 }
7694 ASSERT_ALLOC( e_read_buffer, e_read_size );
7695 ASSERT_ALLOC( exported, exported_size );
7696
7697 PSA_ASSERT( psa_crypto_init( ) );
7698
7699 psa_set_key_usage_flags( &attributes, usage );
7700 psa_set_key_algorithm( &attributes, alg );
7701 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7702 e_arg->x, e_arg->len ) );
7703 psa_set_key_bits( &attributes, bits );
7704
7705 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007706 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007707 if( expected_status != PSA_SUCCESS )
7708 goto exit;
7709
7710 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007711 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007712 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7713 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7714 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7715 e_read_buffer, e_read_size,
7716 &e_read_length ) );
7717 if( is_default_public_exponent )
7718 TEST_EQUAL( e_read_length, 0 );
7719 else
7720 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7721
7722 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007723 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007724 goto exit;
7725
7726 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007727 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007728 exported, exported_size,
7729 &exported_length ) );
7730 {
7731 uint8_t *p = exported;
7732 uint8_t *end = exported + exported_length;
7733 size_t len;
7734 /* RSAPublicKey ::= SEQUENCE {
7735 * modulus INTEGER, -- n
7736 * publicExponent INTEGER } -- e
7737 */
7738 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007739 MBEDTLS_ASN1_SEQUENCE |
7740 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007741 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007742 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7743 MBEDTLS_ASN1_INTEGER ) );
7744 if( len >= 1 && p[0] == 0 )
7745 {
7746 ++p;
7747 --len;
7748 }
7749 if( e_arg->len == 0 )
7750 {
7751 TEST_EQUAL( len, 3 );
7752 TEST_EQUAL( p[0], 1 );
7753 TEST_EQUAL( p[1], 0 );
7754 TEST_EQUAL( p[2], 1 );
7755 }
7756 else
7757 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7758 }
7759
7760exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007761 /*
7762 * Key attributes may have been returned by psa_get_key_attributes() or
7763 * set by psa_set_key_domain_parameters() thus reset them as required.
7764 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007765 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007766
Ronald Cron5425a212020-08-04 14:58:35 +02007767 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007768 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007769 mbedtls_free( e_read_buffer );
7770 mbedtls_free( exported );
7771}
7772/* END_CASE */
7773
Darryl Greend49a4992018-06-18 17:27:26 +01007774/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007775void persistent_key_load_key_from_storage( data_t *data,
7776 int type_arg, int bits_arg,
7777 int usage_flags_arg, int alg_arg,
7778 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007779{
Ronald Cron71016a92020-08-28 19:01:50 +02007780 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007781 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7783 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007784 psa_key_type_t type = type_arg;
7785 size_t bits = bits_arg;
7786 psa_key_usage_t usage_flags = usage_flags_arg;
7787 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007788 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007789 unsigned char *first_export = NULL;
7790 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007791 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007792 size_t first_exported_length;
7793 size_t second_exported_length;
7794
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007795 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7796 {
7797 ASSERT_ALLOC( first_export, export_size );
7798 ASSERT_ALLOC( second_export, export_size );
7799 }
Darryl Greend49a4992018-06-18 17:27:26 +01007800
Gilles Peskine8817f612018-12-18 00:18:46 +01007801 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007802
Gilles Peskinec87af662019-05-15 16:12:22 +02007803 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007804 psa_set_key_usage_flags( &attributes, usage_flags );
7805 psa_set_key_algorithm( &attributes, alg );
7806 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007807 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007808
Darryl Green0c6575a2018-11-07 16:05:30 +00007809 switch( generation_method )
7810 {
7811 case IMPORT_KEY:
7812 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007813 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007814 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007815 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007816
Darryl Green0c6575a2018-11-07 16:05:30 +00007817 case GENERATE_KEY:
7818 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007819 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007820 break;
7821
7822 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007823#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007824 {
7825 /* Create base key */
7826 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7827 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7828 psa_set_key_usage_flags( &base_attributes,
7829 PSA_KEY_USAGE_DERIVE );
7830 psa_set_key_algorithm( &base_attributes, derive_alg );
7831 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007832 PSA_ASSERT( psa_import_key( &base_attributes,
7833 data->x, data->len,
7834 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007835 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007836 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007837 PSA_ASSERT( psa_key_derivation_input_key(
7838 &operation,
7839 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007840 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007841 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007842 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007843 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7844 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007845 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007846 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007847 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007848 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007849 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007850#else
7851 TEST_ASSUME( ! "KDF not supported in this configuration" );
7852#endif
7853 break;
7854
7855 default:
7856 TEST_ASSERT( ! "generation_method not implemented in test" );
7857 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007858 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007859 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007860
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007861 /* Export the key if permitted by the key policy. */
7862 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7863 {
Ronald Cron5425a212020-08-04 14:58:35 +02007864 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007865 first_export, export_size,
7866 &first_exported_length ) );
7867 if( generation_method == IMPORT_KEY )
7868 ASSERT_COMPARE( data->x, data->len,
7869 first_export, first_exported_length );
7870 }
Darryl Greend49a4992018-06-18 17:27:26 +01007871
7872 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007873 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007874 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007875 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007876
Darryl Greend49a4992018-06-18 17:27:26 +01007877 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007878 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007879 TEST_ASSERT( mbedtls_svc_key_id_equal(
7880 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007881 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7882 PSA_KEY_LIFETIME_PERSISTENT );
7883 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7884 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007885 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007886 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007887 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007888
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007889 /* Export the key again if permitted by the key policy. */
7890 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007891 {
Ronald Cron5425a212020-08-04 14:58:35 +02007892 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007893 second_export, export_size,
7894 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007895 ASSERT_COMPARE( first_export, first_exported_length,
7896 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007897 }
7898
7899 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007900 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007901 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007902
7903exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007904 /*
7905 * Key attributes may have been returned by psa_get_key_attributes()
7906 * thus reset them as required.
7907 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007908 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007909
Darryl Greend49a4992018-06-18 17:27:26 +01007910 mbedtls_free( first_export );
7911 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007912 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007913 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007914 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007915 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007916}
7917/* END_CASE */