blob: 41ff635a1dc676666f32a7bc17504d5b07bd42f6 [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 {
510 memcpy( ( output_data + part_offset ), part_data,
511 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
Gilles Peskinee59236f2018-01-27 23:32:46 +0100592/* END_HEADER */
593
594/* BEGIN_DEPENDENCIES
595 * depends_on:MBEDTLS_PSA_CRYPTO_C
596 * END_DEPENDENCIES
597 */
598
599/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200600void static_checks( )
601{
602 size_t max_truncated_mac_size =
603 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
604
605 /* Check that the length for a truncated MAC always fits in the algorithm
606 * encoding. The shifted mask is the maximum truncated value. The
607 * untruncated algorithm may be one byte larger. */
608 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
609}
610/* END_CASE */
611
612/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200613void import_with_policy( int type_arg,
614 int usage_arg, int alg_arg,
615 int expected_status_arg )
616{
617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
618 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200620 psa_key_type_t type = type_arg;
621 psa_key_usage_t usage = usage_arg;
622 psa_algorithm_t alg = alg_arg;
623 psa_status_t expected_status = expected_status_arg;
624 const uint8_t key_material[16] = {0};
625 psa_status_t status;
626
627 PSA_ASSERT( psa_crypto_init( ) );
628
629 psa_set_key_type( &attributes, type );
630 psa_set_key_usage_flags( &attributes, usage );
631 psa_set_key_algorithm( &attributes, alg );
632
633 status = psa_import_key( &attributes,
634 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200635 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636 TEST_EQUAL( status, expected_status );
637 if( status != PSA_SUCCESS )
638 goto exit;
639
Ronald Cron5425a212020-08-04 14:58:35 +0200640 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200642 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200643 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200644 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200645 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200646
Ronald Cron5425a212020-08-04 14:58:35 +0200647 PSA_ASSERT( psa_destroy_key( key ) );
648 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200649
650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100651 /*
652 * Key attributes may have been returned by psa_get_key_attributes()
653 * thus reset them as required.
654 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200655 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100656
657 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200658 PSA_DONE( );
659}
660/* END_CASE */
661
662/* BEGIN_CASE */
663void import_with_data( data_t *data, int type_arg,
664 int attr_bits_arg,
665 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200666{
667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
668 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200670 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200671 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200672 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
Gilles Peskine4747d192019-04-17 15:05:45 +0200677 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200678 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200679
Ronald Cron5425a212020-08-04 14:58:35 +0200680 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100681 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200682 if( status != PSA_SUCCESS )
683 goto exit;
684
Ronald Cron5425a212020-08-04 14:58:35 +0200685 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200686 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200687 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200688 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200689 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200690
Ronald Cron5425a212020-08-04 14:58:35 +0200691 PSA_ASSERT( psa_destroy_key( key ) );
692 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693
694exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100695 /*
696 * Key attributes may have been returned by psa_get_key_attributes()
697 * thus reset them as required.
698 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200699 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100700
701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200702 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703}
704/* END_CASE */
705
706/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200707void import_large_key( int type_arg, int byte_size_arg,
708 int expected_status_arg )
709{
710 psa_key_type_t type = type_arg;
711 size_t byte_size = byte_size_arg;
712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
713 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200715 psa_status_t status;
716 uint8_t *buffer = NULL;
717 size_t buffer_size = byte_size + 1;
718 size_t n;
719
Steven Cooreman69967ce2021-01-18 18:01:08 +0100720 /* Skip the test case if the target running the test cannot
721 * accomodate large keys due to heap size constraints */
722 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200723 memset( buffer, 'K', byte_size );
724
725 PSA_ASSERT( psa_crypto_init( ) );
726
727 /* Try importing the key */
728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
729 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200730 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100731 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200732 TEST_EQUAL( status, expected_status );
733
734 if( status == PSA_SUCCESS )
735 {
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200737 TEST_EQUAL( psa_get_key_type( &attributes ), type );
738 TEST_EQUAL( psa_get_key_bits( &attributes ),
739 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200740 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200742 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200743 for( n = 0; n < byte_size; n++ )
744 TEST_EQUAL( buffer[n], 'K' );
745 for( n = byte_size; n < buffer_size; n++ )
746 TEST_EQUAL( buffer[n], 0 );
747 }
748
749exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100750 /*
751 * Key attributes may have been returned by psa_get_key_attributes()
752 * thus reset them as required.
753 */
754 psa_reset_key_attributes( &attributes );
755
Ronald Cron5425a212020-08-04 14:58:35 +0200756 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200757 PSA_DONE( );
758 mbedtls_free( buffer );
759}
760/* END_CASE */
761
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100762/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
764{
Ronald Cron5425a212020-08-04 14:58:35 +0200765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200766 size_t bits = bits_arg;
767 psa_status_t expected_status = expected_status_arg;
768 psa_status_t status;
769 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200770 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200771 size_t buffer_size = /* Slight overapproximations */
772 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200773 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200774 unsigned char *p;
775 int ret;
776 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778
Gilles Peskine8817f612018-12-18 00:18:46 +0100779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200780 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200781
782 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
783 bits, keypair ) ) >= 0 );
784 length = ret;
785
786 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200787 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200788 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100789 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200790
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200791 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200792 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200793
794exit:
795 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200796 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200797}
798/* END_CASE */
799
800/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300801void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300802 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200803 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530804 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 int expected_bits,
806 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200807 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 int canonical_input )
809{
Ronald Cron5425a212020-08-04 14:58:35 +0200810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200812 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200813 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530815 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 unsigned char *exported = NULL;
817 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100819 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200822 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823
Moran Pekercb088e72018-07-17 17:36:59 +0300824 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200825 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200827 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829
Archana4d7ae1d2021-07-07 02:50:22 +0530830 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200831 psa_set_key_usage_flags( &attributes, usage_arg );
832 psa_set_key_algorithm( &attributes, alg );
833 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700834
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200836 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837
838 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200839 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200840 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
841 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200842 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843
844 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200845 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100846 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100847
848 /* The exported length must be set by psa_export_key() to a value between 0
849 * and export_size. On errors, the exported length must be 0. */
850 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
851 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
852 TEST_ASSERT( exported_length <= export_size );
853
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200854 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200855 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100856 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200857 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200860 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861
Gilles Peskineea38a922021-02-13 00:05:16 +0100862 /* Run sanity checks on the exported key. For non-canonical inputs,
863 * this validates the canonical representations. For canonical inputs,
864 * this doesn't directly validate the implementation, but it still helps
865 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530866 if( !psa_key_lifetime_is_external( lifetime ) )
867 {
868 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
869 goto exit;
870 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200873 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 else
875 {
Ronald Cron5425a212020-08-04 14:58:35 +0200876 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200877 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200878 &key2 ) );
879 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100880 reexported,
881 export_size,
882 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200883 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +0530884 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100887 TEST_ASSERT( exported_length <=
Archana4d7ae1d2021-07-07 02:50:22 +0530888 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +0530889 psa_get_key_bits( &got_attributes ) ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100890 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891
892destroy:
893 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200894 PSA_ASSERT( psa_destroy_key( key ) );
895 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896
897exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100898 /*
899 * Key attributes may have been returned by psa_get_key_attributes()
900 * thus reset them as required.
901 */
902 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +0530903 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300904 mbedtls_free( exported );
905 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200906 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907}
908/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100909
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300911void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200912 int type_arg,
913 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530914 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 int export_size_delta,
916 int expected_export_status_arg,
917 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918{
Ronald Cron5425a212020-08-04 14:58:35 +0200919 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300920 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200921 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200922 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300923 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530924 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300925 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100926 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100927 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929
Gilles Peskine8817f612018-12-18 00:18:46 +0100930 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300931
Archana4d7ae1d2021-07-07 02:50:22 +0530932 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
934 psa_set_key_algorithm( &attributes, alg );
935 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300936
937 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200938 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300939
Gilles Peskine49c25912018-10-29 15:15:31 +0100940 /* Export the public key */
941 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200942 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200943 exported, export_size,
944 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100946 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100947 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200948 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100949 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200950 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200951 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100952 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100953 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100954 TEST_ASSERT( expected_public_key->len <=
955 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
956 TEST_ASSERT( expected_public_key->len <=
957 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100958 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
959 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100960 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300961exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100962 /*
963 * Key attributes may have been returned by psa_get_key_attributes()
964 * thus reset them as required.
965 */
966 psa_reset_key_attributes( &attributes );
967
itayzafrir3e02b3b2018-06-12 17:06:52 +0300968 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200969 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200970 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300971}
972/* END_CASE */
973
Gilles Peskine20035e32018-02-03 22:44:14 +0100974/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975void import_and_exercise_key( data_t *data,
976 int type_arg,
977 int bits_arg,
978 int alg_arg )
979{
Ronald Cron5425a212020-08-04 14:58:35 +0200980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200981 psa_key_type_t type = type_arg;
982 size_t bits = bits_arg;
983 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100984 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200986 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200987
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
Gilles Peskine4747d192019-04-17 15:05:45 +0200990 psa_set_key_usage_flags( &attributes, usage );
991 psa_set_key_algorithm( &attributes, alg );
992 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993
994 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200995 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200996
997 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200998 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200999 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1000 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001001
1002 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001003 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001004 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001005
Ronald Cron5425a212020-08-04 14:58:35 +02001006 PSA_ASSERT( psa_destroy_key( key ) );
1007 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001008
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001009exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001010 /*
1011 * Key attributes may have been returned by psa_get_key_attributes()
1012 * thus reset them as required.
1013 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001014 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001015
1016 psa_reset_key_attributes( &attributes );
1017 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001018 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001019}
1020/* END_CASE */
1021
1022/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001023void effective_key_attributes( int type_arg, int expected_type_arg,
1024 int bits_arg, int expected_bits_arg,
1025 int usage_arg, int expected_usage_arg,
1026 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001027{
Ronald Cron5425a212020-08-04 14:58:35 +02001028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001029 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001030 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001031 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001032 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001033 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001034 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001035 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001036 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
Gilles Peskine8817f612018-12-18 00:18:46 +01001039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001040
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001041 psa_set_key_usage_flags( &attributes, usage );
1042 psa_set_key_algorithm( &attributes, alg );
1043 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001044 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001047 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048
Ronald Cron5425a212020-08-04 14:58:35 +02001049 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001050 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1051 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1052 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1053 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001054
1055exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001056 /*
1057 * Key attributes may have been returned by psa_get_key_attributes()
1058 * thus reset them as required.
1059 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001060 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001061
1062 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001063 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001064}
1065/* END_CASE */
1066
1067/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001068void check_key_policy( int type_arg, int bits_arg,
1069 int usage_arg, int alg_arg )
1070{
1071 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001072 usage_arg,
1073 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001074 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001075 goto exit;
1076}
1077/* END_CASE */
1078
1079/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001080void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001081{
1082 /* Test each valid way of initializing the object, except for `= {0}`, as
1083 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1084 * though it's OK by the C standard. We could test for this, but we'd need
1085 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001086 psa_key_attributes_t func = psa_key_attributes_init( );
1087 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1088 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001089
1090 memset( &zero, 0, sizeof( zero ) );
1091
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001092 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1093 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1094 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001096 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1097 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1098 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1099
1100 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1101 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1102 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1103
1104 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1105 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1106 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1107
1108 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1109 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1110 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001111}
1112/* END_CASE */
1113
1114/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001115void mac_key_policy( int policy_usage_arg,
1116 int policy_alg_arg,
1117 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001119 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001120 int expected_status_sign_arg,
1121 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001126 psa_key_type_t key_type = key_type_arg;
1127 psa_algorithm_t policy_alg = policy_alg_arg;
1128 psa_algorithm_t exercise_alg = exercise_alg_arg;
1129 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001130 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001131 psa_status_t expected_status_sign = expected_status_sign_arg;
1132 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001134
Gilles Peskine8817f612018-12-18 00:18:46 +01001135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001137 psa_set_key_usage_flags( &attributes, policy_usage );
1138 psa_set_key_algorithm( &attributes, policy_alg );
1139 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001140
Gilles Peskine049c7532019-05-15 20:22:09 +02001141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001142 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001143
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001144 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1145 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001146
Ronald Cron5425a212020-08-04 14:58:35 +02001147 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001148 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001149
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001150 /* Calculate the MAC, one-shot case. */
1151 uint8_t input[128] = {0};
1152 size_t mac_len;
1153 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1154 input, 128,
1155 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1156 expected_status_sign );
1157
1158 /* Verify correct MAC, one-shot case. */
1159 status = psa_mac_verify( key, exercise_alg, input, 128,
1160 mac, mac_len );
1161
1162 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1163 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001165 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001166
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001168
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001170 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001171 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
1173exit:
1174 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001176 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177}
1178/* END_CASE */
1179
1180/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001181void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182 int policy_alg,
1183 int key_type,
1184 data_t *key_data,
1185 int exercise_alg )
1186{
Ronald Cron5425a212020-08-04 14:58:35 +02001187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001189 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001190 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191 psa_status_t status;
1192
Gilles Peskine8817f612018-12-18 00:18:46 +01001193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001195 psa_set_key_usage_flags( &attributes, policy_usage );
1196 psa_set_key_algorithm( &attributes, policy_alg );
1197 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001198
Gilles Peskine049c7532019-05-15 20:22:09 +02001199 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001200 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001202 /* Check if no key usage flag implication is done */
1203 TEST_EQUAL( policy_usage,
1204 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001205
Ronald Cron5425a212020-08-04 14:58:35 +02001206 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207 if( policy_alg == exercise_alg &&
1208 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001209 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001211 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001212 psa_cipher_abort( &operation );
1213
Ronald Cron5425a212020-08-04 14:58:35 +02001214 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001215 if( policy_alg == exercise_alg &&
1216 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001217 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001219 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220
1221exit:
1222 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001223 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001224 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225}
1226/* END_CASE */
1227
1228/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001229void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 int policy_alg,
1231 int key_type,
1232 data_t *key_data,
1233 int nonce_length_arg,
1234 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001235 int exercise_alg,
1236 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001237{
Ronald Cron5425a212020-08-04 14:58:35 +02001238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001240 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001241 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001242 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001243 unsigned char nonce[16] = {0};
1244 size_t nonce_length = nonce_length_arg;
1245 unsigned char tag[16];
1246 size_t tag_length = tag_length_arg;
1247 size_t output_length;
1248
1249 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1250 TEST_ASSERT( tag_length <= sizeof( tag ) );
1251
Gilles Peskine8817f612018-12-18 00:18:46 +01001252 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001253
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001254 psa_set_key_usage_flags( &attributes, policy_usage );
1255 psa_set_key_algorithm( &attributes, policy_alg );
1256 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001257
Gilles Peskine049c7532019-05-15 20:22:09 +02001258 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001259 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001260
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001261 /* Check if no key usage implication is done */
1262 TEST_EQUAL( policy_usage,
1263 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001264
Ronald Cron5425a212020-08-04 14:58:35 +02001265 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266 nonce, nonce_length,
1267 NULL, 0,
1268 NULL, 0,
1269 tag, tag_length,
1270 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001271 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1272 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001273 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001274 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001275
1276 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001277 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001278 nonce, nonce_length,
1279 NULL, 0,
1280 tag, tag_length,
1281 NULL, 0,
1282 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001283 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1284 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1285 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001286 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001288 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001289
1290exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001291 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001292 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001293}
1294/* END_CASE */
1295
1296/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001297void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001298 int policy_alg,
1299 int key_type,
1300 data_t *key_data,
1301 int exercise_alg )
1302{
Ronald Cron5425a212020-08-04 14:58:35 +02001303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001305 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001306 psa_status_t status;
1307 size_t key_bits;
1308 size_t buffer_length;
1309 unsigned char *buffer = NULL;
1310 size_t output_length;
1311
Gilles Peskine8817f612018-12-18 00:18:46 +01001312 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001313
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001314 psa_set_key_usage_flags( &attributes, policy_usage );
1315 psa_set_key_algorithm( &attributes, policy_alg );
1316 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317
Gilles Peskine049c7532019-05-15 20:22:09 +02001318 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001319 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001321 /* Check if no key usage implication is done */
1322 TEST_EQUAL( policy_usage,
1323 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001324
Ronald Cron5425a212020-08-04 14:58:35 +02001325 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001326 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001327 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1328 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001329 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001330
Ronald Cron5425a212020-08-04 14:58:35 +02001331 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332 NULL, 0,
1333 NULL, 0,
1334 buffer, buffer_length,
1335 &output_length );
1336 if( policy_alg == exercise_alg &&
1337 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001338 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001340 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001341
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001342 if( buffer_length != 0 )
1343 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001344 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001345 buffer, buffer_length,
1346 NULL, 0,
1347 buffer, buffer_length,
1348 &output_length );
1349 if( policy_alg == exercise_alg &&
1350 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001351 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001352 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001353 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354
1355exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001356 /*
1357 * Key attributes may have been returned by psa_get_key_attributes()
1358 * thus reset them as required.
1359 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001360 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001361
1362 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001363 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364 mbedtls_free( buffer );
1365}
1366/* END_CASE */
1367
1368/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001369void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001370 int policy_alg,
1371 int key_type,
1372 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001373 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001374 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001375 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001376{
Ronald Cron5425a212020-08-04 14:58:35 +02001377 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001379 psa_key_usage_t policy_usage = policy_usage_arg;
1380 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001381 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001382 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1383 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1384 * compatible with the policy and `payload_length_arg` is supposed to be
1385 * a valid input length to sign. If `payload_length_arg <= 0`,
1386 * `exercise_alg` is supposed to be forbidden by the policy. */
1387 int compatible_alg = payload_length_arg > 0;
1388 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001389 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001390 size_t signature_length;
1391
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001392 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001393 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001394 TEST_EQUAL( expected_usage,
1395 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001396
Gilles Peskine8817f612018-12-18 00:18:46 +01001397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001398
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001399 psa_set_key_usage_flags( &attributes, policy_usage );
1400 psa_set_key_algorithm( &attributes, policy_alg );
1401 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001402
Gilles Peskine049c7532019-05-15 20:22:09 +02001403 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001404 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001405
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001406 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1407
Ronald Cron5425a212020-08-04 14:58:35 +02001408 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001409 payload, payload_length,
1410 signature, sizeof( signature ),
1411 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001412 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001413 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001414 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001415 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001416
1417 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001418 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001419 payload, payload_length,
1420 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001421 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001422 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001423 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001424 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001425
Gilles Peskinef7b41372021-09-22 16:15:05 +02001426 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001427 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001428 {
1429 status = psa_sign_message( key, exercise_alg,
1430 payload, payload_length,
1431 signature, sizeof( signature ),
1432 &signature_length );
1433 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1434 PSA_ASSERT( status );
1435 else
1436 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1437
1438 memset( signature, 0, sizeof( signature ) );
1439 status = psa_verify_message( key, exercise_alg,
1440 payload, payload_length,
1441 signature, sizeof( signature ) );
1442 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1443 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1444 else
1445 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1446 }
1447
Gilles Peskined5b33222018-06-18 22:20:03 +02001448exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001449 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001450 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001451}
1452/* END_CASE */
1453
Janos Follathba3fab92019-06-11 14:50:16 +01001454/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001455void derive_key_policy( int policy_usage,
1456 int policy_alg,
1457 int key_type,
1458 data_t *key_data,
1459 int exercise_alg )
1460{
Ronald Cron5425a212020-08-04 14:58:35 +02001461 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001463 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001464 psa_status_t status;
1465
Gilles Peskine8817f612018-12-18 00:18:46 +01001466 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001467
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001468 psa_set_key_usage_flags( &attributes, policy_usage );
1469 psa_set_key_algorithm( &attributes, policy_alg );
1470 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001471
Gilles Peskine049c7532019-05-15 20:22:09 +02001472 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001473 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001474
Janos Follathba3fab92019-06-11 14:50:16 +01001475 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1476
1477 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1478 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001479 {
Janos Follathba3fab92019-06-11 14:50:16 +01001480 PSA_ASSERT( psa_key_derivation_input_bytes(
1481 &operation,
1482 PSA_KEY_DERIVATION_INPUT_SEED,
1483 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001484 }
Janos Follathba3fab92019-06-11 14:50:16 +01001485
1486 status = psa_key_derivation_input_key( &operation,
1487 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001488 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001489
Gilles Peskineea0fb492018-07-12 17:17:20 +02001490 if( policy_alg == exercise_alg &&
1491 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001492 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001493 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001494 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001495
1496exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001497 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001498 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001499 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001500}
1501/* END_CASE */
1502
1503/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001504void agreement_key_policy( int policy_usage,
1505 int policy_alg,
1506 int key_type_arg,
1507 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001508 int exercise_alg,
1509 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001510{
Ronald Cron5425a212020-08-04 14:58:35 +02001511 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001513 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001514 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001515 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001516 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001517
Gilles Peskine8817f612018-12-18 00:18:46 +01001518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001519
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001520 psa_set_key_usage_flags( &attributes, policy_usage );
1521 psa_set_key_algorithm( &attributes, policy_alg );
1522 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001523
Gilles Peskine049c7532019-05-15 20:22:09 +02001524 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001525 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001526
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001527 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001528 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001529
Steven Cooremance48e852020-10-05 16:02:45 +02001530 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001531
1532exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001533 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001534 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001535 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001536}
1537/* END_CASE */
1538
1539/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001540void key_policy_alg2( int key_type_arg, data_t *key_data,
1541 int usage_arg, int alg_arg, int alg2_arg )
1542{
Ronald Cron5425a212020-08-04 14:58:35 +02001543 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001544 psa_key_type_t key_type = key_type_arg;
1545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1546 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1547 psa_key_usage_t usage = usage_arg;
1548 psa_algorithm_t alg = alg_arg;
1549 psa_algorithm_t alg2 = alg2_arg;
1550
1551 PSA_ASSERT( psa_crypto_init( ) );
1552
1553 psa_set_key_usage_flags( &attributes, usage );
1554 psa_set_key_algorithm( &attributes, alg );
1555 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1556 psa_set_key_type( &attributes, key_type );
1557 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001558 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001559
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001560 /* Update the usage flags to obtain implicit usage flags */
1561 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001562 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001563 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1564 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1565 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1566
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001567 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001568 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001569 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001570 goto exit;
1571
1572exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001573 /*
1574 * Key attributes may have been returned by psa_get_key_attributes()
1575 * thus reset them as required.
1576 */
1577 psa_reset_key_attributes( &got_attributes );
1578
Ronald Cron5425a212020-08-04 14:58:35 +02001579 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001580 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001581}
1582/* END_CASE */
1583
1584/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001585void raw_agreement_key_policy( int policy_usage,
1586 int policy_alg,
1587 int key_type_arg,
1588 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001589 int exercise_alg,
1590 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001591{
Ronald Cron5425a212020-08-04 14:58:35 +02001592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001594 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001595 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001596 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001597 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001598
1599 PSA_ASSERT( psa_crypto_init( ) );
1600
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001601 psa_set_key_usage_flags( &attributes, policy_usage );
1602 psa_set_key_algorithm( &attributes, policy_alg );
1603 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001604
Gilles Peskine049c7532019-05-15 20:22:09 +02001605 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001606 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001607
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001608 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001609
Steven Cooremance48e852020-10-05 16:02:45 +02001610 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001611
1612exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001613 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001614 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001615 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001616}
1617/* END_CASE */
1618
1619/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001620void copy_success( int source_usage_arg,
1621 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301622 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001623 int type_arg, data_t *material,
1624 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001625 int target_usage_arg,
1626 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301627 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001628 int expected_usage_arg,
1629 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001630{
Gilles Peskineca25db92019-04-19 11:43:08 +02001631 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1632 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001633 psa_key_usage_t expected_usage = expected_usage_arg;
1634 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001635 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301636 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1637 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001638 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1639 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001640 uint8_t *export_buffer = NULL;
1641
Gilles Peskine57ab7212019-01-28 13:03:09 +01001642 PSA_ASSERT( psa_crypto_init( ) );
1643
Gilles Peskineca25db92019-04-19 11:43:08 +02001644 /* Prepare the source key. */
1645 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1646 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001647 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001648 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301649 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001650 PSA_ASSERT( psa_import_key( &source_attributes,
1651 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001652 &source_key ) );
1653 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001654
Gilles Peskineca25db92019-04-19 11:43:08 +02001655 /* Prepare the target attributes. */
1656 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001657 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001658 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001659 }
Archana8a180362021-07-05 02:18:48 +05301660 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001661
Gilles Peskineca25db92019-04-19 11:43:08 +02001662 if( target_usage_arg != -1 )
1663 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1664 if( target_alg_arg != -1 )
1665 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001666 if( target_alg2_arg != -1 )
1667 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001668
Archana8a180362021-07-05 02:18:48 +05301669
Gilles Peskine57ab7212019-01-28 13:03:09 +01001670 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001671 PSA_ASSERT( psa_copy_key( source_key,
1672 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001673
1674 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001675 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001676
1677 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001678 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001679 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1680 psa_get_key_type( &target_attributes ) );
1681 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1682 psa_get_key_bits( &target_attributes ) );
1683 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1684 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001685 TEST_EQUAL( expected_alg2,
1686 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001687 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1688 {
1689 size_t length;
1690 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001691 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001692 material->len, &length ) );
1693 ASSERT_COMPARE( material->x, material->len,
1694 export_buffer, length );
1695 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001696
Archana8a180362021-07-05 02:18:48 +05301697 if( !psa_key_lifetime_is_external( target_lifetime ) )
1698 {
1699 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1700 goto exit;
1701 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1702 goto exit;
1703 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001704
Ronald Cron5425a212020-08-04 14:58:35 +02001705 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001706
1707exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001708 /*
1709 * Source and target key attributes may have been returned by
1710 * psa_get_key_attributes() thus reset them as required.
1711 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001712 psa_reset_key_attributes( &source_attributes );
1713 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001714
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001715 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001716 mbedtls_free( export_buffer );
1717}
1718/* END_CASE */
1719
1720/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001721void copy_fail( int source_usage_arg,
1722 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301723 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001724 int type_arg, data_t *material,
1725 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001726 int target_usage_arg,
1727 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001728 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001729 int expected_status_arg )
1730{
1731 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1732 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001733 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1734 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001735 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001736
1737 PSA_ASSERT( psa_crypto_init( ) );
1738
1739 /* Prepare the source key. */
1740 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1741 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001742 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001743 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301744 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001745 PSA_ASSERT( psa_import_key( &source_attributes,
1746 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001747 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001748
1749 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001750 psa_set_key_id( &target_attributes, key_id );
1751 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001752 psa_set_key_type( &target_attributes, target_type_arg );
1753 psa_set_key_bits( &target_attributes, target_bits_arg );
1754 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1755 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001756 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001757
1758 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001759 TEST_EQUAL( psa_copy_key( source_key,
1760 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001761 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001762
Ronald Cron5425a212020-08-04 14:58:35 +02001763 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001764
Gilles Peskine4a644642019-05-03 17:14:08 +02001765exit:
1766 psa_reset_key_attributes( &source_attributes );
1767 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001768 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001769}
1770/* END_CASE */
1771
1772/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001773void hash_operation_init( )
1774{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001775 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001776 /* Test each valid way of initializing the object, except for `= {0}`, as
1777 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1778 * though it's OK by the C standard. We could test for this, but we'd need
1779 * to supress the Clang warning for the test. */
1780 psa_hash_operation_t func = psa_hash_operation_init( );
1781 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1782 psa_hash_operation_t zero;
1783
1784 memset( &zero, 0, sizeof( zero ) );
1785
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001786 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001787 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1788 PSA_ERROR_BAD_STATE );
1789 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1790 PSA_ERROR_BAD_STATE );
1791 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1792 PSA_ERROR_BAD_STATE );
1793
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001794 /* A default hash operation should be abortable without error. */
1795 PSA_ASSERT( psa_hash_abort( &func ) );
1796 PSA_ASSERT( psa_hash_abort( &init ) );
1797 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001798}
1799/* END_CASE */
1800
1801/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001802void hash_setup( int alg_arg,
1803 int expected_status_arg )
1804{
1805 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01001806 uint8_t *output = NULL;
1807 size_t output_size = 0;
1808 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001809 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001810 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001811 psa_status_t status;
1812
Gilles Peskine8817f612018-12-18 00:18:46 +01001813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001814
Neil Armstrongedb20862022-02-07 15:47:44 +01001815 /* Hash Setup, one-shot */
1816 output_size = PSA_HASH_LENGTH(alg);
1817 ASSERT_ALLOC( output, output_size );
1818
1819 status = psa_hash_compute( alg, NULL, 0,
1820 output, output_size, &output_length );
1821 TEST_EQUAL( status, expected_status );
1822
1823 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001824 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001825 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001826
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001827 /* Whether setup succeeded or failed, abort must succeed. */
1828 PSA_ASSERT( psa_hash_abort( &operation ) );
1829
1830 /* If setup failed, reproduce the failure, so as to
1831 * test the resulting state of the operation object. */
1832 if( status != PSA_SUCCESS )
1833 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1834
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001835 /* Now the operation object should be reusable. */
1836#if defined(KNOWN_SUPPORTED_HASH_ALG)
1837 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1838 PSA_ASSERT( psa_hash_abort( &operation ) );
1839#endif
1840
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001841exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01001842 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001843 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001844}
1845/* END_CASE */
1846
1847/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001848void hash_compute_fail( int alg_arg, data_t *input,
1849 int output_size_arg, int expected_status_arg )
1850{
1851 psa_algorithm_t alg = alg_arg;
1852 uint8_t *output = NULL;
1853 size_t output_size = output_size_arg;
1854 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001855 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01001856 psa_status_t expected_status = expected_status_arg;
1857 psa_status_t status;
1858
1859 ASSERT_ALLOC( output, output_size );
1860
1861 PSA_ASSERT( psa_crypto_init( ) );
1862
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001863 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001864 status = psa_hash_compute( alg, input->x, input->len,
1865 output, output_size, &output_length );
1866 TEST_EQUAL( status, expected_status );
1867 TEST_ASSERT( output_length <= output_size );
1868
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001869 /* Hash Compute, multi-part */
1870 status = psa_hash_setup( &operation, alg );
1871 if( status == PSA_SUCCESS )
1872 {
1873 status = psa_hash_update( &operation, input->x, input->len );
1874 if( status == PSA_SUCCESS )
1875 {
1876 status = psa_hash_finish( &operation, output, output_size,
1877 &output_length );
1878 if( status == PSA_SUCCESS )
1879 TEST_ASSERT( output_length <= output_size );
1880 else
1881 TEST_EQUAL( status, expected_status );
1882 }
1883 else
1884 {
1885 TEST_EQUAL( status, expected_status );
1886 }
1887 }
1888 else
1889 {
1890 TEST_EQUAL( status, expected_status );
1891 }
1892
Gilles Peskine0a749c82019-11-28 19:33:58 +01001893exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001894 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001895 mbedtls_free( output );
1896 PSA_DONE( );
1897}
1898/* END_CASE */
1899
1900/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001901void hash_compare_fail( int alg_arg, data_t *input,
1902 data_t *reference_hash,
1903 int expected_status_arg )
1904{
1905 psa_algorithm_t alg = alg_arg;
1906 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01001907 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01001908 psa_status_t status;
1909
1910 PSA_ASSERT( psa_crypto_init( ) );
1911
Neil Armstrong55a1be12022-02-07 11:23:20 +01001912 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01001913 status = psa_hash_compare( alg, input->x, input->len,
1914 reference_hash->x, reference_hash->len );
1915 TEST_EQUAL( status, expected_status );
1916
Neil Armstrong55a1be12022-02-07 11:23:20 +01001917 /* Hash Compare, multi-part */
1918 status = psa_hash_setup( &operation, alg );
1919 if( status == PSA_SUCCESS )
1920 {
1921 status = psa_hash_update( &operation, input->x, input->len );
1922 if( status == PSA_SUCCESS )
1923 {
1924 status = psa_hash_verify( &operation, reference_hash->x,
1925 reference_hash->len );
1926 TEST_EQUAL( status, expected_status );
1927 }
1928 else
1929 {
1930 TEST_EQUAL( status, expected_status );
1931 }
1932 }
1933 else
1934 {
1935 TEST_EQUAL( status, expected_status );
1936 }
1937
Gilles Peskine88e08462020-01-28 20:43:00 +01001938exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01001939 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01001940 PSA_DONE( );
1941}
1942/* END_CASE */
1943
1944/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001945void hash_compute_compare( int alg_arg, data_t *input,
1946 data_t *expected_output )
1947{
1948 psa_algorithm_t alg = alg_arg;
1949 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1950 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01001951 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01001952 size_t i;
1953
1954 PSA_ASSERT( psa_crypto_init( ) );
1955
Neil Armstrongca30a002022-02-07 11:40:23 +01001956 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001957 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001958 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001959 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001960 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001961 ASSERT_COMPARE( output, output_length,
1962 expected_output->x, expected_output->len );
1963
Neil Armstrongca30a002022-02-07 11:40:23 +01001964 /* Compute with tight buffer, multi-part */
1965 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1966 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
1967 PSA_ASSERT( psa_hash_finish( &operation, output,
1968 PSA_HASH_LENGTH( alg ),
1969 &output_length ) );
1970 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
1971 ASSERT_COMPARE( output, output_length,
1972 expected_output->x, expected_output->len );
1973
1974 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001975 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1976 output, sizeof( output ),
1977 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001978 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001979 ASSERT_COMPARE( output, output_length,
1980 expected_output->x, expected_output->len );
1981
Neil Armstrongca30a002022-02-07 11:40:23 +01001982 /* Compute with larger buffer, multi-part */
1983 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1984 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
1985 PSA_ASSERT( psa_hash_finish( &operation, output,
1986 sizeof( output ), &output_length ) );
1987 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
1988 ASSERT_COMPARE( output, output_length,
1989 expected_output->x, expected_output->len );
1990
1991 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001992 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1993 output, output_length ) );
1994
Neil Armstrongca30a002022-02-07 11:40:23 +01001995 /* Compare with correct hash, multi-part */
1996 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1997 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
1998 PSA_ASSERT( psa_hash_verify( &operation, output,
1999 output_length ) );
2000
2001 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002002 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2003 output, output_length + 1 ),
2004 PSA_ERROR_INVALID_SIGNATURE );
2005
Neil Armstrongca30a002022-02-07 11:40:23 +01002006 /* Compare with trailing garbage, multi-part */
2007 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2008 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2009 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2010 PSA_ERROR_INVALID_SIGNATURE );
2011
2012 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002013 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2014 output, output_length - 1 ),
2015 PSA_ERROR_INVALID_SIGNATURE );
2016
Neil Armstrongca30a002022-02-07 11:40:23 +01002017 /* Compare with truncated hash, multi-part */
2018 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2019 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2020 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2021 PSA_ERROR_INVALID_SIGNATURE );
2022
Gilles Peskine0a749c82019-11-28 19:33:58 +01002023 /* Compare with corrupted value */
2024 for( i = 0; i < output_length; i++ )
2025 {
Chris Jones9634bb12021-01-20 15:56:42 +00002026 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002027 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002028
2029 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002030 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2031 output, output_length ),
2032 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002033
2034 /* Multi-Part */
2035 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2036 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2037 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2038 PSA_ERROR_INVALID_SIGNATURE );
2039
Gilles Peskine0a749c82019-11-28 19:33:58 +01002040 output[i] ^= 1;
2041 }
2042
2043exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002044 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002045 PSA_DONE( );
2046}
2047/* END_CASE */
2048
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002049/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002050void hash_bad_order( )
2051{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002052 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002053 unsigned char input[] = "";
2054 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002055 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002056 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2057 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2058 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002059 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002060 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002061 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002062
Gilles Peskine8817f612018-12-18 00:18:46 +01002063 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002064
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002065 /* Call setup twice in a row. */
2066 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002067 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002068 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2069 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002070 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002071 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002072 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002073
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002074 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002075 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002076 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002077 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002078
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002079 /* Check that update calls abort on error. */
2080 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002081 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002082 ASSERT_OPERATION_IS_ACTIVE( operation );
2083 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2084 PSA_ERROR_BAD_STATE );
2085 ASSERT_OPERATION_IS_INACTIVE( operation );
2086 PSA_ASSERT( psa_hash_abort( &operation ) );
2087 ASSERT_OPERATION_IS_INACTIVE( operation );
2088
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002089 /* Call update after finish. */
2090 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2091 PSA_ASSERT( psa_hash_finish( &operation,
2092 hash, sizeof( hash ), &hash_len ) );
2093 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002094 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002095 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002096
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002097 /* Call verify without calling setup beforehand. */
2098 TEST_EQUAL( psa_hash_verify( &operation,
2099 valid_hash, sizeof( valid_hash ) ),
2100 PSA_ERROR_BAD_STATE );
2101 PSA_ASSERT( psa_hash_abort( &operation ) );
2102
2103 /* Call verify after finish. */
2104 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2105 PSA_ASSERT( psa_hash_finish( &operation,
2106 hash, sizeof( hash ), &hash_len ) );
2107 TEST_EQUAL( psa_hash_verify( &operation,
2108 valid_hash, sizeof( valid_hash ) ),
2109 PSA_ERROR_BAD_STATE );
2110 PSA_ASSERT( psa_hash_abort( &operation ) );
2111
2112 /* Call verify twice in a row. */
2113 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002114 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002115 PSA_ASSERT( psa_hash_verify( &operation,
2116 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002117 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002118 TEST_EQUAL( psa_hash_verify( &operation,
2119 valid_hash, sizeof( valid_hash ) ),
2120 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002121 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002122 PSA_ASSERT( psa_hash_abort( &operation ) );
2123
2124 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002125 TEST_EQUAL( psa_hash_finish( &operation,
2126 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002127 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002128 PSA_ASSERT( psa_hash_abort( &operation ) );
2129
2130 /* Call finish twice in a row. */
2131 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2132 PSA_ASSERT( psa_hash_finish( &operation,
2133 hash, sizeof( hash ), &hash_len ) );
2134 TEST_EQUAL( psa_hash_finish( &operation,
2135 hash, sizeof( hash ), &hash_len ),
2136 PSA_ERROR_BAD_STATE );
2137 PSA_ASSERT( psa_hash_abort( &operation ) );
2138
2139 /* Call finish after calling verify. */
2140 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2141 PSA_ASSERT( psa_hash_verify( &operation,
2142 valid_hash, sizeof( valid_hash ) ) );
2143 TEST_EQUAL( psa_hash_finish( &operation,
2144 hash, sizeof( hash ), &hash_len ),
2145 PSA_ERROR_BAD_STATE );
2146 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002147
2148exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002149 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002150}
2151/* END_CASE */
2152
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002153/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002154void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002155{
2156 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002157 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2158 * appended to it */
2159 unsigned char hash[] = {
2160 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2161 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2162 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002163 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002164 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002165
Gilles Peskine8817f612018-12-18 00:18:46 +01002166 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002167
itayzafrir27e69452018-11-01 14:26:34 +02002168 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002169 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002170 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002171 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002172 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002173 ASSERT_OPERATION_IS_INACTIVE( operation );
2174 PSA_ASSERT( psa_hash_abort( &operation ) );
2175 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002176
itayzafrir27e69452018-11-01 14:26:34 +02002177 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002178 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002179 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002180 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002181
itayzafrir27e69452018-11-01 14:26:34 +02002182 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002183 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002184 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002185 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002186
itayzafrirec93d302018-10-18 18:01:10 +03002187exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002188 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002189}
2190/* END_CASE */
2191
Ronald Cronee414c72021-03-18 18:50:08 +01002192/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002193void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002194{
2195 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002196 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002197 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002198 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002199 size_t hash_len;
2200
Gilles Peskine8817f612018-12-18 00:18:46 +01002201 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002202
itayzafrir58028322018-10-25 10:22:01 +03002203 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002204 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002205 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002206 hash, expected_size - 1, &hash_len ),
2207 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002208
2209exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002210 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002211}
2212/* END_CASE */
2213
Ronald Cronee414c72021-03-18 18:50:08 +01002214/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002215void hash_clone_source_state( )
2216{
2217 psa_algorithm_t alg = PSA_ALG_SHA_256;
2218 unsigned char hash[PSA_HASH_MAX_SIZE];
2219 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2220 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2221 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2222 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2223 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2224 size_t hash_len;
2225
2226 PSA_ASSERT( psa_crypto_init( ) );
2227 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2228
2229 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2230 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2231 PSA_ASSERT( psa_hash_finish( &op_finished,
2232 hash, sizeof( hash ), &hash_len ) );
2233 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2234 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2235
2236 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2237 PSA_ERROR_BAD_STATE );
2238
2239 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2240 PSA_ASSERT( psa_hash_finish( &op_init,
2241 hash, sizeof( hash ), &hash_len ) );
2242 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2243 PSA_ASSERT( psa_hash_finish( &op_finished,
2244 hash, sizeof( hash ), &hash_len ) );
2245 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2246 PSA_ASSERT( psa_hash_finish( &op_aborted,
2247 hash, sizeof( hash ), &hash_len ) );
2248
2249exit:
2250 psa_hash_abort( &op_source );
2251 psa_hash_abort( &op_init );
2252 psa_hash_abort( &op_setup );
2253 psa_hash_abort( &op_finished );
2254 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002255 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002256}
2257/* END_CASE */
2258
Ronald Cronee414c72021-03-18 18:50:08 +01002259/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002260void hash_clone_target_state( )
2261{
2262 psa_algorithm_t alg = PSA_ALG_SHA_256;
2263 unsigned char hash[PSA_HASH_MAX_SIZE];
2264 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2265 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2266 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2267 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2268 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2269 size_t hash_len;
2270
2271 PSA_ASSERT( psa_crypto_init( ) );
2272
2273 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2274 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2275 PSA_ASSERT( psa_hash_finish( &op_finished,
2276 hash, sizeof( hash ), &hash_len ) );
2277 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2278 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2279
2280 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2281 PSA_ASSERT( psa_hash_finish( &op_target,
2282 hash, sizeof( hash ), &hash_len ) );
2283
2284 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2285 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2286 PSA_ERROR_BAD_STATE );
2287 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2288 PSA_ERROR_BAD_STATE );
2289
2290exit:
2291 psa_hash_abort( &op_target );
2292 psa_hash_abort( &op_init );
2293 psa_hash_abort( &op_setup );
2294 psa_hash_abort( &op_finished );
2295 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002296 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002297}
2298/* END_CASE */
2299
itayzafrir58028322018-10-25 10:22:01 +03002300/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002301void mac_operation_init( )
2302{
Jaeden Amero252ef282019-02-15 14:05:35 +00002303 const uint8_t input[1] = { 0 };
2304
Jaeden Amero769ce272019-01-04 11:48:03 +00002305 /* Test each valid way of initializing the object, except for `= {0}`, as
2306 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2307 * though it's OK by the C standard. We could test for this, but we'd need
2308 * to supress the Clang warning for the test. */
2309 psa_mac_operation_t func = psa_mac_operation_init( );
2310 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2311 psa_mac_operation_t zero;
2312
2313 memset( &zero, 0, sizeof( zero ) );
2314
Jaeden Amero252ef282019-02-15 14:05:35 +00002315 /* A freshly-initialized MAC operation should not be usable. */
2316 TEST_EQUAL( psa_mac_update( &func,
2317 input, sizeof( input ) ),
2318 PSA_ERROR_BAD_STATE );
2319 TEST_EQUAL( psa_mac_update( &init,
2320 input, sizeof( input ) ),
2321 PSA_ERROR_BAD_STATE );
2322 TEST_EQUAL( psa_mac_update( &zero,
2323 input, sizeof( input ) ),
2324 PSA_ERROR_BAD_STATE );
2325
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002326 /* A default MAC operation should be abortable without error. */
2327 PSA_ASSERT( psa_mac_abort( &func ) );
2328 PSA_ASSERT( psa_mac_abort( &init ) );
2329 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002330}
2331/* END_CASE */
2332
2333/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002334void mac_setup( int key_type_arg,
2335 data_t *key,
2336 int alg_arg,
2337 int expected_status_arg )
2338{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002339 psa_key_type_t key_type = key_type_arg;
2340 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002341 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002342 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002343 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2344#if defined(KNOWN_SUPPORTED_MAC_ALG)
2345 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2346#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347
Gilles Peskine8817f612018-12-18 00:18:46 +01002348 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002349
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002350 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2351 &operation, &status ) )
2352 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002353 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002354
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002355 /* The operation object should be reusable. */
2356#if defined(KNOWN_SUPPORTED_MAC_ALG)
2357 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2358 smoke_test_key_data,
2359 sizeof( smoke_test_key_data ),
2360 KNOWN_SUPPORTED_MAC_ALG,
2361 &operation, &status ) )
2362 goto exit;
2363 TEST_EQUAL( status, PSA_SUCCESS );
2364#endif
2365
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002366exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002367 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002368}
2369/* END_CASE */
2370
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002371/* 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 +00002372void mac_bad_order( )
2373{
Ronald Cron5425a212020-08-04 14:58:35 +02002374 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002375 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2376 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002377 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002378 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2379 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2380 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002381 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002382 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2383 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2384 size_t sign_mac_length = 0;
2385 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2386 const uint8_t verify_mac[] = {
2387 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2388 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2389 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2390
2391 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002392 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002393 psa_set_key_algorithm( &attributes, alg );
2394 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002395
Ronald Cron5425a212020-08-04 14:58:35 +02002396 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2397 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002398
Jaeden Amero252ef282019-02-15 14:05:35 +00002399 /* Call update without calling setup beforehand. */
2400 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2401 PSA_ERROR_BAD_STATE );
2402 PSA_ASSERT( psa_mac_abort( &operation ) );
2403
2404 /* Call sign finish without calling setup beforehand. */
2405 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2406 &sign_mac_length),
2407 PSA_ERROR_BAD_STATE );
2408 PSA_ASSERT( psa_mac_abort( &operation ) );
2409
2410 /* Call verify finish without calling setup beforehand. */
2411 TEST_EQUAL( psa_mac_verify_finish( &operation,
2412 verify_mac, sizeof( verify_mac ) ),
2413 PSA_ERROR_BAD_STATE );
2414 PSA_ASSERT( psa_mac_abort( &operation ) );
2415
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002416 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002417 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002418 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002419 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002420 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002421 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002422 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002423 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002424
Jaeden Amero252ef282019-02-15 14:05:35 +00002425 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002426 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002427 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2428 PSA_ASSERT( psa_mac_sign_finish( &operation,
2429 sign_mac, sizeof( sign_mac ),
2430 &sign_mac_length ) );
2431 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2432 PSA_ERROR_BAD_STATE );
2433 PSA_ASSERT( psa_mac_abort( &operation ) );
2434
2435 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002436 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002437 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2438 PSA_ASSERT( psa_mac_verify_finish( &operation,
2439 verify_mac, sizeof( verify_mac ) ) );
2440 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2441 PSA_ERROR_BAD_STATE );
2442 PSA_ASSERT( psa_mac_abort( &operation ) );
2443
2444 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002445 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002446 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2447 PSA_ASSERT( psa_mac_sign_finish( &operation,
2448 sign_mac, sizeof( sign_mac ),
2449 &sign_mac_length ) );
2450 TEST_EQUAL( psa_mac_sign_finish( &operation,
2451 sign_mac, sizeof( sign_mac ),
2452 &sign_mac_length ),
2453 PSA_ERROR_BAD_STATE );
2454 PSA_ASSERT( psa_mac_abort( &operation ) );
2455
2456 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002457 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002458 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2459 PSA_ASSERT( psa_mac_verify_finish( &operation,
2460 verify_mac, sizeof( verify_mac ) ) );
2461 TEST_EQUAL( psa_mac_verify_finish( &operation,
2462 verify_mac, sizeof( verify_mac ) ),
2463 PSA_ERROR_BAD_STATE );
2464 PSA_ASSERT( psa_mac_abort( &operation ) );
2465
2466 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002467 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002468 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002469 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002470 TEST_EQUAL( psa_mac_verify_finish( &operation,
2471 verify_mac, sizeof( verify_mac ) ),
2472 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002473 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002474 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002475 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002476
2477 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002478 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002479 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002480 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002481 TEST_EQUAL( psa_mac_sign_finish( &operation,
2482 sign_mac, sizeof( sign_mac ),
2483 &sign_mac_length ),
2484 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002485 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002486 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002487 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002488
Ronald Cron5425a212020-08-04 14:58:35 +02002489 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002490
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002491exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002492 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002493}
2494/* END_CASE */
2495
2496/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002497void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002498 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002499 int alg_arg,
2500 data_t *input,
2501 data_t *expected_mac )
2502{
Ronald Cron5425a212020-08-04 14:58:35 +02002503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002504 psa_key_type_t key_type = key_type_arg;
2505 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002506 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002508 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002509 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002510 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002511 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002512 const size_t output_sizes_to_test[] = {
2513 0,
2514 1,
2515 expected_mac->len - 1,
2516 expected_mac->len,
2517 expected_mac->len + 1,
2518 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002519
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002520 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002521 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002522 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002523
Gilles Peskine8817f612018-12-18 00:18:46 +01002524 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002525
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002526 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002527 psa_set_key_algorithm( &attributes, alg );
2528 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002529
Ronald Cron5425a212020-08-04 14:58:35 +02002530 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2531 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002532
Gilles Peskine8b356b52020-08-25 23:44:59 +02002533 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2534 {
2535 const size_t output_size = output_sizes_to_test[i];
2536 psa_status_t expected_status =
2537 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2538 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002539
Chris Jones9634bb12021-01-20 15:56:42 +00002540 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002541 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002542
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002543 /* Calculate the MAC, one-shot case. */
2544 TEST_EQUAL( psa_mac_compute( key, alg,
2545 input->x, input->len,
2546 actual_mac, output_size, &mac_length ),
2547 expected_status );
2548 if( expected_status == PSA_SUCCESS )
2549 {
2550 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2551 actual_mac, mac_length );
2552 }
2553
2554 if( output_size > 0 )
2555 memset( actual_mac, 0, output_size );
2556
2557 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002558 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002559 PSA_ASSERT( psa_mac_update( &operation,
2560 input->x, input->len ) );
2561 TEST_EQUAL( psa_mac_sign_finish( &operation,
2562 actual_mac, output_size,
2563 &mac_length ),
2564 expected_status );
2565 PSA_ASSERT( psa_mac_abort( &operation ) );
2566
2567 if( expected_status == PSA_SUCCESS )
2568 {
2569 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2570 actual_mac, mac_length );
2571 }
2572 mbedtls_free( actual_mac );
2573 actual_mac = NULL;
2574 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002575
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002576exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002577 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002578 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002579 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002580 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002581}
2582/* END_CASE */
2583
2584/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002585void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002586 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002587 int alg_arg,
2588 data_t *input,
2589 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002590{
Ronald Cron5425a212020-08-04 14:58:35 +02002591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592 psa_key_type_t key_type = key_type_arg;
2593 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002594 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002595 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002596 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597
Gilles Peskine69c12672018-06-28 00:07:19 +02002598 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2599
Gilles Peskine8817f612018-12-18 00:18:46 +01002600 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002602 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002603 psa_set_key_algorithm( &attributes, alg );
2604 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002605
Ronald Cron5425a212020-08-04 14:58:35 +02002606 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2607 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002608
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002609 /* Verify correct MAC, one-shot case. */
2610 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2611 expected_mac->x, expected_mac->len ) );
2612
2613 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002614 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002615 PSA_ASSERT( psa_mac_update( &operation,
2616 input->x, input->len ) );
2617 PSA_ASSERT( psa_mac_verify_finish( &operation,
2618 expected_mac->x,
2619 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002621 /* Test a MAC that's too short, one-shot case. */
2622 TEST_EQUAL( psa_mac_verify( key, alg,
2623 input->x, input->len,
2624 expected_mac->x,
2625 expected_mac->len - 1 ),
2626 PSA_ERROR_INVALID_SIGNATURE );
2627
2628 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002629 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002630 PSA_ASSERT( psa_mac_update( &operation,
2631 input->x, input->len ) );
2632 TEST_EQUAL( psa_mac_verify_finish( &operation,
2633 expected_mac->x,
2634 expected_mac->len - 1 ),
2635 PSA_ERROR_INVALID_SIGNATURE );
2636
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002637 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002638 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2639 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002640 TEST_EQUAL( psa_mac_verify( key, alg,
2641 input->x, input->len,
2642 perturbed_mac, expected_mac->len + 1 ),
2643 PSA_ERROR_INVALID_SIGNATURE );
2644
2645 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002646 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002647 PSA_ASSERT( psa_mac_update( &operation,
2648 input->x, input->len ) );
2649 TEST_EQUAL( psa_mac_verify_finish( &operation,
2650 perturbed_mac,
2651 expected_mac->len + 1 ),
2652 PSA_ERROR_INVALID_SIGNATURE );
2653
2654 /* Test changing one byte. */
2655 for( size_t i = 0; i < expected_mac->len; i++ )
2656 {
Chris Jones9634bb12021-01-20 15:56:42 +00002657 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002658 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002659
2660 TEST_EQUAL( psa_mac_verify( key, alg,
2661 input->x, input->len,
2662 perturbed_mac, expected_mac->len ),
2663 PSA_ERROR_INVALID_SIGNATURE );
2664
Ronald Cron5425a212020-08-04 14:58:35 +02002665 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002666 PSA_ASSERT( psa_mac_update( &operation,
2667 input->x, input->len ) );
2668 TEST_EQUAL( psa_mac_verify_finish( &operation,
2669 perturbed_mac,
2670 expected_mac->len ),
2671 PSA_ERROR_INVALID_SIGNATURE );
2672 perturbed_mac[i] ^= 1;
2673 }
2674
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002676 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002677 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002678 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002679 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002680}
2681/* END_CASE */
2682
2683/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002684void cipher_operation_init( )
2685{
Jaeden Ameroab439972019-02-15 14:12:05 +00002686 const uint8_t input[1] = { 0 };
2687 unsigned char output[1] = { 0 };
2688 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002689 /* Test each valid way of initializing the object, except for `= {0}`, as
2690 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2691 * though it's OK by the C standard. We could test for this, but we'd need
2692 * to supress the Clang warning for the test. */
2693 psa_cipher_operation_t func = psa_cipher_operation_init( );
2694 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2695 psa_cipher_operation_t zero;
2696
2697 memset( &zero, 0, sizeof( zero ) );
2698
Jaeden Ameroab439972019-02-15 14:12:05 +00002699 /* A freshly-initialized cipher operation should not be usable. */
2700 TEST_EQUAL( psa_cipher_update( &func,
2701 input, sizeof( input ),
2702 output, sizeof( output ),
2703 &output_length ),
2704 PSA_ERROR_BAD_STATE );
2705 TEST_EQUAL( psa_cipher_update( &init,
2706 input, sizeof( input ),
2707 output, sizeof( output ),
2708 &output_length ),
2709 PSA_ERROR_BAD_STATE );
2710 TEST_EQUAL( psa_cipher_update( &zero,
2711 input, sizeof( input ),
2712 output, sizeof( output ),
2713 &output_length ),
2714 PSA_ERROR_BAD_STATE );
2715
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002716 /* A default cipher operation should be abortable without error. */
2717 PSA_ASSERT( psa_cipher_abort( &func ) );
2718 PSA_ASSERT( psa_cipher_abort( &init ) );
2719 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002720}
2721/* END_CASE */
2722
2723/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002724void cipher_setup( int key_type_arg,
2725 data_t *key,
2726 int alg_arg,
2727 int expected_status_arg )
2728{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002729 psa_key_type_t key_type = key_type_arg;
2730 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002731 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002732 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002733 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002734#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002735 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2736#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002737
Gilles Peskine8817f612018-12-18 00:18:46 +01002738 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002739
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002740 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2741 &operation, &status ) )
2742 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002743 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002744
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002745 /* The operation object should be reusable. */
2746#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2747 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2748 smoke_test_key_data,
2749 sizeof( smoke_test_key_data ),
2750 KNOWN_SUPPORTED_CIPHER_ALG,
2751 &operation, &status ) )
2752 goto exit;
2753 TEST_EQUAL( status, PSA_SUCCESS );
2754#endif
2755
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002756exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002757 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002758 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002759}
2760/* END_CASE */
2761
Ronald Cronee414c72021-03-18 18:50:08 +01002762/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002763void cipher_bad_order( )
2764{
Ronald Cron5425a212020-08-04 14:58:35 +02002765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002766 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2767 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002769 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002770 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002771 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002772 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2773 0xaa, 0xaa, 0xaa, 0xaa };
2774 const uint8_t text[] = {
2775 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2776 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002777 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002778 size_t length = 0;
2779
2780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002781 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2782 psa_set_key_algorithm( &attributes, alg );
2783 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002784 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2785 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002786
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002787 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002788 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002789 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002790 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002791 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002792 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002793 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002794 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002795
2796 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002797 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002798 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002799 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002800 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002801 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002802 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002803 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002804
Jaeden Ameroab439972019-02-15 14:12:05 +00002805 /* Generate an IV without calling setup beforehand. */
2806 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2807 buffer, sizeof( buffer ),
2808 &length ),
2809 PSA_ERROR_BAD_STATE );
2810 PSA_ASSERT( psa_cipher_abort( &operation ) );
2811
2812 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002813 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002814 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2815 buffer, sizeof( buffer ),
2816 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002817 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002818 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2819 buffer, sizeof( buffer ),
2820 &length ),
2821 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002822 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002823 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002824 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002825
2826 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002827 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002828 PSA_ASSERT( psa_cipher_set_iv( &operation,
2829 iv, sizeof( iv ) ) );
2830 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2831 buffer, sizeof( buffer ),
2832 &length ),
2833 PSA_ERROR_BAD_STATE );
2834 PSA_ASSERT( psa_cipher_abort( &operation ) );
2835
2836 /* Set an IV without calling setup beforehand. */
2837 TEST_EQUAL( psa_cipher_set_iv( &operation,
2838 iv, sizeof( iv ) ),
2839 PSA_ERROR_BAD_STATE );
2840 PSA_ASSERT( psa_cipher_abort( &operation ) );
2841
2842 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002843 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002844 PSA_ASSERT( psa_cipher_set_iv( &operation,
2845 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002846 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002847 TEST_EQUAL( psa_cipher_set_iv( &operation,
2848 iv, sizeof( iv ) ),
2849 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002850 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002851 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002852 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002853
2854 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002855 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002856 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2857 buffer, sizeof( buffer ),
2858 &length ) );
2859 TEST_EQUAL( psa_cipher_set_iv( &operation,
2860 iv, sizeof( iv ) ),
2861 PSA_ERROR_BAD_STATE );
2862 PSA_ASSERT( psa_cipher_abort( &operation ) );
2863
2864 /* Call update without calling setup beforehand. */
2865 TEST_EQUAL( psa_cipher_update( &operation,
2866 text, sizeof( text ),
2867 buffer, sizeof( buffer ),
2868 &length ),
2869 PSA_ERROR_BAD_STATE );
2870 PSA_ASSERT( psa_cipher_abort( &operation ) );
2871
2872 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002873 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002874 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002875 TEST_EQUAL( psa_cipher_update( &operation,
2876 text, sizeof( text ),
2877 buffer, sizeof( buffer ),
2878 &length ),
2879 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002880 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002881 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002882 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002883
2884 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002885 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002886 PSA_ASSERT( psa_cipher_set_iv( &operation,
2887 iv, sizeof( iv ) ) );
2888 PSA_ASSERT( psa_cipher_finish( &operation,
2889 buffer, sizeof( buffer ), &length ) );
2890 TEST_EQUAL( psa_cipher_update( &operation,
2891 text, sizeof( text ),
2892 buffer, sizeof( buffer ),
2893 &length ),
2894 PSA_ERROR_BAD_STATE );
2895 PSA_ASSERT( psa_cipher_abort( &operation ) );
2896
2897 /* Call finish without calling setup beforehand. */
2898 TEST_EQUAL( psa_cipher_finish( &operation,
2899 buffer, sizeof( buffer ), &length ),
2900 PSA_ERROR_BAD_STATE );
2901 PSA_ASSERT( psa_cipher_abort( &operation ) );
2902
2903 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002904 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002905 /* Not calling update means we are encrypting an empty buffer, which is OK
2906 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002907 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002908 TEST_EQUAL( psa_cipher_finish( &operation,
2909 buffer, sizeof( buffer ), &length ),
2910 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002911 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002912 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002913 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002914
2915 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002916 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002917 PSA_ASSERT( psa_cipher_set_iv( &operation,
2918 iv, sizeof( iv ) ) );
2919 PSA_ASSERT( psa_cipher_finish( &operation,
2920 buffer, sizeof( buffer ), &length ) );
2921 TEST_EQUAL( psa_cipher_finish( &operation,
2922 buffer, sizeof( buffer ), &length ),
2923 PSA_ERROR_BAD_STATE );
2924 PSA_ASSERT( psa_cipher_abort( &operation ) );
2925
Ronald Cron5425a212020-08-04 14:58:35 +02002926 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002927
Jaeden Ameroab439972019-02-15 14:12:05 +00002928exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002929 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002930 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002931}
2932/* END_CASE */
2933
2934/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002935void cipher_encrypt_fail( int alg_arg,
2936 int key_type_arg,
2937 data_t *key_data,
2938 data_t *input,
2939 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002940{
Ronald Cron5425a212020-08-04 14:58:35 +02002941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002942 psa_status_t status;
2943 psa_key_type_t key_type = key_type_arg;
2944 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002945 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002946 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002947 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002948 size_t output_length = 0;
2949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2950
2951 if ( PSA_ERROR_BAD_STATE != expected_status )
2952 {
2953 PSA_ASSERT( psa_crypto_init( ) );
2954
2955 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2956 psa_set_key_algorithm( &attributes, alg );
2957 psa_set_key_type( &attributes, key_type );
2958
2959 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2960 input->len );
2961 ASSERT_ALLOC( output, output_buffer_size );
2962
2963 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2964 &key ) );
2965 }
2966
2967 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2968 output_buffer_size, &output_length );
2969
2970 TEST_EQUAL( status, expected_status );
2971
2972exit:
2973 mbedtls_free( output );
2974 psa_destroy_key( key );
2975 PSA_DONE( );
2976}
2977/* END_CASE */
2978
2979/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02002980void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
2981 data_t *input, int iv_length,
2982 int expected_result )
2983{
2984 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2985 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2987 size_t output_buffer_size = 0;
2988 unsigned char *output = NULL;
2989
2990 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2991 ASSERT_ALLOC( output, output_buffer_size );
2992
2993 PSA_ASSERT( psa_crypto_init( ) );
2994
2995 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2996 psa_set_key_algorithm( &attributes, alg );
2997 psa_set_key_type( &attributes, key_type );
2998
2999 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3000 &key ) );
3001 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3002 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3003 iv_length ) );
3004
3005exit:
3006 psa_cipher_abort( &operation );
3007 mbedtls_free( output );
3008 psa_destroy_key( key );
3009 PSA_DONE( );
3010}
3011/* END_CASE */
3012
3013/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003014void cipher_encrypt_alg_without_iv( int alg_arg,
3015 int key_type_arg,
3016 data_t *key_data,
3017 data_t *input,
3018 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003019{
3020 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3021 psa_key_type_t key_type = key_type_arg;
3022 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003023 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3024 uint8_t iv[1] = { 0x5a };
3025 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003026 unsigned char *output = NULL;
3027 size_t output_buffer_size = 0;
3028 size_t output_length = 0;
3029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3030
3031 PSA_ASSERT( psa_crypto_init( ) );
3032
3033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3034 psa_set_key_algorithm( &attributes, alg );
3035 psa_set_key_type( &attributes, key_type );
3036
3037 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3038 ASSERT_ALLOC( output, output_buffer_size );
3039
3040 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3041 &key ) );
3042
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003043 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3044 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3045 PSA_ERROR_BAD_STATE );
3046 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3047 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
3048 &iv_length ),
3049 PSA_ERROR_BAD_STATE );
3050
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003051 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
3052 output_buffer_size, &output_length ) );
3053 TEST_ASSERT( output_length <=
3054 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3055 TEST_ASSERT( output_length <=
3056 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3057
3058 ASSERT_COMPARE( expected_output->x, expected_output->len,
3059 output, output_length );
3060exit:
3061 mbedtls_free( output );
3062 psa_destroy_key( key );
3063 PSA_DONE( );
3064}
3065/* END_CASE */
3066
3067/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003068void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3069{
3070 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3071 psa_algorithm_t alg = alg_arg;
3072 psa_key_type_t key_type = key_type_arg;
3073 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3074 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3075 psa_status_t status;
3076
3077 PSA_ASSERT( psa_crypto_init( ) );
3078
3079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3080 psa_set_key_algorithm( &attributes, alg );
3081 psa_set_key_type( &attributes, key_type );
3082
3083 /* Usage of either of these two size macros would cause divide by zero
3084 * with incorrect key types previously. Input length should be irrelevant
3085 * here. */
3086 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3087 0 );
3088 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3089
3090
3091 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3092 &key ) );
3093
3094 /* Should fail due to invalid alg type (to support invalid key type).
3095 * Encrypt or decrypt will end up in the same place. */
3096 status = psa_cipher_encrypt_setup( &operation, key, alg );
3097
3098 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3099
3100exit:
3101 psa_cipher_abort( &operation );
3102 psa_destroy_key( key );
3103 PSA_DONE( );
3104}
3105/* END_CASE */
3106
3107/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003108void cipher_encrypt_validation( int alg_arg,
3109 int key_type_arg,
3110 data_t *key_data,
3111 data_t *input )
3112{
3113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3114 psa_key_type_t key_type = key_type_arg;
3115 psa_algorithm_t alg = alg_arg;
3116 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3117 unsigned char *output1 = NULL;
3118 size_t output1_buffer_size = 0;
3119 size_t output1_length = 0;
3120 unsigned char *output2 = NULL;
3121 size_t output2_buffer_size = 0;
3122 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003123 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003124 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003125 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003126
Gilles Peskine8817f612018-12-18 00:18:46 +01003127 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003128
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003129 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3130 psa_set_key_algorithm( &attributes, alg );
3131 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003132
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003133 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3134 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3135 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3136 ASSERT_ALLOC( output1, output1_buffer_size );
3137 ASSERT_ALLOC( output2, output2_buffer_size );
3138
Ronald Cron5425a212020-08-04 14:58:35 +02003139 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3140 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003141
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003142 /* The one-shot cipher encryption uses generated iv so validating
3143 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003144 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3145 output1_buffer_size, &output1_length ) );
3146 TEST_ASSERT( output1_length <=
3147 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3148 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003149 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003150
3151 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3152 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003153
Gilles Peskine8817f612018-12-18 00:18:46 +01003154 PSA_ASSERT( psa_cipher_update( &operation,
3155 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003156 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003157 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003158 TEST_ASSERT( function_output_length <=
3159 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3160 TEST_ASSERT( function_output_length <=
3161 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003162 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003163
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003164 PSA_ASSERT( psa_cipher_finish( &operation,
3165 output2 + output2_length,
3166 output2_buffer_size - output2_length,
3167 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003168 TEST_ASSERT( function_output_length <=
3169 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3170 TEST_ASSERT( function_output_length <=
3171 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003172 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003173
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003174 PSA_ASSERT( psa_cipher_abort( &operation ) );
3175 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3176 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003177
Gilles Peskine50e586b2018-06-08 14:28:46 +02003178exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003179 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003180 mbedtls_free( output1 );
3181 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003182 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003183 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003184}
3185/* END_CASE */
3186
3187/* BEGIN_CASE */
3188void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003189 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003190 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003191 int first_part_size_arg,
3192 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003193 data_t *expected_output,
3194 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003195{
Ronald Cron5425a212020-08-04 14:58:35 +02003196 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003197 psa_key_type_t key_type = key_type_arg;
3198 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003199 psa_status_t status;
3200 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003201 size_t first_part_size = first_part_size_arg;
3202 size_t output1_length = output1_length_arg;
3203 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003204 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003205 size_t output_buffer_size = 0;
3206 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003207 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003208 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003210
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003212
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003213 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3214 psa_set_key_algorithm( &attributes, alg );
3215 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003216
Ronald Cron5425a212020-08-04 14:58:35 +02003217 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3218 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003219
Ronald Cron5425a212020-08-04 14:58:35 +02003220 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003221
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003222 if( iv->len > 0 )
3223 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003224 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003225 }
3226
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003227 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3228 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003229 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003230
Gilles Peskinee0866522019-02-19 19:44:00 +01003231 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003232 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3233 output, output_buffer_size,
3234 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003235 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003236 TEST_ASSERT( function_output_length <=
3237 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3238 TEST_ASSERT( function_output_length <=
3239 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003240 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003241
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003242 if( first_part_size < input->len )
3243 {
3244 PSA_ASSERT( psa_cipher_update( &operation,
3245 input->x + first_part_size,
3246 input->len - first_part_size,
3247 ( output_buffer_size == 0 ? NULL :
3248 output + total_output_length ),
3249 output_buffer_size - total_output_length,
3250 &function_output_length ) );
3251 TEST_ASSERT( function_output_length == output2_length );
3252 TEST_ASSERT( function_output_length <=
3253 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3254 alg,
3255 input->len - first_part_size ) );
3256 TEST_ASSERT( function_output_length <=
3257 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3258 total_output_length += function_output_length;
3259 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003260
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003261 status = psa_cipher_finish( &operation,
3262 ( output_buffer_size == 0 ? NULL :
3263 output + total_output_length ),
3264 output_buffer_size - total_output_length,
3265 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003266 TEST_ASSERT( function_output_length <=
3267 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3268 TEST_ASSERT( function_output_length <=
3269 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003270 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003271 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003272
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003273 if( expected_status == PSA_SUCCESS )
3274 {
3275 PSA_ASSERT( psa_cipher_abort( &operation ) );
3276
3277 ASSERT_COMPARE( expected_output->x, expected_output->len,
3278 output, total_output_length );
3279 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003280
3281exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003282 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003283 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003284 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003285 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003286}
3287/* END_CASE */
3288
3289/* BEGIN_CASE */
3290void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003291 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003292 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003293 int first_part_size_arg,
3294 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003295 data_t *expected_output,
3296 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003297{
Ronald Cron5425a212020-08-04 14:58:35 +02003298 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003299 psa_key_type_t key_type = key_type_arg;
3300 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003301 psa_status_t status;
3302 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003303 size_t first_part_size = first_part_size_arg;
3304 size_t output1_length = output1_length_arg;
3305 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003306 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003307 size_t output_buffer_size = 0;
3308 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003309 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003310 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003311 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003312
Gilles Peskine8817f612018-12-18 00:18:46 +01003313 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003314
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003315 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3316 psa_set_key_algorithm( &attributes, alg );
3317 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003318
Ronald Cron5425a212020-08-04 14:58:35 +02003319 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3320 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003321
Ronald Cron5425a212020-08-04 14:58:35 +02003322 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003323
Steven Cooreman177deba2020-09-07 17:14:14 +02003324 if( iv->len > 0 )
3325 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003326 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003327 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003328
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003329 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3330 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003331 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003332
Gilles Peskinee0866522019-02-19 19:44:00 +01003333 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003334 PSA_ASSERT( psa_cipher_update( &operation,
3335 input->x, first_part_size,
3336 output, output_buffer_size,
3337 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003338 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003339 TEST_ASSERT( function_output_length <=
3340 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3341 TEST_ASSERT( function_output_length <=
3342 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003343 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003344
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003345 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003346 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003347 PSA_ASSERT( psa_cipher_update( &operation,
3348 input->x + first_part_size,
3349 input->len - first_part_size,
3350 ( output_buffer_size == 0 ? NULL :
3351 output + total_output_length ),
3352 output_buffer_size - total_output_length,
3353 &function_output_length ) );
3354 TEST_ASSERT( function_output_length == output2_length );
3355 TEST_ASSERT( function_output_length <=
3356 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3357 alg,
3358 input->len - first_part_size ) );
3359 TEST_ASSERT( function_output_length <=
3360 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3361 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003362 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003363
Gilles Peskine50e586b2018-06-08 14:28:46 +02003364 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003365 ( output_buffer_size == 0 ? NULL :
3366 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003367 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003368 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003369 TEST_ASSERT( function_output_length <=
3370 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3371 TEST_ASSERT( function_output_length <=
3372 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003373 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003374 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003375
3376 if( expected_status == PSA_SUCCESS )
3377 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003378 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003379
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003380 ASSERT_COMPARE( expected_output->x, expected_output->len,
3381 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003382 }
3383
Gilles Peskine50e586b2018-06-08 14:28:46 +02003384exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003385 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003386 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003387 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003388 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003389}
3390/* END_CASE */
3391
Gilles Peskine50e586b2018-06-08 14:28:46 +02003392/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003393void cipher_decrypt_fail( int alg_arg,
3394 int key_type_arg,
3395 data_t *key_data,
3396 data_t *iv,
3397 data_t *input_arg,
3398 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003399{
3400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3401 psa_status_t status;
3402 psa_key_type_t key_type = key_type_arg;
3403 psa_algorithm_t alg = alg_arg;
3404 psa_status_t expected_status = expected_status_arg;
3405 unsigned char *input = NULL;
3406 size_t input_buffer_size = 0;
3407 unsigned char *output = NULL;
3408 size_t output_buffer_size = 0;
3409 size_t output_length = 0;
3410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3411
3412 if ( PSA_ERROR_BAD_STATE != expected_status )
3413 {
3414 PSA_ASSERT( psa_crypto_init( ) );
3415
3416 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3417 psa_set_key_algorithm( &attributes, alg );
3418 psa_set_key_type( &attributes, key_type );
3419
3420 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3421 &key ) );
3422 }
3423
3424 /* Allocate input buffer and copy the iv and the plaintext */
3425 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3426 if ( input_buffer_size > 0 )
3427 {
3428 ASSERT_ALLOC( input, input_buffer_size );
3429 memcpy( input, iv->x, iv->len );
3430 memcpy( input + iv->len, input_arg->x, input_arg->len );
3431 }
3432
3433 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3434 ASSERT_ALLOC( output, output_buffer_size );
3435
3436 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3437 output_buffer_size, &output_length );
3438 TEST_EQUAL( status, expected_status );
3439
3440exit:
3441 mbedtls_free( input );
3442 mbedtls_free( output );
3443 psa_destroy_key( key );
3444 PSA_DONE( );
3445}
3446/* END_CASE */
3447
3448/* BEGIN_CASE */
3449void cipher_decrypt( int alg_arg,
3450 int key_type_arg,
3451 data_t *key_data,
3452 data_t *iv,
3453 data_t *input_arg,
3454 data_t *expected_output )
3455{
3456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3457 psa_key_type_t key_type = key_type_arg;
3458 psa_algorithm_t alg = alg_arg;
3459 unsigned char *input = NULL;
3460 size_t input_buffer_size = 0;
3461 unsigned char *output = NULL;
3462 size_t output_buffer_size = 0;
3463 size_t output_length = 0;
3464 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3465
3466 PSA_ASSERT( psa_crypto_init( ) );
3467
3468 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3469 psa_set_key_algorithm( &attributes, alg );
3470 psa_set_key_type( &attributes, key_type );
3471
3472 /* Allocate input buffer and copy the iv and the plaintext */
3473 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3474 if ( input_buffer_size > 0 )
3475 {
3476 ASSERT_ALLOC( input, input_buffer_size );
3477 memcpy( input, iv->x, iv->len );
3478 memcpy( input + iv->len, input_arg->x, input_arg->len );
3479 }
3480
3481 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3482 ASSERT_ALLOC( output, output_buffer_size );
3483
3484 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3485 &key ) );
3486
3487 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3488 output_buffer_size, &output_length ) );
3489 TEST_ASSERT( output_length <=
3490 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3491 TEST_ASSERT( output_length <=
3492 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3493
3494 ASSERT_COMPARE( expected_output->x, expected_output->len,
3495 output, output_length );
3496exit:
3497 mbedtls_free( input );
3498 mbedtls_free( output );
3499 psa_destroy_key( key );
3500 PSA_DONE( );
3501}
3502/* END_CASE */
3503
3504/* BEGIN_CASE */
3505void cipher_verify_output( int alg_arg,
3506 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003507 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003508 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003509{
Ronald Cron5425a212020-08-04 14:58:35 +02003510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003511 psa_key_type_t key_type = key_type_arg;
3512 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003513 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003514 size_t output1_size = 0;
3515 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003516 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003517 size_t output2_size = 0;
3518 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003520
Gilles Peskine8817f612018-12-18 00:18:46 +01003521 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003522
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003523 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3524 psa_set_key_algorithm( &attributes, alg );
3525 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003526
Ronald Cron5425a212020-08-04 14:58:35 +02003527 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3528 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003529 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003530 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003531
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003532 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3533 output1, output1_size,
3534 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003535 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003536 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003537 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003538 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003539
3540 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003541 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003542
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003543 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3544 output2, output2_size,
3545 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003546 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003547 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003548 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003549 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003550
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003551 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003552
3553exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003554 mbedtls_free( output1 );
3555 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003556 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003557 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003558}
3559/* END_CASE */
3560
3561/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003562void cipher_verify_output_multipart( int alg_arg,
3563 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003564 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003565 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003566 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003567{
Ronald Cron5425a212020-08-04 14:58:35 +02003568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003569 psa_key_type_t key_type = key_type_arg;
3570 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003571 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003572 unsigned char iv[16] = {0};
3573 size_t iv_size = 16;
3574 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003575 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003576 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003577 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003578 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003579 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003580 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003581 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003582 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3583 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003585
Gilles Peskine8817f612018-12-18 00:18:46 +01003586 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003587
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003588 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3589 psa_set_key_algorithm( &attributes, alg );
3590 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003591
Ronald Cron5425a212020-08-04 14:58:35 +02003592 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3593 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003594
Ronald Cron5425a212020-08-04 14:58:35 +02003595 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3596 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003597
Steven Cooreman177deba2020-09-07 17:14:14 +02003598 if( alg != PSA_ALG_ECB_NO_PADDING )
3599 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003600 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3601 iv, iv_size,
3602 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003603 }
3604
gabor-mezei-armceface22021-01-21 12:26:17 +01003605 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3606 TEST_ASSERT( output1_buffer_size <=
3607 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003608 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003609
Gilles Peskinee0866522019-02-19 19:44:00 +01003610 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003611
Gilles Peskine8817f612018-12-18 00:18:46 +01003612 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3613 output1, output1_buffer_size,
3614 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003615 TEST_ASSERT( function_output_length <=
3616 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3617 TEST_ASSERT( function_output_length <=
3618 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003619 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003620
Gilles Peskine8817f612018-12-18 00:18:46 +01003621 PSA_ASSERT( psa_cipher_update( &operation1,
3622 input->x + first_part_size,
3623 input->len - first_part_size,
3624 output1, output1_buffer_size,
3625 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003626 TEST_ASSERT( function_output_length <=
3627 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3628 alg,
3629 input->len - first_part_size ) );
3630 TEST_ASSERT( function_output_length <=
3631 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003632 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003633
Gilles Peskine8817f612018-12-18 00:18:46 +01003634 PSA_ASSERT( psa_cipher_finish( &operation1,
3635 output1 + output1_length,
3636 output1_buffer_size - output1_length,
3637 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003638 TEST_ASSERT( function_output_length <=
3639 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3640 TEST_ASSERT( function_output_length <=
3641 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003642 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003643
Gilles Peskine8817f612018-12-18 00:18:46 +01003644 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003645
Gilles Peskine048b7f02018-06-08 14:20:49 +02003646 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003647 TEST_ASSERT( output2_buffer_size <=
3648 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3649 TEST_ASSERT( output2_buffer_size <=
3650 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003651 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003652
Steven Cooreman177deba2020-09-07 17:14:14 +02003653 if( iv_length > 0 )
3654 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003655 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3656 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003657 }
Moran Pekerded84402018-06-06 16:36:50 +03003658
Gilles Peskine8817f612018-12-18 00:18:46 +01003659 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3660 output2, output2_buffer_size,
3661 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003662 TEST_ASSERT( function_output_length <=
3663 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3664 TEST_ASSERT( function_output_length <=
3665 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003666 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003667
Gilles Peskine8817f612018-12-18 00:18:46 +01003668 PSA_ASSERT( psa_cipher_update( &operation2,
3669 output1 + first_part_size,
3670 output1_length - first_part_size,
3671 output2, output2_buffer_size,
3672 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003673 TEST_ASSERT( function_output_length <=
3674 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3675 alg,
3676 output1_length - first_part_size ) );
3677 TEST_ASSERT( function_output_length <=
3678 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003679 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003680
Gilles Peskine8817f612018-12-18 00:18:46 +01003681 PSA_ASSERT( psa_cipher_finish( &operation2,
3682 output2 + output2_length,
3683 output2_buffer_size - output2_length,
3684 &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 Peskine048b7f02018-06-08 14:20:49 +02003689 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003690
Gilles Peskine8817f612018-12-18 00:18:46 +01003691 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003692
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003693 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003694
3695exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003696 psa_cipher_abort( &operation1 );
3697 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003698 mbedtls_free( output1 );
3699 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003701 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003702}
3703/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003704
Gilles Peskine20035e32018-02-03 22:44:14 +01003705/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003706void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003707 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003708 data_t *nonce,
3709 data_t *additional_data,
3710 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003711 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003712{
Ronald Cron5425a212020-08-04 14:58:35 +02003713 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003714 psa_key_type_t key_type = key_type_arg;
3715 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003716 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003717 unsigned char *output_data = NULL;
3718 size_t output_size = 0;
3719 size_t output_length = 0;
3720 unsigned char *output_data2 = NULL;
3721 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003722 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003723 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003725
Gilles Peskine8817f612018-12-18 00:18:46 +01003726 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003727
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3729 psa_set_key_algorithm( &attributes, alg );
3730 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003731
Gilles Peskine049c7532019-05-15 20:22:09 +02003732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003733 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003734 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3735 key_bits = psa_get_key_bits( &attributes );
3736
3737 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3738 alg );
3739 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3740 * should be exact. */
3741 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3742 expected_result != PSA_ERROR_NOT_SUPPORTED )
3743 {
3744 TEST_EQUAL( output_size,
3745 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3746 TEST_ASSERT( output_size <=
3747 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3748 }
3749 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003750
Steven Cooremanf49478b2021-02-15 15:19:25 +01003751 status = psa_aead_encrypt( key, alg,
3752 nonce->x, nonce->len,
3753 additional_data->x,
3754 additional_data->len,
3755 input_data->x, input_data->len,
3756 output_data, output_size,
3757 &output_length );
3758
3759 /* If the operation is not supported, just skip and not fail in case the
3760 * encryption involves a common limitation of cryptography hardwares and
3761 * an alternative implementation. */
3762 if( status == PSA_ERROR_NOT_SUPPORTED )
3763 {
3764 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3765 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3766 }
3767
3768 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003769
3770 if( PSA_SUCCESS == expected_result )
3771 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003772 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003773
Gilles Peskine003a4a92019-05-14 16:09:40 +02003774 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3775 * should be exact. */
3776 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003777 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003778
gabor-mezei-armceface22021-01-21 12:26:17 +01003779 TEST_ASSERT( input_data->len <=
3780 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3781
Ronald Cron5425a212020-08-04 14:58:35 +02003782 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003783 nonce->x, nonce->len,
3784 additional_data->x,
3785 additional_data->len,
3786 output_data, output_length,
3787 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003788 &output_length2 ),
3789 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003790
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003791 ASSERT_COMPARE( input_data->x, input_data->len,
3792 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003793 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003794
Gilles Peskinea1cac842018-06-11 19:33:02 +02003795exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003796 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003797 mbedtls_free( output_data );
3798 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003799 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003800}
3801/* END_CASE */
3802
3803/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003804void aead_encrypt( int key_type_arg, data_t *key_data,
3805 int alg_arg,
3806 data_t *nonce,
3807 data_t *additional_data,
3808 data_t *input_data,
3809 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003810{
Ronald Cron5425a212020-08-04 14:58:35 +02003811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003812 psa_key_type_t key_type = key_type_arg;
3813 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003814 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003815 unsigned char *output_data = NULL;
3816 size_t output_size = 0;
3817 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003819 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003820
Gilles Peskine8817f612018-12-18 00:18:46 +01003821 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003822
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003823 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3824 psa_set_key_algorithm( &attributes, alg );
3825 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003826
Gilles Peskine049c7532019-05-15 20:22:09 +02003827 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003828 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003829 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3830 key_bits = psa_get_key_bits( &attributes );
3831
3832 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3833 alg );
3834 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3835 * should be exact. */
3836 TEST_EQUAL( output_size,
3837 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3838 TEST_ASSERT( output_size <=
3839 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3840 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003841
Steven Cooremand588ea12021-01-11 19:36:04 +01003842 status = psa_aead_encrypt( key, alg,
3843 nonce->x, nonce->len,
3844 additional_data->x, additional_data->len,
3845 input_data->x, input_data->len,
3846 output_data, output_size,
3847 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003848
Ronald Cron28a45ed2021-02-09 20:35:42 +01003849 /* If the operation is not supported, just skip and not fail in case the
3850 * encryption involves a common limitation of cryptography hardwares and
3851 * an alternative implementation. */
3852 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003853 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003854 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3855 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003856 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003857
3858 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003859 ASSERT_COMPARE( expected_result->x, expected_result->len,
3860 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003861
Gilles Peskinea1cac842018-06-11 19:33:02 +02003862exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003863 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003864 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003865 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003866}
3867/* END_CASE */
3868
3869/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003870void aead_decrypt( int key_type_arg, data_t *key_data,
3871 int alg_arg,
3872 data_t *nonce,
3873 data_t *additional_data,
3874 data_t *input_data,
3875 data_t *expected_data,
3876 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003877{
Ronald Cron5425a212020-08-04 14:58:35 +02003878 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003879 psa_key_type_t key_type = key_type_arg;
3880 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003881 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003882 unsigned char *output_data = NULL;
3883 size_t output_size = 0;
3884 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003886 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003887 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003888
Gilles Peskine8817f612018-12-18 00:18:46 +01003889 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003890
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003891 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3892 psa_set_key_algorithm( &attributes, alg );
3893 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003894
Gilles Peskine049c7532019-05-15 20:22:09 +02003895 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003896 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003897 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3898 key_bits = psa_get_key_bits( &attributes );
3899
3900 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3901 alg );
3902 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3903 expected_result != PSA_ERROR_NOT_SUPPORTED )
3904 {
3905 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3906 * should be exact. */
3907 TEST_EQUAL( output_size,
3908 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3909 TEST_ASSERT( output_size <=
3910 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3911 }
3912 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003913
Steven Cooremand588ea12021-01-11 19:36:04 +01003914 status = psa_aead_decrypt( key, alg,
3915 nonce->x, nonce->len,
3916 additional_data->x,
3917 additional_data->len,
3918 input_data->x, input_data->len,
3919 output_data, output_size,
3920 &output_length );
3921
Ronald Cron28a45ed2021-02-09 20:35:42 +01003922 /* If the operation is not supported, just skip and not fail in case the
3923 * decryption involves a common limitation of cryptography hardwares and
3924 * an alternative implementation. */
3925 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003926 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003927 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3928 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003929 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003930
3931 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003932
Gilles Peskine2d277862018-06-18 15:41:12 +02003933 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003934 ASSERT_COMPARE( expected_data->x, expected_data->len,
3935 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003936
Gilles Peskinea1cac842018-06-11 19:33:02 +02003937exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003938 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003939 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003940 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003941}
3942/* END_CASE */
3943
3944/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003945void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3946 int alg_arg,
3947 data_t *nonce,
3948 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003949 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003950 int do_set_lengths,
3951 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003952{
Paul Elliottd3f82412021-06-16 16:52:21 +01003953 size_t ad_part_len = 0;
3954 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003955 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003956
Paul Elliott32f46ba2021-09-23 18:24:36 +01003957 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003958 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003959 mbedtls_test_set_step( ad_part_len );
3960
3961 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003962 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003963 if( ad_part_len & 0x01 )
3964 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3965 else
3966 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003967 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003968
3969 /* Split ad into length(ad_part_len) parts. */
3970 if( !aead_multipart_internal_func( key_type_arg, key_data,
3971 alg_arg, nonce,
3972 additional_data,
3973 ad_part_len,
3974 input_data, -1,
3975 set_lengths_method,
3976 expected_output,
3977 1, 0 ) )
3978 break;
3979
3980 /* length(0) part, length(ad_part_len) part, length(0) part... */
3981 mbedtls_test_set_step( 1000 + ad_part_len );
3982
3983 if( !aead_multipart_internal_func( key_type_arg, key_data,
3984 alg_arg, nonce,
3985 additional_data,
3986 ad_part_len,
3987 input_data, -1,
3988 set_lengths_method,
3989 expected_output,
3990 1, 1 ) )
3991 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003992 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003993
Paul Elliott32f46ba2021-09-23 18:24:36 +01003994 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003995 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003996 /* Split data into length(data_part_len) parts. */
3997 mbedtls_test_set_step( 2000 + data_part_len );
3998
3999 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004000 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004001 if( data_part_len & 0x01 )
4002 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4003 else
4004 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004005 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004006
Paul Elliott32f46ba2021-09-23 18:24:36 +01004007 if( !aead_multipart_internal_func( key_type_arg, key_data,
4008 alg_arg, nonce,
4009 additional_data, -1,
4010 input_data, data_part_len,
4011 set_lengths_method,
4012 expected_output,
4013 1, 0 ) )
4014 break;
4015
4016 /* length(0) part, length(data_part_len) part, length(0) part... */
4017 mbedtls_test_set_step( 3000 + data_part_len );
4018
4019 if( !aead_multipart_internal_func( key_type_arg, key_data,
4020 alg_arg, nonce,
4021 additional_data, -1,
4022 input_data, data_part_len,
4023 set_lengths_method,
4024 expected_output,
4025 1, 1 ) )
4026 break;
4027 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004028
Paul Elliott8fc45162021-06-23 16:06:01 +01004029 /* Goto is required to silence warnings about unused labels, as we
4030 * don't actually do any test assertions in this function. */
4031 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004032}
4033/* END_CASE */
4034
4035/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004036void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4037 int alg_arg,
4038 data_t *nonce,
4039 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004040 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004041 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004042 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004043{
Paul Elliottd3f82412021-06-16 16:52:21 +01004044 size_t ad_part_len = 0;
4045 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004046 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004047
Paul Elliott32f46ba2021-09-23 18:24:36 +01004048 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004049 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004050 /* Split ad into length(ad_part_len) parts. */
4051 mbedtls_test_set_step( ad_part_len );
4052
4053 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004054 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004055 if( ad_part_len & 0x01 )
4056 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4057 else
4058 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004059 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004060
4061 if( !aead_multipart_internal_func( key_type_arg, key_data,
4062 alg_arg, nonce,
4063 additional_data,
4064 ad_part_len,
4065 input_data, -1,
4066 set_lengths_method,
4067 expected_output,
4068 0, 0 ) )
4069 break;
4070
4071 /* length(0) part, length(ad_part_len) part, length(0) part... */
4072 mbedtls_test_set_step( 1000 + ad_part_len );
4073
4074 if( !aead_multipart_internal_func( key_type_arg, key_data,
4075 alg_arg, nonce,
4076 additional_data,
4077 ad_part_len,
4078 input_data, -1,
4079 set_lengths_method,
4080 expected_output,
4081 0, 1 ) )
4082 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004083 }
4084
Paul Elliott32f46ba2021-09-23 18:24:36 +01004085 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004086 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004087 /* Split data into length(data_part_len) parts. */
4088 mbedtls_test_set_step( 2000 + data_part_len );
4089
4090 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004091 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004092 if( data_part_len & 0x01 )
4093 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4094 else
4095 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004096 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004097
4098 if( !aead_multipart_internal_func( key_type_arg, key_data,
4099 alg_arg, nonce,
4100 additional_data, -1,
4101 input_data, data_part_len,
4102 set_lengths_method,
4103 expected_output,
4104 0, 0 ) )
4105 break;
4106
4107 /* length(0) part, length(data_part_len) part, length(0) part... */
4108 mbedtls_test_set_step( 3000 + data_part_len );
4109
4110 if( !aead_multipart_internal_func( key_type_arg, key_data,
4111 alg_arg, nonce,
4112 additional_data, -1,
4113 input_data, data_part_len,
4114 set_lengths_method,
4115 expected_output,
4116 0, 1 ) )
4117 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004118 }
4119
Paul Elliott8fc45162021-06-23 16:06:01 +01004120 /* Goto is required to silence warnings about unused labels, as we
4121 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004122 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004123}
4124/* END_CASE */
4125
4126/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004127void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4128 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004129 int nonce_length,
4130 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004131 data_t *additional_data,
4132 data_t *input_data,
4133 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004134{
4135
4136 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4137 psa_key_type_t key_type = key_type_arg;
4138 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004139 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004140 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4141 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4142 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004143 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004144 size_t actual_nonce_length = 0;
4145 size_t expected_nonce_length = expected_nonce_length_arg;
4146 unsigned char *output = NULL;
4147 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004148 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004149 size_t ciphertext_size = 0;
4150 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004151 size_t tag_length = 0;
4152 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004153
4154 PSA_ASSERT( psa_crypto_init( ) );
4155
4156 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4157 psa_set_key_algorithm( & attributes, alg );
4158 psa_set_key_type( & attributes, key_type );
4159
4160 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4161 &key ) );
4162
4163 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4164
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004165 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4166
Paul Elliottf1277632021-08-24 18:11:37 +01004167 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004168
Paul Elliottf1277632021-08-24 18:11:37 +01004169 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004170
Paul Elliottf1277632021-08-24 18:11:37 +01004171 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004172
Paul Elliottf1277632021-08-24 18:11:37 +01004173 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004174
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004175 status = psa_aead_encrypt_setup( &operation, key, alg );
4176
4177 /* If the operation is not supported, just skip and not fail in case the
4178 * encryption involves a common limitation of cryptography hardwares and
4179 * an alternative implementation. */
4180 if( status == PSA_ERROR_NOT_SUPPORTED )
4181 {
4182 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004183 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004184 }
4185
4186 PSA_ASSERT( status );
4187
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004188 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004189 nonce_length,
4190 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004191
Paul Elliott693bf312021-07-23 17:40:41 +01004192 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004193
Paul Elliottf1277632021-08-24 18:11:37 +01004194 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004195
Paul Elliott88ecbe12021-09-22 17:23:03 +01004196 if( expected_status == PSA_SUCCESS )
4197 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4198 alg ) );
4199
Paul Elliottd79c5c52021-10-06 21:49:41 +01004200 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004201
Paul Elliott693bf312021-07-23 17:40:41 +01004202 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004203 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004204 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004205 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4206 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004207
4208 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4209 additional_data->len ) );
4210
4211 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004212 output, output_size,
4213 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004214
Paul Elliottf1277632021-08-24 18:11:37 +01004215 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4216 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004217 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4218 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004219
4220exit:
4221 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004222 mbedtls_free( output );
4223 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004224 psa_aead_abort( &operation );
4225 PSA_DONE( );
4226}
4227/* END_CASE */
4228
4229/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004230void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4231 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004232 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004233 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004234 data_t *additional_data,
4235 data_t *input_data,
4236 int expected_status_arg )
4237{
4238
4239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4240 psa_key_type_t key_type = key_type_arg;
4241 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004242 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004243 uint8_t *nonce_buffer = NULL;
4244 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4245 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4246 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004247 unsigned char *output = NULL;
4248 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004249 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004250 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004251 size_t ciphertext_size = 0;
4252 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004253 size_t tag_length = 0;
4254 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004255 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004256 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004257
4258 PSA_ASSERT( psa_crypto_init( ) );
4259
4260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4261 psa_set_key_algorithm( &attributes, alg );
4262 psa_set_key_type( &attributes, key_type );
4263
4264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4265 &key ) );
4266
4267 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4268
4269 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4270
Paul Elliott6f0e7202021-08-25 12:57:18 +01004271 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004272
Paul Elliott6f0e7202021-08-25 12:57:18 +01004273 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004274
Paul Elliott6f0e7202021-08-25 12:57:18 +01004275 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004276
Paul Elliott6f0e7202021-08-25 12:57:18 +01004277 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004278
Paul Elliott863864a2021-07-23 17:28:31 +01004279 status = psa_aead_encrypt_setup( &operation, key, alg );
4280
4281 /* If the operation is not supported, just skip and not fail in case the
4282 * encryption involves a common limitation of cryptography hardwares and
4283 * an alternative implementation. */
4284 if( status == PSA_ERROR_NOT_SUPPORTED )
4285 {
4286 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004287 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004288 }
4289
4290 PSA_ASSERT( status );
4291
Paul Elliott4023ffd2021-09-10 16:21:22 +01004292 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4293 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004294 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004295 /* Arbitrary size buffer, to test zero length valid buffer. */
4296 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004297 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004298 }
4299 else
4300 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004301 /* If length is zero, then this will return NULL. */
4302 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004303 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004304
Paul Elliott4023ffd2021-09-10 16:21:22 +01004305 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004306 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004307 for( index = 0; index < nonce_length - 1; ++index )
4308 {
4309 nonce_buffer[index] = 'a' + index;
4310 }
Paul Elliott66696b52021-08-16 18:42:41 +01004311 }
Paul Elliott863864a2021-07-23 17:28:31 +01004312 }
4313
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004314 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4315 {
4316 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4317 input_data->len ) );
4318 }
4319
Paul Elliott6f0e7202021-08-25 12:57:18 +01004320 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004321
Paul Elliott693bf312021-07-23 17:40:41 +01004322 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004323
4324 if( expected_status == PSA_SUCCESS )
4325 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004326 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4327 {
4328 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4329 input_data->len ) );
4330 }
4331 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4332 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004333
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004334 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4335 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4336 additional_data->len ),
4337 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004338
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004339 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004340 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004341 &ciphertext_length ),
4342 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004343
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004344 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004345 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004346 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4347 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004348 }
4349
4350exit:
4351 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004352 mbedtls_free( output );
4353 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004354 mbedtls_free( nonce_buffer );
4355 psa_aead_abort( &operation );
4356 PSA_DONE( );
4357}
4358/* END_CASE */
4359
4360/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004361void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4362 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004363 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004364 data_t *nonce,
4365 data_t *additional_data,
4366 data_t *input_data,
4367 int expected_status_arg )
4368{
4369
4370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4371 psa_key_type_t key_type = key_type_arg;
4372 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004373 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4375 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4376 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004377 unsigned char *output = NULL;
4378 unsigned char *ciphertext = NULL;
4379 size_t output_size = output_size_arg;
4380 size_t ciphertext_size = 0;
4381 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004382 size_t tag_length = 0;
4383 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4384
4385 PSA_ASSERT( psa_crypto_init( ) );
4386
4387 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4388 psa_set_key_algorithm( &attributes, alg );
4389 psa_set_key_type( &attributes, key_type );
4390
4391 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4392 &key ) );
4393
4394 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4395
Paul Elliottc6d11d02021-09-01 12:04:23 +01004396 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004397
Paul Elliottc6d11d02021-09-01 12:04:23 +01004398 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004399
Paul Elliottc6d11d02021-09-01 12:04:23 +01004400 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004401
Paul Elliott43fbda62021-07-23 18:30:59 +01004402 status = psa_aead_encrypt_setup( &operation, key, alg );
4403
4404 /* If the operation is not supported, just skip and not fail in case the
4405 * encryption involves a common limitation of cryptography hardwares and
4406 * an alternative implementation. */
4407 if( status == PSA_ERROR_NOT_SUPPORTED )
4408 {
4409 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4410 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4411 }
4412
4413 PSA_ASSERT( status );
4414
Paul Elliott47b9a142021-10-07 15:04:57 +01004415 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4416 input_data->len ) );
4417
Paul Elliott43fbda62021-07-23 18:30:59 +01004418 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4419
4420 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4421 additional_data->len ) );
4422
4423 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004424 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004425
4426 TEST_EQUAL( status, expected_status );
4427
4428 if( expected_status == PSA_SUCCESS )
4429 {
4430 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004431 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4432 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004433 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4434 }
4435
4436exit:
4437 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004438 mbedtls_free( output );
4439 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004440 psa_aead_abort( &operation );
4441 PSA_DONE( );
4442}
4443/* END_CASE */
4444
Paul Elliott91b021e2021-07-23 18:52:31 +01004445/* BEGIN_CASE */
4446void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4447 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004448 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004449 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004450 data_t *nonce,
4451 data_t *additional_data,
4452 data_t *input_data,
4453 int expected_status_arg )
4454{
4455
4456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4457 psa_key_type_t key_type = key_type_arg;
4458 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004459 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4461 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4462 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004463 unsigned char *ciphertext = NULL;
4464 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004465 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004466 size_t ciphertext_size = 0;
4467 size_t ciphertext_length = 0;
4468 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004469 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004470 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004471
4472 PSA_ASSERT( psa_crypto_init( ) );
4473
4474 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4475 psa_set_key_algorithm( &attributes, alg );
4476 psa_set_key_type( &attributes, key_type );
4477
4478 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4479 &key ) );
4480
4481 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4482
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004483 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004484
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004485 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004486
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004487 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004488
Paul Elliott719c1322021-09-13 18:27:22 +01004489 ASSERT_ALLOC( tag_buffer, tag_size );
4490
Paul Elliott91b021e2021-07-23 18:52:31 +01004491 status = psa_aead_encrypt_setup( &operation, key, alg );
4492
4493 /* If the operation is not supported, just skip and not fail in case the
4494 * encryption involves a common limitation of cryptography hardwares and
4495 * an alternative implementation. */
4496 if( status == PSA_ERROR_NOT_SUPPORTED )
4497 {
4498 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4499 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4500 }
4501
4502 PSA_ASSERT( status );
4503
4504 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4505
Paul Elliott76bda482021-10-07 17:07:23 +01004506 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4507 input_data->len ) );
4508
Paul Elliott91b021e2021-07-23 18:52:31 +01004509 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4510 additional_data->len ) );
4511
4512 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004513 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004514
4515 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004516 status = psa_aead_finish( &operation, finish_ciphertext,
4517 finish_ciphertext_size,
4518 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004519 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004520
4521 TEST_EQUAL( status, expected_status );
4522
4523exit:
4524 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004525 mbedtls_free( ciphertext );
4526 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004527 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004528 psa_aead_abort( &operation );
4529 PSA_DONE( );
4530}
4531/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004532
4533/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004534void aead_multipart_verify( int key_type_arg, data_t *key_data,
4535 int alg_arg,
4536 data_t *nonce,
4537 data_t *additional_data,
4538 data_t *input_data,
4539 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004540 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004541 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004542 int expected_status_arg )
4543{
4544 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4545 psa_key_type_t key_type = key_type_arg;
4546 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004547 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004548 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4549 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4550 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004551 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004552 unsigned char *plaintext = NULL;
4553 unsigned char *finish_plaintext = NULL;
4554 size_t plaintext_size = 0;
4555 size_t plaintext_length = 0;
4556 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004557 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004558 unsigned char *tag_buffer = NULL;
4559 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004560
4561 PSA_ASSERT( psa_crypto_init( ) );
4562
4563 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4564 psa_set_key_algorithm( &attributes, alg );
4565 psa_set_key_type( &attributes, key_type );
4566
4567 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4568 &key ) );
4569
4570 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4571
4572 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4573 input_data->len );
4574
4575 ASSERT_ALLOC( plaintext, plaintext_size );
4576
4577 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4578
4579 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4580
Paul Elliott9961a662021-09-17 19:19:02 +01004581 status = psa_aead_decrypt_setup( &operation, key, alg );
4582
4583 /* If the operation is not supported, just skip and not fail in case the
4584 * encryption involves a common limitation of cryptography hardwares and
4585 * an alternative implementation. */
4586 if( status == PSA_ERROR_NOT_SUPPORTED )
4587 {
4588 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4589 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4590 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004591 TEST_EQUAL( status, expected_setup_status );
4592
4593 if( status != PSA_SUCCESS )
4594 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004595
4596 PSA_ASSERT( status );
4597
4598 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4599
Paul Elliottfec6f372021-10-06 17:15:02 +01004600 status = psa_aead_set_lengths( &operation, additional_data->len,
4601 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004602 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004603
Paul Elliott9961a662021-09-17 19:19:02 +01004604 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4605 additional_data->len ) );
4606
4607 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4608 input_data->len,
4609 plaintext, plaintext_size,
4610 &plaintext_length ) );
4611
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004612 if( tag_usage == USE_GIVEN_TAG )
4613 {
4614 tag_buffer = tag->x;
4615 tag_size = tag->len;
4616 }
4617
Paul Elliott9961a662021-09-17 19:19:02 +01004618 status = psa_aead_verify( &operation, finish_plaintext,
4619 verify_plaintext_size,
4620 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004621 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004622
4623 TEST_EQUAL( status, expected_status );
4624
4625exit:
4626 psa_destroy_key( key );
4627 mbedtls_free( plaintext );
4628 mbedtls_free( finish_plaintext );
4629 psa_aead_abort( &operation );
4630 PSA_DONE( );
4631}
4632/* END_CASE */
4633
Paul Elliott9961a662021-09-17 19:19:02 +01004634/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004635void aead_multipart_setup( int key_type_arg, data_t *key_data,
4636 int alg_arg, int expected_status_arg )
4637{
4638 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4639 psa_key_type_t key_type = key_type_arg;
4640 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004641 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4643 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4644 psa_status_t expected_status = expected_status_arg;
4645
4646 PSA_ASSERT( psa_crypto_init( ) );
4647
4648 psa_set_key_usage_flags( &attributes,
4649 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4650 psa_set_key_algorithm( &attributes, alg );
4651 psa_set_key_type( &attributes, key_type );
4652
4653 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4654 &key ) );
4655
Paul Elliott5221ef62021-09-19 17:33:03 +01004656 status = psa_aead_encrypt_setup( &operation, key, alg );
4657
4658 TEST_EQUAL( status, expected_status );
4659
4660 psa_aead_abort( &operation );
4661
Paul Elliott5221ef62021-09-19 17:33:03 +01004662 status = psa_aead_decrypt_setup( &operation, key, alg );
4663
4664 TEST_EQUAL(status, expected_status );
4665
4666exit:
4667 psa_destroy_key( key );
4668 psa_aead_abort( &operation );
4669 PSA_DONE( );
4670}
4671/* END_CASE */
4672
4673/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004674void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4675 int alg_arg,
4676 data_t *nonce,
4677 data_t *additional_data,
4678 data_t *input_data )
4679{
4680 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4681 psa_key_type_t key_type = key_type_arg;
4682 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004683 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004684 unsigned char *output_data = NULL;
4685 unsigned char *final_data = NULL;
4686 size_t output_size = 0;
4687 size_t finish_output_size = 0;
4688 size_t output_length = 0;
4689 size_t key_bits = 0;
4690 size_t tag_length = 0;
4691 size_t tag_size = 0;
4692 size_t nonce_length = 0;
4693 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4694 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4695 size_t output_part_length = 0;
4696 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4697
4698 PSA_ASSERT( psa_crypto_init( ) );
4699
4700 psa_set_key_usage_flags( & attributes,
4701 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4702 psa_set_key_algorithm( & attributes, alg );
4703 psa_set_key_type( & attributes, key_type );
4704
4705 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4706 &key ) );
4707
4708 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4709 key_bits = psa_get_key_bits( &attributes );
4710
4711 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4712
4713 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4714
4715 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4716
4717 ASSERT_ALLOC( output_data, output_size );
4718
4719 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4720
4721 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4722
4723 ASSERT_ALLOC( final_data, finish_output_size );
4724
4725 /* Test all operations error without calling setup first. */
4726
Paul Elliottc23a9a02021-06-21 18:32:46 +01004727 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4728 PSA_ERROR_BAD_STATE );
4729
4730 psa_aead_abort( &operation );
4731
Paul Elliottc23a9a02021-06-21 18:32:46 +01004732 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4733 PSA_AEAD_NONCE_MAX_SIZE,
4734 &nonce_length ),
4735 PSA_ERROR_BAD_STATE );
4736
4737 psa_aead_abort( &operation );
4738
Paul Elliott481be342021-07-16 17:38:47 +01004739 /* ------------------------------------------------------- */
4740
Paul Elliottc23a9a02021-06-21 18:32:46 +01004741 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4742 input_data->len ),
4743 PSA_ERROR_BAD_STATE );
4744
4745 psa_aead_abort( &operation );
4746
Paul Elliott481be342021-07-16 17:38:47 +01004747 /* ------------------------------------------------------- */
4748
Paul Elliottc23a9a02021-06-21 18:32:46 +01004749 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4750 additional_data->len ),
4751 PSA_ERROR_BAD_STATE );
4752
4753 psa_aead_abort( &operation );
4754
Paul Elliott481be342021-07-16 17:38:47 +01004755 /* ------------------------------------------------------- */
4756
Paul Elliottc23a9a02021-06-21 18:32:46 +01004757 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4758 input_data->len, output_data,
4759 output_size, &output_length ),
4760 PSA_ERROR_BAD_STATE );
4761
4762 psa_aead_abort( &operation );
4763
Paul Elliott481be342021-07-16 17:38:47 +01004764 /* ------------------------------------------------------- */
4765
Paul Elliottc23a9a02021-06-21 18:32:46 +01004766 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4767 finish_output_size,
4768 &output_part_length,
4769 tag_buffer, tag_length,
4770 &tag_size ),
4771 PSA_ERROR_BAD_STATE );
4772
4773 psa_aead_abort( &operation );
4774
Paul Elliott481be342021-07-16 17:38:47 +01004775 /* ------------------------------------------------------- */
4776
Paul Elliottc23a9a02021-06-21 18:32:46 +01004777 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4778 finish_output_size,
4779 &output_part_length,
4780 tag_buffer,
4781 tag_length ),
4782 PSA_ERROR_BAD_STATE );
4783
4784 psa_aead_abort( &operation );
4785
4786 /* Test for double setups. */
4787
Paul Elliottc23a9a02021-06-21 18:32:46 +01004788 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4789
4790 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4791 PSA_ERROR_BAD_STATE );
4792
4793 psa_aead_abort( &operation );
4794
Paul Elliott481be342021-07-16 17:38:47 +01004795 /* ------------------------------------------------------- */
4796
Paul Elliottc23a9a02021-06-21 18:32:46 +01004797 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4798
4799 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4800 PSA_ERROR_BAD_STATE );
4801
4802 psa_aead_abort( &operation );
4803
Paul Elliott374a2be2021-07-16 17:53:40 +01004804 /* ------------------------------------------------------- */
4805
Paul Elliott374a2be2021-07-16 17:53:40 +01004806 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4807
4808 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4809 PSA_ERROR_BAD_STATE );
4810
4811 psa_aead_abort( &operation );
4812
4813 /* ------------------------------------------------------- */
4814
Paul Elliott374a2be2021-07-16 17:53:40 +01004815 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4816
4817 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4818 PSA_ERROR_BAD_STATE );
4819
4820 psa_aead_abort( &operation );
4821
Paul Elliottc23a9a02021-06-21 18:32:46 +01004822 /* Test for not setting a nonce. */
4823
Paul Elliottc23a9a02021-06-21 18:32:46 +01004824 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4825
4826 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4827 additional_data->len ),
4828 PSA_ERROR_BAD_STATE );
4829
4830 psa_aead_abort( &operation );
4831
Paul Elliott7f628422021-09-01 12:08:29 +01004832 /* ------------------------------------------------------- */
4833
Paul Elliott7f628422021-09-01 12:08:29 +01004834 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4835
4836 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4837 input_data->len, output_data,
4838 output_size, &output_length ),
4839 PSA_ERROR_BAD_STATE );
4840
4841 psa_aead_abort( &operation );
4842
Paul Elliottbdc2c682021-09-21 18:37:10 +01004843 /* ------------------------------------------------------- */
4844
Paul Elliottbdc2c682021-09-21 18:37:10 +01004845 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4846
4847 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4848 finish_output_size,
4849 &output_part_length,
4850 tag_buffer, tag_length,
4851 &tag_size ),
4852 PSA_ERROR_BAD_STATE );
4853
4854 psa_aead_abort( &operation );
4855
4856 /* ------------------------------------------------------- */
4857
Paul Elliottbdc2c682021-09-21 18:37:10 +01004858 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4859
4860 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4861 finish_output_size,
4862 &output_part_length,
4863 tag_buffer,
4864 tag_length ),
4865 PSA_ERROR_BAD_STATE );
4866
4867 psa_aead_abort( &operation );
4868
Paul Elliottc23a9a02021-06-21 18:32:46 +01004869 /* Test for double setting nonce. */
4870
Paul Elliottc23a9a02021-06-21 18:32:46 +01004871 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4872
4873 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4874
4875 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4876 PSA_ERROR_BAD_STATE );
4877
4878 psa_aead_abort( &operation );
4879
Paul Elliott374a2be2021-07-16 17:53:40 +01004880 /* Test for double generating nonce. */
4881
Paul Elliott374a2be2021-07-16 17:53:40 +01004882 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4883
4884 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4885 PSA_AEAD_NONCE_MAX_SIZE,
4886 &nonce_length ) );
4887
4888 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4889 PSA_AEAD_NONCE_MAX_SIZE,
4890 &nonce_length ),
4891 PSA_ERROR_BAD_STATE );
4892
4893
4894 psa_aead_abort( &operation );
4895
4896 /* Test for generate nonce then set and vice versa */
4897
Paul Elliott374a2be2021-07-16 17:53:40 +01004898 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4899
4900 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4901 PSA_AEAD_NONCE_MAX_SIZE,
4902 &nonce_length ) );
4903
4904 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4905 PSA_ERROR_BAD_STATE );
4906
4907 psa_aead_abort( &operation );
4908
Andrzej Kurekad837522021-12-15 15:28:49 +01004909 /* Test for generating nonce after calling set lengths */
4910
4911 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4912
4913 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4914 input_data->len ) );
4915
4916 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4917 PSA_AEAD_NONCE_MAX_SIZE,
4918 &nonce_length ) );
4919
4920 psa_aead_abort( &operation );
4921
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004922 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004923
4924 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4925
4926 if( operation.alg == PSA_ALG_CCM )
4927 {
4928 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4929 input_data->len ),
4930 PSA_ERROR_INVALID_ARGUMENT );
4931 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4932 PSA_AEAD_NONCE_MAX_SIZE,
4933 &nonce_length ),
4934 PSA_ERROR_BAD_STATE );
4935 }
4936 else
4937 {
4938 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4939 input_data->len ) );
4940 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4941 PSA_AEAD_NONCE_MAX_SIZE,
4942 &nonce_length ) );
4943 }
4944
4945 psa_aead_abort( &operation );
4946
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004947 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004948#if SIZE_MAX > UINT32_MAX
4949 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4950
4951 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
4952 {
4953 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
4954 input_data->len ),
4955 PSA_ERROR_INVALID_ARGUMENT );
4956 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4957 PSA_AEAD_NONCE_MAX_SIZE,
4958 &nonce_length ),
4959 PSA_ERROR_BAD_STATE );
4960 }
4961 else
4962 {
4963 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
4964 input_data->len ) );
4965 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4966 PSA_AEAD_NONCE_MAX_SIZE,
4967 &nonce_length ) );
4968 }
4969
4970 psa_aead_abort( &operation );
4971#endif
4972
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004973 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01004974
4975 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4976
4977 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4978 PSA_AEAD_NONCE_MAX_SIZE,
4979 &nonce_length ) );
4980
4981 if( operation.alg == PSA_ALG_CCM )
4982 {
4983 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4984 input_data->len ),
4985 PSA_ERROR_INVALID_ARGUMENT );
4986 }
4987 else
4988 {
4989 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4990 input_data->len ) );
4991 }
4992
4993 psa_aead_abort( &operation );
4994
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004995 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004996 /* Test for setting nonce after calling set lengths */
4997
4998 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4999
5000 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5001 input_data->len ) );
5002
5003 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5004
5005 psa_aead_abort( &operation );
5006
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005007 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005008
5009 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5010
5011 if( operation.alg == PSA_ALG_CCM )
5012 {
5013 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5014 input_data->len ),
5015 PSA_ERROR_INVALID_ARGUMENT );
5016 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5017 PSA_ERROR_BAD_STATE );
5018 }
5019 else
5020 {
5021 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5022 input_data->len ) );
5023 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5024 }
5025
5026 psa_aead_abort( &operation );
5027
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005028 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005029#if SIZE_MAX > UINT32_MAX
5030 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5031
5032 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5033 {
5034 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5035 input_data->len ),
5036 PSA_ERROR_INVALID_ARGUMENT );
5037 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5038 PSA_ERROR_BAD_STATE );
5039 }
5040 else
5041 {
5042 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5043 input_data->len ) );
5044 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5045 }
5046
5047 psa_aead_abort( &operation );
5048#endif
5049
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005050 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005051
5052 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5053
5054 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5055
5056 if( operation.alg == PSA_ALG_CCM )
5057 {
5058 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5059 input_data->len ),
5060 PSA_ERROR_INVALID_ARGUMENT );
5061 }
5062 else
5063 {
5064 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5065 input_data->len ) );
5066 }
5067
5068 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005069
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005070 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005071#if SIZE_MAX > UINT32_MAX
5072 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5073
5074 if( operation.alg == PSA_ALG_GCM )
5075 {
5076 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5077 SIZE_MAX ),
5078 PSA_ERROR_INVALID_ARGUMENT );
5079 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5080 PSA_ERROR_BAD_STATE );
5081 }
5082 else if ( operation.alg != PSA_ALG_CCM )
5083 {
5084 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5085 SIZE_MAX ) );
5086 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5087 }
5088
5089 psa_aead_abort( &operation );
5090
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005091 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005092 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5093
5094 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5095
5096 if( operation.alg == PSA_ALG_GCM )
5097 {
5098 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5099 SIZE_MAX ),
5100 PSA_ERROR_INVALID_ARGUMENT );
5101 }
5102 else if ( operation.alg != PSA_ALG_CCM )
5103 {
5104 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5105 SIZE_MAX ) );
5106 }
5107
5108 psa_aead_abort( &operation );
5109#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005110
5111 /* ------------------------------------------------------- */
5112
Paul Elliott374a2be2021-07-16 17:53:40 +01005113 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5114
5115 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5116
5117 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5118 PSA_AEAD_NONCE_MAX_SIZE,
5119 &nonce_length ),
5120 PSA_ERROR_BAD_STATE );
5121
5122 psa_aead_abort( &operation );
5123
Paul Elliott7220cae2021-06-22 17:25:57 +01005124 /* Test for generating nonce in decrypt setup. */
5125
Paul Elliott7220cae2021-06-22 17:25:57 +01005126 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5127
5128 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5129 PSA_AEAD_NONCE_MAX_SIZE,
5130 &nonce_length ),
5131 PSA_ERROR_BAD_STATE );
5132
5133 psa_aead_abort( &operation );
5134
Paul Elliottc23a9a02021-06-21 18:32:46 +01005135 /* Test for setting lengths twice. */
5136
Paul Elliottc23a9a02021-06-21 18:32:46 +01005137 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5138
5139 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5140
5141 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5142 input_data->len ) );
5143
5144 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5145 input_data->len ),
5146 PSA_ERROR_BAD_STATE );
5147
5148 psa_aead_abort( &operation );
5149
Andrzej Kurekad837522021-12-15 15:28:49 +01005150 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005151
Paul Elliottc23a9a02021-06-21 18:32:46 +01005152 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5153
5154 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5155
Andrzej Kurekad837522021-12-15 15:28:49 +01005156 if( operation.alg == PSA_ALG_CCM )
5157 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005158
Andrzej Kurekad837522021-12-15 15:28:49 +01005159 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5160 additional_data->len ),
5161 PSA_ERROR_BAD_STATE );
5162 }
5163 else
5164 {
5165 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5166 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005167
Andrzej Kurekad837522021-12-15 15:28:49 +01005168 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5169 input_data->len ),
5170 PSA_ERROR_BAD_STATE );
5171 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005172 psa_aead_abort( &operation );
5173
5174 /* ------------------------------------------------------- */
5175
Paul Elliottf94bd992021-09-19 18:15:59 +01005176 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5177
5178 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5179
Andrzej Kurekad837522021-12-15 15:28:49 +01005180 if( operation.alg == PSA_ALG_CCM )
5181 {
5182 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5183 input_data->len, output_data,
5184 output_size, &output_length ),
5185 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005186
Andrzej Kurekad837522021-12-15 15:28:49 +01005187 }
5188 else
5189 {
5190 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5191 input_data->len, output_data,
5192 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005193
Andrzej Kurekad837522021-12-15 15:28:49 +01005194 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5195 input_data->len ),
5196 PSA_ERROR_BAD_STATE );
5197 }
5198 psa_aead_abort( &operation );
5199
5200 /* ------------------------------------------------------- */
5201
5202 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5203
5204 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5205
5206 if( operation.alg == PSA_ALG_CCM )
5207 {
5208 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5209 finish_output_size,
5210 &output_part_length,
5211 tag_buffer, tag_length,
5212 &tag_size ) );
5213 }
5214 else
5215 {
5216 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5217 finish_output_size,
5218 &output_part_length,
5219 tag_buffer, tag_length,
5220 &tag_size ) );
5221
5222 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5223 input_data->len ),
5224 PSA_ERROR_BAD_STATE );
5225 }
5226 psa_aead_abort( &operation );
5227
5228 /* Test for setting lengths after generating nonce + already starting data. */
5229
5230 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5231
5232 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5233 PSA_AEAD_NONCE_MAX_SIZE,
5234 &nonce_length ) );
5235 if( operation.alg == PSA_ALG_CCM )
5236 {
5237
5238 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5239 additional_data->len ),
5240 PSA_ERROR_BAD_STATE );
5241 }
5242 else
5243 {
5244 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5245 additional_data->len ) );
5246
5247 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5248 input_data->len ),
5249 PSA_ERROR_BAD_STATE );
5250 }
5251 psa_aead_abort( &operation );
5252
5253 /* ------------------------------------------------------- */
5254
5255 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5256
5257 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5258 PSA_AEAD_NONCE_MAX_SIZE,
5259 &nonce_length ) );
5260 if( operation.alg == PSA_ALG_CCM )
5261 {
5262 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5263 input_data->len, output_data,
5264 output_size, &output_length ),
5265 PSA_ERROR_BAD_STATE );
5266
5267 }
5268 else
5269 {
5270 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5271 input_data->len, output_data,
5272 output_size, &output_length ) );
5273
5274 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5275 input_data->len ),
5276 PSA_ERROR_BAD_STATE );
5277 }
5278 psa_aead_abort( &operation );
5279
5280 /* ------------------------------------------------------- */
5281
5282 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5283
5284 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5285 PSA_AEAD_NONCE_MAX_SIZE,
5286 &nonce_length ) );
5287 if( operation.alg == PSA_ALG_CCM )
5288 {
5289 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5290 finish_output_size,
5291 &output_part_length,
5292 tag_buffer, tag_length,
5293 &tag_size ) );
5294 }
5295 else
5296 {
5297 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5298 finish_output_size,
5299 &output_part_length,
5300 tag_buffer, tag_length,
5301 &tag_size ) );
5302
5303 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5304 input_data->len ),
5305 PSA_ERROR_BAD_STATE );
5306 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005307 psa_aead_abort( &operation );
5308
Paul Elliott243080c2021-07-21 19:01:17 +01005309 /* Test for not sending any additional data or data after setting non zero
5310 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005311
Paul Elliottc23a9a02021-06-21 18:32:46 +01005312 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5313
5314 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5315
5316 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5317 input_data->len ) );
5318
5319 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5320 finish_output_size,
5321 &output_part_length,
5322 tag_buffer, tag_length,
5323 &tag_size ),
5324 PSA_ERROR_INVALID_ARGUMENT );
5325
5326 psa_aead_abort( &operation );
5327
Paul Elliott243080c2021-07-21 19:01:17 +01005328 /* Test for not sending any additional data or data after setting non-zero
5329 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005330
Paul Elliottc23a9a02021-06-21 18:32:46 +01005331 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5332
5333 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5334
5335 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5336 input_data->len ) );
5337
5338 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5339 finish_output_size,
5340 &output_part_length,
5341 tag_buffer,
5342 tag_length ),
5343 PSA_ERROR_INVALID_ARGUMENT );
5344
5345 psa_aead_abort( &operation );
5346
Paul Elliott243080c2021-07-21 19:01:17 +01005347 /* Test for not sending any additional data after setting a non-zero length
5348 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005349
Paul Elliottc23a9a02021-06-21 18:32:46 +01005350 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5351
5352 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5353
5354 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5355 input_data->len ) );
5356
5357 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5358 input_data->len, output_data,
5359 output_size, &output_length ),
5360 PSA_ERROR_INVALID_ARGUMENT );
5361
5362 psa_aead_abort( &operation );
5363
Paul Elliottf94bd992021-09-19 18:15:59 +01005364 /* Test for not sending any data after setting a non-zero length for it.*/
5365
Paul Elliottf94bd992021-09-19 18:15:59 +01005366 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5367
5368 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5369
5370 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5371 input_data->len ) );
5372
5373 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5374 additional_data->len ) );
5375
5376 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5377 finish_output_size,
5378 &output_part_length,
5379 tag_buffer, tag_length,
5380 &tag_size ),
5381 PSA_ERROR_INVALID_ARGUMENT );
5382
5383 psa_aead_abort( &operation );
5384
Paul Elliottb0450fe2021-09-01 15:06:26 +01005385 /* Test for sending too much additional data after setting lengths. */
5386
Paul Elliottb0450fe2021-09-01 15:06:26 +01005387 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5388
5389 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5390
5391 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5392
5393
5394 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5395 additional_data->len ),
5396 PSA_ERROR_INVALID_ARGUMENT );
5397
5398 psa_aead_abort( &operation );
5399
Paul Elliotta2a09b02021-09-22 14:56:40 +01005400 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005401
5402 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5403
5404 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5405
5406 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5407 input_data->len ) );
5408
5409 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5410 additional_data->len ) );
5411
5412 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5413 1 ),
5414 PSA_ERROR_INVALID_ARGUMENT );
5415
5416 psa_aead_abort( &operation );
5417
Paul Elliottb0450fe2021-09-01 15:06:26 +01005418 /* Test for sending too much data after setting lengths. */
5419
Paul Elliottb0450fe2021-09-01 15:06:26 +01005420 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5421
5422 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5423
5424 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5425
5426 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5427 input_data->len, output_data,
5428 output_size, &output_length ),
5429 PSA_ERROR_INVALID_ARGUMENT );
5430
5431 psa_aead_abort( &operation );
5432
Paul Elliotta2a09b02021-09-22 14:56:40 +01005433 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005434
5435 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5436
5437 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5438
5439 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5440 input_data->len ) );
5441
5442 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5443 additional_data->len ) );
5444
5445 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5446 input_data->len, output_data,
5447 output_size, &output_length ) );
5448
5449 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5450 1, output_data,
5451 output_size, &output_length ),
5452 PSA_ERROR_INVALID_ARGUMENT );
5453
5454 psa_aead_abort( &operation );
5455
Paul Elliottc23a9a02021-06-21 18:32:46 +01005456 /* Test sending additional data after data. */
5457
Paul Elliottc23a9a02021-06-21 18:32:46 +01005458 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5459
5460 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5461
Andrzej Kurekad837522021-12-15 15:28:49 +01005462 if( operation.alg != PSA_ALG_CCM )
5463 {
5464 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5465 input_data->len, output_data,
5466 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005467
Andrzej Kurekad837522021-12-15 15:28:49 +01005468 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5469 additional_data->len ),
5470 PSA_ERROR_BAD_STATE );
5471 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005472 psa_aead_abort( &operation );
5473
Paul Elliott534d0b42021-06-22 19:15:20 +01005474 /* Test calling finish on decryption. */
5475
Paul Elliott534d0b42021-06-22 19:15:20 +01005476 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5477
5478 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5479
5480 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5481 finish_output_size,
5482 &output_part_length,
5483 tag_buffer, tag_length,
5484 &tag_size ),
5485 PSA_ERROR_BAD_STATE );
5486
5487 psa_aead_abort( &operation );
5488
5489 /* Test calling verify on encryption. */
5490
Paul Elliott534d0b42021-06-22 19:15:20 +01005491 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5492
5493 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5494
5495 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5496 finish_output_size,
5497 &output_part_length,
5498 tag_buffer,
5499 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005500 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005501
5502 psa_aead_abort( &operation );
5503
5504
Paul Elliottc23a9a02021-06-21 18:32:46 +01005505exit:
5506 psa_destroy_key( key );
5507 psa_aead_abort( &operation );
5508 mbedtls_free( output_data );
5509 mbedtls_free( final_data );
5510 PSA_DONE( );
5511}
5512/* END_CASE */
5513
5514/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005515void signature_size( int type_arg,
5516 int bits,
5517 int alg_arg,
5518 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005519{
5520 psa_key_type_t type = type_arg;
5521 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005522 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005523
Gilles Peskinefe11b722018-12-18 00:24:04 +01005524 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005525
Gilles Peskinee59236f2018-01-27 23:32:46 +01005526exit:
5527 ;
5528}
5529/* END_CASE */
5530
5531/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005532void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5533 int alg_arg, data_t *input_data,
5534 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005535{
Ronald Cron5425a212020-08-04 14:58:35 +02005536 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005537 psa_key_type_t key_type = key_type_arg;
5538 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005539 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005540 unsigned char *signature = NULL;
5541 size_t signature_size;
5542 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005543 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005544
Gilles Peskine8817f612018-12-18 00:18:46 +01005545 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005546
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005547 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005548 psa_set_key_algorithm( &attributes, alg );
5549 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005550
Gilles Peskine049c7532019-05-15 20:22:09 +02005551 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005552 &key ) );
5553 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005554 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005555
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005556 /* Allocate a buffer which has the size advertized by the
5557 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005558 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005559 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005560 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005561 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005562 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005563
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005564 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005565 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005566 input_data->x, input_data->len,
5567 signature, signature_size,
5568 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005569 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005570 ASSERT_COMPARE( output_data->x, output_data->len,
5571 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005572
5573exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005574 /*
5575 * Key attributes may have been returned by psa_get_key_attributes()
5576 * thus reset them as required.
5577 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005578 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005579
Ronald Cron5425a212020-08-04 14:58:35 +02005580 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005581 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005582 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005583}
5584/* END_CASE */
5585
5586/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005587void sign_hash_fail( int key_type_arg, data_t *key_data,
5588 int alg_arg, data_t *input_data,
5589 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005590{
Ronald Cron5425a212020-08-04 14:58:35 +02005591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005592 psa_key_type_t key_type = key_type_arg;
5593 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005594 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005595 psa_status_t actual_status;
5596 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005597 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005598 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005600
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005601 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005602
Gilles Peskine8817f612018-12-18 00:18:46 +01005603 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005604
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005605 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005606 psa_set_key_algorithm( &attributes, alg );
5607 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005608
Gilles Peskine049c7532019-05-15 20:22:09 +02005609 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005610 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005611
Ronald Cron5425a212020-08-04 14:58:35 +02005612 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005613 input_data->x, input_data->len,
5614 signature, signature_size,
5615 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005616 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005617 /* The value of *signature_length is unspecified on error, but
5618 * whatever it is, it should be less than signature_size, so that
5619 * if the caller tries to read *signature_length bytes without
5620 * checking the error code then they don't overflow a buffer. */
5621 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005622
5623exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005624 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005625 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005626 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005627 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005628}
5629/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005630
5631/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005632void sign_verify_hash( int key_type_arg, data_t *key_data,
5633 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005634{
Ronald Cron5425a212020-08-04 14:58:35 +02005635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005636 psa_key_type_t key_type = key_type_arg;
5637 psa_algorithm_t alg = alg_arg;
5638 size_t key_bits;
5639 unsigned char *signature = NULL;
5640 size_t signature_size;
5641 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005643
Gilles Peskine8817f612018-12-18 00:18:46 +01005644 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005645
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005646 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005647 psa_set_key_algorithm( &attributes, alg );
5648 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005649
Gilles Peskine049c7532019-05-15 20:22:09 +02005650 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005651 &key ) );
5652 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005653 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005654
5655 /* Allocate a buffer which has the size advertized by the
5656 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005657 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005658 key_bits, alg );
5659 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005660 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005661 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005662
5663 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005664 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005665 input_data->x, input_data->len,
5666 signature, signature_size,
5667 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005668 /* Check that the signature length looks sensible. */
5669 TEST_ASSERT( signature_length <= signature_size );
5670 TEST_ASSERT( signature_length > 0 );
5671
5672 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005673 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005674 input_data->x, input_data->len,
5675 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005676
5677 if( input_data->len != 0 )
5678 {
5679 /* Flip a bit in the input and verify that the signature is now
5680 * detected as invalid. Flip a bit at the beginning, not at the end,
5681 * because ECDSA may ignore the last few bits of the input. */
5682 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005683 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005684 input_data->x, input_data->len,
5685 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005686 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005687 }
5688
5689exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005690 /*
5691 * Key attributes may have been returned by psa_get_key_attributes()
5692 * thus reset them as required.
5693 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005694 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005695
Ronald Cron5425a212020-08-04 14:58:35 +02005696 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005697 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005698 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005699}
5700/* END_CASE */
5701
5702/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005703void verify_hash( int key_type_arg, data_t *key_data,
5704 int alg_arg, data_t *hash_data,
5705 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005706{
Ronald Cron5425a212020-08-04 14:58:35 +02005707 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005708 psa_key_type_t key_type = key_type_arg;
5709 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005710 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005711
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005712 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005713
Gilles Peskine8817f612018-12-18 00:18:46 +01005714 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005715
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005716 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005717 psa_set_key_algorithm( &attributes, alg );
5718 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005719
Gilles Peskine049c7532019-05-15 20:22:09 +02005720 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005721 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005722
Ronald Cron5425a212020-08-04 14:58:35 +02005723 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005724 hash_data->x, hash_data->len,
5725 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005726
itayzafrir5c753392018-05-08 11:18:38 +03005727exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005728 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005729 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005730 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005731}
5732/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005733
5734/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005735void verify_hash_fail( int key_type_arg, data_t *key_data,
5736 int alg_arg, data_t *hash_data,
5737 data_t *signature_data,
5738 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005739{
Ronald Cron5425a212020-08-04 14:58:35 +02005740 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005741 psa_key_type_t key_type = key_type_arg;
5742 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005743 psa_status_t actual_status;
5744 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005746
Gilles Peskine8817f612018-12-18 00:18:46 +01005747 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005748
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005749 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005750 psa_set_key_algorithm( &attributes, alg );
5751 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005752
Gilles Peskine049c7532019-05-15 20:22:09 +02005753 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005754 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005755
Ronald Cron5425a212020-08-04 14:58:35 +02005756 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005757 hash_data->x, hash_data->len,
5758 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005759 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005760
5761exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005762 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005763 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005764 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005765}
5766/* END_CASE */
5767
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005768/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005769void sign_message_deterministic( int key_type_arg,
5770 data_t *key_data,
5771 int alg_arg,
5772 data_t *input_data,
5773 data_t *output_data )
5774{
5775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5776 psa_key_type_t key_type = key_type_arg;
5777 psa_algorithm_t alg = alg_arg;
5778 size_t key_bits;
5779 unsigned char *signature = NULL;
5780 size_t signature_size;
5781 size_t signature_length = 0xdeadbeef;
5782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5783
5784 PSA_ASSERT( psa_crypto_init( ) );
5785
5786 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5787 psa_set_key_algorithm( &attributes, alg );
5788 psa_set_key_type( &attributes, key_type );
5789
5790 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5791 &key ) );
5792 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5793 key_bits = psa_get_key_bits( &attributes );
5794
5795 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5796 TEST_ASSERT( signature_size != 0 );
5797 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5798 ASSERT_ALLOC( signature, signature_size );
5799
5800 PSA_ASSERT( psa_sign_message( key, alg,
5801 input_data->x, input_data->len,
5802 signature, signature_size,
5803 &signature_length ) );
5804
5805 ASSERT_COMPARE( output_data->x, output_data->len,
5806 signature, signature_length );
5807
5808exit:
5809 psa_reset_key_attributes( &attributes );
5810
5811 psa_destroy_key( key );
5812 mbedtls_free( signature );
5813 PSA_DONE( );
5814
5815}
5816/* END_CASE */
5817
5818/* BEGIN_CASE */
5819void sign_message_fail( int key_type_arg,
5820 data_t *key_data,
5821 int alg_arg,
5822 data_t *input_data,
5823 int signature_size_arg,
5824 int expected_status_arg )
5825{
5826 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5827 psa_key_type_t key_type = key_type_arg;
5828 psa_algorithm_t alg = alg_arg;
5829 size_t signature_size = signature_size_arg;
5830 psa_status_t actual_status;
5831 psa_status_t expected_status = expected_status_arg;
5832 unsigned char *signature = NULL;
5833 size_t signature_length = 0xdeadbeef;
5834 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5835
5836 ASSERT_ALLOC( signature, signature_size );
5837
5838 PSA_ASSERT( psa_crypto_init( ) );
5839
5840 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5841 psa_set_key_algorithm( &attributes, alg );
5842 psa_set_key_type( &attributes, key_type );
5843
5844 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5845 &key ) );
5846
5847 actual_status = psa_sign_message( key, alg,
5848 input_data->x, input_data->len,
5849 signature, signature_size,
5850 &signature_length );
5851 TEST_EQUAL( actual_status, expected_status );
5852 /* The value of *signature_length is unspecified on error, but
5853 * whatever it is, it should be less than signature_size, so that
5854 * if the caller tries to read *signature_length bytes without
5855 * checking the error code then they don't overflow a buffer. */
5856 TEST_ASSERT( signature_length <= signature_size );
5857
5858exit:
5859 psa_reset_key_attributes( &attributes );
5860 psa_destroy_key( key );
5861 mbedtls_free( signature );
5862 PSA_DONE( );
5863}
5864/* END_CASE */
5865
5866/* BEGIN_CASE */
5867void sign_verify_message( int key_type_arg,
5868 data_t *key_data,
5869 int alg_arg,
5870 data_t *input_data )
5871{
5872 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5873 psa_key_type_t key_type = key_type_arg;
5874 psa_algorithm_t alg = alg_arg;
5875 size_t key_bits;
5876 unsigned char *signature = NULL;
5877 size_t signature_size;
5878 size_t signature_length = 0xdeadbeef;
5879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5880
5881 PSA_ASSERT( psa_crypto_init( ) );
5882
5883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5884 PSA_KEY_USAGE_VERIFY_MESSAGE );
5885 psa_set_key_algorithm( &attributes, alg );
5886 psa_set_key_type( &attributes, key_type );
5887
5888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5889 &key ) );
5890 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5891 key_bits = psa_get_key_bits( &attributes );
5892
5893 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5894 TEST_ASSERT( signature_size != 0 );
5895 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5896 ASSERT_ALLOC( signature, signature_size );
5897
5898 PSA_ASSERT( psa_sign_message( key, alg,
5899 input_data->x, input_data->len,
5900 signature, signature_size,
5901 &signature_length ) );
5902 TEST_ASSERT( signature_length <= signature_size );
5903 TEST_ASSERT( signature_length > 0 );
5904
5905 PSA_ASSERT( psa_verify_message( key, alg,
5906 input_data->x, input_data->len,
5907 signature, signature_length ) );
5908
5909 if( input_data->len != 0 )
5910 {
5911 /* Flip a bit in the input and verify that the signature is now
5912 * detected as invalid. Flip a bit at the beginning, not at the end,
5913 * because ECDSA may ignore the last few bits of the input. */
5914 input_data->x[0] ^= 1;
5915 TEST_EQUAL( psa_verify_message( key, alg,
5916 input_data->x, input_data->len,
5917 signature, signature_length ),
5918 PSA_ERROR_INVALID_SIGNATURE );
5919 }
5920
5921exit:
5922 psa_reset_key_attributes( &attributes );
5923
5924 psa_destroy_key( key );
5925 mbedtls_free( signature );
5926 PSA_DONE( );
5927}
5928/* END_CASE */
5929
5930/* BEGIN_CASE */
5931void verify_message( int key_type_arg,
5932 data_t *key_data,
5933 int alg_arg,
5934 data_t *input_data,
5935 data_t *signature_data )
5936{
5937 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5938 psa_key_type_t key_type = key_type_arg;
5939 psa_algorithm_t alg = alg_arg;
5940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5941
5942 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5943
5944 PSA_ASSERT( psa_crypto_init( ) );
5945
5946 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5947 psa_set_key_algorithm( &attributes, alg );
5948 psa_set_key_type( &attributes, key_type );
5949
5950 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5951 &key ) );
5952
5953 PSA_ASSERT( psa_verify_message( key, alg,
5954 input_data->x, input_data->len,
5955 signature_data->x, signature_data->len ) );
5956
5957exit:
5958 psa_reset_key_attributes( &attributes );
5959 psa_destroy_key( key );
5960 PSA_DONE( );
5961}
5962/* END_CASE */
5963
5964/* BEGIN_CASE */
5965void verify_message_fail( int key_type_arg,
5966 data_t *key_data,
5967 int alg_arg,
5968 data_t *hash_data,
5969 data_t *signature_data,
5970 int expected_status_arg )
5971{
5972 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5973 psa_key_type_t key_type = key_type_arg;
5974 psa_algorithm_t alg = alg_arg;
5975 psa_status_t actual_status;
5976 psa_status_t expected_status = expected_status_arg;
5977 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5978
5979 PSA_ASSERT( psa_crypto_init( ) );
5980
5981 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5982 psa_set_key_algorithm( &attributes, alg );
5983 psa_set_key_type( &attributes, key_type );
5984
5985 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5986 &key ) );
5987
5988 actual_status = psa_verify_message( key, alg,
5989 hash_data->x, hash_data->len,
5990 signature_data->x,
5991 signature_data->len );
5992 TEST_EQUAL( actual_status, expected_status );
5993
5994exit:
5995 psa_reset_key_attributes( &attributes );
5996 psa_destroy_key( key );
5997 PSA_DONE( );
5998}
5999/* END_CASE */
6000
6001/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006002void asymmetric_encrypt( int key_type_arg,
6003 data_t *key_data,
6004 int alg_arg,
6005 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006006 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006007 int expected_output_length_arg,
6008 int expected_status_arg )
6009{
Ronald Cron5425a212020-08-04 14:58:35 +02006010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006011 psa_key_type_t key_type = key_type_arg;
6012 psa_algorithm_t alg = alg_arg;
6013 size_t expected_output_length = expected_output_length_arg;
6014 size_t key_bits;
6015 unsigned char *output = NULL;
6016 size_t output_size;
6017 size_t output_length = ~0;
6018 psa_status_t actual_status;
6019 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006021
Gilles Peskine8817f612018-12-18 00:18:46 +01006022 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006023
Gilles Peskine656896e2018-06-29 19:12:28 +02006024 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006025 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6026 psa_set_key_algorithm( &attributes, alg );
6027 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006028 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006029 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006030
6031 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006032 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006033 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006034
Gilles Peskine656896e2018-06-29 19:12:28 +02006035 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006036 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006037 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006038
6039 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006040 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006041 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006042 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006043 output, output_size,
6044 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006045 TEST_EQUAL( actual_status, expected_status );
6046 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006047
Gilles Peskine68428122018-06-30 18:42:41 +02006048 /* If the label is empty, the test framework puts a non-null pointer
6049 * in label->x. Test that a null pointer works as well. */
6050 if( label->len == 0 )
6051 {
6052 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006053 if( output_size != 0 )
6054 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006055 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006056 input_data->x, input_data->len,
6057 NULL, label->len,
6058 output, output_size,
6059 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006060 TEST_EQUAL( actual_status, expected_status );
6061 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006062 }
6063
Gilles Peskine656896e2018-06-29 19:12:28 +02006064exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006065 /*
6066 * Key attributes may have been returned by psa_get_key_attributes()
6067 * thus reset them as required.
6068 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006069 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006070
Ronald Cron5425a212020-08-04 14:58:35 +02006071 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006072 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006073 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006074}
6075/* END_CASE */
6076
6077/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006078void asymmetric_encrypt_decrypt( int key_type_arg,
6079 data_t *key_data,
6080 int alg_arg,
6081 data_t *input_data,
6082 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006083{
Ronald Cron5425a212020-08-04 14:58:35 +02006084 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006085 psa_key_type_t key_type = key_type_arg;
6086 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006087 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006088 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006089 size_t output_size;
6090 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006091 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006092 size_t output2_size;
6093 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006095
Gilles Peskine8817f612018-12-18 00:18:46 +01006096 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006097
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006098 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6099 psa_set_key_algorithm( &attributes, alg );
6100 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006101
Gilles Peskine049c7532019-05-15 20:22:09 +02006102 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006103 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006104
6105 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006106 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006107 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006108
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006109 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006110 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006111 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006112
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006113 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006114 TEST_ASSERT( output2_size <=
6115 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6116 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006117 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006118
Gilles Peskineeebd7382018-06-08 18:11:54 +02006119 /* We test encryption by checking that encrypt-then-decrypt gives back
6120 * the original plaintext because of the non-optional random
6121 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006122 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006123 input_data->x, input_data->len,
6124 label->x, label->len,
6125 output, output_size,
6126 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006127 /* We don't know what ciphertext length to expect, but check that
6128 * it looks sensible. */
6129 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006130
Ronald Cron5425a212020-08-04 14:58:35 +02006131 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006132 output, output_length,
6133 label->x, label->len,
6134 output2, output2_size,
6135 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006136 ASSERT_COMPARE( input_data->x, input_data->len,
6137 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006138
6139exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006140 /*
6141 * Key attributes may have been returned by psa_get_key_attributes()
6142 * thus reset them as required.
6143 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006144 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006145
Ronald Cron5425a212020-08-04 14:58:35 +02006146 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006147 mbedtls_free( output );
6148 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006149 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006150}
6151/* END_CASE */
6152
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006153/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006154void asymmetric_decrypt( int key_type_arg,
6155 data_t *key_data,
6156 int alg_arg,
6157 data_t *input_data,
6158 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006159 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006160{
Ronald Cron5425a212020-08-04 14:58:35 +02006161 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006162 psa_key_type_t key_type = key_type_arg;
6163 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006164 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006165 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006166 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006167 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006168 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006169
Gilles Peskine8817f612018-12-18 00:18:46 +01006170 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006171
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006172 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6173 psa_set_key_algorithm( &attributes, alg );
6174 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006175
Gilles Peskine049c7532019-05-15 20:22:09 +02006176 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006177 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006178
gabor-mezei-armceface22021-01-21 12:26:17 +01006179 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6180 key_bits = psa_get_key_bits( &attributes );
6181
6182 /* Determine the maximum ciphertext length */
6183 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6184 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6185 ASSERT_ALLOC( output, output_size );
6186
Ronald Cron5425a212020-08-04 14:58:35 +02006187 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006188 input_data->x, input_data->len,
6189 label->x, label->len,
6190 output,
6191 output_size,
6192 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006193 ASSERT_COMPARE( expected_data->x, expected_data->len,
6194 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006195
Gilles Peskine68428122018-06-30 18:42:41 +02006196 /* If the label is empty, the test framework puts a non-null pointer
6197 * in label->x. Test that a null pointer works as well. */
6198 if( label->len == 0 )
6199 {
6200 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006201 if( output_size != 0 )
6202 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006203 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006204 input_data->x, input_data->len,
6205 NULL, label->len,
6206 output,
6207 output_size,
6208 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006209 ASSERT_COMPARE( expected_data->x, expected_data->len,
6210 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006211 }
6212
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006213exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006214 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006215 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006216 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006217 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006218}
6219/* END_CASE */
6220
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006221/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006222void asymmetric_decrypt_fail( int key_type_arg,
6223 data_t *key_data,
6224 int alg_arg,
6225 data_t *input_data,
6226 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006227 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006228 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006229{
Ronald Cron5425a212020-08-04 14:58:35 +02006230 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006231 psa_key_type_t key_type = key_type_arg;
6232 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006233 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006234 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006235 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006236 psa_status_t actual_status;
6237 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006239
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006240 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006241
Gilles Peskine8817f612018-12-18 00:18:46 +01006242 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006243
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006244 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6245 psa_set_key_algorithm( &attributes, alg );
6246 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006247
Gilles Peskine049c7532019-05-15 20:22:09 +02006248 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006249 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006250
Ronald Cron5425a212020-08-04 14:58:35 +02006251 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006252 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006253 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006254 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006255 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006256 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006257 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006258
Gilles Peskine68428122018-06-30 18:42:41 +02006259 /* If the label is empty, the test framework puts a non-null pointer
6260 * in label->x. Test that a null pointer works as well. */
6261 if( label->len == 0 )
6262 {
6263 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006264 if( output_size != 0 )
6265 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006266 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006267 input_data->x, input_data->len,
6268 NULL, label->len,
6269 output, output_size,
6270 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006271 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006272 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006273 }
6274
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006275exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006276 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006277 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006278 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006279 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006280}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006281/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006282
6283/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006284void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006285{
6286 /* Test each valid way of initializing the object, except for `= {0}`, as
6287 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6288 * though it's OK by the C standard. We could test for this, but we'd need
6289 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006290 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006291 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6292 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6293 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006294
6295 memset( &zero, 0, sizeof( zero ) );
6296
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006297 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006298 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006299 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006300 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006301 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006302 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006303 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006304
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006305 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006306 PSA_ASSERT( psa_key_derivation_abort(&func) );
6307 PSA_ASSERT( psa_key_derivation_abort(&init) );
6308 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006309}
6310/* END_CASE */
6311
Janos Follath16de4a42019-06-13 16:32:24 +01006312/* BEGIN_CASE */
6313void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006314{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006315 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006316 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006317 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006318
Gilles Peskine8817f612018-12-18 00:18:46 +01006319 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006320
Janos Follath16de4a42019-06-13 16:32:24 +01006321 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006322 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006323
6324exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006325 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006326 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006327}
6328/* END_CASE */
6329
Janos Follathaf3c2a02019-06-12 12:34:34 +01006330/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006331void derive_set_capacity( int alg_arg, int capacity_arg,
6332 int expected_status_arg )
6333{
6334 psa_algorithm_t alg = alg_arg;
6335 size_t capacity = capacity_arg;
6336 psa_status_t expected_status = expected_status_arg;
6337 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6338
6339 PSA_ASSERT( psa_crypto_init( ) );
6340
6341 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6342
6343 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6344 expected_status );
6345
6346exit:
6347 psa_key_derivation_abort( &operation );
6348 PSA_DONE( );
6349}
6350/* END_CASE */
6351
6352/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006353void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006354 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006355 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006356 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006357 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006358 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006359 int expected_status_arg3,
6360 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006361{
6362 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006363 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6364 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006365 psa_status_t expected_statuses[] = {expected_status_arg1,
6366 expected_status_arg2,
6367 expected_status_arg3};
6368 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006369 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6370 MBEDTLS_SVC_KEY_ID_INIT,
6371 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006372 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6374 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006375 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006376 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006377 psa_status_t expected_output_status = expected_output_status_arg;
6378 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006379
6380 PSA_ASSERT( psa_crypto_init( ) );
6381
6382 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6383 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006384
6385 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6386
6387 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6388 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006389 mbedtls_test_set_step( i );
6390 if( steps[i] == 0 )
6391 {
6392 /* Skip this step */
6393 }
6394 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006395 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006396 psa_set_key_type( &attributes, key_types[i] );
6397 PSA_ASSERT( psa_import_key( &attributes,
6398 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006399 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006400 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6401 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6402 {
6403 // When taking a private key as secret input, use key agreement
6404 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006405 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6406 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006407 expected_statuses[i] );
6408 }
6409 else
6410 {
6411 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006412 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006413 expected_statuses[i] );
6414 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006415 }
6416 else
6417 {
6418 TEST_EQUAL( psa_key_derivation_input_bytes(
6419 &operation, steps[i],
6420 inputs[i]->x, inputs[i]->len ),
6421 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006422 }
6423 }
6424
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006425 if( output_key_type != PSA_KEY_TYPE_NONE )
6426 {
6427 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006428 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006429 psa_set_key_bits( &attributes, 8 );
6430 actual_output_status =
6431 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006432 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006433 }
6434 else
6435 {
6436 uint8_t buffer[1];
6437 actual_output_status =
6438 psa_key_derivation_output_bytes( &operation,
6439 buffer, sizeof( buffer ) );
6440 }
6441 TEST_EQUAL( actual_output_status, expected_output_status );
6442
Janos Follathaf3c2a02019-06-12 12:34:34 +01006443exit:
6444 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006445 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6446 psa_destroy_key( keys[i] );
6447 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006448 PSA_DONE( );
6449}
6450/* END_CASE */
6451
Janos Follathd958bb72019-07-03 15:02:16 +01006452/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006453void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006454{
Janos Follathd958bb72019-07-03 15:02:16 +01006455 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006457 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006458 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006459 unsigned char input1[] = "Input 1";
6460 size_t input1_length = sizeof( input1 );
6461 unsigned char input2[] = "Input 2";
6462 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006463 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006464 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006465 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6466 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6467 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006469
Gilles Peskine8817f612018-12-18 00:18:46 +01006470 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006471
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6473 psa_set_key_algorithm( &attributes, alg );
6474 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006475
Gilles Peskine73676cb2019-05-15 20:15:10 +02006476 PSA_ASSERT( psa_import_key( &attributes,
6477 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006478 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006479
6480 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006481 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6482 input1, input1_length,
6483 input2, input2_length,
6484 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006485 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006486
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006487 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006488 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006489 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006490
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006491 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006492
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006493 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006494 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006495
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006496exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006497 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006498 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006499 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006500}
6501/* END_CASE */
6502
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006503/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006504void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006505{
6506 uint8_t output_buffer[16];
6507 size_t buffer_size = 16;
6508 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006509 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006510
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006511 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6512 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006513 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006514
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006515 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006516 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006517
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006518 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006519
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006520 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6521 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006522 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006523
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006524 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006525 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006526
6527exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006528 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006529}
6530/* END_CASE */
6531
6532/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006533void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006534 int step1_arg, data_t *input1,
6535 int step2_arg, data_t *input2,
6536 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006537 int requested_capacity_arg,
6538 data_t *expected_output1,
6539 data_t *expected_output2 )
6540{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006541 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006542 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6543 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006544 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6545 MBEDTLS_SVC_KEY_ID_INIT,
6546 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006547 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006548 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006549 uint8_t *expected_outputs[2] =
6550 {expected_output1->x, expected_output2->x};
6551 size_t output_sizes[2] =
6552 {expected_output1->len, expected_output2->len};
6553 size_t output_buffer_size = 0;
6554 uint8_t *output_buffer = NULL;
6555 size_t expected_capacity;
6556 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006558 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006559 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006560
6561 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6562 {
6563 if( output_sizes[i] > output_buffer_size )
6564 output_buffer_size = output_sizes[i];
6565 if( output_sizes[i] == 0 )
6566 expected_outputs[i] = NULL;
6567 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006568 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006569 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006570
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6572 psa_set_key_algorithm( &attributes, alg );
6573 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006574
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006575 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006576 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6577 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6578 requested_capacity ) );
6579 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006580 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006581 switch( steps[i] )
6582 {
6583 case 0:
6584 break;
6585 case PSA_KEY_DERIVATION_INPUT_SECRET:
6586 PSA_ASSERT( psa_import_key( &attributes,
6587 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006588 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006589
6590 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6591 {
6592 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6593 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6594 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6595 }
6596
Gilles Peskine1468da72019-05-29 17:35:49 +02006597 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006598 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006599 break;
6600 default:
6601 PSA_ASSERT( psa_key_derivation_input_bytes(
6602 &operation, steps[i],
6603 inputs[i]->x, inputs[i]->len ) );
6604 break;
6605 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006606 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006607
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006608 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006609 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006610 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006611 expected_capacity = requested_capacity;
6612
6613 /* Expansion phase. */
6614 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6615 {
6616 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006617 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006618 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006619 if( expected_capacity == 0 && output_sizes[i] == 0 )
6620 {
6621 /* Reading 0 bytes when 0 bytes are available can go either way. */
6622 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006623 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006624 continue;
6625 }
6626 else if( expected_capacity == 0 ||
6627 output_sizes[i] > expected_capacity )
6628 {
6629 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006630 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006631 expected_capacity = 0;
6632 continue;
6633 }
6634 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006635 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006636 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006637 ASSERT_COMPARE( output_buffer, output_sizes[i],
6638 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006639 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006640 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006641 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006642 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006643 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006644 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006645 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006646
6647exit:
6648 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006649 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006650 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6651 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006652 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006653}
6654/* END_CASE */
6655
6656/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006657void derive_full( int alg_arg,
6658 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006659 data_t *input1,
6660 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006661 int requested_capacity_arg )
6662{
Ronald Cron5425a212020-08-04 14:58:35 +02006663 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006664 psa_algorithm_t alg = alg_arg;
6665 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006666 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006667 unsigned char output_buffer[16];
6668 size_t expected_capacity = requested_capacity;
6669 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006671
Gilles Peskine8817f612018-12-18 00:18:46 +01006672 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006673
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006674 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6675 psa_set_key_algorithm( &attributes, alg );
6676 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006677
Gilles Peskine049c7532019-05-15 20:22:09 +02006678 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006679 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006680
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006681 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6682 input1->x, input1->len,
6683 input2->x, input2->len,
6684 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006685 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006686
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006687 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006688 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006689 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006690
6691 /* Expansion phase. */
6692 while( current_capacity > 0 )
6693 {
6694 size_t read_size = sizeof( output_buffer );
6695 if( read_size > current_capacity )
6696 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006697 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006698 output_buffer,
6699 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006700 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006701 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006702 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006703 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006704 }
6705
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006706 /* Check that the operation refuses to go over capacity. */
6707 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006708 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006709
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006710 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006711
6712exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006713 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006714 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006715 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006716}
6717/* END_CASE */
6718
Janos Follathe60c9052019-07-03 13:51:30 +01006719/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006720void derive_key_exercise( int alg_arg,
6721 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006722 data_t *input1,
6723 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006724 int derived_type_arg,
6725 int derived_bits_arg,
6726 int derived_usage_arg,
6727 int derived_alg_arg )
6728{
Ronald Cron5425a212020-08-04 14:58:35 +02006729 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6730 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006731 psa_algorithm_t alg = alg_arg;
6732 psa_key_type_t derived_type = derived_type_arg;
6733 size_t derived_bits = derived_bits_arg;
6734 psa_key_usage_t derived_usage = derived_usage_arg;
6735 psa_algorithm_t derived_alg = derived_alg_arg;
6736 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006737 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006738 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006739 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006740
Gilles Peskine8817f612018-12-18 00:18:46 +01006741 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006742
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006743 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6744 psa_set_key_algorithm( &attributes, alg );
6745 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006746 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006747 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006748
6749 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006750 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6751 input1->x, input1->len,
6752 input2->x, input2->len,
6753 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006754 goto exit;
6755
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006756 psa_set_key_usage_flags( &attributes, derived_usage );
6757 psa_set_key_algorithm( &attributes, derived_alg );
6758 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006759 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006760 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006761 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006762
6763 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006764 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006765 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6766 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006767
6768 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006769 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006770 goto exit;
6771
6772exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006773 /*
6774 * Key attributes may have been returned by psa_get_key_attributes()
6775 * thus reset them as required.
6776 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006777 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006778
6779 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006780 psa_destroy_key( base_key );
6781 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006782 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006783}
6784/* END_CASE */
6785
Janos Follath42fd8882019-07-03 14:17:09 +01006786/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006787void derive_key_export( int alg_arg,
6788 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006789 data_t *input1,
6790 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006791 int bytes1_arg,
6792 int bytes2_arg )
6793{
Ronald Cron5425a212020-08-04 14:58:35 +02006794 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6795 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006796 psa_algorithm_t alg = alg_arg;
6797 size_t bytes1 = bytes1_arg;
6798 size_t bytes2 = bytes2_arg;
6799 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006800 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006801 uint8_t *output_buffer = NULL;
6802 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006803 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6804 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006805 size_t length;
6806
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006807 ASSERT_ALLOC( output_buffer, capacity );
6808 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006809 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006810
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006811 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6812 psa_set_key_algorithm( &base_attributes, alg );
6813 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006814 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006815 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006816
6817 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006818 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6819 input1->x, input1->len,
6820 input2->x, input2->len,
6821 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006822 goto exit;
6823
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006824 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006825 output_buffer,
6826 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006827 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006828
6829 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006830 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6831 input1->x, input1->len,
6832 input2->x, input2->len,
6833 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006834 goto exit;
6835
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006836 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6837 psa_set_key_algorithm( &derived_attributes, 0 );
6838 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006839 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006840 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006841 &derived_key ) );
6842 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006843 export_buffer, bytes1,
6844 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006845 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006846 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006847 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006848 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006849 &derived_key ) );
6850 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006851 export_buffer + bytes1, bytes2,
6852 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006853 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006854
6855 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006856 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6857 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006858
6859exit:
6860 mbedtls_free( output_buffer );
6861 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006862 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006863 psa_destroy_key( base_key );
6864 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006865 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006866}
6867/* END_CASE */
6868
6869/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006870void derive_key( int alg_arg,
6871 data_t *key_data, data_t *input1, data_t *input2,
6872 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006873 int expected_status_arg,
6874 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006875{
Ronald Cron5425a212020-08-04 14:58:35 +02006876 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6877 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006878 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006879 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006880 size_t bits = bits_arg;
6881 psa_status_t expected_status = expected_status_arg;
6882 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6883 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6884 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6885
6886 PSA_ASSERT( psa_crypto_init( ) );
6887
6888 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6889 psa_set_key_algorithm( &base_attributes, alg );
6890 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6891 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006892 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006893
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006894 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6895 input1->x, input1->len,
6896 input2->x, input2->len,
6897 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006898 goto exit;
6899
6900 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6901 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006902 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006903 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006904
6905 psa_status_t status =
6906 psa_key_derivation_output_key( &derived_attributes,
6907 &operation,
6908 &derived_key );
6909 if( is_large_output > 0 )
6910 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6911 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006912
6913exit:
6914 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006915 psa_destroy_key( base_key );
6916 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006917 PSA_DONE( );
6918}
6919/* END_CASE */
6920
6921/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006922void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006923 int our_key_type_arg, int our_key_alg_arg,
6924 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006925 int expected_status_arg )
6926{
Ronald Cron5425a212020-08-04 14:58:35 +02006927 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006928 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006929 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006930 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006931 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006933 psa_status_t expected_status = expected_status_arg;
6934 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006935
Gilles Peskine8817f612018-12-18 00:18:46 +01006936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006937
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006938 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006939 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006940 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006941 PSA_ASSERT( psa_import_key( &attributes,
6942 our_key_data->x, our_key_data->len,
6943 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006944
Gilles Peskine77f40d82019-04-11 21:27:06 +02006945 /* The tests currently include inputs that should fail at either step.
6946 * Test cases that fail at the setup step should be changed to call
6947 * key_derivation_setup instead, and this function should be renamed
6948 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006949 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006950 if( status == PSA_SUCCESS )
6951 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006952 TEST_EQUAL( psa_key_derivation_key_agreement(
6953 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6954 our_key,
6955 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006956 expected_status );
6957 }
6958 else
6959 {
6960 TEST_ASSERT( status == expected_status );
6961 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006962
6963exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006964 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006965 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006966 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006967}
6968/* END_CASE */
6969
6970/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006971void raw_key_agreement( int alg_arg,
6972 int our_key_type_arg, data_t *our_key_data,
6973 data_t *peer_key_data,
6974 data_t *expected_output )
6975{
Ronald Cron5425a212020-08-04 14:58:35 +02006976 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006977 psa_algorithm_t alg = alg_arg;
6978 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006980 unsigned char *output = NULL;
6981 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006982 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006983
6984 ASSERT_ALLOC( output, expected_output->len );
6985 PSA_ASSERT( psa_crypto_init( ) );
6986
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006987 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6988 psa_set_key_algorithm( &attributes, alg );
6989 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006990 PSA_ASSERT( psa_import_key( &attributes,
6991 our_key_data->x, our_key_data->len,
6992 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006993
gabor-mezei-armceface22021-01-21 12:26:17 +01006994 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6995 key_bits = psa_get_key_bits( &attributes );
6996
Gilles Peskinebe697d82019-05-16 18:00:41 +02006997 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6998 peer_key_data->x, peer_key_data->len,
6999 output, expected_output->len,
7000 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007001 ASSERT_COMPARE( output, output_length,
7002 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01007003 TEST_ASSERT( output_length <=
7004 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7005 TEST_ASSERT( output_length <=
7006 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007007
7008exit:
7009 mbedtls_free( output );
7010 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007011 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007012}
7013/* END_CASE */
7014
7015/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007016void key_agreement_capacity( int alg_arg,
7017 int our_key_type_arg, data_t *our_key_data,
7018 data_t *peer_key_data,
7019 int expected_capacity_arg )
7020{
Ronald Cron5425a212020-08-04 14:58:35 +02007021 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007022 psa_algorithm_t alg = alg_arg;
7023 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007024 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007026 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007027 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007028
Gilles Peskine8817f612018-12-18 00:18:46 +01007029 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007030
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007031 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7032 psa_set_key_algorithm( &attributes, alg );
7033 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007034 PSA_ASSERT( psa_import_key( &attributes,
7035 our_key_data->x, our_key_data->len,
7036 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007037
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007038 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007039 PSA_ASSERT( psa_key_derivation_key_agreement(
7040 &operation,
7041 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7042 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007043 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7044 {
7045 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007046 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007047 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007048 NULL, 0 ) );
7049 }
Gilles Peskine59685592018-09-18 12:11:34 +02007050
Gilles Peskinebf491972018-10-25 22:36:12 +02007051 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007052 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007053 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007054 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007055
Gilles Peskinebf491972018-10-25 22:36:12 +02007056 /* Test the actual capacity by reading the output. */
7057 while( actual_capacity > sizeof( output ) )
7058 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007059 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007060 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007061 actual_capacity -= sizeof( output );
7062 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007063 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007064 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007065 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007066 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007067
Gilles Peskine59685592018-09-18 12:11:34 +02007068exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007069 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007070 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007071 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007072}
7073/* END_CASE */
7074
7075/* BEGIN_CASE */
7076void key_agreement_output( int alg_arg,
7077 int our_key_type_arg, data_t *our_key_data,
7078 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007079 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007080{
Ronald Cron5425a212020-08-04 14:58:35 +02007081 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007082 psa_algorithm_t alg = alg_arg;
7083 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007084 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007086 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007087
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007088 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7089 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007090
Gilles Peskine8817f612018-12-18 00:18:46 +01007091 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007092
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007093 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7094 psa_set_key_algorithm( &attributes, alg );
7095 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007096 PSA_ASSERT( psa_import_key( &attributes,
7097 our_key_data->x, our_key_data->len,
7098 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007099
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007100 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007101 PSA_ASSERT( psa_key_derivation_key_agreement(
7102 &operation,
7103 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7104 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007105 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7106 {
7107 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007108 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007109 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007110 NULL, 0 ) );
7111 }
Gilles Peskine59685592018-09-18 12:11:34 +02007112
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007113 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007114 actual_output,
7115 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007116 ASSERT_COMPARE( actual_output, expected_output1->len,
7117 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007118 if( expected_output2->len != 0 )
7119 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007120 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007121 actual_output,
7122 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007123 ASSERT_COMPARE( actual_output, expected_output2->len,
7124 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007125 }
Gilles Peskine59685592018-09-18 12:11:34 +02007126
7127exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007128 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007129 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007130 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007131 mbedtls_free( actual_output );
7132}
7133/* END_CASE */
7134
7135/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007136void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007137{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007138 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007139 unsigned char *output = NULL;
7140 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007141 size_t i;
7142 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007143
Simon Butcher49f8e312020-03-03 15:51:50 +00007144 TEST_ASSERT( bytes_arg >= 0 );
7145
Gilles Peskine91892022021-02-08 19:50:26 +01007146 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007147 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007148
Gilles Peskine8817f612018-12-18 00:18:46 +01007149 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007150
Gilles Peskinea50d7392018-06-21 10:22:13 +02007151 /* Run several times, to ensure that every output byte will be
7152 * nonzero at least once with overwhelming probability
7153 * (2^(-8*number_of_runs)). */
7154 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007155 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007156 if( bytes != 0 )
7157 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007158 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007159
Gilles Peskinea50d7392018-06-21 10:22:13 +02007160 for( i = 0; i < bytes; i++ )
7161 {
7162 if( output[i] != 0 )
7163 ++changed[i];
7164 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007165 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007166
7167 /* Check that every byte was changed to nonzero at least once. This
7168 * validates that psa_generate_random is overwriting every byte of
7169 * the output buffer. */
7170 for( i = 0; i < bytes; i++ )
7171 {
7172 TEST_ASSERT( changed[i] != 0 );
7173 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007174
7175exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007176 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007177 mbedtls_free( output );
7178 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007179}
7180/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007181
7182/* BEGIN_CASE */
7183void generate_key( int type_arg,
7184 int bits_arg,
7185 int usage_arg,
7186 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007187 int expected_status_arg,
7188 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007189{
Ronald Cron5425a212020-08-04 14:58:35 +02007190 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007191 psa_key_type_t type = type_arg;
7192 psa_key_usage_t usage = usage_arg;
7193 size_t bits = bits_arg;
7194 psa_algorithm_t alg = alg_arg;
7195 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007197 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007198
Gilles Peskine8817f612018-12-18 00:18:46 +01007199 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007200
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007201 psa_set_key_usage_flags( &attributes, usage );
7202 psa_set_key_algorithm( &attributes, alg );
7203 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007204 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007205
7206 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007207 psa_status_t status = psa_generate_key( &attributes, &key );
7208
7209 if( is_large_key > 0 )
7210 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7211 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007212 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007213 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007214
7215 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007216 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007217 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7218 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007219
Gilles Peskine818ca122018-06-20 18:16:48 +02007220 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007221 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007222 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007223
7224exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007225 /*
7226 * Key attributes may have been returned by psa_get_key_attributes()
7227 * thus reset them as required.
7228 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007229 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007230
Ronald Cron5425a212020-08-04 14:58:35 +02007231 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007232 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007233}
7234/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007235
Ronald Cronee414c72021-03-18 18:50:08 +01007236/* 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 +02007237void generate_key_rsa( int bits_arg,
7238 data_t *e_arg,
7239 int expected_status_arg )
7240{
Ronald Cron5425a212020-08-04 14:58:35 +02007241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007242 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007243 size_t bits = bits_arg;
7244 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7245 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7246 psa_status_t expected_status = expected_status_arg;
7247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7248 uint8_t *exported = NULL;
7249 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007250 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007251 size_t exported_length = SIZE_MAX;
7252 uint8_t *e_read_buffer = NULL;
7253 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007254 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007255 size_t e_read_length = SIZE_MAX;
7256
7257 if( e_arg->len == 0 ||
7258 ( e_arg->len == 3 &&
7259 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7260 {
7261 is_default_public_exponent = 1;
7262 e_read_size = 0;
7263 }
7264 ASSERT_ALLOC( e_read_buffer, e_read_size );
7265 ASSERT_ALLOC( exported, exported_size );
7266
7267 PSA_ASSERT( psa_crypto_init( ) );
7268
7269 psa_set_key_usage_flags( &attributes, usage );
7270 psa_set_key_algorithm( &attributes, alg );
7271 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7272 e_arg->x, e_arg->len ) );
7273 psa_set_key_bits( &attributes, bits );
7274
7275 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007276 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007277 if( expected_status != PSA_SUCCESS )
7278 goto exit;
7279
7280 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007281 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007282 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7283 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7284 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7285 e_read_buffer, e_read_size,
7286 &e_read_length ) );
7287 if( is_default_public_exponent )
7288 TEST_EQUAL( e_read_length, 0 );
7289 else
7290 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7291
7292 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007293 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007294 goto exit;
7295
7296 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007297 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007298 exported, exported_size,
7299 &exported_length ) );
7300 {
7301 uint8_t *p = exported;
7302 uint8_t *end = exported + exported_length;
7303 size_t len;
7304 /* RSAPublicKey ::= SEQUENCE {
7305 * modulus INTEGER, -- n
7306 * publicExponent INTEGER } -- e
7307 */
7308 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007309 MBEDTLS_ASN1_SEQUENCE |
7310 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007311 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007312 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7313 MBEDTLS_ASN1_INTEGER ) );
7314 if( len >= 1 && p[0] == 0 )
7315 {
7316 ++p;
7317 --len;
7318 }
7319 if( e_arg->len == 0 )
7320 {
7321 TEST_EQUAL( len, 3 );
7322 TEST_EQUAL( p[0], 1 );
7323 TEST_EQUAL( p[1], 0 );
7324 TEST_EQUAL( p[2], 1 );
7325 }
7326 else
7327 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7328 }
7329
7330exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007331 /*
7332 * Key attributes may have been returned by psa_get_key_attributes() or
7333 * set by psa_set_key_domain_parameters() thus reset them as required.
7334 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007335 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007336
Ronald Cron5425a212020-08-04 14:58:35 +02007337 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007338 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007339 mbedtls_free( e_read_buffer );
7340 mbedtls_free( exported );
7341}
7342/* END_CASE */
7343
Darryl Greend49a4992018-06-18 17:27:26 +01007344/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007345void persistent_key_load_key_from_storage( data_t *data,
7346 int type_arg, int bits_arg,
7347 int usage_flags_arg, int alg_arg,
7348 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007349{
Ronald Cron71016a92020-08-28 19:01:50 +02007350 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7353 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007354 psa_key_type_t type = type_arg;
7355 size_t bits = bits_arg;
7356 psa_key_usage_t usage_flags = usage_flags_arg;
7357 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007358 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007359 unsigned char *first_export = NULL;
7360 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007361 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007362 size_t first_exported_length;
7363 size_t second_exported_length;
7364
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007365 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7366 {
7367 ASSERT_ALLOC( first_export, export_size );
7368 ASSERT_ALLOC( second_export, export_size );
7369 }
Darryl Greend49a4992018-06-18 17:27:26 +01007370
Gilles Peskine8817f612018-12-18 00:18:46 +01007371 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007372
Gilles Peskinec87af662019-05-15 16:12:22 +02007373 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007374 psa_set_key_usage_flags( &attributes, usage_flags );
7375 psa_set_key_algorithm( &attributes, alg );
7376 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007377 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007378
Darryl Green0c6575a2018-11-07 16:05:30 +00007379 switch( generation_method )
7380 {
7381 case IMPORT_KEY:
7382 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007383 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007384 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007385 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007386
Darryl Green0c6575a2018-11-07 16:05:30 +00007387 case GENERATE_KEY:
7388 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007389 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007390 break;
7391
7392 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007393#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007394 {
7395 /* Create base key */
7396 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7397 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7398 psa_set_key_usage_flags( &base_attributes,
7399 PSA_KEY_USAGE_DERIVE );
7400 psa_set_key_algorithm( &base_attributes, derive_alg );
7401 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007402 PSA_ASSERT( psa_import_key( &base_attributes,
7403 data->x, data->len,
7404 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007405 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007406 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007407 PSA_ASSERT( psa_key_derivation_input_key(
7408 &operation,
7409 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007410 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007411 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007412 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007413 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7414 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007415 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007416 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007417 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007418 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007419 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007420#else
7421 TEST_ASSUME( ! "KDF not supported in this configuration" );
7422#endif
7423 break;
7424
7425 default:
7426 TEST_ASSERT( ! "generation_method not implemented in test" );
7427 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007428 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007429 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007430
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007431 /* Export the key if permitted by the key policy. */
7432 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7433 {
Ronald Cron5425a212020-08-04 14:58:35 +02007434 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007435 first_export, export_size,
7436 &first_exported_length ) );
7437 if( generation_method == IMPORT_KEY )
7438 ASSERT_COMPARE( data->x, data->len,
7439 first_export, first_exported_length );
7440 }
Darryl Greend49a4992018-06-18 17:27:26 +01007441
7442 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007443 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007444 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007445 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007446
Darryl Greend49a4992018-06-18 17:27:26 +01007447 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007448 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007449 TEST_ASSERT( mbedtls_svc_key_id_equal(
7450 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007451 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7452 PSA_KEY_LIFETIME_PERSISTENT );
7453 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7454 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007455 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007456 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007457 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007458
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007459 /* Export the key again if permitted by the key policy. */
7460 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007461 {
Ronald Cron5425a212020-08-04 14:58:35 +02007462 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007463 second_export, export_size,
7464 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007465 ASSERT_COMPARE( first_export, first_exported_length,
7466 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007467 }
7468
7469 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007470 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007471 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007472
7473exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007474 /*
7475 * Key attributes may have been returned by psa_get_key_attributes()
7476 * thus reset them as required.
7477 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007478 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007479
Darryl Greend49a4992018-06-18 17:27:26 +01007480 mbedtls_free( first_export );
7481 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007482 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007483 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007484 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007485 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007486}
7487/* END_CASE */