blob: 9fb8363a42d232eb788259b6c9dd3d1c0163e6d6 [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 Elliott33746aa2021-09-15 16:40:40 +0100267typedef enum
268{
269 DO_NOT_SET_LENGTHS = 0,
270 SET_LENGTHS_BEFORE_NONCE = 1,
271 SET_LENGTHS_AFTER_NONCE = 2
272} setlengths_method;
273
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100274typedef enum
275{
276 USE_NULL_TAG = 0,
277 USE_GIVEN_TAG = 1,
278} tagusage_method;
279
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100280/*!
281 * \brief Internal Function for AEAD multipart tests.
282 *
283 * \param key_type_arg Type of key passed in
284 * \param key_data The encryption / decryption key data
285 * \param alg_arg The type of algorithm used
286 * \param nonce Nonce data
287 * \param additional_data Additional data
288 * \param ad_part_len If not -1, the length of chunks to
289 * feed additional data in to be encrypted /
290 * decrypted. If -1, no chunking.
291 * \param input_data Data to encrypt / decrypt
292 * \param data_part_len If not -1, the length of chunks to feed the
293 * data in to be encrypted / decrypted. If -1,
294 * no chunking
295 * \param do_set_lengths If non-zero, then set lengths prior to
296 * calling encryption / decryption.
297 * \param expected_output Expected output
Paul Elliott41ffae12021-07-22 21:52:01 +0100298 * \param expect_valid_signature If non zero, we expect the signature to be
299 * valid
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100300 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100301 * \param do_zero_parts If non-zero, interleave zero length chunks
302 * with normal length chunks
303 * \param swap_set_functions If non-zero, swap the order of set lengths
304 * and set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100305 *
306 * \return int Zero on failure, non-zero on success.
307 *
308 */
309static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
310 int alg_arg,
311 data_t *nonce,
312 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100313 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100315 int data_part_len_arg,
Paul Elliott33746aa2021-09-15 16:40:40 +0100316 setlengths_method set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100318 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100319 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100320{
321 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
322 psa_key_type_t key_type = key_type_arg;
323 psa_algorithm_t alg = alg_arg;
324 psa_aead_operation_t operation;
325 unsigned char *output_data = NULL;
326 unsigned char *part_data = NULL;
327 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100328 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100329 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100330 size_t output_size = 0;
331 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100332 size_t output_length = 0;
333 size_t key_bits = 0;
334 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100335 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100336 size_t part_length = 0;
337 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100339 size_t ad_part_len = 0;
340 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
343 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100346 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100347
Paul Elliottd3f82412021-06-16 16:52:21 +0100348 PSA_ASSERT( psa_crypto_init( ) );
349
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100350 if( is_encrypt )
351 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
352 else
353 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
354
Paul Elliottd3f82412021-06-16 16:52:21 +0100355 psa_set_key_algorithm( &attributes, alg );
356 psa_set_key_type( &attributes, key_type );
357
358 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
359 &key ) );
360
361 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
362 key_bits = psa_get_key_bits( &attributes );
363
364 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
365
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100366 if( is_encrypt )
367 {
368 /* Tag gets written at end of buffer. */
369 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
370 ( input_data->len +
371 tag_length ) );
372 data_true_size = input_data->len;
373 }
374 else
375 {
376 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
377 ( input_data->len -
378 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100379
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100380 /* Do not want to attempt to decrypt tag. */
381 data_true_size = input_data->len - tag_length;
382 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100383
384 ASSERT_ALLOC( output_data, output_size );
385
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100386 if( is_encrypt )
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
391 else
392 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100393 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
394 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100395 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100396
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100397 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100398
399 operation = psa_aead_operation_init( );
400
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100401
402 if( is_encrypt )
403 status = psa_aead_encrypt_setup( &operation, key, alg );
404 else
405 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
407 /* If the operation is not supported, just skip and not fail in case the
408 * encryption involves a common limitation of cryptography hardwares and
409 * an alternative implementation. */
410 if( status == PSA_ERROR_NOT_SUPPORTED )
411 {
412 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
413 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
414 }
415
416 PSA_ASSERT( status );
417
Paul Elliott33746aa2021-09-15 16:40:40 +0100418 if( set_lengths_method == DO_NOT_SET_LENGTHS )
419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100421 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100422 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
423 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100424 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
425 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100426 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100427 {
428 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
429
Paul Elliott33746aa2021-09-15 16:40:40 +0100430 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
431 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100432 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100434 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100435 {
436 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100437 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100438
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100439 for( part_offset = 0, part_count = 0;
440 part_offset < additional_data->len;
441 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100443 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100444 {
Paul Elliott329d5382021-07-22 17:10:45 +0100445 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100447 else if( additional_data->len - part_offset < ad_part_len )
448 {
449 part_length = additional_data->len - part_offset;
450 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 else
452 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100453 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100454 }
455
456 PSA_ASSERT( psa_aead_update_ad( &operation,
457 additional_data->x + part_offset,
458 part_length ) );
459
Paul Elliottd3f82412021-06-16 16:52:21 +0100460 }
461 }
462 else
463 {
464 /* Pass additional data in one go. */
465 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
466 additional_data->len ) );
467 }
468
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100469 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100470 {
471 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100472 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100473 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100474 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100475
476 ASSERT_ALLOC( part_data, part_data_size );
477
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100478 for( part_offset = 0, part_count = 0;
479 part_offset < data_true_size;
480 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100482 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 {
Paul Elliott329d5382021-07-22 17:10:45 +0100484 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100486 else if( ( data_true_size - part_offset ) < data_part_len )
487 {
488 part_length = ( data_true_size - part_offset );
489 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 else
491 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100492 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100493 }
494
495 PSA_ASSERT( psa_aead_update( &operation,
496 ( input_data->x + part_offset ),
497 part_length, part_data,
498 part_data_size,
499 &output_part_length ) );
500
501 if( output_data && output_part_length )
502 {
503 memcpy( ( output_data + part_offset ), part_data,
504 output_part_length );
505 }
506
Paul Elliottd3f82412021-06-16 16:52:21 +0100507 output_length += output_part_length;
508 }
509 }
510 else
511 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100512 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100514 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100515 output_size, &output_length ) );
516 }
517
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100518 if( is_encrypt )
519 PSA_ASSERT( psa_aead_finish( &operation, final_data,
520 final_output_size,
521 &output_part_length,
522 tag_buffer, tag_length,
523 &tag_size ) );
524 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100525 {
Paul Elliott9961a662021-09-17 19:19:02 +0100526 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100527 final_output_size,
528 &output_part_length,
529 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100530 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100531 }
532
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100533 if( output_data && output_part_length )
534 memcpy( ( output_data + output_length ), final_data,
535 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100536
537 output_length += output_part_length;
538
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100539
540 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
541 * should be exact.*/
542 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100543 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100544 TEST_EQUAL( tag_length, tag_size );
545
546 if( output_data && tag_length )
547 memcpy( ( output_data + output_length ), tag_buffer,
548 tag_length );
549
550 output_length += tag_length;
551
552 TEST_EQUAL( output_length,
553 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
554 input_data->len ) );
555 TEST_ASSERT( output_length <=
556 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
557 }
558 else
559 {
560 TEST_EQUAL( output_length,
561 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
562 input_data->len ) );
563 TEST_ASSERT( output_length <=
564 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100565 }
566
Paul Elliottd3f82412021-06-16 16:52:21 +0100567
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100568 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100569 output_data, output_length );
570
Paul Elliottd3f82412021-06-16 16:52:21 +0100571
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100572 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100573
574exit:
575 psa_destroy_key( key );
576 psa_aead_abort( &operation );
577 mbedtls_free( output_data );
578 mbedtls_free( part_data );
579 mbedtls_free( final_data );
580 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581
582 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100583}
584
Gilles Peskinee59236f2018-01-27 23:32:46 +0100585/* END_HEADER */
586
587/* BEGIN_DEPENDENCIES
588 * depends_on:MBEDTLS_PSA_CRYPTO_C
589 * END_DEPENDENCIES
590 */
591
592/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200593void static_checks( )
594{
595 size_t max_truncated_mac_size =
596 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
597
598 /* Check that the length for a truncated MAC always fits in the algorithm
599 * encoding. The shifted mask is the maximum truncated value. The
600 * untruncated algorithm may be one byte larger. */
601 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
602}
603/* END_CASE */
604
605/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200606void import_with_policy( int type_arg,
607 int usage_arg, int alg_arg,
608 int expected_status_arg )
609{
610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
611 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200613 psa_key_type_t type = type_arg;
614 psa_key_usage_t usage = usage_arg;
615 psa_algorithm_t alg = alg_arg;
616 psa_status_t expected_status = expected_status_arg;
617 const uint8_t key_material[16] = {0};
618 psa_status_t status;
619
620 PSA_ASSERT( psa_crypto_init( ) );
621
622 psa_set_key_type( &attributes, type );
623 psa_set_key_usage_flags( &attributes, usage );
624 psa_set_key_algorithm( &attributes, alg );
625
626 status = psa_import_key( &attributes,
627 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200628 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200629 TEST_EQUAL( status, expected_status );
630 if( status != PSA_SUCCESS )
631 goto exit;
632
Ronald Cron5425a212020-08-04 14:58:35 +0200633 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200634 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
635 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
636 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200637 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200638
Ronald Cron5425a212020-08-04 14:58:35 +0200639 PSA_ASSERT( psa_destroy_key( key ) );
640 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641
642exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100643 /*
644 * Key attributes may have been returned by psa_get_key_attributes()
645 * thus reset them as required.
646 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200647 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100648
649 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200650 PSA_DONE( );
651}
652/* END_CASE */
653
654/* BEGIN_CASE */
655void import_with_data( data_t *data, int type_arg,
656 int attr_bits_arg,
657 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200658{
659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
660 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200662 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200663 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200664 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666
Gilles Peskine8817f612018-12-18 00:18:46 +0100667 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668
Gilles Peskine4747d192019-04-17 15:05:45 +0200669 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200670 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200671
Ronald Cron5425a212020-08-04 14:58:35 +0200672 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100673 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 if( status != PSA_SUCCESS )
675 goto exit;
676
Ronald Cron5425a212020-08-04 14:58:35 +0200677 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200678 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200679 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200680 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200681 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200682
Ronald Cron5425a212020-08-04 14:58:35 +0200683 PSA_ASSERT( psa_destroy_key( key ) );
684 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100685
686exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100687 /*
688 * Key attributes may have been returned by psa_get_key_attributes()
689 * thus reset them as required.
690 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200691 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100692
693 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200694 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100695}
696/* END_CASE */
697
698/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200699void import_large_key( int type_arg, int byte_size_arg,
700 int expected_status_arg )
701{
702 psa_key_type_t type = type_arg;
703 size_t byte_size = byte_size_arg;
704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
705 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200706 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200707 psa_status_t status;
708 uint8_t *buffer = NULL;
709 size_t buffer_size = byte_size + 1;
710 size_t n;
711
Steven Cooreman69967ce2021-01-18 18:01:08 +0100712 /* Skip the test case if the target running the test cannot
713 * accomodate large keys due to heap size constraints */
714 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200715 memset( buffer, 'K', byte_size );
716
717 PSA_ASSERT( psa_crypto_init( ) );
718
719 /* Try importing the key */
720 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
721 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200722 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100723 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200724 TEST_EQUAL( status, expected_status );
725
726 if( status == PSA_SUCCESS )
727 {
Ronald Cron5425a212020-08-04 14:58:35 +0200728 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200729 TEST_EQUAL( psa_get_key_type( &attributes ), type );
730 TEST_EQUAL( psa_get_key_bits( &attributes ),
731 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200732 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200733 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200734 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200735 for( n = 0; n < byte_size; n++ )
736 TEST_EQUAL( buffer[n], 'K' );
737 for( n = byte_size; n < buffer_size; n++ )
738 TEST_EQUAL( buffer[n], 0 );
739 }
740
741exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100742 /*
743 * Key attributes may have been returned by psa_get_key_attributes()
744 * thus reset them as required.
745 */
746 psa_reset_key_attributes( &attributes );
747
Ronald Cron5425a212020-08-04 14:58:35 +0200748 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200749 PSA_DONE( );
750 mbedtls_free( buffer );
751}
752/* END_CASE */
753
754/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200755void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
756{
Ronald Cron5425a212020-08-04 14:58:35 +0200757 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200758 size_t bits = bits_arg;
759 psa_status_t expected_status = expected_status_arg;
760 psa_status_t status;
761 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200762 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763 size_t buffer_size = /* Slight overapproximations */
764 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200765 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200766 unsigned char *p;
767 int ret;
768 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200769 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200770
Gilles Peskine8817f612018-12-18 00:18:46 +0100771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200772 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200773
774 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
775 bits, keypair ) ) >= 0 );
776 length = ret;
777
778 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200779 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200780 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100781 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200782
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200783 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200784 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200785
786exit:
787 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200788 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200789}
790/* END_CASE */
791
792/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300793void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300794 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200795 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100796 int expected_bits,
797 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200798 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100799 int canonical_input )
800{
Ronald Cron5425a212020-08-04 14:58:35 +0200801 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100802 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200803 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200804 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100806 unsigned char *exported = NULL;
807 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100809 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100810 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200812 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100813
Moran Pekercb088e72018-07-17 17:36:59 +0300814 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200815 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200817 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819
Gilles Peskine4747d192019-04-17 15:05:45 +0200820 psa_set_key_usage_flags( &attributes, usage_arg );
821 psa_set_key_algorithm( &attributes, alg );
822 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700823
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200825 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826
827 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200828 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200829 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
830 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200831 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100832
833 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200834 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100835 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100836
837 /* The exported length must be set by psa_export_key() to a value between 0
838 * and export_size. On errors, the exported length must be 0. */
839 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
840 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
841 TEST_ASSERT( exported_length <= export_size );
842
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200843 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200844 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200846 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100847 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100848 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200849 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850
Gilles Peskineea38a922021-02-13 00:05:16 +0100851 /* Run sanity checks on the exported key. For non-canonical inputs,
852 * this validates the canonical representations. For canonical inputs,
853 * this doesn't directly validate the implementation, but it still helps
854 * by cross-validating the test data with the sanity check code. */
855 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200856 goto exit;
857
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200859 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100860 else
861 {
Ronald Cron5425a212020-08-04 14:58:35 +0200862 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200863 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200864 &key2 ) );
865 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100866 reexported,
867 export_size,
868 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200869 ASSERT_COMPARE( exported, exported_length,
870 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200871 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100873 TEST_ASSERT( exported_length <=
874 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
875 psa_get_key_bits( &got_attributes ) ) );
876 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877
878destroy:
879 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200880 PSA_ASSERT( psa_destroy_key( key ) );
881 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100882
883exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100884 /*
885 * Key attributes may have been returned by psa_get_key_attributes()
886 * thus reset them as required.
887 */
888 psa_reset_key_attributes( &got_attributes );
889
itayzafrir3e02b3b2018-06-12 17:06:52 +0300890 mbedtls_free( exported );
891 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200892 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893}
894/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100895
Moran Pekerf709f4a2018-06-06 17:26:04 +0300896/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300897void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200898 int type_arg,
899 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100900 int export_size_delta,
901 int expected_export_status_arg,
902 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300903{
Ronald Cron5425a212020-08-04 14:58:35 +0200904 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300905 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200906 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200907 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300908 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300909 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100910 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100911 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300913
Gilles Peskine8817f612018-12-18 00:18:46 +0100914 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300915
Gilles Peskine4747d192019-04-17 15:05:45 +0200916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
917 psa_set_key_algorithm( &attributes, alg );
918 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300919
920 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200921 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300922
Gilles Peskine49c25912018-10-29 15:15:31 +0100923 /* Export the public key */
924 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200925 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200926 exported, export_size,
927 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100928 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100929 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100930 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200931 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100932 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200933 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200934 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100935 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100936 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100937 TEST_ASSERT( expected_public_key->len <=
938 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
939 TEST_ASSERT( expected_public_key->len <=
940 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100941 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
942 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100943 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300944
945exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100946 /*
947 * Key attributes may have been returned by psa_get_key_attributes()
948 * thus reset them as required.
949 */
950 psa_reset_key_attributes( &attributes );
951
itayzafrir3e02b3b2018-06-12 17:06:52 +0300952 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200953 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200954 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300955}
956/* END_CASE */
957
Gilles Peskine20035e32018-02-03 22:44:14 +0100958/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200959void import_and_exercise_key( data_t *data,
960 int type_arg,
961 int bits_arg,
962 int alg_arg )
963{
Ronald Cron5425a212020-08-04 14:58:35 +0200964 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200965 psa_key_type_t type = type_arg;
966 size_t bits = bits_arg;
967 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100968 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200969 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200970 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200971
Gilles Peskine8817f612018-12-18 00:18:46 +0100972 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200973
Gilles Peskine4747d192019-04-17 15:05:45 +0200974 psa_set_key_usage_flags( &attributes, usage );
975 psa_set_key_algorithm( &attributes, alg );
976 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200977
978 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200979 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980
981 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200982 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200983 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
984 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200985
986 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100987 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200988 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
Ronald Cron5425a212020-08-04 14:58:35 +0200990 PSA_ASSERT( psa_destroy_key( key ) );
991 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200992
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100994 /*
995 * Key attributes may have been returned by psa_get_key_attributes()
996 * thus reset them as required.
997 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200998 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100999
1000 psa_reset_key_attributes( &attributes );
1001 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001002 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001003}
1004/* END_CASE */
1005
1006/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001007void effective_key_attributes( int type_arg, int expected_type_arg,
1008 int bits_arg, int expected_bits_arg,
1009 int usage_arg, int expected_usage_arg,
1010 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001011{
Ronald Cron5425a212020-08-04 14:58:35 +02001012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001013 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001014 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001015 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001016 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001017 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001018 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001019 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001020 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001022
Gilles Peskine8817f612018-12-18 00:18:46 +01001023 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001024
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001025 psa_set_key_usage_flags( &attributes, usage );
1026 psa_set_key_algorithm( &attributes, alg );
1027 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001028 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001029
Ronald Cron5425a212020-08-04 14:58:35 +02001030 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001031 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001032
Ronald Cron5425a212020-08-04 14:58:35 +02001033 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001034 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1035 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1036 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1037 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
1039exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001040 /*
1041 * Key attributes may have been returned by psa_get_key_attributes()
1042 * thus reset them as required.
1043 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001044 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001045
1046 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001047 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048}
1049/* END_CASE */
1050
1051/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001052void check_key_policy( int type_arg, int bits_arg,
1053 int usage_arg, int alg_arg )
1054{
1055 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1056 usage_arg, usage_arg, alg_arg, alg_arg );
1057 goto exit;
1058}
1059/* END_CASE */
1060
1061/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001062void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001063{
1064 /* Test each valid way of initializing the object, except for `= {0}`, as
1065 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1066 * though it's OK by the C standard. We could test for this, but we'd need
1067 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001068 psa_key_attributes_t func = psa_key_attributes_init( );
1069 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1070 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001071
1072 memset( &zero, 0, sizeof( zero ) );
1073
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001074 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1075 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1076 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001077
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001078 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1079 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1080 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1081
1082 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1083 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1084 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1085
1086 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1087 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1088 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1089
1090 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1091 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1092 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001093}
1094/* END_CASE */
1095
1096/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001097void mac_key_policy( int policy_usage,
1098 int policy_alg,
1099 int key_type,
1100 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001101 int exercise_alg,
1102 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001103{
Ronald Cron5425a212020-08-04 14:58:35 +02001104 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001106 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001107 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001108 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001109 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001110
Gilles Peskine8817f612018-12-18 00:18:46 +01001111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001112
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001113 psa_set_key_usage_flags( &attributes, policy_usage );
1114 psa_set_key_algorithm( &attributes, policy_alg );
1115 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001116
Gilles Peskine049c7532019-05-15 20:22:09 +02001117 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001118 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001119
Ronald Cron5425a212020-08-04 14:58:35 +02001120 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001121 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001122 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001123 else
1124 TEST_EQUAL( status, expected_status );
1125
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001126 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001127
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001128 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001129 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001130 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001131 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001132 else
1133 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001134
1135exit:
1136 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001137 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001138 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139}
1140/* END_CASE */
1141
1142/* BEGIN_CASE */
1143void cipher_key_policy( int policy_usage,
1144 int policy_alg,
1145 int key_type,
1146 data_t *key_data,
1147 int exercise_alg )
1148{
Ronald Cron5425a212020-08-04 14:58:35 +02001149 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001151 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001152 psa_status_t status;
1153
Gilles Peskine8817f612018-12-18 00:18:46 +01001154 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001155
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001156 psa_set_key_usage_flags( &attributes, policy_usage );
1157 psa_set_key_algorithm( &attributes, policy_alg );
1158 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159
Gilles Peskine049c7532019-05-15 20:22:09 +02001160 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001161 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001162
Ronald Cron5425a212020-08-04 14:58:35 +02001163 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164 if( policy_alg == exercise_alg &&
1165 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001166 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001168 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169 psa_cipher_abort( &operation );
1170
Ronald Cron5425a212020-08-04 14:58:35 +02001171 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172 if( policy_alg == exercise_alg &&
1173 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001174 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001175 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001176 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177
1178exit:
1179 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001180 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001181 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182}
1183/* END_CASE */
1184
1185/* BEGIN_CASE */
1186void aead_key_policy( int policy_usage,
1187 int policy_alg,
1188 int key_type,
1189 data_t *key_data,
1190 int nonce_length_arg,
1191 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001192 int exercise_alg,
1193 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194{
Ronald Cron5425a212020-08-04 14:58:35 +02001195 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001197 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001198 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001199 unsigned char nonce[16] = {0};
1200 size_t nonce_length = nonce_length_arg;
1201 unsigned char tag[16];
1202 size_t tag_length = tag_length_arg;
1203 size_t output_length;
1204
1205 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1206 TEST_ASSERT( tag_length <= sizeof( tag ) );
1207
Gilles Peskine8817f612018-12-18 00:18:46 +01001208 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001210 psa_set_key_usage_flags( &attributes, policy_usage );
1211 psa_set_key_algorithm( &attributes, policy_alg );
1212 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213
Gilles Peskine049c7532019-05-15 20:22:09 +02001214 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001215 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001216
Ronald Cron5425a212020-08-04 14:58:35 +02001217 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218 nonce, nonce_length,
1219 NULL, 0,
1220 NULL, 0,
1221 tag, tag_length,
1222 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001223 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1224 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001226 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001227
1228 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001229 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 nonce, nonce_length,
1231 NULL, 0,
1232 tag, tag_length,
1233 NULL, 0,
1234 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001235 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1236 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1237 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001238 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001239 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001240 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001241
1242exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001243 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001244 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001245}
1246/* END_CASE */
1247
1248/* BEGIN_CASE */
1249void asymmetric_encryption_key_policy( int policy_usage,
1250 int policy_alg,
1251 int key_type,
1252 data_t *key_data,
1253 int exercise_alg )
1254{
Ronald Cron5425a212020-08-04 14:58:35 +02001255 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001257 psa_status_t status;
1258 size_t key_bits;
1259 size_t buffer_length;
1260 unsigned char *buffer = NULL;
1261 size_t output_length;
1262
Gilles Peskine8817f612018-12-18 00:18:46 +01001263 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001264
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001265 psa_set_key_usage_flags( &attributes, policy_usage );
1266 psa_set_key_algorithm( &attributes, policy_alg );
1267 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001268
Gilles Peskine049c7532019-05-15 20:22:09 +02001269 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001270 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001271
Ronald Cron5425a212020-08-04 14:58:35 +02001272 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001273 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001274 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1275 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001276 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277
Ronald Cron5425a212020-08-04 14:58:35 +02001278 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001279 NULL, 0,
1280 NULL, 0,
1281 buffer, buffer_length,
1282 &output_length );
1283 if( policy_alg == exercise_alg &&
1284 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001285 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001286 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001287 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001288
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001289 if( buffer_length != 0 )
1290 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001291 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001292 buffer, buffer_length,
1293 NULL, 0,
1294 buffer, buffer_length,
1295 &output_length );
1296 if( policy_alg == exercise_alg &&
1297 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001298 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001299 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001300 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001301
1302exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001303 /*
1304 * Key attributes may have been returned by psa_get_key_attributes()
1305 * thus reset them as required.
1306 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001307 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001308
1309 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001310 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001311 mbedtls_free( buffer );
1312}
1313/* END_CASE */
1314
1315/* BEGIN_CASE */
1316void asymmetric_signature_key_policy( int policy_usage,
1317 int policy_alg,
1318 int key_type,
1319 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001320 int exercise_alg,
1321 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001322{
Ronald Cron5425a212020-08-04 14:58:35 +02001323 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001325 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001326 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1327 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1328 * compatible with the policy and `payload_length_arg` is supposed to be
1329 * a valid input length to sign. If `payload_length_arg <= 0`,
1330 * `exercise_alg` is supposed to be forbidden by the policy. */
1331 int compatible_alg = payload_length_arg > 0;
1332 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001333 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001334 size_t signature_length;
1335
Gilles Peskine8817f612018-12-18 00:18:46 +01001336 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001337
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001338 psa_set_key_usage_flags( &attributes, policy_usage );
1339 psa_set_key_algorithm( &attributes, policy_alg );
1340 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001341
Gilles Peskine049c7532019-05-15 20:22:09 +02001342 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001343 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001344
Ronald Cron5425a212020-08-04 14:58:35 +02001345 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001346 payload, payload_length,
1347 signature, sizeof( signature ),
1348 &signature_length );
1349 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001350 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001351 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001352 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001353
1354 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001355 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001356 payload, payload_length,
1357 signature, sizeof( signature ) );
1358 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001359 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001360 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001361 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001362
1363exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001364 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001365 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001366}
1367/* END_CASE */
1368
Janos Follathba3fab92019-06-11 14:50:16 +01001369/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001370void derive_key_policy( int policy_usage,
1371 int policy_alg,
1372 int key_type,
1373 data_t *key_data,
1374 int exercise_alg )
1375{
Ronald Cron5425a212020-08-04 14:58:35 +02001376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001378 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001379 psa_status_t status;
1380
Gilles Peskine8817f612018-12-18 00:18:46 +01001381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001382
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001383 psa_set_key_usage_flags( &attributes, policy_usage );
1384 psa_set_key_algorithm( &attributes, policy_alg );
1385 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001386
Gilles Peskine049c7532019-05-15 20:22:09 +02001387 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001388 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001389
Janos Follathba3fab92019-06-11 14:50:16 +01001390 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1391
1392 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1393 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001394 {
Janos Follathba3fab92019-06-11 14:50:16 +01001395 PSA_ASSERT( psa_key_derivation_input_bytes(
1396 &operation,
1397 PSA_KEY_DERIVATION_INPUT_SEED,
1398 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001399 }
Janos Follathba3fab92019-06-11 14:50:16 +01001400
1401 status = psa_key_derivation_input_key( &operation,
1402 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001403 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001404
Gilles Peskineea0fb492018-07-12 17:17:20 +02001405 if( policy_alg == exercise_alg &&
1406 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001407 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001408 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001409 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001410
1411exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001412 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001413 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001414 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001415}
1416/* END_CASE */
1417
1418/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001419void agreement_key_policy( int policy_usage,
1420 int policy_alg,
1421 int key_type_arg,
1422 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001423 int exercise_alg,
1424 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001425{
Ronald Cron5425a212020-08-04 14:58:35 +02001426 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001428 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001429 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001430 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001431 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001432
Gilles Peskine8817f612018-12-18 00:18:46 +01001433 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001434
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001435 psa_set_key_usage_flags( &attributes, policy_usage );
1436 psa_set_key_algorithm( &attributes, policy_alg );
1437 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001438
Gilles Peskine049c7532019-05-15 20:22:09 +02001439 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001440 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001442 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001443 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001444
Steven Cooremance48e852020-10-05 16:02:45 +02001445 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001446
1447exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001448 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001449 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001450 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001451}
1452/* END_CASE */
1453
1454/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001455void key_policy_alg2( int key_type_arg, data_t *key_data,
1456 int usage_arg, int alg_arg, int alg2_arg )
1457{
Ronald Cron5425a212020-08-04 14:58:35 +02001458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001459 psa_key_type_t key_type = key_type_arg;
1460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1461 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1462 psa_key_usage_t usage = usage_arg;
1463 psa_algorithm_t alg = alg_arg;
1464 psa_algorithm_t alg2 = alg2_arg;
1465
1466 PSA_ASSERT( psa_crypto_init( ) );
1467
1468 psa_set_key_usage_flags( &attributes, usage );
1469 psa_set_key_algorithm( &attributes, alg );
1470 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1471 psa_set_key_type( &attributes, key_type );
1472 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001473 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001474
Ronald Cron5425a212020-08-04 14:58:35 +02001475 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001476 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1477 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1478 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1479
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001480 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001481 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001482 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001483 goto exit;
1484
1485exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001486 /*
1487 * Key attributes may have been returned by psa_get_key_attributes()
1488 * thus reset them as required.
1489 */
1490 psa_reset_key_attributes( &got_attributes );
1491
Ronald Cron5425a212020-08-04 14:58:35 +02001492 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001493 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001494}
1495/* END_CASE */
1496
1497/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001498void raw_agreement_key_policy( int policy_usage,
1499 int policy_alg,
1500 int key_type_arg,
1501 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001502 int exercise_alg,
1503 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001504{
Ronald Cron5425a212020-08-04 14:58:35 +02001505 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001507 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001508 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001509 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001510 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001511
1512 PSA_ASSERT( psa_crypto_init( ) );
1513
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001514 psa_set_key_usage_flags( &attributes, policy_usage );
1515 psa_set_key_algorithm( &attributes, policy_alg );
1516 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517
Gilles Peskine049c7532019-05-15 20:22:09 +02001518 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001519 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001520
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001521 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001522
Steven Cooremance48e852020-10-05 16:02:45 +02001523 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001524
1525exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001526 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001527 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001528 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001529}
1530/* END_CASE */
1531
1532/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001533void copy_success( int source_usage_arg,
1534 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001535 int type_arg, data_t *material,
1536 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001537 int target_usage_arg,
1538 int target_alg_arg, int target_alg2_arg,
1539 int expected_usage_arg,
1540 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001541{
Gilles Peskineca25db92019-04-19 11:43:08 +02001542 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1543 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001544 psa_key_usage_t expected_usage = expected_usage_arg;
1545 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001546 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001547 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1548 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001549 uint8_t *export_buffer = NULL;
1550
Gilles Peskine57ab7212019-01-28 13:03:09 +01001551 PSA_ASSERT( psa_crypto_init( ) );
1552
Gilles Peskineca25db92019-04-19 11:43:08 +02001553 /* Prepare the source key. */
1554 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1555 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001556 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001557 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001558 PSA_ASSERT( psa_import_key( &source_attributes,
1559 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001560 &source_key ) );
1561 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001562
Gilles Peskineca25db92019-04-19 11:43:08 +02001563 /* Prepare the target attributes. */
1564 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001565 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001567 /* Set volatile lifetime to reset the key identifier to 0. */
1568 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1569 }
1570
Gilles Peskineca25db92019-04-19 11:43:08 +02001571 if( target_usage_arg != -1 )
1572 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1573 if( target_alg_arg != -1 )
1574 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001575 if( target_alg2_arg != -1 )
1576 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001577
1578 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001579 PSA_ASSERT( psa_copy_key( source_key,
1580 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001581
1582 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001583 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001584
1585 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001586 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001587 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1588 psa_get_key_type( &target_attributes ) );
1589 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1590 psa_get_key_bits( &target_attributes ) );
1591 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1592 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001593 TEST_EQUAL( expected_alg2,
1594 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001595 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1596 {
1597 size_t length;
1598 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001599 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001600 material->len, &length ) );
1601 ASSERT_COMPARE( material->x, material->len,
1602 export_buffer, length );
1603 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001604
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001605 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001606 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001607 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001608 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001609
Ronald Cron5425a212020-08-04 14:58:35 +02001610 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001611
1612exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001613 /*
1614 * Source and target key attributes may have been returned by
1615 * psa_get_key_attributes() thus reset them as required.
1616 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001617 psa_reset_key_attributes( &source_attributes );
1618 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001619
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001620 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001621 mbedtls_free( export_buffer );
1622}
1623/* END_CASE */
1624
1625/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001626void copy_fail( int source_usage_arg,
1627 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001628 int type_arg, data_t *material,
1629 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001630 int target_usage_arg,
1631 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001632 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001633 int expected_status_arg )
1634{
1635 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1636 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001637 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1638 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001639 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001640
1641 PSA_ASSERT( psa_crypto_init( ) );
1642
1643 /* Prepare the source key. */
1644 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1645 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001646 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001647 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001648 PSA_ASSERT( psa_import_key( &source_attributes,
1649 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001650 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001651
1652 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001653 psa_set_key_id( &target_attributes, key_id );
1654 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001655 psa_set_key_type( &target_attributes, target_type_arg );
1656 psa_set_key_bits( &target_attributes, target_bits_arg );
1657 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1658 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001659 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001660
1661 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001662 TEST_EQUAL( psa_copy_key( source_key,
1663 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001664 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001665
Ronald Cron5425a212020-08-04 14:58:35 +02001666 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001667
Gilles Peskine4a644642019-05-03 17:14:08 +02001668exit:
1669 psa_reset_key_attributes( &source_attributes );
1670 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001671 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001672}
1673/* END_CASE */
1674
1675/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001676void hash_operation_init( )
1677{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001678 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001679 /* Test each valid way of initializing the object, except for `= {0}`, as
1680 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1681 * though it's OK by the C standard. We could test for this, but we'd need
1682 * to supress the Clang warning for the test. */
1683 psa_hash_operation_t func = psa_hash_operation_init( );
1684 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1685 psa_hash_operation_t zero;
1686
1687 memset( &zero, 0, sizeof( zero ) );
1688
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001689 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001690 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1691 PSA_ERROR_BAD_STATE );
1692 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1693 PSA_ERROR_BAD_STATE );
1694 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1695 PSA_ERROR_BAD_STATE );
1696
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001697 /* A default hash operation should be abortable without error. */
1698 PSA_ASSERT( psa_hash_abort( &func ) );
1699 PSA_ASSERT( psa_hash_abort( &init ) );
1700 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001701}
1702/* END_CASE */
1703
1704/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001705void hash_setup( int alg_arg,
1706 int expected_status_arg )
1707{
1708 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001709 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001710 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001711 psa_status_t status;
1712
Gilles Peskine8817f612018-12-18 00:18:46 +01001713 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001714
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001715 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001716 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001717
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001718 /* Whether setup succeeded or failed, abort must succeed. */
1719 PSA_ASSERT( psa_hash_abort( &operation ) );
1720
1721 /* If setup failed, reproduce the failure, so as to
1722 * test the resulting state of the operation object. */
1723 if( status != PSA_SUCCESS )
1724 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1725
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001726 /* Now the operation object should be reusable. */
1727#if defined(KNOWN_SUPPORTED_HASH_ALG)
1728 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1729 PSA_ASSERT( psa_hash_abort( &operation ) );
1730#endif
1731
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001732exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001733 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001734}
1735/* END_CASE */
1736
1737/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001738void hash_compute_fail( int alg_arg, data_t *input,
1739 int output_size_arg, int expected_status_arg )
1740{
1741 psa_algorithm_t alg = alg_arg;
1742 uint8_t *output = NULL;
1743 size_t output_size = output_size_arg;
1744 size_t output_length = INVALID_EXPORT_LENGTH;
1745 psa_status_t expected_status = expected_status_arg;
1746 psa_status_t status;
1747
1748 ASSERT_ALLOC( output, output_size );
1749
1750 PSA_ASSERT( psa_crypto_init( ) );
1751
1752 status = psa_hash_compute( alg, input->x, input->len,
1753 output, output_size, &output_length );
1754 TEST_EQUAL( status, expected_status );
1755 TEST_ASSERT( output_length <= output_size );
1756
1757exit:
1758 mbedtls_free( output );
1759 PSA_DONE( );
1760}
1761/* END_CASE */
1762
1763/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001764void hash_compare_fail( int alg_arg, data_t *input,
1765 data_t *reference_hash,
1766 int expected_status_arg )
1767{
1768 psa_algorithm_t alg = alg_arg;
1769 psa_status_t expected_status = expected_status_arg;
1770 psa_status_t status;
1771
1772 PSA_ASSERT( psa_crypto_init( ) );
1773
1774 status = psa_hash_compare( alg, input->x, input->len,
1775 reference_hash->x, reference_hash->len );
1776 TEST_EQUAL( status, expected_status );
1777
1778exit:
1779 PSA_DONE( );
1780}
1781/* END_CASE */
1782
1783/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001784void hash_compute_compare( int alg_arg, data_t *input,
1785 data_t *expected_output )
1786{
1787 psa_algorithm_t alg = alg_arg;
1788 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1789 size_t output_length = INVALID_EXPORT_LENGTH;
1790 size_t i;
1791
1792 PSA_ASSERT( psa_crypto_init( ) );
1793
1794 /* Compute with tight buffer */
1795 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001796 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001797 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001798 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001799 ASSERT_COMPARE( output, output_length,
1800 expected_output->x, expected_output->len );
1801
1802 /* Compute with larger buffer */
1803 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1804 output, sizeof( output ),
1805 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001806 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001807 ASSERT_COMPARE( output, output_length,
1808 expected_output->x, expected_output->len );
1809
1810 /* Compare with correct hash */
1811 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1812 output, output_length ) );
1813
1814 /* Compare with trailing garbage */
1815 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1816 output, output_length + 1 ),
1817 PSA_ERROR_INVALID_SIGNATURE );
1818
1819 /* Compare with truncated hash */
1820 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1821 output, output_length - 1 ),
1822 PSA_ERROR_INVALID_SIGNATURE );
1823
1824 /* Compare with corrupted value */
1825 for( i = 0; i < output_length; i++ )
1826 {
Chris Jones9634bb12021-01-20 15:56:42 +00001827 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001828 output[i] ^= 1;
1829 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1830 output, output_length ),
1831 PSA_ERROR_INVALID_SIGNATURE );
1832 output[i] ^= 1;
1833 }
1834
1835exit:
1836 PSA_DONE( );
1837}
1838/* END_CASE */
1839
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001840/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001841void hash_bad_order( )
1842{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001843 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001844 unsigned char input[] = "";
1845 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001846 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001847 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1848 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1849 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001850 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001851 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001852 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001853
Gilles Peskine8817f612018-12-18 00:18:46 +01001854 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001855
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001856 /* Call setup twice in a row. */
1857 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1858 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1859 PSA_ERROR_BAD_STATE );
1860 PSA_ASSERT( psa_hash_abort( &operation ) );
1861
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001862 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001863 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001864 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001865 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001866
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001867 /* Call update after finish. */
1868 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1869 PSA_ASSERT( psa_hash_finish( &operation,
1870 hash, sizeof( hash ), &hash_len ) );
1871 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001872 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001873 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001874
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001875 /* Call verify without calling setup beforehand. */
1876 TEST_EQUAL( psa_hash_verify( &operation,
1877 valid_hash, sizeof( valid_hash ) ),
1878 PSA_ERROR_BAD_STATE );
1879 PSA_ASSERT( psa_hash_abort( &operation ) );
1880
1881 /* Call verify after finish. */
1882 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1883 PSA_ASSERT( psa_hash_finish( &operation,
1884 hash, sizeof( hash ), &hash_len ) );
1885 TEST_EQUAL( psa_hash_verify( &operation,
1886 valid_hash, sizeof( valid_hash ) ),
1887 PSA_ERROR_BAD_STATE );
1888 PSA_ASSERT( psa_hash_abort( &operation ) );
1889
1890 /* Call verify twice in a row. */
1891 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1892 PSA_ASSERT( psa_hash_verify( &operation,
1893 valid_hash, sizeof( valid_hash ) ) );
1894 TEST_EQUAL( psa_hash_verify( &operation,
1895 valid_hash, sizeof( valid_hash ) ),
1896 PSA_ERROR_BAD_STATE );
1897 PSA_ASSERT( psa_hash_abort( &operation ) );
1898
1899 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001900 TEST_EQUAL( psa_hash_finish( &operation,
1901 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001902 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001903 PSA_ASSERT( psa_hash_abort( &operation ) );
1904
1905 /* Call finish twice in a row. */
1906 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1907 PSA_ASSERT( psa_hash_finish( &operation,
1908 hash, sizeof( hash ), &hash_len ) );
1909 TEST_EQUAL( psa_hash_finish( &operation,
1910 hash, sizeof( hash ), &hash_len ),
1911 PSA_ERROR_BAD_STATE );
1912 PSA_ASSERT( psa_hash_abort( &operation ) );
1913
1914 /* Call finish after calling verify. */
1915 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1916 PSA_ASSERT( psa_hash_verify( &operation,
1917 valid_hash, sizeof( valid_hash ) ) );
1918 TEST_EQUAL( psa_hash_finish( &operation,
1919 hash, sizeof( hash ), &hash_len ),
1920 PSA_ERROR_BAD_STATE );
1921 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001922
1923exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001924 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001925}
1926/* END_CASE */
1927
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001928/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001929void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001930{
1931 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001932 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1933 * appended to it */
1934 unsigned char hash[] = {
1935 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1936 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1937 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001938 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001939 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001940
Gilles Peskine8817f612018-12-18 00:18:46 +01001941 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001942
itayzafrir27e69452018-11-01 14:26:34 +02001943 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001944 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001945 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001946 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001947
itayzafrir27e69452018-11-01 14:26:34 +02001948 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001950 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001951 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001952
itayzafrir27e69452018-11-01 14:26:34 +02001953 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001954 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001955 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001956 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001957
itayzafrirec93d302018-10-18 18:01:10 +03001958exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001959 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001960}
1961/* END_CASE */
1962
Ronald Cronee414c72021-03-18 18:50:08 +01001963/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001964void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001965{
1966 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001967 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001968 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001969 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001970 size_t hash_len;
1971
Gilles Peskine8817f612018-12-18 00:18:46 +01001972 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001973
itayzafrir58028322018-10-25 10:22:01 +03001974 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001975 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001976 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001977 hash, expected_size - 1, &hash_len ),
1978 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001979
1980exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001981 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001982}
1983/* END_CASE */
1984
Ronald Cronee414c72021-03-18 18:50:08 +01001985/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001986void hash_clone_source_state( )
1987{
1988 psa_algorithm_t alg = PSA_ALG_SHA_256;
1989 unsigned char hash[PSA_HASH_MAX_SIZE];
1990 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1991 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1992 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1993 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1994 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1995 size_t hash_len;
1996
1997 PSA_ASSERT( psa_crypto_init( ) );
1998 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1999
2000 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2001 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2002 PSA_ASSERT( psa_hash_finish( &op_finished,
2003 hash, sizeof( hash ), &hash_len ) );
2004 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2005 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2006
2007 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2008 PSA_ERROR_BAD_STATE );
2009
2010 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2011 PSA_ASSERT( psa_hash_finish( &op_init,
2012 hash, sizeof( hash ), &hash_len ) );
2013 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2014 PSA_ASSERT( psa_hash_finish( &op_finished,
2015 hash, sizeof( hash ), &hash_len ) );
2016 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2017 PSA_ASSERT( psa_hash_finish( &op_aborted,
2018 hash, sizeof( hash ), &hash_len ) );
2019
2020exit:
2021 psa_hash_abort( &op_source );
2022 psa_hash_abort( &op_init );
2023 psa_hash_abort( &op_setup );
2024 psa_hash_abort( &op_finished );
2025 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002026 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002027}
2028/* END_CASE */
2029
Ronald Cronee414c72021-03-18 18:50:08 +01002030/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002031void hash_clone_target_state( )
2032{
2033 psa_algorithm_t alg = PSA_ALG_SHA_256;
2034 unsigned char hash[PSA_HASH_MAX_SIZE];
2035 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2036 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2037 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2038 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2039 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2040 size_t hash_len;
2041
2042 PSA_ASSERT( psa_crypto_init( ) );
2043
2044 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2045 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2046 PSA_ASSERT( psa_hash_finish( &op_finished,
2047 hash, sizeof( hash ), &hash_len ) );
2048 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2049 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2050
2051 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2052 PSA_ASSERT( psa_hash_finish( &op_target,
2053 hash, sizeof( hash ), &hash_len ) );
2054
2055 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2056 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2057 PSA_ERROR_BAD_STATE );
2058 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2059 PSA_ERROR_BAD_STATE );
2060
2061exit:
2062 psa_hash_abort( &op_target );
2063 psa_hash_abort( &op_init );
2064 psa_hash_abort( &op_setup );
2065 psa_hash_abort( &op_finished );
2066 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002067 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002068}
2069/* END_CASE */
2070
itayzafrir58028322018-10-25 10:22:01 +03002071/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002072void mac_operation_init( )
2073{
Jaeden Amero252ef282019-02-15 14:05:35 +00002074 const uint8_t input[1] = { 0 };
2075
Jaeden Amero769ce272019-01-04 11:48:03 +00002076 /* Test each valid way of initializing the object, except for `= {0}`, as
2077 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2078 * though it's OK by the C standard. We could test for this, but we'd need
2079 * to supress the Clang warning for the test. */
2080 psa_mac_operation_t func = psa_mac_operation_init( );
2081 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2082 psa_mac_operation_t zero;
2083
2084 memset( &zero, 0, sizeof( zero ) );
2085
Jaeden Amero252ef282019-02-15 14:05:35 +00002086 /* A freshly-initialized MAC operation should not be usable. */
2087 TEST_EQUAL( psa_mac_update( &func,
2088 input, sizeof( input ) ),
2089 PSA_ERROR_BAD_STATE );
2090 TEST_EQUAL( psa_mac_update( &init,
2091 input, sizeof( input ) ),
2092 PSA_ERROR_BAD_STATE );
2093 TEST_EQUAL( psa_mac_update( &zero,
2094 input, sizeof( input ) ),
2095 PSA_ERROR_BAD_STATE );
2096
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002097 /* A default MAC operation should be abortable without error. */
2098 PSA_ASSERT( psa_mac_abort( &func ) );
2099 PSA_ASSERT( psa_mac_abort( &init ) );
2100 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002101}
2102/* END_CASE */
2103
2104/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105void mac_setup( int key_type_arg,
2106 data_t *key,
2107 int alg_arg,
2108 int expected_status_arg )
2109{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002110 psa_key_type_t key_type = key_type_arg;
2111 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002112 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002113 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002114 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2115#if defined(KNOWN_SUPPORTED_MAC_ALG)
2116 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2117#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002118
Gilles Peskine8817f612018-12-18 00:18:46 +01002119 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002120
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002121 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2122 &operation, &status ) )
2123 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002124 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002125
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002126 /* The operation object should be reusable. */
2127#if defined(KNOWN_SUPPORTED_MAC_ALG)
2128 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2129 smoke_test_key_data,
2130 sizeof( smoke_test_key_data ),
2131 KNOWN_SUPPORTED_MAC_ALG,
2132 &operation, &status ) )
2133 goto exit;
2134 TEST_EQUAL( status, PSA_SUCCESS );
2135#endif
2136
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002137exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002138 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002139}
2140/* END_CASE */
2141
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002142/* 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 +00002143void mac_bad_order( )
2144{
Ronald Cron5425a212020-08-04 14:58:35 +02002145 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002146 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2147 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002148 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002149 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2150 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2151 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002153 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2154 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2155 size_t sign_mac_length = 0;
2156 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2157 const uint8_t verify_mac[] = {
2158 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2159 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2160 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2161
2162 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002163 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002164 psa_set_key_algorithm( &attributes, alg );
2165 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002166
Ronald Cron5425a212020-08-04 14:58:35 +02002167 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2168 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002169
Jaeden Amero252ef282019-02-15 14:05:35 +00002170 /* Call update without calling setup beforehand. */
2171 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2172 PSA_ERROR_BAD_STATE );
2173 PSA_ASSERT( psa_mac_abort( &operation ) );
2174
2175 /* Call sign finish without calling setup beforehand. */
2176 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2177 &sign_mac_length),
2178 PSA_ERROR_BAD_STATE );
2179 PSA_ASSERT( psa_mac_abort( &operation ) );
2180
2181 /* Call verify finish without calling setup beforehand. */
2182 TEST_EQUAL( psa_mac_verify_finish( &operation,
2183 verify_mac, sizeof( verify_mac ) ),
2184 PSA_ERROR_BAD_STATE );
2185 PSA_ASSERT( psa_mac_abort( &operation ) );
2186
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002187 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002188 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2189 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002190 PSA_ERROR_BAD_STATE );
2191 PSA_ASSERT( psa_mac_abort( &operation ) );
2192
Jaeden Amero252ef282019-02-15 14:05:35 +00002193 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002194 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002195 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2196 PSA_ASSERT( psa_mac_sign_finish( &operation,
2197 sign_mac, sizeof( sign_mac ),
2198 &sign_mac_length ) );
2199 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2200 PSA_ERROR_BAD_STATE );
2201 PSA_ASSERT( psa_mac_abort( &operation ) );
2202
2203 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002204 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002205 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2206 PSA_ASSERT( psa_mac_verify_finish( &operation,
2207 verify_mac, sizeof( verify_mac ) ) );
2208 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2209 PSA_ERROR_BAD_STATE );
2210 PSA_ASSERT( psa_mac_abort( &operation ) );
2211
2212 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002213 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002214 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2215 PSA_ASSERT( psa_mac_sign_finish( &operation,
2216 sign_mac, sizeof( sign_mac ),
2217 &sign_mac_length ) );
2218 TEST_EQUAL( psa_mac_sign_finish( &operation,
2219 sign_mac, sizeof( sign_mac ),
2220 &sign_mac_length ),
2221 PSA_ERROR_BAD_STATE );
2222 PSA_ASSERT( psa_mac_abort( &operation ) );
2223
2224 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002225 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002226 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2227 PSA_ASSERT( psa_mac_verify_finish( &operation,
2228 verify_mac, sizeof( verify_mac ) ) );
2229 TEST_EQUAL( psa_mac_verify_finish( &operation,
2230 verify_mac, sizeof( verify_mac ) ),
2231 PSA_ERROR_BAD_STATE );
2232 PSA_ASSERT( psa_mac_abort( &operation ) );
2233
2234 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002235 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002236 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2237 TEST_EQUAL( psa_mac_verify_finish( &operation,
2238 verify_mac, sizeof( verify_mac ) ),
2239 PSA_ERROR_BAD_STATE );
2240 PSA_ASSERT( psa_mac_abort( &operation ) );
2241
2242 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002243 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002244 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2245 TEST_EQUAL( psa_mac_sign_finish( &operation,
2246 sign_mac, sizeof( sign_mac ),
2247 &sign_mac_length ),
2248 PSA_ERROR_BAD_STATE );
2249 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002250
Ronald Cron5425a212020-08-04 14:58:35 +02002251 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002252
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002253exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002254 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002255}
2256/* END_CASE */
2257
2258/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002259void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002260 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002261 int alg_arg,
2262 data_t *input,
2263 data_t *expected_mac )
2264{
Ronald Cron5425a212020-08-04 14:58:35 +02002265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002266 psa_key_type_t key_type = key_type_arg;
2267 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002268 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002270 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002271 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002272 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002273 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002274 const size_t output_sizes_to_test[] = {
2275 0,
2276 1,
2277 expected_mac->len - 1,
2278 expected_mac->len,
2279 expected_mac->len + 1,
2280 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002281
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002282 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002283 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002284 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002285
Gilles Peskine8817f612018-12-18 00:18:46 +01002286 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002287
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002288 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_set_key_algorithm( &attributes, alg );
2290 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002291
Ronald Cron5425a212020-08-04 14:58:35 +02002292 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2293 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002294
Gilles Peskine8b356b52020-08-25 23:44:59 +02002295 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2296 {
2297 const size_t output_size = output_sizes_to_test[i];
2298 psa_status_t expected_status =
2299 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2300 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002301
Chris Jones9634bb12021-01-20 15:56:42 +00002302 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002303 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002304
Gilles Peskine8b356b52020-08-25 23:44:59 +02002305 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002306 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002307 PSA_ASSERT( psa_mac_update( &operation,
2308 input->x, input->len ) );
2309 TEST_EQUAL( psa_mac_sign_finish( &operation,
2310 actual_mac, output_size,
2311 &mac_length ),
2312 expected_status );
2313 PSA_ASSERT( psa_mac_abort( &operation ) );
2314
2315 if( expected_status == PSA_SUCCESS )
2316 {
2317 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2318 actual_mac, mac_length );
2319 }
2320 mbedtls_free( actual_mac );
2321 actual_mac = NULL;
2322 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002323
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002324exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002325 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002326 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002327 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002328 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002329}
2330/* END_CASE */
2331
2332/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002333void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002334 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002335 int alg_arg,
2336 data_t *input,
2337 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002338{
Ronald Cron5425a212020-08-04 14:58:35 +02002339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002340 psa_key_type_t key_type = key_type_arg;
2341 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002342 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002344 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002345
Gilles Peskine69c12672018-06-28 00:07:19 +02002346 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2347
Gilles Peskine8817f612018-12-18 00:18:46 +01002348 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002349
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002350 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002351 psa_set_key_algorithm( &attributes, alg );
2352 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002353
Ronald Cron5425a212020-08-04 14:58:35 +02002354 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2355 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002356
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002357 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002358 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002359 PSA_ASSERT( psa_mac_update( &operation,
2360 input->x, input->len ) );
2361 PSA_ASSERT( psa_mac_verify_finish( &operation,
2362 expected_mac->x,
2363 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002364
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002365 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002366 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002367 PSA_ASSERT( psa_mac_update( &operation,
2368 input->x, input->len ) );
2369 TEST_EQUAL( psa_mac_verify_finish( &operation,
2370 expected_mac->x,
2371 expected_mac->len - 1 ),
2372 PSA_ERROR_INVALID_SIGNATURE );
2373
2374 /* Test a MAC that's too long. */
2375 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2376 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002377 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002378 PSA_ASSERT( psa_mac_update( &operation,
2379 input->x, input->len ) );
2380 TEST_EQUAL( psa_mac_verify_finish( &operation,
2381 perturbed_mac,
2382 expected_mac->len + 1 ),
2383 PSA_ERROR_INVALID_SIGNATURE );
2384
2385 /* Test changing one byte. */
2386 for( size_t i = 0; i < expected_mac->len; i++ )
2387 {
Chris Jones9634bb12021-01-20 15:56:42 +00002388 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002389 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002390 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002391 PSA_ASSERT( psa_mac_update( &operation,
2392 input->x, input->len ) );
2393 TEST_EQUAL( psa_mac_verify_finish( &operation,
2394 perturbed_mac,
2395 expected_mac->len ),
2396 PSA_ERROR_INVALID_SIGNATURE );
2397 perturbed_mac[i] ^= 1;
2398 }
2399
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002401 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002402 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002403 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002404 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002405}
2406/* END_CASE */
2407
2408/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002409void cipher_operation_init( )
2410{
Jaeden Ameroab439972019-02-15 14:12:05 +00002411 const uint8_t input[1] = { 0 };
2412 unsigned char output[1] = { 0 };
2413 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002414 /* Test each valid way of initializing the object, except for `= {0}`, as
2415 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2416 * though it's OK by the C standard. We could test for this, but we'd need
2417 * to supress the Clang warning for the test. */
2418 psa_cipher_operation_t func = psa_cipher_operation_init( );
2419 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2420 psa_cipher_operation_t zero;
2421
2422 memset( &zero, 0, sizeof( zero ) );
2423
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 /* A freshly-initialized cipher operation should not be usable. */
2425 TEST_EQUAL( psa_cipher_update( &func,
2426 input, sizeof( input ),
2427 output, sizeof( output ),
2428 &output_length ),
2429 PSA_ERROR_BAD_STATE );
2430 TEST_EQUAL( psa_cipher_update( &init,
2431 input, sizeof( input ),
2432 output, sizeof( output ),
2433 &output_length ),
2434 PSA_ERROR_BAD_STATE );
2435 TEST_EQUAL( psa_cipher_update( &zero,
2436 input, sizeof( input ),
2437 output, sizeof( output ),
2438 &output_length ),
2439 PSA_ERROR_BAD_STATE );
2440
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002441 /* A default cipher operation should be abortable without error. */
2442 PSA_ASSERT( psa_cipher_abort( &func ) );
2443 PSA_ASSERT( psa_cipher_abort( &init ) );
2444 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002445}
2446/* END_CASE */
2447
2448/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449void cipher_setup( int key_type_arg,
2450 data_t *key,
2451 int alg_arg,
2452 int expected_status_arg )
2453{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002454 psa_key_type_t key_type = key_type_arg;
2455 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002456 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002457 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002458 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002459#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002460 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2461#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002462
Gilles Peskine8817f612018-12-18 00:18:46 +01002463 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002464
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002465 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2466 &operation, &status ) )
2467 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002468 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002469
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002470 /* The operation object should be reusable. */
2471#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2472 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2473 smoke_test_key_data,
2474 sizeof( smoke_test_key_data ),
2475 KNOWN_SUPPORTED_CIPHER_ALG,
2476 &operation, &status ) )
2477 goto exit;
2478 TEST_EQUAL( status, PSA_SUCCESS );
2479#endif
2480
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002481exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002482 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002483 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002484}
2485/* END_CASE */
2486
Ronald Cronee414c72021-03-18 18:50:08 +01002487/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002488void cipher_bad_order( )
2489{
Ronald Cron5425a212020-08-04 14:58:35 +02002490 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002491 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2492 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002494 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002495 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002496 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002497 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2498 0xaa, 0xaa, 0xaa, 0xaa };
2499 const uint8_t text[] = {
2500 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2501 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002502 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002503 size_t length = 0;
2504
2505 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002506 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2507 psa_set_key_algorithm( &attributes, alg );
2508 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002509 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2510 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002511
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002512 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002513 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2514 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002515 PSA_ERROR_BAD_STATE );
2516 PSA_ASSERT( psa_cipher_abort( &operation ) );
2517
2518 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002519 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2520 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002521 PSA_ERROR_BAD_STATE );
2522 PSA_ASSERT( psa_cipher_abort( &operation ) );
2523
Jaeden Ameroab439972019-02-15 14:12:05 +00002524 /* Generate an IV without calling setup beforehand. */
2525 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2526 buffer, sizeof( buffer ),
2527 &length ),
2528 PSA_ERROR_BAD_STATE );
2529 PSA_ASSERT( psa_cipher_abort( &operation ) );
2530
2531 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002532 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002533 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2534 buffer, sizeof( buffer ),
2535 &length ) );
2536 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2537 buffer, sizeof( buffer ),
2538 &length ),
2539 PSA_ERROR_BAD_STATE );
2540 PSA_ASSERT( psa_cipher_abort( &operation ) );
2541
2542 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002543 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002544 PSA_ASSERT( psa_cipher_set_iv( &operation,
2545 iv, sizeof( iv ) ) );
2546 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2547 buffer, sizeof( buffer ),
2548 &length ),
2549 PSA_ERROR_BAD_STATE );
2550 PSA_ASSERT( psa_cipher_abort( &operation ) );
2551
2552 /* Set an IV without calling setup beforehand. */
2553 TEST_EQUAL( psa_cipher_set_iv( &operation,
2554 iv, sizeof( iv ) ),
2555 PSA_ERROR_BAD_STATE );
2556 PSA_ASSERT( psa_cipher_abort( &operation ) );
2557
2558 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002559 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002560 PSA_ASSERT( psa_cipher_set_iv( &operation,
2561 iv, sizeof( iv ) ) );
2562 TEST_EQUAL( psa_cipher_set_iv( &operation,
2563 iv, sizeof( iv ) ),
2564 PSA_ERROR_BAD_STATE );
2565 PSA_ASSERT( psa_cipher_abort( &operation ) );
2566
2567 /* Set an IV after it's already generated. */
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_generate_iv( &operation,
2570 buffer, sizeof( buffer ),
2571 &length ) );
2572 TEST_EQUAL( psa_cipher_set_iv( &operation,
2573 iv, sizeof( iv ) ),
2574 PSA_ERROR_BAD_STATE );
2575 PSA_ASSERT( psa_cipher_abort( &operation ) );
2576
2577 /* Call update without calling setup beforehand. */
2578 TEST_EQUAL( psa_cipher_update( &operation,
2579 text, sizeof( text ),
2580 buffer, sizeof( buffer ),
2581 &length ),
2582 PSA_ERROR_BAD_STATE );
2583 PSA_ASSERT( psa_cipher_abort( &operation ) );
2584
2585 /* Call update without an IV where an IV is required. */
2586 TEST_EQUAL( psa_cipher_update( &operation,
2587 text, sizeof( text ),
2588 buffer, sizeof( buffer ),
2589 &length ),
2590 PSA_ERROR_BAD_STATE );
2591 PSA_ASSERT( psa_cipher_abort( &operation ) );
2592
2593 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002594 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002595 PSA_ASSERT( psa_cipher_set_iv( &operation,
2596 iv, sizeof( iv ) ) );
2597 PSA_ASSERT( psa_cipher_finish( &operation,
2598 buffer, sizeof( buffer ), &length ) );
2599 TEST_EQUAL( psa_cipher_update( &operation,
2600 text, sizeof( text ),
2601 buffer, sizeof( buffer ),
2602 &length ),
2603 PSA_ERROR_BAD_STATE );
2604 PSA_ASSERT( psa_cipher_abort( &operation ) );
2605
2606 /* Call finish without calling setup beforehand. */
2607 TEST_EQUAL( psa_cipher_finish( &operation,
2608 buffer, sizeof( buffer ), &length ),
2609 PSA_ERROR_BAD_STATE );
2610 PSA_ASSERT( psa_cipher_abort( &operation ) );
2611
2612 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002613 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002614 /* Not calling update means we are encrypting an empty buffer, which is OK
2615 * for cipher modes with padding. */
2616 TEST_EQUAL( psa_cipher_finish( &operation,
2617 buffer, sizeof( buffer ), &length ),
2618 PSA_ERROR_BAD_STATE );
2619 PSA_ASSERT( psa_cipher_abort( &operation ) );
2620
2621 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002622 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002623 PSA_ASSERT( psa_cipher_set_iv( &operation,
2624 iv, sizeof( iv ) ) );
2625 PSA_ASSERT( psa_cipher_finish( &operation,
2626 buffer, sizeof( buffer ), &length ) );
2627 TEST_EQUAL( psa_cipher_finish( &operation,
2628 buffer, sizeof( buffer ), &length ),
2629 PSA_ERROR_BAD_STATE );
2630 PSA_ASSERT( psa_cipher_abort( &operation ) );
2631
Ronald Cron5425a212020-08-04 14:58:35 +02002632 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002633
Jaeden Ameroab439972019-02-15 14:12:05 +00002634exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002635 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002636 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002637}
2638/* END_CASE */
2639
2640/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002641void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002642 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002643 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002644 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002645{
Ronald Cron5425a212020-08-04 14:58:35 +02002646 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647 psa_status_t status;
2648 psa_key_type_t key_type = key_type_arg;
2649 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002650 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002651 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652 size_t output_buffer_size = 0;
2653 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002654 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002655 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657
Gilles Peskine8817f612018-12-18 00:18:46 +01002658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002660 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2661 psa_set_key_algorithm( &attributes, alg );
2662 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002663
Ronald Cron5425a212020-08-04 14:58:35 +02002664 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2665 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666
Ronald Cron5425a212020-08-04 14:58:35 +02002667 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002668
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002669 if( iv->len > 0 )
2670 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002671 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002672 }
2673
gabor-mezei-armceface22021-01-21 12:26:17 +01002674 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2675 TEST_ASSERT( output_buffer_size <=
2676 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002677 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678
Gilles Peskine8817f612018-12-18 00:18:46 +01002679 PSA_ASSERT( psa_cipher_update( &operation,
2680 input->x, input->len,
2681 output, output_buffer_size,
2682 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002683 TEST_ASSERT( function_output_length <=
2684 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2685 TEST_ASSERT( function_output_length <=
2686 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002687 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002688
Gilles Peskine50e586b2018-06-08 14:28:46 +02002689 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002690 ( output_buffer_size == 0 ? NULL :
2691 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002692 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002693 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002694 TEST_ASSERT( function_output_length <=
2695 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2696 TEST_ASSERT( function_output_length <=
2697 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002698 total_output_length += function_output_length;
2699
Gilles Peskinefe11b722018-12-18 00:24:04 +01002700 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002701 if( expected_status == PSA_SUCCESS )
2702 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002704 ASSERT_COMPARE( expected_output->x, expected_output->len,
2705 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002707
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002709 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002710 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002711 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002712 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713}
2714/* END_CASE */
2715
2716/* BEGIN_CASE */
2717void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002718 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002719 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002720 int first_part_size_arg,
2721 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002722 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002723{
Ronald Cron5425a212020-08-04 14:58:35 +02002724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002725 psa_key_type_t key_type = key_type_arg;
2726 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002727 size_t first_part_size = first_part_size_arg;
2728 size_t output1_length = output1_length_arg;
2729 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002730 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002731 size_t output_buffer_size = 0;
2732 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002733 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002734 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002735 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736
Gilles Peskine8817f612018-12-18 00:18:46 +01002737 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002739 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2740 psa_set_key_algorithm( &attributes, alg );
2741 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002742
Ronald Cron5425a212020-08-04 14:58:35 +02002743 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2744 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002745
Ronald Cron5425a212020-08-04 14:58:35 +02002746 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002747
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002748 if( iv->len > 0 )
2749 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002750 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002751 }
2752
gabor-mezei-armceface22021-01-21 12:26:17 +01002753 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2754 TEST_ASSERT( output_buffer_size <=
2755 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002756 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002757
Gilles Peskinee0866522019-02-19 19:44:00 +01002758 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002759 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2760 output, output_buffer_size,
2761 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002762 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002763 TEST_ASSERT( function_output_length <=
2764 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2765 TEST_ASSERT( function_output_length <=
2766 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002767 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002768
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 PSA_ASSERT( psa_cipher_update( &operation,
2770 input->x + first_part_size,
2771 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002772 ( output_buffer_size == 0 ? NULL :
2773 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002774 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002775 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002776 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002777 TEST_ASSERT( function_output_length <=
2778 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2779 alg,
2780 input->len - first_part_size ) );
2781 TEST_ASSERT( function_output_length <=
2782 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002783 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002784
Gilles Peskine8817f612018-12-18 00:18:46 +01002785 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002786 ( output_buffer_size == 0 ? NULL :
2787 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002788 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002790 TEST_ASSERT( function_output_length <=
2791 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2792 TEST_ASSERT( function_output_length <=
2793 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002794 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002796
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002797 ASSERT_COMPARE( expected_output->x, expected_output->len,
2798 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002799
2800exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002801 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002802 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002803 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002804 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805}
2806/* END_CASE */
2807
2808/* BEGIN_CASE */
2809void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002810 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002811 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002812 int first_part_size_arg,
2813 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815{
Ronald Cron5425a212020-08-04 14:58:35 +02002816 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002817 psa_key_type_t key_type = key_type_arg;
2818 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002819 size_t first_part_size = first_part_size_arg;
2820 size_t output1_length = output1_length_arg;
2821 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002822 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823 size_t output_buffer_size = 0;
2824 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002825 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002826 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828
Gilles Peskine8817f612018-12-18 00:18:46 +01002829 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2832 psa_set_key_algorithm( &attributes, alg );
2833 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002834
Ronald Cron5425a212020-08-04 14:58:35 +02002835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2836 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837
Ronald Cron5425a212020-08-04 14:58:35 +02002838 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839
Steven Cooreman177deba2020-09-07 17:14:14 +02002840 if( iv->len > 0 )
2841 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002842 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002843 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
gabor-mezei-armceface22021-01-21 12:26:17 +01002845 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2846 TEST_ASSERT( output_buffer_size <=
2847 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002848 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002849
Gilles Peskinee0866522019-02-19 19:44:00 +01002850 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002851 PSA_ASSERT( psa_cipher_update( &operation,
2852 input->x, first_part_size,
2853 output, output_buffer_size,
2854 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002855 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002856 TEST_ASSERT( function_output_length <=
2857 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2858 TEST_ASSERT( function_output_length <=
2859 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002860 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002861
Gilles Peskine8817f612018-12-18 00:18:46 +01002862 PSA_ASSERT( psa_cipher_update( &operation,
2863 input->x + first_part_size,
2864 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002865 ( output_buffer_size == 0 ? NULL :
2866 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002867 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002868 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002869 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002870 TEST_ASSERT( function_output_length <=
2871 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2872 alg,
2873 input->len - first_part_size ) );
2874 TEST_ASSERT( function_output_length <=
2875 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002876 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002877
Gilles Peskine8817f612018-12-18 00:18:46 +01002878 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002879 ( output_buffer_size == 0 ? NULL :
2880 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002881 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002882 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002883 TEST_ASSERT( function_output_length <=
2884 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2885 TEST_ASSERT( function_output_length <=
2886 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002887 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002888 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002890 ASSERT_COMPARE( expected_output->x, expected_output->len,
2891 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892
2893exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002894 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002895 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002896 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002897 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898}
2899/* END_CASE */
2900
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901/* BEGIN_CASE */
2902void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002903 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002904 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002905 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002906{
Ronald Cron5425a212020-08-04 14:58:35 +02002907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908 psa_status_t status;
2909 psa_key_type_t key_type = key_type_arg;
2910 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002911 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002912 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913 size_t output_buffer_size = 0;
2914 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002915 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002916 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002918
Gilles Peskine8817f612018-12-18 00:18:46 +01002919 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002921 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2922 psa_set_key_algorithm( &attributes, alg );
2923 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002924
Ronald Cron5425a212020-08-04 14:58:35 +02002925 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2926 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927
Ronald Cron5425a212020-08-04 14:58:35 +02002928 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929
Steven Cooreman177deba2020-09-07 17:14:14 +02002930 if( iv->len > 0 )
2931 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002932 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002933 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934
gabor-mezei-armceface22021-01-21 12:26:17 +01002935 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2936 TEST_ASSERT( output_buffer_size <=
2937 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002938 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002939
Gilles Peskine8817f612018-12-18 00:18:46 +01002940 PSA_ASSERT( psa_cipher_update( &operation,
2941 input->x, input->len,
2942 output, output_buffer_size,
2943 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002944 TEST_ASSERT( function_output_length <=
2945 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2946 TEST_ASSERT( function_output_length <=
2947 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002948 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002949
Gilles Peskine50e586b2018-06-08 14:28:46 +02002950 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002951 ( output_buffer_size == 0 ? NULL :
2952 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002953 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002954 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002955 TEST_ASSERT( function_output_length <=
2956 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2957 TEST_ASSERT( function_output_length <=
2958 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002959 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002960 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961
2962 if( expected_status == PSA_SUCCESS )
2963 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002964 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002965 ASSERT_COMPARE( expected_output->x, expected_output->len,
2966 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967 }
2968
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002970 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002971 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002972 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002973 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974}
2975/* END_CASE */
2976
Gilles Peskine50e586b2018-06-08 14:28:46 +02002977/* BEGIN_CASE */
2978void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002979 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002980 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981{
Ronald Cron5425a212020-08-04 14:58:35 +02002982 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002983 psa_key_type_t key_type = key_type_arg;
2984 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002985 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002986 size_t iv_size = 16;
2987 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002988 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002989 size_t output1_size = 0;
2990 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002991 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002992 size_t output2_size = 0;
2993 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002994 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002995 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2996 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002998
Gilles Peskine8817f612018-12-18 00:18:46 +01002999 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003000
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003001 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3002 psa_set_key_algorithm( &attributes, alg );
3003 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003004
Ronald Cron5425a212020-08-04 14:58:35 +02003005 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3006 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003007
Ronald Cron5425a212020-08-04 14:58:35 +02003008 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3009 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003010
Steven Cooreman177deba2020-09-07 17:14:14 +02003011 if( alg != PSA_ALG_ECB_NO_PADDING )
3012 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003013 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3014 iv, iv_size,
3015 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003016 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003017 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3018 TEST_ASSERT( output1_size <=
3019 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003020 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003021
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3023 output1, output1_size,
3024 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003025 TEST_ASSERT( output1_length <=
3026 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3027 TEST_ASSERT( output1_length <=
3028 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3029
Gilles Peskine8817f612018-12-18 00:18:46 +01003030 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003031 output1 + output1_length,
3032 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003033 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003034 TEST_ASSERT( function_output_length <=
3035 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3036 TEST_ASSERT( function_output_length <=
3037 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003038
Gilles Peskine048b7f02018-06-08 14:20:49 +02003039 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003040
Gilles Peskine8817f612018-12-18 00:18:46 +01003041 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003042
3043 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003044 TEST_ASSERT( output2_size <=
3045 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3046 TEST_ASSERT( output2_size <=
3047 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003048 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003049
Steven Cooreman177deba2020-09-07 17:14:14 +02003050 if( iv_length > 0 )
3051 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003052 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3053 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003054 }
3055
Gilles Peskine8817f612018-12-18 00:18:46 +01003056 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3057 output2, output2_size,
3058 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003059 TEST_ASSERT( output2_length <=
3060 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3061 TEST_ASSERT( output2_length <=
3062 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3063
Gilles Peskine048b7f02018-06-08 14:20:49 +02003064 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003065 PSA_ASSERT( psa_cipher_finish( &operation2,
3066 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003067 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003068 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003069 TEST_ASSERT( function_output_length <=
3070 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3071 TEST_ASSERT( function_output_length <=
3072 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003073
Gilles Peskine048b7f02018-06-08 14:20:49 +02003074 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003075
Gilles Peskine8817f612018-12-18 00:18:46 +01003076 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003077
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003078 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003079
3080exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003081 psa_cipher_abort( &operation1 );
3082 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003083 mbedtls_free( output1 );
3084 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003085 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003086 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003087}
3088/* END_CASE */
3089
3090/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003091void cipher_verify_output_multipart( int alg_arg,
3092 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003093 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003094 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003095 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003096{
Ronald Cron5425a212020-08-04 14:58:35 +02003097 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003098 psa_key_type_t key_type = key_type_arg;
3099 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003100 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003101 unsigned char iv[16] = {0};
3102 size_t iv_size = 16;
3103 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003104 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003105 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003106 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003107 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003108 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003109 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003110 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003111 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3112 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003114
Gilles Peskine8817f612018-12-18 00:18:46 +01003115 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003116
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003117 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3118 psa_set_key_algorithm( &attributes, alg );
3119 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003120
Ronald Cron5425a212020-08-04 14:58:35 +02003121 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3122 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003123
Ronald Cron5425a212020-08-04 14:58:35 +02003124 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3125 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003126
Steven Cooreman177deba2020-09-07 17:14:14 +02003127 if( alg != PSA_ALG_ECB_NO_PADDING )
3128 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003129 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3130 iv, iv_size,
3131 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003132 }
3133
gabor-mezei-armceface22021-01-21 12:26:17 +01003134 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3135 TEST_ASSERT( output1_buffer_size <=
3136 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003137 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003138
Gilles Peskinee0866522019-02-19 19:44:00 +01003139 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003140
Gilles Peskine8817f612018-12-18 00:18:46 +01003141 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3142 output1, output1_buffer_size,
3143 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003144 TEST_ASSERT( function_output_length <=
3145 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3146 TEST_ASSERT( function_output_length <=
3147 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003148 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003149
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 PSA_ASSERT( psa_cipher_update( &operation1,
3151 input->x + first_part_size,
3152 input->len - first_part_size,
3153 output1, output1_buffer_size,
3154 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003155 TEST_ASSERT( function_output_length <=
3156 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3157 alg,
3158 input->len - first_part_size ) );
3159 TEST_ASSERT( function_output_length <=
3160 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003161 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003162
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 PSA_ASSERT( psa_cipher_finish( &operation1,
3164 output1 + output1_length,
3165 output1_buffer_size - output1_length,
3166 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003167 TEST_ASSERT( function_output_length <=
3168 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3169 TEST_ASSERT( function_output_length <=
3170 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003171 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003172
Gilles Peskine8817f612018-12-18 00:18:46 +01003173 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003174
Gilles Peskine048b7f02018-06-08 14:20:49 +02003175 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003176 TEST_ASSERT( output2_buffer_size <=
3177 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3178 TEST_ASSERT( output2_buffer_size <=
3179 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003180 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003181
Steven Cooreman177deba2020-09-07 17:14:14 +02003182 if( iv_length > 0 )
3183 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003184 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3185 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003186 }
Moran Pekerded84402018-06-06 16:36:50 +03003187
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3189 output2, output2_buffer_size,
3190 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003191 TEST_ASSERT( function_output_length <=
3192 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3193 TEST_ASSERT( function_output_length <=
3194 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003195 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003196
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_cipher_update( &operation2,
3198 output1 + first_part_size,
3199 output1_length - first_part_size,
3200 output2, output2_buffer_size,
3201 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003202 TEST_ASSERT( function_output_length <=
3203 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3204 alg,
3205 output1_length - first_part_size ) );
3206 TEST_ASSERT( function_output_length <=
3207 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003208 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003209
Gilles Peskine8817f612018-12-18 00:18:46 +01003210 PSA_ASSERT( psa_cipher_finish( &operation2,
3211 output2 + output2_length,
3212 output2_buffer_size - output2_length,
3213 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003214 TEST_ASSERT( function_output_length <=
3215 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3216 TEST_ASSERT( function_output_length <=
3217 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003218 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003219
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003221
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003222 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003223
3224exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003225 psa_cipher_abort( &operation1 );
3226 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003227 mbedtls_free( output1 );
3228 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003229 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003230 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003231}
3232/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003233
Gilles Peskine20035e32018-02-03 22:44:14 +01003234/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003235void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003236 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003237 data_t *nonce,
3238 data_t *additional_data,
3239 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003240 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003241{
Ronald Cron5425a212020-08-04 14:58:35 +02003242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003243 psa_key_type_t key_type = key_type_arg;
3244 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003245 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003246 unsigned char *output_data = NULL;
3247 size_t output_size = 0;
3248 size_t output_length = 0;
3249 unsigned char *output_data2 = NULL;
3250 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003251 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003252 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254
Gilles Peskine8817f612018-12-18 00:18:46 +01003255 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003256
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3258 psa_set_key_algorithm( &attributes, alg );
3259 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003260
Gilles Peskine049c7532019-05-15 20:22:09 +02003261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003262 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003263 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3264 key_bits = psa_get_key_bits( &attributes );
3265
3266 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3267 alg );
3268 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3269 * should be exact. */
3270 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3271 expected_result != PSA_ERROR_NOT_SUPPORTED )
3272 {
3273 TEST_EQUAL( output_size,
3274 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3275 TEST_ASSERT( output_size <=
3276 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3277 }
3278 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003279
Steven Cooremanf49478b2021-02-15 15:19:25 +01003280 status = psa_aead_encrypt( key, alg,
3281 nonce->x, nonce->len,
3282 additional_data->x,
3283 additional_data->len,
3284 input_data->x, input_data->len,
3285 output_data, output_size,
3286 &output_length );
3287
3288 /* If the operation is not supported, just skip and not fail in case the
3289 * encryption involves a common limitation of cryptography hardwares and
3290 * an alternative implementation. */
3291 if( status == PSA_ERROR_NOT_SUPPORTED )
3292 {
3293 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3294 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3295 }
3296
3297 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003298
3299 if( PSA_SUCCESS == expected_result )
3300 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003301 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003302
Gilles Peskine003a4a92019-05-14 16:09:40 +02003303 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3304 * should be exact. */
3305 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003306 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003307
gabor-mezei-armceface22021-01-21 12:26:17 +01003308 TEST_ASSERT( input_data->len <=
3309 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3310
Ronald Cron5425a212020-08-04 14:58:35 +02003311 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003312 nonce->x, nonce->len,
3313 additional_data->x,
3314 additional_data->len,
3315 output_data, output_length,
3316 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003317 &output_length2 ),
3318 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003319
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003320 ASSERT_COMPARE( input_data->x, input_data->len,
3321 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003322 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003323
Gilles Peskinea1cac842018-06-11 19:33:02 +02003324exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003325 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003326 mbedtls_free( output_data );
3327 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003328 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329}
3330/* END_CASE */
3331
3332/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003333void aead_encrypt( int key_type_arg, data_t *key_data,
3334 int alg_arg,
3335 data_t *nonce,
3336 data_t *additional_data,
3337 data_t *input_data,
3338 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339{
Ronald Cron5425a212020-08-04 14:58:35 +02003340 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341 psa_key_type_t key_type = key_type_arg;
3342 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003343 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344 unsigned char *output_data = NULL;
3345 size_t output_size = 0;
3346 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003348 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003349
Gilles Peskine8817f612018-12-18 00:18:46 +01003350 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3353 psa_set_key_algorithm( &attributes, alg );
3354 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003355
Gilles Peskine049c7532019-05-15 20:22:09 +02003356 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003357 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003358 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3359 key_bits = psa_get_key_bits( &attributes );
3360
3361 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3362 alg );
3363 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3364 * should be exact. */
3365 TEST_EQUAL( output_size,
3366 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3367 TEST_ASSERT( output_size <=
3368 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3369 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003370
Steven Cooremand588ea12021-01-11 19:36:04 +01003371 status = psa_aead_encrypt( key, alg,
3372 nonce->x, nonce->len,
3373 additional_data->x, additional_data->len,
3374 input_data->x, input_data->len,
3375 output_data, output_size,
3376 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377
Ronald Cron28a45ed2021-02-09 20:35:42 +01003378 /* If the operation is not supported, just skip and not fail in case the
3379 * encryption involves a common limitation of cryptography hardwares and
3380 * an alternative implementation. */
3381 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003382 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003383 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3384 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003385 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003386
3387 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003388 ASSERT_COMPARE( expected_result->x, expected_result->len,
3389 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003390
Gilles Peskinea1cac842018-06-11 19:33:02 +02003391exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003392 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003393 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003394 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003395}
3396/* END_CASE */
3397
3398/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003399void aead_decrypt( int key_type_arg, data_t *key_data,
3400 int alg_arg,
3401 data_t *nonce,
3402 data_t *additional_data,
3403 data_t *input_data,
3404 data_t *expected_data,
3405 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406{
Ronald Cron5425a212020-08-04 14:58:35 +02003407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003408 psa_key_type_t key_type = key_type_arg;
3409 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003410 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003411 unsigned char *output_data = NULL;
3412 size_t output_size = 0;
3413 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003415 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003416 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417
Gilles Peskine8817f612018-12-18 00:18:46 +01003418 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003419
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003420 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3421 psa_set_key_algorithm( &attributes, alg );
3422 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003423
Gilles Peskine049c7532019-05-15 20:22:09 +02003424 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003425 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003426 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3427 key_bits = psa_get_key_bits( &attributes );
3428
3429 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3430 alg );
3431 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3432 expected_result != PSA_ERROR_NOT_SUPPORTED )
3433 {
3434 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3435 * should be exact. */
3436 TEST_EQUAL( output_size,
3437 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3438 TEST_ASSERT( output_size <=
3439 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3440 }
3441 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442
Steven Cooremand588ea12021-01-11 19:36:04 +01003443 status = psa_aead_decrypt( key, alg,
3444 nonce->x, nonce->len,
3445 additional_data->x,
3446 additional_data->len,
3447 input_data->x, input_data->len,
3448 output_data, output_size,
3449 &output_length );
3450
Ronald Cron28a45ed2021-02-09 20:35:42 +01003451 /* If the operation is not supported, just skip and not fail in case the
3452 * decryption involves a common limitation of cryptography hardwares and
3453 * an alternative implementation. */
3454 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003455 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003456 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3457 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003458 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003459
3460 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461
Gilles Peskine2d277862018-06-18 15:41:12 +02003462 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003463 ASSERT_COMPARE( expected_data->x, expected_data->len,
3464 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465
Gilles Peskinea1cac842018-06-11 19:33:02 +02003466exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003467 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003468 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003469 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470}
3471/* END_CASE */
3472
3473/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003474void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3475 int alg_arg,
3476 data_t *nonce,
3477 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003478 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003479 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003480 int do_test_data_chunked,
3481 int do_set_lengths,
3482 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003483{
Paul Elliottd3f82412021-06-16 16:52:21 +01003484 size_t ad_part_len = 0;
3485 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003486 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003487
Paul Elliotte64deda2021-09-09 14:07:23 +01003488 /* Ensure that either one part of the test or the other is done, i.e this
3489 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003490 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3491
3492 /* Temporary whilst we have algorithms that cannot support chunking */
3493 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003494 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003495 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3496 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003497 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003498 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003499
Paul Elliott33746aa2021-09-15 16:40:40 +01003500 if( do_set_lengths )
3501 {
3502 if( ad_part_len & 0x01 )
3503 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3504 else
3505 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3506 }
3507
Paul Elliott329d5382021-07-22 17:10:45 +01003508 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003509 if( !aead_multipart_internal_func( key_type_arg, key_data,
3510 alg_arg, nonce,
3511 additional_data,
3512 ad_part_len,
3513 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003514 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003515 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003516 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003517 break;
3518
3519 /* length(0) part, length(ad_part_len) part, length(0) part... */
3520 mbedtls_test_set_step( 1000 + ad_part_len );
3521
3522 if( !aead_multipart_internal_func( key_type_arg, key_data,
3523 alg_arg, nonce,
3524 additional_data,
3525 ad_part_len,
3526 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003527 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003528 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003529 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003530 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003531 }
3532 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003533
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003534 /* Temporary whilst we have algorithms that cannot support chunking */
3535 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003536 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003537 for( data_part_len = 1; data_part_len <= input_data->len;
3538 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003539 {
Paul Elliott329d5382021-07-22 17:10:45 +01003540 /* Split data into length(data_part_len) parts. */
3541 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003542
Paul Elliott33746aa2021-09-15 16:40:40 +01003543 if( do_set_lengths )
3544 {
3545 if( data_part_len & 0x01 )
3546 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3547 else
3548 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3549 }
3550
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003551 if( !aead_multipart_internal_func( key_type_arg, key_data,
3552 alg_arg, nonce,
3553 additional_data, -1,
3554 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003555 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003556 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003557 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003558 break;
3559
3560 /* length(0) part, length(data_part_len) part, length(0) part... */
3561 mbedtls_test_set_step( 3000 + data_part_len );
3562
3563 if( !aead_multipart_internal_func( key_type_arg, key_data,
3564 alg_arg, nonce,
3565 additional_data, -1,
3566 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003567 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003568 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003569 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003570 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003571 }
3572 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003573
Paul Elliott0023e0a2021-04-27 10:06:22 +01003574
Paul Elliott8fc45162021-06-23 16:06:01 +01003575 /* Goto is required to silence warnings about unused labels, as we
3576 * don't actually do any test assertions in this function. */
3577 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003578}
3579/* END_CASE */
3580
3581/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3583 int alg_arg,
3584 data_t *nonce,
3585 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003586 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003587 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003588 int do_test_data_chunked,
3589 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003590 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003591{
Paul Elliottd3f82412021-06-16 16:52:21 +01003592 size_t ad_part_len = 0;
3593 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003594 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003595
Paul Elliotte64deda2021-09-09 14:07:23 +01003596 /* Ensure that either one part of the test or the other is done, i.e this
3597 * test does something. */
3598 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3599
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003600 /* Temporary whilst we have algorithms that cannot support chunking */
3601 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003602 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003603 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3604 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003605 {
Paul Elliott329d5382021-07-22 17:10:45 +01003606 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003607 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003608
Paul Elliott33746aa2021-09-15 16:40:40 +01003609 if( do_set_lengths )
3610 {
3611 if( ad_part_len & 0x01 )
3612 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3613 else
3614 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3615 }
3616
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003617 if( !aead_multipart_internal_func( key_type_arg, key_data,
3618 alg_arg, nonce,
3619 additional_data,
3620 ad_part_len,
3621 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003622 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003623 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003624 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003625 break;
3626
3627 /* length(0) part, length(ad_part_len) part, length(0) part... */
3628 mbedtls_test_set_step( 1000 + ad_part_len );
3629
3630 if( !aead_multipart_internal_func( key_type_arg, key_data,
3631 alg_arg, nonce,
3632 additional_data,
3633 ad_part_len,
3634 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003635 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003636 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003637 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003638 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003639 }
3640 }
3641
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003642 /* Temporary whilst we have algorithms that cannot support chunking */
3643 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003644 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003645 for( data_part_len = 1; data_part_len <= input_data->len;
3646 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003647 {
Paul Elliott329d5382021-07-22 17:10:45 +01003648 /* Split data into length(data_part_len) parts. */
3649 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003650
Paul Elliott33746aa2021-09-15 16:40:40 +01003651 if( do_set_lengths )
3652 {
3653 if( data_part_len & 0x01 )
3654 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3655 else
3656 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3657 }
3658
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003659 if( !aead_multipart_internal_func( key_type_arg, key_data,
3660 alg_arg, nonce,
3661 additional_data, -1,
3662 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003663 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003664 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003665 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003666 break;
3667
3668 /* length(0) part, length(data_part_len) part, length(0) part... */
3669 mbedtls_test_set_step( 3000 + data_part_len );
3670
3671 if( !aead_multipart_internal_func( key_type_arg, key_data,
3672 alg_arg, nonce,
3673 additional_data, -1,
3674 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003675 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003676 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003677 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003678 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003679 }
3680 }
3681
Paul Elliott8fc45162021-06-23 16:06:01 +01003682 /* Goto is required to silence warnings about unused labels, as we
3683 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003684 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003685}
3686/* END_CASE */
3687
3688/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003689void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3690 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003691 int nonce_length,
3692 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003693 data_t *additional_data,
3694 data_t *input_data,
3695 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003696{
3697
3698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3699 psa_key_type_t key_type = key_type_arg;
3700 psa_algorithm_t alg = alg_arg;
3701 psa_aead_operation_t operation;
3702 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3704 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003705 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003706 size_t actual_nonce_length = 0;
3707 size_t expected_nonce_length = expected_nonce_length_arg;
3708 unsigned char *output = NULL;
3709 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003710 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003711 size_t ciphertext_size = 0;
3712 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003713 size_t tag_length = 0;
3714 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003715
3716 PSA_ASSERT( psa_crypto_init( ) );
3717
3718 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3719 psa_set_key_algorithm( & attributes, alg );
3720 psa_set_key_type( & attributes, key_type );
3721
3722 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3723 &key ) );
3724
3725 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3726
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003727 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3728
Paul Elliottf1277632021-08-24 18:11:37 +01003729 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003730
Paul Elliottf1277632021-08-24 18:11:37 +01003731 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003732
Paul Elliottf1277632021-08-24 18:11:37 +01003733 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003734
Paul Elliottf1277632021-08-24 18:11:37 +01003735 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003736
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003737 operation = psa_aead_operation_init( );
3738
3739 status = psa_aead_encrypt_setup( &operation, key, alg );
3740
3741 /* If the operation is not supported, just skip and not fail in case the
3742 * encryption involves a common limitation of cryptography hardwares and
3743 * an alternative implementation. */
3744 if( status == PSA_ERROR_NOT_SUPPORTED )
3745 {
3746 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003747 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003748 }
3749
3750 PSA_ASSERT( status );
3751
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003752 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003753 nonce_length,
3754 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003755
Paul Elliott693bf312021-07-23 17:40:41 +01003756 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003757
Paul Elliottf1277632021-08-24 18:11:37 +01003758 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003759
Paul Elliottf1277632021-08-24 18:11:37 +01003760 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003761
Paul Elliott693bf312021-07-23 17:40:41 +01003762 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003763 {
3764
3765 /* Ensure we can still complete operation. */
3766
3767 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3768 additional_data->len ) );
3769
3770 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003771 output, output_size,
3772 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003773
Paul Elliottf1277632021-08-24 18:11:37 +01003774 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3775 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003776 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3777 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003778
3779exit:
3780 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003781 mbedtls_free( output );
3782 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003783 psa_aead_abort( &operation );
3784 PSA_DONE( );
3785}
3786/* END_CASE */
3787
3788/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003789void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3790 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003791 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003792 data_t *additional_data,
3793 data_t *input_data,
3794 int expected_status_arg )
3795{
3796
3797 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3798 psa_key_type_t key_type = key_type_arg;
3799 psa_algorithm_t alg = alg_arg;
3800 psa_aead_operation_t operation;
3801 uint8_t *nonce_buffer = NULL;
3802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3803 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3804 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003805 unsigned char *output = NULL;
3806 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003807 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003808 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003809 size_t ciphertext_size = 0;
3810 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003811 size_t tag_length = 0;
3812 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003813 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003814
3815 PSA_ASSERT( psa_crypto_init( ) );
3816
3817 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3818 psa_set_key_algorithm( &attributes, alg );
3819 psa_set_key_type( &attributes, key_type );
3820
3821 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3822 &key ) );
3823
3824 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3825
3826 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3827
Paul Elliott6f0e7202021-08-25 12:57:18 +01003828 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003829
Paul Elliott6f0e7202021-08-25 12:57:18 +01003830 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003831
Paul Elliott6f0e7202021-08-25 12:57:18 +01003832 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003833
Paul Elliott6f0e7202021-08-25 12:57:18 +01003834 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003835
3836 operation = psa_aead_operation_init( );
3837
3838 status = psa_aead_encrypt_setup( &operation, key, alg );
3839
3840 /* If the operation is not supported, just skip and not fail in case the
3841 * encryption involves a common limitation of cryptography hardwares and
3842 * an alternative implementation. */
3843 if( status == PSA_ERROR_NOT_SUPPORTED )
3844 {
3845 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003846 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003847 }
3848
3849 PSA_ASSERT( status );
3850
Paul Elliott4023ffd2021-09-10 16:21:22 +01003851 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3852 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003853 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003854 /* Arbitrary size buffer, to test zero length valid buffer. */
3855 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003856 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003857 }
3858 else
3859 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003860 /* If length is zero, then this will return NULL. */
3861 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003862 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003863
Paul Elliott4023ffd2021-09-10 16:21:22 +01003864 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003865 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003866 for( index = 0; index < nonce_length - 1; ++index )
3867 {
3868 nonce_buffer[index] = 'a' + index;
3869 }
Paul Elliott66696b52021-08-16 18:42:41 +01003870 }
Paul Elliott863864a2021-07-23 17:28:31 +01003871 }
3872
Paul Elliott6f0e7202021-08-25 12:57:18 +01003873 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003874
Paul Elliott693bf312021-07-23 17:40:41 +01003875 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003876
3877 if( expected_status == PSA_SUCCESS )
3878 {
3879 /* Ensure we can still complete operation. */
3880
3881 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3882 additional_data->len ) );
3883
3884 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003885 output, output_size,
3886 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003887
Paul Elliott6f0e7202021-08-25 12:57:18 +01003888 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3889 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003890 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3891 }
3892
3893exit:
3894 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003895 mbedtls_free( output );
3896 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003897 mbedtls_free( nonce_buffer );
3898 psa_aead_abort( &operation );
3899 PSA_DONE( );
3900}
3901/* END_CASE */
3902
3903/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003904void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3905 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003906 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003907 data_t *nonce,
3908 data_t *additional_data,
3909 data_t *input_data,
3910 int expected_status_arg )
3911{
3912
3913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3914 psa_key_type_t key_type = key_type_arg;
3915 psa_algorithm_t alg = alg_arg;
3916 psa_aead_operation_t operation;
3917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3918 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3919 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003920 unsigned char *output = NULL;
3921 unsigned char *ciphertext = NULL;
3922 size_t output_size = output_size_arg;
3923 size_t ciphertext_size = 0;
3924 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003925 size_t tag_length = 0;
3926 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3927
3928 PSA_ASSERT( psa_crypto_init( ) );
3929
3930 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3931 psa_set_key_algorithm( &attributes, alg );
3932 psa_set_key_type( &attributes, key_type );
3933
3934 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3935 &key ) );
3936
3937 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3938
Paul Elliottc6d11d02021-09-01 12:04:23 +01003939 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003940
Paul Elliottc6d11d02021-09-01 12:04:23 +01003941 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003942
Paul Elliottc6d11d02021-09-01 12:04:23 +01003943 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003944
3945 operation = psa_aead_operation_init( );
3946
3947 status = psa_aead_encrypt_setup( &operation, key, alg );
3948
3949 /* If the operation is not supported, just skip and not fail in case the
3950 * encryption involves a common limitation of cryptography hardwares and
3951 * an alternative implementation. */
3952 if( status == PSA_ERROR_NOT_SUPPORTED )
3953 {
3954 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3955 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3956 }
3957
3958 PSA_ASSERT( status );
3959
3960 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3961
3962 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3963 additional_data->len ) );
3964
3965 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003966 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003967
3968 TEST_EQUAL( status, expected_status );
3969
3970 if( expected_status == PSA_SUCCESS )
3971 {
3972 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003973 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3974 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003975 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3976 }
3977
3978exit:
3979 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003980 mbedtls_free( output );
3981 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003982 psa_aead_abort( &operation );
3983 PSA_DONE( );
3984}
3985/* END_CASE */
3986
Paul Elliott91b021e2021-07-23 18:52:31 +01003987/* BEGIN_CASE */
3988void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3989 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003990 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003991 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003992 data_t *nonce,
3993 data_t *additional_data,
3994 data_t *input_data,
3995 int expected_status_arg )
3996{
3997
3998 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3999 psa_key_type_t key_type = key_type_arg;
4000 psa_algorithm_t alg = alg_arg;
4001 psa_aead_operation_t operation;
4002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4003 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4004 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004005 unsigned char *ciphertext = NULL;
4006 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004007 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004008 size_t ciphertext_size = 0;
4009 size_t ciphertext_length = 0;
4010 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004011 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004012 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004013
4014 PSA_ASSERT( psa_crypto_init( ) );
4015
4016 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4017 psa_set_key_algorithm( &attributes, alg );
4018 psa_set_key_type( &attributes, key_type );
4019
4020 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4021 &key ) );
4022
4023 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4024
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004025 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004026
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004027 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004028
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004029 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004030
Paul Elliott719c1322021-09-13 18:27:22 +01004031 ASSERT_ALLOC( tag_buffer, tag_size );
4032
Paul Elliott91b021e2021-07-23 18:52:31 +01004033 operation = psa_aead_operation_init( );
4034
4035 status = psa_aead_encrypt_setup( &operation, key, alg );
4036
4037 /* If the operation is not supported, just skip and not fail in case the
4038 * encryption involves a common limitation of cryptography hardwares and
4039 * an alternative implementation. */
4040 if( status == PSA_ERROR_NOT_SUPPORTED )
4041 {
4042 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4043 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4044 }
4045
4046 PSA_ASSERT( status );
4047
4048 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4049
4050 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4051 additional_data->len ) );
4052
4053 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004054 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004055
4056 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004057 status = psa_aead_finish( &operation, finish_ciphertext,
4058 finish_ciphertext_size,
4059 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004060 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004061
4062 TEST_EQUAL( status, expected_status );
4063
4064exit:
4065 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004066 mbedtls_free( ciphertext );
4067 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004068 psa_aead_abort( &operation );
4069 PSA_DONE( );
4070}
4071/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004072
4073/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004074void aead_multipart_verify( int key_type_arg, data_t *key_data,
4075 int alg_arg,
4076 data_t *nonce,
4077 data_t *additional_data,
4078 data_t *input_data,
4079 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004080 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004081 int expected_status_arg )
4082{
4083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4084 psa_key_type_t key_type = key_type_arg;
4085 psa_algorithm_t alg = alg_arg;
4086 psa_aead_operation_t operation;
4087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4088 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4089 psa_status_t expected_status = expected_status_arg;
4090 unsigned char *plaintext = NULL;
4091 unsigned char *finish_plaintext = NULL;
4092 size_t plaintext_size = 0;
4093 size_t plaintext_length = 0;
4094 size_t verify_plaintext_size = 0;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004095 tagusage_method tag_usage = tag_usage_arg;
4096 unsigned char *tag_buffer = NULL;
4097 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004098
4099 PSA_ASSERT( psa_crypto_init( ) );
4100
4101 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4102 psa_set_key_algorithm( &attributes, alg );
4103 psa_set_key_type( &attributes, key_type );
4104
4105 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4106 &key ) );
4107
4108 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4109
4110 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4111 input_data->len );
4112
4113 ASSERT_ALLOC( plaintext, plaintext_size );
4114
4115 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4116
4117 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4118
4119 operation = psa_aead_operation_init( );
4120
4121 status = psa_aead_decrypt_setup( &operation, key, alg );
4122
4123 /* If the operation is not supported, just skip and not fail in case the
4124 * encryption involves a common limitation of cryptography hardwares and
4125 * an alternative implementation. */
4126 if( status == PSA_ERROR_NOT_SUPPORTED )
4127 {
4128 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4129 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4130 }
4131
4132 PSA_ASSERT( status );
4133
4134 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4135
4136 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4137 additional_data->len ) );
4138
4139 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4140 input_data->len,
4141 plaintext, plaintext_size,
4142 &plaintext_length ) );
4143
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004144 if( tag_usage == USE_GIVEN_TAG )
4145 {
4146 tag_buffer = tag->x;
4147 tag_size = tag->len;
4148 }
4149
Paul Elliott9961a662021-09-17 19:19:02 +01004150 status = psa_aead_verify( &operation, finish_plaintext,
4151 verify_plaintext_size,
4152 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004153 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004154
4155 TEST_EQUAL( status, expected_status );
4156
4157exit:
4158 psa_destroy_key( key );
4159 mbedtls_free( plaintext );
4160 mbedtls_free( finish_plaintext );
4161 psa_aead_abort( &operation );
4162 PSA_DONE( );
4163}
4164/* END_CASE */
4165
Paul Elliott9961a662021-09-17 19:19:02 +01004166/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004167void aead_multipart_setup( int key_type_arg, data_t *key_data,
4168 int alg_arg, int expected_status_arg )
4169{
4170 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4171 psa_key_type_t key_type = key_type_arg;
4172 psa_algorithm_t alg = alg_arg;
4173 psa_aead_operation_t operation;
4174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4175 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4176 psa_status_t expected_status = expected_status_arg;
4177
4178 PSA_ASSERT( psa_crypto_init( ) );
4179
4180 psa_set_key_usage_flags( &attributes,
4181 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4182 psa_set_key_algorithm( &attributes, alg );
4183 psa_set_key_type( &attributes, key_type );
4184
4185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4186 &key ) );
4187
4188 mbedtls_test_set_step( 0 );
4189
4190 status = psa_aead_encrypt_setup( &operation, key, alg );
4191
4192 TEST_EQUAL( status, expected_status );
4193
4194 psa_aead_abort( &operation );
4195
4196 operation = psa_aead_operation_init( );
4197
4198 mbedtls_test_set_step( 1 );
4199
4200 status = psa_aead_decrypt_setup( &operation, key, alg );
4201
4202 TEST_EQUAL(status, expected_status );
4203
4204exit:
4205 psa_destroy_key( key );
4206 psa_aead_abort( &operation );
4207 PSA_DONE( );
4208}
4209/* END_CASE */
4210
4211/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004212void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4213 int alg_arg,
4214 data_t *nonce,
4215 data_t *additional_data,
4216 data_t *input_data )
4217{
4218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4219 psa_key_type_t key_type = key_type_arg;
4220 psa_algorithm_t alg = alg_arg;
4221 psa_aead_operation_t operation;
4222 unsigned char *output_data = NULL;
4223 unsigned char *final_data = NULL;
4224 size_t output_size = 0;
4225 size_t finish_output_size = 0;
4226 size_t output_length = 0;
4227 size_t key_bits = 0;
4228 size_t tag_length = 0;
4229 size_t tag_size = 0;
4230 size_t nonce_length = 0;
4231 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4232 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4233 size_t output_part_length = 0;
4234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4235
4236 PSA_ASSERT( psa_crypto_init( ) );
4237
4238 psa_set_key_usage_flags( & attributes,
4239 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4240 psa_set_key_algorithm( & attributes, alg );
4241 psa_set_key_type( & attributes, key_type );
4242
4243 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4244 &key ) );
4245
4246 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4247 key_bits = psa_get_key_bits( &attributes );
4248
4249 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4250
4251 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4252
4253 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4254
4255 ASSERT_ALLOC( output_data, output_size );
4256
4257 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4258
4259 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4260
4261 ASSERT_ALLOC( final_data, finish_output_size );
4262
4263 /* Test all operations error without calling setup first. */
4264
4265 operation = psa_aead_operation_init( );
4266
4267 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4268 PSA_ERROR_BAD_STATE );
4269
4270 psa_aead_abort( &operation );
4271
4272 operation = psa_aead_operation_init( );
4273
4274 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4275 PSA_AEAD_NONCE_MAX_SIZE,
4276 &nonce_length ),
4277 PSA_ERROR_BAD_STATE );
4278
4279 psa_aead_abort( &operation );
4280
Paul Elliott481be342021-07-16 17:38:47 +01004281 /* ------------------------------------------------------- */
4282
Paul Elliottc23a9a02021-06-21 18:32:46 +01004283 operation = psa_aead_operation_init( );
4284
4285 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4286 input_data->len ),
4287 PSA_ERROR_BAD_STATE );
4288
4289 psa_aead_abort( &operation );
4290
Paul Elliott481be342021-07-16 17:38:47 +01004291 /* ------------------------------------------------------- */
4292
Paul Elliottc23a9a02021-06-21 18:32:46 +01004293 operation = psa_aead_operation_init( );
4294
4295 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4296 additional_data->len ),
4297 PSA_ERROR_BAD_STATE );
4298
4299 psa_aead_abort( &operation );
4300
Paul Elliott481be342021-07-16 17:38:47 +01004301 /* ------------------------------------------------------- */
4302
Paul Elliottc23a9a02021-06-21 18:32:46 +01004303 operation = psa_aead_operation_init( );
4304
4305 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4306 input_data->len, output_data,
4307 output_size, &output_length ),
4308 PSA_ERROR_BAD_STATE );
4309
4310 psa_aead_abort( &operation );
4311
Paul Elliott481be342021-07-16 17:38:47 +01004312 /* ------------------------------------------------------- */
4313
Paul Elliottc23a9a02021-06-21 18:32:46 +01004314 operation = psa_aead_operation_init( );
4315
4316 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4317 finish_output_size,
4318 &output_part_length,
4319 tag_buffer, tag_length,
4320 &tag_size ),
4321 PSA_ERROR_BAD_STATE );
4322
4323 psa_aead_abort( &operation );
4324
Paul Elliott481be342021-07-16 17:38:47 +01004325 /* ------------------------------------------------------- */
4326
Paul Elliottc23a9a02021-06-21 18:32:46 +01004327 operation = psa_aead_operation_init( );
4328
4329 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4330 finish_output_size,
4331 &output_part_length,
4332 tag_buffer,
4333 tag_length ),
4334 PSA_ERROR_BAD_STATE );
4335
4336 psa_aead_abort( &operation );
4337
4338 /* Test for double setups. */
4339
4340 operation = psa_aead_operation_init( );
4341
4342 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4343
4344 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4345 PSA_ERROR_BAD_STATE );
4346
4347 psa_aead_abort( &operation );
4348
Paul Elliott481be342021-07-16 17:38:47 +01004349 /* ------------------------------------------------------- */
4350
Paul Elliottc23a9a02021-06-21 18:32:46 +01004351 operation = psa_aead_operation_init( );
4352
4353 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4354
4355 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4356 PSA_ERROR_BAD_STATE );
4357
4358 psa_aead_abort( &operation );
4359
Paul Elliott374a2be2021-07-16 17:53:40 +01004360 /* ------------------------------------------------------- */
4361
4362 operation = psa_aead_operation_init( );
4363
4364 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4365
4366 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4367 PSA_ERROR_BAD_STATE );
4368
4369 psa_aead_abort( &operation );
4370
4371 /* ------------------------------------------------------- */
4372
4373 operation = psa_aead_operation_init( );
4374
4375 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4376
4377 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4378 PSA_ERROR_BAD_STATE );
4379
4380 psa_aead_abort( &operation );
4381
Paul Elliottc23a9a02021-06-21 18:32:46 +01004382 /* Test for not setting a nonce. */
4383
4384 operation = psa_aead_operation_init( );
4385
4386 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4387
4388 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4389 additional_data->len ),
4390 PSA_ERROR_BAD_STATE );
4391
4392 psa_aead_abort( &operation );
4393
Paul Elliott7f628422021-09-01 12:08:29 +01004394 /* ------------------------------------------------------- */
4395
4396 operation = psa_aead_operation_init( );
4397
4398 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4399
4400 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4401 input_data->len, output_data,
4402 output_size, &output_length ),
4403 PSA_ERROR_BAD_STATE );
4404
4405 psa_aead_abort( &operation );
4406
Paul Elliottc23a9a02021-06-21 18:32:46 +01004407 /* Test for double setting nonce. */
4408
4409 operation = psa_aead_operation_init( );
4410
4411 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4412
4413 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4414
4415 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4416 PSA_ERROR_BAD_STATE );
4417
4418 psa_aead_abort( &operation );
4419
Paul Elliott374a2be2021-07-16 17:53:40 +01004420 /* Test for double generating nonce. */
4421
4422 operation = psa_aead_operation_init( );
4423
4424 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4425
4426 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4427 PSA_AEAD_NONCE_MAX_SIZE,
4428 &nonce_length ) );
4429
4430 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4431 PSA_AEAD_NONCE_MAX_SIZE,
4432 &nonce_length ),
4433 PSA_ERROR_BAD_STATE );
4434
4435
4436 psa_aead_abort( &operation );
4437
4438 /* Test for generate nonce then set and vice versa */
4439
4440 operation = psa_aead_operation_init( );
4441
4442 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4443
4444 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4445 PSA_AEAD_NONCE_MAX_SIZE,
4446 &nonce_length ) );
4447
4448 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4449 PSA_ERROR_BAD_STATE );
4450
4451 psa_aead_abort( &operation );
4452
4453 /* ------------------------------------------------------- */
4454
4455 operation = psa_aead_operation_init( );
4456
4457 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4458
4459 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4460
4461 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4462 PSA_AEAD_NONCE_MAX_SIZE,
4463 &nonce_length ),
4464 PSA_ERROR_BAD_STATE );
4465
4466 psa_aead_abort( &operation );
4467
Paul Elliott7220cae2021-06-22 17:25:57 +01004468 /* Test for generating nonce in decrypt setup. */
4469
4470 operation = psa_aead_operation_init( );
4471
4472 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4473
4474 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4475 PSA_AEAD_NONCE_MAX_SIZE,
4476 &nonce_length ),
4477 PSA_ERROR_BAD_STATE );
4478
4479 psa_aead_abort( &operation );
4480
Paul Elliottc23a9a02021-06-21 18:32:46 +01004481 /* Test for setting lengths twice. */
4482
4483 operation = psa_aead_operation_init( );
4484
4485 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4486
4487 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4488
4489 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4490 input_data->len ) );
4491
4492 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4493 input_data->len ),
4494 PSA_ERROR_BAD_STATE );
4495
4496 psa_aead_abort( &operation );
4497
4498 /* Test for setting lengths after already starting data. */
4499
4500 operation = psa_aead_operation_init( );
4501
4502 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4503
4504 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4505
4506 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4507 input_data->len, output_data,
4508 output_size, &output_length ) );
4509
4510 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4511 input_data->len ),
4512 PSA_ERROR_BAD_STATE );
4513
4514 psa_aead_abort( &operation );
4515
Paul Elliott243080c2021-07-21 19:01:17 +01004516 /* Test for not sending any additional data or data after setting non zero
4517 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004518
4519 operation = psa_aead_operation_init( );
4520
4521 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4522
4523 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4524
4525 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4526 input_data->len ) );
4527
4528 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4529 finish_output_size,
4530 &output_part_length,
4531 tag_buffer, tag_length,
4532 &tag_size ),
4533 PSA_ERROR_INVALID_ARGUMENT );
4534
4535 psa_aead_abort( &operation );
4536
Paul Elliott243080c2021-07-21 19:01:17 +01004537 /* Test for not sending any additional data or data after setting non-zero
4538 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004539
4540 operation = psa_aead_operation_init( );
4541
4542 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4543
4544 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4545
4546 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4547 input_data->len ) );
4548
4549 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4550 finish_output_size,
4551 &output_part_length,
4552 tag_buffer,
4553 tag_length ),
4554 PSA_ERROR_INVALID_ARGUMENT );
4555
4556 psa_aead_abort( &operation );
4557
Paul Elliott243080c2021-07-21 19:01:17 +01004558 /* Test for not sending any additional data after setting a non-zero length
4559 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004560
4561 operation = psa_aead_operation_init( );
4562
4563 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4564
4565 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4566
4567 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4568 input_data->len ) );
4569
4570 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4571 input_data->len, output_data,
4572 output_size, &output_length ),
4573 PSA_ERROR_INVALID_ARGUMENT );
4574
4575 psa_aead_abort( &operation );
4576
Paul Elliottb0450fe2021-09-01 15:06:26 +01004577 /* Test for sending too much additional data after setting lengths. */
4578
4579 operation = psa_aead_operation_init( );
4580
4581 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4582
4583 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4584
4585 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4586
4587
4588 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4589 additional_data->len ),
4590 PSA_ERROR_INVALID_ARGUMENT );
4591
4592 psa_aead_abort( &operation );
4593
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004594 operation = psa_aead_operation_init( );
4595
4596 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4597
4598 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4599
4600 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4601 input_data->len ) );
4602
4603 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4604 additional_data->len ) );
4605
4606 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4607 1 ),
4608 PSA_ERROR_INVALID_ARGUMENT );
4609
4610 psa_aead_abort( &operation );
4611
Paul Elliottb0450fe2021-09-01 15:06:26 +01004612 /* Test for sending too much data after setting lengths. */
4613
4614 operation = psa_aead_operation_init( );
4615
4616 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4617
4618 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4619
4620 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4621
4622 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4623 input_data->len, output_data,
4624 output_size, &output_length ),
4625 PSA_ERROR_INVALID_ARGUMENT );
4626
4627 psa_aead_abort( &operation );
4628
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004629 operation = psa_aead_operation_init( );
4630
4631 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4632
4633 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4634
4635 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4636 input_data->len ) );
4637
4638 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4639 additional_data->len ) );
4640
4641 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4642 input_data->len, output_data,
4643 output_size, &output_length ) );
4644
4645 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4646 1, output_data,
4647 output_size, &output_length ),
4648 PSA_ERROR_INVALID_ARGUMENT );
4649
4650 psa_aead_abort( &operation );
4651
Paul Elliottc23a9a02021-06-21 18:32:46 +01004652 /* Test sending additional data after data. */
4653
4654 operation = psa_aead_operation_init( );
4655
4656 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4657
4658 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4659
4660 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4661 input_data->len, output_data,
4662 output_size, &output_length ) );
4663
4664 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4665 additional_data->len ),
4666 PSA_ERROR_BAD_STATE );
4667
4668 psa_aead_abort( &operation );
4669
Paul Elliott534d0b42021-06-22 19:15:20 +01004670 /* Test calling finish on decryption. */
4671
4672 operation = psa_aead_operation_init( );
4673
4674 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4675
4676 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4677
4678 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4679 finish_output_size,
4680 &output_part_length,
4681 tag_buffer, tag_length,
4682 &tag_size ),
4683 PSA_ERROR_BAD_STATE );
4684
4685 psa_aead_abort( &operation );
4686
4687 /* Test calling verify on encryption. */
4688
4689 operation = psa_aead_operation_init( );
4690
4691 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4692
4693 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4694
4695 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4696 finish_output_size,
4697 &output_part_length,
4698 tag_buffer,
4699 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004700 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004701
4702 psa_aead_abort( &operation );
4703
4704
Paul Elliottc23a9a02021-06-21 18:32:46 +01004705exit:
4706 psa_destroy_key( key );
4707 psa_aead_abort( &operation );
4708 mbedtls_free( output_data );
4709 mbedtls_free( final_data );
4710 PSA_DONE( );
4711}
4712/* END_CASE */
4713
4714/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004715void signature_size( int type_arg,
4716 int bits,
4717 int alg_arg,
4718 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004719{
4720 psa_key_type_t type = type_arg;
4721 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004722 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004723
Gilles Peskinefe11b722018-12-18 00:24:04 +01004724 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004725
Gilles Peskinee59236f2018-01-27 23:32:46 +01004726exit:
4727 ;
4728}
4729/* END_CASE */
4730
4731/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004732void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4733 int alg_arg, data_t *input_data,
4734 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004735{
Ronald Cron5425a212020-08-04 14:58:35 +02004736 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004737 psa_key_type_t key_type = key_type_arg;
4738 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004739 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004740 unsigned char *signature = NULL;
4741 size_t signature_size;
4742 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004744
Gilles Peskine8817f612018-12-18 00:18:46 +01004745 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004746
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004747 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004748 psa_set_key_algorithm( &attributes, alg );
4749 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004750
Gilles Peskine049c7532019-05-15 20:22:09 +02004751 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004752 &key ) );
4753 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004754 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004755
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004756 /* Allocate a buffer which has the size advertized by the
4757 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004758 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004759 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004760 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004761 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004762 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004763
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004764 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004765 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004766 input_data->x, input_data->len,
4767 signature, signature_size,
4768 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004769 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004770 ASSERT_COMPARE( output_data->x, output_data->len,
4771 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004772
4773exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004774 /*
4775 * Key attributes may have been returned by psa_get_key_attributes()
4776 * thus reset them as required.
4777 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004778 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004779
Ronald Cron5425a212020-08-04 14:58:35 +02004780 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004781 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004782 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004783}
4784/* END_CASE */
4785
4786/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004787void sign_hash_fail( int key_type_arg, data_t *key_data,
4788 int alg_arg, data_t *input_data,
4789 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004790{
Ronald Cron5425a212020-08-04 14:58:35 +02004791 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004792 psa_key_type_t key_type = key_type_arg;
4793 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004794 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004795 psa_status_t actual_status;
4796 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004797 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004798 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004799 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004800
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004801 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004802
Gilles Peskine8817f612018-12-18 00:18:46 +01004803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004804
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004805 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004806 psa_set_key_algorithm( &attributes, alg );
4807 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004808
Gilles Peskine049c7532019-05-15 20:22:09 +02004809 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004810 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004811
Ronald Cron5425a212020-08-04 14:58:35 +02004812 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004813 input_data->x, input_data->len,
4814 signature, signature_size,
4815 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004816 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004817 /* The value of *signature_length is unspecified on error, but
4818 * whatever it is, it should be less than signature_size, so that
4819 * if the caller tries to read *signature_length bytes without
4820 * checking the error code then they don't overflow a buffer. */
4821 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004822
4823exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004824 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004825 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004826 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004827 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004828}
4829/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004830
4831/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004832void sign_verify_hash( int key_type_arg, data_t *key_data,
4833 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004834{
Ronald Cron5425a212020-08-04 14:58:35 +02004835 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004836 psa_key_type_t key_type = key_type_arg;
4837 psa_algorithm_t alg = alg_arg;
4838 size_t key_bits;
4839 unsigned char *signature = NULL;
4840 size_t signature_size;
4841 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004842 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004843
Gilles Peskine8817f612018-12-18 00:18:46 +01004844 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004845
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004846 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004847 psa_set_key_algorithm( &attributes, alg );
4848 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004849
Gilles Peskine049c7532019-05-15 20:22:09 +02004850 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004851 &key ) );
4852 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004853 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004854
4855 /* Allocate a buffer which has the size advertized by the
4856 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004857 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004858 key_bits, alg );
4859 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004860 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004861 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004862
4863 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004864 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004865 input_data->x, input_data->len,
4866 signature, signature_size,
4867 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004868 /* Check that the signature length looks sensible. */
4869 TEST_ASSERT( signature_length <= signature_size );
4870 TEST_ASSERT( signature_length > 0 );
4871
4872 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004873 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004874 input_data->x, input_data->len,
4875 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004876
4877 if( input_data->len != 0 )
4878 {
4879 /* Flip a bit in the input and verify that the signature is now
4880 * detected as invalid. Flip a bit at the beginning, not at the end,
4881 * because ECDSA may ignore the last few bits of the input. */
4882 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004883 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004884 input_data->x, input_data->len,
4885 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004886 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004887 }
4888
4889exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004890 /*
4891 * Key attributes may have been returned by psa_get_key_attributes()
4892 * thus reset them as required.
4893 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004894 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004895
Ronald Cron5425a212020-08-04 14:58:35 +02004896 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004897 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004898 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004899}
4900/* END_CASE */
4901
4902/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004903void verify_hash( int key_type_arg, data_t *key_data,
4904 int alg_arg, data_t *hash_data,
4905 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004906{
Ronald Cron5425a212020-08-04 14:58:35 +02004907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004908 psa_key_type_t key_type = key_type_arg;
4909 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004911
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004912 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004913
Gilles Peskine8817f612018-12-18 00:18:46 +01004914 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004915
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004917 psa_set_key_algorithm( &attributes, alg );
4918 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004919
Gilles Peskine049c7532019-05-15 20:22:09 +02004920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004921 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004922
Ronald Cron5425a212020-08-04 14:58:35 +02004923 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004924 hash_data->x, hash_data->len,
4925 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004926
itayzafrir5c753392018-05-08 11:18:38 +03004927exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004928 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004929 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004930 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004931}
4932/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004933
4934/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004935void verify_hash_fail( int key_type_arg, data_t *key_data,
4936 int alg_arg, data_t *hash_data,
4937 data_t *signature_data,
4938 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004939{
Ronald Cron5425a212020-08-04 14:58:35 +02004940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004941 psa_key_type_t key_type = key_type_arg;
4942 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004943 psa_status_t actual_status;
4944 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004945 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004946
Gilles Peskine8817f612018-12-18 00:18:46 +01004947 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004948
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004949 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004950 psa_set_key_algorithm( &attributes, alg );
4951 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004952
Gilles Peskine049c7532019-05-15 20:22:09 +02004953 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004954 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004955
Ronald Cron5425a212020-08-04 14:58:35 +02004956 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004957 hash_data->x, hash_data->len,
4958 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004959 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004960
4961exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004962 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004963 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004964 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004965}
4966/* END_CASE */
4967
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004968/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004969void sign_message_deterministic( int key_type_arg,
4970 data_t *key_data,
4971 int alg_arg,
4972 data_t *input_data,
4973 data_t *output_data )
4974{
4975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4976 psa_key_type_t key_type = key_type_arg;
4977 psa_algorithm_t alg = alg_arg;
4978 size_t key_bits;
4979 unsigned char *signature = NULL;
4980 size_t signature_size;
4981 size_t signature_length = 0xdeadbeef;
4982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4983
4984 PSA_ASSERT( psa_crypto_init( ) );
4985
4986 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4987 psa_set_key_algorithm( &attributes, alg );
4988 psa_set_key_type( &attributes, key_type );
4989
4990 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4991 &key ) );
4992 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4993 key_bits = psa_get_key_bits( &attributes );
4994
4995 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4996 TEST_ASSERT( signature_size != 0 );
4997 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4998 ASSERT_ALLOC( signature, signature_size );
4999
5000 PSA_ASSERT( psa_sign_message( key, alg,
5001 input_data->x, input_data->len,
5002 signature, signature_size,
5003 &signature_length ) );
5004
5005 ASSERT_COMPARE( output_data->x, output_data->len,
5006 signature, signature_length );
5007
5008exit:
5009 psa_reset_key_attributes( &attributes );
5010
5011 psa_destroy_key( key );
5012 mbedtls_free( signature );
5013 PSA_DONE( );
5014
5015}
5016/* END_CASE */
5017
5018/* BEGIN_CASE */
5019void sign_message_fail( int key_type_arg,
5020 data_t *key_data,
5021 int alg_arg,
5022 data_t *input_data,
5023 int signature_size_arg,
5024 int expected_status_arg )
5025{
5026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5027 psa_key_type_t key_type = key_type_arg;
5028 psa_algorithm_t alg = alg_arg;
5029 size_t signature_size = signature_size_arg;
5030 psa_status_t actual_status;
5031 psa_status_t expected_status = expected_status_arg;
5032 unsigned char *signature = NULL;
5033 size_t signature_length = 0xdeadbeef;
5034 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5035
5036 ASSERT_ALLOC( signature, signature_size );
5037
5038 PSA_ASSERT( psa_crypto_init( ) );
5039
5040 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5041 psa_set_key_algorithm( &attributes, alg );
5042 psa_set_key_type( &attributes, key_type );
5043
5044 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5045 &key ) );
5046
5047 actual_status = psa_sign_message( key, alg,
5048 input_data->x, input_data->len,
5049 signature, signature_size,
5050 &signature_length );
5051 TEST_EQUAL( actual_status, expected_status );
5052 /* The value of *signature_length is unspecified on error, but
5053 * whatever it is, it should be less than signature_size, so that
5054 * if the caller tries to read *signature_length bytes without
5055 * checking the error code then they don't overflow a buffer. */
5056 TEST_ASSERT( signature_length <= signature_size );
5057
5058exit:
5059 psa_reset_key_attributes( &attributes );
5060 psa_destroy_key( key );
5061 mbedtls_free( signature );
5062 PSA_DONE( );
5063}
5064/* END_CASE */
5065
5066/* BEGIN_CASE */
5067void sign_verify_message( int key_type_arg,
5068 data_t *key_data,
5069 int alg_arg,
5070 data_t *input_data )
5071{
5072 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5073 psa_key_type_t key_type = key_type_arg;
5074 psa_algorithm_t alg = alg_arg;
5075 size_t key_bits;
5076 unsigned char *signature = NULL;
5077 size_t signature_size;
5078 size_t signature_length = 0xdeadbeef;
5079 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5080
5081 PSA_ASSERT( psa_crypto_init( ) );
5082
5083 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5084 PSA_KEY_USAGE_VERIFY_MESSAGE );
5085 psa_set_key_algorithm( &attributes, alg );
5086 psa_set_key_type( &attributes, key_type );
5087
5088 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5089 &key ) );
5090 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5091 key_bits = psa_get_key_bits( &attributes );
5092
5093 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5094 TEST_ASSERT( signature_size != 0 );
5095 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5096 ASSERT_ALLOC( signature, signature_size );
5097
5098 PSA_ASSERT( psa_sign_message( key, alg,
5099 input_data->x, input_data->len,
5100 signature, signature_size,
5101 &signature_length ) );
5102 TEST_ASSERT( signature_length <= signature_size );
5103 TEST_ASSERT( signature_length > 0 );
5104
5105 PSA_ASSERT( psa_verify_message( key, alg,
5106 input_data->x, input_data->len,
5107 signature, signature_length ) );
5108
5109 if( input_data->len != 0 )
5110 {
5111 /* Flip a bit in the input and verify that the signature is now
5112 * detected as invalid. Flip a bit at the beginning, not at the end,
5113 * because ECDSA may ignore the last few bits of the input. */
5114 input_data->x[0] ^= 1;
5115 TEST_EQUAL( psa_verify_message( key, alg,
5116 input_data->x, input_data->len,
5117 signature, signature_length ),
5118 PSA_ERROR_INVALID_SIGNATURE );
5119 }
5120
5121exit:
5122 psa_reset_key_attributes( &attributes );
5123
5124 psa_destroy_key( key );
5125 mbedtls_free( signature );
5126 PSA_DONE( );
5127}
5128/* END_CASE */
5129
5130/* BEGIN_CASE */
5131void verify_message( int key_type_arg,
5132 data_t *key_data,
5133 int alg_arg,
5134 data_t *input_data,
5135 data_t *signature_data )
5136{
5137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5138 psa_key_type_t key_type = key_type_arg;
5139 psa_algorithm_t alg = alg_arg;
5140 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5141
5142 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5143
5144 PSA_ASSERT( psa_crypto_init( ) );
5145
5146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5147 psa_set_key_algorithm( &attributes, alg );
5148 psa_set_key_type( &attributes, key_type );
5149
5150 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5151 &key ) );
5152
5153 PSA_ASSERT( psa_verify_message( key, alg,
5154 input_data->x, input_data->len,
5155 signature_data->x, signature_data->len ) );
5156
5157exit:
5158 psa_reset_key_attributes( &attributes );
5159 psa_destroy_key( key );
5160 PSA_DONE( );
5161}
5162/* END_CASE */
5163
5164/* BEGIN_CASE */
5165void verify_message_fail( int key_type_arg,
5166 data_t *key_data,
5167 int alg_arg,
5168 data_t *hash_data,
5169 data_t *signature_data,
5170 int expected_status_arg )
5171{
5172 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5173 psa_key_type_t key_type = key_type_arg;
5174 psa_algorithm_t alg = alg_arg;
5175 psa_status_t actual_status;
5176 psa_status_t expected_status = expected_status_arg;
5177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5178
5179 PSA_ASSERT( psa_crypto_init( ) );
5180
5181 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5182 psa_set_key_algorithm( &attributes, alg );
5183 psa_set_key_type( &attributes, key_type );
5184
5185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5186 &key ) );
5187
5188 actual_status = psa_verify_message( key, alg,
5189 hash_data->x, hash_data->len,
5190 signature_data->x,
5191 signature_data->len );
5192 TEST_EQUAL( actual_status, expected_status );
5193
5194exit:
5195 psa_reset_key_attributes( &attributes );
5196 psa_destroy_key( key );
5197 PSA_DONE( );
5198}
5199/* END_CASE */
5200
5201/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005202void asymmetric_encrypt( int key_type_arg,
5203 data_t *key_data,
5204 int alg_arg,
5205 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005206 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005207 int expected_output_length_arg,
5208 int expected_status_arg )
5209{
Ronald Cron5425a212020-08-04 14:58:35 +02005210 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005211 psa_key_type_t key_type = key_type_arg;
5212 psa_algorithm_t alg = alg_arg;
5213 size_t expected_output_length = expected_output_length_arg;
5214 size_t key_bits;
5215 unsigned char *output = NULL;
5216 size_t output_size;
5217 size_t output_length = ~0;
5218 psa_status_t actual_status;
5219 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005221
Gilles Peskine8817f612018-12-18 00:18:46 +01005222 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005223
Gilles Peskine656896e2018-06-29 19:12:28 +02005224 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005225 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5226 psa_set_key_algorithm( &attributes, alg );
5227 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005228 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005229 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005230
5231 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005232 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005233 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005234
Gilles Peskine656896e2018-06-29 19:12:28 +02005235 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005236 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005237 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005238
5239 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005240 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005241 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005242 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005243 output, output_size,
5244 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005245 TEST_EQUAL( actual_status, expected_status );
5246 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005247
Gilles Peskine68428122018-06-30 18:42:41 +02005248 /* If the label is empty, the test framework puts a non-null pointer
5249 * in label->x. Test that a null pointer works as well. */
5250 if( label->len == 0 )
5251 {
5252 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005253 if( output_size != 0 )
5254 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005255 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005256 input_data->x, input_data->len,
5257 NULL, label->len,
5258 output, output_size,
5259 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005260 TEST_EQUAL( actual_status, expected_status );
5261 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005262 }
5263
Gilles Peskine656896e2018-06-29 19:12:28 +02005264exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005265 /*
5266 * Key attributes may have been returned by psa_get_key_attributes()
5267 * thus reset them as required.
5268 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005269 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005270
Ronald Cron5425a212020-08-04 14:58:35 +02005271 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005272 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005273 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005274}
5275/* END_CASE */
5276
5277/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005278void asymmetric_encrypt_decrypt( int key_type_arg,
5279 data_t *key_data,
5280 int alg_arg,
5281 data_t *input_data,
5282 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005283{
Ronald Cron5425a212020-08-04 14:58:35 +02005284 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005285 psa_key_type_t key_type = key_type_arg;
5286 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005287 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005288 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005289 size_t output_size;
5290 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005291 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005292 size_t output2_size;
5293 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005294 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005295
Gilles Peskine8817f612018-12-18 00:18:46 +01005296 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005297
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005298 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5299 psa_set_key_algorithm( &attributes, alg );
5300 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005301
Gilles Peskine049c7532019-05-15 20:22:09 +02005302 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005303 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005304
5305 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005306 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005307 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005308
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005309 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005310 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005311 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005312
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005313 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005314 TEST_ASSERT( output2_size <=
5315 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5316 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005317 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005318
Gilles Peskineeebd7382018-06-08 18:11:54 +02005319 /* We test encryption by checking that encrypt-then-decrypt gives back
5320 * the original plaintext because of the non-optional random
5321 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005322 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005323 input_data->x, input_data->len,
5324 label->x, label->len,
5325 output, output_size,
5326 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005327 /* We don't know what ciphertext length to expect, but check that
5328 * it looks sensible. */
5329 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005330
Ronald Cron5425a212020-08-04 14:58:35 +02005331 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005332 output, output_length,
5333 label->x, label->len,
5334 output2, output2_size,
5335 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005336 ASSERT_COMPARE( input_data->x, input_data->len,
5337 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005338
5339exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005340 /*
5341 * Key attributes may have been returned by psa_get_key_attributes()
5342 * thus reset them as required.
5343 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005344 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005345
Ronald Cron5425a212020-08-04 14:58:35 +02005346 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005347 mbedtls_free( output );
5348 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005349 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005350}
5351/* END_CASE */
5352
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005353/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005354void asymmetric_decrypt( int key_type_arg,
5355 data_t *key_data,
5356 int alg_arg,
5357 data_t *input_data,
5358 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005359 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005360{
Ronald Cron5425a212020-08-04 14:58:35 +02005361 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005362 psa_key_type_t key_type = key_type_arg;
5363 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005364 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005365 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005366 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005367 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005369
Gilles Peskine8817f612018-12-18 00:18:46 +01005370 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005371
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005372 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5373 psa_set_key_algorithm( &attributes, alg );
5374 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005375
Gilles Peskine049c7532019-05-15 20:22:09 +02005376 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005377 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005378
gabor-mezei-armceface22021-01-21 12:26:17 +01005379 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5380 key_bits = psa_get_key_bits( &attributes );
5381
5382 /* Determine the maximum ciphertext length */
5383 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5384 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5385 ASSERT_ALLOC( output, output_size );
5386
Ronald Cron5425a212020-08-04 14:58:35 +02005387 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005388 input_data->x, input_data->len,
5389 label->x, label->len,
5390 output,
5391 output_size,
5392 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005393 ASSERT_COMPARE( expected_data->x, expected_data->len,
5394 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005395
Gilles Peskine68428122018-06-30 18:42:41 +02005396 /* If the label is empty, the test framework puts a non-null pointer
5397 * in label->x. Test that a null pointer works as well. */
5398 if( label->len == 0 )
5399 {
5400 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005401 if( output_size != 0 )
5402 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005403 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005404 input_data->x, input_data->len,
5405 NULL, label->len,
5406 output,
5407 output_size,
5408 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005409 ASSERT_COMPARE( expected_data->x, expected_data->len,
5410 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005411 }
5412
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005413exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005414 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005415 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005416 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005417 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005418}
5419/* END_CASE */
5420
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005421/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005422void asymmetric_decrypt_fail( int key_type_arg,
5423 data_t *key_data,
5424 int alg_arg,
5425 data_t *input_data,
5426 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005427 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005428 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005429{
Ronald Cron5425a212020-08-04 14:58:35 +02005430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005431 psa_key_type_t key_type = key_type_arg;
5432 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005433 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005434 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005435 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005436 psa_status_t actual_status;
5437 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005439
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005440 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005441
Gilles Peskine8817f612018-12-18 00:18:46 +01005442 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005443
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005444 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5445 psa_set_key_algorithm( &attributes, alg );
5446 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005447
Gilles Peskine049c7532019-05-15 20:22:09 +02005448 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005449 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005450
Ronald Cron5425a212020-08-04 14:58:35 +02005451 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005452 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005453 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005454 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005455 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005456 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005457 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005458
Gilles Peskine68428122018-06-30 18:42:41 +02005459 /* If the label is empty, the test framework puts a non-null pointer
5460 * in label->x. Test that a null pointer works as well. */
5461 if( label->len == 0 )
5462 {
5463 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005464 if( output_size != 0 )
5465 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005466 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005467 input_data->x, input_data->len,
5468 NULL, label->len,
5469 output, output_size,
5470 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005471 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005472 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005473 }
5474
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005475exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005476 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005477 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005478 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005479 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005480}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005481/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005482
5483/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005484void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005485{
5486 /* Test each valid way of initializing the object, except for `= {0}`, as
5487 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5488 * though it's OK by the C standard. We could test for this, but we'd need
5489 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005490 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005491 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5492 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5493 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005494
5495 memset( &zero, 0, sizeof( zero ) );
5496
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005497 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005498 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005499 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005500 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005501 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005502 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005503 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005504
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005505 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005506 PSA_ASSERT( psa_key_derivation_abort(&func) );
5507 PSA_ASSERT( psa_key_derivation_abort(&init) );
5508 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005509}
5510/* END_CASE */
5511
Janos Follath16de4a42019-06-13 16:32:24 +01005512/* BEGIN_CASE */
5513void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005514{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005515 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005516 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005518
Gilles Peskine8817f612018-12-18 00:18:46 +01005519 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005520
Janos Follath16de4a42019-06-13 16:32:24 +01005521 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005522 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005523
5524exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005526 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005527}
5528/* END_CASE */
5529
Janos Follathaf3c2a02019-06-12 12:34:34 +01005530/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005531void derive_set_capacity( int alg_arg, int capacity_arg,
5532 int expected_status_arg )
5533{
5534 psa_algorithm_t alg = alg_arg;
5535 size_t capacity = capacity_arg;
5536 psa_status_t expected_status = expected_status_arg;
5537 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5538
5539 PSA_ASSERT( psa_crypto_init( ) );
5540
5541 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5542
5543 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5544 expected_status );
5545
5546exit:
5547 psa_key_derivation_abort( &operation );
5548 PSA_DONE( );
5549}
5550/* END_CASE */
5551
5552/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005553void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005554 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005555 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005556 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005557 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005558 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005559 int expected_status_arg3,
5560 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005561{
5562 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005563 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5564 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005565 psa_status_t expected_statuses[] = {expected_status_arg1,
5566 expected_status_arg2,
5567 expected_status_arg3};
5568 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005569 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5570 MBEDTLS_SVC_KEY_ID_INIT,
5571 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005572 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5573 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5574 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005575 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005576 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005577 psa_status_t expected_output_status = expected_output_status_arg;
5578 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005579
5580 PSA_ASSERT( psa_crypto_init( ) );
5581
5582 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5583 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005584
5585 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5586
5587 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5588 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005589 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005590 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005591 psa_set_key_type( &attributes, key_types[i] );
5592 PSA_ASSERT( psa_import_key( &attributes,
5593 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005594 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005595 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5596 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5597 {
5598 // When taking a private key as secret input, use key agreement
5599 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005600 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5601 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005602 expected_statuses[i] );
5603 }
5604 else
5605 {
5606 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005607 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005608 expected_statuses[i] );
5609 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005610 }
5611 else
5612 {
5613 TEST_EQUAL( psa_key_derivation_input_bytes(
5614 &operation, steps[i],
5615 inputs[i]->x, inputs[i]->len ),
5616 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005617 }
5618 }
5619
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005620 if( output_key_type != PSA_KEY_TYPE_NONE )
5621 {
5622 psa_reset_key_attributes( &attributes );
5623 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5624 psa_set_key_bits( &attributes, 8 );
5625 actual_output_status =
5626 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005627 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005628 }
5629 else
5630 {
5631 uint8_t buffer[1];
5632 actual_output_status =
5633 psa_key_derivation_output_bytes( &operation,
5634 buffer, sizeof( buffer ) );
5635 }
5636 TEST_EQUAL( actual_output_status, expected_output_status );
5637
Janos Follathaf3c2a02019-06-12 12:34:34 +01005638exit:
5639 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005640 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5641 psa_destroy_key( keys[i] );
5642 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005643 PSA_DONE( );
5644}
5645/* END_CASE */
5646
Janos Follathd958bb72019-07-03 15:02:16 +01005647/* BEGIN_CASE */
5648void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005649{
Janos Follathd958bb72019-07-03 15:02:16 +01005650 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005652 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005653 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005654 unsigned char input1[] = "Input 1";
5655 size_t input1_length = sizeof( input1 );
5656 unsigned char input2[] = "Input 2";
5657 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005658 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005659 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005660 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5661 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5662 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005664
Gilles Peskine8817f612018-12-18 00:18:46 +01005665 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005666
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005667 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5668 psa_set_key_algorithm( &attributes, alg );
5669 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005670
Gilles Peskine73676cb2019-05-15 20:15:10 +02005671 PSA_ASSERT( psa_import_key( &attributes,
5672 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005673 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005674
5675 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005676 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5677 input1, input1_length,
5678 input2, input2_length,
5679 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005680 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005681
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005683 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005684 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005685
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005687
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005688 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005689 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005690
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005691exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005692 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005693 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005694 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005695}
5696/* END_CASE */
5697
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005698/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005699void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005700{
5701 uint8_t output_buffer[16];
5702 size_t buffer_size = 16;
5703 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005704 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005705
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005706 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5707 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005708 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005709
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005710 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005711 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005712
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005713 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005714
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005715 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5716 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005717 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005718
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005719 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005720 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005721
5722exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005724}
5725/* END_CASE */
5726
5727/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005728void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005729 int step1_arg, data_t *input1,
5730 int step2_arg, data_t *input2,
5731 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005732 int requested_capacity_arg,
5733 data_t *expected_output1,
5734 data_t *expected_output2 )
5735{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005736 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005737 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5738 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005739 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5740 MBEDTLS_SVC_KEY_ID_INIT,
5741 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005742 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005743 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005744 uint8_t *expected_outputs[2] =
5745 {expected_output1->x, expected_output2->x};
5746 size_t output_sizes[2] =
5747 {expected_output1->len, expected_output2->len};
5748 size_t output_buffer_size = 0;
5749 uint8_t *output_buffer = NULL;
5750 size_t expected_capacity;
5751 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005752 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005753 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005754 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005755
5756 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5757 {
5758 if( output_sizes[i] > output_buffer_size )
5759 output_buffer_size = output_sizes[i];
5760 if( output_sizes[i] == 0 )
5761 expected_outputs[i] = NULL;
5762 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005763 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005764 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005765
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005766 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5767 psa_set_key_algorithm( &attributes, alg );
5768 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005769
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005770 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005771 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5772 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5773 requested_capacity ) );
5774 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005775 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005776 switch( steps[i] )
5777 {
5778 case 0:
5779 break;
5780 case PSA_KEY_DERIVATION_INPUT_SECRET:
5781 PSA_ASSERT( psa_import_key( &attributes,
5782 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005783 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005784
5785 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5786 {
5787 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5788 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5789 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5790 }
5791
Gilles Peskine1468da72019-05-29 17:35:49 +02005792 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005793 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005794 break;
5795 default:
5796 PSA_ASSERT( psa_key_derivation_input_bytes(
5797 &operation, steps[i],
5798 inputs[i]->x, inputs[i]->len ) );
5799 break;
5800 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005801 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005802
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005803 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005804 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005805 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005806 expected_capacity = requested_capacity;
5807
5808 /* Expansion phase. */
5809 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5810 {
5811 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005812 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005813 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005814 if( expected_capacity == 0 && output_sizes[i] == 0 )
5815 {
5816 /* Reading 0 bytes when 0 bytes are available can go either way. */
5817 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005818 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005819 continue;
5820 }
5821 else if( expected_capacity == 0 ||
5822 output_sizes[i] > expected_capacity )
5823 {
5824 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005825 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005826 expected_capacity = 0;
5827 continue;
5828 }
5829 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005830 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005831 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005832 ASSERT_COMPARE( output_buffer, output_sizes[i],
5833 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005834 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005835 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005836 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005837 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005838 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005839 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005840 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005841
5842exit:
5843 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005844 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005845 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5846 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005847 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005848}
5849/* END_CASE */
5850
5851/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005852void derive_full( int alg_arg,
5853 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005854 data_t *input1,
5855 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005856 int requested_capacity_arg )
5857{
Ronald Cron5425a212020-08-04 14:58:35 +02005858 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005859 psa_algorithm_t alg = alg_arg;
5860 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005861 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005862 unsigned char output_buffer[16];
5863 size_t expected_capacity = requested_capacity;
5864 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005866
Gilles Peskine8817f612018-12-18 00:18:46 +01005867 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005868
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005869 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5870 psa_set_key_algorithm( &attributes, alg );
5871 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005872
Gilles Peskine049c7532019-05-15 20:22:09 +02005873 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005874 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005875
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005876 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5877 input1->x, input1->len,
5878 input2->x, input2->len,
5879 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005880 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005881
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005882 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005883 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005884 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005885
5886 /* Expansion phase. */
5887 while( current_capacity > 0 )
5888 {
5889 size_t read_size = sizeof( output_buffer );
5890 if( read_size > current_capacity )
5891 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005892 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005893 output_buffer,
5894 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005895 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005896 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005897 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005898 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005899 }
5900
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005901 /* Check that the operation refuses to go over capacity. */
5902 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005903 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005904
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005905 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005906
5907exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005908 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005909 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005910 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005911}
5912/* END_CASE */
5913
Janos Follathe60c9052019-07-03 13:51:30 +01005914/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005915void derive_key_exercise( int alg_arg,
5916 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005917 data_t *input1,
5918 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005919 int derived_type_arg,
5920 int derived_bits_arg,
5921 int derived_usage_arg,
5922 int derived_alg_arg )
5923{
Ronald Cron5425a212020-08-04 14:58:35 +02005924 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5925 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005926 psa_algorithm_t alg = alg_arg;
5927 psa_key_type_t derived_type = derived_type_arg;
5928 size_t derived_bits = derived_bits_arg;
5929 psa_key_usage_t derived_usage = derived_usage_arg;
5930 psa_algorithm_t derived_alg = derived_alg_arg;
5931 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005932 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005934 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005935
Gilles Peskine8817f612018-12-18 00:18:46 +01005936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005937
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005938 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5939 psa_set_key_algorithm( &attributes, alg );
5940 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005941 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005942 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005943
5944 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005945 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5946 input1->x, input1->len,
5947 input2->x, input2->len,
5948 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005949 goto exit;
5950
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005951 psa_set_key_usage_flags( &attributes, derived_usage );
5952 psa_set_key_algorithm( &attributes, derived_alg );
5953 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005954 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005955 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005956 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005957
5958 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005959 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005960 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5961 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005962
5963 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005964 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005965 goto exit;
5966
5967exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005968 /*
5969 * Key attributes may have been returned by psa_get_key_attributes()
5970 * thus reset them as required.
5971 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005972 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005973
5974 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005975 psa_destroy_key( base_key );
5976 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005977 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005978}
5979/* END_CASE */
5980
Janos Follath42fd8882019-07-03 14:17:09 +01005981/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005982void derive_key_export( int alg_arg,
5983 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005984 data_t *input1,
5985 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005986 int bytes1_arg,
5987 int bytes2_arg )
5988{
Ronald Cron5425a212020-08-04 14:58:35 +02005989 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5990 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005991 psa_algorithm_t alg = alg_arg;
5992 size_t bytes1 = bytes1_arg;
5993 size_t bytes2 = bytes2_arg;
5994 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005995 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005996 uint8_t *output_buffer = NULL;
5997 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005998 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5999 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006000 size_t length;
6001
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006002 ASSERT_ALLOC( output_buffer, capacity );
6003 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006004 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006005
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006006 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6007 psa_set_key_algorithm( &base_attributes, alg );
6008 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006009 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006010 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006011
6012 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006013 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6014 input1->x, input1->len,
6015 input2->x, input2->len,
6016 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006017 goto exit;
6018
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006019 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006020 output_buffer,
6021 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006022 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006023
6024 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006025 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6026 input1->x, input1->len,
6027 input2->x, input2->len,
6028 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006029 goto exit;
6030
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006031 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6032 psa_set_key_algorithm( &derived_attributes, 0 );
6033 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006034 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006035 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006036 &derived_key ) );
6037 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006038 export_buffer, bytes1,
6039 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006040 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006041 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006042 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006043 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006044 &derived_key ) );
6045 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006046 export_buffer + bytes1, bytes2,
6047 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006048 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006049
6050 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006051 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6052 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006053
6054exit:
6055 mbedtls_free( output_buffer );
6056 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006057 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006058 psa_destroy_key( base_key );
6059 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006060 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006061}
6062/* END_CASE */
6063
6064/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006065void derive_key( int alg_arg,
6066 data_t *key_data, data_t *input1, data_t *input2,
6067 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006068 int expected_status_arg,
6069 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006070{
Ronald Cron5425a212020-08-04 14:58:35 +02006071 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6072 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006073 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006074 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006075 size_t bits = bits_arg;
6076 psa_status_t expected_status = expected_status_arg;
6077 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6078 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6079 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6080
6081 PSA_ASSERT( psa_crypto_init( ) );
6082
6083 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6084 psa_set_key_algorithm( &base_attributes, alg );
6085 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6086 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006087 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006088
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006089 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6090 input1->x, input1->len,
6091 input2->x, input2->len,
6092 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006093 goto exit;
6094
6095 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6096 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006097 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006098 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006099
6100 psa_status_t status =
6101 psa_key_derivation_output_key( &derived_attributes,
6102 &operation,
6103 &derived_key );
6104 if( is_large_output > 0 )
6105 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6106 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006107
6108exit:
6109 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006110 psa_destroy_key( base_key );
6111 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006112 PSA_DONE( );
6113}
6114/* END_CASE */
6115
6116/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006117void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006118 int our_key_type_arg, int our_key_alg_arg,
6119 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006120 int expected_status_arg )
6121{
Ronald Cron5425a212020-08-04 14:58:35 +02006122 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006123 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006124 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006125 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006126 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006127 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006128 psa_status_t expected_status = expected_status_arg;
6129 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006130
Gilles Peskine8817f612018-12-18 00:18:46 +01006131 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006132
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006133 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006134 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006135 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006136 PSA_ASSERT( psa_import_key( &attributes,
6137 our_key_data->x, our_key_data->len,
6138 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006139
Gilles Peskine77f40d82019-04-11 21:27:06 +02006140 /* The tests currently include inputs that should fail at either step.
6141 * Test cases that fail at the setup step should be changed to call
6142 * key_derivation_setup instead, and this function should be renamed
6143 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006144 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006145 if( status == PSA_SUCCESS )
6146 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006147 TEST_EQUAL( psa_key_derivation_key_agreement(
6148 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6149 our_key,
6150 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006151 expected_status );
6152 }
6153 else
6154 {
6155 TEST_ASSERT( status == expected_status );
6156 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006157
6158exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006159 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006160 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006161 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006162}
6163/* END_CASE */
6164
6165/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006166void raw_key_agreement( int alg_arg,
6167 int our_key_type_arg, data_t *our_key_data,
6168 data_t *peer_key_data,
6169 data_t *expected_output )
6170{
Ronald Cron5425a212020-08-04 14:58:35 +02006171 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006172 psa_algorithm_t alg = alg_arg;
6173 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006175 unsigned char *output = NULL;
6176 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006177 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006178
6179 ASSERT_ALLOC( output, expected_output->len );
6180 PSA_ASSERT( psa_crypto_init( ) );
6181
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006182 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6183 psa_set_key_algorithm( &attributes, alg );
6184 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006185 PSA_ASSERT( psa_import_key( &attributes,
6186 our_key_data->x, our_key_data->len,
6187 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006188
gabor-mezei-armceface22021-01-21 12:26:17 +01006189 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6190 key_bits = psa_get_key_bits( &attributes );
6191
Gilles Peskinebe697d82019-05-16 18:00:41 +02006192 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6193 peer_key_data->x, peer_key_data->len,
6194 output, expected_output->len,
6195 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006196 ASSERT_COMPARE( output, output_length,
6197 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006198 TEST_ASSERT( output_length <=
6199 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6200 TEST_ASSERT( output_length <=
6201 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006202
6203exit:
6204 mbedtls_free( output );
6205 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006206 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006207}
6208/* END_CASE */
6209
6210/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006211void key_agreement_capacity( int alg_arg,
6212 int our_key_type_arg, data_t *our_key_data,
6213 data_t *peer_key_data,
6214 int expected_capacity_arg )
6215{
Ronald Cron5425a212020-08-04 14:58:35 +02006216 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006217 psa_algorithm_t alg = alg_arg;
6218 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006219 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006221 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006222 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006223
Gilles Peskine8817f612018-12-18 00:18:46 +01006224 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006225
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006226 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6227 psa_set_key_algorithm( &attributes, alg );
6228 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006229 PSA_ASSERT( psa_import_key( &attributes,
6230 our_key_data->x, our_key_data->len,
6231 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006232
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006233 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006234 PSA_ASSERT( psa_key_derivation_key_agreement(
6235 &operation,
6236 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6237 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006238 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6239 {
6240 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006241 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006242 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006243 NULL, 0 ) );
6244 }
Gilles Peskine59685592018-09-18 12:11:34 +02006245
Gilles Peskinebf491972018-10-25 22:36:12 +02006246 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006247 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006248 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006249 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006250
Gilles Peskinebf491972018-10-25 22:36:12 +02006251 /* Test the actual capacity by reading the output. */
6252 while( actual_capacity > sizeof( output ) )
6253 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006254 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006255 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006256 actual_capacity -= sizeof( output );
6257 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006258 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006259 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006260 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006261 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006262
Gilles Peskine59685592018-09-18 12:11:34 +02006263exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006264 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006265 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006266 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006267}
6268/* END_CASE */
6269
6270/* BEGIN_CASE */
6271void key_agreement_output( int alg_arg,
6272 int our_key_type_arg, data_t *our_key_data,
6273 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006274 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006275{
Ronald Cron5425a212020-08-04 14:58:35 +02006276 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006277 psa_algorithm_t alg = alg_arg;
6278 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006279 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006281 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006282
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006283 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6284 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006285
Gilles Peskine8817f612018-12-18 00:18:46 +01006286 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006287
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006288 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6289 psa_set_key_algorithm( &attributes, alg );
6290 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006291 PSA_ASSERT( psa_import_key( &attributes,
6292 our_key_data->x, our_key_data->len,
6293 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006294
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006295 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006296 PSA_ASSERT( psa_key_derivation_key_agreement(
6297 &operation,
6298 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6299 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006300 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6301 {
6302 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006303 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006304 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006305 NULL, 0 ) );
6306 }
Gilles Peskine59685592018-09-18 12:11:34 +02006307
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006308 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006309 actual_output,
6310 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006311 ASSERT_COMPARE( actual_output, expected_output1->len,
6312 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006313 if( expected_output2->len != 0 )
6314 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006315 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006316 actual_output,
6317 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006318 ASSERT_COMPARE( actual_output, expected_output2->len,
6319 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006320 }
Gilles Peskine59685592018-09-18 12:11:34 +02006321
6322exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006323 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006324 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006325 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006326 mbedtls_free( actual_output );
6327}
6328/* END_CASE */
6329
6330/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006331void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006332{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006333 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006334 unsigned char *output = NULL;
6335 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006336 size_t i;
6337 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006338
Simon Butcher49f8e312020-03-03 15:51:50 +00006339 TEST_ASSERT( bytes_arg >= 0 );
6340
Gilles Peskine91892022021-02-08 19:50:26 +01006341 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006342 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006343
Gilles Peskine8817f612018-12-18 00:18:46 +01006344 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006345
Gilles Peskinea50d7392018-06-21 10:22:13 +02006346 /* Run several times, to ensure that every output byte will be
6347 * nonzero at least once with overwhelming probability
6348 * (2^(-8*number_of_runs)). */
6349 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006350 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006351 if( bytes != 0 )
6352 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006353 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006354
Gilles Peskinea50d7392018-06-21 10:22:13 +02006355 for( i = 0; i < bytes; i++ )
6356 {
6357 if( output[i] != 0 )
6358 ++changed[i];
6359 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006360 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006361
6362 /* Check that every byte was changed to nonzero at least once. This
6363 * validates that psa_generate_random is overwriting every byte of
6364 * the output buffer. */
6365 for( i = 0; i < bytes; i++ )
6366 {
6367 TEST_ASSERT( changed[i] != 0 );
6368 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006369
6370exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006371 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006372 mbedtls_free( output );
6373 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006374}
6375/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006376
6377/* BEGIN_CASE */
6378void generate_key( int type_arg,
6379 int bits_arg,
6380 int usage_arg,
6381 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006382 int expected_status_arg,
6383 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006384{
Ronald Cron5425a212020-08-04 14:58:35 +02006385 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006386 psa_key_type_t type = type_arg;
6387 psa_key_usage_t usage = usage_arg;
6388 size_t bits = bits_arg;
6389 psa_algorithm_t alg = alg_arg;
6390 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006391 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006392 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006393
Gilles Peskine8817f612018-12-18 00:18:46 +01006394 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006395
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006396 psa_set_key_usage_flags( &attributes, usage );
6397 psa_set_key_algorithm( &attributes, alg );
6398 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006399 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006400
6401 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006402 psa_status_t status = psa_generate_key( &attributes, &key );
6403
6404 if( is_large_key > 0 )
6405 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6406 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006407 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006408 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006409
6410 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006411 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006412 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6413 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006414
Gilles Peskine818ca122018-06-20 18:16:48 +02006415 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006416 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006417 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006418
6419exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006420 /*
6421 * Key attributes may have been returned by psa_get_key_attributes()
6422 * thus reset them as required.
6423 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006424 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006425
Ronald Cron5425a212020-08-04 14:58:35 +02006426 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006427 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006428}
6429/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006430
Ronald Cronee414c72021-03-18 18:50:08 +01006431/* 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 +02006432void generate_key_rsa( int bits_arg,
6433 data_t *e_arg,
6434 int expected_status_arg )
6435{
Ronald Cron5425a212020-08-04 14:58:35 +02006436 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006437 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006438 size_t bits = bits_arg;
6439 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6440 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6441 psa_status_t expected_status = expected_status_arg;
6442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6443 uint8_t *exported = NULL;
6444 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006445 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006446 size_t exported_length = SIZE_MAX;
6447 uint8_t *e_read_buffer = NULL;
6448 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006449 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006450 size_t e_read_length = SIZE_MAX;
6451
6452 if( e_arg->len == 0 ||
6453 ( e_arg->len == 3 &&
6454 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6455 {
6456 is_default_public_exponent = 1;
6457 e_read_size = 0;
6458 }
6459 ASSERT_ALLOC( e_read_buffer, e_read_size );
6460 ASSERT_ALLOC( exported, exported_size );
6461
6462 PSA_ASSERT( psa_crypto_init( ) );
6463
6464 psa_set_key_usage_flags( &attributes, usage );
6465 psa_set_key_algorithm( &attributes, alg );
6466 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6467 e_arg->x, e_arg->len ) );
6468 psa_set_key_bits( &attributes, bits );
6469
6470 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006471 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006472 if( expected_status != PSA_SUCCESS )
6473 goto exit;
6474
6475 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006476 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006477 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6478 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6479 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6480 e_read_buffer, e_read_size,
6481 &e_read_length ) );
6482 if( is_default_public_exponent )
6483 TEST_EQUAL( e_read_length, 0 );
6484 else
6485 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6486
6487 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006488 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006489 goto exit;
6490
6491 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006492 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006493 exported, exported_size,
6494 &exported_length ) );
6495 {
6496 uint8_t *p = exported;
6497 uint8_t *end = exported + exported_length;
6498 size_t len;
6499 /* RSAPublicKey ::= SEQUENCE {
6500 * modulus INTEGER, -- n
6501 * publicExponent INTEGER } -- e
6502 */
6503 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006504 MBEDTLS_ASN1_SEQUENCE |
6505 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006506 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006507 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6508 MBEDTLS_ASN1_INTEGER ) );
6509 if( len >= 1 && p[0] == 0 )
6510 {
6511 ++p;
6512 --len;
6513 }
6514 if( e_arg->len == 0 )
6515 {
6516 TEST_EQUAL( len, 3 );
6517 TEST_EQUAL( p[0], 1 );
6518 TEST_EQUAL( p[1], 0 );
6519 TEST_EQUAL( p[2], 1 );
6520 }
6521 else
6522 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6523 }
6524
6525exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006526 /*
6527 * Key attributes may have been returned by psa_get_key_attributes() or
6528 * set by psa_set_key_domain_parameters() thus reset them as required.
6529 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006530 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006531
Ronald Cron5425a212020-08-04 14:58:35 +02006532 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006533 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006534 mbedtls_free( e_read_buffer );
6535 mbedtls_free( exported );
6536}
6537/* END_CASE */
6538
Darryl Greend49a4992018-06-18 17:27:26 +01006539/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006540void persistent_key_load_key_from_storage( data_t *data,
6541 int type_arg, int bits_arg,
6542 int usage_flags_arg, int alg_arg,
6543 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006544{
Ronald Cron71016a92020-08-28 19:01:50 +02006545 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006546 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6548 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006549 psa_key_type_t type = type_arg;
6550 size_t bits = bits_arg;
6551 psa_key_usage_t usage_flags = usage_flags_arg;
6552 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006553 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006554 unsigned char *first_export = NULL;
6555 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006556 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006557 size_t first_exported_length;
6558 size_t second_exported_length;
6559
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006560 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6561 {
6562 ASSERT_ALLOC( first_export, export_size );
6563 ASSERT_ALLOC( second_export, export_size );
6564 }
Darryl Greend49a4992018-06-18 17:27:26 +01006565
Gilles Peskine8817f612018-12-18 00:18:46 +01006566 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006567
Gilles Peskinec87af662019-05-15 16:12:22 +02006568 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006569 psa_set_key_usage_flags( &attributes, usage_flags );
6570 psa_set_key_algorithm( &attributes, alg );
6571 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006572 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006573
Darryl Green0c6575a2018-11-07 16:05:30 +00006574 switch( generation_method )
6575 {
6576 case IMPORT_KEY:
6577 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006578 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006579 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006580 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006581
Darryl Green0c6575a2018-11-07 16:05:30 +00006582 case GENERATE_KEY:
6583 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006584 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006585 break;
6586
6587 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006588#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006589 {
6590 /* Create base key */
6591 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6592 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6593 psa_set_key_usage_flags( &base_attributes,
6594 PSA_KEY_USAGE_DERIVE );
6595 psa_set_key_algorithm( &base_attributes, derive_alg );
6596 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006597 PSA_ASSERT( psa_import_key( &base_attributes,
6598 data->x, data->len,
6599 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006600 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006601 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006602 PSA_ASSERT( psa_key_derivation_input_key(
6603 &operation,
6604 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006605 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006606 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006607 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006608 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6609 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006610 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006611 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006612 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006613 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006614 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006615#else
6616 TEST_ASSUME( ! "KDF not supported in this configuration" );
6617#endif
6618 break;
6619
6620 default:
6621 TEST_ASSERT( ! "generation_method not implemented in test" );
6622 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006623 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006624 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006625
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006626 /* Export the key if permitted by the key policy. */
6627 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6628 {
Ronald Cron5425a212020-08-04 14:58:35 +02006629 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006630 first_export, export_size,
6631 &first_exported_length ) );
6632 if( generation_method == IMPORT_KEY )
6633 ASSERT_COMPARE( data->x, data->len,
6634 first_export, first_exported_length );
6635 }
Darryl Greend49a4992018-06-18 17:27:26 +01006636
6637 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006638 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006639 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006640 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006641
Darryl Greend49a4992018-06-18 17:27:26 +01006642 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006643 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006644 TEST_ASSERT( mbedtls_svc_key_id_equal(
6645 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006646 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6647 PSA_KEY_LIFETIME_PERSISTENT );
6648 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6649 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6650 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6651 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006652
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006653 /* Export the key again if permitted by the key policy. */
6654 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006655 {
Ronald Cron5425a212020-08-04 14:58:35 +02006656 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006657 second_export, export_size,
6658 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006659 ASSERT_COMPARE( first_export, first_exported_length,
6660 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006661 }
6662
6663 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006664 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006665 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006666
6667exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006668 /*
6669 * Key attributes may have been returned by psa_get_key_attributes()
6670 * thus reset them as required.
6671 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006672 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006673
Darryl Greend49a4992018-06-18 17:27:26 +01006674 mbedtls_free( first_export );
6675 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006676 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006677 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006678 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006679 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006680}
6681/* END_CASE */