blob: fe9e0014dbbdb986bb4665332a8cc0ab0d854264 [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"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100267/*!
268 * \brief Internal Function for AEAD multipart tests.
269 *
270 * \param key_type_arg Type of key passed in
271 * \param key_data The encryption / decryption key data
272 * \param alg_arg The type of algorithm used
273 * \param nonce Nonce data
274 * \param additional_data Additional data
275 * \param ad_part_len If not -1, the length of chunks to
276 * feed additional data in to be encrypted /
277 * decrypted. If -1, no chunking.
278 * \param input_data Data to encrypt / decrypt
279 * \param data_part_len If not -1, the length of chunks to feed the
280 * data in to be encrypted / decrypted. If -1,
281 * no chunking
282 * \param do_set_lengths If non-zero, then set lengths prior to
283 * calling encryption / decryption.
284 * \param expected_output Expected output
285 * \param expected_status_arg Expected status
286 * \param is_encrypt If non-zero this is an encryption operation.
287 *
288 * \return int Zero on failure, non-zero on success.
289 *
290 */
291static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
292 int alg_arg,
293 data_t *nonce,
294 data_t *additional_data,
295 int ad_part_len,
296 data_t *input_data,
297 int data_part_len,
298 int do_set_lengths,
299 data_t *expected_output,
300 int expect_valid_signature,
301 int is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100302{
303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
304 psa_key_type_t key_type = key_type_arg;
305 psa_algorithm_t alg = alg_arg;
306 psa_aead_operation_t operation;
307 unsigned char *output_data = NULL;
308 unsigned char *part_data = NULL;
309 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100310 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100311 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100312 size_t output_size = 0;
313 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100314 size_t output_length = 0;
315 size_t key_bits = 0;
316 size_t tag_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100317 uint32_t part_offset = 0;
318 size_t part_length = 0;
319 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100320 size_t tag_size = 0;
321 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
323 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
324
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 int test_ok = 0;
326
Paul Elliottd3f82412021-06-16 16:52:21 +0100327 PSA_ASSERT( psa_crypto_init( ) );
328
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100329 if( is_encrypt )
330 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
331 else
332 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
333
Paul Elliottd3f82412021-06-16 16:52:21 +0100334 psa_set_key_algorithm( &attributes, alg );
335 psa_set_key_type( &attributes, key_type );
336
337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
338 &key ) );
339
340 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
341 key_bits = psa_get_key_bits( &attributes );
342
343 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 if( is_encrypt )
346 {
347 /* Tag gets written at end of buffer. */
348 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
349 ( input_data->len +
350 tag_length ) );
351 data_true_size = input_data->len;
352 }
353 else
354 {
355 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
356 ( input_data->len -
357 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100358
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100359 /* Do not want to attempt to decrypt tag. */
360 data_true_size = input_data->len - tag_length;
361 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100362
363 ASSERT_ALLOC( output_data, output_size );
364
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100365 if( is_encrypt )
366 {
367 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
368 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
369 }
370 else
371 {
372 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
373 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
374 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100377
378 operation = psa_aead_operation_init( );
379
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100380
381 if( is_encrypt )
382 status = psa_aead_encrypt_setup( &operation, key, alg );
383 else
384 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100385
386 /* If the operation is not supported, just skip and not fail in case the
387 * encryption involves a common limitation of cryptography hardwares and
388 * an alternative implementation. */
389 if( status == PSA_ERROR_NOT_SUPPORTED )
390 {
391 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
392 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
393 }
394
395 PSA_ASSERT( status );
396
397 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
398
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100399 if( do_set_lengths )
Paul Elliottd3f82412021-06-16 16:52:21 +0100400 {
401 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100402 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100403 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100404
405 if( ad_part_len != -1 )
406 {
407 /* Pass additional data in parts */
408 part_offset = 0;
409
410 while( part_offset < additional_data->len )
411 {
412 if( additional_data->len - part_offset < ( uint32_t ) ad_part_len )
413 {
414 part_length = additional_data->len - part_offset;
415 }
416 else
417 {
418 part_length = ad_part_len;
419 }
420
421 PSA_ASSERT( psa_aead_update_ad( &operation,
422 additional_data->x + part_offset,
423 part_length ) );
424
425 part_offset += part_length;
426 }
427 }
428 else
429 {
430 /* Pass additional data in one go. */
431 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
432 additional_data->len ) );
433 }
434
435 if( data_part_len != -1 )
436 {
437 /* Pass data in parts */
438 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100439 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
441 ASSERT_ALLOC( part_data, part_data_size );
442
443 part_offset = 0;
444
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100445 while( part_offset < data_true_size )
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100447 if( ( data_true_size - part_offset ) < ( uint32_t ) data_part_len )
Paul Elliottd3f82412021-06-16 16:52:21 +0100448 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100449 part_length = ( data_true_size - part_offset );
Paul Elliottd3f82412021-06-16 16:52:21 +0100450 }
451 else
452 {
453 part_length = data_part_len;
454 }
455
456 PSA_ASSERT( psa_aead_update( &operation,
457 ( input_data->x + part_offset ),
458 part_length, part_data,
459 part_data_size,
460 &output_part_length ) );
461
462 if( output_data && output_part_length )
463 {
464 memcpy( ( output_data + part_offset ), part_data,
465 output_part_length );
466 }
467
468 part_offset += part_length;
469 output_length += output_part_length;
470 }
471 }
472 else
473 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100474 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100475 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100476 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 output_size, &output_length ) );
478 }
479
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100480 if( is_encrypt )
481 PSA_ASSERT( psa_aead_finish( &operation, final_data,
482 final_output_size,
483 &output_part_length,
484 tag_buffer, tag_length,
485 &tag_size ) );
486 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100487 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100488 status = psa_aead_verify( &operation, final_data,
489 final_output_size,
490 &output_part_length,
491 ( input_data->x + data_true_size ),
492 tag_length );
493
494 if( status != PSA_SUCCESS )
495 {
496 if( !expect_valid_signature )
497 {
498 /* Expected failure. */
499 test_ok = 1;
500 goto exit;
501 }
502 else
503 PSA_ASSERT( status );
504 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100505 }
506
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100507 if( output_data && output_part_length )
508 memcpy( ( output_data + output_length ), final_data,
509 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100510
511 output_length += output_part_length;
512
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100513
514 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
515 * should be exact.*/
516 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100517 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100518 TEST_EQUAL( tag_length, tag_size );
519
520 if( output_data && tag_length )
521 memcpy( ( output_data + output_length ), tag_buffer,
522 tag_length );
523
524 output_length += tag_length;
525
526 TEST_EQUAL( output_length,
527 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
528 input_data->len ) );
529 TEST_ASSERT( output_length <=
530 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
531 }
532 else
533 {
534 TEST_EQUAL( output_length,
535 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
536 input_data->len ) );
537 TEST_ASSERT( output_length <=
538 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100539 }
540
Paul Elliottd3f82412021-06-16 16:52:21 +0100541
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100542 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100543 output_data, output_length );
544
Paul Elliottd3f82412021-06-16 16:52:21 +0100545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100547
548exit:
549 psa_destroy_key( key );
550 psa_aead_abort( &operation );
551 mbedtls_free( output_data );
552 mbedtls_free( part_data );
553 mbedtls_free( final_data );
554 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100555
556 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100557}
558
Gilles Peskinee59236f2018-01-27 23:32:46 +0100559/* END_HEADER */
560
561/* BEGIN_DEPENDENCIES
562 * depends_on:MBEDTLS_PSA_CRYPTO_C
563 * END_DEPENDENCIES
564 */
565
566/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200567void static_checks( )
568{
569 size_t max_truncated_mac_size =
570 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
571
572 /* Check that the length for a truncated MAC always fits in the algorithm
573 * encoding. The shifted mask is the maximum truncated value. The
574 * untruncated algorithm may be one byte larger. */
575 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
576}
577/* END_CASE */
578
579/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200580void import_with_policy( int type_arg,
581 int usage_arg, int alg_arg,
582 int expected_status_arg )
583{
584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
585 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200587 psa_key_type_t type = type_arg;
588 psa_key_usage_t usage = usage_arg;
589 psa_algorithm_t alg = alg_arg;
590 psa_status_t expected_status = expected_status_arg;
591 const uint8_t key_material[16] = {0};
592 psa_status_t status;
593
594 PSA_ASSERT( psa_crypto_init( ) );
595
596 psa_set_key_type( &attributes, type );
597 psa_set_key_usage_flags( &attributes, usage );
598 psa_set_key_algorithm( &attributes, alg );
599
600 status = psa_import_key( &attributes,
601 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200602 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200603 TEST_EQUAL( status, expected_status );
604 if( status != PSA_SUCCESS )
605 goto exit;
606
Ronald Cron5425a212020-08-04 14:58:35 +0200607 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200608 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
609 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
610 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200611 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200612
Ronald Cron5425a212020-08-04 14:58:35 +0200613 PSA_ASSERT( psa_destroy_key( key ) );
614 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200615
616exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100617 /*
618 * Key attributes may have been returned by psa_get_key_attributes()
619 * thus reset them as required.
620 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200621 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100622
623 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200624 PSA_DONE( );
625}
626/* END_CASE */
627
628/* BEGIN_CASE */
629void import_with_data( data_t *data, int type_arg,
630 int attr_bits_arg,
631 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200632{
633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
634 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200636 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200637 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200638 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100639 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640
Gilles Peskine8817f612018-12-18 00:18:46 +0100641 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100642
Gilles Peskine4747d192019-04-17 15:05:45 +0200643 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200644 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200645
Ronald Cron5425a212020-08-04 14:58:35 +0200646 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100647 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200648 if( status != PSA_SUCCESS )
649 goto exit;
650
Ronald Cron5425a212020-08-04 14:58:35 +0200651 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200652 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200653 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200654 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200655 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200656
Ronald Cron5425a212020-08-04 14:58:35 +0200657 PSA_ASSERT( psa_destroy_key( key ) );
658 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659
660exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100661 /*
662 * Key attributes may have been returned by psa_get_key_attributes()
663 * thus reset them as required.
664 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200665 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100666
667 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200668 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100669}
670/* END_CASE */
671
672/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200673void import_large_key( int type_arg, int byte_size_arg,
674 int expected_status_arg )
675{
676 psa_key_type_t type = type_arg;
677 size_t byte_size = byte_size_arg;
678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
679 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200680 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200681 psa_status_t status;
682 uint8_t *buffer = NULL;
683 size_t buffer_size = byte_size + 1;
684 size_t n;
685
Steven Cooreman69967ce2021-01-18 18:01:08 +0100686 /* Skip the test case if the target running the test cannot
687 * accomodate large keys due to heap size constraints */
688 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200689 memset( buffer, 'K', byte_size );
690
691 PSA_ASSERT( psa_crypto_init( ) );
692
693 /* Try importing the key */
694 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
695 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200696 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100697 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200698 TEST_EQUAL( status, expected_status );
699
700 if( status == PSA_SUCCESS )
701 {
Ronald Cron5425a212020-08-04 14:58:35 +0200702 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200703 TEST_EQUAL( psa_get_key_type( &attributes ), type );
704 TEST_EQUAL( psa_get_key_bits( &attributes ),
705 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200706 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200707 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200708 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200709 for( n = 0; n < byte_size; n++ )
710 TEST_EQUAL( buffer[n], 'K' );
711 for( n = byte_size; n < buffer_size; n++ )
712 TEST_EQUAL( buffer[n], 0 );
713 }
714
715exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100716 /*
717 * Key attributes may have been returned by psa_get_key_attributes()
718 * thus reset them as required.
719 */
720 psa_reset_key_attributes( &attributes );
721
Ronald Cron5425a212020-08-04 14:58:35 +0200722 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200723 PSA_DONE( );
724 mbedtls_free( buffer );
725}
726/* END_CASE */
727
728/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200729void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
730{
Ronald Cron5425a212020-08-04 14:58:35 +0200731 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200732 size_t bits = bits_arg;
733 psa_status_t expected_status = expected_status_arg;
734 psa_status_t status;
735 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200736 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200737 size_t buffer_size = /* Slight overapproximations */
738 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200739 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200740 unsigned char *p;
741 int ret;
742 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200744
Gilles Peskine8817f612018-12-18 00:18:46 +0100745 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200746 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200747
748 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
749 bits, keypair ) ) >= 0 );
750 length = ret;
751
752 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200753 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200754 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100755 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200756
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200757 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200758 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200759
760exit:
761 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200762 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763}
764/* END_CASE */
765
766/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300767void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300768 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200769 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770 int expected_bits,
771 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200772 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100773 int canonical_input )
774{
Ronald Cron5425a212020-08-04 14:58:35 +0200775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100776 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200777 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200778 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100780 unsigned char *exported = NULL;
781 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100783 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100784 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200786 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100787
Moran Pekercb088e72018-07-17 17:36:59 +0300788 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200789 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200791 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100792 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100793
Gilles Peskine4747d192019-04-17 15:05:45 +0200794 psa_set_key_usage_flags( &attributes, usage_arg );
795 psa_set_key_algorithm( &attributes, alg );
796 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700797
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100798 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200799 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800
801 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200802 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200803 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
804 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200805 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100806
807 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200808 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100809 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100810
811 /* The exported length must be set by psa_export_key() to a value between 0
812 * and export_size. On errors, the exported length must be 0. */
813 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
814 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
815 TEST_ASSERT( exported_length <= export_size );
816
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200817 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200818 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200820 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100821 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100822 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200823 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824
Gilles Peskineea38a922021-02-13 00:05:16 +0100825 /* Run sanity checks on the exported key. For non-canonical inputs,
826 * this validates the canonical representations. For canonical inputs,
827 * this doesn't directly validate the implementation, but it still helps
828 * by cross-validating the test data with the sanity check code. */
829 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200830 goto exit;
831
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100832 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200833 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100834 else
835 {
Ronald Cron5425a212020-08-04 14:58:35 +0200836 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200837 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200838 &key2 ) );
839 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100840 reexported,
841 export_size,
842 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200843 ASSERT_COMPARE( exported, exported_length,
844 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200845 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100846 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100847 TEST_ASSERT( exported_length <=
848 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
849 psa_get_key_bits( &got_attributes ) ) );
850 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100851
852destroy:
853 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200854 PSA_ASSERT( psa_destroy_key( key ) );
855 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100856
857exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100858 /*
859 * Key attributes may have been returned by psa_get_key_attributes()
860 * thus reset them as required.
861 */
862 psa_reset_key_attributes( &got_attributes );
863
itayzafrir3e02b3b2018-06-12 17:06:52 +0300864 mbedtls_free( exported );
865 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200866 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867}
868/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100869
Moran Pekerf709f4a2018-06-06 17:26:04 +0300870/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300871void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200872 int type_arg,
873 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100874 int export_size_delta,
875 int expected_export_status_arg,
876 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300877{
Ronald Cron5425a212020-08-04 14:58:35 +0200878 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300879 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200880 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200881 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300882 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300883 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100884 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100885 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300887
Gilles Peskine8817f612018-12-18 00:18:46 +0100888 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300889
Gilles Peskine4747d192019-04-17 15:05:45 +0200890 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
891 psa_set_key_algorithm( &attributes, alg );
892 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300893
894 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200895 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300896
Gilles Peskine49c25912018-10-29 15:15:31 +0100897 /* Export the public key */
898 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200899 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200900 exported, export_size,
901 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100902 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100903 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100904 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200905 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100906 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200907 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200908 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100909 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100910 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100911 TEST_ASSERT( expected_public_key->len <=
912 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
913 TEST_ASSERT( expected_public_key->len <=
914 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
916 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100917 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918
919exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100920 /*
921 * Key attributes may have been returned by psa_get_key_attributes()
922 * thus reset them as required.
923 */
924 psa_reset_key_attributes( &attributes );
925
itayzafrir3e02b3b2018-06-12 17:06:52 +0300926 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200927 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200928 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929}
930/* END_CASE */
931
Gilles Peskine20035e32018-02-03 22:44:14 +0100932/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200933void import_and_exercise_key( data_t *data,
934 int type_arg,
935 int bits_arg,
936 int alg_arg )
937{
Ronald Cron5425a212020-08-04 14:58:35 +0200938 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200939 psa_key_type_t type = type_arg;
940 size_t bits = bits_arg;
941 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100942 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200943 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200944 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200945
Gilles Peskine8817f612018-12-18 00:18:46 +0100946 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200947
Gilles Peskine4747d192019-04-17 15:05:45 +0200948 psa_set_key_usage_flags( &attributes, usage );
949 psa_set_key_algorithm( &attributes, alg );
950 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200951
952 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200953 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200954
955 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200956 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200957 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
958 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200959
960 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100961 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200962 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200963
Ronald Cron5425a212020-08-04 14:58:35 +0200964 PSA_ASSERT( psa_destroy_key( key ) );
965 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200966
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200967exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100968 /*
969 * Key attributes may have been returned by psa_get_key_attributes()
970 * thus reset them as required.
971 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200972 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100973
974 psa_reset_key_attributes( &attributes );
975 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200976 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200977}
978/* END_CASE */
979
980/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100981void effective_key_attributes( int type_arg, int expected_type_arg,
982 int bits_arg, int expected_bits_arg,
983 int usage_arg, int expected_usage_arg,
984 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200985{
Ronald Cron5425a212020-08-04 14:58:35 +0200986 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100987 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100988 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100989 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100990 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200991 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100992 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200993 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100994 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200996
Gilles Peskine8817f612018-12-18 00:18:46 +0100997 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200998
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200999 psa_set_key_usage_flags( &attributes, usage );
1000 psa_set_key_algorithm( &attributes, alg );
1001 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001002 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001003
Ronald Cron5425a212020-08-04 14:58:35 +02001004 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001005 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001006
Ronald Cron5425a212020-08-04 14:58:35 +02001007 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001008 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1009 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1010 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1011 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001012
1013exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001014 /*
1015 * Key attributes may have been returned by psa_get_key_attributes()
1016 * thus reset them as required.
1017 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001018 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001019
1020 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001021 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001022}
1023/* END_CASE */
1024
1025/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001026void check_key_policy( int type_arg, int bits_arg,
1027 int usage_arg, int alg_arg )
1028{
1029 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1030 usage_arg, usage_arg, alg_arg, alg_arg );
1031 goto exit;
1032}
1033/* END_CASE */
1034
1035/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001036void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001037{
1038 /* Test each valid way of initializing the object, except for `= {0}`, as
1039 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1040 * though it's OK by the C standard. We could test for this, but we'd need
1041 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001042 psa_key_attributes_t func = psa_key_attributes_init( );
1043 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1044 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001045
1046 memset( &zero, 0, sizeof( zero ) );
1047
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001048 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1049 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1050 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001051
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001052 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1053 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1054 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1055
1056 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1057 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1058 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1059
1060 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1061 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1062 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1063
1064 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1065 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1066 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001067}
1068/* END_CASE */
1069
1070/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001071void mac_key_policy( int policy_usage,
1072 int policy_alg,
1073 int key_type,
1074 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001075 int exercise_alg,
1076 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001077{
Ronald Cron5425a212020-08-04 14:58:35 +02001078 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001079 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001080 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001081 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001082 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001083 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001084
Gilles Peskine8817f612018-12-18 00:18:46 +01001085 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001086
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001087 psa_set_key_usage_flags( &attributes, policy_usage );
1088 psa_set_key_algorithm( &attributes, policy_alg );
1089 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001090
Gilles Peskine049c7532019-05-15 20:22:09 +02001091 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001092 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001093
Ronald Cron5425a212020-08-04 14:58:35 +02001094 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001095 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001096 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001097 else
1098 TEST_EQUAL( status, expected_status );
1099
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001100 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001101
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001102 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001103 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001104 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001105 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001106 else
1107 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001108
1109exit:
1110 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001111 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001112 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001113}
1114/* END_CASE */
1115
1116/* BEGIN_CASE */
1117void cipher_key_policy( int policy_usage,
1118 int policy_alg,
1119 int key_type,
1120 data_t *key_data,
1121 int exercise_alg )
1122{
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 Amero5bae2272019-01-04 11:48:27 +00001125 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001126 psa_status_t status;
1127
Gilles Peskine8817f612018-12-18 00:18:46 +01001128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001130 psa_set_key_usage_flags( &attributes, policy_usage );
1131 psa_set_key_algorithm( &attributes, policy_alg );
1132 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133
Gilles Peskine049c7532019-05-15 20:22:09 +02001134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001135 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001136
Ronald Cron5425a212020-08-04 14:58:35 +02001137 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001138 if( policy_alg == exercise_alg &&
1139 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001140 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001142 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001143 psa_cipher_abort( &operation );
1144
Ronald Cron5425a212020-08-04 14:58:35 +02001145 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001146 if( policy_alg == exercise_alg &&
1147 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001148 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001149 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001150 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001151
1152exit:
1153 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001154 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001155 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001156}
1157/* END_CASE */
1158
1159/* BEGIN_CASE */
1160void aead_key_policy( int policy_usage,
1161 int policy_alg,
1162 int key_type,
1163 data_t *key_data,
1164 int nonce_length_arg,
1165 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001166 int exercise_alg,
1167 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001168{
Ronald Cron5425a212020-08-04 14:58:35 +02001169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001171 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001172 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001173 unsigned char nonce[16] = {0};
1174 size_t nonce_length = nonce_length_arg;
1175 unsigned char tag[16];
1176 size_t tag_length = tag_length_arg;
1177 size_t output_length;
1178
1179 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1180 TEST_ASSERT( tag_length <= sizeof( tag ) );
1181
Gilles Peskine8817f612018-12-18 00:18:46 +01001182 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001183
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001184 psa_set_key_usage_flags( &attributes, policy_usage );
1185 psa_set_key_algorithm( &attributes, policy_alg );
1186 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001187
Gilles Peskine049c7532019-05-15 20:22:09 +02001188 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001189 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001190
Ronald Cron5425a212020-08-04 14:58:35 +02001191 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001192 nonce, nonce_length,
1193 NULL, 0,
1194 NULL, 0,
1195 tag, tag_length,
1196 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001197 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1198 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001199 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001200 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201
1202 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001203 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204 nonce, nonce_length,
1205 NULL, 0,
1206 tag, tag_length,
1207 NULL, 0,
1208 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001209 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1210 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1211 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001212 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001214 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001215
1216exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001218 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219}
1220/* END_CASE */
1221
1222/* BEGIN_CASE */
1223void asymmetric_encryption_key_policy( int policy_usage,
1224 int policy_alg,
1225 int key_type,
1226 data_t *key_data,
1227 int exercise_alg )
1228{
Ronald Cron5425a212020-08-04 14:58:35 +02001229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001231 psa_status_t status;
1232 size_t key_bits;
1233 size_t buffer_length;
1234 unsigned char *buffer = NULL;
1235 size_t output_length;
1236
Gilles Peskine8817f612018-12-18 00:18:46 +01001237 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001238
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001239 psa_set_key_usage_flags( &attributes, policy_usage );
1240 psa_set_key_algorithm( &attributes, policy_alg );
1241 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001242
Gilles Peskine049c7532019-05-15 20:22:09 +02001243 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001244 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001245
Ronald Cron5425a212020-08-04 14:58:35 +02001246 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001247 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001248 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1249 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001250 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001251
Ronald Cron5425a212020-08-04 14:58:35 +02001252 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001253 NULL, 0,
1254 NULL, 0,
1255 buffer, buffer_length,
1256 &output_length );
1257 if( policy_alg == exercise_alg &&
1258 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001259 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001260 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001261 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001262
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001263 if( buffer_length != 0 )
1264 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001265 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266 buffer, buffer_length,
1267 NULL, 0,
1268 buffer, buffer_length,
1269 &output_length );
1270 if( policy_alg == exercise_alg &&
1271 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001272 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
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
1276exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001277 /*
1278 * Key attributes may have been returned by psa_get_key_attributes()
1279 * thus reset them as required.
1280 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001281 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001282
1283 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001284 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001285 mbedtls_free( buffer );
1286}
1287/* END_CASE */
1288
1289/* BEGIN_CASE */
1290void asymmetric_signature_key_policy( int policy_usage,
1291 int policy_alg,
1292 int key_type,
1293 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001294 int exercise_alg,
1295 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296{
Ronald Cron5425a212020-08-04 14:58:35 +02001297 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001298 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001299 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001300 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1301 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1302 * compatible with the policy and `payload_length_arg` is supposed to be
1303 * a valid input length to sign. If `payload_length_arg <= 0`,
1304 * `exercise_alg` is supposed to be forbidden by the policy. */
1305 int compatible_alg = payload_length_arg > 0;
1306 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001307 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001308 size_t signature_length;
1309
Gilles Peskine8817f612018-12-18 00:18:46 +01001310 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001311
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001312 psa_set_key_usage_flags( &attributes, policy_usage );
1313 psa_set_key_algorithm( &attributes, policy_alg );
1314 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001315
Gilles Peskine049c7532019-05-15 20:22:09 +02001316 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001317 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001318
Ronald Cron5425a212020-08-04 14:58:35 +02001319 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001320 payload, payload_length,
1321 signature, sizeof( signature ),
1322 &signature_length );
1323 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001324 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001325 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001326 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001327
1328 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001329 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001330 payload, payload_length,
1331 signature, sizeof( signature ) );
1332 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001333 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001334 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001335 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001336
1337exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001338 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001339 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001340}
1341/* END_CASE */
1342
Janos Follathba3fab92019-06-11 14:50:16 +01001343/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001344void derive_key_policy( int policy_usage,
1345 int policy_alg,
1346 int key_type,
1347 data_t *key_data,
1348 int exercise_alg )
1349{
Ronald Cron5425a212020-08-04 14:58:35 +02001350 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001352 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001353 psa_status_t status;
1354
Gilles Peskine8817f612018-12-18 00:18:46 +01001355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001356
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001357 psa_set_key_usage_flags( &attributes, policy_usage );
1358 psa_set_key_algorithm( &attributes, policy_alg );
1359 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001360
Gilles Peskine049c7532019-05-15 20:22:09 +02001361 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001362 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001363
Janos Follathba3fab92019-06-11 14:50:16 +01001364 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1365
1366 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1367 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001368 {
Janos Follathba3fab92019-06-11 14:50:16 +01001369 PSA_ASSERT( psa_key_derivation_input_bytes(
1370 &operation,
1371 PSA_KEY_DERIVATION_INPUT_SEED,
1372 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001373 }
Janos Follathba3fab92019-06-11 14:50:16 +01001374
1375 status = psa_key_derivation_input_key( &operation,
1376 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001377 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001378
Gilles Peskineea0fb492018-07-12 17:17:20 +02001379 if( policy_alg == exercise_alg &&
1380 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001381 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001382 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001383 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001384
1385exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001386 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001387 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001388 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001389}
1390/* END_CASE */
1391
1392/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001393void agreement_key_policy( int policy_usage,
1394 int policy_alg,
1395 int key_type_arg,
1396 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001397 int exercise_alg,
1398 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001399{
Ronald Cron5425a212020-08-04 14:58:35 +02001400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001402 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001403 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001404 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001405 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001406
Gilles Peskine8817f612018-12-18 00:18:46 +01001407 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001408
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001409 psa_set_key_usage_flags( &attributes, policy_usage );
1410 psa_set_key_algorithm( &attributes, policy_alg );
1411 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001412
Gilles Peskine049c7532019-05-15 20:22:09 +02001413 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001414 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001415
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001416 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001417 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001418
Steven Cooremance48e852020-10-05 16:02:45 +02001419 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001420
1421exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001422 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001423 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001424 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001425}
1426/* END_CASE */
1427
1428/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001429void key_policy_alg2( int key_type_arg, data_t *key_data,
1430 int usage_arg, int alg_arg, int alg2_arg )
1431{
Ronald Cron5425a212020-08-04 14:58:35 +02001432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001433 psa_key_type_t key_type = key_type_arg;
1434 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1435 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1436 psa_key_usage_t usage = usage_arg;
1437 psa_algorithm_t alg = alg_arg;
1438 psa_algorithm_t alg2 = alg2_arg;
1439
1440 PSA_ASSERT( psa_crypto_init( ) );
1441
1442 psa_set_key_usage_flags( &attributes, usage );
1443 psa_set_key_algorithm( &attributes, alg );
1444 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1445 psa_set_key_type( &attributes, key_type );
1446 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001447 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001448
Ronald Cron5425a212020-08-04 14:58:35 +02001449 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001450 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1451 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1452 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1453
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001454 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001455 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001456 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001457 goto exit;
1458
1459exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001460 /*
1461 * Key attributes may have been returned by psa_get_key_attributes()
1462 * thus reset them as required.
1463 */
1464 psa_reset_key_attributes( &got_attributes );
1465
Ronald Cron5425a212020-08-04 14:58:35 +02001466 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001467 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001468}
1469/* END_CASE */
1470
1471/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001472void raw_agreement_key_policy( int policy_usage,
1473 int policy_alg,
1474 int key_type_arg,
1475 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001476 int exercise_alg,
1477 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001478{
Ronald Cron5425a212020-08-04 14:58:35 +02001479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001480 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001481 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001482 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001483 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001484 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001485
1486 PSA_ASSERT( psa_crypto_init( ) );
1487
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001488 psa_set_key_usage_flags( &attributes, policy_usage );
1489 psa_set_key_algorithm( &attributes, policy_alg );
1490 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001491
Gilles Peskine049c7532019-05-15 20:22:09 +02001492 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001493 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001494
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001495 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001496
Steven Cooremance48e852020-10-05 16:02:45 +02001497 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001498
1499exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001500 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001501 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001502 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001503}
1504/* END_CASE */
1505
1506/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001507void copy_success( int source_usage_arg,
1508 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001509 int type_arg, data_t *material,
1510 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001511 int target_usage_arg,
1512 int target_alg_arg, int target_alg2_arg,
1513 int expected_usage_arg,
1514 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001515{
Gilles Peskineca25db92019-04-19 11:43:08 +02001516 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1517 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001518 psa_key_usage_t expected_usage = expected_usage_arg;
1519 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001520 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001521 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1522 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001523 uint8_t *export_buffer = NULL;
1524
Gilles Peskine57ab7212019-01-28 13:03:09 +01001525 PSA_ASSERT( psa_crypto_init( ) );
1526
Gilles Peskineca25db92019-04-19 11:43:08 +02001527 /* Prepare the source key. */
1528 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1529 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001530 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001531 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001532 PSA_ASSERT( psa_import_key( &source_attributes,
1533 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001534 &source_key ) );
1535 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001536
Gilles Peskineca25db92019-04-19 11:43:08 +02001537 /* Prepare the target attributes. */
1538 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001539 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001540 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001541 /* Set volatile lifetime to reset the key identifier to 0. */
1542 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1543 }
1544
Gilles Peskineca25db92019-04-19 11:43:08 +02001545 if( target_usage_arg != -1 )
1546 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1547 if( target_alg_arg != -1 )
1548 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001549 if( target_alg2_arg != -1 )
1550 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001551
1552 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001553 PSA_ASSERT( psa_copy_key( source_key,
1554 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001555
1556 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001557 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001558
1559 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001560 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001561 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1562 psa_get_key_type( &target_attributes ) );
1563 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1564 psa_get_key_bits( &target_attributes ) );
1565 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1566 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001567 TEST_EQUAL( expected_alg2,
1568 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001569 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1570 {
1571 size_t length;
1572 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001573 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001574 material->len, &length ) );
1575 ASSERT_COMPARE( material->x, material->len,
1576 export_buffer, length );
1577 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001578
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001579 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001580 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001581 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001582 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001583
Ronald Cron5425a212020-08-04 14:58:35 +02001584 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001585
1586exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001587 /*
1588 * Source and target key attributes may have been returned by
1589 * psa_get_key_attributes() thus reset them as required.
1590 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001591 psa_reset_key_attributes( &source_attributes );
1592 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001593
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001594 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001595 mbedtls_free( export_buffer );
1596}
1597/* END_CASE */
1598
1599/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001600void copy_fail( int source_usage_arg,
1601 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001602 int type_arg, data_t *material,
1603 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001604 int target_usage_arg,
1605 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001606 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001607 int expected_status_arg )
1608{
1609 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1610 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001611 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1612 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001613 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001614
1615 PSA_ASSERT( psa_crypto_init( ) );
1616
1617 /* Prepare the source key. */
1618 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1619 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001620 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001621 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001622 PSA_ASSERT( psa_import_key( &source_attributes,
1623 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001624 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001625
1626 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001627 psa_set_key_id( &target_attributes, key_id );
1628 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001629 psa_set_key_type( &target_attributes, target_type_arg );
1630 psa_set_key_bits( &target_attributes, target_bits_arg );
1631 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1632 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001633 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001634
1635 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001636 TEST_EQUAL( psa_copy_key( source_key,
1637 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001638 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001639
Ronald Cron5425a212020-08-04 14:58:35 +02001640 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001641
Gilles Peskine4a644642019-05-03 17:14:08 +02001642exit:
1643 psa_reset_key_attributes( &source_attributes );
1644 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001645 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001646}
1647/* END_CASE */
1648
1649/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001650void hash_operation_init( )
1651{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001652 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001653 /* Test each valid way of initializing the object, except for `= {0}`, as
1654 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1655 * though it's OK by the C standard. We could test for this, but we'd need
1656 * to supress the Clang warning for the test. */
1657 psa_hash_operation_t func = psa_hash_operation_init( );
1658 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1659 psa_hash_operation_t zero;
1660
1661 memset( &zero, 0, sizeof( zero ) );
1662
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001663 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001664 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1665 PSA_ERROR_BAD_STATE );
1666 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1667 PSA_ERROR_BAD_STATE );
1668 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1669 PSA_ERROR_BAD_STATE );
1670
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001671 /* A default hash operation should be abortable without error. */
1672 PSA_ASSERT( psa_hash_abort( &func ) );
1673 PSA_ASSERT( psa_hash_abort( &init ) );
1674 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001675}
1676/* END_CASE */
1677
1678/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001679void hash_setup( int alg_arg,
1680 int expected_status_arg )
1681{
1682 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001683 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001684 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001685 psa_status_t status;
1686
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001688
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001689 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001690 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001691
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001692 /* Whether setup succeeded or failed, abort must succeed. */
1693 PSA_ASSERT( psa_hash_abort( &operation ) );
1694
1695 /* If setup failed, reproduce the failure, so as to
1696 * test the resulting state of the operation object. */
1697 if( status != PSA_SUCCESS )
1698 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1699
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001700 /* Now the operation object should be reusable. */
1701#if defined(KNOWN_SUPPORTED_HASH_ALG)
1702 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1703 PSA_ASSERT( psa_hash_abort( &operation ) );
1704#endif
1705
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001706exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001707 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001708}
1709/* END_CASE */
1710
1711/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001712void hash_compute_fail( int alg_arg, data_t *input,
1713 int output_size_arg, int expected_status_arg )
1714{
1715 psa_algorithm_t alg = alg_arg;
1716 uint8_t *output = NULL;
1717 size_t output_size = output_size_arg;
1718 size_t output_length = INVALID_EXPORT_LENGTH;
1719 psa_status_t expected_status = expected_status_arg;
1720 psa_status_t status;
1721
1722 ASSERT_ALLOC( output, output_size );
1723
1724 PSA_ASSERT( psa_crypto_init( ) );
1725
1726 status = psa_hash_compute( alg, input->x, input->len,
1727 output, output_size, &output_length );
1728 TEST_EQUAL( status, expected_status );
1729 TEST_ASSERT( output_length <= output_size );
1730
1731exit:
1732 mbedtls_free( output );
1733 PSA_DONE( );
1734}
1735/* END_CASE */
1736
1737/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001738void hash_compare_fail( int alg_arg, data_t *input,
1739 data_t *reference_hash,
1740 int expected_status_arg )
1741{
1742 psa_algorithm_t alg = alg_arg;
1743 psa_status_t expected_status = expected_status_arg;
1744 psa_status_t status;
1745
1746 PSA_ASSERT( psa_crypto_init( ) );
1747
1748 status = psa_hash_compare( alg, input->x, input->len,
1749 reference_hash->x, reference_hash->len );
1750 TEST_EQUAL( status, expected_status );
1751
1752exit:
1753 PSA_DONE( );
1754}
1755/* END_CASE */
1756
1757/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001758void hash_compute_compare( int alg_arg, data_t *input,
1759 data_t *expected_output )
1760{
1761 psa_algorithm_t alg = alg_arg;
1762 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1763 size_t output_length = INVALID_EXPORT_LENGTH;
1764 size_t i;
1765
1766 PSA_ASSERT( psa_crypto_init( ) );
1767
1768 /* Compute with tight buffer */
1769 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001770 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001771 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001772 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001773 ASSERT_COMPARE( output, output_length,
1774 expected_output->x, expected_output->len );
1775
1776 /* Compute with larger buffer */
1777 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1778 output, sizeof( output ),
1779 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001780 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001781 ASSERT_COMPARE( output, output_length,
1782 expected_output->x, expected_output->len );
1783
1784 /* Compare with correct hash */
1785 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1786 output, output_length ) );
1787
1788 /* Compare with trailing garbage */
1789 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1790 output, output_length + 1 ),
1791 PSA_ERROR_INVALID_SIGNATURE );
1792
1793 /* Compare with truncated hash */
1794 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1795 output, output_length - 1 ),
1796 PSA_ERROR_INVALID_SIGNATURE );
1797
1798 /* Compare with corrupted value */
1799 for( i = 0; i < output_length; i++ )
1800 {
Chris Jones9634bb12021-01-20 15:56:42 +00001801 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001802 output[i] ^= 1;
1803 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1804 output, output_length ),
1805 PSA_ERROR_INVALID_SIGNATURE );
1806 output[i] ^= 1;
1807 }
1808
1809exit:
1810 PSA_DONE( );
1811}
1812/* END_CASE */
1813
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001814/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001815void hash_bad_order( )
1816{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001817 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001818 unsigned char input[] = "";
1819 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001820 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001821 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1822 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1823 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001824 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001825 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001826 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001827
Gilles Peskine8817f612018-12-18 00:18:46 +01001828 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001829
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001830 /* Call setup twice in a row. */
1831 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1832 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1833 PSA_ERROR_BAD_STATE );
1834 PSA_ASSERT( psa_hash_abort( &operation ) );
1835
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001836 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001837 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001838 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001839 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001840
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001841 /* Call update after finish. */
1842 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1843 PSA_ASSERT( psa_hash_finish( &operation,
1844 hash, sizeof( hash ), &hash_len ) );
1845 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001846 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001847 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001848
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001849 /* Call verify without calling setup beforehand. */
1850 TEST_EQUAL( psa_hash_verify( &operation,
1851 valid_hash, sizeof( valid_hash ) ),
1852 PSA_ERROR_BAD_STATE );
1853 PSA_ASSERT( psa_hash_abort( &operation ) );
1854
1855 /* Call verify after finish. */
1856 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1857 PSA_ASSERT( psa_hash_finish( &operation,
1858 hash, sizeof( hash ), &hash_len ) );
1859 TEST_EQUAL( psa_hash_verify( &operation,
1860 valid_hash, sizeof( valid_hash ) ),
1861 PSA_ERROR_BAD_STATE );
1862 PSA_ASSERT( psa_hash_abort( &operation ) );
1863
1864 /* Call verify twice in a row. */
1865 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1866 PSA_ASSERT( psa_hash_verify( &operation,
1867 valid_hash, sizeof( valid_hash ) ) );
1868 TEST_EQUAL( psa_hash_verify( &operation,
1869 valid_hash, sizeof( valid_hash ) ),
1870 PSA_ERROR_BAD_STATE );
1871 PSA_ASSERT( psa_hash_abort( &operation ) );
1872
1873 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001874 TEST_EQUAL( psa_hash_finish( &operation,
1875 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001876 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001877 PSA_ASSERT( psa_hash_abort( &operation ) );
1878
1879 /* Call finish twice in a row. */
1880 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1881 PSA_ASSERT( psa_hash_finish( &operation,
1882 hash, sizeof( hash ), &hash_len ) );
1883 TEST_EQUAL( psa_hash_finish( &operation,
1884 hash, sizeof( hash ), &hash_len ),
1885 PSA_ERROR_BAD_STATE );
1886 PSA_ASSERT( psa_hash_abort( &operation ) );
1887
1888 /* Call finish after calling verify. */
1889 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1890 PSA_ASSERT( psa_hash_verify( &operation,
1891 valid_hash, sizeof( valid_hash ) ) );
1892 TEST_EQUAL( psa_hash_finish( &operation,
1893 hash, sizeof( hash ), &hash_len ),
1894 PSA_ERROR_BAD_STATE );
1895 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001896
1897exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001898 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001899}
1900/* END_CASE */
1901
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001902/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001903void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001904{
1905 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001906 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1907 * appended to it */
1908 unsigned char hash[] = {
1909 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1910 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1911 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001912 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001913 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001914
Gilles Peskine8817f612018-12-18 00:18:46 +01001915 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001916
itayzafrir27e69452018-11-01 14:26:34 +02001917 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001918 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001919 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001920 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001921
itayzafrir27e69452018-11-01 14:26:34 +02001922 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001923 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001924 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001925 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001926
itayzafrir27e69452018-11-01 14:26:34 +02001927 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001928 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001929 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001930 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001931
itayzafrirec93d302018-10-18 18:01:10 +03001932exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001933 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001934}
1935/* END_CASE */
1936
Ronald Cronee414c72021-03-18 18:50:08 +01001937/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001938void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001939{
1940 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001941 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001942 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001943 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001944 size_t hash_len;
1945
Gilles Peskine8817f612018-12-18 00:18:46 +01001946 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001947
itayzafrir58028322018-10-25 10:22:01 +03001948 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001950 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001951 hash, expected_size - 1, &hash_len ),
1952 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001953
1954exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001955 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001956}
1957/* END_CASE */
1958
Ronald Cronee414c72021-03-18 18:50:08 +01001959/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001960void hash_clone_source_state( )
1961{
1962 psa_algorithm_t alg = PSA_ALG_SHA_256;
1963 unsigned char hash[PSA_HASH_MAX_SIZE];
1964 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1965 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1966 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1967 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1968 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1969 size_t hash_len;
1970
1971 PSA_ASSERT( psa_crypto_init( ) );
1972 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1973
1974 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1975 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1976 PSA_ASSERT( psa_hash_finish( &op_finished,
1977 hash, sizeof( hash ), &hash_len ) );
1978 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1979 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1980
1981 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1982 PSA_ERROR_BAD_STATE );
1983
1984 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1985 PSA_ASSERT( psa_hash_finish( &op_init,
1986 hash, sizeof( hash ), &hash_len ) );
1987 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1988 PSA_ASSERT( psa_hash_finish( &op_finished,
1989 hash, sizeof( hash ), &hash_len ) );
1990 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1991 PSA_ASSERT( psa_hash_finish( &op_aborted,
1992 hash, sizeof( hash ), &hash_len ) );
1993
1994exit:
1995 psa_hash_abort( &op_source );
1996 psa_hash_abort( &op_init );
1997 psa_hash_abort( &op_setup );
1998 psa_hash_abort( &op_finished );
1999 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002000 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002001}
2002/* END_CASE */
2003
Ronald Cronee414c72021-03-18 18:50:08 +01002004/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002005void hash_clone_target_state( )
2006{
2007 psa_algorithm_t alg = PSA_ALG_SHA_256;
2008 unsigned char hash[PSA_HASH_MAX_SIZE];
2009 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2010 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2011 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2012 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2013 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2014 size_t hash_len;
2015
2016 PSA_ASSERT( psa_crypto_init( ) );
2017
2018 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2019 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2020 PSA_ASSERT( psa_hash_finish( &op_finished,
2021 hash, sizeof( hash ), &hash_len ) );
2022 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2023 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2024
2025 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2026 PSA_ASSERT( psa_hash_finish( &op_target,
2027 hash, sizeof( hash ), &hash_len ) );
2028
2029 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2030 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2031 PSA_ERROR_BAD_STATE );
2032 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2033 PSA_ERROR_BAD_STATE );
2034
2035exit:
2036 psa_hash_abort( &op_target );
2037 psa_hash_abort( &op_init );
2038 psa_hash_abort( &op_setup );
2039 psa_hash_abort( &op_finished );
2040 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002041 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002042}
2043/* END_CASE */
2044
itayzafrir58028322018-10-25 10:22:01 +03002045/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002046void mac_operation_init( )
2047{
Jaeden Amero252ef282019-02-15 14:05:35 +00002048 const uint8_t input[1] = { 0 };
2049
Jaeden Amero769ce272019-01-04 11:48:03 +00002050 /* Test each valid way of initializing the object, except for `= {0}`, as
2051 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2052 * though it's OK by the C standard. We could test for this, but we'd need
2053 * to supress the Clang warning for the test. */
2054 psa_mac_operation_t func = psa_mac_operation_init( );
2055 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2056 psa_mac_operation_t zero;
2057
2058 memset( &zero, 0, sizeof( zero ) );
2059
Jaeden Amero252ef282019-02-15 14:05:35 +00002060 /* A freshly-initialized MAC operation should not be usable. */
2061 TEST_EQUAL( psa_mac_update( &func,
2062 input, sizeof( input ) ),
2063 PSA_ERROR_BAD_STATE );
2064 TEST_EQUAL( psa_mac_update( &init,
2065 input, sizeof( input ) ),
2066 PSA_ERROR_BAD_STATE );
2067 TEST_EQUAL( psa_mac_update( &zero,
2068 input, sizeof( input ) ),
2069 PSA_ERROR_BAD_STATE );
2070
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002071 /* A default MAC operation should be abortable without error. */
2072 PSA_ASSERT( psa_mac_abort( &func ) );
2073 PSA_ASSERT( psa_mac_abort( &init ) );
2074 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002075}
2076/* END_CASE */
2077
2078/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002079void mac_setup( int key_type_arg,
2080 data_t *key,
2081 int alg_arg,
2082 int expected_status_arg )
2083{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002084 psa_key_type_t key_type = key_type_arg;
2085 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002086 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002087 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002088 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2089#if defined(KNOWN_SUPPORTED_MAC_ALG)
2090 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2091#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002092
Gilles Peskine8817f612018-12-18 00:18:46 +01002093 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002094
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002095 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2096 &operation, &status ) )
2097 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002098 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002099
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002100 /* The operation object should be reusable. */
2101#if defined(KNOWN_SUPPORTED_MAC_ALG)
2102 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2103 smoke_test_key_data,
2104 sizeof( smoke_test_key_data ),
2105 KNOWN_SUPPORTED_MAC_ALG,
2106 &operation, &status ) )
2107 goto exit;
2108 TEST_EQUAL( status, PSA_SUCCESS );
2109#endif
2110
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002111exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002112 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002113}
2114/* END_CASE */
2115
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002116/* 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 +00002117void mac_bad_order( )
2118{
Ronald Cron5425a212020-08-04 14:58:35 +02002119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002120 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2121 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002122 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002123 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2124 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2125 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002127 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2128 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2129 size_t sign_mac_length = 0;
2130 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2131 const uint8_t verify_mac[] = {
2132 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2133 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2134 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2135
2136 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002138 psa_set_key_algorithm( &attributes, alg );
2139 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002140
Ronald Cron5425a212020-08-04 14:58:35 +02002141 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2142 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002143
Jaeden Amero252ef282019-02-15 14:05:35 +00002144 /* Call update without calling setup beforehand. */
2145 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2146 PSA_ERROR_BAD_STATE );
2147 PSA_ASSERT( psa_mac_abort( &operation ) );
2148
2149 /* Call sign finish without calling setup beforehand. */
2150 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2151 &sign_mac_length),
2152 PSA_ERROR_BAD_STATE );
2153 PSA_ASSERT( psa_mac_abort( &operation ) );
2154
2155 /* Call verify finish without calling setup beforehand. */
2156 TEST_EQUAL( psa_mac_verify_finish( &operation,
2157 verify_mac, sizeof( verify_mac ) ),
2158 PSA_ERROR_BAD_STATE );
2159 PSA_ASSERT( psa_mac_abort( &operation ) );
2160
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002161 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002162 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2163 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002164 PSA_ERROR_BAD_STATE );
2165 PSA_ASSERT( psa_mac_abort( &operation ) );
2166
Jaeden Amero252ef282019-02-15 14:05:35 +00002167 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002168 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002169 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2170 PSA_ASSERT( psa_mac_sign_finish( &operation,
2171 sign_mac, sizeof( sign_mac ),
2172 &sign_mac_length ) );
2173 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2174 PSA_ERROR_BAD_STATE );
2175 PSA_ASSERT( psa_mac_abort( &operation ) );
2176
2177 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002178 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002179 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2180 PSA_ASSERT( psa_mac_verify_finish( &operation,
2181 verify_mac, sizeof( verify_mac ) ) );
2182 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2183 PSA_ERROR_BAD_STATE );
2184 PSA_ASSERT( psa_mac_abort( &operation ) );
2185
2186 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002187 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002188 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2189 PSA_ASSERT( psa_mac_sign_finish( &operation,
2190 sign_mac, sizeof( sign_mac ),
2191 &sign_mac_length ) );
2192 TEST_EQUAL( psa_mac_sign_finish( &operation,
2193 sign_mac, sizeof( sign_mac ),
2194 &sign_mac_length ),
2195 PSA_ERROR_BAD_STATE );
2196 PSA_ASSERT( psa_mac_abort( &operation ) );
2197
2198 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002199 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002200 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2201 PSA_ASSERT( psa_mac_verify_finish( &operation,
2202 verify_mac, sizeof( verify_mac ) ) );
2203 TEST_EQUAL( psa_mac_verify_finish( &operation,
2204 verify_mac, sizeof( verify_mac ) ),
2205 PSA_ERROR_BAD_STATE );
2206 PSA_ASSERT( psa_mac_abort( &operation ) );
2207
2208 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002209 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002210 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2211 TEST_EQUAL( psa_mac_verify_finish( &operation,
2212 verify_mac, sizeof( verify_mac ) ),
2213 PSA_ERROR_BAD_STATE );
2214 PSA_ASSERT( psa_mac_abort( &operation ) );
2215
2216 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002217 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002218 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2219 TEST_EQUAL( psa_mac_sign_finish( &operation,
2220 sign_mac, sizeof( sign_mac ),
2221 &sign_mac_length ),
2222 PSA_ERROR_BAD_STATE );
2223 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002224
Ronald Cron5425a212020-08-04 14:58:35 +02002225 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002226
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002227exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002228 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002229}
2230/* END_CASE */
2231
2232/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002233void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002234 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002235 int alg_arg,
2236 data_t *input,
2237 data_t *expected_mac )
2238{
Ronald Cron5425a212020-08-04 14:58:35 +02002239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002240 psa_key_type_t key_type = key_type_arg;
2241 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002242 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002243 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002244 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002245 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002246 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002247 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002248 const size_t output_sizes_to_test[] = {
2249 0,
2250 1,
2251 expected_mac->len - 1,
2252 expected_mac->len,
2253 expected_mac->len + 1,
2254 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002255
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002256 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002257 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002258 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002259
Gilles Peskine8817f612018-12-18 00:18:46 +01002260 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002261
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002262 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002263 psa_set_key_algorithm( &attributes, alg );
2264 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002265
Ronald Cron5425a212020-08-04 14:58:35 +02002266 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2267 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002268
Gilles Peskine8b356b52020-08-25 23:44:59 +02002269 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2270 {
2271 const size_t output_size = output_sizes_to_test[i];
2272 psa_status_t expected_status =
2273 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2274 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002275
Chris Jones9634bb12021-01-20 15:56:42 +00002276 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002277 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002278
Gilles Peskine8b356b52020-08-25 23:44:59 +02002279 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002280 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002281 PSA_ASSERT( psa_mac_update( &operation,
2282 input->x, input->len ) );
2283 TEST_EQUAL( psa_mac_sign_finish( &operation,
2284 actual_mac, output_size,
2285 &mac_length ),
2286 expected_status );
2287 PSA_ASSERT( psa_mac_abort( &operation ) );
2288
2289 if( expected_status == PSA_SUCCESS )
2290 {
2291 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2292 actual_mac, mac_length );
2293 }
2294 mbedtls_free( actual_mac );
2295 actual_mac = NULL;
2296 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002297
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002298exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002299 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002300 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002301 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002302 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002303}
2304/* END_CASE */
2305
2306/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002307void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002308 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002309 int alg_arg,
2310 data_t *input,
2311 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002312{
Ronald Cron5425a212020-08-04 14:58:35 +02002313 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002314 psa_key_type_t key_type = key_type_arg;
2315 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002316 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002318 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002319
Gilles Peskine69c12672018-06-28 00:07:19 +02002320 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2321
Gilles Peskine8817f612018-12-18 00:18:46 +01002322 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002323
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002324 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002325 psa_set_key_algorithm( &attributes, alg );
2326 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002327
Ronald Cron5425a212020-08-04 14:58:35 +02002328 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2329 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002330
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002331 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002332 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002333 PSA_ASSERT( psa_mac_update( &operation,
2334 input->x, input->len ) );
2335 PSA_ASSERT( psa_mac_verify_finish( &operation,
2336 expected_mac->x,
2337 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002338
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002339 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002340 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002341 PSA_ASSERT( psa_mac_update( &operation,
2342 input->x, input->len ) );
2343 TEST_EQUAL( psa_mac_verify_finish( &operation,
2344 expected_mac->x,
2345 expected_mac->len - 1 ),
2346 PSA_ERROR_INVALID_SIGNATURE );
2347
2348 /* Test a MAC that's too long. */
2349 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2350 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002351 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002352 PSA_ASSERT( psa_mac_update( &operation,
2353 input->x, input->len ) );
2354 TEST_EQUAL( psa_mac_verify_finish( &operation,
2355 perturbed_mac,
2356 expected_mac->len + 1 ),
2357 PSA_ERROR_INVALID_SIGNATURE );
2358
2359 /* Test changing one byte. */
2360 for( size_t i = 0; i < expected_mac->len; i++ )
2361 {
Chris Jones9634bb12021-01-20 15:56:42 +00002362 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002363 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002364 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002365 PSA_ASSERT( psa_mac_update( &operation,
2366 input->x, input->len ) );
2367 TEST_EQUAL( psa_mac_verify_finish( &operation,
2368 perturbed_mac,
2369 expected_mac->len ),
2370 PSA_ERROR_INVALID_SIGNATURE );
2371 perturbed_mac[i] ^= 1;
2372 }
2373
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002375 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002376 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002377 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002378 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002379}
2380/* END_CASE */
2381
2382/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002383void cipher_operation_init( )
2384{
Jaeden Ameroab439972019-02-15 14:12:05 +00002385 const uint8_t input[1] = { 0 };
2386 unsigned char output[1] = { 0 };
2387 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002388 /* Test each valid way of initializing the object, except for `= {0}`, as
2389 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2390 * though it's OK by the C standard. We could test for this, but we'd need
2391 * to supress the Clang warning for the test. */
2392 psa_cipher_operation_t func = psa_cipher_operation_init( );
2393 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2394 psa_cipher_operation_t zero;
2395
2396 memset( &zero, 0, sizeof( zero ) );
2397
Jaeden Ameroab439972019-02-15 14:12:05 +00002398 /* A freshly-initialized cipher operation should not be usable. */
2399 TEST_EQUAL( psa_cipher_update( &func,
2400 input, sizeof( input ),
2401 output, sizeof( output ),
2402 &output_length ),
2403 PSA_ERROR_BAD_STATE );
2404 TEST_EQUAL( psa_cipher_update( &init,
2405 input, sizeof( input ),
2406 output, sizeof( output ),
2407 &output_length ),
2408 PSA_ERROR_BAD_STATE );
2409 TEST_EQUAL( psa_cipher_update( &zero,
2410 input, sizeof( input ),
2411 output, sizeof( output ),
2412 &output_length ),
2413 PSA_ERROR_BAD_STATE );
2414
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002415 /* A default cipher operation should be abortable without error. */
2416 PSA_ASSERT( psa_cipher_abort( &func ) );
2417 PSA_ASSERT( psa_cipher_abort( &init ) );
2418 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002419}
2420/* END_CASE */
2421
2422/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002423void cipher_setup( int key_type_arg,
2424 data_t *key,
2425 int alg_arg,
2426 int expected_status_arg )
2427{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002428 psa_key_type_t key_type = key_type_arg;
2429 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002430 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002431 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002432 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002433#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002434 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2435#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002436
Gilles Peskine8817f612018-12-18 00:18:46 +01002437 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002438
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002439 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2440 &operation, &status ) )
2441 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002442 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002443
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002444 /* The operation object should be reusable. */
2445#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2446 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2447 smoke_test_key_data,
2448 sizeof( smoke_test_key_data ),
2449 KNOWN_SUPPORTED_CIPHER_ALG,
2450 &operation, &status ) )
2451 goto exit;
2452 TEST_EQUAL( status, PSA_SUCCESS );
2453#endif
2454
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002455exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002456 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002457 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002458}
2459/* END_CASE */
2460
Ronald Cronee414c72021-03-18 18:50:08 +01002461/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002462void cipher_bad_order( )
2463{
Ronald Cron5425a212020-08-04 14:58:35 +02002464 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002465 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2466 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002468 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002469 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002470 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002471 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2472 0xaa, 0xaa, 0xaa, 0xaa };
2473 const uint8_t text[] = {
2474 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2475 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002476 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002477 size_t length = 0;
2478
2479 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2481 psa_set_key_algorithm( &attributes, alg );
2482 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002483 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2484 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002485
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002486 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002487 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2488 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002489 PSA_ERROR_BAD_STATE );
2490 PSA_ASSERT( psa_cipher_abort( &operation ) );
2491
2492 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002493 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2494 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002495 PSA_ERROR_BAD_STATE );
2496 PSA_ASSERT( psa_cipher_abort( &operation ) );
2497
Jaeden Ameroab439972019-02-15 14:12:05 +00002498 /* Generate an IV without calling setup beforehand. */
2499 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2500 buffer, sizeof( buffer ),
2501 &length ),
2502 PSA_ERROR_BAD_STATE );
2503 PSA_ASSERT( psa_cipher_abort( &operation ) );
2504
2505 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002506 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002507 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2508 buffer, sizeof( buffer ),
2509 &length ) );
2510 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2511 buffer, sizeof( buffer ),
2512 &length ),
2513 PSA_ERROR_BAD_STATE );
2514 PSA_ASSERT( psa_cipher_abort( &operation ) );
2515
2516 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002517 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002518 PSA_ASSERT( psa_cipher_set_iv( &operation,
2519 iv, sizeof( iv ) ) );
2520 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2521 buffer, sizeof( buffer ),
2522 &length ),
2523 PSA_ERROR_BAD_STATE );
2524 PSA_ASSERT( psa_cipher_abort( &operation ) );
2525
2526 /* Set an IV without calling setup beforehand. */
2527 TEST_EQUAL( psa_cipher_set_iv( &operation,
2528 iv, sizeof( iv ) ),
2529 PSA_ERROR_BAD_STATE );
2530 PSA_ASSERT( psa_cipher_abort( &operation ) );
2531
2532 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002533 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002534 PSA_ASSERT( psa_cipher_set_iv( &operation,
2535 iv, sizeof( iv ) ) );
2536 TEST_EQUAL( psa_cipher_set_iv( &operation,
2537 iv, sizeof( iv ) ),
2538 PSA_ERROR_BAD_STATE );
2539 PSA_ASSERT( psa_cipher_abort( &operation ) );
2540
2541 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002542 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002543 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2544 buffer, sizeof( buffer ),
2545 &length ) );
2546 TEST_EQUAL( psa_cipher_set_iv( &operation,
2547 iv, sizeof( iv ) ),
2548 PSA_ERROR_BAD_STATE );
2549 PSA_ASSERT( psa_cipher_abort( &operation ) );
2550
2551 /* Call update without calling setup beforehand. */
2552 TEST_EQUAL( psa_cipher_update( &operation,
2553 text, sizeof( text ),
2554 buffer, sizeof( buffer ),
2555 &length ),
2556 PSA_ERROR_BAD_STATE );
2557 PSA_ASSERT( psa_cipher_abort( &operation ) );
2558
2559 /* Call update without an IV where an IV is required. */
2560 TEST_EQUAL( psa_cipher_update( &operation,
2561 text, sizeof( text ),
2562 buffer, sizeof( buffer ),
2563 &length ),
2564 PSA_ERROR_BAD_STATE );
2565 PSA_ASSERT( psa_cipher_abort( &operation ) );
2566
2567 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002568 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002569 PSA_ASSERT( psa_cipher_set_iv( &operation,
2570 iv, sizeof( iv ) ) );
2571 PSA_ASSERT( psa_cipher_finish( &operation,
2572 buffer, sizeof( buffer ), &length ) );
2573 TEST_EQUAL( psa_cipher_update( &operation,
2574 text, sizeof( text ),
2575 buffer, sizeof( buffer ),
2576 &length ),
2577 PSA_ERROR_BAD_STATE );
2578 PSA_ASSERT( psa_cipher_abort( &operation ) );
2579
2580 /* Call finish without calling setup beforehand. */
2581 TEST_EQUAL( psa_cipher_finish( &operation,
2582 buffer, sizeof( buffer ), &length ),
2583 PSA_ERROR_BAD_STATE );
2584 PSA_ASSERT( psa_cipher_abort( &operation ) );
2585
2586 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002587 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002588 /* Not calling update means we are encrypting an empty buffer, which is OK
2589 * for cipher modes with padding. */
2590 TEST_EQUAL( psa_cipher_finish( &operation,
2591 buffer, sizeof( buffer ), &length ),
2592 PSA_ERROR_BAD_STATE );
2593 PSA_ASSERT( psa_cipher_abort( &operation ) );
2594
2595 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002596 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002597 PSA_ASSERT( psa_cipher_set_iv( &operation,
2598 iv, sizeof( iv ) ) );
2599 PSA_ASSERT( psa_cipher_finish( &operation,
2600 buffer, sizeof( buffer ), &length ) );
2601 TEST_EQUAL( psa_cipher_finish( &operation,
2602 buffer, sizeof( buffer ), &length ),
2603 PSA_ERROR_BAD_STATE );
2604 PSA_ASSERT( psa_cipher_abort( &operation ) );
2605
Ronald Cron5425a212020-08-04 14:58:35 +02002606 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002607
Jaeden Ameroab439972019-02-15 14:12:05 +00002608exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002609 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002610 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002611}
2612/* END_CASE */
2613
2614/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002615void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002616 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002617 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002618 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002619{
Ronald Cron5425a212020-08-04 14:58:35 +02002620 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002621 psa_status_t status;
2622 psa_key_type_t key_type = key_type_arg;
2623 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002624 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002625 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002626 size_t output_buffer_size = 0;
2627 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002628 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002629 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002630 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002631
Gilles Peskine8817f612018-12-18 00:18:46 +01002632 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002633
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002634 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2635 psa_set_key_algorithm( &attributes, alg );
2636 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002637
Ronald Cron5425a212020-08-04 14:58:35 +02002638 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2639 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002640
Ronald Cron5425a212020-08-04 14:58:35 +02002641 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002643 if( iv->len > 0 )
2644 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002645 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002646 }
2647
gabor-mezei-armceface22021-01-21 12:26:17 +01002648 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2649 TEST_ASSERT( output_buffer_size <=
2650 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002651 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652
Gilles Peskine8817f612018-12-18 00:18:46 +01002653 PSA_ASSERT( psa_cipher_update( &operation,
2654 input->x, input->len,
2655 output, output_buffer_size,
2656 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002657 TEST_ASSERT( function_output_length <=
2658 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2659 TEST_ASSERT( function_output_length <=
2660 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002661 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002662
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002664 ( output_buffer_size == 0 ? NULL :
2665 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002666 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002667 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002668 TEST_ASSERT( function_output_length <=
2669 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2670 TEST_ASSERT( function_output_length <=
2671 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002672 total_output_length += function_output_length;
2673
Gilles Peskinefe11b722018-12-18 00:24:04 +01002674 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675 if( expected_status == PSA_SUCCESS )
2676 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002677 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002678 ASSERT_COMPARE( expected_output->x, expected_output->len,
2679 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002680 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002681
Gilles Peskine50e586b2018-06-08 14:28:46 +02002682exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002683 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002684 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002685 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002686 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002687}
2688/* END_CASE */
2689
2690/* BEGIN_CASE */
2691void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002692 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002693 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002694 int first_part_size_arg,
2695 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002696 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002697{
Ronald Cron5425a212020-08-04 14:58:35 +02002698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002699 psa_key_type_t key_type = key_type_arg;
2700 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002701 size_t first_part_size = first_part_size_arg;
2702 size_t output1_length = output1_length_arg;
2703 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002704 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002705 size_t output_buffer_size = 0;
2706 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002707 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002708 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710
Gilles Peskine8817f612018-12-18 00:18:46 +01002711 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002712
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002713 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2714 psa_set_key_algorithm( &attributes, alg );
2715 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002716
Ronald Cron5425a212020-08-04 14:58:35 +02002717 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2718 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002719
Ronald Cron5425a212020-08-04 14:58:35 +02002720 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002721
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002722 if( iv->len > 0 )
2723 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002724 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002725 }
2726
gabor-mezei-armceface22021-01-21 12:26:17 +01002727 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2728 TEST_ASSERT( output_buffer_size <=
2729 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002730 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002731
Gilles Peskinee0866522019-02-19 19:44:00 +01002732 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002733 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2734 output, output_buffer_size,
2735 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002736 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002737 TEST_ASSERT( function_output_length <=
2738 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2739 TEST_ASSERT( function_output_length <=
2740 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002741 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002742
Gilles Peskine8817f612018-12-18 00:18:46 +01002743 PSA_ASSERT( psa_cipher_update( &operation,
2744 input->x + first_part_size,
2745 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002746 ( output_buffer_size == 0 ? NULL :
2747 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002748 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002749 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002750 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002751 TEST_ASSERT( function_output_length <=
2752 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2753 alg,
2754 input->len - first_part_size ) );
2755 TEST_ASSERT( function_output_length <=
2756 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002757 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002758
Gilles Peskine8817f612018-12-18 00:18:46 +01002759 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002760 ( output_buffer_size == 0 ? NULL :
2761 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002762 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002763 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002764 TEST_ASSERT( function_output_length <=
2765 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2766 TEST_ASSERT( function_output_length <=
2767 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002768 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002770
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002771 ASSERT_COMPARE( expected_output->x, expected_output->len,
2772 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002773
2774exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002775 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002776 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002777 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002778 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002779}
2780/* END_CASE */
2781
2782/* BEGIN_CASE */
2783void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002784 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002785 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002786 int first_part_size_arg,
2787 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002788 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002789{
Ronald Cron5425a212020-08-04 14:58:35 +02002790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002791 psa_key_type_t key_type = key_type_arg;
2792 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002793 size_t first_part_size = first_part_size_arg;
2794 size_t output1_length = output1_length_arg;
2795 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002796 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002797 size_t output_buffer_size = 0;
2798 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002799 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002800 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002801 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002802
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002804
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002805 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2806 psa_set_key_algorithm( &attributes, alg );
2807 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002808
Ronald Cron5425a212020-08-04 14:58:35 +02002809 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2810 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002811
Ronald Cron5425a212020-08-04 14:58:35 +02002812 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002813
Steven Cooreman177deba2020-09-07 17:14:14 +02002814 if( iv->len > 0 )
2815 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002816 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002817 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818
gabor-mezei-armceface22021-01-21 12:26:17 +01002819 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2820 TEST_ASSERT( output_buffer_size <=
2821 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002822 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823
Gilles Peskinee0866522019-02-19 19:44:00 +01002824 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002825 PSA_ASSERT( psa_cipher_update( &operation,
2826 input->x, first_part_size,
2827 output, output_buffer_size,
2828 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002829 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002830 TEST_ASSERT( function_output_length <=
2831 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2832 TEST_ASSERT( function_output_length <=
2833 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002834 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002835
Gilles Peskine8817f612018-12-18 00:18:46 +01002836 PSA_ASSERT( psa_cipher_update( &operation,
2837 input->x + first_part_size,
2838 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002839 ( output_buffer_size == 0 ? NULL :
2840 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002841 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002843 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002844 TEST_ASSERT( function_output_length <=
2845 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2846 alg,
2847 input->len - first_part_size ) );
2848 TEST_ASSERT( function_output_length <=
2849 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002850 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002851
Gilles Peskine8817f612018-12-18 00:18:46 +01002852 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002853 ( output_buffer_size == 0 ? NULL :
2854 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002855 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002856 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002857 TEST_ASSERT( function_output_length <=
2858 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2859 TEST_ASSERT( function_output_length <=
2860 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002861 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002862 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002863
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002864 ASSERT_COMPARE( expected_output->x, expected_output->len,
2865 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002866
2867exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002868 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002869 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002870 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002871 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002872}
2873/* END_CASE */
2874
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875/* BEGIN_CASE */
2876void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002877 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002878 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002879 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002880{
Ronald Cron5425a212020-08-04 14:58:35 +02002881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002882 psa_status_t status;
2883 psa_key_type_t key_type = key_type_arg;
2884 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002885 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002886 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002887 size_t output_buffer_size = 0;
2888 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002889 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002890 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892
Gilles Peskine8817f612018-12-18 00:18:46 +01002893 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002894
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2896 psa_set_key_algorithm( &attributes, alg );
2897 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002898
Ronald Cron5425a212020-08-04 14:58:35 +02002899 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2900 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901
Ronald Cron5425a212020-08-04 14:58:35 +02002902 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903
Steven Cooreman177deba2020-09-07 17:14:14 +02002904 if( iv->len > 0 )
2905 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002906 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002907 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908
gabor-mezei-armceface22021-01-21 12:26:17 +01002909 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2910 TEST_ASSERT( output_buffer_size <=
2911 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002912 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913
Gilles Peskine8817f612018-12-18 00:18:46 +01002914 PSA_ASSERT( psa_cipher_update( &operation,
2915 input->x, input->len,
2916 output, output_buffer_size,
2917 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002918 TEST_ASSERT( function_output_length <=
2919 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2920 TEST_ASSERT( function_output_length <=
2921 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002922 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002923
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002925 ( output_buffer_size == 0 ? NULL :
2926 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002927 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002928 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002929 TEST_ASSERT( function_output_length <=
2930 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2931 TEST_ASSERT( function_output_length <=
2932 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002933 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002934 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002935
2936 if( expected_status == PSA_SUCCESS )
2937 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002938 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002939 ASSERT_COMPARE( expected_output->x, expected_output->len,
2940 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002941 }
2942
Gilles Peskine50e586b2018-06-08 14:28:46 +02002943exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002944 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002945 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002946 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002947 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002948}
2949/* END_CASE */
2950
Gilles Peskine50e586b2018-06-08 14:28:46 +02002951/* BEGIN_CASE */
2952void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002953 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002954 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002955{
Ronald Cron5425a212020-08-04 14:58:35 +02002956 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002957 psa_key_type_t key_type = key_type_arg;
2958 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002959 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002960 size_t iv_size = 16;
2961 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002962 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002963 size_t output1_size = 0;
2964 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002965 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002966 size_t output2_size = 0;
2967 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002968 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002969 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2970 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002971 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002972
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002974
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002975 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2976 psa_set_key_algorithm( &attributes, alg );
2977 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002978
Ronald Cron5425a212020-08-04 14:58:35 +02002979 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2980 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981
Ronald Cron5425a212020-08-04 14:58:35 +02002982 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2983 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002984
Steven Cooreman177deba2020-09-07 17:14:14 +02002985 if( alg != PSA_ALG_ECB_NO_PADDING )
2986 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002987 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2988 iv, iv_size,
2989 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002990 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002991 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2992 TEST_ASSERT( output1_size <=
2993 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002994 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002995
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2997 output1, output1_size,
2998 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002999 TEST_ASSERT( output1_length <=
3000 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3001 TEST_ASSERT( output1_length <=
3002 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3003
Gilles Peskine8817f612018-12-18 00:18:46 +01003004 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003005 output1 + output1_length,
3006 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003007 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003008 TEST_ASSERT( function_output_length <=
3009 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3010 TEST_ASSERT( function_output_length <=
3011 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003012
Gilles Peskine048b7f02018-06-08 14:20:49 +02003013 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003014
Gilles Peskine8817f612018-12-18 00:18:46 +01003015 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003016
3017 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003018 TEST_ASSERT( output2_size <=
3019 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3020 TEST_ASSERT( output2_size <=
3021 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003022 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003023
Steven Cooreman177deba2020-09-07 17:14:14 +02003024 if( iv_length > 0 )
3025 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003026 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3027 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003028 }
3029
Gilles Peskine8817f612018-12-18 00:18:46 +01003030 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3031 output2, output2_size,
3032 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003033 TEST_ASSERT( output2_length <=
3034 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3035 TEST_ASSERT( output2_length <=
3036 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3037
Gilles Peskine048b7f02018-06-08 14:20:49 +02003038 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003039 PSA_ASSERT( psa_cipher_finish( &operation2,
3040 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003041 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003042 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003043 TEST_ASSERT( function_output_length <=
3044 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3045 TEST_ASSERT( function_output_length <=
3046 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003047
Gilles Peskine048b7f02018-06-08 14:20:49 +02003048 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003049
Gilles Peskine8817f612018-12-18 00:18:46 +01003050 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003051
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003052 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003053
3054exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003055 psa_cipher_abort( &operation1 );
3056 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003057 mbedtls_free( output1 );
3058 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003059 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003060 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003061}
3062/* END_CASE */
3063
3064/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003065void cipher_verify_output_multipart( int alg_arg,
3066 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003067 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003068 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003069 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003070{
Ronald Cron5425a212020-08-04 14:58:35 +02003071 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003072 psa_key_type_t key_type = key_type_arg;
3073 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003074 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003075 unsigned char iv[16] = {0};
3076 size_t iv_size = 16;
3077 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003078 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003079 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003080 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003081 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003082 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003083 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003084 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003085 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3086 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003090
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003091 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3092 psa_set_key_algorithm( &attributes, alg );
3093 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003094
Ronald Cron5425a212020-08-04 14:58:35 +02003095 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3096 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003097
Ronald Cron5425a212020-08-04 14:58:35 +02003098 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3099 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003100
Steven Cooreman177deba2020-09-07 17:14:14 +02003101 if( alg != PSA_ALG_ECB_NO_PADDING )
3102 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003103 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3104 iv, iv_size,
3105 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003106 }
3107
gabor-mezei-armceface22021-01-21 12:26:17 +01003108 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3109 TEST_ASSERT( output1_buffer_size <=
3110 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003111 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003112
Gilles Peskinee0866522019-02-19 19:44:00 +01003113 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003114
Gilles Peskine8817f612018-12-18 00:18:46 +01003115 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3116 output1, output1_buffer_size,
3117 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003118 TEST_ASSERT( function_output_length <=
3119 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3120 TEST_ASSERT( function_output_length <=
3121 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003122 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003123
Gilles Peskine8817f612018-12-18 00:18:46 +01003124 PSA_ASSERT( psa_cipher_update( &operation1,
3125 input->x + first_part_size,
3126 input->len - first_part_size,
3127 output1, output1_buffer_size,
3128 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003129 TEST_ASSERT( function_output_length <=
3130 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3131 alg,
3132 input->len - first_part_size ) );
3133 TEST_ASSERT( function_output_length <=
3134 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003135 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003136
Gilles Peskine8817f612018-12-18 00:18:46 +01003137 PSA_ASSERT( psa_cipher_finish( &operation1,
3138 output1 + output1_length,
3139 output1_buffer_size - output1_length,
3140 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003141 TEST_ASSERT( function_output_length <=
3142 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3143 TEST_ASSERT( function_output_length <=
3144 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003145 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003146
Gilles Peskine8817f612018-12-18 00:18:46 +01003147 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003148
Gilles Peskine048b7f02018-06-08 14:20:49 +02003149 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003150 TEST_ASSERT( output2_buffer_size <=
3151 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3152 TEST_ASSERT( output2_buffer_size <=
3153 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003154 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003155
Steven Cooreman177deba2020-09-07 17:14:14 +02003156 if( iv_length > 0 )
3157 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003158 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3159 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003160 }
Moran Pekerded84402018-06-06 16:36:50 +03003161
Gilles Peskine8817f612018-12-18 00:18:46 +01003162 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3163 output2, output2_buffer_size,
3164 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003165 TEST_ASSERT( function_output_length <=
3166 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3167 TEST_ASSERT( function_output_length <=
3168 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003169 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003170
Gilles Peskine8817f612018-12-18 00:18:46 +01003171 PSA_ASSERT( psa_cipher_update( &operation2,
3172 output1 + first_part_size,
3173 output1_length - first_part_size,
3174 output2, output2_buffer_size,
3175 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003176 TEST_ASSERT( function_output_length <=
3177 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3178 alg,
3179 output1_length - first_part_size ) );
3180 TEST_ASSERT( function_output_length <=
3181 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003182 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003183
Gilles Peskine8817f612018-12-18 00:18:46 +01003184 PSA_ASSERT( psa_cipher_finish( &operation2,
3185 output2 + output2_length,
3186 output2_buffer_size - output2_length,
3187 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003188 TEST_ASSERT( function_output_length <=
3189 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3190 TEST_ASSERT( function_output_length <=
3191 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003192 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003193
Gilles Peskine8817f612018-12-18 00:18:46 +01003194 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003195
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003196 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003197
3198exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003199 psa_cipher_abort( &operation1 );
3200 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003201 mbedtls_free( output1 );
3202 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003203 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003204 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003205}
3206/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003207
Gilles Peskine20035e32018-02-03 22:44:14 +01003208/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003209void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003210 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003211 data_t *nonce,
3212 data_t *additional_data,
3213 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003214 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003215{
Ronald Cron5425a212020-08-04 14:58:35 +02003216 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003217 psa_key_type_t key_type = key_type_arg;
3218 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003219 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003220 unsigned char *output_data = NULL;
3221 size_t output_size = 0;
3222 size_t output_length = 0;
3223 unsigned char *output_data2 = NULL;
3224 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003225 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003226 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003227 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003228
Gilles Peskine8817f612018-12-18 00:18:46 +01003229 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003230
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003231 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3232 psa_set_key_algorithm( &attributes, alg );
3233 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003234
Gilles Peskine049c7532019-05-15 20:22:09 +02003235 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003236 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003237 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3238 key_bits = psa_get_key_bits( &attributes );
3239
3240 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3241 alg );
3242 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3243 * should be exact. */
3244 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3245 expected_result != PSA_ERROR_NOT_SUPPORTED )
3246 {
3247 TEST_EQUAL( output_size,
3248 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3249 TEST_ASSERT( output_size <=
3250 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3251 }
3252 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003253
Steven Cooremanf49478b2021-02-15 15:19:25 +01003254 status = psa_aead_encrypt( key, alg,
3255 nonce->x, nonce->len,
3256 additional_data->x,
3257 additional_data->len,
3258 input_data->x, input_data->len,
3259 output_data, output_size,
3260 &output_length );
3261
3262 /* If the operation is not supported, just skip and not fail in case the
3263 * encryption involves a common limitation of cryptography hardwares and
3264 * an alternative implementation. */
3265 if( status == PSA_ERROR_NOT_SUPPORTED )
3266 {
3267 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3268 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3269 }
3270
3271 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003272
3273 if( PSA_SUCCESS == expected_result )
3274 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003275 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003276
Gilles Peskine003a4a92019-05-14 16:09:40 +02003277 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3278 * should be exact. */
3279 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003280 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003281
gabor-mezei-armceface22021-01-21 12:26:17 +01003282 TEST_ASSERT( input_data->len <=
3283 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3284
Ronald Cron5425a212020-08-04 14:58:35 +02003285 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003286 nonce->x, nonce->len,
3287 additional_data->x,
3288 additional_data->len,
3289 output_data, output_length,
3290 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003291 &output_length2 ),
3292 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003293
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003294 ASSERT_COMPARE( input_data->x, input_data->len,
3295 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003296 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003297
Gilles Peskinea1cac842018-06-11 19:33:02 +02003298exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003299 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003300 mbedtls_free( output_data );
3301 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003302 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003303}
3304/* END_CASE */
3305
3306/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003307void aead_encrypt( int key_type_arg, data_t *key_data,
3308 int alg_arg,
3309 data_t *nonce,
3310 data_t *additional_data,
3311 data_t *input_data,
3312 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003313{
Ronald Cron5425a212020-08-04 14:58:35 +02003314 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003315 psa_key_type_t key_type = key_type_arg;
3316 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003317 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003318 unsigned char *output_data = NULL;
3319 size_t output_size = 0;
3320 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003321 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003322 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003323
Gilles Peskine8817f612018-12-18 00:18:46 +01003324 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003325
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003326 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3327 psa_set_key_algorithm( &attributes, alg );
3328 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329
Gilles Peskine049c7532019-05-15 20:22:09 +02003330 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003331 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003332 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3333 key_bits = psa_get_key_bits( &attributes );
3334
3335 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3336 alg );
3337 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3338 * should be exact. */
3339 TEST_EQUAL( output_size,
3340 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3341 TEST_ASSERT( output_size <=
3342 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3343 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344
Steven Cooremand588ea12021-01-11 19:36:04 +01003345 status = psa_aead_encrypt( key, alg,
3346 nonce->x, nonce->len,
3347 additional_data->x, additional_data->len,
3348 input_data->x, input_data->len,
3349 output_data, output_size,
3350 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351
Ronald Cron28a45ed2021-02-09 20:35:42 +01003352 /* If the operation is not supported, just skip and not fail in case the
3353 * encryption involves a common limitation of cryptography hardwares and
3354 * an alternative implementation. */
3355 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003356 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003357 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3358 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003359 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003360
3361 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003362 ASSERT_COMPARE( expected_result->x, expected_result->len,
3363 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003364
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003366 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003367 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003368 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003369}
3370/* END_CASE */
3371
3372/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003373void aead_decrypt( int key_type_arg, data_t *key_data,
3374 int alg_arg,
3375 data_t *nonce,
3376 data_t *additional_data,
3377 data_t *input_data,
3378 data_t *expected_data,
3379 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003380{
Ronald Cron5425a212020-08-04 14:58:35 +02003381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003382 psa_key_type_t key_type = key_type_arg;
3383 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003384 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385 unsigned char *output_data = NULL;
3386 size_t output_size = 0;
3387 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003388 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003389 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003390 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003391
Gilles Peskine8817f612018-12-18 00:18:46 +01003392 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003393
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003394 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3395 psa_set_key_algorithm( &attributes, alg );
3396 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003397
Gilles Peskine049c7532019-05-15 20:22:09 +02003398 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003399 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003400 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3401 key_bits = psa_get_key_bits( &attributes );
3402
3403 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3404 alg );
3405 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3406 expected_result != PSA_ERROR_NOT_SUPPORTED )
3407 {
3408 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3409 * should be exact. */
3410 TEST_EQUAL( output_size,
3411 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3412 TEST_ASSERT( output_size <=
3413 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3414 }
3415 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003416
Steven Cooremand588ea12021-01-11 19:36:04 +01003417 status = psa_aead_decrypt( key, alg,
3418 nonce->x, nonce->len,
3419 additional_data->x,
3420 additional_data->len,
3421 input_data->x, input_data->len,
3422 output_data, output_size,
3423 &output_length );
3424
Ronald Cron28a45ed2021-02-09 20:35:42 +01003425 /* If the operation is not supported, just skip and not fail in case the
3426 * decryption involves a common limitation of cryptography hardwares and
3427 * an alternative implementation. */
3428 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003429 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003430 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3431 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003432 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003433
3434 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003435
Gilles Peskine2d277862018-06-18 15:41:12 +02003436 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003437 ASSERT_COMPARE( expected_data->x, expected_data->len,
3438 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003439
Gilles Peskinea1cac842018-06-11 19:33:02 +02003440exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003441 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003443 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444}
3445/* END_CASE */
3446
3447/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003448void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3449 int alg_arg,
3450 data_t *nonce,
3451 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003452 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003453 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003454 int do_test_data_chunked,
3455 int do_set_lengths,
3456 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003457{
Paul Elliottd3f82412021-06-16 16:52:21 +01003458 size_t ad_part_len = 0;
3459 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003460
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003461 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3462
3463 /* Temporary whilst we have algorithms that cannot support chunking */
3464 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003465 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003466 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3467 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003468 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003469 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003470
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003471 if( !aead_multipart_internal_func( key_type_arg, key_data,
3472 alg_arg, nonce,
3473 additional_data,
3474 ad_part_len,
3475 input_data, -1,
3476 do_set_lengths,
3477 expected_output,
3478 1, 1 ) )
3479 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003480 }
3481 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003482
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003483 /* Temporary whilst we have algorithms that cannot support chunking */
3484 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003485 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003486 for( data_part_len = 1; data_part_len <= input_data->len;
3487 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003488 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003489 mbedtls_test_set_step( 1000 + data_part_len );
3490
3491 if( !aead_multipart_internal_func( key_type_arg, key_data,
3492 alg_arg, nonce,
3493 additional_data, -1,
3494 input_data, data_part_len,
3495 do_set_lengths,
3496 expected_output,
3497 1, 1 ) )
3498 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003499 }
3500 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003501
Paul Elliott0023e0a2021-04-27 10:06:22 +01003502
Paul Elliott8fc45162021-06-23 16:06:01 +01003503 /* Goto is required to silence warnings about unused labels, as we
3504 * don't actually do any test assertions in this function. */
3505 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003506}
3507/* END_CASE */
3508
3509/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003510void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3511 int alg_arg,
3512 data_t *nonce,
3513 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003514 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003515 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003516 int do_test_data_chunked,
3517 int do_set_lengths,
3518 data_t *expected_output,
3519 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003520{
Paul Elliottd3f82412021-06-16 16:52:21 +01003521 size_t ad_part_len = 0;
3522 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003523
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003524 /* Temporary whilst we have algorithms that cannot support chunking */
3525 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003526 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003527 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3528 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003529 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003530 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003531
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003532 if( !aead_multipart_internal_func( key_type_arg, key_data,
3533 alg_arg, nonce,
3534 additional_data,
3535 ad_part_len,
3536 input_data, -1,
3537 do_set_lengths,
3538 expected_output,
3539 expect_valid_signature,
3540 0 ) )
3541 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003542 }
3543 }
3544
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003545 /* Temporary whilst we have algorithms that cannot support chunking */
3546 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003547 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003548 for( data_part_len = 1; data_part_len <= input_data->len;
3549 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003551 mbedtls_test_set_step( 1000 + data_part_len );
3552
3553 if( !aead_multipart_internal_func( key_type_arg, key_data,
3554 alg_arg, nonce,
3555 additional_data, -1,
3556 input_data, data_part_len,
3557 do_set_lengths,
3558 expected_output,
3559 expect_valid_signature,
3560 0 ) )
3561 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003562 }
3563 }
3564
Paul Elliott8fc45162021-06-23 16:06:01 +01003565 /* Goto is required to silence warnings about unused labels, as we
3566 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003567 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003568}
3569/* END_CASE */
3570
3571/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003572void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3573 int alg_arg,
3574 int nonce_len,
Paul Elliottd85f5472021-07-16 18:20:16 +01003575 int expected_generated_len_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003576 data_t *additional_data,
3577 data_t *input_data,
3578 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003579{
3580
3581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3582 psa_key_type_t key_type = key_type_arg;
3583 psa_algorithm_t alg = alg_arg;
3584 psa_aead_operation_t operation;
3585 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3587 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3588 size_t nonce_generated_len = 0;
Paul Elliottd85f5472021-07-16 18:20:16 +01003589 size_t expected_generated_len = expected_generated_len_arg;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003590 unsigned char *output_data = NULL;
3591 unsigned char *final_data = NULL;
3592 size_t output_size = 0;
3593 size_t finish_output_size = 0;
3594 size_t output_length = 0;
3595 size_t tag_length = 0;
3596 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003597
3598 PSA_ASSERT( psa_crypto_init( ) );
3599
3600 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3601 psa_set_key_algorithm( & attributes, alg );
3602 psa_set_key_type( & attributes, key_type );
3603
3604 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3605 &key ) );
3606
3607 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3608
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003609 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3610
3611 ASSERT_ALLOC( output_data, output_size );
3612
3613 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3614
3615 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3616
3617 ASSERT_ALLOC( final_data, finish_output_size );
3618
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003619 operation = psa_aead_operation_init( );
3620
3621 status = psa_aead_encrypt_setup( &operation, key, alg );
3622
3623 /* If the operation is not supported, just skip and not fail in case the
3624 * encryption involves a common limitation of cryptography hardwares and
3625 * an alternative implementation. */
3626 if( status == PSA_ERROR_NOT_SUPPORTED )
3627 {
3628 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3629 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len );
3630 }
3631
3632 PSA_ASSERT( status );
3633
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003634 status = psa_aead_generate_nonce( &operation, nonce_buffer,
3635 nonce_len,
3636 &nonce_generated_len );
3637
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003638 TEST_ASSERT( status == expected_status_arg );
3639
Paul Elliottd85f5472021-07-16 18:20:16 +01003640 TEST_EQUAL( nonce_generated_len, expected_generated_len );
3641
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003642 TEST_ASSERT( nonce_generated_len < PSA_AEAD_NONCE_MAX_SIZE );
3643
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003644 if( expected_status_arg == PSA_SUCCESS )
3645 {
3646
3647 /* Ensure we can still complete operation. */
3648
3649 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3650 additional_data->len ) );
3651
3652 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
3653 output_data, output_size, &output_length ) );
3654
3655 PSA_ASSERT( psa_aead_finish( &operation, final_data, finish_output_size,
3656 &output_length, tag_buffer,
3657 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3658 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003659
3660exit:
3661 psa_destroy_key( key );
Paul Elliott16906f92021-06-24 09:57:01 +01003662 mbedtls_free( output_data );
3663 mbedtls_free( final_data );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003664 psa_aead_abort( &operation );
3665 PSA_DONE( );
3666}
3667/* END_CASE */
3668
3669/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01003670void aead_multipart_state_test( int key_type_arg, data_t *key_data,
3671 int alg_arg,
3672 data_t *nonce,
3673 data_t *additional_data,
3674 data_t *input_data )
3675{
3676 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3677 psa_key_type_t key_type = key_type_arg;
3678 psa_algorithm_t alg = alg_arg;
3679 psa_aead_operation_t operation;
3680 unsigned char *output_data = NULL;
3681 unsigned char *final_data = NULL;
3682 size_t output_size = 0;
3683 size_t finish_output_size = 0;
3684 size_t output_length = 0;
3685 size_t key_bits = 0;
3686 size_t tag_length = 0;
3687 size_t tag_size = 0;
3688 size_t nonce_length = 0;
3689 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3690 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3691 size_t output_part_length = 0;
3692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3693
3694 PSA_ASSERT( psa_crypto_init( ) );
3695
3696 psa_set_key_usage_flags( & attributes,
3697 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3698 psa_set_key_algorithm( & attributes, alg );
3699 psa_set_key_type( & attributes, key_type );
3700
3701 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3702 &key ) );
3703
3704 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3705 key_bits = psa_get_key_bits( &attributes );
3706
3707 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
3708
3709 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
3710
3711 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3712
3713 ASSERT_ALLOC( output_data, output_size );
3714
3715 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3716
3717 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3718
3719 ASSERT_ALLOC( final_data, finish_output_size );
3720
3721 /* Test all operations error without calling setup first. */
3722
3723 operation = psa_aead_operation_init( );
3724
3725 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3726 PSA_ERROR_BAD_STATE );
3727
3728 psa_aead_abort( &operation );
3729
3730 operation = psa_aead_operation_init( );
3731
3732 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3733 PSA_AEAD_NONCE_MAX_SIZE,
3734 &nonce_length ),
3735 PSA_ERROR_BAD_STATE );
3736
3737 psa_aead_abort( &operation );
3738
Paul Elliott481be342021-07-16 17:38:47 +01003739 /* ------------------------------------------------------- */
3740
Paul Elliottc23a9a02021-06-21 18:32:46 +01003741 operation = psa_aead_operation_init( );
3742
3743 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
3744 input_data->len ),
3745 PSA_ERROR_BAD_STATE );
3746
3747 psa_aead_abort( &operation );
3748
Paul Elliott481be342021-07-16 17:38:47 +01003749 /* ------------------------------------------------------- */
3750
Paul Elliottc23a9a02021-06-21 18:32:46 +01003751 operation = psa_aead_operation_init( );
3752
3753 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3754 additional_data->len ),
3755 PSA_ERROR_BAD_STATE );
3756
3757 psa_aead_abort( &operation );
3758
Paul Elliott481be342021-07-16 17:38:47 +01003759 /* ------------------------------------------------------- */
3760
Paul Elliottc23a9a02021-06-21 18:32:46 +01003761 operation = psa_aead_operation_init( );
3762
3763 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
3764 input_data->len, output_data,
3765 output_size, &output_length ),
3766 PSA_ERROR_BAD_STATE );
3767
3768 psa_aead_abort( &operation );
3769
Paul Elliott481be342021-07-16 17:38:47 +01003770 /* ------------------------------------------------------- */
3771
Paul Elliottc23a9a02021-06-21 18:32:46 +01003772 operation = psa_aead_operation_init( );
3773
3774 TEST_EQUAL( psa_aead_finish( &operation, final_data,
3775 finish_output_size,
3776 &output_part_length,
3777 tag_buffer, tag_length,
3778 &tag_size ),
3779 PSA_ERROR_BAD_STATE );
3780
3781 psa_aead_abort( &operation );
3782
Paul Elliott481be342021-07-16 17:38:47 +01003783 /* ------------------------------------------------------- */
3784
Paul Elliottc23a9a02021-06-21 18:32:46 +01003785 operation = psa_aead_operation_init( );
3786
3787 TEST_EQUAL( psa_aead_verify( &operation, final_data,
3788 finish_output_size,
3789 &output_part_length,
3790 tag_buffer,
3791 tag_length ),
3792 PSA_ERROR_BAD_STATE );
3793
3794 psa_aead_abort( &operation );
3795
3796 /* Test for double setups. */
3797
3798 operation = psa_aead_operation_init( );
3799
3800 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3801
3802 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
3803 PSA_ERROR_BAD_STATE );
3804
3805 psa_aead_abort( &operation );
3806
Paul Elliott481be342021-07-16 17:38:47 +01003807 /* ------------------------------------------------------- */
3808
Paul Elliottc23a9a02021-06-21 18:32:46 +01003809 operation = psa_aead_operation_init( );
3810
3811 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3812
3813 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
3814 PSA_ERROR_BAD_STATE );
3815
3816 psa_aead_abort( &operation );
3817
Paul Elliott374a2be2021-07-16 17:53:40 +01003818 /* ------------------------------------------------------- */
3819
3820 operation = psa_aead_operation_init( );
3821
3822 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3823
3824 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
3825 PSA_ERROR_BAD_STATE );
3826
3827 psa_aead_abort( &operation );
3828
3829 /* ------------------------------------------------------- */
3830
3831 operation = psa_aead_operation_init( );
3832
3833 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3834
3835 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
3836 PSA_ERROR_BAD_STATE );
3837
3838 psa_aead_abort( &operation );
3839
Paul Elliottc23a9a02021-06-21 18:32:46 +01003840 /* Test for not setting a nonce. */
3841
3842 operation = psa_aead_operation_init( );
3843
3844 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3845
3846 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3847 additional_data->len ),
3848 PSA_ERROR_BAD_STATE );
3849
3850 psa_aead_abort( &operation );
3851
3852 /* Test for double setting nonce. */
3853
3854 operation = psa_aead_operation_init( );
3855
3856 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3857
3858 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3859
3860 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3861 PSA_ERROR_BAD_STATE );
3862
3863 psa_aead_abort( &operation );
3864
Paul Elliott374a2be2021-07-16 17:53:40 +01003865 /* Test for double generating nonce. */
3866
3867 operation = psa_aead_operation_init( );
3868
3869 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3870
3871 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3872 PSA_AEAD_NONCE_MAX_SIZE,
3873 &nonce_length ) );
3874
3875 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3876 PSA_AEAD_NONCE_MAX_SIZE,
3877 &nonce_length ),
3878 PSA_ERROR_BAD_STATE );
3879
3880
3881 psa_aead_abort( &operation );
3882
3883 /* Test for generate nonce then set and vice versa */
3884
3885 operation = psa_aead_operation_init( );
3886
3887 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3888
3889 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3890 PSA_AEAD_NONCE_MAX_SIZE,
3891 &nonce_length ) );
3892
3893 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3894 PSA_ERROR_BAD_STATE );
3895
3896 psa_aead_abort( &operation );
3897
3898 /* ------------------------------------------------------- */
3899
3900 operation = psa_aead_operation_init( );
3901
3902 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3903
3904 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3905
3906 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3907 PSA_AEAD_NONCE_MAX_SIZE,
3908 &nonce_length ),
3909 PSA_ERROR_BAD_STATE );
3910
3911 psa_aead_abort( &operation );
3912
Paul Elliott7220cae2021-06-22 17:25:57 +01003913 /* Test for generating nonce in decrypt setup. */
3914
3915 operation = psa_aead_operation_init( );
3916
3917 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3918
3919 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3920 PSA_AEAD_NONCE_MAX_SIZE,
3921 &nonce_length ),
3922 PSA_ERROR_BAD_STATE );
3923
3924 psa_aead_abort( &operation );
3925
Paul Elliottc23a9a02021-06-21 18:32:46 +01003926 /* Test for setting lengths twice. */
3927
3928 operation = psa_aead_operation_init( );
3929
3930 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3931
3932 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3933
3934 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3935 input_data->len ) );
3936
3937 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
3938 input_data->len ),
3939 PSA_ERROR_BAD_STATE );
3940
3941 psa_aead_abort( &operation );
3942
Paul Elliott01876512021-06-23 18:13:04 +01003943 /* Test that generate/set nonce and set lengths are interchangeable (we
3944 * already tested set nonce followed by set lengths above). */
3945
3946 operation = psa_aead_operation_init( );
3947
3948 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3949
3950 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3951 input_data->len ) );
3952
3953 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3954
3955 psa_aead_abort( &operation );
3956
Paul Elliott481be342021-07-16 17:38:47 +01003957 /* ------------------------------------------------------- */
3958
Paul Elliott01876512021-06-23 18:13:04 +01003959 operation = psa_aead_operation_init( );
3960
3961 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3962
3963 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3964 input_data->len ) );
3965
3966 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3967 PSA_AEAD_NONCE_MAX_SIZE,
3968 &nonce_length ) );
3969
3970 psa_aead_abort( &operation );
3971
Paul Elliott481be342021-07-16 17:38:47 +01003972 /* ------------------------------------------------------- */
3973
Paul Elliott01876512021-06-23 18:13:04 +01003974 operation = psa_aead_operation_init( );
3975
3976 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3977
3978 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3979 PSA_AEAD_NONCE_MAX_SIZE,
3980 &nonce_length ) );
3981
3982 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3983 input_data->len ) );
3984
3985 psa_aead_abort( &operation );
3986
Paul Elliottc23a9a02021-06-21 18:32:46 +01003987 /* Test for setting lengths after already starting data. */
3988
3989 operation = psa_aead_operation_init( );
3990
3991 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3992
3993 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3994
3995 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
3996 input_data->len, output_data,
3997 output_size, &output_length ) );
3998
3999 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4000 input_data->len ),
4001 PSA_ERROR_BAD_STATE );
4002
4003 psa_aead_abort( &operation );
4004
4005 /* Test for not sending any additional data or data (encrypt) */
4006
4007 operation = psa_aead_operation_init( );
4008
4009 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4010
4011 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4012
4013 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4014 input_data->len ) );
4015
4016 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4017 finish_output_size,
4018 &output_part_length,
4019 tag_buffer, tag_length,
4020 &tag_size ),
4021 PSA_ERROR_INVALID_ARGUMENT );
4022
4023 psa_aead_abort( &operation );
4024
4025 /* Test for not sending any additional data or data (decrypt) */
4026
4027 operation = psa_aead_operation_init( );
4028
4029 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4030
4031 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4032
4033 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4034 input_data->len ) );
4035
4036 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4037 finish_output_size,
4038 &output_part_length,
4039 tag_buffer,
4040 tag_length ),
4041 PSA_ERROR_INVALID_ARGUMENT );
4042
4043 psa_aead_abort( &operation );
4044
4045 /* Test for not sending any additional data. */
4046
4047 operation = psa_aead_operation_init( );
4048
4049 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4050
4051 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4052
4053 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4054 input_data->len ) );
4055
4056 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4057 input_data->len, output_data,
4058 output_size, &output_length ),
4059 PSA_ERROR_INVALID_ARGUMENT );
4060
4061 psa_aead_abort( &operation );
4062
4063 /* Test sending additional data after data. */
4064
4065 operation = psa_aead_operation_init( );
4066
4067 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4068
4069 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4070
4071 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4072 input_data->len, output_data,
4073 output_size, &output_length ) );
4074
4075 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4076 additional_data->len ),
4077 PSA_ERROR_BAD_STATE );
4078
4079 psa_aead_abort( &operation );
4080
Paul Elliott534d0b42021-06-22 19:15:20 +01004081 /* Test calling finish on decryption. */
4082
4083 operation = psa_aead_operation_init( );
4084
4085 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4086
4087 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4088
4089 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4090 finish_output_size,
4091 &output_part_length,
4092 tag_buffer, tag_length,
4093 &tag_size ),
4094 PSA_ERROR_BAD_STATE );
4095
4096 psa_aead_abort( &operation );
4097
4098 /* Test calling verify on encryption. */
4099
4100 operation = psa_aead_operation_init( );
4101
4102 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4103
4104 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4105
4106 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4107 finish_output_size,
4108 &output_part_length,
4109 tag_buffer,
4110 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004111 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004112
4113 psa_aead_abort( &operation );
4114
4115
Paul Elliottc23a9a02021-06-21 18:32:46 +01004116exit:
4117 psa_destroy_key( key );
4118 psa_aead_abort( &operation );
4119 mbedtls_free( output_data );
4120 mbedtls_free( final_data );
4121 PSA_DONE( );
4122}
4123/* END_CASE */
4124
4125/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004126void signature_size( int type_arg,
4127 int bits,
4128 int alg_arg,
4129 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004130{
4131 psa_key_type_t type = type_arg;
4132 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004133 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004134
Gilles Peskinefe11b722018-12-18 00:24:04 +01004135 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004136
Gilles Peskinee59236f2018-01-27 23:32:46 +01004137exit:
4138 ;
4139}
4140/* END_CASE */
4141
4142/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004143void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4144 int alg_arg, data_t *input_data,
4145 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004146{
Ronald Cron5425a212020-08-04 14:58:35 +02004147 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004148 psa_key_type_t key_type = key_type_arg;
4149 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004150 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004151 unsigned char *signature = NULL;
4152 size_t signature_size;
4153 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004154 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004155
Gilles Peskine8817f612018-12-18 00:18:46 +01004156 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004157
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004158 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004159 psa_set_key_algorithm( &attributes, alg );
4160 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004161
Gilles Peskine049c7532019-05-15 20:22:09 +02004162 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004163 &key ) );
4164 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004165 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004166
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004167 /* Allocate a buffer which has the size advertized by the
4168 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004169 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004170 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004171 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004172 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004173 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004174
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004175 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004176 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004177 input_data->x, input_data->len,
4178 signature, signature_size,
4179 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004180 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004181 ASSERT_COMPARE( output_data->x, output_data->len,
4182 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004183
4184exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004185 /*
4186 * Key attributes may have been returned by psa_get_key_attributes()
4187 * thus reset them as required.
4188 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004189 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004190
Ronald Cron5425a212020-08-04 14:58:35 +02004191 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004192 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004193 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004194}
4195/* END_CASE */
4196
4197/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004198void sign_hash_fail( int key_type_arg, data_t *key_data,
4199 int alg_arg, data_t *input_data,
4200 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004201{
Ronald Cron5425a212020-08-04 14:58:35 +02004202 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004203 psa_key_type_t key_type = key_type_arg;
4204 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004205 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004206 psa_status_t actual_status;
4207 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004208 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004209 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004210 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004211
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004212 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004213
Gilles Peskine8817f612018-12-18 00:18:46 +01004214 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004215
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004217 psa_set_key_algorithm( &attributes, alg );
4218 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004219
Gilles Peskine049c7532019-05-15 20:22:09 +02004220 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004221 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004222
Ronald Cron5425a212020-08-04 14:58:35 +02004223 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004224 input_data->x, input_data->len,
4225 signature, signature_size,
4226 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004227 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004228 /* The value of *signature_length is unspecified on error, but
4229 * whatever it is, it should be less than signature_size, so that
4230 * if the caller tries to read *signature_length bytes without
4231 * checking the error code then they don't overflow a buffer. */
4232 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004233
4234exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004235 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004236 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004237 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004238 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004239}
4240/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004241
4242/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004243void sign_verify_hash( int key_type_arg, data_t *key_data,
4244 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004245{
Ronald Cron5425a212020-08-04 14:58:35 +02004246 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004247 psa_key_type_t key_type = key_type_arg;
4248 psa_algorithm_t alg = alg_arg;
4249 size_t key_bits;
4250 unsigned char *signature = NULL;
4251 size_t signature_size;
4252 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004254
Gilles Peskine8817f612018-12-18 00:18:46 +01004255 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004256
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004258 psa_set_key_algorithm( &attributes, alg );
4259 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004260
Gilles Peskine049c7532019-05-15 20:22:09 +02004261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004262 &key ) );
4263 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004264 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004265
4266 /* Allocate a buffer which has the size advertized by the
4267 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004268 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004269 key_bits, alg );
4270 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004271 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004272 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004273
4274 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004275 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004276 input_data->x, input_data->len,
4277 signature, signature_size,
4278 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004279 /* Check that the signature length looks sensible. */
4280 TEST_ASSERT( signature_length <= signature_size );
4281 TEST_ASSERT( signature_length > 0 );
4282
4283 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004284 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004285 input_data->x, input_data->len,
4286 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004287
4288 if( input_data->len != 0 )
4289 {
4290 /* Flip a bit in the input and verify that the signature is now
4291 * detected as invalid. Flip a bit at the beginning, not at the end,
4292 * because ECDSA may ignore the last few bits of the input. */
4293 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004294 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004295 input_data->x, input_data->len,
4296 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004297 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004298 }
4299
4300exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004301 /*
4302 * Key attributes may have been returned by psa_get_key_attributes()
4303 * thus reset them as required.
4304 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004305 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004306
Ronald Cron5425a212020-08-04 14:58:35 +02004307 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004308 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004309 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004310}
4311/* END_CASE */
4312
4313/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004314void verify_hash( int key_type_arg, data_t *key_data,
4315 int alg_arg, data_t *hash_data,
4316 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004317{
Ronald Cron5425a212020-08-04 14:58:35 +02004318 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004319 psa_key_type_t key_type = key_type_arg;
4320 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004321 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004322
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004323 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004324
Gilles Peskine8817f612018-12-18 00:18:46 +01004325 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004326
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004327 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004328 psa_set_key_algorithm( &attributes, alg );
4329 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004330
Gilles Peskine049c7532019-05-15 20:22:09 +02004331 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004332 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004333
Ronald Cron5425a212020-08-04 14:58:35 +02004334 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004335 hash_data->x, hash_data->len,
4336 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004337
itayzafrir5c753392018-05-08 11:18:38 +03004338exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004339 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004340 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004341 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004342}
4343/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004344
4345/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004346void verify_hash_fail( int key_type_arg, data_t *key_data,
4347 int alg_arg, data_t *hash_data,
4348 data_t *signature_data,
4349 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004350{
Ronald Cron5425a212020-08-04 14:58:35 +02004351 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004352 psa_key_type_t key_type = key_type_arg;
4353 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004354 psa_status_t actual_status;
4355 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004357
Gilles Peskine8817f612018-12-18 00:18:46 +01004358 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004359
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004360 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004361 psa_set_key_algorithm( &attributes, alg );
4362 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004363
Gilles Peskine049c7532019-05-15 20:22:09 +02004364 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004365 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004366
Ronald Cron5425a212020-08-04 14:58:35 +02004367 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004368 hash_data->x, hash_data->len,
4369 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004370 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004371
4372exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004373 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004374 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004375 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004376}
4377/* END_CASE */
4378
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004379/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004380void sign_message_deterministic( int key_type_arg,
4381 data_t *key_data,
4382 int alg_arg,
4383 data_t *input_data,
4384 data_t *output_data )
4385{
4386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4387 psa_key_type_t key_type = key_type_arg;
4388 psa_algorithm_t alg = alg_arg;
4389 size_t key_bits;
4390 unsigned char *signature = NULL;
4391 size_t signature_size;
4392 size_t signature_length = 0xdeadbeef;
4393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4394
4395 PSA_ASSERT( psa_crypto_init( ) );
4396
4397 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4398 psa_set_key_algorithm( &attributes, alg );
4399 psa_set_key_type( &attributes, key_type );
4400
4401 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4402 &key ) );
4403 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4404 key_bits = psa_get_key_bits( &attributes );
4405
4406 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4407 TEST_ASSERT( signature_size != 0 );
4408 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4409 ASSERT_ALLOC( signature, signature_size );
4410
4411 PSA_ASSERT( psa_sign_message( key, alg,
4412 input_data->x, input_data->len,
4413 signature, signature_size,
4414 &signature_length ) );
4415
4416 ASSERT_COMPARE( output_data->x, output_data->len,
4417 signature, signature_length );
4418
4419exit:
4420 psa_reset_key_attributes( &attributes );
4421
4422 psa_destroy_key( key );
4423 mbedtls_free( signature );
4424 PSA_DONE( );
4425
4426}
4427/* END_CASE */
4428
4429/* BEGIN_CASE */
4430void sign_message_fail( int key_type_arg,
4431 data_t *key_data,
4432 int alg_arg,
4433 data_t *input_data,
4434 int signature_size_arg,
4435 int expected_status_arg )
4436{
4437 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4438 psa_key_type_t key_type = key_type_arg;
4439 psa_algorithm_t alg = alg_arg;
4440 size_t signature_size = signature_size_arg;
4441 psa_status_t actual_status;
4442 psa_status_t expected_status = expected_status_arg;
4443 unsigned char *signature = NULL;
4444 size_t signature_length = 0xdeadbeef;
4445 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4446
4447 ASSERT_ALLOC( signature, signature_size );
4448
4449 PSA_ASSERT( psa_crypto_init( ) );
4450
4451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4452 psa_set_key_algorithm( &attributes, alg );
4453 psa_set_key_type( &attributes, key_type );
4454
4455 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4456 &key ) );
4457
4458 actual_status = psa_sign_message( key, alg,
4459 input_data->x, input_data->len,
4460 signature, signature_size,
4461 &signature_length );
4462 TEST_EQUAL( actual_status, expected_status );
4463 /* The value of *signature_length is unspecified on error, but
4464 * whatever it is, it should be less than signature_size, so that
4465 * if the caller tries to read *signature_length bytes without
4466 * checking the error code then they don't overflow a buffer. */
4467 TEST_ASSERT( signature_length <= signature_size );
4468
4469exit:
4470 psa_reset_key_attributes( &attributes );
4471 psa_destroy_key( key );
4472 mbedtls_free( signature );
4473 PSA_DONE( );
4474}
4475/* END_CASE */
4476
4477/* BEGIN_CASE */
4478void sign_verify_message( int key_type_arg,
4479 data_t *key_data,
4480 int alg_arg,
4481 data_t *input_data )
4482{
4483 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4484 psa_key_type_t key_type = key_type_arg;
4485 psa_algorithm_t alg = alg_arg;
4486 size_t key_bits;
4487 unsigned char *signature = NULL;
4488 size_t signature_size;
4489 size_t signature_length = 0xdeadbeef;
4490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4491
4492 PSA_ASSERT( psa_crypto_init( ) );
4493
4494 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4495 PSA_KEY_USAGE_VERIFY_MESSAGE );
4496 psa_set_key_algorithm( &attributes, alg );
4497 psa_set_key_type( &attributes, key_type );
4498
4499 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4500 &key ) );
4501 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4502 key_bits = psa_get_key_bits( &attributes );
4503
4504 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4505 TEST_ASSERT( signature_size != 0 );
4506 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4507 ASSERT_ALLOC( signature, signature_size );
4508
4509 PSA_ASSERT( psa_sign_message( key, alg,
4510 input_data->x, input_data->len,
4511 signature, signature_size,
4512 &signature_length ) );
4513 TEST_ASSERT( signature_length <= signature_size );
4514 TEST_ASSERT( signature_length > 0 );
4515
4516 PSA_ASSERT( psa_verify_message( key, alg,
4517 input_data->x, input_data->len,
4518 signature, signature_length ) );
4519
4520 if( input_data->len != 0 )
4521 {
4522 /* Flip a bit in the input and verify that the signature is now
4523 * detected as invalid. Flip a bit at the beginning, not at the end,
4524 * because ECDSA may ignore the last few bits of the input. */
4525 input_data->x[0] ^= 1;
4526 TEST_EQUAL( psa_verify_message( key, alg,
4527 input_data->x, input_data->len,
4528 signature, signature_length ),
4529 PSA_ERROR_INVALID_SIGNATURE );
4530 }
4531
4532exit:
4533 psa_reset_key_attributes( &attributes );
4534
4535 psa_destroy_key( key );
4536 mbedtls_free( signature );
4537 PSA_DONE( );
4538}
4539/* END_CASE */
4540
4541/* BEGIN_CASE */
4542void verify_message( int key_type_arg,
4543 data_t *key_data,
4544 int alg_arg,
4545 data_t *input_data,
4546 data_t *signature_data )
4547{
4548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4549 psa_key_type_t key_type = key_type_arg;
4550 psa_algorithm_t alg = alg_arg;
4551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4552
4553 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4554
4555 PSA_ASSERT( psa_crypto_init( ) );
4556
4557 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4558 psa_set_key_algorithm( &attributes, alg );
4559 psa_set_key_type( &attributes, key_type );
4560
4561 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4562 &key ) );
4563
4564 PSA_ASSERT( psa_verify_message( key, alg,
4565 input_data->x, input_data->len,
4566 signature_data->x, signature_data->len ) );
4567
4568exit:
4569 psa_reset_key_attributes( &attributes );
4570 psa_destroy_key( key );
4571 PSA_DONE( );
4572}
4573/* END_CASE */
4574
4575/* BEGIN_CASE */
4576void verify_message_fail( int key_type_arg,
4577 data_t *key_data,
4578 int alg_arg,
4579 data_t *hash_data,
4580 data_t *signature_data,
4581 int expected_status_arg )
4582{
4583 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4584 psa_key_type_t key_type = key_type_arg;
4585 psa_algorithm_t alg = alg_arg;
4586 psa_status_t actual_status;
4587 psa_status_t expected_status = expected_status_arg;
4588 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4589
4590 PSA_ASSERT( psa_crypto_init( ) );
4591
4592 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4593 psa_set_key_algorithm( &attributes, alg );
4594 psa_set_key_type( &attributes, key_type );
4595
4596 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4597 &key ) );
4598
4599 actual_status = psa_verify_message( key, alg,
4600 hash_data->x, hash_data->len,
4601 signature_data->x,
4602 signature_data->len );
4603 TEST_EQUAL( actual_status, expected_status );
4604
4605exit:
4606 psa_reset_key_attributes( &attributes );
4607 psa_destroy_key( key );
4608 PSA_DONE( );
4609}
4610/* END_CASE */
4611
4612/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004613void asymmetric_encrypt( int key_type_arg,
4614 data_t *key_data,
4615 int alg_arg,
4616 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004617 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004618 int expected_output_length_arg,
4619 int expected_status_arg )
4620{
Ronald Cron5425a212020-08-04 14:58:35 +02004621 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004622 psa_key_type_t key_type = key_type_arg;
4623 psa_algorithm_t alg = alg_arg;
4624 size_t expected_output_length = expected_output_length_arg;
4625 size_t key_bits;
4626 unsigned char *output = NULL;
4627 size_t output_size;
4628 size_t output_length = ~0;
4629 psa_status_t actual_status;
4630 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004632
Gilles Peskine8817f612018-12-18 00:18:46 +01004633 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004634
Gilles Peskine656896e2018-06-29 19:12:28 +02004635 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004636 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4637 psa_set_key_algorithm( &attributes, alg );
4638 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004639 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004640 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004641
4642 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004643 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004644 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004645
Gilles Peskine656896e2018-06-29 19:12:28 +02004646 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004647 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004648 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004649
4650 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004651 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004652 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004653 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004654 output, output_size,
4655 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004656 TEST_EQUAL( actual_status, expected_status );
4657 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004658
Gilles Peskine68428122018-06-30 18:42:41 +02004659 /* If the label is empty, the test framework puts a non-null pointer
4660 * in label->x. Test that a null pointer works as well. */
4661 if( label->len == 0 )
4662 {
4663 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004664 if( output_size != 0 )
4665 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004666 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004667 input_data->x, input_data->len,
4668 NULL, label->len,
4669 output, output_size,
4670 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004671 TEST_EQUAL( actual_status, expected_status );
4672 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004673 }
4674
Gilles Peskine656896e2018-06-29 19:12:28 +02004675exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004676 /*
4677 * Key attributes may have been returned by psa_get_key_attributes()
4678 * thus reset them as required.
4679 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004680 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004681
Ronald Cron5425a212020-08-04 14:58:35 +02004682 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004683 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004684 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004685}
4686/* END_CASE */
4687
4688/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004689void asymmetric_encrypt_decrypt( int key_type_arg,
4690 data_t *key_data,
4691 int alg_arg,
4692 data_t *input_data,
4693 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004694{
Ronald Cron5425a212020-08-04 14:58:35 +02004695 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004696 psa_key_type_t key_type = key_type_arg;
4697 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004698 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004699 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004700 size_t output_size;
4701 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004702 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004703 size_t output2_size;
4704 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004706
Gilles Peskine8817f612018-12-18 00:18:46 +01004707 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004708
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004709 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4710 psa_set_key_algorithm( &attributes, alg );
4711 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004712
Gilles Peskine049c7532019-05-15 20:22:09 +02004713 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004714 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004715
4716 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004717 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004718 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004719
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004720 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004721 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004722 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004723
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004724 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004725 TEST_ASSERT( output2_size <=
4726 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4727 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004728 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004729
Gilles Peskineeebd7382018-06-08 18:11:54 +02004730 /* We test encryption by checking that encrypt-then-decrypt gives back
4731 * the original plaintext because of the non-optional random
4732 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004733 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004734 input_data->x, input_data->len,
4735 label->x, label->len,
4736 output, output_size,
4737 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004738 /* We don't know what ciphertext length to expect, but check that
4739 * it looks sensible. */
4740 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004741
Ronald Cron5425a212020-08-04 14:58:35 +02004742 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004743 output, output_length,
4744 label->x, label->len,
4745 output2, output2_size,
4746 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004747 ASSERT_COMPARE( input_data->x, input_data->len,
4748 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004749
4750exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004751 /*
4752 * Key attributes may have been returned by psa_get_key_attributes()
4753 * thus reset them as required.
4754 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004755 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004756
Ronald Cron5425a212020-08-04 14:58:35 +02004757 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004758 mbedtls_free( output );
4759 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004760 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004761}
4762/* END_CASE */
4763
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004764/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004765void asymmetric_decrypt( int key_type_arg,
4766 data_t *key_data,
4767 int alg_arg,
4768 data_t *input_data,
4769 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004770 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004771{
Ronald Cron5425a212020-08-04 14:58:35 +02004772 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004773 psa_key_type_t key_type = key_type_arg;
4774 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004775 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004776 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004777 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004778 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004779 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004780
Gilles Peskine8817f612018-12-18 00:18:46 +01004781 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004782
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004783 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4784 psa_set_key_algorithm( &attributes, alg );
4785 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004786
Gilles Peskine049c7532019-05-15 20:22:09 +02004787 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004788 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004789
gabor-mezei-armceface22021-01-21 12:26:17 +01004790 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4791 key_bits = psa_get_key_bits( &attributes );
4792
4793 /* Determine the maximum ciphertext length */
4794 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4795 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4796 ASSERT_ALLOC( output, output_size );
4797
Ronald Cron5425a212020-08-04 14:58:35 +02004798 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004799 input_data->x, input_data->len,
4800 label->x, label->len,
4801 output,
4802 output_size,
4803 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004804 ASSERT_COMPARE( expected_data->x, expected_data->len,
4805 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004806
Gilles Peskine68428122018-06-30 18:42:41 +02004807 /* If the label is empty, the test framework puts a non-null pointer
4808 * in label->x. Test that a null pointer works as well. */
4809 if( label->len == 0 )
4810 {
4811 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004812 if( output_size != 0 )
4813 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004814 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004815 input_data->x, input_data->len,
4816 NULL, label->len,
4817 output,
4818 output_size,
4819 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004820 ASSERT_COMPARE( expected_data->x, expected_data->len,
4821 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004822 }
4823
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004824exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004825 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004826 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004827 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004828 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004829}
4830/* END_CASE */
4831
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004832/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004833void asymmetric_decrypt_fail( int key_type_arg,
4834 data_t *key_data,
4835 int alg_arg,
4836 data_t *input_data,
4837 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004838 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004839 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004840{
Ronald Cron5425a212020-08-04 14:58:35 +02004841 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004842 psa_key_type_t key_type = key_type_arg;
4843 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004844 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004845 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004846 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004847 psa_status_t actual_status;
4848 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004849 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004850
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004851 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004852
Gilles Peskine8817f612018-12-18 00:18:46 +01004853 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004854
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004855 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4856 psa_set_key_algorithm( &attributes, alg );
4857 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004858
Gilles Peskine049c7532019-05-15 20:22:09 +02004859 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004860 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004861
Ronald Cron5425a212020-08-04 14:58:35 +02004862 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004863 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004864 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004865 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004866 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004867 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004868 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004869
Gilles Peskine68428122018-06-30 18:42:41 +02004870 /* If the label is empty, the test framework puts a non-null pointer
4871 * in label->x. Test that a null pointer works as well. */
4872 if( label->len == 0 )
4873 {
4874 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004875 if( output_size != 0 )
4876 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004877 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004878 input_data->x, input_data->len,
4879 NULL, label->len,
4880 output, output_size,
4881 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004882 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004883 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004884 }
4885
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004886exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004887 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004888 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004889 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004890 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004891}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004892/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004893
4894/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004895void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004896{
4897 /* Test each valid way of initializing the object, except for `= {0}`, as
4898 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4899 * though it's OK by the C standard. We could test for this, but we'd need
4900 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004901 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004902 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4903 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4904 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004905
4906 memset( &zero, 0, sizeof( zero ) );
4907
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004908 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004909 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004910 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004911 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004912 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004913 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004914 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004915
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004917 PSA_ASSERT( psa_key_derivation_abort(&func) );
4918 PSA_ASSERT( psa_key_derivation_abort(&init) );
4919 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004920}
4921/* END_CASE */
4922
Janos Follath16de4a42019-06-13 16:32:24 +01004923/* BEGIN_CASE */
4924void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004925{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004926 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004927 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004929
Gilles Peskine8817f612018-12-18 00:18:46 +01004930 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004931
Janos Follath16de4a42019-06-13 16:32:24 +01004932 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004933 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004934
4935exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004937 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004938}
4939/* END_CASE */
4940
Janos Follathaf3c2a02019-06-12 12:34:34 +01004941/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004942void derive_set_capacity( int alg_arg, int capacity_arg,
4943 int expected_status_arg )
4944{
4945 psa_algorithm_t alg = alg_arg;
4946 size_t capacity = capacity_arg;
4947 psa_status_t expected_status = expected_status_arg;
4948 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4949
4950 PSA_ASSERT( psa_crypto_init( ) );
4951
4952 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4953
4954 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4955 expected_status );
4956
4957exit:
4958 psa_key_derivation_abort( &operation );
4959 PSA_DONE( );
4960}
4961/* END_CASE */
4962
4963/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004964void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004965 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004966 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004967 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004968 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004969 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004970 int expected_status_arg3,
4971 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004972{
4973 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004974 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4975 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004976 psa_status_t expected_statuses[] = {expected_status_arg1,
4977 expected_status_arg2,
4978 expected_status_arg3};
4979 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004980 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4981 MBEDTLS_SVC_KEY_ID_INIT,
4982 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004983 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4984 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4985 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004986 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004987 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004988 psa_status_t expected_output_status = expected_output_status_arg;
4989 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004990
4991 PSA_ASSERT( psa_crypto_init( ) );
4992
4993 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4994 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004995
4996 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4997
4998 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4999 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005000 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005001 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005002 psa_set_key_type( &attributes, key_types[i] );
5003 PSA_ASSERT( psa_import_key( &attributes,
5004 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005005 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005006 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5007 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5008 {
5009 // When taking a private key as secret input, use key agreement
5010 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005011 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5012 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005013 expected_statuses[i] );
5014 }
5015 else
5016 {
5017 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005018 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005019 expected_statuses[i] );
5020 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005021 }
5022 else
5023 {
5024 TEST_EQUAL( psa_key_derivation_input_bytes(
5025 &operation, steps[i],
5026 inputs[i]->x, inputs[i]->len ),
5027 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005028 }
5029 }
5030
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005031 if( output_key_type != PSA_KEY_TYPE_NONE )
5032 {
5033 psa_reset_key_attributes( &attributes );
5034 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5035 psa_set_key_bits( &attributes, 8 );
5036 actual_output_status =
5037 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005038 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005039 }
5040 else
5041 {
5042 uint8_t buffer[1];
5043 actual_output_status =
5044 psa_key_derivation_output_bytes( &operation,
5045 buffer, sizeof( buffer ) );
5046 }
5047 TEST_EQUAL( actual_output_status, expected_output_status );
5048
Janos Follathaf3c2a02019-06-12 12:34:34 +01005049exit:
5050 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005051 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5052 psa_destroy_key( keys[i] );
5053 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005054 PSA_DONE( );
5055}
5056/* END_CASE */
5057
Janos Follathd958bb72019-07-03 15:02:16 +01005058/* BEGIN_CASE */
5059void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005060{
Janos Follathd958bb72019-07-03 15:02:16 +01005061 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005062 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005063 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005065 unsigned char input1[] = "Input 1";
5066 size_t input1_length = sizeof( input1 );
5067 unsigned char input2[] = "Input 2";
5068 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005069 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005070 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005071 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5072 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5073 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005074 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005075
Gilles Peskine8817f612018-12-18 00:18:46 +01005076 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005077
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005078 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5079 psa_set_key_algorithm( &attributes, alg );
5080 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005081
Gilles Peskine73676cb2019-05-15 20:15:10 +02005082 PSA_ASSERT( psa_import_key( &attributes,
5083 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005084 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005085
5086 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005087 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5088 input1, input1_length,
5089 input2, input2_length,
5090 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005091 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005092
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005093 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005094 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005095 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005096
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005097 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005098
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005100 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005101
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005102exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005103 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005104 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005105 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005106}
5107/* END_CASE */
5108
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005109/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005110void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005111{
5112 uint8_t output_buffer[16];
5113 size_t buffer_size = 16;
5114 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005116
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005117 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5118 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005119 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005120
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005122 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005123
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005124 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005125
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005126 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5127 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005128 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005129
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005130 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005131 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005132
5133exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005135}
5136/* END_CASE */
5137
5138/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005139void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005140 int step1_arg, data_t *input1,
5141 int step2_arg, data_t *input2,
5142 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005143 int requested_capacity_arg,
5144 data_t *expected_output1,
5145 data_t *expected_output2 )
5146{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005147 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005148 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5149 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005150 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5151 MBEDTLS_SVC_KEY_ID_INIT,
5152 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005153 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005154 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005155 uint8_t *expected_outputs[2] =
5156 {expected_output1->x, expected_output2->x};
5157 size_t output_sizes[2] =
5158 {expected_output1->len, expected_output2->len};
5159 size_t output_buffer_size = 0;
5160 uint8_t *output_buffer = NULL;
5161 size_t expected_capacity;
5162 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005164 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005165 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005166
5167 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5168 {
5169 if( output_sizes[i] > output_buffer_size )
5170 output_buffer_size = output_sizes[i];
5171 if( output_sizes[i] == 0 )
5172 expected_outputs[i] = NULL;
5173 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005174 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005175 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005176
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005177 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5178 psa_set_key_algorithm( &attributes, alg );
5179 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005180
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005181 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005182 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5183 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5184 requested_capacity ) );
5185 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005186 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005187 switch( steps[i] )
5188 {
5189 case 0:
5190 break;
5191 case PSA_KEY_DERIVATION_INPUT_SECRET:
5192 PSA_ASSERT( psa_import_key( &attributes,
5193 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005194 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005195
5196 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5197 {
5198 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5199 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5200 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5201 }
5202
Gilles Peskine1468da72019-05-29 17:35:49 +02005203 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005204 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005205 break;
5206 default:
5207 PSA_ASSERT( psa_key_derivation_input_bytes(
5208 &operation, steps[i],
5209 inputs[i]->x, inputs[i]->len ) );
5210 break;
5211 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005212 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005213
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005215 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005216 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005217 expected_capacity = requested_capacity;
5218
5219 /* Expansion phase. */
5220 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5221 {
5222 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005223 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005224 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005225 if( expected_capacity == 0 && output_sizes[i] == 0 )
5226 {
5227 /* Reading 0 bytes when 0 bytes are available can go either way. */
5228 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005229 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005230 continue;
5231 }
5232 else if( expected_capacity == 0 ||
5233 output_sizes[i] > expected_capacity )
5234 {
5235 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005236 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005237 expected_capacity = 0;
5238 continue;
5239 }
5240 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005241 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005242 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005243 ASSERT_COMPARE( output_buffer, output_sizes[i],
5244 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005246 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005247 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005248 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005249 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005250 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005252
5253exit:
5254 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005256 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5257 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005258 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005259}
5260/* END_CASE */
5261
5262/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005263void derive_full( int alg_arg,
5264 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005265 data_t *input1,
5266 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005267 int requested_capacity_arg )
5268{
Ronald Cron5425a212020-08-04 14:58:35 +02005269 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005270 psa_algorithm_t alg = alg_arg;
5271 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005273 unsigned char output_buffer[16];
5274 size_t expected_capacity = requested_capacity;
5275 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005277
Gilles Peskine8817f612018-12-18 00:18:46 +01005278 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005279
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005280 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5281 psa_set_key_algorithm( &attributes, alg );
5282 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005283
Gilles Peskine049c7532019-05-15 20:22:09 +02005284 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005285 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005286
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005287 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5288 input1->x, input1->len,
5289 input2->x, input2->len,
5290 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005291 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005292
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005294 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005295 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005296
5297 /* Expansion phase. */
5298 while( current_capacity > 0 )
5299 {
5300 size_t read_size = sizeof( output_buffer );
5301 if( read_size > current_capacity )
5302 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005303 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005304 output_buffer,
5305 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005306 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005307 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005308 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005309 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005310 }
5311
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005312 /* Check that the operation refuses to go over capacity. */
5313 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005314 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005315
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005316 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005317
5318exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005319 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005320 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005321 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005322}
5323/* END_CASE */
5324
Janos Follathe60c9052019-07-03 13:51:30 +01005325/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005326void derive_key_exercise( int alg_arg,
5327 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005328 data_t *input1,
5329 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005330 int derived_type_arg,
5331 int derived_bits_arg,
5332 int derived_usage_arg,
5333 int derived_alg_arg )
5334{
Ronald Cron5425a212020-08-04 14:58:35 +02005335 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5336 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005337 psa_algorithm_t alg = alg_arg;
5338 psa_key_type_t derived_type = derived_type_arg;
5339 size_t derived_bits = derived_bits_arg;
5340 psa_key_usage_t derived_usage = derived_usage_arg;
5341 psa_algorithm_t derived_alg = derived_alg_arg;
5342 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005343 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005345 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005346
Gilles Peskine8817f612018-12-18 00:18:46 +01005347 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005348
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005349 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5350 psa_set_key_algorithm( &attributes, alg );
5351 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005352 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005353 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005354
5355 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005356 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5357 input1->x, input1->len,
5358 input2->x, input2->len,
5359 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005360 goto exit;
5361
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005362 psa_set_key_usage_flags( &attributes, derived_usage );
5363 psa_set_key_algorithm( &attributes, derived_alg );
5364 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005365 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005366 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005367 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005368
5369 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005370 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005371 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5372 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005373
5374 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005375 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005376 goto exit;
5377
5378exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005379 /*
5380 * Key attributes may have been returned by psa_get_key_attributes()
5381 * thus reset them as required.
5382 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005383 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005384
5385 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005386 psa_destroy_key( base_key );
5387 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005388 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005389}
5390/* END_CASE */
5391
Janos Follath42fd8882019-07-03 14:17:09 +01005392/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005393void derive_key_export( int alg_arg,
5394 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005395 data_t *input1,
5396 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005397 int bytes1_arg,
5398 int bytes2_arg )
5399{
Ronald Cron5425a212020-08-04 14:58:35 +02005400 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5401 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005402 psa_algorithm_t alg = alg_arg;
5403 size_t bytes1 = bytes1_arg;
5404 size_t bytes2 = bytes2_arg;
5405 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005406 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005407 uint8_t *output_buffer = NULL;
5408 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005409 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5410 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005411 size_t length;
5412
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005413 ASSERT_ALLOC( output_buffer, capacity );
5414 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005415 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005416
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005417 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5418 psa_set_key_algorithm( &base_attributes, alg );
5419 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005420 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005421 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005422
5423 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005424 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5425 input1->x, input1->len,
5426 input2->x, input2->len,
5427 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005428 goto exit;
5429
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005430 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005431 output_buffer,
5432 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005434
5435 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005436 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5437 input1->x, input1->len,
5438 input2->x, input2->len,
5439 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005440 goto exit;
5441
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005442 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5443 psa_set_key_algorithm( &derived_attributes, 0 );
5444 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005445 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005446 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005447 &derived_key ) );
5448 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005449 export_buffer, bytes1,
5450 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005451 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005452 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005453 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005454 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005455 &derived_key ) );
5456 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005457 export_buffer + bytes1, bytes2,
5458 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005459 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005460
5461 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005462 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5463 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005464
5465exit:
5466 mbedtls_free( output_buffer );
5467 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005469 psa_destroy_key( base_key );
5470 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005471 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005472}
5473/* END_CASE */
5474
5475/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005476void derive_key( int alg_arg,
5477 data_t *key_data, data_t *input1, data_t *input2,
5478 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005479 int expected_status_arg,
5480 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005481{
Ronald Cron5425a212020-08-04 14:58:35 +02005482 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5483 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005484 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005485 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005486 size_t bits = bits_arg;
5487 psa_status_t expected_status = expected_status_arg;
5488 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5489 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5490 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5491
5492 PSA_ASSERT( psa_crypto_init( ) );
5493
5494 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5495 psa_set_key_algorithm( &base_attributes, alg );
5496 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5497 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005498 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005499
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005500 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5501 input1->x, input1->len,
5502 input2->x, input2->len,
5503 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005504 goto exit;
5505
5506 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5507 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005508 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005509 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005510
5511 psa_status_t status =
5512 psa_key_derivation_output_key( &derived_attributes,
5513 &operation,
5514 &derived_key );
5515 if( is_large_output > 0 )
5516 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5517 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005518
5519exit:
5520 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005521 psa_destroy_key( base_key );
5522 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005523 PSA_DONE( );
5524}
5525/* END_CASE */
5526
5527/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005528void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005529 int our_key_type_arg, int our_key_alg_arg,
5530 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005531 int expected_status_arg )
5532{
Ronald Cron5425a212020-08-04 14:58:35 +02005533 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005534 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005535 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005536 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005539 psa_status_t expected_status = expected_status_arg;
5540 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005541
Gilles Peskine8817f612018-12-18 00:18:46 +01005542 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005543
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005544 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005545 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005546 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005547 PSA_ASSERT( psa_import_key( &attributes,
5548 our_key_data->x, our_key_data->len,
5549 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005550
Gilles Peskine77f40d82019-04-11 21:27:06 +02005551 /* The tests currently include inputs that should fail at either step.
5552 * Test cases that fail at the setup step should be changed to call
5553 * key_derivation_setup instead, and this function should be renamed
5554 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005556 if( status == PSA_SUCCESS )
5557 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005558 TEST_EQUAL( psa_key_derivation_key_agreement(
5559 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5560 our_key,
5561 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005562 expected_status );
5563 }
5564 else
5565 {
5566 TEST_ASSERT( status == expected_status );
5567 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005568
5569exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005570 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005571 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005572 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005573}
5574/* END_CASE */
5575
5576/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005577void raw_key_agreement( int alg_arg,
5578 int our_key_type_arg, data_t *our_key_data,
5579 data_t *peer_key_data,
5580 data_t *expected_output )
5581{
Ronald Cron5425a212020-08-04 14:58:35 +02005582 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005583 psa_algorithm_t alg = alg_arg;
5584 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005586 unsigned char *output = NULL;
5587 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005588 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005589
5590 ASSERT_ALLOC( output, expected_output->len );
5591 PSA_ASSERT( psa_crypto_init( ) );
5592
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005593 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5594 psa_set_key_algorithm( &attributes, alg );
5595 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005596 PSA_ASSERT( psa_import_key( &attributes,
5597 our_key_data->x, our_key_data->len,
5598 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005599
gabor-mezei-armceface22021-01-21 12:26:17 +01005600 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5601 key_bits = psa_get_key_bits( &attributes );
5602
Gilles Peskinebe697d82019-05-16 18:00:41 +02005603 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5604 peer_key_data->x, peer_key_data->len,
5605 output, expected_output->len,
5606 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005607 ASSERT_COMPARE( output, output_length,
5608 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005609 TEST_ASSERT( output_length <=
5610 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5611 TEST_ASSERT( output_length <=
5612 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005613
5614exit:
5615 mbedtls_free( output );
5616 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005617 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005618}
5619/* END_CASE */
5620
5621/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005622void key_agreement_capacity( int alg_arg,
5623 int our_key_type_arg, data_t *our_key_data,
5624 data_t *peer_key_data,
5625 int expected_capacity_arg )
5626{
Ronald Cron5425a212020-08-04 14:58:35 +02005627 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005628 psa_algorithm_t alg = alg_arg;
5629 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005630 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005632 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005633 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005634
Gilles Peskine8817f612018-12-18 00:18:46 +01005635 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005636
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005637 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5638 psa_set_key_algorithm( &attributes, alg );
5639 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005640 PSA_ASSERT( psa_import_key( &attributes,
5641 our_key_data->x, our_key_data->len,
5642 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005643
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005644 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005645 PSA_ASSERT( psa_key_derivation_key_agreement(
5646 &operation,
5647 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5648 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005649 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5650 {
5651 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005652 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005653 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005654 NULL, 0 ) );
5655 }
Gilles Peskine59685592018-09-18 12:11:34 +02005656
Gilles Peskinebf491972018-10-25 22:36:12 +02005657 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005658 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005659 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005660 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005661
Gilles Peskinebf491972018-10-25 22:36:12 +02005662 /* Test the actual capacity by reading the output. */
5663 while( actual_capacity > sizeof( output ) )
5664 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005665 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005666 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005667 actual_capacity -= sizeof( output );
5668 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005669 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005670 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005671 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005672 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005673
Gilles Peskine59685592018-09-18 12:11:34 +02005674exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005675 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005676 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005677 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005678}
5679/* END_CASE */
5680
5681/* BEGIN_CASE */
5682void key_agreement_output( int alg_arg,
5683 int our_key_type_arg, data_t *our_key_data,
5684 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005685 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005686{
Ronald Cron5425a212020-08-04 14:58:35 +02005687 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005688 psa_algorithm_t alg = alg_arg;
5689 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005690 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005691 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005692 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005693
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005694 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5695 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005696
Gilles Peskine8817f612018-12-18 00:18:46 +01005697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005698
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005699 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5700 psa_set_key_algorithm( &attributes, alg );
5701 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005702 PSA_ASSERT( psa_import_key( &attributes,
5703 our_key_data->x, our_key_data->len,
5704 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005705
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005706 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005707 PSA_ASSERT( psa_key_derivation_key_agreement(
5708 &operation,
5709 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5710 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005711 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5712 {
5713 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005714 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005715 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005716 NULL, 0 ) );
5717 }
Gilles Peskine59685592018-09-18 12:11:34 +02005718
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005719 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005720 actual_output,
5721 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005722 ASSERT_COMPARE( actual_output, expected_output1->len,
5723 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005724 if( expected_output2->len != 0 )
5725 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005726 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005727 actual_output,
5728 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005729 ASSERT_COMPARE( actual_output, expected_output2->len,
5730 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005731 }
Gilles Peskine59685592018-09-18 12:11:34 +02005732
5733exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005734 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005735 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005736 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005737 mbedtls_free( actual_output );
5738}
5739/* END_CASE */
5740
5741/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005742void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005743{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005744 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005745 unsigned char *output = NULL;
5746 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005747 size_t i;
5748 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005749
Simon Butcher49f8e312020-03-03 15:51:50 +00005750 TEST_ASSERT( bytes_arg >= 0 );
5751
Gilles Peskine91892022021-02-08 19:50:26 +01005752 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005753 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005754
Gilles Peskine8817f612018-12-18 00:18:46 +01005755 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005756
Gilles Peskinea50d7392018-06-21 10:22:13 +02005757 /* Run several times, to ensure that every output byte will be
5758 * nonzero at least once with overwhelming probability
5759 * (2^(-8*number_of_runs)). */
5760 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005761 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005762 if( bytes != 0 )
5763 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005764 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005765
Gilles Peskinea50d7392018-06-21 10:22:13 +02005766 for( i = 0; i < bytes; i++ )
5767 {
5768 if( output[i] != 0 )
5769 ++changed[i];
5770 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005771 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005772
5773 /* Check that every byte was changed to nonzero at least once. This
5774 * validates that psa_generate_random is overwriting every byte of
5775 * the output buffer. */
5776 for( i = 0; i < bytes; i++ )
5777 {
5778 TEST_ASSERT( changed[i] != 0 );
5779 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005780
5781exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005782 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005783 mbedtls_free( output );
5784 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005785}
5786/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005787
5788/* BEGIN_CASE */
5789void generate_key( int type_arg,
5790 int bits_arg,
5791 int usage_arg,
5792 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005793 int expected_status_arg,
5794 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005795{
Ronald Cron5425a212020-08-04 14:58:35 +02005796 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005797 psa_key_type_t type = type_arg;
5798 psa_key_usage_t usage = usage_arg;
5799 size_t bits = bits_arg;
5800 psa_algorithm_t alg = alg_arg;
5801 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005803 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005804
Gilles Peskine8817f612018-12-18 00:18:46 +01005805 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005806
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005807 psa_set_key_usage_flags( &attributes, usage );
5808 psa_set_key_algorithm( &attributes, alg );
5809 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005810 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005811
5812 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005813 psa_status_t status = psa_generate_key( &attributes, &key );
5814
5815 if( is_large_key > 0 )
5816 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5817 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005818 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005819 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005820
5821 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005822 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005823 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5824 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005825
Gilles Peskine818ca122018-06-20 18:16:48 +02005826 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005827 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005828 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005829
5830exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005831 /*
5832 * Key attributes may have been returned by psa_get_key_attributes()
5833 * thus reset them as required.
5834 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005835 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005836
Ronald Cron5425a212020-08-04 14:58:35 +02005837 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005838 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005839}
5840/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005841
Ronald Cronee414c72021-03-18 18:50:08 +01005842/* 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 +02005843void generate_key_rsa( int bits_arg,
5844 data_t *e_arg,
5845 int expected_status_arg )
5846{
Ronald Cron5425a212020-08-04 14:58:35 +02005847 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005848 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005849 size_t bits = bits_arg;
5850 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5851 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5852 psa_status_t expected_status = expected_status_arg;
5853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5854 uint8_t *exported = NULL;
5855 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005856 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005857 size_t exported_length = SIZE_MAX;
5858 uint8_t *e_read_buffer = NULL;
5859 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005860 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005861 size_t e_read_length = SIZE_MAX;
5862
5863 if( e_arg->len == 0 ||
5864 ( e_arg->len == 3 &&
5865 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5866 {
5867 is_default_public_exponent = 1;
5868 e_read_size = 0;
5869 }
5870 ASSERT_ALLOC( e_read_buffer, e_read_size );
5871 ASSERT_ALLOC( exported, exported_size );
5872
5873 PSA_ASSERT( psa_crypto_init( ) );
5874
5875 psa_set_key_usage_flags( &attributes, usage );
5876 psa_set_key_algorithm( &attributes, alg );
5877 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5878 e_arg->x, e_arg->len ) );
5879 psa_set_key_bits( &attributes, bits );
5880
5881 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005882 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005883 if( expected_status != PSA_SUCCESS )
5884 goto exit;
5885
5886 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005887 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005888 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5889 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5890 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5891 e_read_buffer, e_read_size,
5892 &e_read_length ) );
5893 if( is_default_public_exponent )
5894 TEST_EQUAL( e_read_length, 0 );
5895 else
5896 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5897
5898 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005899 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005900 goto exit;
5901
5902 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005903 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005904 exported, exported_size,
5905 &exported_length ) );
5906 {
5907 uint8_t *p = exported;
5908 uint8_t *end = exported + exported_length;
5909 size_t len;
5910 /* RSAPublicKey ::= SEQUENCE {
5911 * modulus INTEGER, -- n
5912 * publicExponent INTEGER } -- e
5913 */
5914 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005915 MBEDTLS_ASN1_SEQUENCE |
5916 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005917 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005918 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5919 MBEDTLS_ASN1_INTEGER ) );
5920 if( len >= 1 && p[0] == 0 )
5921 {
5922 ++p;
5923 --len;
5924 }
5925 if( e_arg->len == 0 )
5926 {
5927 TEST_EQUAL( len, 3 );
5928 TEST_EQUAL( p[0], 1 );
5929 TEST_EQUAL( p[1], 0 );
5930 TEST_EQUAL( p[2], 1 );
5931 }
5932 else
5933 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5934 }
5935
5936exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005937 /*
5938 * Key attributes may have been returned by psa_get_key_attributes() or
5939 * set by psa_set_key_domain_parameters() thus reset them as required.
5940 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005941 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005942
Ronald Cron5425a212020-08-04 14:58:35 +02005943 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005944 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005945 mbedtls_free( e_read_buffer );
5946 mbedtls_free( exported );
5947}
5948/* END_CASE */
5949
Darryl Greend49a4992018-06-18 17:27:26 +01005950/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005951void persistent_key_load_key_from_storage( data_t *data,
5952 int type_arg, int bits_arg,
5953 int usage_flags_arg, int alg_arg,
5954 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005955{
Ronald Cron71016a92020-08-28 19:01:50 +02005956 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005958 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5959 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005960 psa_key_type_t type = type_arg;
5961 size_t bits = bits_arg;
5962 psa_key_usage_t usage_flags = usage_flags_arg;
5963 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005964 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005965 unsigned char *first_export = NULL;
5966 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005967 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005968 size_t first_exported_length;
5969 size_t second_exported_length;
5970
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005971 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5972 {
5973 ASSERT_ALLOC( first_export, export_size );
5974 ASSERT_ALLOC( second_export, export_size );
5975 }
Darryl Greend49a4992018-06-18 17:27:26 +01005976
Gilles Peskine8817f612018-12-18 00:18:46 +01005977 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005978
Gilles Peskinec87af662019-05-15 16:12:22 +02005979 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005980 psa_set_key_usage_flags( &attributes, usage_flags );
5981 psa_set_key_algorithm( &attributes, alg );
5982 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005983 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005984
Darryl Green0c6575a2018-11-07 16:05:30 +00005985 switch( generation_method )
5986 {
5987 case IMPORT_KEY:
5988 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005989 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005990 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005991 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005992
Darryl Green0c6575a2018-11-07 16:05:30 +00005993 case GENERATE_KEY:
5994 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005995 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005996 break;
5997
5998 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005999#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006000 {
6001 /* Create base key */
6002 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6003 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6004 psa_set_key_usage_flags( &base_attributes,
6005 PSA_KEY_USAGE_DERIVE );
6006 psa_set_key_algorithm( &base_attributes, derive_alg );
6007 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006008 PSA_ASSERT( psa_import_key( &base_attributes,
6009 data->x, data->len,
6010 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006011 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006012 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006013 PSA_ASSERT( psa_key_derivation_input_key(
6014 &operation,
6015 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006016 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006017 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006018 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006019 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6020 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006021 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006022 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006023 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006024 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006025 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006026#else
6027 TEST_ASSUME( ! "KDF not supported in this configuration" );
6028#endif
6029 break;
6030
6031 default:
6032 TEST_ASSERT( ! "generation_method not implemented in test" );
6033 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006034 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006035 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006036
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006037 /* Export the key if permitted by the key policy. */
6038 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6039 {
Ronald Cron5425a212020-08-04 14:58:35 +02006040 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006041 first_export, export_size,
6042 &first_exported_length ) );
6043 if( generation_method == IMPORT_KEY )
6044 ASSERT_COMPARE( data->x, data->len,
6045 first_export, first_exported_length );
6046 }
Darryl Greend49a4992018-06-18 17:27:26 +01006047
6048 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006049 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006050 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006051 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006052
Darryl Greend49a4992018-06-18 17:27:26 +01006053 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006054 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006055 TEST_ASSERT( mbedtls_svc_key_id_equal(
6056 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006057 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6058 PSA_KEY_LIFETIME_PERSISTENT );
6059 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6060 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6061 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6062 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006063
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006064 /* Export the key again if permitted by the key policy. */
6065 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006066 {
Ronald Cron5425a212020-08-04 14:58:35 +02006067 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006068 second_export, export_size,
6069 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006070 ASSERT_COMPARE( first_export, first_exported_length,
6071 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006072 }
6073
6074 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006075 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006076 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006077
6078exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006079 /*
6080 * Key attributes may have been returned by psa_get_key_attributes()
6081 * thus reset them as required.
6082 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006083 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006084
Darryl Greend49a4992018-06-18 17:27:26 +01006085 mbedtls_free( first_export );
6086 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006087 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006088 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006089 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006090 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006091}
6092/* END_CASE */