blob: b99be90ff80513f3b2cf7781f35318e472f9bbeb [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100267/*!
268 * \brief Internal Function for AEAD multipart tests.
269 *
270 * \param key_type_arg Type of key passed in
271 * \param key_data The encryption / decryption key data
272 * \param alg_arg The type of algorithm used
273 * \param nonce Nonce data
274 * \param additional_data Additional data
275 * \param ad_part_len If not -1, the length of chunks to
276 * feed additional data in to be encrypted /
277 * decrypted. If -1, no chunking.
278 * \param input_data Data to encrypt / decrypt
279 * \param data_part_len If not -1, the length of chunks to feed the
280 * data in to be encrypted / decrypted. If -1,
281 * no chunking
282 * \param do_set_lengths If non-zero, then set lengths prior to
283 * calling encryption / decryption.
284 * \param expected_output Expected output
Paul Elliott41ffae12021-07-22 21:52:01 +0100285 * \param expect_valid_signature If non zero, we expect the signature to be
286 * valid
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100287 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100288 * \param do_zero_parts If non-zero, interleave zero length chunks
289 * with normal length chunks
290 * \param swap_set_functions If non-zero, swap the order of set lengths
291 * and set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100292 *
293 * \return int Zero on failure, non-zero on success.
294 *
295 */
296static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
297 int alg_arg,
298 data_t *nonce,
299 data_t *additional_data,
300 int ad_part_len,
301 data_t *input_data,
302 int data_part_len,
303 int do_set_lengths,
304 data_t *expected_output,
305 int expect_valid_signature,
Paul Elliott329d5382021-07-22 17:10:45 +0100306 int is_encrypt,
Paul Elliottebf91632021-07-22 17:54:42 +0100307 int do_zero_parts,
308 int swap_set_functions )
Paul Elliottd3f82412021-06-16 16:52:21 +0100309{
310 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
311 psa_key_type_t key_type = key_type_arg;
312 psa_algorithm_t alg = alg_arg;
313 psa_aead_operation_t operation;
314 unsigned char *output_data = NULL;
315 unsigned char *part_data = NULL;
316 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100318 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100319 size_t output_size = 0;
320 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100321 size_t output_length = 0;
322 size_t key_bits = 0;
323 size_t tag_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100324 uint32_t part_offset = 0;
325 size_t part_length = 0;
326 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 size_t tag_size = 0;
328 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
330 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
331
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100332 int test_ok = 0;
Paul Elliott329d5382021-07-22 17:10:45 +0100333 uint32_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100334
Paul Elliottd3f82412021-06-16 16:52:21 +0100335 PSA_ASSERT( psa_crypto_init( ) );
336
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100337 if( is_encrypt )
338 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
339 else
340 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
341
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 psa_set_key_algorithm( &attributes, alg );
343 psa_set_key_type( &attributes, key_type );
344
345 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
346 &key ) );
347
348 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
349 key_bits = psa_get_key_bits( &attributes );
350
351 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
352
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100353 if( is_encrypt )
354 {
355 /* Tag gets written at end of buffer. */
356 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
357 ( input_data->len +
358 tag_length ) );
359 data_true_size = input_data->len;
360 }
361 else
362 {
363 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
364 ( input_data->len -
365 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100366
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100367 /* Do not want to attempt to decrypt tag. */
368 data_true_size = input_data->len - tag_length;
369 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100370
371 ASSERT_ALLOC( output_data, output_size );
372
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100373 if( is_encrypt )
374 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100375 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
376 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100377 }
378 else
379 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100380 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
381 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100382 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100383
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100384 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100385
386 operation = psa_aead_operation_init( );
387
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100388
389 if( is_encrypt )
390 status = psa_aead_encrypt_setup( &operation, key, alg );
391 else
392 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 /* If the operation is not supported, just skip and not fail in case the
395 * encryption involves a common limitation of cryptography hardwares and
396 * an alternative implementation. */
397 if( status == PSA_ERROR_NOT_SUPPORTED )
398 {
399 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
400 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
401 }
402
403 PSA_ASSERT( status );
404
Paul Elliottebf91632021-07-22 17:54:42 +0100405 if( swap_set_functions )
Paul Elliottd3f82412021-06-16 16:52:21 +0100406 {
Paul Elliottebf91632021-07-22 17:54:42 +0100407 if( do_set_lengths )
408 {
409 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
410 data_true_size ) );
411 }
412
413 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
414 }
415 else
416 {
417 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
418
419 if( do_set_lengths )
420 {
421 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
422 data_true_size ) );
423 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100424 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100425
426 if( ad_part_len != -1 )
427 {
428 /* Pass additional data in parts */
429 part_offset = 0;
430
431 while( part_offset < additional_data->len )
432 {
Paul Elliott329d5382021-07-22 17:10:45 +0100433 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100434 {
Paul Elliott329d5382021-07-22 17:10:45 +0100435 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100436 }
437 else
438 {
Paul Elliott329d5382021-07-22 17:10:45 +0100439 if( additional_data->len - part_offset <
440 ( uint32_t ) ad_part_len )
441 {
442 part_length = additional_data->len - part_offset;
443 }
444 else
445 {
446 part_length = ad_part_len;
447 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100448 }
449
450 PSA_ASSERT( psa_aead_update_ad( &operation,
451 additional_data->x + part_offset,
452 part_length ) );
453
454 part_offset += part_length;
455 }
456 }
457 else
458 {
459 /* Pass additional data in one go. */
460 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
461 additional_data->len ) );
462 }
463
464 if( data_part_len != -1 )
465 {
466 /* Pass data in parts */
467 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100468 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100469
470 ASSERT_ALLOC( part_data, part_data_size );
471
472 part_offset = 0;
473
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100474 while( part_offset < data_true_size )
Paul Elliottd3f82412021-06-16 16:52:21 +0100475 {
Paul Elliott329d5382021-07-22 17:10:45 +0100476 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
Paul Elliott329d5382021-07-22 17:10:45 +0100478 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100479 }
480 else
481 {
Paul Elliott329d5382021-07-22 17:10:45 +0100482 if( ( data_true_size - part_offset ) < ( uint32_t ) data_part_len )
483 {
484 part_length = ( data_true_size - part_offset );
485 }
486 else
487 {
488 part_length = data_part_len;
489 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 }
491
492 PSA_ASSERT( psa_aead_update( &operation,
493 ( input_data->x + part_offset ),
494 part_length, part_data,
495 part_data_size,
496 &output_part_length ) );
497
498 if( output_data && output_part_length )
499 {
500 memcpy( ( output_data + part_offset ), part_data,
501 output_part_length );
502 }
503
504 part_offset += part_length;
505 output_length += output_part_length;
506 }
507 }
508 else
509 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100510 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100512 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 output_size, &output_length ) );
514 }
515
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100516 if( is_encrypt )
517 PSA_ASSERT( psa_aead_finish( &operation, final_data,
518 final_output_size,
519 &output_part_length,
520 tag_buffer, tag_length,
521 &tag_size ) );
522 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100523 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100524 status = psa_aead_verify( &operation, final_data,
525 final_output_size,
526 &output_part_length,
527 ( input_data->x + data_true_size ),
528 tag_length );
529
530 if( status != PSA_SUCCESS )
531 {
532 if( !expect_valid_signature )
533 {
534 /* Expected failure. */
535 test_ok = 1;
536 goto exit;
537 }
538 else
539 PSA_ASSERT( status );
540 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100541 }
542
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100543 if( output_data && output_part_length )
544 memcpy( ( output_data + output_length ), final_data,
545 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100546
547 output_length += output_part_length;
548
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100549
550 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
551 * should be exact.*/
552 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100553 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100554 TEST_EQUAL( tag_length, tag_size );
555
556 if( output_data && tag_length )
557 memcpy( ( output_data + output_length ), tag_buffer,
558 tag_length );
559
560 output_length += tag_length;
561
562 TEST_EQUAL( output_length,
563 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
564 input_data->len ) );
565 TEST_ASSERT( output_length <=
566 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
567 }
568 else
569 {
570 TEST_EQUAL( output_length,
571 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
572 input_data->len ) );
573 TEST_ASSERT( output_length <=
574 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100575 }
576
Paul Elliottd3f82412021-06-16 16:52:21 +0100577
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100578 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100579 output_data, output_length );
580
Paul Elliottd3f82412021-06-16 16:52:21 +0100581
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100582 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100583
584exit:
585 psa_destroy_key( key );
586 psa_aead_abort( &operation );
587 mbedtls_free( output_data );
588 mbedtls_free( part_data );
589 mbedtls_free( final_data );
590 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100591
592 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100593}
594
Gilles Peskinee59236f2018-01-27 23:32:46 +0100595/* END_HEADER */
596
597/* BEGIN_DEPENDENCIES
598 * depends_on:MBEDTLS_PSA_CRYPTO_C
599 * END_DEPENDENCIES
600 */
601
602/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200603void static_checks( )
604{
605 size_t max_truncated_mac_size =
606 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
607
608 /* Check that the length for a truncated MAC always fits in the algorithm
609 * encoding. The shifted mask is the maximum truncated value. The
610 * untruncated algorithm may be one byte larger. */
611 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
612}
613/* END_CASE */
614
615/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200616void import_with_policy( int type_arg,
617 int usage_arg, int alg_arg,
618 int expected_status_arg )
619{
620 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
621 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200622 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200623 psa_key_type_t type = type_arg;
624 psa_key_usage_t usage = usage_arg;
625 psa_algorithm_t alg = alg_arg;
626 psa_status_t expected_status = expected_status_arg;
627 const uint8_t key_material[16] = {0};
628 psa_status_t status;
629
630 PSA_ASSERT( psa_crypto_init( ) );
631
632 psa_set_key_type( &attributes, type );
633 psa_set_key_usage_flags( &attributes, usage );
634 psa_set_key_algorithm( &attributes, alg );
635
636 status = psa_import_key( &attributes,
637 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200638 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200639 TEST_EQUAL( status, expected_status );
640 if( status != PSA_SUCCESS )
641 goto exit;
642
Ronald Cron5425a212020-08-04 14:58:35 +0200643 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200644 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
645 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
646 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200647 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200648
Ronald Cron5425a212020-08-04 14:58:35 +0200649 PSA_ASSERT( psa_destroy_key( key ) );
650 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200651
652exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100653 /*
654 * Key attributes may have been returned by psa_get_key_attributes()
655 * thus reset them as required.
656 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200657 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100658
659 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200660 PSA_DONE( );
661}
662/* END_CASE */
663
664/* BEGIN_CASE */
665void import_with_data( data_t *data, int type_arg,
666 int attr_bits_arg,
667 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200668{
669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
670 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200671 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200672 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200673 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200674 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
Gilles Peskine8817f612018-12-18 00:18:46 +0100677 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678
Gilles Peskine4747d192019-04-17 15:05:45 +0200679 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200680 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200681
Ronald Cron5425a212020-08-04 14:58:35 +0200682 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100683 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200684 if( status != PSA_SUCCESS )
685 goto exit;
686
Ronald Cron5425a212020-08-04 14:58:35 +0200687 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200688 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200689 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200690 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200691 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200692
Ronald Cron5425a212020-08-04 14:58:35 +0200693 PSA_ASSERT( psa_destroy_key( key ) );
694 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100695
696exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100697 /*
698 * Key attributes may have been returned by psa_get_key_attributes()
699 * thus reset them as required.
700 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200701 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100702
703 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200704 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100705}
706/* END_CASE */
707
708/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200709void import_large_key( int type_arg, int byte_size_arg,
710 int expected_status_arg )
711{
712 psa_key_type_t type = type_arg;
713 size_t byte_size = byte_size_arg;
714 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
715 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200717 psa_status_t status;
718 uint8_t *buffer = NULL;
719 size_t buffer_size = byte_size + 1;
720 size_t n;
721
Steven Cooreman69967ce2021-01-18 18:01:08 +0100722 /* Skip the test case if the target running the test cannot
723 * accomodate large keys due to heap size constraints */
724 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200725 memset( buffer, 'K', byte_size );
726
727 PSA_ASSERT( psa_crypto_init( ) );
728
729 /* Try importing the key */
730 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
731 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200732 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100733 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200734 TEST_EQUAL( status, expected_status );
735
736 if( status == PSA_SUCCESS )
737 {
Ronald Cron5425a212020-08-04 14:58:35 +0200738 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200739 TEST_EQUAL( psa_get_key_type( &attributes ), type );
740 TEST_EQUAL( psa_get_key_bits( &attributes ),
741 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200742 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200743 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200744 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200745 for( n = 0; n < byte_size; n++ )
746 TEST_EQUAL( buffer[n], 'K' );
747 for( n = byte_size; n < buffer_size; n++ )
748 TEST_EQUAL( buffer[n], 0 );
749 }
750
751exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100752 /*
753 * Key attributes may have been returned by psa_get_key_attributes()
754 * thus reset them as required.
755 */
756 psa_reset_key_attributes( &attributes );
757
Ronald Cron5425a212020-08-04 14:58:35 +0200758 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200759 PSA_DONE( );
760 mbedtls_free( buffer );
761}
762/* END_CASE */
763
764/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200765void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
766{
Ronald Cron5425a212020-08-04 14:58:35 +0200767 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200768 size_t bits = bits_arg;
769 psa_status_t expected_status = expected_status_arg;
770 psa_status_t status;
771 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200772 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200773 size_t buffer_size = /* Slight overapproximations */
774 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200775 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200776 unsigned char *p;
777 int ret;
778 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200779 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200780
Gilles Peskine8817f612018-12-18 00:18:46 +0100781 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200782 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200783
784 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
785 bits, keypair ) ) >= 0 );
786 length = ret;
787
788 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200789 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200790 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100791 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200792
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200793 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200794 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200795
796exit:
797 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200798 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200799}
800/* END_CASE */
801
802/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300803void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300804 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200805 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100806 int expected_bits,
807 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200808 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100809 int canonical_input )
810{
Ronald Cron5425a212020-08-04 14:58:35 +0200811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100812 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200813 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200814 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 unsigned char *exported = NULL;
817 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100819 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200822 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823
Moran Pekercb088e72018-07-17 17:36:59 +0300824 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200825 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200827 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829
Gilles Peskine4747d192019-04-17 15:05:45 +0200830 psa_set_key_usage_flags( &attributes, usage_arg );
831 psa_set_key_algorithm( &attributes, alg );
832 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700833
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100834 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200835 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100836
837 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200838 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200839 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
840 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200841 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100842
843 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200844 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100845 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100846
847 /* The exported length must be set by psa_export_key() to a value between 0
848 * and export_size. On errors, the exported length must be 0. */
849 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
850 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
851 TEST_ASSERT( exported_length <= export_size );
852
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200853 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200854 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200856 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100857 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200859 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100860
Gilles Peskineea38a922021-02-13 00:05:16 +0100861 /* Run sanity checks on the exported key. For non-canonical inputs,
862 * this validates the canonical representations. For canonical inputs,
863 * this doesn't directly validate the implementation, but it still helps
864 * by cross-validating the test data with the sanity check code. */
865 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200866 goto exit;
867
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100868 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200869 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100870 else
871 {
Ronald Cron5425a212020-08-04 14:58:35 +0200872 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200873 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200874 &key2 ) );
875 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100876 reexported,
877 export_size,
878 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200879 ASSERT_COMPARE( exported, exported_length,
880 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200881 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100882 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100883 TEST_ASSERT( exported_length <=
884 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
885 psa_get_key_bits( &got_attributes ) ) );
886 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887
888destroy:
889 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200890 PSA_ASSERT( psa_destroy_key( key ) );
891 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100892
893exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100894 /*
895 * Key attributes may have been returned by psa_get_key_attributes()
896 * thus reset them as required.
897 */
898 psa_reset_key_attributes( &got_attributes );
899
itayzafrir3e02b3b2018-06-12 17:06:52 +0300900 mbedtls_free( exported );
901 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200902 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100903}
904/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100905
Moran Pekerf709f4a2018-06-06 17:26:04 +0300906/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300907void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200908 int type_arg,
909 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100910 int export_size_delta,
911 int expected_export_status_arg,
912 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300913{
Ronald Cron5425a212020-08-04 14:58:35 +0200914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300915 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200916 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200917 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300919 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100920 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100921 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200922 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300923
Gilles Peskine8817f612018-12-18 00:18:46 +0100924 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300925
Gilles Peskine4747d192019-04-17 15:05:45 +0200926 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
927 psa_set_key_algorithm( &attributes, alg );
928 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929
930 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200931 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300932
Gilles Peskine49c25912018-10-29 15:15:31 +0100933 /* Export the public key */
934 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200935 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200936 exported, export_size,
937 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100938 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100939 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100940 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200941 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100942 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200943 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200944 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100945 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100946 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100947 TEST_ASSERT( expected_public_key->len <=
948 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
949 TEST_ASSERT( expected_public_key->len <=
950 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100951 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
952 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100953 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300954
955exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100956 /*
957 * Key attributes may have been returned by psa_get_key_attributes()
958 * thus reset them as required.
959 */
960 psa_reset_key_attributes( &attributes );
961
itayzafrir3e02b3b2018-06-12 17:06:52 +0300962 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200963 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200964 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300965}
966/* END_CASE */
967
Gilles Peskine20035e32018-02-03 22:44:14 +0100968/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200969void import_and_exercise_key( data_t *data,
970 int type_arg,
971 int bits_arg,
972 int alg_arg )
973{
Ronald Cron5425a212020-08-04 14:58:35 +0200974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975 psa_key_type_t type = type_arg;
976 size_t bits = bits_arg;
977 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100978 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200980 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200981
Gilles Peskine8817f612018-12-18 00:18:46 +0100982 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200983
Gilles Peskine4747d192019-04-17 15:05:45 +0200984 psa_set_key_usage_flags( &attributes, usage );
985 psa_set_key_algorithm( &attributes, alg );
986 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200987
988 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200989 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200990
991 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200992 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200993 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
994 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200995
996 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100997 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200998 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200999
Ronald Cron5425a212020-08-04 14:58:35 +02001000 PSA_ASSERT( psa_destroy_key( key ) );
1001 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001002
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001003exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001004 /*
1005 * Key attributes may have been returned by psa_get_key_attributes()
1006 * thus reset them as required.
1007 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001008 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001009
1010 psa_reset_key_attributes( &attributes );
1011 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001012 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001013}
1014/* END_CASE */
1015
1016/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001017void effective_key_attributes( int type_arg, int expected_type_arg,
1018 int bits_arg, int expected_bits_arg,
1019 int usage_arg, int expected_usage_arg,
1020 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001021{
Ronald Cron5425a212020-08-04 14:58:35 +02001022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001023 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001024 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001025 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001026 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001027 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001028 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001029 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001030 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001031 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001032
Gilles Peskine8817f612018-12-18 00:18:46 +01001033 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001034
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001035 psa_set_key_usage_flags( &attributes, usage );
1036 psa_set_key_algorithm( &attributes, alg );
1037 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001038 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001039
Ronald Cron5425a212020-08-04 14:58:35 +02001040 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001041 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001042
Ronald Cron5425a212020-08-04 14:58:35 +02001043 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001044 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1045 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1046 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1047 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048
1049exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001050 /*
1051 * Key attributes may have been returned by psa_get_key_attributes()
1052 * thus reset them as required.
1053 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001054 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001055
1056 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001057 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001058}
1059/* END_CASE */
1060
1061/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001062void check_key_policy( int type_arg, int bits_arg,
1063 int usage_arg, int alg_arg )
1064{
1065 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1066 usage_arg, usage_arg, alg_arg, alg_arg );
1067 goto exit;
1068}
1069/* END_CASE */
1070
1071/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001072void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001073{
1074 /* Test each valid way of initializing the object, except for `= {0}`, as
1075 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1076 * though it's OK by the C standard. We could test for this, but we'd need
1077 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001078 psa_key_attributes_t func = psa_key_attributes_init( );
1079 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1080 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001081
1082 memset( &zero, 0, sizeof( zero ) );
1083
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001084 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1085 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1086 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001087
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001088 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1089 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1090 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1091
1092 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1093 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1094 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1095
1096 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1097 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1098 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1099
1100 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1101 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1102 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001103}
1104/* END_CASE */
1105
1106/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001107void mac_key_policy( int policy_usage,
1108 int policy_alg,
1109 int key_type,
1110 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001111 int exercise_alg,
1112 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001113{
Ronald Cron5425a212020-08-04 14:58:35 +02001114 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001116 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001117 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001118 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001119 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001120
Gilles Peskine8817f612018-12-18 00:18:46 +01001121 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001122
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001123 psa_set_key_usage_flags( &attributes, policy_usage );
1124 psa_set_key_algorithm( &attributes, policy_alg );
1125 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001126
Gilles Peskine049c7532019-05-15 20:22:09 +02001127 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001128 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001129
Ronald Cron5425a212020-08-04 14:58:35 +02001130 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001131 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001132 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001133 else
1134 TEST_EQUAL( status, expected_status );
1135
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001136 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001137
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001138 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001139 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001140 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001141 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001142 else
1143 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001144
1145exit:
1146 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001147 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001148 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001149}
1150/* END_CASE */
1151
1152/* BEGIN_CASE */
1153void cipher_key_policy( int policy_usage,
1154 int policy_alg,
1155 int key_type,
1156 data_t *key_data,
1157 int exercise_alg )
1158{
Ronald Cron5425a212020-08-04 14:58:35 +02001159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001161 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001162 psa_status_t status;
1163
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001165
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001166 psa_set_key_usage_flags( &attributes, policy_usage );
1167 psa_set_key_algorithm( &attributes, policy_alg );
1168 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169
Gilles Peskine049c7532019-05-15 20:22:09 +02001170 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001171 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
Ronald Cron5425a212020-08-04 14:58:35 +02001173 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001174 if( policy_alg == exercise_alg &&
1175 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001176 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001178 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001179 psa_cipher_abort( &operation );
1180
Ronald Cron5425a212020-08-04 14:58:35 +02001181 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182 if( policy_alg == exercise_alg &&
1183 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001184 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001185 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001186 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001187
1188exit:
1189 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001190 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001191 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001192}
1193/* END_CASE */
1194
1195/* BEGIN_CASE */
1196void aead_key_policy( int policy_usage,
1197 int policy_alg,
1198 int key_type,
1199 data_t *key_data,
1200 int nonce_length_arg,
1201 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001202 int exercise_alg,
1203 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204{
Ronald Cron5425a212020-08-04 14:58:35 +02001205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001208 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209 unsigned char nonce[16] = {0};
1210 size_t nonce_length = nonce_length_arg;
1211 unsigned char tag[16];
1212 size_t tag_length = tag_length_arg;
1213 size_t output_length;
1214
1215 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1216 TEST_ASSERT( tag_length <= sizeof( tag ) );
1217
Gilles Peskine8817f612018-12-18 00:18:46 +01001218 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001220 psa_set_key_usage_flags( &attributes, policy_usage );
1221 psa_set_key_algorithm( &attributes, policy_alg );
1222 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001223
Gilles Peskine049c7532019-05-15 20:22:09 +02001224 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001225 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001226
Ronald Cron5425a212020-08-04 14:58:35 +02001227 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001228 nonce, nonce_length,
1229 NULL, 0,
1230 NULL, 0,
1231 tag, tag_length,
1232 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001233 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1234 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001235 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001236 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001237
1238 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001239 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001240 nonce, nonce_length,
1241 NULL, 0,
1242 tag, tag_length,
1243 NULL, 0,
1244 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001245 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1246 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1247 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001248 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001249 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001250 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001251
1252exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001253 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001254 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001255}
1256/* END_CASE */
1257
1258/* BEGIN_CASE */
1259void asymmetric_encryption_key_policy( int policy_usage,
1260 int policy_alg,
1261 int key_type,
1262 data_t *key_data,
1263 int exercise_alg )
1264{
Ronald Cron5425a212020-08-04 14:58:35 +02001265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001266 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001267 psa_status_t status;
1268 size_t key_bits;
1269 size_t buffer_length;
1270 unsigned char *buffer = NULL;
1271 size_t output_length;
1272
Gilles Peskine8817f612018-12-18 00:18:46 +01001273 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001274
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001275 psa_set_key_usage_flags( &attributes, policy_usage );
1276 psa_set_key_algorithm( &attributes, policy_alg );
1277 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001278
Gilles Peskine049c7532019-05-15 20:22:09 +02001279 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001280 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001281
Ronald Cron5425a212020-08-04 14:58:35 +02001282 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001283 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001284 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1285 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001286 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287
Ronald Cron5425a212020-08-04 14:58:35 +02001288 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001289 NULL, 0,
1290 NULL, 0,
1291 buffer, buffer_length,
1292 &output_length );
1293 if( policy_alg == exercise_alg &&
1294 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001295 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001297 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001298
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001299 if( buffer_length != 0 )
1300 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001301 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001302 buffer, buffer_length,
1303 NULL, 0,
1304 buffer, buffer_length,
1305 &output_length );
1306 if( policy_alg == exercise_alg &&
1307 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001308 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001309 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001310 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001311
1312exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001313 /*
1314 * Key attributes may have been returned by psa_get_key_attributes()
1315 * thus reset them as required.
1316 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001317 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001318
1319 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001320 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001321 mbedtls_free( buffer );
1322}
1323/* END_CASE */
1324
1325/* BEGIN_CASE */
1326void asymmetric_signature_key_policy( int policy_usage,
1327 int policy_alg,
1328 int key_type,
1329 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001330 int exercise_alg,
1331 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332{
Ronald Cron5425a212020-08-04 14:58:35 +02001333 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001335 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001336 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1337 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1338 * compatible with the policy and `payload_length_arg` is supposed to be
1339 * a valid input length to sign. If `payload_length_arg <= 0`,
1340 * `exercise_alg` is supposed to be forbidden by the policy. */
1341 int compatible_alg = payload_length_arg > 0;
1342 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001343 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001344 size_t signature_length;
1345
Gilles Peskine8817f612018-12-18 00:18:46 +01001346 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001347
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001348 psa_set_key_usage_flags( &attributes, policy_usage );
1349 psa_set_key_algorithm( &attributes, policy_alg );
1350 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001351
Gilles Peskine049c7532019-05-15 20:22:09 +02001352 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001353 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354
Ronald Cron5425a212020-08-04 14:58:35 +02001355 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001356 payload, payload_length,
1357 signature, sizeof( signature ),
1358 &signature_length );
1359 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001360 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001361 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001362 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001363
1364 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001365 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001366 payload, payload_length,
1367 signature, sizeof( signature ) );
1368 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001369 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001370 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001371 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001372
1373exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001374 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001375 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001376}
1377/* END_CASE */
1378
Janos Follathba3fab92019-06-11 14:50:16 +01001379/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001380void derive_key_policy( int policy_usage,
1381 int policy_alg,
1382 int key_type,
1383 data_t *key_data,
1384 int exercise_alg )
1385{
Ronald Cron5425a212020-08-04 14:58:35 +02001386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001387 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001388 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001389 psa_status_t status;
1390
Gilles Peskine8817f612018-12-18 00:18:46 +01001391 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001392
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001393 psa_set_key_usage_flags( &attributes, policy_usage );
1394 psa_set_key_algorithm( &attributes, policy_alg );
1395 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001396
Gilles Peskine049c7532019-05-15 20:22:09 +02001397 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001398 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001399
Janos Follathba3fab92019-06-11 14:50:16 +01001400 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1401
1402 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1403 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001404 {
Janos Follathba3fab92019-06-11 14:50:16 +01001405 PSA_ASSERT( psa_key_derivation_input_bytes(
1406 &operation,
1407 PSA_KEY_DERIVATION_INPUT_SEED,
1408 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001409 }
Janos Follathba3fab92019-06-11 14:50:16 +01001410
1411 status = psa_key_derivation_input_key( &operation,
1412 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001413 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001414
Gilles Peskineea0fb492018-07-12 17:17:20 +02001415 if( policy_alg == exercise_alg &&
1416 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001417 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001418 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001419 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001420
1421exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001422 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001423 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001424 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001425}
1426/* END_CASE */
1427
1428/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001429void agreement_key_policy( int policy_usage,
1430 int policy_alg,
1431 int key_type_arg,
1432 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001433 int exercise_alg,
1434 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001435{
Ronald Cron5425a212020-08-04 14:58:35 +02001436 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001438 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001439 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001440 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001441 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001442
Gilles Peskine8817f612018-12-18 00:18:46 +01001443 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001444
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001445 psa_set_key_usage_flags( &attributes, policy_usage );
1446 psa_set_key_algorithm( &attributes, policy_alg );
1447 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001448
Gilles Peskine049c7532019-05-15 20:22:09 +02001449 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001450 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001451
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001452 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001453 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001454
Steven Cooremance48e852020-10-05 16:02:45 +02001455 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001456
1457exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001458 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001459 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001460 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001461}
1462/* END_CASE */
1463
1464/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001465void key_policy_alg2( int key_type_arg, data_t *key_data,
1466 int usage_arg, int alg_arg, int alg2_arg )
1467{
Ronald Cron5425a212020-08-04 14:58:35 +02001468 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001469 psa_key_type_t key_type = key_type_arg;
1470 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1471 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1472 psa_key_usage_t usage = usage_arg;
1473 psa_algorithm_t alg = alg_arg;
1474 psa_algorithm_t alg2 = alg2_arg;
1475
1476 PSA_ASSERT( psa_crypto_init( ) );
1477
1478 psa_set_key_usage_flags( &attributes, usage );
1479 psa_set_key_algorithm( &attributes, alg );
1480 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1481 psa_set_key_type( &attributes, key_type );
1482 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001483 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001484
Ronald Cron5425a212020-08-04 14:58:35 +02001485 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001486 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1487 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1488 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1489
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001490 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001491 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001492 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001493 goto exit;
1494
1495exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001496 /*
1497 * Key attributes may have been returned by psa_get_key_attributes()
1498 * thus reset them as required.
1499 */
1500 psa_reset_key_attributes( &got_attributes );
1501
Ronald Cron5425a212020-08-04 14:58:35 +02001502 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001503 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001504}
1505/* END_CASE */
1506
1507/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001508void raw_agreement_key_policy( int policy_usage,
1509 int policy_alg,
1510 int key_type_arg,
1511 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001512 int exercise_alg,
1513 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001514{
Ronald Cron5425a212020-08-04 14:58:35 +02001515 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001518 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001519 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001520 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001521
1522 PSA_ASSERT( psa_crypto_init( ) );
1523
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001524 psa_set_key_usage_flags( &attributes, policy_usage );
1525 psa_set_key_algorithm( &attributes, policy_alg );
1526 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001527
Gilles Peskine049c7532019-05-15 20:22:09 +02001528 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001529 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001530
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001531 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001532
Steven Cooremance48e852020-10-05 16:02:45 +02001533 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001534
1535exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001536 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001537 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001538 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001539}
1540/* END_CASE */
1541
1542/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001543void copy_success( int source_usage_arg,
1544 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001545 int type_arg, data_t *material,
1546 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001547 int target_usage_arg,
1548 int target_alg_arg, int target_alg2_arg,
1549 int expected_usage_arg,
1550 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001551{
Gilles Peskineca25db92019-04-19 11:43:08 +02001552 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1553 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001554 psa_key_usage_t expected_usage = expected_usage_arg;
1555 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001556 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001557 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1558 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001559 uint8_t *export_buffer = NULL;
1560
Gilles Peskine57ab7212019-01-28 13:03:09 +01001561 PSA_ASSERT( psa_crypto_init( ) );
1562
Gilles Peskineca25db92019-04-19 11:43:08 +02001563 /* Prepare the source key. */
1564 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1565 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001566 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001567 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001568 PSA_ASSERT( psa_import_key( &source_attributes,
1569 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001570 &source_key ) );
1571 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001572
Gilles Peskineca25db92019-04-19 11:43:08 +02001573 /* Prepare the target attributes. */
1574 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001575 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001576 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001577 /* Set volatile lifetime to reset the key identifier to 0. */
1578 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1579 }
1580
Gilles Peskineca25db92019-04-19 11:43:08 +02001581 if( target_usage_arg != -1 )
1582 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1583 if( target_alg_arg != -1 )
1584 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001585 if( target_alg2_arg != -1 )
1586 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001587
1588 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001589 PSA_ASSERT( psa_copy_key( source_key,
1590 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001591
1592 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001593 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001594
1595 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001596 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001597 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1598 psa_get_key_type( &target_attributes ) );
1599 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1600 psa_get_key_bits( &target_attributes ) );
1601 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1602 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001603 TEST_EQUAL( expected_alg2,
1604 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001605 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1606 {
1607 size_t length;
1608 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001609 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001610 material->len, &length ) );
1611 ASSERT_COMPARE( material->x, material->len,
1612 export_buffer, length );
1613 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001614
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001615 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001616 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001617 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001618 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001619
Ronald Cron5425a212020-08-04 14:58:35 +02001620 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001621
1622exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001623 /*
1624 * Source and target key attributes may have been returned by
1625 * psa_get_key_attributes() thus reset them as required.
1626 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001627 psa_reset_key_attributes( &source_attributes );
1628 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001629
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001630 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001631 mbedtls_free( export_buffer );
1632}
1633/* END_CASE */
1634
1635/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001636void copy_fail( int source_usage_arg,
1637 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001638 int type_arg, data_t *material,
1639 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001640 int target_usage_arg,
1641 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001642 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001643 int expected_status_arg )
1644{
1645 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1646 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001647 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1648 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001649 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001650
1651 PSA_ASSERT( psa_crypto_init( ) );
1652
1653 /* Prepare the source key. */
1654 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1655 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001656 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001657 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001658 PSA_ASSERT( psa_import_key( &source_attributes,
1659 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001660 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001661
1662 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001663 psa_set_key_id( &target_attributes, key_id );
1664 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001665 psa_set_key_type( &target_attributes, target_type_arg );
1666 psa_set_key_bits( &target_attributes, target_bits_arg );
1667 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1668 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001669 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001670
1671 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001672 TEST_EQUAL( psa_copy_key( source_key,
1673 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001674 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001675
Ronald Cron5425a212020-08-04 14:58:35 +02001676 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001677
Gilles Peskine4a644642019-05-03 17:14:08 +02001678exit:
1679 psa_reset_key_attributes( &source_attributes );
1680 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001681 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001682}
1683/* END_CASE */
1684
1685/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001686void hash_operation_init( )
1687{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001688 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001689 /* Test each valid way of initializing the object, except for `= {0}`, as
1690 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1691 * though it's OK by the C standard. We could test for this, but we'd need
1692 * to supress the Clang warning for the test. */
1693 psa_hash_operation_t func = psa_hash_operation_init( );
1694 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1695 psa_hash_operation_t zero;
1696
1697 memset( &zero, 0, sizeof( zero ) );
1698
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001699 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001700 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1701 PSA_ERROR_BAD_STATE );
1702 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1703 PSA_ERROR_BAD_STATE );
1704 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1705 PSA_ERROR_BAD_STATE );
1706
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001707 /* A default hash operation should be abortable without error. */
1708 PSA_ASSERT( psa_hash_abort( &func ) );
1709 PSA_ASSERT( psa_hash_abort( &init ) );
1710 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001711}
1712/* END_CASE */
1713
1714/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001715void hash_setup( int alg_arg,
1716 int expected_status_arg )
1717{
1718 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001719 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001720 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001721 psa_status_t status;
1722
Gilles Peskine8817f612018-12-18 00:18:46 +01001723 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001724
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001725 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001726 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001727
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001728 /* Whether setup succeeded or failed, abort must succeed. */
1729 PSA_ASSERT( psa_hash_abort( &operation ) );
1730
1731 /* If setup failed, reproduce the failure, so as to
1732 * test the resulting state of the operation object. */
1733 if( status != PSA_SUCCESS )
1734 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1735
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001736 /* Now the operation object should be reusable. */
1737#if defined(KNOWN_SUPPORTED_HASH_ALG)
1738 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1739 PSA_ASSERT( psa_hash_abort( &operation ) );
1740#endif
1741
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001742exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001743 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001744}
1745/* END_CASE */
1746
1747/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001748void hash_compute_fail( int alg_arg, data_t *input,
1749 int output_size_arg, int expected_status_arg )
1750{
1751 psa_algorithm_t alg = alg_arg;
1752 uint8_t *output = NULL;
1753 size_t output_size = output_size_arg;
1754 size_t output_length = INVALID_EXPORT_LENGTH;
1755 psa_status_t expected_status = expected_status_arg;
1756 psa_status_t status;
1757
1758 ASSERT_ALLOC( output, output_size );
1759
1760 PSA_ASSERT( psa_crypto_init( ) );
1761
1762 status = psa_hash_compute( alg, input->x, input->len,
1763 output, output_size, &output_length );
1764 TEST_EQUAL( status, expected_status );
1765 TEST_ASSERT( output_length <= output_size );
1766
1767exit:
1768 mbedtls_free( output );
1769 PSA_DONE( );
1770}
1771/* END_CASE */
1772
1773/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001774void hash_compare_fail( int alg_arg, data_t *input,
1775 data_t *reference_hash,
1776 int expected_status_arg )
1777{
1778 psa_algorithm_t alg = alg_arg;
1779 psa_status_t expected_status = expected_status_arg;
1780 psa_status_t status;
1781
1782 PSA_ASSERT( psa_crypto_init( ) );
1783
1784 status = psa_hash_compare( alg, input->x, input->len,
1785 reference_hash->x, reference_hash->len );
1786 TEST_EQUAL( status, expected_status );
1787
1788exit:
1789 PSA_DONE( );
1790}
1791/* END_CASE */
1792
1793/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001794void hash_compute_compare( int alg_arg, data_t *input,
1795 data_t *expected_output )
1796{
1797 psa_algorithm_t alg = alg_arg;
1798 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1799 size_t output_length = INVALID_EXPORT_LENGTH;
1800 size_t i;
1801
1802 PSA_ASSERT( psa_crypto_init( ) );
1803
1804 /* Compute with tight buffer */
1805 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001806 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001807 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001808 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001809 ASSERT_COMPARE( output, output_length,
1810 expected_output->x, expected_output->len );
1811
1812 /* Compute with larger buffer */
1813 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1814 output, sizeof( output ),
1815 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001816 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001817 ASSERT_COMPARE( output, output_length,
1818 expected_output->x, expected_output->len );
1819
1820 /* Compare with correct hash */
1821 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1822 output, output_length ) );
1823
1824 /* Compare with trailing garbage */
1825 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1826 output, output_length + 1 ),
1827 PSA_ERROR_INVALID_SIGNATURE );
1828
1829 /* Compare with truncated hash */
1830 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1831 output, output_length - 1 ),
1832 PSA_ERROR_INVALID_SIGNATURE );
1833
1834 /* Compare with corrupted value */
1835 for( i = 0; i < output_length; i++ )
1836 {
Chris Jones9634bb12021-01-20 15:56:42 +00001837 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001838 output[i] ^= 1;
1839 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1840 output, output_length ),
1841 PSA_ERROR_INVALID_SIGNATURE );
1842 output[i] ^= 1;
1843 }
1844
1845exit:
1846 PSA_DONE( );
1847}
1848/* END_CASE */
1849
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001850/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001851void hash_bad_order( )
1852{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001853 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001854 unsigned char input[] = "";
1855 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001856 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001857 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1858 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1859 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001860 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001861 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001862 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001863
Gilles Peskine8817f612018-12-18 00:18:46 +01001864 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001865
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001866 /* Call setup twice in a row. */
1867 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1868 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1869 PSA_ERROR_BAD_STATE );
1870 PSA_ASSERT( psa_hash_abort( &operation ) );
1871
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001872 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001873 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001874 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001875 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001876
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001877 /* Call update after finish. */
1878 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1879 PSA_ASSERT( psa_hash_finish( &operation,
1880 hash, sizeof( hash ), &hash_len ) );
1881 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001882 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001883 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001884
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001885 /* Call verify without calling setup beforehand. */
1886 TEST_EQUAL( psa_hash_verify( &operation,
1887 valid_hash, sizeof( valid_hash ) ),
1888 PSA_ERROR_BAD_STATE );
1889 PSA_ASSERT( psa_hash_abort( &operation ) );
1890
1891 /* Call verify after finish. */
1892 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1893 PSA_ASSERT( psa_hash_finish( &operation,
1894 hash, sizeof( hash ), &hash_len ) );
1895 TEST_EQUAL( psa_hash_verify( &operation,
1896 valid_hash, sizeof( valid_hash ) ),
1897 PSA_ERROR_BAD_STATE );
1898 PSA_ASSERT( psa_hash_abort( &operation ) );
1899
1900 /* Call verify twice in a row. */
1901 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1902 PSA_ASSERT( psa_hash_verify( &operation,
1903 valid_hash, sizeof( valid_hash ) ) );
1904 TEST_EQUAL( psa_hash_verify( &operation,
1905 valid_hash, sizeof( valid_hash ) ),
1906 PSA_ERROR_BAD_STATE );
1907 PSA_ASSERT( psa_hash_abort( &operation ) );
1908
1909 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001910 TEST_EQUAL( psa_hash_finish( &operation,
1911 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001912 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001913 PSA_ASSERT( psa_hash_abort( &operation ) );
1914
1915 /* Call finish twice in a row. */
1916 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1917 PSA_ASSERT( psa_hash_finish( &operation,
1918 hash, sizeof( hash ), &hash_len ) );
1919 TEST_EQUAL( psa_hash_finish( &operation,
1920 hash, sizeof( hash ), &hash_len ),
1921 PSA_ERROR_BAD_STATE );
1922 PSA_ASSERT( psa_hash_abort( &operation ) );
1923
1924 /* Call finish after calling verify. */
1925 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1926 PSA_ASSERT( psa_hash_verify( &operation,
1927 valid_hash, sizeof( valid_hash ) ) );
1928 TEST_EQUAL( psa_hash_finish( &operation,
1929 hash, sizeof( hash ), &hash_len ),
1930 PSA_ERROR_BAD_STATE );
1931 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001932
1933exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001934 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001935}
1936/* END_CASE */
1937
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001938/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001939void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001940{
1941 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001942 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1943 * appended to it */
1944 unsigned char hash[] = {
1945 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1946 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1947 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001948 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001949 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001950
Gilles Peskine8817f612018-12-18 00:18:46 +01001951 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001952
itayzafrir27e69452018-11-01 14:26:34 +02001953 /* psa_hash_verify with a smaller hash 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, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001956 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001957
itayzafrir27e69452018-11-01 14:26:34 +02001958 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001959 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001960 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001961 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001962
itayzafrir27e69452018-11-01 14:26:34 +02001963 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001964 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001965 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001966 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001967
itayzafrirec93d302018-10-18 18:01:10 +03001968exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001969 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001970}
1971/* END_CASE */
1972
Ronald Cronee414c72021-03-18 18:50:08 +01001973/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001974void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001975{
1976 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001977 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001978 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001979 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001980 size_t hash_len;
1981
Gilles Peskine8817f612018-12-18 00:18:46 +01001982 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001983
itayzafrir58028322018-10-25 10:22:01 +03001984 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001985 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001986 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001987 hash, expected_size - 1, &hash_len ),
1988 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001989
1990exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001991 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001992}
1993/* END_CASE */
1994
Ronald Cronee414c72021-03-18 18:50:08 +01001995/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001996void hash_clone_source_state( )
1997{
1998 psa_algorithm_t alg = PSA_ALG_SHA_256;
1999 unsigned char hash[PSA_HASH_MAX_SIZE];
2000 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2001 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2002 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2003 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2004 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2005 size_t hash_len;
2006
2007 PSA_ASSERT( psa_crypto_init( ) );
2008 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2009
2010 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2011 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2012 PSA_ASSERT( psa_hash_finish( &op_finished,
2013 hash, sizeof( hash ), &hash_len ) );
2014 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2015 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2016
2017 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2018 PSA_ERROR_BAD_STATE );
2019
2020 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2021 PSA_ASSERT( psa_hash_finish( &op_init,
2022 hash, sizeof( hash ), &hash_len ) );
2023 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2024 PSA_ASSERT( psa_hash_finish( &op_finished,
2025 hash, sizeof( hash ), &hash_len ) );
2026 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2027 PSA_ASSERT( psa_hash_finish( &op_aborted,
2028 hash, sizeof( hash ), &hash_len ) );
2029
2030exit:
2031 psa_hash_abort( &op_source );
2032 psa_hash_abort( &op_init );
2033 psa_hash_abort( &op_setup );
2034 psa_hash_abort( &op_finished );
2035 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002036 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002037}
2038/* END_CASE */
2039
Ronald Cronee414c72021-03-18 18:50:08 +01002040/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002041void hash_clone_target_state( )
2042{
2043 psa_algorithm_t alg = PSA_ALG_SHA_256;
2044 unsigned char hash[PSA_HASH_MAX_SIZE];
2045 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2046 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2047 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2048 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2049 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2050 size_t hash_len;
2051
2052 PSA_ASSERT( psa_crypto_init( ) );
2053
2054 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2055 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2056 PSA_ASSERT( psa_hash_finish( &op_finished,
2057 hash, sizeof( hash ), &hash_len ) );
2058 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2059 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2060
2061 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2062 PSA_ASSERT( psa_hash_finish( &op_target,
2063 hash, sizeof( hash ), &hash_len ) );
2064
2065 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2066 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2067 PSA_ERROR_BAD_STATE );
2068 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2069 PSA_ERROR_BAD_STATE );
2070
2071exit:
2072 psa_hash_abort( &op_target );
2073 psa_hash_abort( &op_init );
2074 psa_hash_abort( &op_setup );
2075 psa_hash_abort( &op_finished );
2076 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002077 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002078}
2079/* END_CASE */
2080
itayzafrir58028322018-10-25 10:22:01 +03002081/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002082void mac_operation_init( )
2083{
Jaeden Amero252ef282019-02-15 14:05:35 +00002084 const uint8_t input[1] = { 0 };
2085
Jaeden Amero769ce272019-01-04 11:48:03 +00002086 /* Test each valid way of initializing the object, except for `= {0}`, as
2087 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2088 * though it's OK by the C standard. We could test for this, but we'd need
2089 * to supress the Clang warning for the test. */
2090 psa_mac_operation_t func = psa_mac_operation_init( );
2091 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2092 psa_mac_operation_t zero;
2093
2094 memset( &zero, 0, sizeof( zero ) );
2095
Jaeden Amero252ef282019-02-15 14:05:35 +00002096 /* A freshly-initialized MAC operation should not be usable. */
2097 TEST_EQUAL( psa_mac_update( &func,
2098 input, sizeof( input ) ),
2099 PSA_ERROR_BAD_STATE );
2100 TEST_EQUAL( psa_mac_update( &init,
2101 input, sizeof( input ) ),
2102 PSA_ERROR_BAD_STATE );
2103 TEST_EQUAL( psa_mac_update( &zero,
2104 input, sizeof( input ) ),
2105 PSA_ERROR_BAD_STATE );
2106
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002107 /* A default MAC operation should be abortable without error. */
2108 PSA_ASSERT( psa_mac_abort( &func ) );
2109 PSA_ASSERT( psa_mac_abort( &init ) );
2110 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002111}
2112/* END_CASE */
2113
2114/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002115void mac_setup( int key_type_arg,
2116 data_t *key,
2117 int alg_arg,
2118 int expected_status_arg )
2119{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002120 psa_key_type_t key_type = key_type_arg;
2121 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002122 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002123 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002124 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2125#if defined(KNOWN_SUPPORTED_MAC_ALG)
2126 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2127#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002128
Gilles Peskine8817f612018-12-18 00:18:46 +01002129 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002130
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002131 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2132 &operation, &status ) )
2133 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002134 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002135
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002136 /* The operation object should be reusable. */
2137#if defined(KNOWN_SUPPORTED_MAC_ALG)
2138 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2139 smoke_test_key_data,
2140 sizeof( smoke_test_key_data ),
2141 KNOWN_SUPPORTED_MAC_ALG,
2142 &operation, &status ) )
2143 goto exit;
2144 TEST_EQUAL( status, PSA_SUCCESS );
2145#endif
2146
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002147exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002148 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002149}
2150/* END_CASE */
2151
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002152/* 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 +00002153void mac_bad_order( )
2154{
Ronald Cron5425a212020-08-04 14:58:35 +02002155 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002156 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2157 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002158 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002159 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2160 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2161 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002163 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2164 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2165 size_t sign_mac_length = 0;
2166 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2167 const uint8_t verify_mac[] = {
2168 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2169 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2170 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2171
2172 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002173 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002174 psa_set_key_algorithm( &attributes, alg );
2175 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002176
Ronald Cron5425a212020-08-04 14:58:35 +02002177 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2178 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002179
Jaeden Amero252ef282019-02-15 14:05:35 +00002180 /* Call update without calling setup beforehand. */
2181 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2182 PSA_ERROR_BAD_STATE );
2183 PSA_ASSERT( psa_mac_abort( &operation ) );
2184
2185 /* Call sign finish without calling setup beforehand. */
2186 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2187 &sign_mac_length),
2188 PSA_ERROR_BAD_STATE );
2189 PSA_ASSERT( psa_mac_abort( &operation ) );
2190
2191 /* Call verify finish without calling setup beforehand. */
2192 TEST_EQUAL( psa_mac_verify_finish( &operation,
2193 verify_mac, sizeof( verify_mac ) ),
2194 PSA_ERROR_BAD_STATE );
2195 PSA_ASSERT( psa_mac_abort( &operation ) );
2196
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002197 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002198 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2199 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002200 PSA_ERROR_BAD_STATE );
2201 PSA_ASSERT( psa_mac_abort( &operation ) );
2202
Jaeden Amero252ef282019-02-15 14:05:35 +00002203 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002204 PSA_ASSERT( psa_mac_sign_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_sign_finish( &operation,
2207 sign_mac, sizeof( sign_mac ),
2208 &sign_mac_length ) );
2209 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2210 PSA_ERROR_BAD_STATE );
2211 PSA_ASSERT( psa_mac_abort( &operation ) );
2212
2213 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002214 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002215 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2216 PSA_ASSERT( psa_mac_verify_finish( &operation,
2217 verify_mac, sizeof( verify_mac ) ) );
2218 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2219 PSA_ERROR_BAD_STATE );
2220 PSA_ASSERT( psa_mac_abort( &operation ) );
2221
2222 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002223 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002224 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2225 PSA_ASSERT( psa_mac_sign_finish( &operation,
2226 sign_mac, sizeof( sign_mac ),
2227 &sign_mac_length ) );
2228 TEST_EQUAL( psa_mac_sign_finish( &operation,
2229 sign_mac, sizeof( sign_mac ),
2230 &sign_mac_length ),
2231 PSA_ERROR_BAD_STATE );
2232 PSA_ASSERT( psa_mac_abort( &operation ) );
2233
2234 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002235 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002236 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2237 PSA_ASSERT( psa_mac_verify_finish( &operation,
2238 verify_mac, sizeof( verify_mac ) ) );
2239 TEST_EQUAL( psa_mac_verify_finish( &operation,
2240 verify_mac, sizeof( verify_mac ) ),
2241 PSA_ERROR_BAD_STATE );
2242 PSA_ASSERT( psa_mac_abort( &operation ) );
2243
2244 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002245 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002246 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2247 TEST_EQUAL( psa_mac_verify_finish( &operation,
2248 verify_mac, sizeof( verify_mac ) ),
2249 PSA_ERROR_BAD_STATE );
2250 PSA_ASSERT( psa_mac_abort( &operation ) );
2251
2252 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002253 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002254 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2255 TEST_EQUAL( psa_mac_sign_finish( &operation,
2256 sign_mac, sizeof( sign_mac ),
2257 &sign_mac_length ),
2258 PSA_ERROR_BAD_STATE );
2259 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002260
Ronald Cron5425a212020-08-04 14:58:35 +02002261 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002262
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002263exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002264 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002265}
2266/* END_CASE */
2267
2268/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002269void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002270 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002271 int alg_arg,
2272 data_t *input,
2273 data_t *expected_mac )
2274{
Ronald Cron5425a212020-08-04 14:58:35 +02002275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002276 psa_key_type_t key_type = key_type_arg;
2277 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002278 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002280 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002281 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002282 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002283 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002284 const size_t output_sizes_to_test[] = {
2285 0,
2286 1,
2287 expected_mac->len - 1,
2288 expected_mac->len,
2289 expected_mac->len + 1,
2290 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002291
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002292 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002293 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002294 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002295
Gilles Peskine8817f612018-12-18 00:18:46 +01002296 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002297
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002298 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002299 psa_set_key_algorithm( &attributes, alg );
2300 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002301
Ronald Cron5425a212020-08-04 14:58:35 +02002302 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2303 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002304
Gilles Peskine8b356b52020-08-25 23:44:59 +02002305 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2306 {
2307 const size_t output_size = output_sizes_to_test[i];
2308 psa_status_t expected_status =
2309 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2310 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002311
Chris Jones9634bb12021-01-20 15:56:42 +00002312 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002313 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002314
Gilles Peskine8b356b52020-08-25 23:44:59 +02002315 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002316 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002317 PSA_ASSERT( psa_mac_update( &operation,
2318 input->x, input->len ) );
2319 TEST_EQUAL( psa_mac_sign_finish( &operation,
2320 actual_mac, output_size,
2321 &mac_length ),
2322 expected_status );
2323 PSA_ASSERT( psa_mac_abort( &operation ) );
2324
2325 if( expected_status == PSA_SUCCESS )
2326 {
2327 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2328 actual_mac, mac_length );
2329 }
2330 mbedtls_free( actual_mac );
2331 actual_mac = NULL;
2332 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002333
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002334exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002335 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002336 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002337 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002338 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002339}
2340/* END_CASE */
2341
2342/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002343void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002344 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002345 int alg_arg,
2346 data_t *input,
2347 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002348{
Ronald Cron5425a212020-08-04 14:58:35 +02002349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002350 psa_key_type_t key_type = key_type_arg;
2351 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002352 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002354 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002355
Gilles Peskine69c12672018-06-28 00:07:19 +02002356 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2357
Gilles Peskine8817f612018-12-18 00:18:46 +01002358 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002359
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002360 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002361 psa_set_key_algorithm( &attributes, alg );
2362 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002363
Ronald Cron5425a212020-08-04 14:58:35 +02002364 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2365 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002366
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002367 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002368 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002369 PSA_ASSERT( psa_mac_update( &operation,
2370 input->x, input->len ) );
2371 PSA_ASSERT( psa_mac_verify_finish( &operation,
2372 expected_mac->x,
2373 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002375 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002376 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002377 PSA_ASSERT( psa_mac_update( &operation,
2378 input->x, input->len ) );
2379 TEST_EQUAL( psa_mac_verify_finish( &operation,
2380 expected_mac->x,
2381 expected_mac->len - 1 ),
2382 PSA_ERROR_INVALID_SIGNATURE );
2383
2384 /* Test a MAC that's too long. */
2385 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2386 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002387 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002388 PSA_ASSERT( psa_mac_update( &operation,
2389 input->x, input->len ) );
2390 TEST_EQUAL( psa_mac_verify_finish( &operation,
2391 perturbed_mac,
2392 expected_mac->len + 1 ),
2393 PSA_ERROR_INVALID_SIGNATURE );
2394
2395 /* Test changing one byte. */
2396 for( size_t i = 0; i < expected_mac->len; i++ )
2397 {
Chris Jones9634bb12021-01-20 15:56:42 +00002398 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002399 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002400 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002401 PSA_ASSERT( psa_mac_update( &operation,
2402 input->x, input->len ) );
2403 TEST_EQUAL( psa_mac_verify_finish( &operation,
2404 perturbed_mac,
2405 expected_mac->len ),
2406 PSA_ERROR_INVALID_SIGNATURE );
2407 perturbed_mac[i] ^= 1;
2408 }
2409
Gilles Peskine8c9def32018-02-08 10:02:12 +01002410exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002411 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002412 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002413 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002414 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002415}
2416/* END_CASE */
2417
2418/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002419void cipher_operation_init( )
2420{
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 const uint8_t input[1] = { 0 };
2422 unsigned char output[1] = { 0 };
2423 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002424 /* Test each valid way of initializing the object, except for `= {0}`, as
2425 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2426 * though it's OK by the C standard. We could test for this, but we'd need
2427 * to supress the Clang warning for the test. */
2428 psa_cipher_operation_t func = psa_cipher_operation_init( );
2429 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2430 psa_cipher_operation_t zero;
2431
2432 memset( &zero, 0, sizeof( zero ) );
2433
Jaeden Ameroab439972019-02-15 14:12:05 +00002434 /* A freshly-initialized cipher operation should not be usable. */
2435 TEST_EQUAL( psa_cipher_update( &func,
2436 input, sizeof( input ),
2437 output, sizeof( output ),
2438 &output_length ),
2439 PSA_ERROR_BAD_STATE );
2440 TEST_EQUAL( psa_cipher_update( &init,
2441 input, sizeof( input ),
2442 output, sizeof( output ),
2443 &output_length ),
2444 PSA_ERROR_BAD_STATE );
2445 TEST_EQUAL( psa_cipher_update( &zero,
2446 input, sizeof( input ),
2447 output, sizeof( output ),
2448 &output_length ),
2449 PSA_ERROR_BAD_STATE );
2450
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002451 /* A default cipher operation should be abortable without error. */
2452 PSA_ASSERT( psa_cipher_abort( &func ) );
2453 PSA_ASSERT( psa_cipher_abort( &init ) );
2454 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002455}
2456/* END_CASE */
2457
2458/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002459void cipher_setup( int key_type_arg,
2460 data_t *key,
2461 int alg_arg,
2462 int expected_status_arg )
2463{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002464 psa_key_type_t key_type = key_type_arg;
2465 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002466 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002467 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002468 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002469#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002470 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2471#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002472
Gilles Peskine8817f612018-12-18 00:18:46 +01002473 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002474
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002475 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2476 &operation, &status ) )
2477 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002478 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002479
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002480 /* The operation object should be reusable. */
2481#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2482 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2483 smoke_test_key_data,
2484 sizeof( smoke_test_key_data ),
2485 KNOWN_SUPPORTED_CIPHER_ALG,
2486 &operation, &status ) )
2487 goto exit;
2488 TEST_EQUAL( status, PSA_SUCCESS );
2489#endif
2490
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002491exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002492 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002493 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002494}
2495/* END_CASE */
2496
Ronald Cronee414c72021-03-18 18:50:08 +01002497/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002498void cipher_bad_order( )
2499{
Ronald Cron5425a212020-08-04 14:58:35 +02002500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002501 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2502 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002503 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002504 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002505 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002506 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002507 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2508 0xaa, 0xaa, 0xaa, 0xaa };
2509 const uint8_t text[] = {
2510 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2511 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002512 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002513 size_t length = 0;
2514
2515 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002516 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2517 psa_set_key_algorithm( &attributes, alg );
2518 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002519 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2520 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002521
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002522 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002523 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2524 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002525 PSA_ERROR_BAD_STATE );
2526 PSA_ASSERT( psa_cipher_abort( &operation ) );
2527
2528 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002529 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2530 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002531 PSA_ERROR_BAD_STATE );
2532 PSA_ASSERT( psa_cipher_abort( &operation ) );
2533
Jaeden Ameroab439972019-02-15 14:12:05 +00002534 /* Generate an IV without calling setup beforehand. */
2535 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2536 buffer, sizeof( buffer ),
2537 &length ),
2538 PSA_ERROR_BAD_STATE );
2539 PSA_ASSERT( psa_cipher_abort( &operation ) );
2540
2541 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002542 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002543 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2544 buffer, sizeof( buffer ),
2545 &length ) );
2546 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2547 buffer, sizeof( buffer ),
2548 &length ),
2549 PSA_ERROR_BAD_STATE );
2550 PSA_ASSERT( psa_cipher_abort( &operation ) );
2551
2552 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002553 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002554 PSA_ASSERT( psa_cipher_set_iv( &operation,
2555 iv, sizeof( iv ) ) );
2556 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2557 buffer, sizeof( buffer ),
2558 &length ),
2559 PSA_ERROR_BAD_STATE );
2560 PSA_ASSERT( psa_cipher_abort( &operation ) );
2561
2562 /* Set an IV without calling setup beforehand. */
2563 TEST_EQUAL( psa_cipher_set_iv( &operation,
2564 iv, sizeof( iv ) ),
2565 PSA_ERROR_BAD_STATE );
2566 PSA_ASSERT( psa_cipher_abort( &operation ) );
2567
2568 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002569 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002570 PSA_ASSERT( psa_cipher_set_iv( &operation,
2571 iv, sizeof( iv ) ) );
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 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002578 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002579 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2580 buffer, sizeof( buffer ),
2581 &length ) );
2582 TEST_EQUAL( psa_cipher_set_iv( &operation,
2583 iv, sizeof( iv ) ),
2584 PSA_ERROR_BAD_STATE );
2585 PSA_ASSERT( psa_cipher_abort( &operation ) );
2586
2587 /* Call update without calling setup beforehand. */
2588 TEST_EQUAL( psa_cipher_update( &operation,
2589 text, sizeof( text ),
2590 buffer, sizeof( buffer ),
2591 &length ),
2592 PSA_ERROR_BAD_STATE );
2593 PSA_ASSERT( psa_cipher_abort( &operation ) );
2594
2595 /* Call update without an IV where an IV is required. */
2596 TEST_EQUAL( psa_cipher_update( &operation,
2597 text, sizeof( text ),
2598 buffer, sizeof( buffer ),
2599 &length ),
2600 PSA_ERROR_BAD_STATE );
2601 PSA_ASSERT( psa_cipher_abort( &operation ) );
2602
2603 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002604 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002605 PSA_ASSERT( psa_cipher_set_iv( &operation,
2606 iv, sizeof( iv ) ) );
2607 PSA_ASSERT( psa_cipher_finish( &operation,
2608 buffer, sizeof( buffer ), &length ) );
2609 TEST_EQUAL( psa_cipher_update( &operation,
2610 text, sizeof( text ),
2611 buffer, sizeof( buffer ),
2612 &length ),
2613 PSA_ERROR_BAD_STATE );
2614 PSA_ASSERT( psa_cipher_abort( &operation ) );
2615
2616 /* Call finish without calling setup beforehand. */
2617 TEST_EQUAL( psa_cipher_finish( &operation,
2618 buffer, sizeof( buffer ), &length ),
2619 PSA_ERROR_BAD_STATE );
2620 PSA_ASSERT( psa_cipher_abort( &operation ) );
2621
2622 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002623 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002624 /* Not calling update means we are encrypting an empty buffer, which is OK
2625 * for cipher modes with padding. */
2626 TEST_EQUAL( psa_cipher_finish( &operation,
2627 buffer, sizeof( buffer ), &length ),
2628 PSA_ERROR_BAD_STATE );
2629 PSA_ASSERT( psa_cipher_abort( &operation ) );
2630
2631 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002632 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002633 PSA_ASSERT( psa_cipher_set_iv( &operation,
2634 iv, sizeof( iv ) ) );
2635 PSA_ASSERT( psa_cipher_finish( &operation,
2636 buffer, sizeof( buffer ), &length ) );
2637 TEST_EQUAL( psa_cipher_finish( &operation,
2638 buffer, sizeof( buffer ), &length ),
2639 PSA_ERROR_BAD_STATE );
2640 PSA_ASSERT( psa_cipher_abort( &operation ) );
2641
Ronald Cron5425a212020-08-04 14:58:35 +02002642 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002643
Jaeden Ameroab439972019-02-15 14:12:05 +00002644exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002645 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002646 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002647}
2648/* END_CASE */
2649
2650/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002651void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002652 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002653 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002654 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002655{
Ronald Cron5425a212020-08-04 14:58:35 +02002656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657 psa_status_t status;
2658 psa_key_type_t key_type = key_type_arg;
2659 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002660 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002661 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002662 size_t output_buffer_size = 0;
2663 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002664 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002665 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002667
Gilles Peskine8817f612018-12-18 00:18:46 +01002668 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002669
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002670 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2671 psa_set_key_algorithm( &attributes, alg );
2672 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002673
Ronald Cron5425a212020-08-04 14:58:35 +02002674 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2675 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002676
Ronald Cron5425a212020-08-04 14:58:35 +02002677 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002679 if( iv->len > 0 )
2680 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002681 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002682 }
2683
gabor-mezei-armceface22021-01-21 12:26:17 +01002684 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2685 TEST_ASSERT( output_buffer_size <=
2686 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002687 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002688
Gilles Peskine8817f612018-12-18 00:18:46 +01002689 PSA_ASSERT( psa_cipher_update( &operation,
2690 input->x, input->len,
2691 output, output_buffer_size,
2692 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002693 TEST_ASSERT( function_output_length <=
2694 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2695 TEST_ASSERT( function_output_length <=
2696 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002697 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002698
Gilles Peskine50e586b2018-06-08 14:28:46 +02002699 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002700 ( output_buffer_size == 0 ? NULL :
2701 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002702 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002704 TEST_ASSERT( function_output_length <=
2705 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2706 TEST_ASSERT( function_output_length <=
2707 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002708 total_output_length += function_output_length;
2709
Gilles Peskinefe11b722018-12-18 00:24:04 +01002710 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002711 if( expected_status == PSA_SUCCESS )
2712 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002713 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002714 ASSERT_COMPARE( expected_output->x, expected_output->len,
2715 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002716 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002717
Gilles Peskine50e586b2018-06-08 14:28:46 +02002718exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002719 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002720 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002721 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002722 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002723}
2724/* END_CASE */
2725
2726/* BEGIN_CASE */
2727void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002728 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002729 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002730 int first_part_size_arg,
2731 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002732 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002733{
Ronald Cron5425a212020-08-04 14:58:35 +02002734 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002735 psa_key_type_t key_type = key_type_arg;
2736 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002737 size_t first_part_size = first_part_size_arg;
2738 size_t output1_length = output1_length_arg;
2739 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002740 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002741 size_t output_buffer_size = 0;
2742 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002743 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002744 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002746
Gilles Peskine8817f612018-12-18 00:18:46 +01002747 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002748
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002749 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2750 psa_set_key_algorithm( &attributes, alg );
2751 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002752
Ronald Cron5425a212020-08-04 14:58:35 +02002753 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2754 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002755
Ronald Cron5425a212020-08-04 14:58:35 +02002756 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002757
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002758 if( iv->len > 0 )
2759 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002760 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002761 }
2762
gabor-mezei-armceface22021-01-21 12:26:17 +01002763 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2764 TEST_ASSERT( output_buffer_size <=
2765 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002766 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002767
Gilles Peskinee0866522019-02-19 19:44:00 +01002768 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2770 output, output_buffer_size,
2771 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002772 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002773 TEST_ASSERT( function_output_length <=
2774 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2775 TEST_ASSERT( function_output_length <=
2776 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002777 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002778
Gilles Peskine8817f612018-12-18 00:18:46 +01002779 PSA_ASSERT( psa_cipher_update( &operation,
2780 input->x + first_part_size,
2781 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002782 ( output_buffer_size == 0 ? NULL :
2783 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002784 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002785 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002786 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002787 TEST_ASSERT( function_output_length <=
2788 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2789 alg,
2790 input->len - first_part_size ) );
2791 TEST_ASSERT( function_output_length <=
2792 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002793 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002794
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002796 ( output_buffer_size == 0 ? NULL :
2797 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002798 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002799 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002800 TEST_ASSERT( function_output_length <=
2801 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2802 TEST_ASSERT( function_output_length <=
2803 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002804 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002805 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002806
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002807 ASSERT_COMPARE( expected_output->x, expected_output->len,
2808 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809
2810exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002811 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002812 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002813 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002814 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815}
2816/* END_CASE */
2817
2818/* BEGIN_CASE */
2819void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002820 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002821 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002822 int first_part_size_arg,
2823 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002824 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002825{
Ronald Cron5425a212020-08-04 14:58:35 +02002826 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002827 psa_key_type_t key_type = key_type_arg;
2828 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002829 size_t first_part_size = first_part_size_arg;
2830 size_t output1_length = output1_length_arg;
2831 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002832 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002833 size_t output_buffer_size = 0;
2834 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002835 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002836 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002838
Gilles Peskine8817f612018-12-18 00:18:46 +01002839 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002840
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002841 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2842 psa_set_key_algorithm( &attributes, alg );
2843 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002844
Ronald Cron5425a212020-08-04 14:58:35 +02002845 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2846 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002847
Ronald Cron5425a212020-08-04 14:58:35 +02002848 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002849
Steven Cooreman177deba2020-09-07 17:14:14 +02002850 if( iv->len > 0 )
2851 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002852 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002853 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002854
gabor-mezei-armceface22021-01-21 12:26:17 +01002855 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2856 TEST_ASSERT( output_buffer_size <=
2857 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002858 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002859
Gilles Peskinee0866522019-02-19 19:44:00 +01002860 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002861 PSA_ASSERT( psa_cipher_update( &operation,
2862 input->x, first_part_size,
2863 output, output_buffer_size,
2864 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002865 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002866 TEST_ASSERT( function_output_length <=
2867 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2868 TEST_ASSERT( function_output_length <=
2869 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002870 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002871
Gilles Peskine8817f612018-12-18 00:18:46 +01002872 PSA_ASSERT( psa_cipher_update( &operation,
2873 input->x + first_part_size,
2874 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002875 ( output_buffer_size == 0 ? NULL :
2876 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002877 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002878 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002879 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002880 TEST_ASSERT( function_output_length <=
2881 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2882 alg,
2883 input->len - first_part_size ) );
2884 TEST_ASSERT( function_output_length <=
2885 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002886 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002887
Gilles Peskine8817f612018-12-18 00:18:46 +01002888 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002889 ( output_buffer_size == 0 ? NULL :
2890 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002891 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002892 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002893 TEST_ASSERT( function_output_length <=
2894 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2895 TEST_ASSERT( function_output_length <=
2896 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002897 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002898 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002899
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002900 ASSERT_COMPARE( expected_output->x, expected_output->len,
2901 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002902
2903exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002904 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002905 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002906 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002907 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908}
2909/* END_CASE */
2910
Gilles Peskine50e586b2018-06-08 14:28:46 +02002911/* BEGIN_CASE */
2912void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002913 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002914 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002915 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002916{
Ronald Cron5425a212020-08-04 14:58:35 +02002917 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002918 psa_status_t status;
2919 psa_key_type_t key_type = key_type_arg;
2920 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002921 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002922 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002923 size_t output_buffer_size = 0;
2924 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002925 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002926 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002928
Gilles Peskine8817f612018-12-18 00:18:46 +01002929 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002930
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002931 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2932 psa_set_key_algorithm( &attributes, alg );
2933 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002934
Ronald Cron5425a212020-08-04 14:58:35 +02002935 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2936 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002937
Ronald Cron5425a212020-08-04 14:58:35 +02002938 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002939
Steven Cooreman177deba2020-09-07 17:14:14 +02002940 if( iv->len > 0 )
2941 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002942 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002943 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002944
gabor-mezei-armceface22021-01-21 12:26:17 +01002945 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2946 TEST_ASSERT( output_buffer_size <=
2947 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002948 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002949
Gilles Peskine8817f612018-12-18 00:18:46 +01002950 PSA_ASSERT( psa_cipher_update( &operation,
2951 input->x, input->len,
2952 output, output_buffer_size,
2953 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002954 TEST_ASSERT( function_output_length <=
2955 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2956 TEST_ASSERT( function_output_length <=
2957 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002958 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002959
Gilles Peskine50e586b2018-06-08 14:28:46 +02002960 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002961 ( output_buffer_size == 0 ? NULL :
2962 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002963 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002964 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002965 TEST_ASSERT( function_output_length <=
2966 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2967 TEST_ASSERT( function_output_length <=
2968 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002969 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002970 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002971
2972 if( expected_status == PSA_SUCCESS )
2973 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002974 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002975 ASSERT_COMPARE( expected_output->x, expected_output->len,
2976 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002977 }
2978
Gilles Peskine50e586b2018-06-08 14:28:46 +02002979exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002980 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002981 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002982 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002983 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002984}
2985/* END_CASE */
2986
Gilles Peskine50e586b2018-06-08 14:28:46 +02002987/* BEGIN_CASE */
2988void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002989 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002990 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002991{
Ronald Cron5425a212020-08-04 14:58:35 +02002992 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002993 psa_key_type_t key_type = key_type_arg;
2994 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002995 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002996 size_t iv_size = 16;
2997 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002998 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002999 size_t output1_size = 0;
3000 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003001 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003002 size_t output2_size = 0;
3003 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003004 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003005 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3006 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003008
Gilles Peskine8817f612018-12-18 00:18:46 +01003009 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003010
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003011 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3012 psa_set_key_algorithm( &attributes, alg );
3013 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003014
Ronald Cron5425a212020-08-04 14:58:35 +02003015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3016 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003017
Ronald Cron5425a212020-08-04 14:58:35 +02003018 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3019 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003020
Steven Cooreman177deba2020-09-07 17:14:14 +02003021 if( alg != PSA_ALG_ECB_NO_PADDING )
3022 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003023 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3024 iv, iv_size,
3025 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003026 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003027 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3028 TEST_ASSERT( output1_size <=
3029 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003030 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003031
Gilles Peskine8817f612018-12-18 00:18:46 +01003032 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3033 output1, output1_size,
3034 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003035 TEST_ASSERT( output1_length <=
3036 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3037 TEST_ASSERT( output1_length <=
3038 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3039
Gilles Peskine8817f612018-12-18 00:18:46 +01003040 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003041 output1 + output1_length,
3042 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003043 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003044 TEST_ASSERT( function_output_length <=
3045 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3046 TEST_ASSERT( function_output_length <=
3047 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003048
Gilles Peskine048b7f02018-06-08 14:20:49 +02003049 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003050
Gilles Peskine8817f612018-12-18 00:18:46 +01003051 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003052
3053 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003054 TEST_ASSERT( output2_size <=
3055 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3056 TEST_ASSERT( output2_size <=
3057 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003058 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003059
Steven Cooreman177deba2020-09-07 17:14:14 +02003060 if( iv_length > 0 )
3061 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003062 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3063 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003064 }
3065
Gilles Peskine8817f612018-12-18 00:18:46 +01003066 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3067 output2, output2_size,
3068 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003069 TEST_ASSERT( output2_length <=
3070 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3071 TEST_ASSERT( output2_length <=
3072 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3073
Gilles Peskine048b7f02018-06-08 14:20:49 +02003074 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003075 PSA_ASSERT( psa_cipher_finish( &operation2,
3076 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003077 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003078 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003079 TEST_ASSERT( function_output_length <=
3080 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3081 TEST_ASSERT( function_output_length <=
3082 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003083
Gilles Peskine048b7f02018-06-08 14:20:49 +02003084 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003085
Gilles Peskine8817f612018-12-18 00:18:46 +01003086 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003087
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003088 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003089
3090exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003091 psa_cipher_abort( &operation1 );
3092 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003093 mbedtls_free( output1 );
3094 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003095 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003096 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003097}
3098/* END_CASE */
3099
3100/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003101void cipher_verify_output_multipart( int alg_arg,
3102 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003103 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003104 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003105 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003106{
Ronald Cron5425a212020-08-04 14:58:35 +02003107 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003108 psa_key_type_t key_type = key_type_arg;
3109 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003110 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003111 unsigned char iv[16] = {0};
3112 size_t iv_size = 16;
3113 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003114 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003115 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003116 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003117 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003118 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003119 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003120 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003121 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3122 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003124
Gilles Peskine8817f612018-12-18 00:18:46 +01003125 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003126
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003127 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3128 psa_set_key_algorithm( &attributes, alg );
3129 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003130
Ronald Cron5425a212020-08-04 14:58:35 +02003131 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3132 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003133
Ronald Cron5425a212020-08-04 14:58:35 +02003134 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3135 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003136
Steven Cooreman177deba2020-09-07 17:14:14 +02003137 if( alg != PSA_ALG_ECB_NO_PADDING )
3138 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003139 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3140 iv, iv_size,
3141 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003142 }
3143
gabor-mezei-armceface22021-01-21 12:26:17 +01003144 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3145 TEST_ASSERT( output1_buffer_size <=
3146 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003147 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003148
Gilles Peskinee0866522019-02-19 19:44:00 +01003149 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003150
Gilles Peskine8817f612018-12-18 00:18:46 +01003151 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3152 output1, output1_buffer_size,
3153 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003154 TEST_ASSERT( function_output_length <=
3155 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3156 TEST_ASSERT( function_output_length <=
3157 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003158 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003159
Gilles Peskine8817f612018-12-18 00:18:46 +01003160 PSA_ASSERT( psa_cipher_update( &operation1,
3161 input->x + first_part_size,
3162 input->len - first_part_size,
3163 output1, output1_buffer_size,
3164 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003165 TEST_ASSERT( function_output_length <=
3166 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3167 alg,
3168 input->len - first_part_size ) );
3169 TEST_ASSERT( function_output_length <=
3170 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003171 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003172
Gilles Peskine8817f612018-12-18 00:18:46 +01003173 PSA_ASSERT( psa_cipher_finish( &operation1,
3174 output1 + output1_length,
3175 output1_buffer_size - output1_length,
3176 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003177 TEST_ASSERT( function_output_length <=
3178 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3179 TEST_ASSERT( function_output_length <=
3180 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003181 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003182
Gilles Peskine8817f612018-12-18 00:18:46 +01003183 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003184
Gilles Peskine048b7f02018-06-08 14:20:49 +02003185 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003186 TEST_ASSERT( output2_buffer_size <=
3187 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3188 TEST_ASSERT( output2_buffer_size <=
3189 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003190 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003191
Steven Cooreman177deba2020-09-07 17:14:14 +02003192 if( iv_length > 0 )
3193 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003194 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3195 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003196 }
Moran Pekerded84402018-06-06 16:36:50 +03003197
Gilles Peskine8817f612018-12-18 00:18:46 +01003198 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3199 output2, output2_buffer_size,
3200 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003201 TEST_ASSERT( function_output_length <=
3202 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3203 TEST_ASSERT( function_output_length <=
3204 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003205 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003206
Gilles Peskine8817f612018-12-18 00:18:46 +01003207 PSA_ASSERT( psa_cipher_update( &operation2,
3208 output1 + first_part_size,
3209 output1_length - first_part_size,
3210 output2, output2_buffer_size,
3211 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003212 TEST_ASSERT( function_output_length <=
3213 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3214 alg,
3215 output1_length - first_part_size ) );
3216 TEST_ASSERT( function_output_length <=
3217 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003218 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003219
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_cipher_finish( &operation2,
3221 output2 + output2_length,
3222 output2_buffer_size - output2_length,
3223 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003224 TEST_ASSERT( function_output_length <=
3225 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3226 TEST_ASSERT( function_output_length <=
3227 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003228 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003229
Gilles Peskine8817f612018-12-18 00:18:46 +01003230 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003231
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003232 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003233
3234exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003235 psa_cipher_abort( &operation1 );
3236 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003237 mbedtls_free( output1 );
3238 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003239 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003240 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003241}
3242/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003243
Gilles Peskine20035e32018-02-03 22:44:14 +01003244/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003245void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003246 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003247 data_t *nonce,
3248 data_t *additional_data,
3249 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003250 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251{
Ronald Cron5425a212020-08-04 14:58:35 +02003252 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003253 psa_key_type_t key_type = key_type_arg;
3254 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003255 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003256 unsigned char *output_data = NULL;
3257 size_t output_size = 0;
3258 size_t output_length = 0;
3259 unsigned char *output_data2 = NULL;
3260 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003261 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003262 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003264
Gilles Peskine8817f612018-12-18 00:18:46 +01003265 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003266
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003267 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3268 psa_set_key_algorithm( &attributes, alg );
3269 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003270
Gilles Peskine049c7532019-05-15 20:22:09 +02003271 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003272 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003273 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3274 key_bits = psa_get_key_bits( &attributes );
3275
3276 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3277 alg );
3278 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3279 * should be exact. */
3280 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3281 expected_result != PSA_ERROR_NOT_SUPPORTED )
3282 {
3283 TEST_EQUAL( output_size,
3284 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3285 TEST_ASSERT( output_size <=
3286 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3287 }
3288 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003289
Steven Cooremanf49478b2021-02-15 15:19:25 +01003290 status = psa_aead_encrypt( key, alg,
3291 nonce->x, nonce->len,
3292 additional_data->x,
3293 additional_data->len,
3294 input_data->x, input_data->len,
3295 output_data, output_size,
3296 &output_length );
3297
3298 /* If the operation is not supported, just skip and not fail in case the
3299 * encryption involves a common limitation of cryptography hardwares and
3300 * an alternative implementation. */
3301 if( status == PSA_ERROR_NOT_SUPPORTED )
3302 {
3303 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3304 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3305 }
3306
3307 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003308
3309 if( PSA_SUCCESS == expected_result )
3310 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003311 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003312
Gilles Peskine003a4a92019-05-14 16:09:40 +02003313 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3314 * should be exact. */
3315 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003316 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003317
gabor-mezei-armceface22021-01-21 12:26:17 +01003318 TEST_ASSERT( input_data->len <=
3319 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3320
Ronald Cron5425a212020-08-04 14:58:35 +02003321 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003322 nonce->x, nonce->len,
3323 additional_data->x,
3324 additional_data->len,
3325 output_data, output_length,
3326 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003327 &output_length2 ),
3328 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003329
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003330 ASSERT_COMPARE( input_data->x, input_data->len,
3331 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003332 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003333
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003335 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336 mbedtls_free( output_data );
3337 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003338 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339}
3340/* END_CASE */
3341
3342/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003343void aead_encrypt( int key_type_arg, data_t *key_data,
3344 int alg_arg,
3345 data_t *nonce,
3346 data_t *additional_data,
3347 data_t *input_data,
3348 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003349{
Ronald Cron5425a212020-08-04 14:58:35 +02003350 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351 psa_key_type_t key_type = key_type_arg;
3352 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003353 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003354 unsigned char *output_data = NULL;
3355 size_t output_size = 0;
3356 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003358 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003359
Gilles Peskine8817f612018-12-18 00:18:46 +01003360 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003361
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003362 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3363 psa_set_key_algorithm( &attributes, alg );
3364 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365
Gilles Peskine049c7532019-05-15 20:22:09 +02003366 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003367 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003368 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3369 key_bits = psa_get_key_bits( &attributes );
3370
3371 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3372 alg );
3373 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3374 * should be exact. */
3375 TEST_EQUAL( output_size,
3376 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3377 TEST_ASSERT( output_size <=
3378 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3379 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003380
Steven Cooremand588ea12021-01-11 19:36:04 +01003381 status = psa_aead_encrypt( key, alg,
3382 nonce->x, nonce->len,
3383 additional_data->x, additional_data->len,
3384 input_data->x, input_data->len,
3385 output_data, output_size,
3386 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003387
Ronald Cron28a45ed2021-02-09 20:35:42 +01003388 /* If the operation is not supported, just skip and not fail in case the
3389 * encryption involves a common limitation of cryptography hardwares and
3390 * an alternative implementation. */
3391 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003392 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003393 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3394 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003395 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003396
3397 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003398 ASSERT_COMPARE( expected_result->x, expected_result->len,
3399 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003400
Gilles Peskinea1cac842018-06-11 19:33:02 +02003401exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003402 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003404 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003405}
3406/* END_CASE */
3407
3408/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003409void aead_decrypt( int key_type_arg, data_t *key_data,
3410 int alg_arg,
3411 data_t *nonce,
3412 data_t *additional_data,
3413 data_t *input_data,
3414 data_t *expected_data,
3415 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003416{
Ronald Cron5425a212020-08-04 14:58:35 +02003417 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418 psa_key_type_t key_type = key_type_arg;
3419 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003420 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003421 unsigned char *output_data = NULL;
3422 size_t output_size = 0;
3423 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003424 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003425 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003426 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003427
Gilles Peskine8817f612018-12-18 00:18:46 +01003428 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003429
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003430 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3431 psa_set_key_algorithm( &attributes, alg );
3432 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003433
Gilles Peskine049c7532019-05-15 20:22:09 +02003434 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003435 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003436 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3437 key_bits = psa_get_key_bits( &attributes );
3438
3439 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3440 alg );
3441 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3442 expected_result != PSA_ERROR_NOT_SUPPORTED )
3443 {
3444 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3445 * should be exact. */
3446 TEST_EQUAL( output_size,
3447 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3448 TEST_ASSERT( output_size <=
3449 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3450 }
3451 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003452
Steven Cooremand588ea12021-01-11 19:36:04 +01003453 status = psa_aead_decrypt( key, alg,
3454 nonce->x, nonce->len,
3455 additional_data->x,
3456 additional_data->len,
3457 input_data->x, input_data->len,
3458 output_data, output_size,
3459 &output_length );
3460
Ronald Cron28a45ed2021-02-09 20:35:42 +01003461 /* If the operation is not supported, just skip and not fail in case the
3462 * decryption involves a common limitation of cryptography hardwares and
3463 * an alternative implementation. */
3464 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003465 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003466 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3467 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003468 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003469
3470 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003471
Gilles Peskine2d277862018-06-18 15:41:12 +02003472 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003473 ASSERT_COMPARE( expected_data->x, expected_data->len,
3474 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475
Gilles Peskinea1cac842018-06-11 19:33:02 +02003476exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003477 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003479 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003480}
3481/* END_CASE */
3482
3483/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003484void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3485 int alg_arg,
3486 data_t *nonce,
3487 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003488 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003489 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003490 int do_test_data_chunked,
3491 int do_set_lengths,
3492 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003493{
Paul Elliottd3f82412021-06-16 16:52:21 +01003494 size_t ad_part_len = 0;
3495 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003496
Paul Elliotte64deda2021-09-09 14:07:23 +01003497 /* Ensure that either one part of the test or the other is done, i.e this
3498 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003499 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3500
3501 /* Temporary whilst we have algorithms that cannot support chunking */
3502 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003503 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003504 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3505 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003506 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003507 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003508
Paul Elliott329d5382021-07-22 17:10:45 +01003509 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003510 if( !aead_multipart_internal_func( key_type_arg, key_data,
3511 alg_arg, nonce,
3512 additional_data,
3513 ad_part_len,
3514 input_data, -1,
3515 do_set_lengths,
3516 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003517 1, 1, 0,
3518 ( ad_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003519 break;
3520
3521 /* length(0) part, length(ad_part_len) part, length(0) part... */
3522 mbedtls_test_set_step( 1000 + ad_part_len );
3523
3524 if( !aead_multipart_internal_func( key_type_arg, key_data,
3525 alg_arg, nonce,
3526 additional_data,
3527 ad_part_len,
3528 input_data, -1,
3529 do_set_lengths,
3530 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003531 1, 1, 1,
3532 ( ad_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003533 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003534 }
3535 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003536
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003537 /* Temporary whilst we have algorithms that cannot support chunking */
3538 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003539 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003540 for( data_part_len = 1; data_part_len <= input_data->len;
3541 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003542 {
Paul Elliott329d5382021-07-22 17:10:45 +01003543 /* Split data into length(data_part_len) parts. */
3544 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003545
3546 if( !aead_multipart_internal_func( key_type_arg, key_data,
3547 alg_arg, nonce,
3548 additional_data, -1,
3549 input_data, data_part_len,
3550 do_set_lengths,
3551 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003552 1, 1, 0,
3553 ( data_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003554 break;
3555
3556 /* length(0) part, length(data_part_len) part, length(0) part... */
3557 mbedtls_test_set_step( 3000 + data_part_len );
3558
3559 if( !aead_multipart_internal_func( key_type_arg, key_data,
3560 alg_arg, nonce,
3561 additional_data, -1,
3562 input_data, data_part_len,
3563 do_set_lengths,
3564 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003565 1, 1, 1,
3566 ( data_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003567 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003568 }
3569 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003570
Paul Elliott0023e0a2021-04-27 10:06:22 +01003571
Paul Elliott8fc45162021-06-23 16:06:01 +01003572 /* Goto is required to silence warnings about unused labels, as we
3573 * don't actually do any test assertions in this function. */
3574 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003575}
3576/* END_CASE */
3577
3578/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003579void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3580 int alg_arg,
3581 data_t *nonce,
3582 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003583 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003584 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003585 int do_test_data_chunked,
3586 int do_set_lengths,
3587 data_t *expected_output,
3588 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003589{
Paul Elliottd3f82412021-06-16 16:52:21 +01003590 size_t ad_part_len = 0;
3591 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003592
Paul Elliotte64deda2021-09-09 14:07:23 +01003593 /* Ensure that either one part of the test or the other is done, i.e this
3594 * test does something. */
3595 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3596
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003597 /* Temporary whilst we have algorithms that cannot support chunking */
3598 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003599 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003600 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3601 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003602 {
Paul Elliott329d5382021-07-22 17:10:45 +01003603 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003604 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003605
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003606 if( !aead_multipart_internal_func( key_type_arg, key_data,
3607 alg_arg, nonce,
3608 additional_data,
3609 ad_part_len,
3610 input_data, -1,
3611 do_set_lengths,
3612 expected_output,
3613 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003614 0, 0,
3615 ( ad_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003616 break;
3617
3618 /* length(0) part, length(ad_part_len) part, length(0) part... */
3619 mbedtls_test_set_step( 1000 + ad_part_len );
3620
3621 if( !aead_multipart_internal_func( key_type_arg, key_data,
3622 alg_arg, nonce,
3623 additional_data,
3624 ad_part_len,
3625 input_data, -1,
3626 do_set_lengths,
3627 expected_output,
3628 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003629 0, 1,
3630 ( ad_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003631 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003632 }
3633 }
3634
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003635 /* Temporary whilst we have algorithms that cannot support chunking */
3636 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003637 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003638 for( data_part_len = 1; data_part_len <= input_data->len;
3639 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003640 {
Paul Elliott329d5382021-07-22 17:10:45 +01003641 /* Split data into length(data_part_len) parts. */
3642 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003643
3644 if( !aead_multipart_internal_func( key_type_arg, key_data,
3645 alg_arg, nonce,
3646 additional_data, -1,
3647 input_data, data_part_len,
3648 do_set_lengths,
3649 expected_output,
3650 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003651 0, 0,
3652 ( data_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003653 break;
3654
3655 /* length(0) part, length(data_part_len) part, length(0) part... */
3656 mbedtls_test_set_step( 3000 + data_part_len );
3657
3658 if( !aead_multipart_internal_func( key_type_arg, key_data,
3659 alg_arg, nonce,
3660 additional_data, -1,
3661 input_data, data_part_len,
3662 do_set_lengths,
3663 expected_output,
3664 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003665 0, 1,
3666 ( data_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003667 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003668 }
3669 }
3670
Paul Elliott8fc45162021-06-23 16:06:01 +01003671 /* Goto is required to silence warnings about unused labels, as we
3672 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003673 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003674}
3675/* END_CASE */
3676
3677/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003678void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3679 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003680 int nonce_length,
3681 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003682 data_t *additional_data,
3683 data_t *input_data,
3684 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003685{
3686
3687 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3688 psa_key_type_t key_type = key_type_arg;
3689 psa_algorithm_t alg = alg_arg;
3690 psa_aead_operation_t operation;
3691 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3693 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003694 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003695 size_t actual_nonce_length = 0;
3696 size_t expected_nonce_length = expected_nonce_length_arg;
3697 unsigned char *output = NULL;
3698 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003699 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003700 size_t ciphertext_size = 0;
3701 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003702 size_t tag_length = 0;
3703 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003704
3705 PSA_ASSERT( psa_crypto_init( ) );
3706
3707 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3708 psa_set_key_algorithm( & attributes, alg );
3709 psa_set_key_type( & attributes, key_type );
3710
3711 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3712 &key ) );
3713
3714 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3715
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003716 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3717
Paul Elliottf1277632021-08-24 18:11:37 +01003718 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003719
Paul Elliottf1277632021-08-24 18:11:37 +01003720 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003721
Paul Elliottf1277632021-08-24 18:11:37 +01003722 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003723
Paul Elliottf1277632021-08-24 18:11:37 +01003724 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003725
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003726 operation = psa_aead_operation_init( );
3727
3728 status = psa_aead_encrypt_setup( &operation, key, alg );
3729
3730 /* If the operation is not supported, just skip and not fail in case the
3731 * encryption involves a common limitation of cryptography hardwares and
3732 * an alternative implementation. */
3733 if( status == PSA_ERROR_NOT_SUPPORTED )
3734 {
3735 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003736 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003737 }
3738
3739 PSA_ASSERT( status );
3740
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003741 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003742 nonce_length,
3743 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003744
Paul Elliott693bf312021-07-23 17:40:41 +01003745 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003746
Paul Elliottf1277632021-08-24 18:11:37 +01003747 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003748
Paul Elliottf1277632021-08-24 18:11:37 +01003749 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003750
Paul Elliott693bf312021-07-23 17:40:41 +01003751 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003752 {
3753
3754 /* Ensure we can still complete operation. */
3755
3756 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3757 additional_data->len ) );
3758
3759 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003760 output, output_size,
3761 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003762
Paul Elliottf1277632021-08-24 18:11:37 +01003763 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3764 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003765 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3766 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003767
3768exit:
3769 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003770 mbedtls_free( output );
3771 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003772 psa_aead_abort( &operation );
3773 PSA_DONE( );
3774}
3775/* END_CASE */
3776
3777/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003778void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3779 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003780 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003781 data_t *additional_data,
3782 data_t *input_data,
3783 int expected_status_arg )
3784{
3785
3786 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3787 psa_key_type_t key_type = key_type_arg;
3788 psa_algorithm_t alg = alg_arg;
3789 psa_aead_operation_t operation;
3790 uint8_t *nonce_buffer = NULL;
3791 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3792 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3793 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003794 unsigned char *output = NULL;
3795 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003796 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003797 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003798 size_t ciphertext_size = 0;
3799 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003800 size_t tag_length = 0;
3801 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003802 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003803
3804 PSA_ASSERT( psa_crypto_init( ) );
3805
3806 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3807 psa_set_key_algorithm( &attributes, alg );
3808 psa_set_key_type( &attributes, key_type );
3809
3810 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3811 &key ) );
3812
3813 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3814
3815 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3816
Paul Elliott6f0e7202021-08-25 12:57:18 +01003817 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003818
Paul Elliott6f0e7202021-08-25 12:57:18 +01003819 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003820
Paul Elliott6f0e7202021-08-25 12:57:18 +01003821 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003822
Paul Elliott6f0e7202021-08-25 12:57:18 +01003823 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003824
3825 operation = psa_aead_operation_init( );
3826
3827 status = psa_aead_encrypt_setup( &operation, key, alg );
3828
3829 /* If the operation is not supported, just skip and not fail in case the
3830 * encryption involves a common limitation of cryptography hardwares and
3831 * an alternative implementation. */
3832 if( status == PSA_ERROR_NOT_SUPPORTED )
3833 {
3834 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003835 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003836 }
3837
3838 PSA_ASSERT( status );
3839
Paul Elliott4023ffd2021-09-10 16:21:22 +01003840 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3841 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003842 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003843 /* Arbitrary size buffer, to test zero length valid buffer. */
3844 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003845 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003846 }
3847 else
3848 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003849 /* If length is zero, then this will return NULL. */
3850 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003851 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003852
Paul Elliott4023ffd2021-09-10 16:21:22 +01003853 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003854 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003855 for( index = 0; index < nonce_length - 1; ++index )
3856 {
3857 nonce_buffer[index] = 'a' + index;
3858 }
Paul Elliott66696b52021-08-16 18:42:41 +01003859 }
Paul Elliott863864a2021-07-23 17:28:31 +01003860 }
3861
Paul Elliott6f0e7202021-08-25 12:57:18 +01003862 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003863
Paul Elliott693bf312021-07-23 17:40:41 +01003864 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003865
3866 if( expected_status == PSA_SUCCESS )
3867 {
3868 /* Ensure we can still complete operation. */
3869
3870 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3871 additional_data->len ) );
3872
3873 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003874 output, output_size,
3875 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003876
Paul Elliott6f0e7202021-08-25 12:57:18 +01003877 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3878 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003879 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3880 }
3881
3882exit:
3883 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003884 mbedtls_free( output );
3885 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003886 mbedtls_free( nonce_buffer );
3887 psa_aead_abort( &operation );
3888 PSA_DONE( );
3889}
3890/* END_CASE */
3891
3892/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003893void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3894 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003895 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003896 data_t *nonce,
3897 data_t *additional_data,
3898 data_t *input_data,
3899 int expected_status_arg )
3900{
3901
3902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3903 psa_key_type_t key_type = key_type_arg;
3904 psa_algorithm_t alg = alg_arg;
3905 psa_aead_operation_t operation;
3906 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3907 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3908 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003909 unsigned char *output = NULL;
3910 unsigned char *ciphertext = NULL;
3911 size_t output_size = output_size_arg;
3912 size_t ciphertext_size = 0;
3913 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003914 size_t tag_length = 0;
3915 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3916
3917 PSA_ASSERT( psa_crypto_init( ) );
3918
3919 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3920 psa_set_key_algorithm( &attributes, alg );
3921 psa_set_key_type( &attributes, key_type );
3922
3923 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3924 &key ) );
3925
3926 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3927
Paul Elliottc6d11d02021-09-01 12:04:23 +01003928 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003929
Paul Elliottc6d11d02021-09-01 12:04:23 +01003930 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003931
Paul Elliottc6d11d02021-09-01 12:04:23 +01003932 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003933
3934 operation = psa_aead_operation_init( );
3935
3936 status = psa_aead_encrypt_setup( &operation, key, alg );
3937
3938 /* If the operation is not supported, just skip and not fail in case the
3939 * encryption involves a common limitation of cryptography hardwares and
3940 * an alternative implementation. */
3941 if( status == PSA_ERROR_NOT_SUPPORTED )
3942 {
3943 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3944 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3945 }
3946
3947 PSA_ASSERT( status );
3948
3949 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3950
3951 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3952 additional_data->len ) );
3953
3954 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003955 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003956
3957 TEST_EQUAL( status, expected_status );
3958
3959 if( expected_status == PSA_SUCCESS )
3960 {
3961 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003962 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3963 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003964 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3965 }
3966
3967exit:
3968 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003969 mbedtls_free( output );
3970 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003971 psa_aead_abort( &operation );
3972 PSA_DONE( );
3973}
3974/* END_CASE */
3975
Paul Elliott91b021e2021-07-23 18:52:31 +01003976/* BEGIN_CASE */
3977void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3978 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003979 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003980 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003981 data_t *nonce,
3982 data_t *additional_data,
3983 data_t *input_data,
3984 int expected_status_arg )
3985{
3986
3987 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3988 psa_key_type_t key_type = key_type_arg;
3989 psa_algorithm_t alg = alg_arg;
3990 psa_aead_operation_t operation;
3991 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3992 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3993 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003994 unsigned char *ciphertext = NULL;
3995 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01003996 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003997 size_t ciphertext_size = 0;
3998 size_t ciphertext_length = 0;
3999 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004000 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004001 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004002
4003 PSA_ASSERT( psa_crypto_init( ) );
4004
4005 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4006 psa_set_key_algorithm( &attributes, alg );
4007 psa_set_key_type( &attributes, key_type );
4008
4009 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4010 &key ) );
4011
4012 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4013
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004014 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004015
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004016 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004017
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004018 TEST_ASSERT( finish_ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott91b021e2021-07-23 18:52:31 +01004019
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004020 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004021
Paul Elliott719c1322021-09-13 18:27:22 +01004022 ASSERT_ALLOC( tag_buffer, tag_size );
4023
Paul Elliott91b021e2021-07-23 18:52:31 +01004024 operation = psa_aead_operation_init( );
4025
4026 status = psa_aead_encrypt_setup( &operation, key, alg );
4027
4028 /* If the operation is not supported, just skip and not fail in case the
4029 * encryption involves a common limitation of cryptography hardwares and
4030 * an alternative implementation. */
4031 if( status == PSA_ERROR_NOT_SUPPORTED )
4032 {
4033 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4034 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4035 }
4036
4037 PSA_ASSERT( status );
4038
4039 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4040
4041 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4042 additional_data->len ) );
4043
4044 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004045 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004046
4047 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004048 status = psa_aead_finish( &operation, finish_ciphertext,
4049 finish_ciphertext_size,
4050 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004051 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004052
4053 TEST_EQUAL( status, expected_status );
4054
4055exit:
4056 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004057 mbedtls_free( ciphertext );
4058 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004059 psa_aead_abort( &operation );
4060 PSA_DONE( );
4061}
4062/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004063
4064/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004065void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4066 int alg_arg,
4067 data_t *nonce,
4068 data_t *additional_data,
4069 data_t *input_data )
4070{
4071 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4072 psa_key_type_t key_type = key_type_arg;
4073 psa_algorithm_t alg = alg_arg;
4074 psa_aead_operation_t operation;
4075 unsigned char *output_data = NULL;
4076 unsigned char *final_data = NULL;
4077 size_t output_size = 0;
4078 size_t finish_output_size = 0;
4079 size_t output_length = 0;
4080 size_t key_bits = 0;
4081 size_t tag_length = 0;
4082 size_t tag_size = 0;
4083 size_t nonce_length = 0;
4084 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4085 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4086 size_t output_part_length = 0;
4087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4088
4089 PSA_ASSERT( psa_crypto_init( ) );
4090
4091 psa_set_key_usage_flags( & attributes,
4092 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4093 psa_set_key_algorithm( & attributes, alg );
4094 psa_set_key_type( & attributes, key_type );
4095
4096 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4097 &key ) );
4098
4099 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4100 key_bits = psa_get_key_bits( &attributes );
4101
4102 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4103
4104 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4105
4106 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4107
4108 ASSERT_ALLOC( output_data, output_size );
4109
4110 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4111
4112 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4113
4114 ASSERT_ALLOC( final_data, finish_output_size );
4115
4116 /* Test all operations error without calling setup first. */
4117
4118 operation = psa_aead_operation_init( );
4119
4120 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4121 PSA_ERROR_BAD_STATE );
4122
4123 psa_aead_abort( &operation );
4124
4125 operation = psa_aead_operation_init( );
4126
4127 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4128 PSA_AEAD_NONCE_MAX_SIZE,
4129 &nonce_length ),
4130 PSA_ERROR_BAD_STATE );
4131
4132 psa_aead_abort( &operation );
4133
Paul Elliott481be342021-07-16 17:38:47 +01004134 /* ------------------------------------------------------- */
4135
Paul Elliottc23a9a02021-06-21 18:32:46 +01004136 operation = psa_aead_operation_init( );
4137
4138 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4139 input_data->len ),
4140 PSA_ERROR_BAD_STATE );
4141
4142 psa_aead_abort( &operation );
4143
Paul Elliott481be342021-07-16 17:38:47 +01004144 /* ------------------------------------------------------- */
4145
Paul Elliottc23a9a02021-06-21 18:32:46 +01004146 operation = psa_aead_operation_init( );
4147
4148 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4149 additional_data->len ),
4150 PSA_ERROR_BAD_STATE );
4151
4152 psa_aead_abort( &operation );
4153
Paul Elliott481be342021-07-16 17:38:47 +01004154 /* ------------------------------------------------------- */
4155
Paul Elliottc23a9a02021-06-21 18:32:46 +01004156 operation = psa_aead_operation_init( );
4157
4158 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4159 input_data->len, output_data,
4160 output_size, &output_length ),
4161 PSA_ERROR_BAD_STATE );
4162
4163 psa_aead_abort( &operation );
4164
Paul Elliott481be342021-07-16 17:38:47 +01004165 /* ------------------------------------------------------- */
4166
Paul Elliottc23a9a02021-06-21 18:32:46 +01004167 operation = psa_aead_operation_init( );
4168
4169 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4170 finish_output_size,
4171 &output_part_length,
4172 tag_buffer, tag_length,
4173 &tag_size ),
4174 PSA_ERROR_BAD_STATE );
4175
4176 psa_aead_abort( &operation );
4177
Paul Elliott481be342021-07-16 17:38:47 +01004178 /* ------------------------------------------------------- */
4179
Paul Elliottc23a9a02021-06-21 18:32:46 +01004180 operation = psa_aead_operation_init( );
4181
4182 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4183 finish_output_size,
4184 &output_part_length,
4185 tag_buffer,
4186 tag_length ),
4187 PSA_ERROR_BAD_STATE );
4188
4189 psa_aead_abort( &operation );
4190
4191 /* Test for double setups. */
4192
4193 operation = psa_aead_operation_init( );
4194
4195 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4196
4197 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4198 PSA_ERROR_BAD_STATE );
4199
4200 psa_aead_abort( &operation );
4201
Paul Elliott481be342021-07-16 17:38:47 +01004202 /* ------------------------------------------------------- */
4203
Paul Elliottc23a9a02021-06-21 18:32:46 +01004204 operation = psa_aead_operation_init( );
4205
4206 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4207
4208 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4209 PSA_ERROR_BAD_STATE );
4210
4211 psa_aead_abort( &operation );
4212
Paul Elliott374a2be2021-07-16 17:53:40 +01004213 /* ------------------------------------------------------- */
4214
4215 operation = psa_aead_operation_init( );
4216
4217 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4218
4219 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4220 PSA_ERROR_BAD_STATE );
4221
4222 psa_aead_abort( &operation );
4223
4224 /* ------------------------------------------------------- */
4225
4226 operation = psa_aead_operation_init( );
4227
4228 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4229
4230 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4231 PSA_ERROR_BAD_STATE );
4232
4233 psa_aead_abort( &operation );
4234
Paul Elliottc23a9a02021-06-21 18:32:46 +01004235 /* Test for not setting a nonce. */
4236
4237 operation = psa_aead_operation_init( );
4238
4239 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4240
4241 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4242 additional_data->len ),
4243 PSA_ERROR_BAD_STATE );
4244
4245 psa_aead_abort( &operation );
4246
Paul Elliott7f628422021-09-01 12:08:29 +01004247 /* ------------------------------------------------------- */
4248
4249 operation = psa_aead_operation_init( );
4250
4251 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4252
4253 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4254 input_data->len, output_data,
4255 output_size, &output_length ),
4256 PSA_ERROR_BAD_STATE );
4257
4258 psa_aead_abort( &operation );
4259
Paul Elliottc23a9a02021-06-21 18:32:46 +01004260 /* Test for double setting nonce. */
4261
4262 operation = psa_aead_operation_init( );
4263
4264 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4265
4266 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4267
4268 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4269 PSA_ERROR_BAD_STATE );
4270
4271 psa_aead_abort( &operation );
4272
Paul Elliott374a2be2021-07-16 17:53:40 +01004273 /* Test for double generating nonce. */
4274
4275 operation = psa_aead_operation_init( );
4276
4277 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4278
4279 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4280 PSA_AEAD_NONCE_MAX_SIZE,
4281 &nonce_length ) );
4282
4283 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4284 PSA_AEAD_NONCE_MAX_SIZE,
4285 &nonce_length ),
4286 PSA_ERROR_BAD_STATE );
4287
4288
4289 psa_aead_abort( &operation );
4290
4291 /* Test for generate nonce then set and vice versa */
4292
4293 operation = psa_aead_operation_init( );
4294
4295 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4296
4297 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4298 PSA_AEAD_NONCE_MAX_SIZE,
4299 &nonce_length ) );
4300
4301 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4302 PSA_ERROR_BAD_STATE );
4303
4304 psa_aead_abort( &operation );
4305
4306 /* ------------------------------------------------------- */
4307
4308 operation = psa_aead_operation_init( );
4309
4310 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4311
4312 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4313
4314 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4315 PSA_AEAD_NONCE_MAX_SIZE,
4316 &nonce_length ),
4317 PSA_ERROR_BAD_STATE );
4318
4319 psa_aead_abort( &operation );
4320
Paul Elliott7220cae2021-06-22 17:25:57 +01004321 /* Test for generating nonce in decrypt setup. */
4322
4323 operation = psa_aead_operation_init( );
4324
4325 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4326
4327 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4328 PSA_AEAD_NONCE_MAX_SIZE,
4329 &nonce_length ),
4330 PSA_ERROR_BAD_STATE );
4331
4332 psa_aead_abort( &operation );
4333
Paul Elliottc23a9a02021-06-21 18:32:46 +01004334 /* Test for setting lengths twice. */
4335
4336 operation = psa_aead_operation_init( );
4337
4338 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4339
4340 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4341
4342 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4343 input_data->len ) );
4344
4345 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4346 input_data->len ),
4347 PSA_ERROR_BAD_STATE );
4348
4349 psa_aead_abort( &operation );
4350
4351 /* Test for setting lengths after already starting data. */
4352
4353 operation = psa_aead_operation_init( );
4354
4355 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4356
4357 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4358
4359 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4360 input_data->len, output_data,
4361 output_size, &output_length ) );
4362
4363 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4364 input_data->len ),
4365 PSA_ERROR_BAD_STATE );
4366
4367 psa_aead_abort( &operation );
4368
Paul Elliott243080c2021-07-21 19:01:17 +01004369 /* Test for not sending any additional data or data after setting non zero
4370 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004371
4372 operation = psa_aead_operation_init( );
4373
4374 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4375
4376 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4377
4378 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4379 input_data->len ) );
4380
4381 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4382 finish_output_size,
4383 &output_part_length,
4384 tag_buffer, tag_length,
4385 &tag_size ),
4386 PSA_ERROR_INVALID_ARGUMENT );
4387
4388 psa_aead_abort( &operation );
4389
Paul Elliott243080c2021-07-21 19:01:17 +01004390 /* Test for not sending any additional data or data after setting non-zero
4391 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004392
4393 operation = psa_aead_operation_init( );
4394
4395 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4396
4397 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4398
4399 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4400 input_data->len ) );
4401
4402 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4403 finish_output_size,
4404 &output_part_length,
4405 tag_buffer,
4406 tag_length ),
4407 PSA_ERROR_INVALID_ARGUMENT );
4408
4409 psa_aead_abort( &operation );
4410
Paul Elliott243080c2021-07-21 19:01:17 +01004411 /* Test for not sending any additional data after setting a non-zero length
4412 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004413
4414 operation = psa_aead_operation_init( );
4415
4416 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4417
4418 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4419
4420 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4421 input_data->len ) );
4422
4423 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4424 input_data->len, output_data,
4425 output_size, &output_length ),
4426 PSA_ERROR_INVALID_ARGUMENT );
4427
4428 psa_aead_abort( &operation );
4429
Paul Elliottb0450fe2021-09-01 15:06:26 +01004430 /* Test for sending too much additional data after setting lengths. */
4431
4432 operation = psa_aead_operation_init( );
4433
4434 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4435
4436 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4437
4438 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4439
4440
4441 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4442 additional_data->len ),
4443 PSA_ERROR_INVALID_ARGUMENT );
4444
4445 psa_aead_abort( &operation );
4446
4447 /* Test for sending too much data after setting lengths. */
4448
4449 operation = psa_aead_operation_init( );
4450
4451 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4452
4453 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4454
4455 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4456
4457 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4458 input_data->len, output_data,
4459 output_size, &output_length ),
4460 PSA_ERROR_INVALID_ARGUMENT );
4461
4462 psa_aead_abort( &operation );
4463
Paul Elliottc23a9a02021-06-21 18:32:46 +01004464 /* Test sending additional data after data. */
4465
4466 operation = psa_aead_operation_init( );
4467
4468 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4469
4470 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4471
4472 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4473 input_data->len, output_data,
4474 output_size, &output_length ) );
4475
4476 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4477 additional_data->len ),
4478 PSA_ERROR_BAD_STATE );
4479
4480 psa_aead_abort( &operation );
4481
Paul Elliott534d0b42021-06-22 19:15:20 +01004482 /* Test calling finish on decryption. */
4483
4484 operation = psa_aead_operation_init( );
4485
4486 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4487
4488 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4489
4490 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4491 finish_output_size,
4492 &output_part_length,
4493 tag_buffer, tag_length,
4494 &tag_size ),
4495 PSA_ERROR_BAD_STATE );
4496
4497 psa_aead_abort( &operation );
4498
4499 /* Test calling verify on encryption. */
4500
4501 operation = psa_aead_operation_init( );
4502
4503 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4504
4505 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4506
4507 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4508 finish_output_size,
4509 &output_part_length,
4510 tag_buffer,
4511 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004512 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004513
4514 psa_aead_abort( &operation );
4515
4516
Paul Elliottc23a9a02021-06-21 18:32:46 +01004517exit:
4518 psa_destroy_key( key );
4519 psa_aead_abort( &operation );
4520 mbedtls_free( output_data );
4521 mbedtls_free( final_data );
4522 PSA_DONE( );
4523}
4524/* END_CASE */
4525
4526/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004527void signature_size( int type_arg,
4528 int bits,
4529 int alg_arg,
4530 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004531{
4532 psa_key_type_t type = type_arg;
4533 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004534 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004535
Gilles Peskinefe11b722018-12-18 00:24:04 +01004536 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004537
Gilles Peskinee59236f2018-01-27 23:32:46 +01004538exit:
4539 ;
4540}
4541/* END_CASE */
4542
4543/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004544void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4545 int alg_arg, data_t *input_data,
4546 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004547{
Ronald Cron5425a212020-08-04 14:58:35 +02004548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004549 psa_key_type_t key_type = key_type_arg;
4550 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004551 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004552 unsigned char *signature = NULL;
4553 size_t signature_size;
4554 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004556
Gilles Peskine8817f612018-12-18 00:18:46 +01004557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004558
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004559 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004560 psa_set_key_algorithm( &attributes, alg );
4561 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004562
Gilles Peskine049c7532019-05-15 20:22:09 +02004563 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004564 &key ) );
4565 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004566 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004567
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004568 /* Allocate a buffer which has the size advertized by the
4569 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004570 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004571 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004572 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004573 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004574 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004575
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004576 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004577 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004578 input_data->x, input_data->len,
4579 signature, signature_size,
4580 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004581 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004582 ASSERT_COMPARE( output_data->x, output_data->len,
4583 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004584
4585exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004586 /*
4587 * Key attributes may have been returned by psa_get_key_attributes()
4588 * thus reset them as required.
4589 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004590 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004591
Ronald Cron5425a212020-08-04 14:58:35 +02004592 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004593 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004594 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004595}
4596/* END_CASE */
4597
4598/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004599void sign_hash_fail( int key_type_arg, data_t *key_data,
4600 int alg_arg, data_t *input_data,
4601 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004602{
Ronald Cron5425a212020-08-04 14:58:35 +02004603 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004604 psa_key_type_t key_type = key_type_arg;
4605 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004606 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004607 psa_status_t actual_status;
4608 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004609 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004610 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004612
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004613 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004614
Gilles Peskine8817f612018-12-18 00:18:46 +01004615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004616
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004618 psa_set_key_algorithm( &attributes, alg );
4619 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004620
Gilles Peskine049c7532019-05-15 20:22:09 +02004621 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004622 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004623
Ronald Cron5425a212020-08-04 14:58:35 +02004624 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004625 input_data->x, input_data->len,
4626 signature, signature_size,
4627 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004628 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004629 /* The value of *signature_length is unspecified on error, but
4630 * whatever it is, it should be less than signature_size, so that
4631 * if the caller tries to read *signature_length bytes without
4632 * checking the error code then they don't overflow a buffer. */
4633 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004634
4635exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004636 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004637 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004638 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004639 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004640}
4641/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004642
4643/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004644void sign_verify_hash( int key_type_arg, data_t *key_data,
4645 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004646{
Ronald Cron5425a212020-08-04 14:58:35 +02004647 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004648 psa_key_type_t key_type = key_type_arg;
4649 psa_algorithm_t alg = alg_arg;
4650 size_t key_bits;
4651 unsigned char *signature = NULL;
4652 size_t signature_size;
4653 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004655
Gilles Peskine8817f612018-12-18 00:18:46 +01004656 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004657
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004658 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004659 psa_set_key_algorithm( &attributes, alg );
4660 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004661
Gilles Peskine049c7532019-05-15 20:22:09 +02004662 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004663 &key ) );
4664 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004665 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004666
4667 /* Allocate a buffer which has the size advertized by the
4668 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004669 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004670 key_bits, alg );
4671 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004672 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004673 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004674
4675 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004676 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004677 input_data->x, input_data->len,
4678 signature, signature_size,
4679 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004680 /* Check that the signature length looks sensible. */
4681 TEST_ASSERT( signature_length <= signature_size );
4682 TEST_ASSERT( signature_length > 0 );
4683
4684 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004685 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004686 input_data->x, input_data->len,
4687 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004688
4689 if( input_data->len != 0 )
4690 {
4691 /* Flip a bit in the input and verify that the signature is now
4692 * detected as invalid. Flip a bit at the beginning, not at the end,
4693 * because ECDSA may ignore the last few bits of the input. */
4694 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004695 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004696 input_data->x, input_data->len,
4697 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004698 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004699 }
4700
4701exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004702 /*
4703 * Key attributes may have been returned by psa_get_key_attributes()
4704 * thus reset them as required.
4705 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004706 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004707
Ronald Cron5425a212020-08-04 14:58:35 +02004708 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004709 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004710 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004711}
4712/* END_CASE */
4713
4714/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004715void verify_hash( int key_type_arg, data_t *key_data,
4716 int alg_arg, data_t *hash_data,
4717 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004718{
Ronald Cron5425a212020-08-04 14:58:35 +02004719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004720 psa_key_type_t key_type = key_type_arg;
4721 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004723
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004724 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004725
Gilles Peskine8817f612018-12-18 00:18:46 +01004726 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004727
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004729 psa_set_key_algorithm( &attributes, alg );
4730 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004731
Gilles Peskine049c7532019-05-15 20:22:09 +02004732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004733 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004734
Ronald Cron5425a212020-08-04 14:58:35 +02004735 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004736 hash_data->x, hash_data->len,
4737 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004738
itayzafrir5c753392018-05-08 11:18:38 +03004739exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004740 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004741 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004742 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004743}
4744/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004745
4746/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004747void verify_hash_fail( int key_type_arg, data_t *key_data,
4748 int alg_arg, data_t *hash_data,
4749 data_t *signature_data,
4750 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004751{
Ronald Cron5425a212020-08-04 14:58:35 +02004752 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004753 psa_key_type_t key_type = key_type_arg;
4754 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004755 psa_status_t actual_status;
4756 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004758
Gilles Peskine8817f612018-12-18 00:18:46 +01004759 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004760
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004761 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004762 psa_set_key_algorithm( &attributes, alg );
4763 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004764
Gilles Peskine049c7532019-05-15 20:22:09 +02004765 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004766 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004767
Ronald Cron5425a212020-08-04 14:58:35 +02004768 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004769 hash_data->x, hash_data->len,
4770 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004771 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004772
4773exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004774 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004775 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004776 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004777}
4778/* END_CASE */
4779
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004780/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004781void sign_message_deterministic( int key_type_arg,
4782 data_t *key_data,
4783 int alg_arg,
4784 data_t *input_data,
4785 data_t *output_data )
4786{
4787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4788 psa_key_type_t key_type = key_type_arg;
4789 psa_algorithm_t alg = alg_arg;
4790 size_t key_bits;
4791 unsigned char *signature = NULL;
4792 size_t signature_size;
4793 size_t signature_length = 0xdeadbeef;
4794 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4795
4796 PSA_ASSERT( psa_crypto_init( ) );
4797
4798 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4799 psa_set_key_algorithm( &attributes, alg );
4800 psa_set_key_type( &attributes, key_type );
4801
4802 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4803 &key ) );
4804 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4805 key_bits = psa_get_key_bits( &attributes );
4806
4807 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4808 TEST_ASSERT( signature_size != 0 );
4809 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4810 ASSERT_ALLOC( signature, signature_size );
4811
4812 PSA_ASSERT( psa_sign_message( key, alg,
4813 input_data->x, input_data->len,
4814 signature, signature_size,
4815 &signature_length ) );
4816
4817 ASSERT_COMPARE( output_data->x, output_data->len,
4818 signature, signature_length );
4819
4820exit:
4821 psa_reset_key_attributes( &attributes );
4822
4823 psa_destroy_key( key );
4824 mbedtls_free( signature );
4825 PSA_DONE( );
4826
4827}
4828/* END_CASE */
4829
4830/* BEGIN_CASE */
4831void sign_message_fail( int key_type_arg,
4832 data_t *key_data,
4833 int alg_arg,
4834 data_t *input_data,
4835 int signature_size_arg,
4836 int expected_status_arg )
4837{
4838 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4839 psa_key_type_t key_type = key_type_arg;
4840 psa_algorithm_t alg = alg_arg;
4841 size_t signature_size = signature_size_arg;
4842 psa_status_t actual_status;
4843 psa_status_t expected_status = expected_status_arg;
4844 unsigned char *signature = NULL;
4845 size_t signature_length = 0xdeadbeef;
4846 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4847
4848 ASSERT_ALLOC( signature, signature_size );
4849
4850 PSA_ASSERT( psa_crypto_init( ) );
4851
4852 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4853 psa_set_key_algorithm( &attributes, alg );
4854 psa_set_key_type( &attributes, key_type );
4855
4856 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4857 &key ) );
4858
4859 actual_status = psa_sign_message( key, alg,
4860 input_data->x, input_data->len,
4861 signature, signature_size,
4862 &signature_length );
4863 TEST_EQUAL( actual_status, expected_status );
4864 /* The value of *signature_length is unspecified on error, but
4865 * whatever it is, it should be less than signature_size, so that
4866 * if the caller tries to read *signature_length bytes without
4867 * checking the error code then they don't overflow a buffer. */
4868 TEST_ASSERT( signature_length <= signature_size );
4869
4870exit:
4871 psa_reset_key_attributes( &attributes );
4872 psa_destroy_key( key );
4873 mbedtls_free( signature );
4874 PSA_DONE( );
4875}
4876/* END_CASE */
4877
4878/* BEGIN_CASE */
4879void sign_verify_message( int key_type_arg,
4880 data_t *key_data,
4881 int alg_arg,
4882 data_t *input_data )
4883{
4884 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4885 psa_key_type_t key_type = key_type_arg;
4886 psa_algorithm_t alg = alg_arg;
4887 size_t key_bits;
4888 unsigned char *signature = NULL;
4889 size_t signature_size;
4890 size_t signature_length = 0xdeadbeef;
4891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4892
4893 PSA_ASSERT( psa_crypto_init( ) );
4894
4895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4896 PSA_KEY_USAGE_VERIFY_MESSAGE );
4897 psa_set_key_algorithm( &attributes, alg );
4898 psa_set_key_type( &attributes, key_type );
4899
4900 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4901 &key ) );
4902 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4903 key_bits = psa_get_key_bits( &attributes );
4904
4905 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4906 TEST_ASSERT( signature_size != 0 );
4907 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4908 ASSERT_ALLOC( signature, signature_size );
4909
4910 PSA_ASSERT( psa_sign_message( key, alg,
4911 input_data->x, input_data->len,
4912 signature, signature_size,
4913 &signature_length ) );
4914 TEST_ASSERT( signature_length <= signature_size );
4915 TEST_ASSERT( signature_length > 0 );
4916
4917 PSA_ASSERT( psa_verify_message( key, alg,
4918 input_data->x, input_data->len,
4919 signature, signature_length ) );
4920
4921 if( input_data->len != 0 )
4922 {
4923 /* Flip a bit in the input and verify that the signature is now
4924 * detected as invalid. Flip a bit at the beginning, not at the end,
4925 * because ECDSA may ignore the last few bits of the input. */
4926 input_data->x[0] ^= 1;
4927 TEST_EQUAL( psa_verify_message( key, alg,
4928 input_data->x, input_data->len,
4929 signature, signature_length ),
4930 PSA_ERROR_INVALID_SIGNATURE );
4931 }
4932
4933exit:
4934 psa_reset_key_attributes( &attributes );
4935
4936 psa_destroy_key( key );
4937 mbedtls_free( signature );
4938 PSA_DONE( );
4939}
4940/* END_CASE */
4941
4942/* BEGIN_CASE */
4943void verify_message( int key_type_arg,
4944 data_t *key_data,
4945 int alg_arg,
4946 data_t *input_data,
4947 data_t *signature_data )
4948{
4949 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4950 psa_key_type_t key_type = key_type_arg;
4951 psa_algorithm_t alg = alg_arg;
4952 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4953
4954 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4955
4956 PSA_ASSERT( psa_crypto_init( ) );
4957
4958 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4959 psa_set_key_algorithm( &attributes, alg );
4960 psa_set_key_type( &attributes, key_type );
4961
4962 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4963 &key ) );
4964
4965 PSA_ASSERT( psa_verify_message( key, alg,
4966 input_data->x, input_data->len,
4967 signature_data->x, signature_data->len ) );
4968
4969exit:
4970 psa_reset_key_attributes( &attributes );
4971 psa_destroy_key( key );
4972 PSA_DONE( );
4973}
4974/* END_CASE */
4975
4976/* BEGIN_CASE */
4977void verify_message_fail( int key_type_arg,
4978 data_t *key_data,
4979 int alg_arg,
4980 data_t *hash_data,
4981 data_t *signature_data,
4982 int expected_status_arg )
4983{
4984 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4985 psa_key_type_t key_type = key_type_arg;
4986 psa_algorithm_t alg = alg_arg;
4987 psa_status_t actual_status;
4988 psa_status_t expected_status = expected_status_arg;
4989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4990
4991 PSA_ASSERT( psa_crypto_init( ) );
4992
4993 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4994 psa_set_key_algorithm( &attributes, alg );
4995 psa_set_key_type( &attributes, key_type );
4996
4997 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4998 &key ) );
4999
5000 actual_status = psa_verify_message( key, alg,
5001 hash_data->x, hash_data->len,
5002 signature_data->x,
5003 signature_data->len );
5004 TEST_EQUAL( actual_status, expected_status );
5005
5006exit:
5007 psa_reset_key_attributes( &attributes );
5008 psa_destroy_key( key );
5009 PSA_DONE( );
5010}
5011/* END_CASE */
5012
5013/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005014void asymmetric_encrypt( int key_type_arg,
5015 data_t *key_data,
5016 int alg_arg,
5017 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005018 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005019 int expected_output_length_arg,
5020 int expected_status_arg )
5021{
Ronald Cron5425a212020-08-04 14:58:35 +02005022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005023 psa_key_type_t key_type = key_type_arg;
5024 psa_algorithm_t alg = alg_arg;
5025 size_t expected_output_length = expected_output_length_arg;
5026 size_t key_bits;
5027 unsigned char *output = NULL;
5028 size_t output_size;
5029 size_t output_length = ~0;
5030 psa_status_t actual_status;
5031 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005033
Gilles Peskine8817f612018-12-18 00:18:46 +01005034 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005035
Gilles Peskine656896e2018-06-29 19:12:28 +02005036 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005037 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5038 psa_set_key_algorithm( &attributes, alg );
5039 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005040 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005041 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005042
5043 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005044 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005045 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005046
Gilles Peskine656896e2018-06-29 19:12:28 +02005047 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005048 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005049 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005050
5051 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005052 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005053 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005054 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005055 output, output_size,
5056 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005057 TEST_EQUAL( actual_status, expected_status );
5058 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005059
Gilles Peskine68428122018-06-30 18:42:41 +02005060 /* If the label is empty, the test framework puts a non-null pointer
5061 * in label->x. Test that a null pointer works as well. */
5062 if( label->len == 0 )
5063 {
5064 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005065 if( output_size != 0 )
5066 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005067 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005068 input_data->x, input_data->len,
5069 NULL, label->len,
5070 output, output_size,
5071 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005072 TEST_EQUAL( actual_status, expected_status );
5073 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005074 }
5075
Gilles Peskine656896e2018-06-29 19:12:28 +02005076exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005077 /*
5078 * Key attributes may have been returned by psa_get_key_attributes()
5079 * thus reset them as required.
5080 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005081 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005082
Ronald Cron5425a212020-08-04 14:58:35 +02005083 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005084 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005085 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005086}
5087/* END_CASE */
5088
5089/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005090void asymmetric_encrypt_decrypt( int key_type_arg,
5091 data_t *key_data,
5092 int alg_arg,
5093 data_t *input_data,
5094 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005095{
Ronald Cron5425a212020-08-04 14:58:35 +02005096 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005097 psa_key_type_t key_type = key_type_arg;
5098 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005099 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005100 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005101 size_t output_size;
5102 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005103 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005104 size_t output2_size;
5105 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005107
Gilles Peskine8817f612018-12-18 00:18:46 +01005108 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005109
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005110 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5111 psa_set_key_algorithm( &attributes, alg );
5112 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005113
Gilles Peskine049c7532019-05-15 20:22:09 +02005114 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005115 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005116
5117 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005118 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005119 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005120
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005121 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005122 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005123 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005124
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005125 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005126 TEST_ASSERT( output2_size <=
5127 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5128 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005129 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005130
Gilles Peskineeebd7382018-06-08 18:11:54 +02005131 /* We test encryption by checking that encrypt-then-decrypt gives back
5132 * the original plaintext because of the non-optional random
5133 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005134 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005135 input_data->x, input_data->len,
5136 label->x, label->len,
5137 output, output_size,
5138 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005139 /* We don't know what ciphertext length to expect, but check that
5140 * it looks sensible. */
5141 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005142
Ronald Cron5425a212020-08-04 14:58:35 +02005143 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005144 output, output_length,
5145 label->x, label->len,
5146 output2, output2_size,
5147 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005148 ASSERT_COMPARE( input_data->x, input_data->len,
5149 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005150
5151exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005152 /*
5153 * Key attributes may have been returned by psa_get_key_attributes()
5154 * thus reset them as required.
5155 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005156 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005157
Ronald Cron5425a212020-08-04 14:58:35 +02005158 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005159 mbedtls_free( output );
5160 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005161 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005162}
5163/* END_CASE */
5164
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005165/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005166void asymmetric_decrypt( int key_type_arg,
5167 data_t *key_data,
5168 int alg_arg,
5169 data_t *input_data,
5170 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005171 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005172{
Ronald Cron5425a212020-08-04 14:58:35 +02005173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005174 psa_key_type_t key_type = key_type_arg;
5175 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005176 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005177 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005178 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005179 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005181
Gilles Peskine8817f612018-12-18 00:18:46 +01005182 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005183
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005184 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5185 psa_set_key_algorithm( &attributes, alg );
5186 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005187
Gilles Peskine049c7532019-05-15 20:22:09 +02005188 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005189 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005190
gabor-mezei-armceface22021-01-21 12:26:17 +01005191 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5192 key_bits = psa_get_key_bits( &attributes );
5193
5194 /* Determine the maximum ciphertext length */
5195 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5196 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5197 ASSERT_ALLOC( output, output_size );
5198
Ronald Cron5425a212020-08-04 14:58:35 +02005199 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005200 input_data->x, input_data->len,
5201 label->x, label->len,
5202 output,
5203 output_size,
5204 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005205 ASSERT_COMPARE( expected_data->x, expected_data->len,
5206 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005207
Gilles Peskine68428122018-06-30 18:42:41 +02005208 /* If the label is empty, the test framework puts a non-null pointer
5209 * in label->x. Test that a null pointer works as well. */
5210 if( label->len == 0 )
5211 {
5212 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005213 if( output_size != 0 )
5214 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005215 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005216 input_data->x, input_data->len,
5217 NULL, label->len,
5218 output,
5219 output_size,
5220 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005221 ASSERT_COMPARE( expected_data->x, expected_data->len,
5222 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005223 }
5224
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005225exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005226 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005227 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005228 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005229 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005230}
5231/* END_CASE */
5232
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005233/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005234void asymmetric_decrypt_fail( int key_type_arg,
5235 data_t *key_data,
5236 int alg_arg,
5237 data_t *input_data,
5238 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005239 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005240 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005241{
Ronald Cron5425a212020-08-04 14:58:35 +02005242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005243 psa_key_type_t key_type = key_type_arg;
5244 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005245 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005246 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005247 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005248 psa_status_t actual_status;
5249 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005251
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005252 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005253
Gilles Peskine8817f612018-12-18 00:18:46 +01005254 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005255
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005256 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5257 psa_set_key_algorithm( &attributes, alg );
5258 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005259
Gilles Peskine049c7532019-05-15 20:22:09 +02005260 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005261 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005262
Ronald Cron5425a212020-08-04 14:58:35 +02005263 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005264 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005265 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005266 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005267 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005268 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005269 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005270
Gilles Peskine68428122018-06-30 18:42:41 +02005271 /* If the label is empty, the test framework puts a non-null pointer
5272 * in label->x. Test that a null pointer works as well. */
5273 if( label->len == 0 )
5274 {
5275 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005276 if( output_size != 0 )
5277 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005278 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005279 input_data->x, input_data->len,
5280 NULL, label->len,
5281 output, output_size,
5282 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005283 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005284 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005285 }
5286
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005287exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005288 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005289 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005290 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005291 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005292}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005293/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005294
5295/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005296void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005297{
5298 /* Test each valid way of initializing the object, except for `= {0}`, as
5299 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5300 * though it's OK by the C standard. We could test for this, but we'd need
5301 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005302 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005303 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5304 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5305 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005306
5307 memset( &zero, 0, sizeof( zero ) );
5308
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005310 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005311 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005312 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005313 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005314 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005315 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005316
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005317 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005318 PSA_ASSERT( psa_key_derivation_abort(&func) );
5319 PSA_ASSERT( psa_key_derivation_abort(&init) );
5320 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005321}
5322/* END_CASE */
5323
Janos Follath16de4a42019-06-13 16:32:24 +01005324/* BEGIN_CASE */
5325void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005326{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005327 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005328 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005329 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005330
Gilles Peskine8817f612018-12-18 00:18:46 +01005331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005332
Janos Follath16de4a42019-06-13 16:32:24 +01005333 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005334 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005335
5336exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005338 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005339}
5340/* END_CASE */
5341
Janos Follathaf3c2a02019-06-12 12:34:34 +01005342/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005343void derive_set_capacity( int alg_arg, int capacity_arg,
5344 int expected_status_arg )
5345{
5346 psa_algorithm_t alg = alg_arg;
5347 size_t capacity = capacity_arg;
5348 psa_status_t expected_status = expected_status_arg;
5349 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5350
5351 PSA_ASSERT( psa_crypto_init( ) );
5352
5353 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5354
5355 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5356 expected_status );
5357
5358exit:
5359 psa_key_derivation_abort( &operation );
5360 PSA_DONE( );
5361}
5362/* END_CASE */
5363
5364/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005365void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005366 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005367 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005368 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005369 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005370 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005371 int expected_status_arg3,
5372 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005373{
5374 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005375 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5376 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005377 psa_status_t expected_statuses[] = {expected_status_arg1,
5378 expected_status_arg2,
5379 expected_status_arg3};
5380 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005381 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5382 MBEDTLS_SVC_KEY_ID_INIT,
5383 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005384 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5385 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5386 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005387 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005388 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005389 psa_status_t expected_output_status = expected_output_status_arg;
5390 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005391
5392 PSA_ASSERT( psa_crypto_init( ) );
5393
5394 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5395 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005396
5397 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5398
5399 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5400 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005401 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005402 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005403 psa_set_key_type( &attributes, key_types[i] );
5404 PSA_ASSERT( psa_import_key( &attributes,
5405 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005406 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005407 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5408 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5409 {
5410 // When taking a private key as secret input, use key agreement
5411 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005412 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5413 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005414 expected_statuses[i] );
5415 }
5416 else
5417 {
5418 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005419 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005420 expected_statuses[i] );
5421 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005422 }
5423 else
5424 {
5425 TEST_EQUAL( psa_key_derivation_input_bytes(
5426 &operation, steps[i],
5427 inputs[i]->x, inputs[i]->len ),
5428 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005429 }
5430 }
5431
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005432 if( output_key_type != PSA_KEY_TYPE_NONE )
5433 {
5434 psa_reset_key_attributes( &attributes );
5435 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5436 psa_set_key_bits( &attributes, 8 );
5437 actual_output_status =
5438 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005439 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005440 }
5441 else
5442 {
5443 uint8_t buffer[1];
5444 actual_output_status =
5445 psa_key_derivation_output_bytes( &operation,
5446 buffer, sizeof( buffer ) );
5447 }
5448 TEST_EQUAL( actual_output_status, expected_output_status );
5449
Janos Follathaf3c2a02019-06-12 12:34:34 +01005450exit:
5451 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005452 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5453 psa_destroy_key( keys[i] );
5454 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005455 PSA_DONE( );
5456}
5457/* END_CASE */
5458
Janos Follathd958bb72019-07-03 15:02:16 +01005459/* BEGIN_CASE */
5460void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005461{
Janos Follathd958bb72019-07-03 15:02:16 +01005462 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005463 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005464 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005465 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005466 unsigned char input1[] = "Input 1";
5467 size_t input1_length = sizeof( input1 );
5468 unsigned char input2[] = "Input 2";
5469 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005470 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005471 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005472 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5473 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5474 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005475 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005476
Gilles Peskine8817f612018-12-18 00:18:46 +01005477 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005478
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005479 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5480 psa_set_key_algorithm( &attributes, alg );
5481 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005482
Gilles Peskine73676cb2019-05-15 20:15:10 +02005483 PSA_ASSERT( psa_import_key( &attributes,
5484 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005485 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005486
5487 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005488 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5489 input1, input1_length,
5490 input2, input2_length,
5491 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005492 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005493
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005494 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005495 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005496 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005497
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005498 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005499
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005500 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005501 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005502
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005503exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005504 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005505 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005506 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005507}
5508/* END_CASE */
5509
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005510/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005511void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005512{
5513 uint8_t output_buffer[16];
5514 size_t buffer_size = 16;
5515 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005516 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005517
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005518 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5519 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005520 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005521
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005523 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005524
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005526
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005527 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5528 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005529 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005530
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005531 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005532 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005533
5534exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005535 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005536}
5537/* END_CASE */
5538
5539/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005540void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005541 int step1_arg, data_t *input1,
5542 int step2_arg, data_t *input2,
5543 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005544 int requested_capacity_arg,
5545 data_t *expected_output1,
5546 data_t *expected_output2 )
5547{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005548 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005549 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5550 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005551 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5552 MBEDTLS_SVC_KEY_ID_INIT,
5553 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005554 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005556 uint8_t *expected_outputs[2] =
5557 {expected_output1->x, expected_output2->x};
5558 size_t output_sizes[2] =
5559 {expected_output1->len, expected_output2->len};
5560 size_t output_buffer_size = 0;
5561 uint8_t *output_buffer = NULL;
5562 size_t expected_capacity;
5563 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005565 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005566 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005567
5568 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5569 {
5570 if( output_sizes[i] > output_buffer_size )
5571 output_buffer_size = output_sizes[i];
5572 if( output_sizes[i] == 0 )
5573 expected_outputs[i] = NULL;
5574 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005575 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005576 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005577
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005578 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5579 psa_set_key_algorithm( &attributes, alg );
5580 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005581
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005582 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005583 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5584 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5585 requested_capacity ) );
5586 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005587 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005588 switch( steps[i] )
5589 {
5590 case 0:
5591 break;
5592 case PSA_KEY_DERIVATION_INPUT_SECRET:
5593 PSA_ASSERT( psa_import_key( &attributes,
5594 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005595 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005596
5597 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5598 {
5599 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5600 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5601 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5602 }
5603
Gilles Peskine1468da72019-05-29 17:35:49 +02005604 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005605 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005606 break;
5607 default:
5608 PSA_ASSERT( psa_key_derivation_input_bytes(
5609 &operation, steps[i],
5610 inputs[i]->x, inputs[i]->len ) );
5611 break;
5612 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005613 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005614
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005615 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005616 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005617 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005618 expected_capacity = requested_capacity;
5619
5620 /* Expansion phase. */
5621 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5622 {
5623 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005624 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005625 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005626 if( expected_capacity == 0 && output_sizes[i] == 0 )
5627 {
5628 /* Reading 0 bytes when 0 bytes are available can go either way. */
5629 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005630 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005631 continue;
5632 }
5633 else if( expected_capacity == 0 ||
5634 output_sizes[i] > expected_capacity )
5635 {
5636 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005637 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005638 expected_capacity = 0;
5639 continue;
5640 }
5641 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005642 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005643 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005644 ASSERT_COMPARE( output_buffer, output_sizes[i],
5645 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005646 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005647 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005648 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005649 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005650 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005651 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005652 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005653
5654exit:
5655 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005656 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005657 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5658 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005659 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005660}
5661/* END_CASE */
5662
5663/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005664void derive_full( int alg_arg,
5665 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005666 data_t *input1,
5667 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005668 int requested_capacity_arg )
5669{
Ronald Cron5425a212020-08-04 14:58:35 +02005670 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005671 psa_algorithm_t alg = alg_arg;
5672 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005673 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005674 unsigned char output_buffer[16];
5675 size_t expected_capacity = requested_capacity;
5676 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005678
Gilles Peskine8817f612018-12-18 00:18:46 +01005679 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005680
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005681 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5682 psa_set_key_algorithm( &attributes, alg );
5683 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005684
Gilles Peskine049c7532019-05-15 20:22:09 +02005685 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005686 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005687
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005688 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5689 input1->x, input1->len,
5690 input2->x, input2->len,
5691 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005692 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005693
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005694 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005695 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005696 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005697
5698 /* Expansion phase. */
5699 while( current_capacity > 0 )
5700 {
5701 size_t read_size = sizeof( output_buffer );
5702 if( read_size > current_capacity )
5703 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005704 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005705 output_buffer,
5706 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005707 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005708 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005709 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005710 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005711 }
5712
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005713 /* Check that the operation refuses to go over capacity. */
5714 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005715 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005716
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005717 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005718
5719exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005721 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005722 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005723}
5724/* END_CASE */
5725
Janos Follathe60c9052019-07-03 13:51:30 +01005726/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005727void derive_key_exercise( int alg_arg,
5728 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005729 data_t *input1,
5730 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005731 int derived_type_arg,
5732 int derived_bits_arg,
5733 int derived_usage_arg,
5734 int derived_alg_arg )
5735{
Ronald Cron5425a212020-08-04 14:58:35 +02005736 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5737 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005738 psa_algorithm_t alg = alg_arg;
5739 psa_key_type_t derived_type = derived_type_arg;
5740 size_t derived_bits = derived_bits_arg;
5741 psa_key_usage_t derived_usage = derived_usage_arg;
5742 psa_algorithm_t derived_alg = derived_alg_arg;
5743 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005746 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005747
Gilles Peskine8817f612018-12-18 00:18:46 +01005748 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005749
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005750 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5751 psa_set_key_algorithm( &attributes, alg );
5752 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005753 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005754 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005755
5756 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005757 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5758 input1->x, input1->len,
5759 input2->x, input2->len,
5760 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005761 goto exit;
5762
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005763 psa_set_key_usage_flags( &attributes, derived_usage );
5764 psa_set_key_algorithm( &attributes, derived_alg );
5765 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005766 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005767 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005768 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005769
5770 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005771 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005772 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5773 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005774
5775 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005776 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005777 goto exit;
5778
5779exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005780 /*
5781 * Key attributes may have been returned by psa_get_key_attributes()
5782 * thus reset them as required.
5783 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005784 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005785
5786 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005787 psa_destroy_key( base_key );
5788 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005789 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005790}
5791/* END_CASE */
5792
Janos Follath42fd8882019-07-03 14:17:09 +01005793/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005794void derive_key_export( int alg_arg,
5795 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005796 data_t *input1,
5797 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005798 int bytes1_arg,
5799 int bytes2_arg )
5800{
Ronald Cron5425a212020-08-04 14:58:35 +02005801 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5802 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005803 psa_algorithm_t alg = alg_arg;
5804 size_t bytes1 = bytes1_arg;
5805 size_t bytes2 = bytes2_arg;
5806 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005807 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005808 uint8_t *output_buffer = NULL;
5809 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005810 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5811 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005812 size_t length;
5813
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005814 ASSERT_ALLOC( output_buffer, capacity );
5815 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005816 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005817
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005818 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5819 psa_set_key_algorithm( &base_attributes, alg );
5820 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005821 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005822 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005823
5824 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005825 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5826 input1->x, input1->len,
5827 input2->x, input2->len,
5828 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005829 goto exit;
5830
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005831 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005832 output_buffer,
5833 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005834 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005835
5836 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005837 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5838 input1->x, input1->len,
5839 input2->x, input2->len,
5840 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005841 goto exit;
5842
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005843 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5844 psa_set_key_algorithm( &derived_attributes, 0 );
5845 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005846 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005847 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005848 &derived_key ) );
5849 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005850 export_buffer, bytes1,
5851 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005852 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005853 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005854 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005855 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005856 &derived_key ) );
5857 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005858 export_buffer + bytes1, bytes2,
5859 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005860 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005861
5862 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005863 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5864 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005865
5866exit:
5867 mbedtls_free( output_buffer );
5868 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005869 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005870 psa_destroy_key( base_key );
5871 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005872 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005873}
5874/* END_CASE */
5875
5876/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005877void derive_key( int alg_arg,
5878 data_t *key_data, data_t *input1, data_t *input2,
5879 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005880 int expected_status_arg,
5881 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005882{
Ronald Cron5425a212020-08-04 14:58:35 +02005883 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5884 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005885 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005886 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005887 size_t bits = bits_arg;
5888 psa_status_t expected_status = expected_status_arg;
5889 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5890 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5891 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5892
5893 PSA_ASSERT( psa_crypto_init( ) );
5894
5895 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5896 psa_set_key_algorithm( &base_attributes, alg );
5897 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5898 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005899 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005900
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005901 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5902 input1->x, input1->len,
5903 input2->x, input2->len,
5904 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005905 goto exit;
5906
5907 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5908 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005909 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005910 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005911
5912 psa_status_t status =
5913 psa_key_derivation_output_key( &derived_attributes,
5914 &operation,
5915 &derived_key );
5916 if( is_large_output > 0 )
5917 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5918 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005919
5920exit:
5921 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005922 psa_destroy_key( base_key );
5923 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005924 PSA_DONE( );
5925}
5926/* END_CASE */
5927
5928/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005929void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005930 int our_key_type_arg, int our_key_alg_arg,
5931 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005932 int expected_status_arg )
5933{
Ronald Cron5425a212020-08-04 14:58:35 +02005934 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005935 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005936 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005937 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005938 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005940 psa_status_t expected_status = expected_status_arg;
5941 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005942
Gilles Peskine8817f612018-12-18 00:18:46 +01005943 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005944
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005945 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005946 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005947 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005948 PSA_ASSERT( psa_import_key( &attributes,
5949 our_key_data->x, our_key_data->len,
5950 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005951
Gilles Peskine77f40d82019-04-11 21:27:06 +02005952 /* The tests currently include inputs that should fail at either step.
5953 * Test cases that fail at the setup step should be changed to call
5954 * key_derivation_setup instead, and this function should be renamed
5955 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005956 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005957 if( status == PSA_SUCCESS )
5958 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005959 TEST_EQUAL( psa_key_derivation_key_agreement(
5960 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5961 our_key,
5962 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005963 expected_status );
5964 }
5965 else
5966 {
5967 TEST_ASSERT( status == expected_status );
5968 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005969
5970exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005971 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005972 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005973 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005974}
5975/* END_CASE */
5976
5977/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005978void raw_key_agreement( int alg_arg,
5979 int our_key_type_arg, data_t *our_key_data,
5980 data_t *peer_key_data,
5981 data_t *expected_output )
5982{
Ronald Cron5425a212020-08-04 14:58:35 +02005983 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005984 psa_algorithm_t alg = alg_arg;
5985 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005987 unsigned char *output = NULL;
5988 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005989 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005990
5991 ASSERT_ALLOC( output, expected_output->len );
5992 PSA_ASSERT( psa_crypto_init( ) );
5993
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005994 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5995 psa_set_key_algorithm( &attributes, alg );
5996 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005997 PSA_ASSERT( psa_import_key( &attributes,
5998 our_key_data->x, our_key_data->len,
5999 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006000
gabor-mezei-armceface22021-01-21 12:26:17 +01006001 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6002 key_bits = psa_get_key_bits( &attributes );
6003
Gilles Peskinebe697d82019-05-16 18:00:41 +02006004 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6005 peer_key_data->x, peer_key_data->len,
6006 output, expected_output->len,
6007 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006008 ASSERT_COMPARE( output, output_length,
6009 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006010 TEST_ASSERT( output_length <=
6011 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6012 TEST_ASSERT( output_length <=
6013 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006014
6015exit:
6016 mbedtls_free( output );
6017 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006018 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006019}
6020/* END_CASE */
6021
6022/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006023void key_agreement_capacity( int alg_arg,
6024 int our_key_type_arg, data_t *our_key_data,
6025 data_t *peer_key_data,
6026 int expected_capacity_arg )
6027{
Ronald Cron5425a212020-08-04 14:58:35 +02006028 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006029 psa_algorithm_t alg = alg_arg;
6030 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006031 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006033 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006034 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006035
Gilles Peskine8817f612018-12-18 00:18:46 +01006036 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006037
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006038 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6039 psa_set_key_algorithm( &attributes, alg );
6040 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006041 PSA_ASSERT( psa_import_key( &attributes,
6042 our_key_data->x, our_key_data->len,
6043 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006044
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006045 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006046 PSA_ASSERT( psa_key_derivation_key_agreement(
6047 &operation,
6048 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6049 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006050 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6051 {
6052 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006053 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006054 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006055 NULL, 0 ) );
6056 }
Gilles Peskine59685592018-09-18 12:11:34 +02006057
Gilles Peskinebf491972018-10-25 22:36:12 +02006058 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006059 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006060 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006061 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006062
Gilles Peskinebf491972018-10-25 22:36:12 +02006063 /* Test the actual capacity by reading the output. */
6064 while( actual_capacity > sizeof( output ) )
6065 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006066 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006067 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006068 actual_capacity -= sizeof( output );
6069 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006070 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006071 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006072 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006073 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006074
Gilles Peskine59685592018-09-18 12:11:34 +02006075exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006076 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006077 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006078 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006079}
6080/* END_CASE */
6081
6082/* BEGIN_CASE */
6083void key_agreement_output( int alg_arg,
6084 int our_key_type_arg, data_t *our_key_data,
6085 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006086 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006087{
Ronald Cron5425a212020-08-04 14:58:35 +02006088 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006089 psa_algorithm_t alg = alg_arg;
6090 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006091 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006093 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006094
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006095 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6096 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006097
Gilles Peskine8817f612018-12-18 00:18:46 +01006098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006099
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6101 psa_set_key_algorithm( &attributes, alg );
6102 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006103 PSA_ASSERT( psa_import_key( &attributes,
6104 our_key_data->x, our_key_data->len,
6105 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006106
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006107 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006108 PSA_ASSERT( psa_key_derivation_key_agreement(
6109 &operation,
6110 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6111 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006112 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6113 {
6114 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006115 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006116 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006117 NULL, 0 ) );
6118 }
Gilles Peskine59685592018-09-18 12:11:34 +02006119
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006120 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006121 actual_output,
6122 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006123 ASSERT_COMPARE( actual_output, expected_output1->len,
6124 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006125 if( expected_output2->len != 0 )
6126 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006127 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006128 actual_output,
6129 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006130 ASSERT_COMPARE( actual_output, expected_output2->len,
6131 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006132 }
Gilles Peskine59685592018-09-18 12:11:34 +02006133
6134exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006135 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006136 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006137 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006138 mbedtls_free( actual_output );
6139}
6140/* END_CASE */
6141
6142/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006143void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006144{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006145 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006146 unsigned char *output = NULL;
6147 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006148 size_t i;
6149 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006150
Simon Butcher49f8e312020-03-03 15:51:50 +00006151 TEST_ASSERT( bytes_arg >= 0 );
6152
Gilles Peskine91892022021-02-08 19:50:26 +01006153 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006154 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006155
Gilles Peskine8817f612018-12-18 00:18:46 +01006156 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006157
Gilles Peskinea50d7392018-06-21 10:22:13 +02006158 /* Run several times, to ensure that every output byte will be
6159 * nonzero at least once with overwhelming probability
6160 * (2^(-8*number_of_runs)). */
6161 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006162 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006163 if( bytes != 0 )
6164 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006165 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006166
Gilles Peskinea50d7392018-06-21 10:22:13 +02006167 for( i = 0; i < bytes; i++ )
6168 {
6169 if( output[i] != 0 )
6170 ++changed[i];
6171 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006172 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006173
6174 /* Check that every byte was changed to nonzero at least once. This
6175 * validates that psa_generate_random is overwriting every byte of
6176 * the output buffer. */
6177 for( i = 0; i < bytes; i++ )
6178 {
6179 TEST_ASSERT( changed[i] != 0 );
6180 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006181
6182exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006183 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006184 mbedtls_free( output );
6185 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006186}
6187/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006188
6189/* BEGIN_CASE */
6190void generate_key( int type_arg,
6191 int bits_arg,
6192 int usage_arg,
6193 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006194 int expected_status_arg,
6195 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006196{
Ronald Cron5425a212020-08-04 14:58:35 +02006197 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006198 psa_key_type_t type = type_arg;
6199 psa_key_usage_t usage = usage_arg;
6200 size_t bits = bits_arg;
6201 psa_algorithm_t alg = alg_arg;
6202 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006204 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006205
Gilles Peskine8817f612018-12-18 00:18:46 +01006206 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006207
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006208 psa_set_key_usage_flags( &attributes, usage );
6209 psa_set_key_algorithm( &attributes, alg );
6210 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006211 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006212
6213 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006214 psa_status_t status = psa_generate_key( &attributes, &key );
6215
6216 if( is_large_key > 0 )
6217 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6218 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006219 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006220 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006221
6222 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006223 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006224 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6225 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006226
Gilles Peskine818ca122018-06-20 18:16:48 +02006227 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006228 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006229 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006230
6231exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006232 /*
6233 * Key attributes may have been returned by psa_get_key_attributes()
6234 * thus reset them as required.
6235 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006236 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006237
Ronald Cron5425a212020-08-04 14:58:35 +02006238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006239 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006240}
6241/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006242
Ronald Cronee414c72021-03-18 18:50:08 +01006243/* 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 +02006244void generate_key_rsa( int bits_arg,
6245 data_t *e_arg,
6246 int expected_status_arg )
6247{
Ronald Cron5425a212020-08-04 14:58:35 +02006248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006249 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006250 size_t bits = bits_arg;
6251 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6252 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6253 psa_status_t expected_status = expected_status_arg;
6254 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6255 uint8_t *exported = NULL;
6256 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006257 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006258 size_t exported_length = SIZE_MAX;
6259 uint8_t *e_read_buffer = NULL;
6260 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006261 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006262 size_t e_read_length = SIZE_MAX;
6263
6264 if( e_arg->len == 0 ||
6265 ( e_arg->len == 3 &&
6266 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6267 {
6268 is_default_public_exponent = 1;
6269 e_read_size = 0;
6270 }
6271 ASSERT_ALLOC( e_read_buffer, e_read_size );
6272 ASSERT_ALLOC( exported, exported_size );
6273
6274 PSA_ASSERT( psa_crypto_init( ) );
6275
6276 psa_set_key_usage_flags( &attributes, usage );
6277 psa_set_key_algorithm( &attributes, alg );
6278 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6279 e_arg->x, e_arg->len ) );
6280 psa_set_key_bits( &attributes, bits );
6281
6282 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006283 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006284 if( expected_status != PSA_SUCCESS )
6285 goto exit;
6286
6287 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006288 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006289 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6290 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6291 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6292 e_read_buffer, e_read_size,
6293 &e_read_length ) );
6294 if( is_default_public_exponent )
6295 TEST_EQUAL( e_read_length, 0 );
6296 else
6297 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6298
6299 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006300 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006301 goto exit;
6302
6303 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006304 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006305 exported, exported_size,
6306 &exported_length ) );
6307 {
6308 uint8_t *p = exported;
6309 uint8_t *end = exported + exported_length;
6310 size_t len;
6311 /* RSAPublicKey ::= SEQUENCE {
6312 * modulus INTEGER, -- n
6313 * publicExponent INTEGER } -- e
6314 */
6315 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006316 MBEDTLS_ASN1_SEQUENCE |
6317 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006318 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006319 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6320 MBEDTLS_ASN1_INTEGER ) );
6321 if( len >= 1 && p[0] == 0 )
6322 {
6323 ++p;
6324 --len;
6325 }
6326 if( e_arg->len == 0 )
6327 {
6328 TEST_EQUAL( len, 3 );
6329 TEST_EQUAL( p[0], 1 );
6330 TEST_EQUAL( p[1], 0 );
6331 TEST_EQUAL( p[2], 1 );
6332 }
6333 else
6334 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6335 }
6336
6337exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006338 /*
6339 * Key attributes may have been returned by psa_get_key_attributes() or
6340 * set by psa_set_key_domain_parameters() thus reset them as required.
6341 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006342 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006343
Ronald Cron5425a212020-08-04 14:58:35 +02006344 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006345 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006346 mbedtls_free( e_read_buffer );
6347 mbedtls_free( exported );
6348}
6349/* END_CASE */
6350
Darryl Greend49a4992018-06-18 17:27:26 +01006351/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006352void persistent_key_load_key_from_storage( data_t *data,
6353 int type_arg, int bits_arg,
6354 int usage_flags_arg, int alg_arg,
6355 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006356{
Ronald Cron71016a92020-08-28 19:01:50 +02006357 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006358 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006359 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6360 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006361 psa_key_type_t type = type_arg;
6362 size_t bits = bits_arg;
6363 psa_key_usage_t usage_flags = usage_flags_arg;
6364 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006365 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006366 unsigned char *first_export = NULL;
6367 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006368 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006369 size_t first_exported_length;
6370 size_t second_exported_length;
6371
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006372 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6373 {
6374 ASSERT_ALLOC( first_export, export_size );
6375 ASSERT_ALLOC( second_export, export_size );
6376 }
Darryl Greend49a4992018-06-18 17:27:26 +01006377
Gilles Peskine8817f612018-12-18 00:18:46 +01006378 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006379
Gilles Peskinec87af662019-05-15 16:12:22 +02006380 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006381 psa_set_key_usage_flags( &attributes, usage_flags );
6382 psa_set_key_algorithm( &attributes, alg );
6383 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006384 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006385
Darryl Green0c6575a2018-11-07 16:05:30 +00006386 switch( generation_method )
6387 {
6388 case IMPORT_KEY:
6389 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006390 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006391 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006392 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006393
Darryl Green0c6575a2018-11-07 16:05:30 +00006394 case GENERATE_KEY:
6395 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006396 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006397 break;
6398
6399 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006400#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006401 {
6402 /* Create base key */
6403 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6404 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6405 psa_set_key_usage_flags( &base_attributes,
6406 PSA_KEY_USAGE_DERIVE );
6407 psa_set_key_algorithm( &base_attributes, derive_alg );
6408 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006409 PSA_ASSERT( psa_import_key( &base_attributes,
6410 data->x, data->len,
6411 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006412 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006413 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006414 PSA_ASSERT( psa_key_derivation_input_key(
6415 &operation,
6416 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006417 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006418 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006419 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006420 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6421 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006422 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006423 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006424 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006425 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006426 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006427#else
6428 TEST_ASSUME( ! "KDF not supported in this configuration" );
6429#endif
6430 break;
6431
6432 default:
6433 TEST_ASSERT( ! "generation_method not implemented in test" );
6434 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006435 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006436 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006437
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006438 /* Export the key if permitted by the key policy. */
6439 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6440 {
Ronald Cron5425a212020-08-04 14:58:35 +02006441 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006442 first_export, export_size,
6443 &first_exported_length ) );
6444 if( generation_method == IMPORT_KEY )
6445 ASSERT_COMPARE( data->x, data->len,
6446 first_export, first_exported_length );
6447 }
Darryl Greend49a4992018-06-18 17:27:26 +01006448
6449 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006450 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006451 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006452 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006453
Darryl Greend49a4992018-06-18 17:27:26 +01006454 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006455 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006456 TEST_ASSERT( mbedtls_svc_key_id_equal(
6457 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006458 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6459 PSA_KEY_LIFETIME_PERSISTENT );
6460 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6461 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6462 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6463 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006464
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006465 /* Export the key again if permitted by the key policy. */
6466 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006467 {
Ronald Cron5425a212020-08-04 14:58:35 +02006468 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006469 second_export, export_size,
6470 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006471 ASSERT_COMPARE( first_export, first_exported_length,
6472 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006473 }
6474
6475 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006476 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006477 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006478
6479exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006480 /*
6481 * Key attributes may have been returned by psa_get_key_attributes()
6482 * thus reset them as required.
6483 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006484 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006485
Darryl Greend49a4992018-06-18 17:27:26 +01006486 mbedtls_free( first_export );
6487 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006488 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006489 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006490 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006491 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006492}
6493/* END_CASE */