blob: 543b2f6b247a6f474c91dc0e704836caa682bb27 [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,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100300 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100301 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100302 int data_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 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 Elliott6bfd0fb2021-09-15 14:15:55 +0100324 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100325 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;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100328 size_t ad_part_len = 0;
329 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100330 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
332 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
333
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100334 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100335 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 PSA_ASSERT( psa_crypto_init( ) );
338
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100339 if( is_encrypt )
340 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
341 else
342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
343
Paul Elliottd3f82412021-06-16 16:52:21 +0100344 psa_set_key_algorithm( &attributes, alg );
345 psa_set_key_type( &attributes, key_type );
346
347 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
348 &key ) );
349
350 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
351 key_bits = psa_get_key_bits( &attributes );
352
353 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 if( is_encrypt )
356 {
357 /* Tag gets written at end of buffer. */
358 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
359 ( input_data->len +
360 tag_length ) );
361 data_true_size = input_data->len;
362 }
363 else
364 {
365 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
366 ( input_data->len -
367 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100368
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100369 /* Do not want to attempt to decrypt tag. */
370 data_true_size = input_data->len - tag_length;
371 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100372
373 ASSERT_ALLOC( output_data, output_size );
374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 if( is_encrypt )
376 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100377 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
378 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 }
380 else
381 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100382 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
383 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100384 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100385
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100386 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100387
388 operation = psa_aead_operation_init( );
389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390
391 if( is_encrypt )
392 status = psa_aead_encrypt_setup( &operation, key, alg );
393 else
394 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100395
396 /* If the operation is not supported, just skip and not fail in case the
397 * encryption involves a common limitation of cryptography hardwares and
398 * an alternative implementation. */
399 if( status == PSA_ERROR_NOT_SUPPORTED )
400 {
401 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
402 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
403 }
404
405 PSA_ASSERT( status );
406
Paul Elliottebf91632021-07-22 17:54:42 +0100407 if( swap_set_functions )
Paul Elliottd3f82412021-06-16 16:52:21 +0100408 {
Paul Elliottebf91632021-07-22 17:54:42 +0100409 if( do_set_lengths )
410 {
411 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
412 data_true_size ) );
413 }
414
415 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
416 }
417 else
418 {
419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420
421 if( do_set_lengths )
422 {
423 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
424 data_true_size ) );
425 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100426 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100427
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100428 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100429 {
430 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100431 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100432 part_offset = 0;
433
434 while( part_offset < additional_data->len )
435 {
Paul Elliott329d5382021-07-22 17:10:45 +0100436 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100437 {
Paul Elliott329d5382021-07-22 17:10:45 +0100438 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
440 else
441 {
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100442 if( additional_data->len - part_offset < ad_part_len )
Paul Elliott329d5382021-07-22 17:10:45 +0100443 {
444 part_length = additional_data->len - part_offset;
445 }
446 else
447 {
448 part_length = ad_part_len;
449 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100450 }
451
452 PSA_ASSERT( psa_aead_update_ad( &operation,
453 additional_data->x + part_offset,
454 part_length ) );
455
456 part_offset += part_length;
457 }
458 }
459 else
460 {
461 /* Pass additional data in one go. */
462 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
463 additional_data->len ) );
464 }
465
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100466 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 {
468 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100469 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100470 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100471 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100472
473 ASSERT_ALLOC( part_data, part_data_size );
474
475 part_offset = 0;
476
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100477 while( part_offset < data_true_size )
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 {
Paul Elliott329d5382021-07-22 17:10:45 +0100479 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 {
Paul Elliott329d5382021-07-22 17:10:45 +0100481 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100482 }
483 else
484 {
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100485 if( ( data_true_size - part_offset ) < data_part_len )
Paul Elliott329d5382021-07-22 17:10:45 +0100486 {
487 part_length = ( data_true_size - part_offset );
488 }
489 else
490 {
491 part_length = data_part_len;
492 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100493 }
494
495 PSA_ASSERT( psa_aead_update( &operation,
496 ( input_data->x + part_offset ),
497 part_length, part_data,
498 part_data_size,
499 &output_part_length ) );
500
501 if( output_data && output_part_length )
502 {
503 memcpy( ( output_data + part_offset ), part_data,
504 output_part_length );
505 }
506
507 part_offset += part_length;
508 output_length += output_part_length;
509 }
510 }
511 else
512 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100513 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100515 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100516 output_size, &output_length ) );
517 }
518
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 if( is_encrypt )
520 PSA_ASSERT( psa_aead_finish( &operation, final_data,
521 final_output_size,
522 &output_part_length,
523 tag_buffer, tag_length,
524 &tag_size ) );
525 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100526 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100527 status = psa_aead_verify( &operation, final_data,
528 final_output_size,
529 &output_part_length,
530 ( input_data->x + data_true_size ),
531 tag_length );
532
533 if( status != PSA_SUCCESS )
534 {
535 if( !expect_valid_signature )
536 {
537 /* Expected failure. */
538 test_ok = 1;
539 goto exit;
540 }
541 else
542 PSA_ASSERT( status );
543 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100544 }
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546 if( output_data && output_part_length )
547 memcpy( ( output_data + output_length ), final_data,
548 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100549
550 output_length += output_part_length;
551
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100552
553 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
554 * should be exact.*/
555 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100556 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100557 TEST_EQUAL( tag_length, tag_size );
558
559 if( output_data && tag_length )
560 memcpy( ( output_data + output_length ), tag_buffer,
561 tag_length );
562
563 output_length += tag_length;
564
565 TEST_EQUAL( output_length,
566 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
567 input_data->len ) );
568 TEST_ASSERT( output_length <=
569 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
570 }
571 else
572 {
573 TEST_EQUAL( output_length,
574 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
575 input_data->len ) );
576 TEST_ASSERT( output_length <=
577 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100578 }
579
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100582 output_data, output_length );
583
Paul Elliottd3f82412021-06-16 16:52:21 +0100584
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100585 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100586
587exit:
588 psa_destroy_key( key );
589 psa_aead_abort( &operation );
590 mbedtls_free( output_data );
591 mbedtls_free( part_data );
592 mbedtls_free( final_data );
593 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100594
595 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100596}
597
Gilles Peskinee59236f2018-01-27 23:32:46 +0100598/* END_HEADER */
599
600/* BEGIN_DEPENDENCIES
601 * depends_on:MBEDTLS_PSA_CRYPTO_C
602 * END_DEPENDENCIES
603 */
604
605/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200606void static_checks( )
607{
608 size_t max_truncated_mac_size =
609 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
610
611 /* Check that the length for a truncated MAC always fits in the algorithm
612 * encoding. The shifted mask is the maximum truncated value. The
613 * untruncated algorithm may be one byte larger. */
614 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
615}
616/* END_CASE */
617
618/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200619void import_with_policy( int type_arg,
620 int usage_arg, int alg_arg,
621 int expected_status_arg )
622{
623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
624 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200626 psa_key_type_t type = type_arg;
627 psa_key_usage_t usage = usage_arg;
628 psa_algorithm_t alg = alg_arg;
629 psa_status_t expected_status = expected_status_arg;
630 const uint8_t key_material[16] = {0};
631 psa_status_t status;
632
633 PSA_ASSERT( psa_crypto_init( ) );
634
635 psa_set_key_type( &attributes, type );
636 psa_set_key_usage_flags( &attributes, usage );
637 psa_set_key_algorithm( &attributes, alg );
638
639 status = psa_import_key( &attributes,
640 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200641 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200642 TEST_EQUAL( status, expected_status );
643 if( status != PSA_SUCCESS )
644 goto exit;
645
Ronald Cron5425a212020-08-04 14:58:35 +0200646 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200647 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
648 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
649 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200650 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200651
Ronald Cron5425a212020-08-04 14:58:35 +0200652 PSA_ASSERT( psa_destroy_key( key ) );
653 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200654
655exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100656 /*
657 * Key attributes may have been returned by psa_get_key_attributes()
658 * thus reset them as required.
659 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200660 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100661
662 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200663 PSA_DONE( );
664}
665/* END_CASE */
666
667/* BEGIN_CASE */
668void import_with_data( data_t *data, int type_arg,
669 int attr_bits_arg,
670 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200671{
672 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
673 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200675 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200676 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200677 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679
Gilles Peskine8817f612018-12-18 00:18:46 +0100680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100681
Gilles Peskine4747d192019-04-17 15:05:45 +0200682 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200683 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200684
Ronald Cron5425a212020-08-04 14:58:35 +0200685 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100686 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200687 if( status != PSA_SUCCESS )
688 goto exit;
689
Ronald Cron5425a212020-08-04 14:58:35 +0200690 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200691 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200692 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200693 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200694 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200695
Ronald Cron5425a212020-08-04 14:58:35 +0200696 PSA_ASSERT( psa_destroy_key( key ) );
697 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698
699exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100700 /*
701 * Key attributes may have been returned by psa_get_key_attributes()
702 * thus reset them as required.
703 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200704 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100705
706 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200707 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708}
709/* END_CASE */
710
711/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200712void import_large_key( int type_arg, int byte_size_arg,
713 int expected_status_arg )
714{
715 psa_key_type_t type = type_arg;
716 size_t byte_size = byte_size_arg;
717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
718 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200720 psa_status_t status;
721 uint8_t *buffer = NULL;
722 size_t buffer_size = byte_size + 1;
723 size_t n;
724
Steven Cooreman69967ce2021-01-18 18:01:08 +0100725 /* Skip the test case if the target running the test cannot
726 * accomodate large keys due to heap size constraints */
727 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200728 memset( buffer, 'K', byte_size );
729
730 PSA_ASSERT( psa_crypto_init( ) );
731
732 /* Try importing the key */
733 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
734 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200735 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100736 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200737 TEST_EQUAL( status, expected_status );
738
739 if( status == PSA_SUCCESS )
740 {
Ronald Cron5425a212020-08-04 14:58:35 +0200741 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200742 TEST_EQUAL( psa_get_key_type( &attributes ), type );
743 TEST_EQUAL( psa_get_key_bits( &attributes ),
744 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200745 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200746 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200747 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 for( n = 0; n < byte_size; n++ )
749 TEST_EQUAL( buffer[n], 'K' );
750 for( n = byte_size; n < buffer_size; n++ )
751 TEST_EQUAL( buffer[n], 0 );
752 }
753
754exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100755 /*
756 * Key attributes may have been returned by psa_get_key_attributes()
757 * thus reset them as required.
758 */
759 psa_reset_key_attributes( &attributes );
760
Ronald Cron5425a212020-08-04 14:58:35 +0200761 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200762 PSA_DONE( );
763 mbedtls_free( buffer );
764}
765/* END_CASE */
766
767/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200768void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
769{
Ronald Cron5425a212020-08-04 14:58:35 +0200770 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200771 size_t bits = bits_arg;
772 psa_status_t expected_status = expected_status_arg;
773 psa_status_t status;
774 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200775 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200776 size_t buffer_size = /* Slight overapproximations */
777 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200778 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200779 unsigned char *p;
780 int ret;
781 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200783
Gilles Peskine8817f612018-12-18 00:18:46 +0100784 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200785 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200786
787 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
788 bits, keypair ) ) >= 0 );
789 length = ret;
790
791 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200792 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200793 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100794 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200795
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200796 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200797 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200798
799exit:
800 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200801 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200802}
803/* END_CASE */
804
805/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300806void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300807 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200808 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100809 int expected_bits,
810 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200811 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100812 int canonical_input )
813{
Ronald Cron5425a212020-08-04 14:58:35 +0200814 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200816 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200817 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 unsigned char *exported = NULL;
820 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100822 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200825 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826
Moran Pekercb088e72018-07-17 17:36:59 +0300827 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200828 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200830 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100831 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100832
Gilles Peskine4747d192019-04-17 15:05:45 +0200833 psa_set_key_usage_flags( &attributes, usage_arg );
834 psa_set_key_algorithm( &attributes, alg );
835 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700836
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200838 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100839
840 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200841 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200842 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
843 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200844 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845
846 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200847 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100848 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100849
850 /* The exported length must be set by psa_export_key() to a value between 0
851 * and export_size. On errors, the exported length must be 0. */
852 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
853 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
854 TEST_ASSERT( exported_length <= export_size );
855
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200856 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200857 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200859 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100860 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200862 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100863
Gilles Peskineea38a922021-02-13 00:05:16 +0100864 /* Run sanity checks on the exported key. For non-canonical inputs,
865 * this validates the canonical representations. For canonical inputs,
866 * this doesn't directly validate the implementation, but it still helps
867 * by cross-validating the test data with the sanity check code. */
868 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200869 goto exit;
870
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200872 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873 else
874 {
Ronald Cron5425a212020-08-04 14:58:35 +0200875 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200876 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200877 &key2 ) );
878 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 reexported,
880 export_size,
881 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200882 ASSERT_COMPARE( exported, exported_length,
883 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200884 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100886 TEST_ASSERT( exported_length <=
887 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
888 psa_get_key_bits( &got_attributes ) ) );
889 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890
891destroy:
892 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200893 PSA_ASSERT( psa_destroy_key( key ) );
894 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100895
896exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100897 /*
898 * Key attributes may have been returned by psa_get_key_attributes()
899 * thus reset them as required.
900 */
901 psa_reset_key_attributes( &got_attributes );
902
itayzafrir3e02b3b2018-06-12 17:06:52 +0300903 mbedtls_free( exported );
904 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200905 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100906}
907/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100908
Moran Pekerf709f4a2018-06-06 17:26:04 +0300909/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300910void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200911 int type_arg,
912 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100913 int export_size_delta,
914 int expected_export_status_arg,
915 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300916{
Ronald Cron5425a212020-08-04 14:58:35 +0200917 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200919 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200920 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300921 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300922 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100923 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100924 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200925 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300926
Gilles Peskine8817f612018-12-18 00:18:46 +0100927 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300928
Gilles Peskine4747d192019-04-17 15:05:45 +0200929 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
930 psa_set_key_algorithm( &attributes, alg );
931 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300932
933 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200934 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300935
Gilles Peskine49c25912018-10-29 15:15:31 +0100936 /* Export the public key */
937 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200938 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200939 exported, export_size,
940 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100941 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100942 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100943 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200944 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100945 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200946 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200947 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100948 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100949 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100950 TEST_ASSERT( expected_public_key->len <=
951 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
952 TEST_ASSERT( expected_public_key->len <=
953 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100954 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
955 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100956 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300957
958exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100959 /*
960 * Key attributes may have been returned by psa_get_key_attributes()
961 * thus reset them as required.
962 */
963 psa_reset_key_attributes( &attributes );
964
itayzafrir3e02b3b2018-06-12 17:06:52 +0300965 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200966 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200967 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300968}
969/* END_CASE */
970
Gilles Peskine20035e32018-02-03 22:44:14 +0100971/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200972void import_and_exercise_key( data_t *data,
973 int type_arg,
974 int bits_arg,
975 int alg_arg )
976{
Ronald Cron5425a212020-08-04 14:58:35 +0200977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200978 psa_key_type_t type = type_arg;
979 size_t bits = bits_arg;
980 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100981 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200983 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200984
Gilles Peskine8817f612018-12-18 00:18:46 +0100985 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200986
Gilles Peskine4747d192019-04-17 15:05:45 +0200987 psa_set_key_usage_flags( &attributes, usage );
988 psa_set_key_algorithm( &attributes, alg );
989 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200990
991 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200992 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993
994 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200995 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200996 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
997 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200998
999 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001000 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001001 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001002
Ronald Cron5425a212020-08-04 14:58:35 +02001003 PSA_ASSERT( psa_destroy_key( key ) );
1004 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001005
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001006exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001007 /*
1008 * Key attributes may have been returned by psa_get_key_attributes()
1009 * thus reset them as required.
1010 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001011 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001012
1013 psa_reset_key_attributes( &attributes );
1014 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001015 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001016}
1017/* END_CASE */
1018
1019/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001020void effective_key_attributes( int type_arg, int expected_type_arg,
1021 int bits_arg, int expected_bits_arg,
1022 int usage_arg, int expected_usage_arg,
1023 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001024{
Ronald Cron5425a212020-08-04 14:58:35 +02001025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001026 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001027 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001028 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001029 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001030 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001031 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001032 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001033 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001034 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001035
Gilles Peskine8817f612018-12-18 00:18:46 +01001036 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001037
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001038 psa_set_key_usage_flags( &attributes, usage );
1039 psa_set_key_algorithm( &attributes, alg );
1040 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001041 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001042
Ronald Cron5425a212020-08-04 14:58:35 +02001043 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001044 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001047 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1048 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1049 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1050 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001051
1052exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001053 /*
1054 * Key attributes may have been returned by psa_get_key_attributes()
1055 * thus reset them as required.
1056 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001057 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001058
1059 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001060 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001061}
1062/* END_CASE */
1063
1064/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001065void check_key_policy( int type_arg, int bits_arg,
1066 int usage_arg, int alg_arg )
1067{
1068 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1069 usage_arg, usage_arg, alg_arg, alg_arg );
1070 goto exit;
1071}
1072/* END_CASE */
1073
1074/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001075void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001076{
1077 /* Test each valid way of initializing the object, except for `= {0}`, as
1078 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1079 * though it's OK by the C standard. We could test for this, but we'd need
1080 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001081 psa_key_attributes_t func = psa_key_attributes_init( );
1082 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1083 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001084
1085 memset( &zero, 0, sizeof( zero ) );
1086
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001087 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1088 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1089 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001090
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001091 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1092 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1093 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1094
1095 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1096 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1097 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1098
1099 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1100 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1101 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1102
1103 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1104 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1105 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001106}
1107/* END_CASE */
1108
1109/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001110void mac_key_policy( int policy_usage,
1111 int policy_alg,
1112 int key_type,
1113 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001114 int exercise_alg,
1115 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001116{
Ronald Cron5425a212020-08-04 14:58:35 +02001117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001119 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001120 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001121 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001122 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001123
Gilles Peskine8817f612018-12-18 00:18:46 +01001124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001126 psa_set_key_usage_flags( &attributes, policy_usage );
1127 psa_set_key_algorithm( &attributes, policy_alg );
1128 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001129
Gilles Peskine049c7532019-05-15 20:22:09 +02001130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001131 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001132
Ronald Cron5425a212020-08-04 14:58:35 +02001133 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001134 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001135 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001136 else
1137 TEST_EQUAL( status, expected_status );
1138
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001140
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001142 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001143 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001144 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001145 else
1146 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001147
1148exit:
1149 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001150 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001151 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001152}
1153/* END_CASE */
1154
1155/* BEGIN_CASE */
1156void cipher_key_policy( int policy_usage,
1157 int policy_alg,
1158 int key_type,
1159 data_t *key_data,
1160 int exercise_alg )
1161{
Ronald Cron5425a212020-08-04 14:58:35 +02001162 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001164 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001165 psa_status_t status;
1166
Gilles Peskine8817f612018-12-18 00:18:46 +01001167 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001168
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001169 psa_set_key_usage_flags( &attributes, policy_usage );
1170 psa_set_key_algorithm( &attributes, policy_alg );
1171 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
Gilles Peskine049c7532019-05-15 20:22:09 +02001173 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001174 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001175
Ronald Cron5425a212020-08-04 14:58:35 +02001176 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177 if( policy_alg == exercise_alg &&
1178 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001179 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001180 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001181 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182 psa_cipher_abort( &operation );
1183
Ronald Cron5425a212020-08-04 14:58:35 +02001184 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001185 if( policy_alg == exercise_alg &&
1186 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001188 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001189 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001190
1191exit:
1192 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001193 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001194 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001195}
1196/* END_CASE */
1197
1198/* BEGIN_CASE */
1199void aead_key_policy( int policy_usage,
1200 int policy_alg,
1201 int key_type,
1202 data_t *key_data,
1203 int nonce_length_arg,
1204 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001205 int exercise_alg,
1206 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207{
Ronald Cron5425a212020-08-04 14:58:35 +02001208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001211 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001212 unsigned char nonce[16] = {0};
1213 size_t nonce_length = nonce_length_arg;
1214 unsigned char tag[16];
1215 size_t tag_length = tag_length_arg;
1216 size_t output_length;
1217
1218 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1219 TEST_ASSERT( tag_length <= sizeof( tag ) );
1220
Gilles Peskine8817f612018-12-18 00:18:46 +01001221 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001223 psa_set_key_usage_flags( &attributes, policy_usage );
1224 psa_set_key_algorithm( &attributes, policy_alg );
1225 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001226
Gilles Peskine049c7532019-05-15 20:22:09 +02001227 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001228 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001229
Ronald Cron5425a212020-08-04 14:58:35 +02001230 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001231 nonce, nonce_length,
1232 NULL, 0,
1233 NULL, 0,
1234 tag, tag_length,
1235 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001236 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1237 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001238 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001239 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001240
1241 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001242 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001243 nonce, nonce_length,
1244 NULL, 0,
1245 tag, tag_length,
1246 NULL, 0,
1247 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001248 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1249 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1250 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001251 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001253 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001254
1255exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001256 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001257 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001258}
1259/* END_CASE */
1260
1261/* BEGIN_CASE */
1262void asymmetric_encryption_key_policy( int policy_usage,
1263 int policy_alg,
1264 int key_type,
1265 data_t *key_data,
1266 int exercise_alg )
1267{
Ronald Cron5425a212020-08-04 14:58:35 +02001268 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001270 psa_status_t status;
1271 size_t key_bits;
1272 size_t buffer_length;
1273 unsigned char *buffer = NULL;
1274 size_t output_length;
1275
Gilles Peskine8817f612018-12-18 00:18:46 +01001276 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001278 psa_set_key_usage_flags( &attributes, policy_usage );
1279 psa_set_key_algorithm( &attributes, policy_alg );
1280 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001281
Gilles Peskine049c7532019-05-15 20:22:09 +02001282 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001283 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001284
Ronald Cron5425a212020-08-04 14:58:35 +02001285 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001286 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1288 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001289 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001290
Ronald Cron5425a212020-08-04 14:58:35 +02001291 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001292 NULL, 0,
1293 NULL, 0,
1294 buffer, buffer_length,
1295 &output_length );
1296 if( policy_alg == exercise_alg &&
1297 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001298 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001299 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001300 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001301
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001302 if( buffer_length != 0 )
1303 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001304 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001305 buffer, buffer_length,
1306 NULL, 0,
1307 buffer, buffer_length,
1308 &output_length );
1309 if( policy_alg == exercise_alg &&
1310 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001311 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001312 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001313 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001314
1315exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001316 /*
1317 * Key attributes may have been returned by psa_get_key_attributes()
1318 * thus reset them as required.
1319 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001320 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001321
1322 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001323 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001324 mbedtls_free( buffer );
1325}
1326/* END_CASE */
1327
1328/* BEGIN_CASE */
1329void asymmetric_signature_key_policy( int policy_usage,
1330 int policy_alg,
1331 int key_type,
1332 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001333 int exercise_alg,
1334 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001335{
Ronald Cron5425a212020-08-04 14:58:35 +02001336 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001338 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001339 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1340 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1341 * compatible with the policy and `payload_length_arg` is supposed to be
1342 * a valid input length to sign. If `payload_length_arg <= 0`,
1343 * `exercise_alg` is supposed to be forbidden by the policy. */
1344 int compatible_alg = payload_length_arg > 0;
1345 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001346 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001347 size_t signature_length;
1348
Gilles Peskine8817f612018-12-18 00:18:46 +01001349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001350
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001351 psa_set_key_usage_flags( &attributes, policy_usage );
1352 psa_set_key_algorithm( &attributes, policy_alg );
1353 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354
Gilles Peskine049c7532019-05-15 20:22:09 +02001355 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001356 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001357
Ronald Cron5425a212020-08-04 14:58:35 +02001358 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001359 payload, payload_length,
1360 signature, sizeof( signature ),
1361 &signature_length );
1362 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001363 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001365 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001366
1367 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001368 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001369 payload, payload_length,
1370 signature, sizeof( signature ) );
1371 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001372 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001373 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001374 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001375
1376exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001377 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001378 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001379}
1380/* END_CASE */
1381
Janos Follathba3fab92019-06-11 14:50:16 +01001382/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001383void derive_key_policy( int policy_usage,
1384 int policy_alg,
1385 int key_type,
1386 data_t *key_data,
1387 int exercise_alg )
1388{
Ronald Cron5425a212020-08-04 14:58:35 +02001389 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001390 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001391 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001392 psa_status_t status;
1393
Gilles Peskine8817f612018-12-18 00:18:46 +01001394 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001395
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001396 psa_set_key_usage_flags( &attributes, policy_usage );
1397 psa_set_key_algorithm( &attributes, policy_alg );
1398 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001399
Gilles Peskine049c7532019-05-15 20:22:09 +02001400 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001401 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001402
Janos Follathba3fab92019-06-11 14:50:16 +01001403 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1404
1405 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1406 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001407 {
Janos Follathba3fab92019-06-11 14:50:16 +01001408 PSA_ASSERT( psa_key_derivation_input_bytes(
1409 &operation,
1410 PSA_KEY_DERIVATION_INPUT_SEED,
1411 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001412 }
Janos Follathba3fab92019-06-11 14:50:16 +01001413
1414 status = psa_key_derivation_input_key( &operation,
1415 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001416 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001417
Gilles Peskineea0fb492018-07-12 17:17:20 +02001418 if( policy_alg == exercise_alg &&
1419 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001420 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001421 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001422 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001423
1424exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001425 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001426 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001427 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001428}
1429/* END_CASE */
1430
1431/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001432void agreement_key_policy( int policy_usage,
1433 int policy_alg,
1434 int key_type_arg,
1435 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001436 int exercise_alg,
1437 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001438{
Ronald Cron5425a212020-08-04 14:58:35 +02001439 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001440 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001442 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001443 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001444 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001445
Gilles Peskine8817f612018-12-18 00:18:46 +01001446 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001447
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001448 psa_set_key_usage_flags( &attributes, policy_usage );
1449 psa_set_key_algorithm( &attributes, policy_alg );
1450 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001451
Gilles Peskine049c7532019-05-15 20:22:09 +02001452 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001453 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001454
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001455 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001456 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001457
Steven Cooremance48e852020-10-05 16:02:45 +02001458 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001459
1460exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001461 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001462 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001463 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001464}
1465/* END_CASE */
1466
1467/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001468void key_policy_alg2( int key_type_arg, data_t *key_data,
1469 int usage_arg, int alg_arg, int alg2_arg )
1470{
Ronald Cron5425a212020-08-04 14:58:35 +02001471 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001472 psa_key_type_t key_type = key_type_arg;
1473 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1474 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1475 psa_key_usage_t usage = usage_arg;
1476 psa_algorithm_t alg = alg_arg;
1477 psa_algorithm_t alg2 = alg2_arg;
1478
1479 PSA_ASSERT( psa_crypto_init( ) );
1480
1481 psa_set_key_usage_flags( &attributes, usage );
1482 psa_set_key_algorithm( &attributes, alg );
1483 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1484 psa_set_key_type( &attributes, key_type );
1485 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001486 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001487
Ronald Cron5425a212020-08-04 14:58:35 +02001488 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001489 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1490 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1491 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1492
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001493 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001494 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001495 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001496 goto exit;
1497
1498exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001499 /*
1500 * Key attributes may have been returned by psa_get_key_attributes()
1501 * thus reset them as required.
1502 */
1503 psa_reset_key_attributes( &got_attributes );
1504
Ronald Cron5425a212020-08-04 14:58:35 +02001505 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001506 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001507}
1508/* END_CASE */
1509
1510/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001511void raw_agreement_key_policy( int policy_usage,
1512 int policy_alg,
1513 int key_type_arg,
1514 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001515 int exercise_alg,
1516 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517{
Ronald Cron5425a212020-08-04 14:58:35 +02001518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001520 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001521 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001522 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001523 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001524
1525 PSA_ASSERT( psa_crypto_init( ) );
1526
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001527 psa_set_key_usage_flags( &attributes, policy_usage );
1528 psa_set_key_algorithm( &attributes, policy_alg );
1529 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001530
Gilles Peskine049c7532019-05-15 20:22:09 +02001531 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001532 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001533
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001534 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001535
Steven Cooremance48e852020-10-05 16:02:45 +02001536 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001537
1538exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001539 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001540 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001541 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001542}
1543/* END_CASE */
1544
1545/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001546void copy_success( int source_usage_arg,
1547 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001548 int type_arg, data_t *material,
1549 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001550 int target_usage_arg,
1551 int target_alg_arg, int target_alg2_arg,
1552 int expected_usage_arg,
1553 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001554{
Gilles Peskineca25db92019-04-19 11:43:08 +02001555 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1556 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001557 psa_key_usage_t expected_usage = expected_usage_arg;
1558 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001559 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001560 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1561 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001562 uint8_t *export_buffer = NULL;
1563
Gilles Peskine57ab7212019-01-28 13:03:09 +01001564 PSA_ASSERT( psa_crypto_init( ) );
1565
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 /* Prepare the source key. */
1567 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1568 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001569 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001570 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001571 PSA_ASSERT( psa_import_key( &source_attributes,
1572 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001573 &source_key ) );
1574 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001575
Gilles Peskineca25db92019-04-19 11:43:08 +02001576 /* Prepare the target attributes. */
1577 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001578 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001579 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001580 /* Set volatile lifetime to reset the key identifier to 0. */
1581 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1582 }
1583
Gilles Peskineca25db92019-04-19 11:43:08 +02001584 if( target_usage_arg != -1 )
1585 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1586 if( target_alg_arg != -1 )
1587 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001588 if( target_alg2_arg != -1 )
1589 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001590
1591 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001592 PSA_ASSERT( psa_copy_key( source_key,
1593 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001594
1595 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001596 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001597
1598 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001599 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001600 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1601 psa_get_key_type( &target_attributes ) );
1602 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1603 psa_get_key_bits( &target_attributes ) );
1604 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1605 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001606 TEST_EQUAL( expected_alg2,
1607 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001608 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1609 {
1610 size_t length;
1611 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001612 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001613 material->len, &length ) );
1614 ASSERT_COMPARE( material->x, material->len,
1615 export_buffer, length );
1616 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001617
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001618 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001619 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001620 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001621 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001622
Ronald Cron5425a212020-08-04 14:58:35 +02001623 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001624
1625exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001626 /*
1627 * Source and target key attributes may have been returned by
1628 * psa_get_key_attributes() thus reset them as required.
1629 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001630 psa_reset_key_attributes( &source_attributes );
1631 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001632
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001633 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001634 mbedtls_free( export_buffer );
1635}
1636/* END_CASE */
1637
1638/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001639void copy_fail( int source_usage_arg,
1640 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001641 int type_arg, data_t *material,
1642 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001643 int target_usage_arg,
1644 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001645 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001646 int expected_status_arg )
1647{
1648 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1649 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001650 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1651 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001652 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001653
1654 PSA_ASSERT( psa_crypto_init( ) );
1655
1656 /* Prepare the source key. */
1657 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1658 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001659 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001660 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001661 PSA_ASSERT( psa_import_key( &source_attributes,
1662 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001663 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001664
1665 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001666 psa_set_key_id( &target_attributes, key_id );
1667 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001668 psa_set_key_type( &target_attributes, target_type_arg );
1669 psa_set_key_bits( &target_attributes, target_bits_arg );
1670 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1671 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001672 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001673
1674 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001675 TEST_EQUAL( psa_copy_key( source_key,
1676 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001677 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001678
Ronald Cron5425a212020-08-04 14:58:35 +02001679 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001680
Gilles Peskine4a644642019-05-03 17:14:08 +02001681exit:
1682 psa_reset_key_attributes( &source_attributes );
1683 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001684 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001685}
1686/* END_CASE */
1687
1688/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001689void hash_operation_init( )
1690{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001691 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001692 /* Test each valid way of initializing the object, except for `= {0}`, as
1693 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1694 * though it's OK by the C standard. We could test for this, but we'd need
1695 * to supress the Clang warning for the test. */
1696 psa_hash_operation_t func = psa_hash_operation_init( );
1697 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1698 psa_hash_operation_t zero;
1699
1700 memset( &zero, 0, sizeof( zero ) );
1701
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001702 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001703 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1704 PSA_ERROR_BAD_STATE );
1705 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1706 PSA_ERROR_BAD_STATE );
1707 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1708 PSA_ERROR_BAD_STATE );
1709
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001710 /* A default hash operation should be abortable without error. */
1711 PSA_ASSERT( psa_hash_abort( &func ) );
1712 PSA_ASSERT( psa_hash_abort( &init ) );
1713 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001714}
1715/* END_CASE */
1716
1717/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001718void hash_setup( int alg_arg,
1719 int expected_status_arg )
1720{
1721 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001722 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001723 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001724 psa_status_t status;
1725
Gilles Peskine8817f612018-12-18 00:18:46 +01001726 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001727
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001728 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001729 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001730
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001731 /* Whether setup succeeded or failed, abort must succeed. */
1732 PSA_ASSERT( psa_hash_abort( &operation ) );
1733
1734 /* If setup failed, reproduce the failure, so as to
1735 * test the resulting state of the operation object. */
1736 if( status != PSA_SUCCESS )
1737 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1738
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001739 /* Now the operation object should be reusable. */
1740#if defined(KNOWN_SUPPORTED_HASH_ALG)
1741 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1742 PSA_ASSERT( psa_hash_abort( &operation ) );
1743#endif
1744
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001745exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001746 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001747}
1748/* END_CASE */
1749
1750/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001751void hash_compute_fail( int alg_arg, data_t *input,
1752 int output_size_arg, int expected_status_arg )
1753{
1754 psa_algorithm_t alg = alg_arg;
1755 uint8_t *output = NULL;
1756 size_t output_size = output_size_arg;
1757 size_t output_length = INVALID_EXPORT_LENGTH;
1758 psa_status_t expected_status = expected_status_arg;
1759 psa_status_t status;
1760
1761 ASSERT_ALLOC( output, output_size );
1762
1763 PSA_ASSERT( psa_crypto_init( ) );
1764
1765 status = psa_hash_compute( alg, input->x, input->len,
1766 output, output_size, &output_length );
1767 TEST_EQUAL( status, expected_status );
1768 TEST_ASSERT( output_length <= output_size );
1769
1770exit:
1771 mbedtls_free( output );
1772 PSA_DONE( );
1773}
1774/* END_CASE */
1775
1776/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001777void hash_compare_fail( int alg_arg, data_t *input,
1778 data_t *reference_hash,
1779 int expected_status_arg )
1780{
1781 psa_algorithm_t alg = alg_arg;
1782 psa_status_t expected_status = expected_status_arg;
1783 psa_status_t status;
1784
1785 PSA_ASSERT( psa_crypto_init( ) );
1786
1787 status = psa_hash_compare( alg, input->x, input->len,
1788 reference_hash->x, reference_hash->len );
1789 TEST_EQUAL( status, expected_status );
1790
1791exit:
1792 PSA_DONE( );
1793}
1794/* END_CASE */
1795
1796/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001797void hash_compute_compare( int alg_arg, data_t *input,
1798 data_t *expected_output )
1799{
1800 psa_algorithm_t alg = alg_arg;
1801 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1802 size_t output_length = INVALID_EXPORT_LENGTH;
1803 size_t i;
1804
1805 PSA_ASSERT( psa_crypto_init( ) );
1806
1807 /* Compute with tight buffer */
1808 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001809 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001810 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001811 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001812 ASSERT_COMPARE( output, output_length,
1813 expected_output->x, expected_output->len );
1814
1815 /* Compute with larger buffer */
1816 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1817 output, sizeof( output ),
1818 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001819 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001820 ASSERT_COMPARE( output, output_length,
1821 expected_output->x, expected_output->len );
1822
1823 /* Compare with correct hash */
1824 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1825 output, output_length ) );
1826
1827 /* Compare with trailing garbage */
1828 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1829 output, output_length + 1 ),
1830 PSA_ERROR_INVALID_SIGNATURE );
1831
1832 /* Compare with truncated hash */
1833 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1834 output, output_length - 1 ),
1835 PSA_ERROR_INVALID_SIGNATURE );
1836
1837 /* Compare with corrupted value */
1838 for( i = 0; i < output_length; i++ )
1839 {
Chris Jones9634bb12021-01-20 15:56:42 +00001840 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001841 output[i] ^= 1;
1842 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1843 output, output_length ),
1844 PSA_ERROR_INVALID_SIGNATURE );
1845 output[i] ^= 1;
1846 }
1847
1848exit:
1849 PSA_DONE( );
1850}
1851/* END_CASE */
1852
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001853/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001854void hash_bad_order( )
1855{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001856 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001857 unsigned char input[] = "";
1858 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001859 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001860 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1861 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1862 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001863 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001864 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001865 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001866
Gilles Peskine8817f612018-12-18 00:18:46 +01001867 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001868
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001869 /* Call setup twice in a row. */
1870 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1871 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1872 PSA_ERROR_BAD_STATE );
1873 PSA_ASSERT( psa_hash_abort( &operation ) );
1874
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001875 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001876 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001877 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001878 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001879
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001880 /* Call update after finish. */
1881 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1882 PSA_ASSERT( psa_hash_finish( &operation,
1883 hash, sizeof( hash ), &hash_len ) );
1884 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001885 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001886 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001887
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001888 /* Call verify without calling setup beforehand. */
1889 TEST_EQUAL( psa_hash_verify( &operation,
1890 valid_hash, sizeof( valid_hash ) ),
1891 PSA_ERROR_BAD_STATE );
1892 PSA_ASSERT( psa_hash_abort( &operation ) );
1893
1894 /* Call verify after finish. */
1895 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1896 PSA_ASSERT( psa_hash_finish( &operation,
1897 hash, sizeof( hash ), &hash_len ) );
1898 TEST_EQUAL( psa_hash_verify( &operation,
1899 valid_hash, sizeof( valid_hash ) ),
1900 PSA_ERROR_BAD_STATE );
1901 PSA_ASSERT( psa_hash_abort( &operation ) );
1902
1903 /* Call verify twice in a row. */
1904 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1905 PSA_ASSERT( psa_hash_verify( &operation,
1906 valid_hash, sizeof( valid_hash ) ) );
1907 TEST_EQUAL( psa_hash_verify( &operation,
1908 valid_hash, sizeof( valid_hash ) ),
1909 PSA_ERROR_BAD_STATE );
1910 PSA_ASSERT( psa_hash_abort( &operation ) );
1911
1912 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001913 TEST_EQUAL( psa_hash_finish( &operation,
1914 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001915 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001916 PSA_ASSERT( psa_hash_abort( &operation ) );
1917
1918 /* Call finish twice in a row. */
1919 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1920 PSA_ASSERT( psa_hash_finish( &operation,
1921 hash, sizeof( hash ), &hash_len ) );
1922 TEST_EQUAL( psa_hash_finish( &operation,
1923 hash, sizeof( hash ), &hash_len ),
1924 PSA_ERROR_BAD_STATE );
1925 PSA_ASSERT( psa_hash_abort( &operation ) );
1926
1927 /* Call finish after calling verify. */
1928 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1929 PSA_ASSERT( psa_hash_verify( &operation,
1930 valid_hash, sizeof( valid_hash ) ) );
1931 TEST_EQUAL( psa_hash_finish( &operation,
1932 hash, sizeof( hash ), &hash_len ),
1933 PSA_ERROR_BAD_STATE );
1934 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001935
1936exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001937 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001938}
1939/* END_CASE */
1940
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001941/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001942void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001943{
1944 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001945 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1946 * appended to it */
1947 unsigned char hash[] = {
1948 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1949 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1950 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001951 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001952 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001953
Gilles Peskine8817f612018-12-18 00:18:46 +01001954 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001955
itayzafrir27e69452018-11-01 14:26:34 +02001956 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001957 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001958 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001959 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001960
itayzafrir27e69452018-11-01 14:26:34 +02001961 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001962 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001963 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001964 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001965
itayzafrir27e69452018-11-01 14:26:34 +02001966 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001968 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001969 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001970
itayzafrirec93d302018-10-18 18:01:10 +03001971exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001972 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001973}
1974/* END_CASE */
1975
Ronald Cronee414c72021-03-18 18:50:08 +01001976/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001977void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001978{
1979 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001980 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001981 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001982 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001983 size_t hash_len;
1984
Gilles Peskine8817f612018-12-18 00:18:46 +01001985 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001986
itayzafrir58028322018-10-25 10:22:01 +03001987 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001988 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001989 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001990 hash, expected_size - 1, &hash_len ),
1991 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001992
1993exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001994 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001995}
1996/* END_CASE */
1997
Ronald Cronee414c72021-03-18 18:50:08 +01001998/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001999void hash_clone_source_state( )
2000{
2001 psa_algorithm_t alg = PSA_ALG_SHA_256;
2002 unsigned char hash[PSA_HASH_MAX_SIZE];
2003 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2004 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2005 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2006 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2007 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2008 size_t hash_len;
2009
2010 PSA_ASSERT( psa_crypto_init( ) );
2011 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2012
2013 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2014 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2015 PSA_ASSERT( psa_hash_finish( &op_finished,
2016 hash, sizeof( hash ), &hash_len ) );
2017 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2018 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2019
2020 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2021 PSA_ERROR_BAD_STATE );
2022
2023 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2024 PSA_ASSERT( psa_hash_finish( &op_init,
2025 hash, sizeof( hash ), &hash_len ) );
2026 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2027 PSA_ASSERT( psa_hash_finish( &op_finished,
2028 hash, sizeof( hash ), &hash_len ) );
2029 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2030 PSA_ASSERT( psa_hash_finish( &op_aborted,
2031 hash, sizeof( hash ), &hash_len ) );
2032
2033exit:
2034 psa_hash_abort( &op_source );
2035 psa_hash_abort( &op_init );
2036 psa_hash_abort( &op_setup );
2037 psa_hash_abort( &op_finished );
2038 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002039 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002040}
2041/* END_CASE */
2042
Ronald Cronee414c72021-03-18 18:50:08 +01002043/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002044void hash_clone_target_state( )
2045{
2046 psa_algorithm_t alg = PSA_ALG_SHA_256;
2047 unsigned char hash[PSA_HASH_MAX_SIZE];
2048 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2049 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2050 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2051 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2052 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2053 size_t hash_len;
2054
2055 PSA_ASSERT( psa_crypto_init( ) );
2056
2057 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2058 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2059 PSA_ASSERT( psa_hash_finish( &op_finished,
2060 hash, sizeof( hash ), &hash_len ) );
2061 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2062 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2063
2064 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2065 PSA_ASSERT( psa_hash_finish( &op_target,
2066 hash, sizeof( hash ), &hash_len ) );
2067
2068 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2069 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2070 PSA_ERROR_BAD_STATE );
2071 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2072 PSA_ERROR_BAD_STATE );
2073
2074exit:
2075 psa_hash_abort( &op_target );
2076 psa_hash_abort( &op_init );
2077 psa_hash_abort( &op_setup );
2078 psa_hash_abort( &op_finished );
2079 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002080 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002081}
2082/* END_CASE */
2083
itayzafrir58028322018-10-25 10:22:01 +03002084/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002085void mac_operation_init( )
2086{
Jaeden Amero252ef282019-02-15 14:05:35 +00002087 const uint8_t input[1] = { 0 };
2088
Jaeden Amero769ce272019-01-04 11:48:03 +00002089 /* Test each valid way of initializing the object, except for `= {0}`, as
2090 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2091 * though it's OK by the C standard. We could test for this, but we'd need
2092 * to supress the Clang warning for the test. */
2093 psa_mac_operation_t func = psa_mac_operation_init( );
2094 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2095 psa_mac_operation_t zero;
2096
2097 memset( &zero, 0, sizeof( zero ) );
2098
Jaeden Amero252ef282019-02-15 14:05:35 +00002099 /* A freshly-initialized MAC operation should not be usable. */
2100 TEST_EQUAL( psa_mac_update( &func,
2101 input, sizeof( input ) ),
2102 PSA_ERROR_BAD_STATE );
2103 TEST_EQUAL( psa_mac_update( &init,
2104 input, sizeof( input ) ),
2105 PSA_ERROR_BAD_STATE );
2106 TEST_EQUAL( psa_mac_update( &zero,
2107 input, sizeof( input ) ),
2108 PSA_ERROR_BAD_STATE );
2109
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002110 /* A default MAC operation should be abortable without error. */
2111 PSA_ASSERT( psa_mac_abort( &func ) );
2112 PSA_ASSERT( psa_mac_abort( &init ) );
2113 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002114}
2115/* END_CASE */
2116
2117/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002118void mac_setup( int key_type_arg,
2119 data_t *key,
2120 int alg_arg,
2121 int expected_status_arg )
2122{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002123 psa_key_type_t key_type = key_type_arg;
2124 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002125 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002126 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002127 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2128#if defined(KNOWN_SUPPORTED_MAC_ALG)
2129 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2130#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002131
Gilles Peskine8817f612018-12-18 00:18:46 +01002132 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002133
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002134 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2135 &operation, &status ) )
2136 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002137 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002138
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002139 /* The operation object should be reusable. */
2140#if defined(KNOWN_SUPPORTED_MAC_ALG)
2141 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2142 smoke_test_key_data,
2143 sizeof( smoke_test_key_data ),
2144 KNOWN_SUPPORTED_MAC_ALG,
2145 &operation, &status ) )
2146 goto exit;
2147 TEST_EQUAL( status, PSA_SUCCESS );
2148#endif
2149
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002150exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002151 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002152}
2153/* END_CASE */
2154
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002155/* 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 +00002156void mac_bad_order( )
2157{
Ronald Cron5425a212020-08-04 14:58:35 +02002158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002159 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2160 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002161 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002162 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2163 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2164 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002166 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2167 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2168 size_t sign_mac_length = 0;
2169 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2170 const uint8_t verify_mac[] = {
2171 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2172 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2173 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2174
2175 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002176 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002177 psa_set_key_algorithm( &attributes, alg );
2178 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002179
Ronald Cron5425a212020-08-04 14:58:35 +02002180 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2181 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002182
Jaeden Amero252ef282019-02-15 14:05:35 +00002183 /* Call update without calling setup beforehand. */
2184 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2185 PSA_ERROR_BAD_STATE );
2186 PSA_ASSERT( psa_mac_abort( &operation ) );
2187
2188 /* Call sign finish without calling setup beforehand. */
2189 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2190 &sign_mac_length),
2191 PSA_ERROR_BAD_STATE );
2192 PSA_ASSERT( psa_mac_abort( &operation ) );
2193
2194 /* Call verify finish without calling setup beforehand. */
2195 TEST_EQUAL( psa_mac_verify_finish( &operation,
2196 verify_mac, sizeof( verify_mac ) ),
2197 PSA_ERROR_BAD_STATE );
2198 PSA_ASSERT( psa_mac_abort( &operation ) );
2199
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002200 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002201 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2202 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002203 PSA_ERROR_BAD_STATE );
2204 PSA_ASSERT( psa_mac_abort( &operation ) );
2205
Jaeden Amero252ef282019-02-15 14:05:35 +00002206 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002207 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002208 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2209 PSA_ASSERT( psa_mac_sign_finish( &operation,
2210 sign_mac, sizeof( sign_mac ),
2211 &sign_mac_length ) );
2212 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2213 PSA_ERROR_BAD_STATE );
2214 PSA_ASSERT( psa_mac_abort( &operation ) );
2215
2216 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002217 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002218 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2219 PSA_ASSERT( psa_mac_verify_finish( &operation,
2220 verify_mac, sizeof( verify_mac ) ) );
2221 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2222 PSA_ERROR_BAD_STATE );
2223 PSA_ASSERT( psa_mac_abort( &operation ) );
2224
2225 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002226 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002227 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2228 PSA_ASSERT( psa_mac_sign_finish( &operation,
2229 sign_mac, sizeof( sign_mac ),
2230 &sign_mac_length ) );
2231 TEST_EQUAL( psa_mac_sign_finish( &operation,
2232 sign_mac, sizeof( sign_mac ),
2233 &sign_mac_length ),
2234 PSA_ERROR_BAD_STATE );
2235 PSA_ASSERT( psa_mac_abort( &operation ) );
2236
2237 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002238 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002239 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2240 PSA_ASSERT( psa_mac_verify_finish( &operation,
2241 verify_mac, sizeof( verify_mac ) ) );
2242 TEST_EQUAL( psa_mac_verify_finish( &operation,
2243 verify_mac, sizeof( verify_mac ) ),
2244 PSA_ERROR_BAD_STATE );
2245 PSA_ASSERT( psa_mac_abort( &operation ) );
2246
2247 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002248 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002249 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2250 TEST_EQUAL( psa_mac_verify_finish( &operation,
2251 verify_mac, sizeof( verify_mac ) ),
2252 PSA_ERROR_BAD_STATE );
2253 PSA_ASSERT( psa_mac_abort( &operation ) );
2254
2255 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002256 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002257 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2258 TEST_EQUAL( psa_mac_sign_finish( &operation,
2259 sign_mac, sizeof( sign_mac ),
2260 &sign_mac_length ),
2261 PSA_ERROR_BAD_STATE );
2262 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002263
Ronald Cron5425a212020-08-04 14:58:35 +02002264 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002265
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002266exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002267 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002268}
2269/* END_CASE */
2270
2271/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002272void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002273 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002274 int alg_arg,
2275 data_t *input,
2276 data_t *expected_mac )
2277{
Ronald Cron5425a212020-08-04 14:58:35 +02002278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002279 psa_key_type_t key_type = key_type_arg;
2280 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002281 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002283 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002284 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002285 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002286 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002287 const size_t output_sizes_to_test[] = {
2288 0,
2289 1,
2290 expected_mac->len - 1,
2291 expected_mac->len,
2292 expected_mac->len + 1,
2293 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002294
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002295 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002296 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002297 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002298
Gilles Peskine8817f612018-12-18 00:18:46 +01002299 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002300
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002301 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002302 psa_set_key_algorithm( &attributes, alg );
2303 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002304
Ronald Cron5425a212020-08-04 14:58:35 +02002305 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2306 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002307
Gilles Peskine8b356b52020-08-25 23:44:59 +02002308 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2309 {
2310 const size_t output_size = output_sizes_to_test[i];
2311 psa_status_t expected_status =
2312 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2313 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002314
Chris Jones9634bb12021-01-20 15:56:42 +00002315 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002316 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002317
Gilles Peskine8b356b52020-08-25 23:44:59 +02002318 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002319 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002320 PSA_ASSERT( psa_mac_update( &operation,
2321 input->x, input->len ) );
2322 TEST_EQUAL( psa_mac_sign_finish( &operation,
2323 actual_mac, output_size,
2324 &mac_length ),
2325 expected_status );
2326 PSA_ASSERT( psa_mac_abort( &operation ) );
2327
2328 if( expected_status == PSA_SUCCESS )
2329 {
2330 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2331 actual_mac, mac_length );
2332 }
2333 mbedtls_free( actual_mac );
2334 actual_mac = NULL;
2335 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002336
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002337exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002338 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002339 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002340 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002341 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002342}
2343/* END_CASE */
2344
2345/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002346void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002347 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002348 int alg_arg,
2349 data_t *input,
2350 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002351{
Ronald Cron5425a212020-08-04 14:58:35 +02002352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002353 psa_key_type_t key_type = key_type_arg;
2354 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002355 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002357 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002358
Gilles Peskine69c12672018-06-28 00:07:19 +02002359 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2360
Gilles Peskine8817f612018-12-18 00:18:46 +01002361 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002362
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002364 psa_set_key_algorithm( &attributes, alg );
2365 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002366
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2368 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002369
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002370 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002371 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002372 PSA_ASSERT( psa_mac_update( &operation,
2373 input->x, input->len ) );
2374 PSA_ASSERT( psa_mac_verify_finish( &operation,
2375 expected_mac->x,
2376 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002377
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002378 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002379 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002380 PSA_ASSERT( psa_mac_update( &operation,
2381 input->x, input->len ) );
2382 TEST_EQUAL( psa_mac_verify_finish( &operation,
2383 expected_mac->x,
2384 expected_mac->len - 1 ),
2385 PSA_ERROR_INVALID_SIGNATURE );
2386
2387 /* Test a MAC that's too long. */
2388 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2389 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002390 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002391 PSA_ASSERT( psa_mac_update( &operation,
2392 input->x, input->len ) );
2393 TEST_EQUAL( psa_mac_verify_finish( &operation,
2394 perturbed_mac,
2395 expected_mac->len + 1 ),
2396 PSA_ERROR_INVALID_SIGNATURE );
2397
2398 /* Test changing one byte. */
2399 for( size_t i = 0; i < expected_mac->len; i++ )
2400 {
Chris Jones9634bb12021-01-20 15:56:42 +00002401 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002402 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002403 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002404 PSA_ASSERT( psa_mac_update( &operation,
2405 input->x, input->len ) );
2406 TEST_EQUAL( psa_mac_verify_finish( &operation,
2407 perturbed_mac,
2408 expected_mac->len ),
2409 PSA_ERROR_INVALID_SIGNATURE );
2410 perturbed_mac[i] ^= 1;
2411 }
2412
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002414 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002415 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002416 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002417 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418}
2419/* END_CASE */
2420
2421/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002422void cipher_operation_init( )
2423{
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 const uint8_t input[1] = { 0 };
2425 unsigned char output[1] = { 0 };
2426 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002427 /* Test each valid way of initializing the object, except for `= {0}`, as
2428 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2429 * though it's OK by the C standard. We could test for this, but we'd need
2430 * to supress the Clang warning for the test. */
2431 psa_cipher_operation_t func = psa_cipher_operation_init( );
2432 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2433 psa_cipher_operation_t zero;
2434
2435 memset( &zero, 0, sizeof( zero ) );
2436
Jaeden Ameroab439972019-02-15 14:12:05 +00002437 /* A freshly-initialized cipher operation should not be usable. */
2438 TEST_EQUAL( psa_cipher_update( &func,
2439 input, sizeof( input ),
2440 output, sizeof( output ),
2441 &output_length ),
2442 PSA_ERROR_BAD_STATE );
2443 TEST_EQUAL( psa_cipher_update( &init,
2444 input, sizeof( input ),
2445 output, sizeof( output ),
2446 &output_length ),
2447 PSA_ERROR_BAD_STATE );
2448 TEST_EQUAL( psa_cipher_update( &zero,
2449 input, sizeof( input ),
2450 output, sizeof( output ),
2451 &output_length ),
2452 PSA_ERROR_BAD_STATE );
2453
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002454 /* A default cipher operation should be abortable without error. */
2455 PSA_ASSERT( psa_cipher_abort( &func ) );
2456 PSA_ASSERT( psa_cipher_abort( &init ) );
2457 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002458}
2459/* END_CASE */
2460
2461/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002462void cipher_setup( int key_type_arg,
2463 data_t *key,
2464 int alg_arg,
2465 int expected_status_arg )
2466{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002467 psa_key_type_t key_type = key_type_arg;
2468 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002469 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002470 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002472#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002473 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2474#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002475
Gilles Peskine8817f612018-12-18 00:18:46 +01002476 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002477
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002478 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2479 &operation, &status ) )
2480 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002481 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002482
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002483 /* The operation object should be reusable. */
2484#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2485 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2486 smoke_test_key_data,
2487 sizeof( smoke_test_key_data ),
2488 KNOWN_SUPPORTED_CIPHER_ALG,
2489 &operation, &status ) )
2490 goto exit;
2491 TEST_EQUAL( status, PSA_SUCCESS );
2492#endif
2493
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002494exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002495 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002496 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002497}
2498/* END_CASE */
2499
Ronald Cronee414c72021-03-18 18:50:08 +01002500/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002501void cipher_bad_order( )
2502{
Ronald Cron5425a212020-08-04 14:58:35 +02002503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002504 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2505 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002507 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002508 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002509 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002510 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2511 0xaa, 0xaa, 0xaa, 0xaa };
2512 const uint8_t text[] = {
2513 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2514 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002515 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002516 size_t length = 0;
2517
2518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002519 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2520 psa_set_key_algorithm( &attributes, alg );
2521 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002522 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2523 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002524
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002525 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002526 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2527 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002528 PSA_ERROR_BAD_STATE );
2529 PSA_ASSERT( psa_cipher_abort( &operation ) );
2530
2531 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002532 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2533 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002534 PSA_ERROR_BAD_STATE );
2535 PSA_ASSERT( psa_cipher_abort( &operation ) );
2536
Jaeden Ameroab439972019-02-15 14:12:05 +00002537 /* Generate an IV without calling setup beforehand. */
2538 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2539 buffer, sizeof( buffer ),
2540 &length ),
2541 PSA_ERROR_BAD_STATE );
2542 PSA_ASSERT( psa_cipher_abort( &operation ) );
2543
2544 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002545 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002546 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2547 buffer, sizeof( buffer ),
2548 &length ) );
2549 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2550 buffer, sizeof( buffer ),
2551 &length ),
2552 PSA_ERROR_BAD_STATE );
2553 PSA_ASSERT( psa_cipher_abort( &operation ) );
2554
2555 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002556 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002557 PSA_ASSERT( psa_cipher_set_iv( &operation,
2558 iv, sizeof( iv ) ) );
2559 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2560 buffer, sizeof( buffer ),
2561 &length ),
2562 PSA_ERROR_BAD_STATE );
2563 PSA_ASSERT( psa_cipher_abort( &operation ) );
2564
2565 /* Set an IV without calling setup beforehand. */
2566 TEST_EQUAL( psa_cipher_set_iv( &operation,
2567 iv, sizeof( iv ) ),
2568 PSA_ERROR_BAD_STATE );
2569 PSA_ASSERT( psa_cipher_abort( &operation ) );
2570
2571 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002572 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002573 PSA_ASSERT( psa_cipher_set_iv( &operation,
2574 iv, sizeof( iv ) ) );
2575 TEST_EQUAL( psa_cipher_set_iv( &operation,
2576 iv, sizeof( iv ) ),
2577 PSA_ERROR_BAD_STATE );
2578 PSA_ASSERT( psa_cipher_abort( &operation ) );
2579
2580 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002581 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002582 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2583 buffer, sizeof( buffer ),
2584 &length ) );
2585 TEST_EQUAL( psa_cipher_set_iv( &operation,
2586 iv, sizeof( iv ) ),
2587 PSA_ERROR_BAD_STATE );
2588 PSA_ASSERT( psa_cipher_abort( &operation ) );
2589
2590 /* Call update without calling setup beforehand. */
2591 TEST_EQUAL( psa_cipher_update( &operation,
2592 text, sizeof( text ),
2593 buffer, sizeof( buffer ),
2594 &length ),
2595 PSA_ERROR_BAD_STATE );
2596 PSA_ASSERT( psa_cipher_abort( &operation ) );
2597
2598 /* Call update without an IV where an IV is required. */
2599 TEST_EQUAL( psa_cipher_update( &operation,
2600 text, sizeof( text ),
2601 buffer, sizeof( buffer ),
2602 &length ),
2603 PSA_ERROR_BAD_STATE );
2604 PSA_ASSERT( psa_cipher_abort( &operation ) );
2605
2606 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002607 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002608 PSA_ASSERT( psa_cipher_set_iv( &operation,
2609 iv, sizeof( iv ) ) );
2610 PSA_ASSERT( psa_cipher_finish( &operation,
2611 buffer, sizeof( buffer ), &length ) );
2612 TEST_EQUAL( psa_cipher_update( &operation,
2613 text, sizeof( text ),
2614 buffer, sizeof( buffer ),
2615 &length ),
2616 PSA_ERROR_BAD_STATE );
2617 PSA_ASSERT( psa_cipher_abort( &operation ) );
2618
2619 /* Call finish without calling setup beforehand. */
2620 TEST_EQUAL( psa_cipher_finish( &operation,
2621 buffer, sizeof( buffer ), &length ),
2622 PSA_ERROR_BAD_STATE );
2623 PSA_ASSERT( psa_cipher_abort( &operation ) );
2624
2625 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002626 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002627 /* Not calling update means we are encrypting an empty buffer, which is OK
2628 * for cipher modes with padding. */
2629 TEST_EQUAL( psa_cipher_finish( &operation,
2630 buffer, sizeof( buffer ), &length ),
2631 PSA_ERROR_BAD_STATE );
2632 PSA_ASSERT( psa_cipher_abort( &operation ) );
2633
2634 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002635 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002636 PSA_ASSERT( psa_cipher_set_iv( &operation,
2637 iv, sizeof( iv ) ) );
2638 PSA_ASSERT( psa_cipher_finish( &operation,
2639 buffer, sizeof( buffer ), &length ) );
2640 TEST_EQUAL( psa_cipher_finish( &operation,
2641 buffer, sizeof( buffer ), &length ),
2642 PSA_ERROR_BAD_STATE );
2643 PSA_ASSERT( psa_cipher_abort( &operation ) );
2644
Ronald Cron5425a212020-08-04 14:58:35 +02002645 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002646
Jaeden Ameroab439972019-02-15 14:12:05 +00002647exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002648 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002649 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002650}
2651/* END_CASE */
2652
2653/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002655 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002656 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002657 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002658{
Ronald Cron5425a212020-08-04 14:58:35 +02002659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002660 psa_status_t status;
2661 psa_key_type_t key_type = key_type_arg;
2662 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002663 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002664 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002665 size_t output_buffer_size = 0;
2666 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002667 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002668 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002670
Gilles Peskine8817f612018-12-18 00:18:46 +01002671 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002672
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002673 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2674 psa_set_key_algorithm( &attributes, alg );
2675 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002676
Ronald Cron5425a212020-08-04 14:58:35 +02002677 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2678 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002679
Ronald Cron5425a212020-08-04 14:58:35 +02002680 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002681
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002682 if( iv->len > 0 )
2683 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002684 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002685 }
2686
gabor-mezei-armceface22021-01-21 12:26:17 +01002687 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2688 TEST_ASSERT( output_buffer_size <=
2689 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002690 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002691
Gilles Peskine8817f612018-12-18 00:18:46 +01002692 PSA_ASSERT( psa_cipher_update( &operation,
2693 input->x, input->len,
2694 output, output_buffer_size,
2695 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002696 TEST_ASSERT( function_output_length <=
2697 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2698 TEST_ASSERT( function_output_length <=
2699 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002700 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002701
Gilles Peskine50e586b2018-06-08 14:28:46 +02002702 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002703 ( output_buffer_size == 0 ? NULL :
2704 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002705 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002707 TEST_ASSERT( function_output_length <=
2708 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2709 TEST_ASSERT( function_output_length <=
2710 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002711 total_output_length += function_output_length;
2712
Gilles Peskinefe11b722018-12-18 00:24:04 +01002713 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002714 if( expected_status == PSA_SUCCESS )
2715 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002716 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002717 ASSERT_COMPARE( expected_output->x, expected_output->len,
2718 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002719 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002720
Gilles Peskine50e586b2018-06-08 14:28:46 +02002721exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002722 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002723 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002724 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002725 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002726}
2727/* END_CASE */
2728
2729/* BEGIN_CASE */
2730void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002731 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002732 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002733 int first_part_size_arg,
2734 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002735 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736{
Ronald Cron5425a212020-08-04 14:58:35 +02002737 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738 psa_key_type_t key_type = key_type_arg;
2739 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002740 size_t first_part_size = first_part_size_arg;
2741 size_t output1_length = output1_length_arg;
2742 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002743 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002744 size_t output_buffer_size = 0;
2745 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002746 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002747 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002749
Gilles Peskine8817f612018-12-18 00:18:46 +01002750 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002751
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002752 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2753 psa_set_key_algorithm( &attributes, alg );
2754 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002755
Ronald Cron5425a212020-08-04 14:58:35 +02002756 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2757 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002758
Ronald Cron5425a212020-08-04 14:58:35 +02002759 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002760
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002761 if( iv->len > 0 )
2762 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002763 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002764 }
2765
gabor-mezei-armceface22021-01-21 12:26:17 +01002766 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2767 TEST_ASSERT( output_buffer_size <=
2768 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002769 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002770
Gilles Peskinee0866522019-02-19 19:44:00 +01002771 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002772 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2773 output, output_buffer_size,
2774 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002775 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002776 TEST_ASSERT( function_output_length <=
2777 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2778 TEST_ASSERT( function_output_length <=
2779 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002780 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002781
Gilles Peskine8817f612018-12-18 00:18:46 +01002782 PSA_ASSERT( psa_cipher_update( &operation,
2783 input->x + first_part_size,
2784 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002785 ( output_buffer_size == 0 ? NULL :
2786 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002787 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002788 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002789 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002790 TEST_ASSERT( function_output_length <=
2791 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2792 alg,
2793 input->len - first_part_size ) );
2794 TEST_ASSERT( function_output_length <=
2795 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002796 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002797
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002799 ( output_buffer_size == 0 ? NULL :
2800 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002801 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002803 TEST_ASSERT( function_output_length <=
2804 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2805 TEST_ASSERT( function_output_length <=
2806 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002807 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002810 ASSERT_COMPARE( expected_output->x, expected_output->len,
2811 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812
2813exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002814 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002815 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002816 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002817 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818}
2819/* END_CASE */
2820
2821/* BEGIN_CASE */
2822void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002823 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002824 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002825 int first_part_size_arg,
2826 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002827 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828{
Ronald Cron5425a212020-08-04 14:58:35 +02002829 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830 psa_key_type_t key_type = key_type_arg;
2831 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002832 size_t first_part_size = first_part_size_arg;
2833 size_t output1_length = output1_length_arg;
2834 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002835 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002836 size_t output_buffer_size = 0;
2837 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002838 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002839 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002841
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002843
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002844 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2845 psa_set_key_algorithm( &attributes, alg );
2846 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002847
Ronald Cron5425a212020-08-04 14:58:35 +02002848 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2849 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002850
Ronald Cron5425a212020-08-04 14:58:35 +02002851 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002852
Steven Cooreman177deba2020-09-07 17:14:14 +02002853 if( iv->len > 0 )
2854 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002855 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002856 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002857
gabor-mezei-armceface22021-01-21 12:26:17 +01002858 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2859 TEST_ASSERT( output_buffer_size <=
2860 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002861 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002862
Gilles Peskinee0866522019-02-19 19:44:00 +01002863 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002864 PSA_ASSERT( psa_cipher_update( &operation,
2865 input->x, first_part_size,
2866 output, output_buffer_size,
2867 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002868 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002869 TEST_ASSERT( function_output_length <=
2870 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2871 TEST_ASSERT( function_output_length <=
2872 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002873 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002874
Gilles Peskine8817f612018-12-18 00:18:46 +01002875 PSA_ASSERT( psa_cipher_update( &operation,
2876 input->x + first_part_size,
2877 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002878 ( output_buffer_size == 0 ? NULL :
2879 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002880 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002881 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002882 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002883 TEST_ASSERT( function_output_length <=
2884 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2885 alg,
2886 input->len - first_part_size ) );
2887 TEST_ASSERT( function_output_length <=
2888 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002889 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002890
Gilles Peskine8817f612018-12-18 00:18:46 +01002891 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002892 ( output_buffer_size == 0 ? NULL :
2893 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002894 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002895 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002896 TEST_ASSERT( function_output_length <=
2897 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2898 TEST_ASSERT( function_output_length <=
2899 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002900 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002901 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002902
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002903 ASSERT_COMPARE( expected_output->x, expected_output->len,
2904 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002905
2906exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002907 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002908 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002909 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002910 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002911}
2912/* END_CASE */
2913
Gilles Peskine50e586b2018-06-08 14:28:46 +02002914/* BEGIN_CASE */
2915void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002916 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002917 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002918 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002919{
Ronald Cron5425a212020-08-04 14:58:35 +02002920 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921 psa_status_t status;
2922 psa_key_type_t key_type = key_type_arg;
2923 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002924 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002925 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926 size_t output_buffer_size = 0;
2927 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002928 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002929 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002931
Gilles Peskine8817f612018-12-18 00:18:46 +01002932 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002933
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2935 psa_set_key_algorithm( &attributes, alg );
2936 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002937
Ronald Cron5425a212020-08-04 14:58:35 +02002938 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2939 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002940
Ronald Cron5425a212020-08-04 14:58:35 +02002941 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002942
Steven Cooreman177deba2020-09-07 17:14:14 +02002943 if( iv->len > 0 )
2944 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002945 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002946 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002947
gabor-mezei-armceface22021-01-21 12:26:17 +01002948 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2949 TEST_ASSERT( output_buffer_size <=
2950 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002951 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002952
Gilles Peskine8817f612018-12-18 00:18:46 +01002953 PSA_ASSERT( psa_cipher_update( &operation,
2954 input->x, input->len,
2955 output, output_buffer_size,
2956 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002957 TEST_ASSERT( function_output_length <=
2958 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2959 TEST_ASSERT( function_output_length <=
2960 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002961 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002962
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002964 ( output_buffer_size == 0 ? NULL :
2965 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002966 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002968 TEST_ASSERT( function_output_length <=
2969 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2970 TEST_ASSERT( function_output_length <=
2971 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002972 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002973 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974
2975 if( expected_status == PSA_SUCCESS )
2976 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002977 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002978 ASSERT_COMPARE( expected_output->x, expected_output->len,
2979 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002980 }
2981
Gilles Peskine50e586b2018-06-08 14:28:46 +02002982exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002983 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002984 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002985 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002986 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002987}
2988/* END_CASE */
2989
Gilles Peskine50e586b2018-06-08 14:28:46 +02002990/* BEGIN_CASE */
2991void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002992 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002993 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002994{
Ronald Cron5425a212020-08-04 14:58:35 +02002995 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002996 psa_key_type_t key_type = key_type_arg;
2997 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002998 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002999 size_t iv_size = 16;
3000 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003001 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003002 size_t output1_size = 0;
3003 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003004 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003005 size_t output2_size = 0;
3006 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003007 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003008 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3009 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003011
Gilles Peskine8817f612018-12-18 00:18:46 +01003012 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003013
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003014 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3015 psa_set_key_algorithm( &attributes, alg );
3016 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003017
Ronald Cron5425a212020-08-04 14:58:35 +02003018 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3019 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003020
Ronald Cron5425a212020-08-04 14:58:35 +02003021 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3022 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003023
Steven Cooreman177deba2020-09-07 17:14:14 +02003024 if( alg != PSA_ALG_ECB_NO_PADDING )
3025 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003026 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3027 iv, iv_size,
3028 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003029 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003030 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3031 TEST_ASSERT( output1_size <=
3032 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003033 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003034
Gilles Peskine8817f612018-12-18 00:18:46 +01003035 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3036 output1, output1_size,
3037 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003038 TEST_ASSERT( output1_length <=
3039 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3040 TEST_ASSERT( output1_length <=
3041 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3042
Gilles Peskine8817f612018-12-18 00:18:46 +01003043 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003044 output1 + output1_length,
3045 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003046 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003047 TEST_ASSERT( function_output_length <=
3048 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3049 TEST_ASSERT( function_output_length <=
3050 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003051
Gilles Peskine048b7f02018-06-08 14:20:49 +02003052 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003053
Gilles Peskine8817f612018-12-18 00:18:46 +01003054 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003055
3056 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003057 TEST_ASSERT( output2_size <=
3058 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3059 TEST_ASSERT( output2_size <=
3060 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003061 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003062
Steven Cooreman177deba2020-09-07 17:14:14 +02003063 if( iv_length > 0 )
3064 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003065 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3066 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003067 }
3068
Gilles Peskine8817f612018-12-18 00:18:46 +01003069 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3070 output2, output2_size,
3071 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003072 TEST_ASSERT( output2_length <=
3073 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3074 TEST_ASSERT( output2_length <=
3075 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3076
Gilles Peskine048b7f02018-06-08 14:20:49 +02003077 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003078 PSA_ASSERT( psa_cipher_finish( &operation2,
3079 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003080 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003081 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003082 TEST_ASSERT( function_output_length <=
3083 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3084 TEST_ASSERT( function_output_length <=
3085 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003086
Gilles Peskine048b7f02018-06-08 14:20:49 +02003087 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003090
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003091 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003092
3093exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003094 psa_cipher_abort( &operation1 );
3095 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003096 mbedtls_free( output1 );
3097 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003098 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003099 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003100}
3101/* END_CASE */
3102
3103/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003104void cipher_verify_output_multipart( int alg_arg,
3105 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003106 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003107 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003108 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003109{
Ronald Cron5425a212020-08-04 14:58:35 +02003110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003111 psa_key_type_t key_type = key_type_arg;
3112 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003113 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003114 unsigned char iv[16] = {0};
3115 size_t iv_size = 16;
3116 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003117 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003118 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003119 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003120 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003121 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003122 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003123 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003124 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3125 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003127
Gilles Peskine8817f612018-12-18 00:18:46 +01003128 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003130 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3131 psa_set_key_algorithm( &attributes, alg );
3132 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003133
Ronald Cron5425a212020-08-04 14:58:35 +02003134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3135 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003136
Ronald Cron5425a212020-08-04 14:58:35 +02003137 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3138 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003139
Steven Cooreman177deba2020-09-07 17:14:14 +02003140 if( alg != PSA_ALG_ECB_NO_PADDING )
3141 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003142 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3143 iv, iv_size,
3144 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003145 }
3146
gabor-mezei-armceface22021-01-21 12:26:17 +01003147 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3148 TEST_ASSERT( output1_buffer_size <=
3149 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003150 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003151
Gilles Peskinee0866522019-02-19 19:44:00 +01003152 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003153
Gilles Peskine8817f612018-12-18 00:18:46 +01003154 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3155 output1, output1_buffer_size,
3156 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003157 TEST_ASSERT( function_output_length <=
3158 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3159 TEST_ASSERT( function_output_length <=
3160 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003161 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003162
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 PSA_ASSERT( psa_cipher_update( &operation1,
3164 input->x + first_part_size,
3165 input->len - first_part_size,
3166 output1, output1_buffer_size,
3167 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003168 TEST_ASSERT( function_output_length <=
3169 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3170 alg,
3171 input->len - first_part_size ) );
3172 TEST_ASSERT( function_output_length <=
3173 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003174 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003175
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_cipher_finish( &operation1,
3177 output1 + output1_length,
3178 output1_buffer_size - output1_length,
3179 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003180 TEST_ASSERT( function_output_length <=
3181 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3182 TEST_ASSERT( function_output_length <=
3183 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003184 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003185
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003187
Gilles Peskine048b7f02018-06-08 14:20:49 +02003188 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003189 TEST_ASSERT( output2_buffer_size <=
3190 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3191 TEST_ASSERT( output2_buffer_size <=
3192 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003193 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003194
Steven Cooreman177deba2020-09-07 17:14:14 +02003195 if( iv_length > 0 )
3196 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003197 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3198 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003199 }
Moran Pekerded84402018-06-06 16:36:50 +03003200
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3202 output2, output2_buffer_size,
3203 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003204 TEST_ASSERT( function_output_length <=
3205 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3206 TEST_ASSERT( function_output_length <=
3207 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003208 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003209
Gilles Peskine8817f612018-12-18 00:18:46 +01003210 PSA_ASSERT( psa_cipher_update( &operation2,
3211 output1 + first_part_size,
3212 output1_length - first_part_size,
3213 output2, output2_buffer_size,
3214 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003215 TEST_ASSERT( function_output_length <=
3216 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3217 alg,
3218 output1_length - first_part_size ) );
3219 TEST_ASSERT( function_output_length <=
3220 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003221 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003222
Gilles Peskine8817f612018-12-18 00:18:46 +01003223 PSA_ASSERT( psa_cipher_finish( &operation2,
3224 output2 + output2_length,
3225 output2_buffer_size - output2_length,
3226 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003227 TEST_ASSERT( function_output_length <=
3228 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3229 TEST_ASSERT( function_output_length <=
3230 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003231 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003232
Gilles Peskine8817f612018-12-18 00:18:46 +01003233 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003234
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003235 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003236
3237exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003238 psa_cipher_abort( &operation1 );
3239 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003240 mbedtls_free( output1 );
3241 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003242 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003243 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003244}
3245/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003246
Gilles Peskine20035e32018-02-03 22:44:14 +01003247/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003248void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003249 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003250 data_t *nonce,
3251 data_t *additional_data,
3252 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003253 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254{
Ronald Cron5425a212020-08-04 14:58:35 +02003255 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003256 psa_key_type_t key_type = key_type_arg;
3257 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003258 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003259 unsigned char *output_data = NULL;
3260 size_t output_size = 0;
3261 size_t output_length = 0;
3262 unsigned char *output_data2 = NULL;
3263 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003264 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003265 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003266 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003267
Gilles Peskine8817f612018-12-18 00:18:46 +01003268 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003269
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003270 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3271 psa_set_key_algorithm( &attributes, alg );
3272 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003273
Gilles Peskine049c7532019-05-15 20:22:09 +02003274 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003275 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003276 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3277 key_bits = psa_get_key_bits( &attributes );
3278
3279 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3280 alg );
3281 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3282 * should be exact. */
3283 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3284 expected_result != PSA_ERROR_NOT_SUPPORTED )
3285 {
3286 TEST_EQUAL( output_size,
3287 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3288 TEST_ASSERT( output_size <=
3289 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3290 }
3291 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003292
Steven Cooremanf49478b2021-02-15 15:19:25 +01003293 status = psa_aead_encrypt( key, alg,
3294 nonce->x, nonce->len,
3295 additional_data->x,
3296 additional_data->len,
3297 input_data->x, input_data->len,
3298 output_data, output_size,
3299 &output_length );
3300
3301 /* If the operation is not supported, just skip and not fail in case the
3302 * encryption involves a common limitation of cryptography hardwares and
3303 * an alternative implementation. */
3304 if( status == PSA_ERROR_NOT_SUPPORTED )
3305 {
3306 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3307 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3308 }
3309
3310 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311
3312 if( PSA_SUCCESS == expected_result )
3313 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003314 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003315
Gilles Peskine003a4a92019-05-14 16:09:40 +02003316 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3317 * should be exact. */
3318 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003319 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003320
gabor-mezei-armceface22021-01-21 12:26:17 +01003321 TEST_ASSERT( input_data->len <=
3322 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3323
Ronald Cron5425a212020-08-04 14:58:35 +02003324 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003325 nonce->x, nonce->len,
3326 additional_data->x,
3327 additional_data->len,
3328 output_data, output_length,
3329 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003330 &output_length2 ),
3331 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003332
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003333 ASSERT_COMPARE( input_data->x, input_data->len,
3334 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003335 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003336
Gilles Peskinea1cac842018-06-11 19:33:02 +02003337exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003338 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339 mbedtls_free( output_data );
3340 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003341 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342}
3343/* END_CASE */
3344
3345/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003346void aead_encrypt( int key_type_arg, data_t *key_data,
3347 int alg_arg,
3348 data_t *nonce,
3349 data_t *additional_data,
3350 data_t *input_data,
3351 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003352{
Ronald Cron5425a212020-08-04 14:58:35 +02003353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003354 psa_key_type_t key_type = key_type_arg;
3355 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003356 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003357 unsigned char *output_data = NULL;
3358 size_t output_size = 0;
3359 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003361 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003362
Gilles Peskine8817f612018-12-18 00:18:46 +01003363 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003365 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3366 psa_set_key_algorithm( &attributes, alg );
3367 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003368
Gilles Peskine049c7532019-05-15 20:22:09 +02003369 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003370 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3372 key_bits = psa_get_key_bits( &attributes );
3373
3374 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3375 alg );
3376 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3377 * should be exact. */
3378 TEST_EQUAL( output_size,
3379 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3380 TEST_ASSERT( output_size <=
3381 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3382 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383
Steven Cooremand588ea12021-01-11 19:36:04 +01003384 status = psa_aead_encrypt( key, alg,
3385 nonce->x, nonce->len,
3386 additional_data->x, additional_data->len,
3387 input_data->x, input_data->len,
3388 output_data, output_size,
3389 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390
Ronald Cron28a45ed2021-02-09 20:35:42 +01003391 /* If the operation is not supported, just skip and not fail in case the
3392 * encryption involves a common limitation of cryptography hardwares and
3393 * an alternative implementation. */
3394 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003395 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003396 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3397 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003398 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003399
3400 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003401 ASSERT_COMPARE( expected_result->x, expected_result->len,
3402 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003403
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003405 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003407 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003408}
3409/* END_CASE */
3410
3411/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003412void aead_decrypt( int key_type_arg, data_t *key_data,
3413 int alg_arg,
3414 data_t *nonce,
3415 data_t *additional_data,
3416 data_t *input_data,
3417 data_t *expected_data,
3418 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003419{
Ronald Cron5425a212020-08-04 14:58:35 +02003420 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003421 psa_key_type_t key_type = key_type_arg;
3422 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003423 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003424 unsigned char *output_data = NULL;
3425 size_t output_size = 0;
3426 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003428 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003429 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003430
Gilles Peskine8817f612018-12-18 00:18:46 +01003431 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003432
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003433 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3434 psa_set_key_algorithm( &attributes, alg );
3435 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003436
Gilles Peskine049c7532019-05-15 20:22:09 +02003437 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003438 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003439 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3440 key_bits = psa_get_key_bits( &attributes );
3441
3442 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3443 alg );
3444 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3445 expected_result != PSA_ERROR_NOT_SUPPORTED )
3446 {
3447 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3448 * should be exact. */
3449 TEST_EQUAL( output_size,
3450 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3451 TEST_ASSERT( output_size <=
3452 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3453 }
3454 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003455
Steven Cooremand588ea12021-01-11 19:36:04 +01003456 status = psa_aead_decrypt( key, alg,
3457 nonce->x, nonce->len,
3458 additional_data->x,
3459 additional_data->len,
3460 input_data->x, input_data->len,
3461 output_data, output_size,
3462 &output_length );
3463
Ronald Cron28a45ed2021-02-09 20:35:42 +01003464 /* If the operation is not supported, just skip and not fail in case the
3465 * decryption involves a common limitation of cryptography hardwares and
3466 * an alternative implementation. */
3467 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003468 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003469 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3470 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003471 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003472
3473 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474
Gilles Peskine2d277862018-06-18 15:41:12 +02003475 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003476 ASSERT_COMPARE( expected_data->x, expected_data->len,
3477 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003480 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003481 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003482 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003483}
3484/* END_CASE */
3485
3486/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003487void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3488 int alg_arg,
3489 data_t *nonce,
3490 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003491 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003492 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003493 int do_test_data_chunked,
3494 int do_set_lengths,
3495 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003496{
Paul Elliottd3f82412021-06-16 16:52:21 +01003497 size_t ad_part_len = 0;
3498 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003499
Paul Elliotte64deda2021-09-09 14:07:23 +01003500 /* Ensure that either one part of the test or the other is done, i.e this
3501 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003502 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3503
3504 /* Temporary whilst we have algorithms that cannot support chunking */
3505 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003506 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003507 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3508 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003509 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003510 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003511
Paul Elliott329d5382021-07-22 17:10:45 +01003512 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003513 if( !aead_multipart_internal_func( key_type_arg, key_data,
3514 alg_arg, nonce,
3515 additional_data,
3516 ad_part_len,
3517 input_data, -1,
3518 do_set_lengths,
3519 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003520 1, 1, 0,
3521 ( ad_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003522 break;
3523
3524 /* length(0) part, length(ad_part_len) part, length(0) part... */
3525 mbedtls_test_set_step( 1000 + ad_part_len );
3526
3527 if( !aead_multipart_internal_func( key_type_arg, key_data,
3528 alg_arg, nonce,
3529 additional_data,
3530 ad_part_len,
3531 input_data, -1,
3532 do_set_lengths,
3533 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003534 1, 1, 1,
3535 ( ad_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003536 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003537 }
3538 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003539
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003540 /* Temporary whilst we have algorithms that cannot support chunking */
3541 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003542 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003543 for( data_part_len = 1; data_part_len <= input_data->len;
3544 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003545 {
Paul Elliott329d5382021-07-22 17:10:45 +01003546 /* Split data into length(data_part_len) parts. */
3547 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003548
3549 if( !aead_multipart_internal_func( key_type_arg, key_data,
3550 alg_arg, nonce,
3551 additional_data, -1,
3552 input_data, data_part_len,
3553 do_set_lengths,
3554 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003555 1, 1, 0,
3556 ( data_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003557 break;
3558
3559 /* length(0) part, length(data_part_len) part, length(0) part... */
3560 mbedtls_test_set_step( 3000 + data_part_len );
3561
3562 if( !aead_multipart_internal_func( key_type_arg, key_data,
3563 alg_arg, nonce,
3564 additional_data, -1,
3565 input_data, data_part_len,
3566 do_set_lengths,
3567 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003568 1, 1, 1,
3569 ( data_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003570 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003571 }
3572 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003573
Paul Elliott0023e0a2021-04-27 10:06:22 +01003574
Paul Elliott8fc45162021-06-23 16:06:01 +01003575 /* Goto is required to silence warnings about unused labels, as we
3576 * don't actually do any test assertions in this function. */
3577 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003578}
3579/* END_CASE */
3580
3581/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3583 int alg_arg,
3584 data_t *nonce,
3585 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003586 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003587 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003588 int do_test_data_chunked,
3589 int do_set_lengths,
3590 data_t *expected_output,
3591 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003592{
Paul Elliottd3f82412021-06-16 16:52:21 +01003593 size_t ad_part_len = 0;
3594 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003595
Paul Elliotte64deda2021-09-09 14:07:23 +01003596 /* Ensure that either one part of the test or the other is done, i.e this
3597 * test does something. */
3598 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3599
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003600 /* Temporary whilst we have algorithms that cannot support chunking */
3601 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003602 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003603 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3604 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003605 {
Paul Elliott329d5382021-07-22 17:10:45 +01003606 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003607 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003608
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003609 if( !aead_multipart_internal_func( key_type_arg, key_data,
3610 alg_arg, nonce,
3611 additional_data,
3612 ad_part_len,
3613 input_data, -1,
3614 do_set_lengths,
3615 expected_output,
3616 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003617 0, 0,
3618 ( ad_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003619 break;
3620
3621 /* length(0) part, length(ad_part_len) part, length(0) part... */
3622 mbedtls_test_set_step( 1000 + ad_part_len );
3623
3624 if( !aead_multipart_internal_func( key_type_arg, key_data,
3625 alg_arg, nonce,
3626 additional_data,
3627 ad_part_len,
3628 input_data, -1,
3629 do_set_lengths,
3630 expected_output,
3631 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003632 0, 1,
3633 ( ad_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003634 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003635 }
3636 }
3637
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003638 /* Temporary whilst we have algorithms that cannot support chunking */
3639 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003640 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003641 for( data_part_len = 1; data_part_len <= input_data->len;
3642 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003643 {
Paul Elliott329d5382021-07-22 17:10:45 +01003644 /* Split data into length(data_part_len) parts. */
3645 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003646
3647 if( !aead_multipart_internal_func( key_type_arg, key_data,
3648 alg_arg, nonce,
3649 additional_data, -1,
3650 input_data, data_part_len,
3651 do_set_lengths,
3652 expected_output,
3653 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003654 0, 0,
3655 ( data_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003656 break;
3657
3658 /* length(0) part, length(data_part_len) part, length(0) part... */
3659 mbedtls_test_set_step( 3000 + data_part_len );
3660
3661 if( !aead_multipart_internal_func( key_type_arg, key_data,
3662 alg_arg, nonce,
3663 additional_data, -1,
3664 input_data, data_part_len,
3665 do_set_lengths,
3666 expected_output,
3667 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003668 0, 1,
3669 ( data_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003670 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003671 }
3672 }
3673
Paul Elliott8fc45162021-06-23 16:06:01 +01003674 /* Goto is required to silence warnings about unused labels, as we
3675 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003676 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003677}
3678/* END_CASE */
3679
3680/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003681void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3682 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003683 int nonce_length,
3684 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003685 data_t *additional_data,
3686 data_t *input_data,
3687 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003688{
3689
3690 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3691 psa_key_type_t key_type = key_type_arg;
3692 psa_algorithm_t alg = alg_arg;
3693 psa_aead_operation_t operation;
3694 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3696 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003697 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003698 size_t actual_nonce_length = 0;
3699 size_t expected_nonce_length = expected_nonce_length_arg;
3700 unsigned char *output = NULL;
3701 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003702 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003703 size_t ciphertext_size = 0;
3704 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003705 size_t tag_length = 0;
3706 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003707
3708 PSA_ASSERT( psa_crypto_init( ) );
3709
3710 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3711 psa_set_key_algorithm( & attributes, alg );
3712 psa_set_key_type( & attributes, key_type );
3713
3714 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3715 &key ) );
3716
3717 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3718
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003719 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3720
Paul Elliottf1277632021-08-24 18:11:37 +01003721 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003722
Paul Elliottf1277632021-08-24 18:11:37 +01003723 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003724
Paul Elliottf1277632021-08-24 18:11:37 +01003725 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003726
Paul Elliottf1277632021-08-24 18:11:37 +01003727 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003728
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003729 operation = psa_aead_operation_init( );
3730
3731 status = psa_aead_encrypt_setup( &operation, key, alg );
3732
3733 /* If the operation is not supported, just skip and not fail in case the
3734 * encryption involves a common limitation of cryptography hardwares and
3735 * an alternative implementation. */
3736 if( status == PSA_ERROR_NOT_SUPPORTED )
3737 {
3738 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003739 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003740 }
3741
3742 PSA_ASSERT( status );
3743
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003744 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003745 nonce_length,
3746 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003747
Paul Elliott693bf312021-07-23 17:40:41 +01003748 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003749
Paul Elliottf1277632021-08-24 18:11:37 +01003750 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003751
Paul Elliottf1277632021-08-24 18:11:37 +01003752 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003753
Paul Elliott693bf312021-07-23 17:40:41 +01003754 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003755 {
3756
3757 /* Ensure we can still complete operation. */
3758
3759 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3760 additional_data->len ) );
3761
3762 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003763 output, output_size,
3764 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003765
Paul Elliottf1277632021-08-24 18:11:37 +01003766 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3767 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003768 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3769 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003770
3771exit:
3772 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003773 mbedtls_free( output );
3774 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003775 psa_aead_abort( &operation );
3776 PSA_DONE( );
3777}
3778/* END_CASE */
3779
3780/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003781void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3782 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003783 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003784 data_t *additional_data,
3785 data_t *input_data,
3786 int expected_status_arg )
3787{
3788
3789 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3790 psa_key_type_t key_type = key_type_arg;
3791 psa_algorithm_t alg = alg_arg;
3792 psa_aead_operation_t operation;
3793 uint8_t *nonce_buffer = NULL;
3794 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3795 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3796 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003797 unsigned char *output = NULL;
3798 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003799 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003800 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003801 size_t ciphertext_size = 0;
3802 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003803 size_t tag_length = 0;
3804 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003805 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003806
3807 PSA_ASSERT( psa_crypto_init( ) );
3808
3809 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3810 psa_set_key_algorithm( &attributes, alg );
3811 psa_set_key_type( &attributes, key_type );
3812
3813 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3814 &key ) );
3815
3816 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3817
3818 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3819
Paul Elliott6f0e7202021-08-25 12:57:18 +01003820 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003821
Paul Elliott6f0e7202021-08-25 12:57:18 +01003822 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003823
Paul Elliott6f0e7202021-08-25 12:57:18 +01003824 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003825
Paul Elliott6f0e7202021-08-25 12:57:18 +01003826 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003827
3828 operation = psa_aead_operation_init( );
3829
3830 status = psa_aead_encrypt_setup( &operation, key, alg );
3831
3832 /* If the operation is not supported, just skip and not fail in case the
3833 * encryption involves a common limitation of cryptography hardwares and
3834 * an alternative implementation. */
3835 if( status == PSA_ERROR_NOT_SUPPORTED )
3836 {
3837 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003838 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003839 }
3840
3841 PSA_ASSERT( status );
3842
Paul Elliott4023ffd2021-09-10 16:21:22 +01003843 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3844 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003845 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003846 /* Arbitrary size buffer, to test zero length valid buffer. */
3847 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003848 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003849 }
3850 else
3851 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003852 /* If length is zero, then this will return NULL. */
3853 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003854 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003855
Paul Elliott4023ffd2021-09-10 16:21:22 +01003856 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003857 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003858 for( index = 0; index < nonce_length - 1; ++index )
3859 {
3860 nonce_buffer[index] = 'a' + index;
3861 }
Paul Elliott66696b52021-08-16 18:42:41 +01003862 }
Paul Elliott863864a2021-07-23 17:28:31 +01003863 }
3864
Paul Elliott6f0e7202021-08-25 12:57:18 +01003865 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003866
Paul Elliott693bf312021-07-23 17:40:41 +01003867 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003868
3869 if( expected_status == PSA_SUCCESS )
3870 {
3871 /* Ensure we can still complete operation. */
3872
3873 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3874 additional_data->len ) );
3875
3876 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003877 output, output_size,
3878 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003879
Paul Elliott6f0e7202021-08-25 12:57:18 +01003880 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3881 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003882 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3883 }
3884
3885exit:
3886 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003887 mbedtls_free( output );
3888 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003889 mbedtls_free( nonce_buffer );
3890 psa_aead_abort( &operation );
3891 PSA_DONE( );
3892}
3893/* END_CASE */
3894
3895/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003896void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3897 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003898 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003899 data_t *nonce,
3900 data_t *additional_data,
3901 data_t *input_data,
3902 int expected_status_arg )
3903{
3904
3905 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3906 psa_key_type_t key_type = key_type_arg;
3907 psa_algorithm_t alg = alg_arg;
3908 psa_aead_operation_t operation;
3909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3910 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3911 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003912 unsigned char *output = NULL;
3913 unsigned char *ciphertext = NULL;
3914 size_t output_size = output_size_arg;
3915 size_t ciphertext_size = 0;
3916 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003917 size_t tag_length = 0;
3918 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3919
3920 PSA_ASSERT( psa_crypto_init( ) );
3921
3922 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3923 psa_set_key_algorithm( &attributes, alg );
3924 psa_set_key_type( &attributes, key_type );
3925
3926 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3927 &key ) );
3928
3929 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3930
Paul Elliottc6d11d02021-09-01 12:04:23 +01003931 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003932
Paul Elliottc6d11d02021-09-01 12:04:23 +01003933 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003934
Paul Elliottc6d11d02021-09-01 12:04:23 +01003935 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003936
3937 operation = psa_aead_operation_init( );
3938
3939 status = psa_aead_encrypt_setup( &operation, key, alg );
3940
3941 /* If the operation is not supported, just skip and not fail in case the
3942 * encryption involves a common limitation of cryptography hardwares and
3943 * an alternative implementation. */
3944 if( status == PSA_ERROR_NOT_SUPPORTED )
3945 {
3946 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3947 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3948 }
3949
3950 PSA_ASSERT( status );
3951
3952 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3953
3954 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3955 additional_data->len ) );
3956
3957 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003958 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003959
3960 TEST_EQUAL( status, expected_status );
3961
3962 if( expected_status == PSA_SUCCESS )
3963 {
3964 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003965 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3966 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003967 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3968 }
3969
3970exit:
3971 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003972 mbedtls_free( output );
3973 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003974 psa_aead_abort( &operation );
3975 PSA_DONE( );
3976}
3977/* END_CASE */
3978
Paul Elliott91b021e2021-07-23 18:52:31 +01003979/* BEGIN_CASE */
3980void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3981 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003982 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003983 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003984 data_t *nonce,
3985 data_t *additional_data,
3986 data_t *input_data,
3987 int expected_status_arg )
3988{
3989
3990 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3991 psa_key_type_t key_type = key_type_arg;
3992 psa_algorithm_t alg = alg_arg;
3993 psa_aead_operation_t operation;
3994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3995 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3996 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003997 unsigned char *ciphertext = NULL;
3998 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01003999 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004000 size_t ciphertext_size = 0;
4001 size_t ciphertext_length = 0;
4002 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004003 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004004 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004005
4006 PSA_ASSERT( psa_crypto_init( ) );
4007
4008 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4009 psa_set_key_algorithm( &attributes, alg );
4010 psa_set_key_type( &attributes, key_type );
4011
4012 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4013 &key ) );
4014
4015 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4016
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004017 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004018
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004019 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004020
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004021 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004022
Paul Elliott719c1322021-09-13 18:27:22 +01004023 ASSERT_ALLOC( tag_buffer, tag_size );
4024
Paul Elliott91b021e2021-07-23 18:52:31 +01004025 operation = psa_aead_operation_init( );
4026
4027 status = psa_aead_encrypt_setup( &operation, key, alg );
4028
4029 /* If the operation is not supported, just skip and not fail in case the
4030 * encryption involves a common limitation of cryptography hardwares and
4031 * an alternative implementation. */
4032 if( status == PSA_ERROR_NOT_SUPPORTED )
4033 {
4034 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4035 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4036 }
4037
4038 PSA_ASSERT( status );
4039
4040 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4041
4042 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4043 additional_data->len ) );
4044
4045 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004046 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004047
4048 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004049 status = psa_aead_finish( &operation, finish_ciphertext,
4050 finish_ciphertext_size,
4051 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004052 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004053
4054 TEST_EQUAL( status, expected_status );
4055
4056exit:
4057 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004058 mbedtls_free( ciphertext );
4059 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004060 psa_aead_abort( &operation );
4061 PSA_DONE( );
4062}
4063/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004064
4065/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004066void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4067 int alg_arg,
4068 data_t *nonce,
4069 data_t *additional_data,
4070 data_t *input_data )
4071{
4072 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4073 psa_key_type_t key_type = key_type_arg;
4074 psa_algorithm_t alg = alg_arg;
4075 psa_aead_operation_t operation;
4076 unsigned char *output_data = NULL;
4077 unsigned char *final_data = NULL;
4078 size_t output_size = 0;
4079 size_t finish_output_size = 0;
4080 size_t output_length = 0;
4081 size_t key_bits = 0;
4082 size_t tag_length = 0;
4083 size_t tag_size = 0;
4084 size_t nonce_length = 0;
4085 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4086 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4087 size_t output_part_length = 0;
4088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4089
4090 PSA_ASSERT( psa_crypto_init( ) );
4091
4092 psa_set_key_usage_flags( & attributes,
4093 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4094 psa_set_key_algorithm( & attributes, alg );
4095 psa_set_key_type( & attributes, key_type );
4096
4097 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4098 &key ) );
4099
4100 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4101 key_bits = psa_get_key_bits( &attributes );
4102
4103 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4104
4105 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4106
4107 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4108
4109 ASSERT_ALLOC( output_data, output_size );
4110
4111 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4112
4113 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4114
4115 ASSERT_ALLOC( final_data, finish_output_size );
4116
4117 /* Test all operations error without calling setup first. */
4118
4119 operation = psa_aead_operation_init( );
4120
4121 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4122 PSA_ERROR_BAD_STATE );
4123
4124 psa_aead_abort( &operation );
4125
4126 operation = psa_aead_operation_init( );
4127
4128 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4129 PSA_AEAD_NONCE_MAX_SIZE,
4130 &nonce_length ),
4131 PSA_ERROR_BAD_STATE );
4132
4133 psa_aead_abort( &operation );
4134
Paul Elliott481be342021-07-16 17:38:47 +01004135 /* ------------------------------------------------------- */
4136
Paul Elliottc23a9a02021-06-21 18:32:46 +01004137 operation = psa_aead_operation_init( );
4138
4139 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4140 input_data->len ),
4141 PSA_ERROR_BAD_STATE );
4142
4143 psa_aead_abort( &operation );
4144
Paul Elliott481be342021-07-16 17:38:47 +01004145 /* ------------------------------------------------------- */
4146
Paul Elliottc23a9a02021-06-21 18:32:46 +01004147 operation = psa_aead_operation_init( );
4148
4149 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4150 additional_data->len ),
4151 PSA_ERROR_BAD_STATE );
4152
4153 psa_aead_abort( &operation );
4154
Paul Elliott481be342021-07-16 17:38:47 +01004155 /* ------------------------------------------------------- */
4156
Paul Elliottc23a9a02021-06-21 18:32:46 +01004157 operation = psa_aead_operation_init( );
4158
4159 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4160 input_data->len, output_data,
4161 output_size, &output_length ),
4162 PSA_ERROR_BAD_STATE );
4163
4164 psa_aead_abort( &operation );
4165
Paul Elliott481be342021-07-16 17:38:47 +01004166 /* ------------------------------------------------------- */
4167
Paul Elliottc23a9a02021-06-21 18:32:46 +01004168 operation = psa_aead_operation_init( );
4169
4170 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4171 finish_output_size,
4172 &output_part_length,
4173 tag_buffer, tag_length,
4174 &tag_size ),
4175 PSA_ERROR_BAD_STATE );
4176
4177 psa_aead_abort( &operation );
4178
Paul Elliott481be342021-07-16 17:38:47 +01004179 /* ------------------------------------------------------- */
4180
Paul Elliottc23a9a02021-06-21 18:32:46 +01004181 operation = psa_aead_operation_init( );
4182
4183 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4184 finish_output_size,
4185 &output_part_length,
4186 tag_buffer,
4187 tag_length ),
4188 PSA_ERROR_BAD_STATE );
4189
4190 psa_aead_abort( &operation );
4191
4192 /* Test for double setups. */
4193
4194 operation = psa_aead_operation_init( );
4195
4196 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4197
4198 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4199 PSA_ERROR_BAD_STATE );
4200
4201 psa_aead_abort( &operation );
4202
Paul Elliott481be342021-07-16 17:38:47 +01004203 /* ------------------------------------------------------- */
4204
Paul Elliottc23a9a02021-06-21 18:32:46 +01004205 operation = psa_aead_operation_init( );
4206
4207 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4208
4209 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4210 PSA_ERROR_BAD_STATE );
4211
4212 psa_aead_abort( &operation );
4213
Paul Elliott374a2be2021-07-16 17:53:40 +01004214 /* ------------------------------------------------------- */
4215
4216 operation = psa_aead_operation_init( );
4217
4218 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4219
4220 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4221 PSA_ERROR_BAD_STATE );
4222
4223 psa_aead_abort( &operation );
4224
4225 /* ------------------------------------------------------- */
4226
4227 operation = psa_aead_operation_init( );
4228
4229 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4230
4231 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4232 PSA_ERROR_BAD_STATE );
4233
4234 psa_aead_abort( &operation );
4235
Paul Elliottc23a9a02021-06-21 18:32:46 +01004236 /* Test for not setting a nonce. */
4237
4238 operation = psa_aead_operation_init( );
4239
4240 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4241
4242 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4243 additional_data->len ),
4244 PSA_ERROR_BAD_STATE );
4245
4246 psa_aead_abort( &operation );
4247
Paul Elliott7f628422021-09-01 12:08:29 +01004248 /* ------------------------------------------------------- */
4249
4250 operation = psa_aead_operation_init( );
4251
4252 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4253
4254 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4255 input_data->len, output_data,
4256 output_size, &output_length ),
4257 PSA_ERROR_BAD_STATE );
4258
4259 psa_aead_abort( &operation );
4260
Paul Elliottc23a9a02021-06-21 18:32:46 +01004261 /* Test for double setting nonce. */
4262
4263 operation = psa_aead_operation_init( );
4264
4265 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4266
4267 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4268
4269 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4270 PSA_ERROR_BAD_STATE );
4271
4272 psa_aead_abort( &operation );
4273
Paul Elliott374a2be2021-07-16 17:53:40 +01004274 /* Test for double generating nonce. */
4275
4276 operation = psa_aead_operation_init( );
4277
4278 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4279
4280 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4281 PSA_AEAD_NONCE_MAX_SIZE,
4282 &nonce_length ) );
4283
4284 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4285 PSA_AEAD_NONCE_MAX_SIZE,
4286 &nonce_length ),
4287 PSA_ERROR_BAD_STATE );
4288
4289
4290 psa_aead_abort( &operation );
4291
4292 /* Test for generate nonce then set and vice versa */
4293
4294 operation = psa_aead_operation_init( );
4295
4296 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4297
4298 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4299 PSA_AEAD_NONCE_MAX_SIZE,
4300 &nonce_length ) );
4301
4302 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4303 PSA_ERROR_BAD_STATE );
4304
4305 psa_aead_abort( &operation );
4306
4307 /* ------------------------------------------------------- */
4308
4309 operation = psa_aead_operation_init( );
4310
4311 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4312
4313 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4314
4315 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4316 PSA_AEAD_NONCE_MAX_SIZE,
4317 &nonce_length ),
4318 PSA_ERROR_BAD_STATE );
4319
4320 psa_aead_abort( &operation );
4321
Paul Elliott7220cae2021-06-22 17:25:57 +01004322 /* Test for generating nonce in decrypt setup. */
4323
4324 operation = psa_aead_operation_init( );
4325
4326 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4327
4328 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4329 PSA_AEAD_NONCE_MAX_SIZE,
4330 &nonce_length ),
4331 PSA_ERROR_BAD_STATE );
4332
4333 psa_aead_abort( &operation );
4334
Paul Elliottc23a9a02021-06-21 18:32:46 +01004335 /* Test for setting lengths twice. */
4336
4337 operation = psa_aead_operation_init( );
4338
4339 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4340
4341 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4342
4343 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4344 input_data->len ) );
4345
4346 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4347 input_data->len ),
4348 PSA_ERROR_BAD_STATE );
4349
4350 psa_aead_abort( &operation );
4351
4352 /* Test for setting lengths after already starting data. */
4353
4354 operation = psa_aead_operation_init( );
4355
4356 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4357
4358 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4359
4360 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4361 input_data->len, output_data,
4362 output_size, &output_length ) );
4363
4364 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4365 input_data->len ),
4366 PSA_ERROR_BAD_STATE );
4367
4368 psa_aead_abort( &operation );
4369
Paul Elliott243080c2021-07-21 19:01:17 +01004370 /* Test for not sending any additional data or data after setting non zero
4371 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004372
4373 operation = psa_aead_operation_init( );
4374
4375 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4376
4377 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4378
4379 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4380 input_data->len ) );
4381
4382 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4383 finish_output_size,
4384 &output_part_length,
4385 tag_buffer, tag_length,
4386 &tag_size ),
4387 PSA_ERROR_INVALID_ARGUMENT );
4388
4389 psa_aead_abort( &operation );
4390
Paul Elliott243080c2021-07-21 19:01:17 +01004391 /* Test for not sending any additional data or data after setting non-zero
4392 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004393
4394 operation = psa_aead_operation_init( );
4395
4396 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4397
4398 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4399
4400 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4401 input_data->len ) );
4402
4403 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4404 finish_output_size,
4405 &output_part_length,
4406 tag_buffer,
4407 tag_length ),
4408 PSA_ERROR_INVALID_ARGUMENT );
4409
4410 psa_aead_abort( &operation );
4411
Paul Elliott243080c2021-07-21 19:01:17 +01004412 /* Test for not sending any additional data after setting a non-zero length
4413 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004414
4415 operation = psa_aead_operation_init( );
4416
4417 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4418
4419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4420
4421 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4422 input_data->len ) );
4423
4424 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4425 input_data->len, output_data,
4426 output_size, &output_length ),
4427 PSA_ERROR_INVALID_ARGUMENT );
4428
4429 psa_aead_abort( &operation );
4430
Paul Elliottb0450fe2021-09-01 15:06:26 +01004431 /* Test for sending too much additional data after setting lengths. */
4432
4433 operation = psa_aead_operation_init( );
4434
4435 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4436
4437 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4438
4439 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4440
4441
4442 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4443 additional_data->len ),
4444 PSA_ERROR_INVALID_ARGUMENT );
4445
4446 psa_aead_abort( &operation );
4447
4448 /* Test for sending too much data after setting lengths. */
4449
4450 operation = psa_aead_operation_init( );
4451
4452 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4453
4454 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4455
4456 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4457
4458 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4459 input_data->len, output_data,
4460 output_size, &output_length ),
4461 PSA_ERROR_INVALID_ARGUMENT );
4462
4463 psa_aead_abort( &operation );
4464
Paul Elliottc23a9a02021-06-21 18:32:46 +01004465 /* Test sending additional data after data. */
4466
4467 operation = psa_aead_operation_init( );
4468
4469 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4470
4471 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4472
4473 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4474 input_data->len, output_data,
4475 output_size, &output_length ) );
4476
4477 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4478 additional_data->len ),
4479 PSA_ERROR_BAD_STATE );
4480
4481 psa_aead_abort( &operation );
4482
Paul Elliott534d0b42021-06-22 19:15:20 +01004483 /* Test calling finish on decryption. */
4484
4485 operation = psa_aead_operation_init( );
4486
4487 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4488
4489 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4490
4491 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4492 finish_output_size,
4493 &output_part_length,
4494 tag_buffer, tag_length,
4495 &tag_size ),
4496 PSA_ERROR_BAD_STATE );
4497
4498 psa_aead_abort( &operation );
4499
4500 /* Test calling verify on encryption. */
4501
4502 operation = psa_aead_operation_init( );
4503
4504 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4505
4506 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4507
4508 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4509 finish_output_size,
4510 &output_part_length,
4511 tag_buffer,
4512 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004513 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004514
4515 psa_aead_abort( &operation );
4516
4517
Paul Elliottc23a9a02021-06-21 18:32:46 +01004518exit:
4519 psa_destroy_key( key );
4520 psa_aead_abort( &operation );
4521 mbedtls_free( output_data );
4522 mbedtls_free( final_data );
4523 PSA_DONE( );
4524}
4525/* END_CASE */
4526
4527/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004528void signature_size( int type_arg,
4529 int bits,
4530 int alg_arg,
4531 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004532{
4533 psa_key_type_t type = type_arg;
4534 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004535 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004536
Gilles Peskinefe11b722018-12-18 00:24:04 +01004537 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004538
Gilles Peskinee59236f2018-01-27 23:32:46 +01004539exit:
4540 ;
4541}
4542/* END_CASE */
4543
4544/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004545void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4546 int alg_arg, data_t *input_data,
4547 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004548{
Ronald Cron5425a212020-08-04 14:58:35 +02004549 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004550 psa_key_type_t key_type = key_type_arg;
4551 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004552 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004553 unsigned char *signature = NULL;
4554 size_t signature_size;
4555 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004556 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004557
Gilles Peskine8817f612018-12-18 00:18:46 +01004558 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004559
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004560 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004561 psa_set_key_algorithm( &attributes, alg );
4562 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004563
Gilles Peskine049c7532019-05-15 20:22:09 +02004564 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004565 &key ) );
4566 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004567 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004568
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004569 /* Allocate a buffer which has the size advertized by the
4570 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004571 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004572 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004573 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004574 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004575 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004576
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004577 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004578 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004579 input_data->x, input_data->len,
4580 signature, signature_size,
4581 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004582 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004583 ASSERT_COMPARE( output_data->x, output_data->len,
4584 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004585
4586exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004587 /*
4588 * Key attributes may have been returned by psa_get_key_attributes()
4589 * thus reset them as required.
4590 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004591 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004592
Ronald Cron5425a212020-08-04 14:58:35 +02004593 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004594 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004595 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004596}
4597/* END_CASE */
4598
4599/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004600void sign_hash_fail( int key_type_arg, data_t *key_data,
4601 int alg_arg, data_t *input_data,
4602 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004603{
Ronald Cron5425a212020-08-04 14:58:35 +02004604 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004605 psa_key_type_t key_type = key_type_arg;
4606 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004607 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004608 psa_status_t actual_status;
4609 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004610 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004611 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004613
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004614 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004615
Gilles Peskine8817f612018-12-18 00:18:46 +01004616 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004617
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004618 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004619 psa_set_key_algorithm( &attributes, alg );
4620 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004621
Gilles Peskine049c7532019-05-15 20:22:09 +02004622 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004623 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004624
Ronald Cron5425a212020-08-04 14:58:35 +02004625 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004626 input_data->x, input_data->len,
4627 signature, signature_size,
4628 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004629 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004630 /* The value of *signature_length is unspecified on error, but
4631 * whatever it is, it should be less than signature_size, so that
4632 * if the caller tries to read *signature_length bytes without
4633 * checking the error code then they don't overflow a buffer. */
4634 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004635
4636exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004637 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004638 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004639 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004640 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004641}
4642/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004643
4644/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004645void sign_verify_hash( int key_type_arg, data_t *key_data,
4646 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004647{
Ronald Cron5425a212020-08-04 14:58:35 +02004648 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004649 psa_key_type_t key_type = key_type_arg;
4650 psa_algorithm_t alg = alg_arg;
4651 size_t key_bits;
4652 unsigned char *signature = NULL;
4653 size_t signature_size;
4654 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004655 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004656
Gilles Peskine8817f612018-12-18 00:18:46 +01004657 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004658
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004659 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004660 psa_set_key_algorithm( &attributes, alg );
4661 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004662
Gilles Peskine049c7532019-05-15 20:22:09 +02004663 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004664 &key ) );
4665 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004666 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004667
4668 /* Allocate a buffer which has the size advertized by the
4669 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004670 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004671 key_bits, alg );
4672 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004673 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004674 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004675
4676 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004677 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004678 input_data->x, input_data->len,
4679 signature, signature_size,
4680 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004681 /* Check that the signature length looks sensible. */
4682 TEST_ASSERT( signature_length <= signature_size );
4683 TEST_ASSERT( signature_length > 0 );
4684
4685 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004686 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004687 input_data->x, input_data->len,
4688 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004689
4690 if( input_data->len != 0 )
4691 {
4692 /* Flip a bit in the input and verify that the signature is now
4693 * detected as invalid. Flip a bit at the beginning, not at the end,
4694 * because ECDSA may ignore the last few bits of the input. */
4695 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004696 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004697 input_data->x, input_data->len,
4698 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004699 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004700 }
4701
4702exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004703 /*
4704 * Key attributes may have been returned by psa_get_key_attributes()
4705 * thus reset them as required.
4706 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004707 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004708
Ronald Cron5425a212020-08-04 14:58:35 +02004709 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004710 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004711 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004712}
4713/* END_CASE */
4714
4715/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004716void verify_hash( int key_type_arg, data_t *key_data,
4717 int alg_arg, data_t *hash_data,
4718 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004719{
Ronald Cron5425a212020-08-04 14:58:35 +02004720 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004721 psa_key_type_t key_type = key_type_arg;
4722 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004723 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004724
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004725 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004726
Gilles Peskine8817f612018-12-18 00:18:46 +01004727 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004728
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004729 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004730 psa_set_key_algorithm( &attributes, alg );
4731 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004732
Gilles Peskine049c7532019-05-15 20:22:09 +02004733 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004734 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004735
Ronald Cron5425a212020-08-04 14:58:35 +02004736 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004737 hash_data->x, hash_data->len,
4738 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004739
itayzafrir5c753392018-05-08 11:18:38 +03004740exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004741 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004742 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004743 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004744}
4745/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004746
4747/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004748void verify_hash_fail( int key_type_arg, data_t *key_data,
4749 int alg_arg, data_t *hash_data,
4750 data_t *signature_data,
4751 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004752{
Ronald Cron5425a212020-08-04 14:58:35 +02004753 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004754 psa_key_type_t key_type = key_type_arg;
4755 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004756 psa_status_t actual_status;
4757 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004759
Gilles Peskine8817f612018-12-18 00:18:46 +01004760 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004761
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004762 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004763 psa_set_key_algorithm( &attributes, alg );
4764 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004765
Gilles Peskine049c7532019-05-15 20:22:09 +02004766 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004767 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004768
Ronald Cron5425a212020-08-04 14:58:35 +02004769 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004770 hash_data->x, hash_data->len,
4771 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004772 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004773
4774exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004775 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004776 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004777 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004778}
4779/* END_CASE */
4780
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004781/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004782void sign_message_deterministic( int key_type_arg,
4783 data_t *key_data,
4784 int alg_arg,
4785 data_t *input_data,
4786 data_t *output_data )
4787{
4788 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4789 psa_key_type_t key_type = key_type_arg;
4790 psa_algorithm_t alg = alg_arg;
4791 size_t key_bits;
4792 unsigned char *signature = NULL;
4793 size_t signature_size;
4794 size_t signature_length = 0xdeadbeef;
4795 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4796
4797 PSA_ASSERT( psa_crypto_init( ) );
4798
4799 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4800 psa_set_key_algorithm( &attributes, alg );
4801 psa_set_key_type( &attributes, key_type );
4802
4803 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4804 &key ) );
4805 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4806 key_bits = psa_get_key_bits( &attributes );
4807
4808 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4809 TEST_ASSERT( signature_size != 0 );
4810 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4811 ASSERT_ALLOC( signature, signature_size );
4812
4813 PSA_ASSERT( psa_sign_message( key, alg,
4814 input_data->x, input_data->len,
4815 signature, signature_size,
4816 &signature_length ) );
4817
4818 ASSERT_COMPARE( output_data->x, output_data->len,
4819 signature, signature_length );
4820
4821exit:
4822 psa_reset_key_attributes( &attributes );
4823
4824 psa_destroy_key( key );
4825 mbedtls_free( signature );
4826 PSA_DONE( );
4827
4828}
4829/* END_CASE */
4830
4831/* BEGIN_CASE */
4832void sign_message_fail( int key_type_arg,
4833 data_t *key_data,
4834 int alg_arg,
4835 data_t *input_data,
4836 int signature_size_arg,
4837 int expected_status_arg )
4838{
4839 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4840 psa_key_type_t key_type = key_type_arg;
4841 psa_algorithm_t alg = alg_arg;
4842 size_t signature_size = signature_size_arg;
4843 psa_status_t actual_status;
4844 psa_status_t expected_status = expected_status_arg;
4845 unsigned char *signature = NULL;
4846 size_t signature_length = 0xdeadbeef;
4847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4848
4849 ASSERT_ALLOC( signature, signature_size );
4850
4851 PSA_ASSERT( psa_crypto_init( ) );
4852
4853 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4854 psa_set_key_algorithm( &attributes, alg );
4855 psa_set_key_type( &attributes, key_type );
4856
4857 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4858 &key ) );
4859
4860 actual_status = psa_sign_message( key, alg,
4861 input_data->x, input_data->len,
4862 signature, signature_size,
4863 &signature_length );
4864 TEST_EQUAL( actual_status, expected_status );
4865 /* The value of *signature_length is unspecified on error, but
4866 * whatever it is, it should be less than signature_size, so that
4867 * if the caller tries to read *signature_length bytes without
4868 * checking the error code then they don't overflow a buffer. */
4869 TEST_ASSERT( signature_length <= signature_size );
4870
4871exit:
4872 psa_reset_key_attributes( &attributes );
4873 psa_destroy_key( key );
4874 mbedtls_free( signature );
4875 PSA_DONE( );
4876}
4877/* END_CASE */
4878
4879/* BEGIN_CASE */
4880void sign_verify_message( int key_type_arg,
4881 data_t *key_data,
4882 int alg_arg,
4883 data_t *input_data )
4884{
4885 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4886 psa_key_type_t key_type = key_type_arg;
4887 psa_algorithm_t alg = alg_arg;
4888 size_t key_bits;
4889 unsigned char *signature = NULL;
4890 size_t signature_size;
4891 size_t signature_length = 0xdeadbeef;
4892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4893
4894 PSA_ASSERT( psa_crypto_init( ) );
4895
4896 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4897 PSA_KEY_USAGE_VERIFY_MESSAGE );
4898 psa_set_key_algorithm( &attributes, alg );
4899 psa_set_key_type( &attributes, key_type );
4900
4901 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4902 &key ) );
4903 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4904 key_bits = psa_get_key_bits( &attributes );
4905
4906 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4907 TEST_ASSERT( signature_size != 0 );
4908 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4909 ASSERT_ALLOC( signature, signature_size );
4910
4911 PSA_ASSERT( psa_sign_message( key, alg,
4912 input_data->x, input_data->len,
4913 signature, signature_size,
4914 &signature_length ) );
4915 TEST_ASSERT( signature_length <= signature_size );
4916 TEST_ASSERT( signature_length > 0 );
4917
4918 PSA_ASSERT( psa_verify_message( key, alg,
4919 input_data->x, input_data->len,
4920 signature, signature_length ) );
4921
4922 if( input_data->len != 0 )
4923 {
4924 /* Flip a bit in the input and verify that the signature is now
4925 * detected as invalid. Flip a bit at the beginning, not at the end,
4926 * because ECDSA may ignore the last few bits of the input. */
4927 input_data->x[0] ^= 1;
4928 TEST_EQUAL( psa_verify_message( key, alg,
4929 input_data->x, input_data->len,
4930 signature, signature_length ),
4931 PSA_ERROR_INVALID_SIGNATURE );
4932 }
4933
4934exit:
4935 psa_reset_key_attributes( &attributes );
4936
4937 psa_destroy_key( key );
4938 mbedtls_free( signature );
4939 PSA_DONE( );
4940}
4941/* END_CASE */
4942
4943/* BEGIN_CASE */
4944void verify_message( int key_type_arg,
4945 data_t *key_data,
4946 int alg_arg,
4947 data_t *input_data,
4948 data_t *signature_data )
4949{
4950 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4951 psa_key_type_t key_type = key_type_arg;
4952 psa_algorithm_t alg = alg_arg;
4953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4954
4955 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4956
4957 PSA_ASSERT( psa_crypto_init( ) );
4958
4959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4960 psa_set_key_algorithm( &attributes, alg );
4961 psa_set_key_type( &attributes, key_type );
4962
4963 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4964 &key ) );
4965
4966 PSA_ASSERT( psa_verify_message( key, alg,
4967 input_data->x, input_data->len,
4968 signature_data->x, signature_data->len ) );
4969
4970exit:
4971 psa_reset_key_attributes( &attributes );
4972 psa_destroy_key( key );
4973 PSA_DONE( );
4974}
4975/* END_CASE */
4976
4977/* BEGIN_CASE */
4978void verify_message_fail( int key_type_arg,
4979 data_t *key_data,
4980 int alg_arg,
4981 data_t *hash_data,
4982 data_t *signature_data,
4983 int expected_status_arg )
4984{
4985 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4986 psa_key_type_t key_type = key_type_arg;
4987 psa_algorithm_t alg = alg_arg;
4988 psa_status_t actual_status;
4989 psa_status_t expected_status = expected_status_arg;
4990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4991
4992 PSA_ASSERT( psa_crypto_init( ) );
4993
4994 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4995 psa_set_key_algorithm( &attributes, alg );
4996 psa_set_key_type( &attributes, key_type );
4997
4998 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4999 &key ) );
5000
5001 actual_status = psa_verify_message( key, alg,
5002 hash_data->x, hash_data->len,
5003 signature_data->x,
5004 signature_data->len );
5005 TEST_EQUAL( actual_status, expected_status );
5006
5007exit:
5008 psa_reset_key_attributes( &attributes );
5009 psa_destroy_key( key );
5010 PSA_DONE( );
5011}
5012/* END_CASE */
5013
5014/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005015void asymmetric_encrypt( int key_type_arg,
5016 data_t *key_data,
5017 int alg_arg,
5018 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005019 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005020 int expected_output_length_arg,
5021 int expected_status_arg )
5022{
Ronald Cron5425a212020-08-04 14:58:35 +02005023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005024 psa_key_type_t key_type = key_type_arg;
5025 psa_algorithm_t alg = alg_arg;
5026 size_t expected_output_length = expected_output_length_arg;
5027 size_t key_bits;
5028 unsigned char *output = NULL;
5029 size_t output_size;
5030 size_t output_length = ~0;
5031 psa_status_t actual_status;
5032 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005033 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005034
Gilles Peskine8817f612018-12-18 00:18:46 +01005035 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005036
Gilles Peskine656896e2018-06-29 19:12:28 +02005037 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005038 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5039 psa_set_key_algorithm( &attributes, alg );
5040 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005041 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005042 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005043
5044 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005045 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005046 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005047
Gilles Peskine656896e2018-06-29 19:12:28 +02005048 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005049 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005050 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005051
5052 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005053 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005054 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005055 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005056 output, output_size,
5057 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005058 TEST_EQUAL( actual_status, expected_status );
5059 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005060
Gilles Peskine68428122018-06-30 18:42:41 +02005061 /* If the label is empty, the test framework puts a non-null pointer
5062 * in label->x. Test that a null pointer works as well. */
5063 if( label->len == 0 )
5064 {
5065 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005066 if( output_size != 0 )
5067 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005068 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005069 input_data->x, input_data->len,
5070 NULL, label->len,
5071 output, output_size,
5072 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005073 TEST_EQUAL( actual_status, expected_status );
5074 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005075 }
5076
Gilles Peskine656896e2018-06-29 19:12:28 +02005077exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005078 /*
5079 * Key attributes may have been returned by psa_get_key_attributes()
5080 * thus reset them as required.
5081 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005082 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005083
Ronald Cron5425a212020-08-04 14:58:35 +02005084 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005085 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005086 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005087}
5088/* END_CASE */
5089
5090/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005091void asymmetric_encrypt_decrypt( int key_type_arg,
5092 data_t *key_data,
5093 int alg_arg,
5094 data_t *input_data,
5095 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005096{
Ronald Cron5425a212020-08-04 14:58:35 +02005097 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005098 psa_key_type_t key_type = key_type_arg;
5099 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005100 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005101 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005102 size_t output_size;
5103 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005104 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005105 size_t output2_size;
5106 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005108
Gilles Peskine8817f612018-12-18 00:18:46 +01005109 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005110
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005111 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5112 psa_set_key_algorithm( &attributes, alg );
5113 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005114
Gilles Peskine049c7532019-05-15 20:22:09 +02005115 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005116 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005117
5118 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005119 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005120 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005121
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005122 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005123 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005124 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005125
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005126 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005127 TEST_ASSERT( output2_size <=
5128 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5129 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005130 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005131
Gilles Peskineeebd7382018-06-08 18:11:54 +02005132 /* We test encryption by checking that encrypt-then-decrypt gives back
5133 * the original plaintext because of the non-optional random
5134 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005135 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005136 input_data->x, input_data->len,
5137 label->x, label->len,
5138 output, output_size,
5139 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005140 /* We don't know what ciphertext length to expect, but check that
5141 * it looks sensible. */
5142 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005143
Ronald Cron5425a212020-08-04 14:58:35 +02005144 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005145 output, output_length,
5146 label->x, label->len,
5147 output2, output2_size,
5148 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005149 ASSERT_COMPARE( input_data->x, input_data->len,
5150 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005151
5152exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005153 /*
5154 * Key attributes may have been returned by psa_get_key_attributes()
5155 * thus reset them as required.
5156 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005157 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005158
Ronald Cron5425a212020-08-04 14:58:35 +02005159 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005160 mbedtls_free( output );
5161 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005162 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005163}
5164/* END_CASE */
5165
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005166/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005167void asymmetric_decrypt( int key_type_arg,
5168 data_t *key_data,
5169 int alg_arg,
5170 data_t *input_data,
5171 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005172 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005173{
Ronald Cron5425a212020-08-04 14:58:35 +02005174 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005175 psa_key_type_t key_type = key_type_arg;
5176 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005177 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005178 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005179 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005180 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005181 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005182
Gilles Peskine8817f612018-12-18 00:18:46 +01005183 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005184
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005185 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5186 psa_set_key_algorithm( &attributes, alg );
5187 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005188
Gilles Peskine049c7532019-05-15 20:22:09 +02005189 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005190 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005191
gabor-mezei-armceface22021-01-21 12:26:17 +01005192 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5193 key_bits = psa_get_key_bits( &attributes );
5194
5195 /* Determine the maximum ciphertext length */
5196 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5197 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5198 ASSERT_ALLOC( output, output_size );
5199
Ronald Cron5425a212020-08-04 14:58:35 +02005200 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005201 input_data->x, input_data->len,
5202 label->x, label->len,
5203 output,
5204 output_size,
5205 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005206 ASSERT_COMPARE( expected_data->x, expected_data->len,
5207 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005208
Gilles Peskine68428122018-06-30 18:42:41 +02005209 /* If the label is empty, the test framework puts a non-null pointer
5210 * in label->x. Test that a null pointer works as well. */
5211 if( label->len == 0 )
5212 {
5213 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005214 if( output_size != 0 )
5215 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005216 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005217 input_data->x, input_data->len,
5218 NULL, label->len,
5219 output,
5220 output_size,
5221 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005222 ASSERT_COMPARE( expected_data->x, expected_data->len,
5223 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005224 }
5225
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005226exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005227 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005228 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005229 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005230 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005231}
5232/* END_CASE */
5233
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005234/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005235void asymmetric_decrypt_fail( int key_type_arg,
5236 data_t *key_data,
5237 int alg_arg,
5238 data_t *input_data,
5239 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005240 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005241 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005242{
Ronald Cron5425a212020-08-04 14:58:35 +02005243 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005244 psa_key_type_t key_type = key_type_arg;
5245 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005246 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005247 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005248 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005249 psa_status_t actual_status;
5250 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005252
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005253 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005254
Gilles Peskine8817f612018-12-18 00:18:46 +01005255 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005256
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5258 psa_set_key_algorithm( &attributes, alg );
5259 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005260
Gilles Peskine049c7532019-05-15 20:22:09 +02005261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005262 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005263
Ronald Cron5425a212020-08-04 14:58:35 +02005264 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005265 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005266 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005267 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005268 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005269 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005270 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005271
Gilles Peskine68428122018-06-30 18:42:41 +02005272 /* If the label is empty, the test framework puts a non-null pointer
5273 * in label->x. Test that a null pointer works as well. */
5274 if( label->len == 0 )
5275 {
5276 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005277 if( output_size != 0 )
5278 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005279 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005280 input_data->x, input_data->len,
5281 NULL, label->len,
5282 output, output_size,
5283 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005284 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005285 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005286 }
5287
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005288exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005289 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005290 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005291 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005292 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005293}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005294/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005295
5296/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005297void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005298{
5299 /* Test each valid way of initializing the object, except for `= {0}`, as
5300 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5301 * though it's OK by the C standard. We could test for this, but we'd need
5302 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005303 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005304 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5305 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5306 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005307
5308 memset( &zero, 0, sizeof( zero ) );
5309
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005310 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005311 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005312 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005313 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005314 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005315 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005316 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005317
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005318 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005319 PSA_ASSERT( psa_key_derivation_abort(&func) );
5320 PSA_ASSERT( psa_key_derivation_abort(&init) );
5321 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005322}
5323/* END_CASE */
5324
Janos Follath16de4a42019-06-13 16:32:24 +01005325/* BEGIN_CASE */
5326void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005327{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005328 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005329 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005330 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005331
Gilles Peskine8817f612018-12-18 00:18:46 +01005332 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005333
Janos Follath16de4a42019-06-13 16:32:24 +01005334 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005335 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005336
5337exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005339 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005340}
5341/* END_CASE */
5342
Janos Follathaf3c2a02019-06-12 12:34:34 +01005343/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005344void derive_set_capacity( int alg_arg, int capacity_arg,
5345 int expected_status_arg )
5346{
5347 psa_algorithm_t alg = alg_arg;
5348 size_t capacity = capacity_arg;
5349 psa_status_t expected_status = expected_status_arg;
5350 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5351
5352 PSA_ASSERT( psa_crypto_init( ) );
5353
5354 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5355
5356 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5357 expected_status );
5358
5359exit:
5360 psa_key_derivation_abort( &operation );
5361 PSA_DONE( );
5362}
5363/* END_CASE */
5364
5365/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005366void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005367 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005368 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005369 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005370 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005371 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005372 int expected_status_arg3,
5373 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005374{
5375 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005376 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5377 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005378 psa_status_t expected_statuses[] = {expected_status_arg1,
5379 expected_status_arg2,
5380 expected_status_arg3};
5381 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005382 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5383 MBEDTLS_SVC_KEY_ID_INIT,
5384 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005385 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5386 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5387 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005388 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005389 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005390 psa_status_t expected_output_status = expected_output_status_arg;
5391 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005392
5393 PSA_ASSERT( psa_crypto_init( ) );
5394
5395 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5396 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005397
5398 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5399
5400 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5401 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005402 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005403 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005404 psa_set_key_type( &attributes, key_types[i] );
5405 PSA_ASSERT( psa_import_key( &attributes,
5406 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005407 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005408 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5409 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5410 {
5411 // When taking a private key as secret input, use key agreement
5412 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005413 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5414 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005415 expected_statuses[i] );
5416 }
5417 else
5418 {
5419 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005420 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005421 expected_statuses[i] );
5422 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005423 }
5424 else
5425 {
5426 TEST_EQUAL( psa_key_derivation_input_bytes(
5427 &operation, steps[i],
5428 inputs[i]->x, inputs[i]->len ),
5429 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005430 }
5431 }
5432
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005433 if( output_key_type != PSA_KEY_TYPE_NONE )
5434 {
5435 psa_reset_key_attributes( &attributes );
5436 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5437 psa_set_key_bits( &attributes, 8 );
5438 actual_output_status =
5439 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005440 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005441 }
5442 else
5443 {
5444 uint8_t buffer[1];
5445 actual_output_status =
5446 psa_key_derivation_output_bytes( &operation,
5447 buffer, sizeof( buffer ) );
5448 }
5449 TEST_EQUAL( actual_output_status, expected_output_status );
5450
Janos Follathaf3c2a02019-06-12 12:34:34 +01005451exit:
5452 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005453 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5454 psa_destroy_key( keys[i] );
5455 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005456 PSA_DONE( );
5457}
5458/* END_CASE */
5459
Janos Follathd958bb72019-07-03 15:02:16 +01005460/* BEGIN_CASE */
5461void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005462{
Janos Follathd958bb72019-07-03 15:02:16 +01005463 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005464 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005465 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005467 unsigned char input1[] = "Input 1";
5468 size_t input1_length = sizeof( input1 );
5469 unsigned char input2[] = "Input 2";
5470 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005471 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005472 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005473 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5474 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5475 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005477
Gilles Peskine8817f612018-12-18 00:18:46 +01005478 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005479
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5481 psa_set_key_algorithm( &attributes, alg );
5482 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005483
Gilles Peskine73676cb2019-05-15 20:15:10 +02005484 PSA_ASSERT( psa_import_key( &attributes,
5485 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005486 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005487
5488 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005489 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5490 input1, input1_length,
5491 input2, input2_length,
5492 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005493 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005495 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005496 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005497 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005498
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005499 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005500
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005501 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005502 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005503
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005504exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005505 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005506 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005507 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005508}
5509/* END_CASE */
5510
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005511/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005512void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005513{
5514 uint8_t output_buffer[16];
5515 size_t buffer_size = 16;
5516 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005518
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005519 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5520 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005521 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005522
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005524 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005525
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005526 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005527
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005528 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5529 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005530 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005531
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005532 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005533 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005534
5535exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005536 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005537}
5538/* END_CASE */
5539
5540/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005541void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005542 int step1_arg, data_t *input1,
5543 int step2_arg, data_t *input2,
5544 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005545 int requested_capacity_arg,
5546 data_t *expected_output1,
5547 data_t *expected_output2 )
5548{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005549 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005550 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5551 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005552 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5553 MBEDTLS_SVC_KEY_ID_INIT,
5554 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005555 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005556 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005557 uint8_t *expected_outputs[2] =
5558 {expected_output1->x, expected_output2->x};
5559 size_t output_sizes[2] =
5560 {expected_output1->len, expected_output2->len};
5561 size_t output_buffer_size = 0;
5562 uint8_t *output_buffer = NULL;
5563 size_t expected_capacity;
5564 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005565 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005566 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005567 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005568
5569 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5570 {
5571 if( output_sizes[i] > output_buffer_size )
5572 output_buffer_size = output_sizes[i];
5573 if( output_sizes[i] == 0 )
5574 expected_outputs[i] = NULL;
5575 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005576 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005577 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005578
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005579 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5580 psa_set_key_algorithm( &attributes, alg );
5581 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005582
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005583 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005584 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5585 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5586 requested_capacity ) );
5587 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005588 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005589 switch( steps[i] )
5590 {
5591 case 0:
5592 break;
5593 case PSA_KEY_DERIVATION_INPUT_SECRET:
5594 PSA_ASSERT( psa_import_key( &attributes,
5595 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005596 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005597
5598 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5599 {
5600 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5601 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5602 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5603 }
5604
Gilles Peskine1468da72019-05-29 17:35:49 +02005605 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005606 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005607 break;
5608 default:
5609 PSA_ASSERT( psa_key_derivation_input_bytes(
5610 &operation, steps[i],
5611 inputs[i]->x, inputs[i]->len ) );
5612 break;
5613 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005614 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005615
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005616 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005617 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005618 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005619 expected_capacity = requested_capacity;
5620
5621 /* Expansion phase. */
5622 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5623 {
5624 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005625 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005626 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005627 if( expected_capacity == 0 && output_sizes[i] == 0 )
5628 {
5629 /* Reading 0 bytes when 0 bytes are available can go either way. */
5630 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005631 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005632 continue;
5633 }
5634 else if( expected_capacity == 0 ||
5635 output_sizes[i] > expected_capacity )
5636 {
5637 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005638 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005639 expected_capacity = 0;
5640 continue;
5641 }
5642 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005643 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005644 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005645 ASSERT_COMPARE( output_buffer, output_sizes[i],
5646 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005647 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005648 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005649 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005650 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005651 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005652 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005653 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005654
5655exit:
5656 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005657 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005658 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5659 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005660 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005661}
5662/* END_CASE */
5663
5664/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005665void derive_full( int alg_arg,
5666 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005667 data_t *input1,
5668 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005669 int requested_capacity_arg )
5670{
Ronald Cron5425a212020-08-04 14:58:35 +02005671 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005672 psa_algorithm_t alg = alg_arg;
5673 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005674 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005675 unsigned char output_buffer[16];
5676 size_t expected_capacity = requested_capacity;
5677 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005679
Gilles Peskine8817f612018-12-18 00:18:46 +01005680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005681
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005682 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5683 psa_set_key_algorithm( &attributes, alg );
5684 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005685
Gilles Peskine049c7532019-05-15 20:22:09 +02005686 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005687 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005688
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005689 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5690 input1->x, input1->len,
5691 input2->x, input2->len,
5692 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005693 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005694
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005695 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005696 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005697 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005698
5699 /* Expansion phase. */
5700 while( current_capacity > 0 )
5701 {
5702 size_t read_size = sizeof( output_buffer );
5703 if( read_size > current_capacity )
5704 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005705 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005706 output_buffer,
5707 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005708 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005709 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005710 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005711 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005712 }
5713
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005714 /* Check that the operation refuses to go over capacity. */
5715 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005716 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005717
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005718 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005719
5720exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005721 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005722 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005723 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005724}
5725/* END_CASE */
5726
Janos Follathe60c9052019-07-03 13:51:30 +01005727/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005728void derive_key_exercise( int alg_arg,
5729 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005730 data_t *input1,
5731 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005732 int derived_type_arg,
5733 int derived_bits_arg,
5734 int derived_usage_arg,
5735 int derived_alg_arg )
5736{
Ronald Cron5425a212020-08-04 14:58:35 +02005737 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5738 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005739 psa_algorithm_t alg = alg_arg;
5740 psa_key_type_t derived_type = derived_type_arg;
5741 size_t derived_bits = derived_bits_arg;
5742 psa_key_usage_t derived_usage = derived_usage_arg;
5743 psa_algorithm_t derived_alg = derived_alg_arg;
5744 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005747 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005748
Gilles Peskine8817f612018-12-18 00:18:46 +01005749 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005750
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005751 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5752 psa_set_key_algorithm( &attributes, alg );
5753 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005754 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005755 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005756
5757 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005758 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5759 input1->x, input1->len,
5760 input2->x, input2->len,
5761 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005762 goto exit;
5763
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005764 psa_set_key_usage_flags( &attributes, derived_usage );
5765 psa_set_key_algorithm( &attributes, derived_alg );
5766 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005767 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005768 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005769 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005770
5771 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005772 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005773 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5774 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005775
5776 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005777 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005778 goto exit;
5779
5780exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005781 /*
5782 * Key attributes may have been returned by psa_get_key_attributes()
5783 * thus reset them as required.
5784 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005785 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005786
5787 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005788 psa_destroy_key( base_key );
5789 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005790 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005791}
5792/* END_CASE */
5793
Janos Follath42fd8882019-07-03 14:17:09 +01005794/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005795void derive_key_export( int alg_arg,
5796 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005797 data_t *input1,
5798 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005799 int bytes1_arg,
5800 int bytes2_arg )
5801{
Ronald Cron5425a212020-08-04 14:58:35 +02005802 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5803 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005804 psa_algorithm_t alg = alg_arg;
5805 size_t bytes1 = bytes1_arg;
5806 size_t bytes2 = bytes2_arg;
5807 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005808 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005809 uint8_t *output_buffer = NULL;
5810 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005811 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5812 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005813 size_t length;
5814
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005815 ASSERT_ALLOC( output_buffer, capacity );
5816 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005817 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005818
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005819 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5820 psa_set_key_algorithm( &base_attributes, alg );
5821 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005822 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005823 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005824
5825 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005826 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5827 input1->x, input1->len,
5828 input2->x, input2->len,
5829 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005830 goto exit;
5831
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005832 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005833 output_buffer,
5834 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005835 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005836
5837 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005838 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5839 input1->x, input1->len,
5840 input2->x, input2->len,
5841 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005842 goto exit;
5843
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005844 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5845 psa_set_key_algorithm( &derived_attributes, 0 );
5846 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005847 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005848 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005849 &derived_key ) );
5850 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005851 export_buffer, bytes1,
5852 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005853 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005854 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005855 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005856 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005857 &derived_key ) );
5858 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005859 export_buffer + bytes1, bytes2,
5860 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005861 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005862
5863 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005864 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5865 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005866
5867exit:
5868 mbedtls_free( output_buffer );
5869 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005870 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005871 psa_destroy_key( base_key );
5872 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005873 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005874}
5875/* END_CASE */
5876
5877/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005878void derive_key( int alg_arg,
5879 data_t *key_data, data_t *input1, data_t *input2,
5880 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005881 int expected_status_arg,
5882 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005883{
Ronald Cron5425a212020-08-04 14:58:35 +02005884 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5885 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005886 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005887 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005888 size_t bits = bits_arg;
5889 psa_status_t expected_status = expected_status_arg;
5890 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5891 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5892 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5893
5894 PSA_ASSERT( psa_crypto_init( ) );
5895
5896 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5897 psa_set_key_algorithm( &base_attributes, alg );
5898 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5899 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005900 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005901
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005902 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5903 input1->x, input1->len,
5904 input2->x, input2->len,
5905 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005906 goto exit;
5907
5908 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5909 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005910 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005911 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005912
5913 psa_status_t status =
5914 psa_key_derivation_output_key( &derived_attributes,
5915 &operation,
5916 &derived_key );
5917 if( is_large_output > 0 )
5918 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5919 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005920
5921exit:
5922 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005923 psa_destroy_key( base_key );
5924 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005925 PSA_DONE( );
5926}
5927/* END_CASE */
5928
5929/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005930void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005931 int our_key_type_arg, int our_key_alg_arg,
5932 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005933 int expected_status_arg )
5934{
Ronald Cron5425a212020-08-04 14:58:35 +02005935 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005936 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005937 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005938 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005939 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005941 psa_status_t expected_status = expected_status_arg;
5942 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005943
Gilles Peskine8817f612018-12-18 00:18:46 +01005944 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005945
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005946 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005947 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005948 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005949 PSA_ASSERT( psa_import_key( &attributes,
5950 our_key_data->x, our_key_data->len,
5951 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005952
Gilles Peskine77f40d82019-04-11 21:27:06 +02005953 /* The tests currently include inputs that should fail at either step.
5954 * Test cases that fail at the setup step should be changed to call
5955 * key_derivation_setup instead, and this function should be renamed
5956 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005957 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005958 if( status == PSA_SUCCESS )
5959 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005960 TEST_EQUAL( psa_key_derivation_key_agreement(
5961 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5962 our_key,
5963 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005964 expected_status );
5965 }
5966 else
5967 {
5968 TEST_ASSERT( status == expected_status );
5969 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005970
5971exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005972 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005973 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005974 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005975}
5976/* END_CASE */
5977
5978/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005979void raw_key_agreement( int alg_arg,
5980 int our_key_type_arg, data_t *our_key_data,
5981 data_t *peer_key_data,
5982 data_t *expected_output )
5983{
Ronald Cron5425a212020-08-04 14:58:35 +02005984 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005985 psa_algorithm_t alg = alg_arg;
5986 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005987 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005988 unsigned char *output = NULL;
5989 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005990 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005991
5992 ASSERT_ALLOC( output, expected_output->len );
5993 PSA_ASSERT( psa_crypto_init( ) );
5994
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005995 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5996 psa_set_key_algorithm( &attributes, alg );
5997 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005998 PSA_ASSERT( psa_import_key( &attributes,
5999 our_key_data->x, our_key_data->len,
6000 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006001
gabor-mezei-armceface22021-01-21 12:26:17 +01006002 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6003 key_bits = psa_get_key_bits( &attributes );
6004
Gilles Peskinebe697d82019-05-16 18:00:41 +02006005 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6006 peer_key_data->x, peer_key_data->len,
6007 output, expected_output->len,
6008 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006009 ASSERT_COMPARE( output, output_length,
6010 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006011 TEST_ASSERT( output_length <=
6012 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6013 TEST_ASSERT( output_length <=
6014 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006015
6016exit:
6017 mbedtls_free( output );
6018 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006019 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006020}
6021/* END_CASE */
6022
6023/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006024void key_agreement_capacity( int alg_arg,
6025 int our_key_type_arg, data_t *our_key_data,
6026 data_t *peer_key_data,
6027 int expected_capacity_arg )
6028{
Ronald Cron5425a212020-08-04 14:58:35 +02006029 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006030 psa_algorithm_t alg = alg_arg;
6031 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006032 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006033 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006034 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006035 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006036
Gilles Peskine8817f612018-12-18 00:18:46 +01006037 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006038
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006039 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6040 psa_set_key_algorithm( &attributes, alg );
6041 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006042 PSA_ASSERT( psa_import_key( &attributes,
6043 our_key_data->x, our_key_data->len,
6044 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006045
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006046 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006047 PSA_ASSERT( psa_key_derivation_key_agreement(
6048 &operation,
6049 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6050 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006051 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6052 {
6053 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006054 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006055 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006056 NULL, 0 ) );
6057 }
Gilles Peskine59685592018-09-18 12:11:34 +02006058
Gilles Peskinebf491972018-10-25 22:36:12 +02006059 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006060 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006061 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006062 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006063
Gilles Peskinebf491972018-10-25 22:36:12 +02006064 /* Test the actual capacity by reading the output. */
6065 while( actual_capacity > sizeof( output ) )
6066 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006067 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006068 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006069 actual_capacity -= sizeof( output );
6070 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006071 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006072 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006073 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006074 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006075
Gilles Peskine59685592018-09-18 12:11:34 +02006076exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006077 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006078 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006079 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006080}
6081/* END_CASE */
6082
6083/* BEGIN_CASE */
6084void key_agreement_output( int alg_arg,
6085 int our_key_type_arg, data_t *our_key_data,
6086 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006087 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006088{
Ronald Cron5425a212020-08-04 14:58:35 +02006089 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006090 psa_algorithm_t alg = alg_arg;
6091 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006092 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006094 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006095
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006096 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6097 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006098
Gilles Peskine8817f612018-12-18 00:18:46 +01006099 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006100
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006101 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6102 psa_set_key_algorithm( &attributes, alg );
6103 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006104 PSA_ASSERT( psa_import_key( &attributes,
6105 our_key_data->x, our_key_data->len,
6106 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006107
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006108 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006109 PSA_ASSERT( psa_key_derivation_key_agreement(
6110 &operation,
6111 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6112 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006113 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6114 {
6115 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006116 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006117 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006118 NULL, 0 ) );
6119 }
Gilles Peskine59685592018-09-18 12:11:34 +02006120
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006121 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006122 actual_output,
6123 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006124 ASSERT_COMPARE( actual_output, expected_output1->len,
6125 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006126 if( expected_output2->len != 0 )
6127 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006128 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006129 actual_output,
6130 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006131 ASSERT_COMPARE( actual_output, expected_output2->len,
6132 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006133 }
Gilles Peskine59685592018-09-18 12:11:34 +02006134
6135exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006136 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006137 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006138 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006139 mbedtls_free( actual_output );
6140}
6141/* END_CASE */
6142
6143/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006144void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006145{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006146 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006147 unsigned char *output = NULL;
6148 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006149 size_t i;
6150 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006151
Simon Butcher49f8e312020-03-03 15:51:50 +00006152 TEST_ASSERT( bytes_arg >= 0 );
6153
Gilles Peskine91892022021-02-08 19:50:26 +01006154 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006155 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006156
Gilles Peskine8817f612018-12-18 00:18:46 +01006157 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006158
Gilles Peskinea50d7392018-06-21 10:22:13 +02006159 /* Run several times, to ensure that every output byte will be
6160 * nonzero at least once with overwhelming probability
6161 * (2^(-8*number_of_runs)). */
6162 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006163 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006164 if( bytes != 0 )
6165 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006166 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006167
Gilles Peskinea50d7392018-06-21 10:22:13 +02006168 for( i = 0; i < bytes; i++ )
6169 {
6170 if( output[i] != 0 )
6171 ++changed[i];
6172 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006173 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006174
6175 /* Check that every byte was changed to nonzero at least once. This
6176 * validates that psa_generate_random is overwriting every byte of
6177 * the output buffer. */
6178 for( i = 0; i < bytes; i++ )
6179 {
6180 TEST_ASSERT( changed[i] != 0 );
6181 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006182
6183exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006184 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006185 mbedtls_free( output );
6186 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006187}
6188/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006189
6190/* BEGIN_CASE */
6191void generate_key( int type_arg,
6192 int bits_arg,
6193 int usage_arg,
6194 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006195 int expected_status_arg,
6196 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006197{
Ronald Cron5425a212020-08-04 14:58:35 +02006198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006199 psa_key_type_t type = type_arg;
6200 psa_key_usage_t usage = usage_arg;
6201 size_t bits = bits_arg;
6202 psa_algorithm_t alg = alg_arg;
6203 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006204 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006205 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006206
Gilles Peskine8817f612018-12-18 00:18:46 +01006207 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006208
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006209 psa_set_key_usage_flags( &attributes, usage );
6210 psa_set_key_algorithm( &attributes, alg );
6211 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006212 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006213
6214 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006215 psa_status_t status = psa_generate_key( &attributes, &key );
6216
6217 if( is_large_key > 0 )
6218 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6219 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006220 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006221 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006222
6223 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006224 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006225 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6226 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006227
Gilles Peskine818ca122018-06-20 18:16:48 +02006228 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006229 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006230 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006231
6232exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006233 /*
6234 * Key attributes may have been returned by psa_get_key_attributes()
6235 * thus reset them as required.
6236 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006237 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006238
Ronald Cron5425a212020-08-04 14:58:35 +02006239 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006240 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006241}
6242/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006243
Ronald Cronee414c72021-03-18 18:50:08 +01006244/* 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 +02006245void generate_key_rsa( int bits_arg,
6246 data_t *e_arg,
6247 int expected_status_arg )
6248{
Ronald Cron5425a212020-08-04 14:58:35 +02006249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006250 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006251 size_t bits = bits_arg;
6252 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6253 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6254 psa_status_t expected_status = expected_status_arg;
6255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6256 uint8_t *exported = NULL;
6257 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006258 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006259 size_t exported_length = SIZE_MAX;
6260 uint8_t *e_read_buffer = NULL;
6261 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006262 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006263 size_t e_read_length = SIZE_MAX;
6264
6265 if( e_arg->len == 0 ||
6266 ( e_arg->len == 3 &&
6267 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6268 {
6269 is_default_public_exponent = 1;
6270 e_read_size = 0;
6271 }
6272 ASSERT_ALLOC( e_read_buffer, e_read_size );
6273 ASSERT_ALLOC( exported, exported_size );
6274
6275 PSA_ASSERT( psa_crypto_init( ) );
6276
6277 psa_set_key_usage_flags( &attributes, usage );
6278 psa_set_key_algorithm( &attributes, alg );
6279 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6280 e_arg->x, e_arg->len ) );
6281 psa_set_key_bits( &attributes, bits );
6282
6283 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006284 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006285 if( expected_status != PSA_SUCCESS )
6286 goto exit;
6287
6288 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006289 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006290 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6291 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6292 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6293 e_read_buffer, e_read_size,
6294 &e_read_length ) );
6295 if( is_default_public_exponent )
6296 TEST_EQUAL( e_read_length, 0 );
6297 else
6298 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6299
6300 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006301 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006302 goto exit;
6303
6304 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006305 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006306 exported, exported_size,
6307 &exported_length ) );
6308 {
6309 uint8_t *p = exported;
6310 uint8_t *end = exported + exported_length;
6311 size_t len;
6312 /* RSAPublicKey ::= SEQUENCE {
6313 * modulus INTEGER, -- n
6314 * publicExponent INTEGER } -- e
6315 */
6316 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006317 MBEDTLS_ASN1_SEQUENCE |
6318 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006319 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006320 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6321 MBEDTLS_ASN1_INTEGER ) );
6322 if( len >= 1 && p[0] == 0 )
6323 {
6324 ++p;
6325 --len;
6326 }
6327 if( e_arg->len == 0 )
6328 {
6329 TEST_EQUAL( len, 3 );
6330 TEST_EQUAL( p[0], 1 );
6331 TEST_EQUAL( p[1], 0 );
6332 TEST_EQUAL( p[2], 1 );
6333 }
6334 else
6335 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6336 }
6337
6338exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006339 /*
6340 * Key attributes may have been returned by psa_get_key_attributes() or
6341 * set by psa_set_key_domain_parameters() thus reset them as required.
6342 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006343 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006344
Ronald Cron5425a212020-08-04 14:58:35 +02006345 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006346 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006347 mbedtls_free( e_read_buffer );
6348 mbedtls_free( exported );
6349}
6350/* END_CASE */
6351
Darryl Greend49a4992018-06-18 17:27:26 +01006352/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006353void persistent_key_load_key_from_storage( data_t *data,
6354 int type_arg, int bits_arg,
6355 int usage_flags_arg, int alg_arg,
6356 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006357{
Ronald Cron71016a92020-08-28 19:01:50 +02006358 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006360 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6361 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006362 psa_key_type_t type = type_arg;
6363 size_t bits = bits_arg;
6364 psa_key_usage_t usage_flags = usage_flags_arg;
6365 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006366 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006367 unsigned char *first_export = NULL;
6368 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006369 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006370 size_t first_exported_length;
6371 size_t second_exported_length;
6372
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006373 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6374 {
6375 ASSERT_ALLOC( first_export, export_size );
6376 ASSERT_ALLOC( second_export, export_size );
6377 }
Darryl Greend49a4992018-06-18 17:27:26 +01006378
Gilles Peskine8817f612018-12-18 00:18:46 +01006379 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006380
Gilles Peskinec87af662019-05-15 16:12:22 +02006381 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006382 psa_set_key_usage_flags( &attributes, usage_flags );
6383 psa_set_key_algorithm( &attributes, alg );
6384 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006385 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006386
Darryl Green0c6575a2018-11-07 16:05:30 +00006387 switch( generation_method )
6388 {
6389 case IMPORT_KEY:
6390 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006391 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006392 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006393 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006394
Darryl Green0c6575a2018-11-07 16:05:30 +00006395 case GENERATE_KEY:
6396 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006397 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006398 break;
6399
6400 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006401#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006402 {
6403 /* Create base key */
6404 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6405 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6406 psa_set_key_usage_flags( &base_attributes,
6407 PSA_KEY_USAGE_DERIVE );
6408 psa_set_key_algorithm( &base_attributes, derive_alg );
6409 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006410 PSA_ASSERT( psa_import_key( &base_attributes,
6411 data->x, data->len,
6412 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006413 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006414 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006415 PSA_ASSERT( psa_key_derivation_input_key(
6416 &operation,
6417 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006418 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006419 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006420 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006421 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6422 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006423 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006424 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006425 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006426 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006427 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006428#else
6429 TEST_ASSUME( ! "KDF not supported in this configuration" );
6430#endif
6431 break;
6432
6433 default:
6434 TEST_ASSERT( ! "generation_method not implemented in test" );
6435 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006436 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006437 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006438
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006439 /* Export the key if permitted by the key policy. */
6440 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6441 {
Ronald Cron5425a212020-08-04 14:58:35 +02006442 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006443 first_export, export_size,
6444 &first_exported_length ) );
6445 if( generation_method == IMPORT_KEY )
6446 ASSERT_COMPARE( data->x, data->len,
6447 first_export, first_exported_length );
6448 }
Darryl Greend49a4992018-06-18 17:27:26 +01006449
6450 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006451 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006452 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006453 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006454
Darryl Greend49a4992018-06-18 17:27:26 +01006455 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006456 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006457 TEST_ASSERT( mbedtls_svc_key_id_equal(
6458 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006459 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6460 PSA_KEY_LIFETIME_PERSISTENT );
6461 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6462 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6463 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6464 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006465
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006466 /* Export the key again if permitted by the key policy. */
6467 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006468 {
Ronald Cron5425a212020-08-04 14:58:35 +02006469 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006470 second_export, export_size,
6471 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006472 ASSERT_COMPARE( first_export, first_exported_length,
6473 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006474 }
6475
6476 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006477 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006478 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006479
6480exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006481 /*
6482 * Key attributes may have been returned by psa_get_key_attributes()
6483 * thus reset them as required.
6484 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006485 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006486
Darryl Greend49a4992018-06-18 17:27:26 +01006487 mbedtls_free( first_export );
6488 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006489 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006490 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006491 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006492 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006493}
6494/* END_CASE */