blob: c91f744b89c6a46106aa3466a3942c64a61e9798 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Paul Elliott33746aa2021-09-15 16:40:40 +0100267typedef enum
268{
269 DO_NOT_SET_LENGTHS = 0,
270 SET_LENGTHS_BEFORE_NONCE = 1,
271 SET_LENGTHS_AFTER_NONCE = 2
272} setlengths_method;
273
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100274typedef enum
275{
276 USE_NULL_TAG = 0,
277 USE_GIVEN_TAG = 1,
278} tagusage_method;
279
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100280/*!
281 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100282 * \param key_type_arg Type of key passed in
283 * \param key_data The encryption / decryption key data
284 * \param alg_arg The type of algorithm used
285 * \param nonce Nonce data
286 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100287 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100288 * feed additional data in to be encrypted /
289 * decrypted. If -1, no chunking.
290 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100291 * \param data_part_len_arg If not -1, the length of chunks to feed
292 * the data in to be encrypted / decrypted. If
293 * -1, no chunking
294 * \param set_lengths_method A member of the setlengths_method enum is
295 * expected here, this controls whether or not
296 * to set lengths, and in what order with
297 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100298 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100299 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100300 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100301 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100302 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 */
304static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
305 int alg_arg,
306 data_t *nonce,
307 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100308 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100309 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100310 int data_part_len_arg,
Paul Elliott33746aa2021-09-15 16:40:40 +0100311 setlengths_method set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100312 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100313 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100314 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100315{
316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
317 psa_key_type_t key_type = key_type_arg;
318 psa_algorithm_t alg = alg_arg;
319 psa_aead_operation_t operation;
320 unsigned char *output_data = NULL;
321 unsigned char *part_data = NULL;
322 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100324 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 size_t output_size = 0;
326 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100327 size_t output_length = 0;
328 size_t key_bits = 0;
329 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100330 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100331 size_t part_length = 0;
332 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100333 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100334 size_t ad_part_len = 0;
335 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
338 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
339
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100341 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342
Paul Elliottd3f82412021-06-16 16:52:21 +0100343 PSA_ASSERT( psa_crypto_init( ) );
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 if( is_encrypt )
346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
347 else
348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
349
Paul Elliottd3f82412021-06-16 16:52:21 +0100350 psa_set_key_algorithm( &attributes, alg );
351 psa_set_key_type( &attributes, key_type );
352
353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
354 &key ) );
355
356 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
357 key_bits = psa_get_key_bits( &attributes );
358
359 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 if( is_encrypt )
362 {
363 /* Tag gets written at end of buffer. */
364 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
365 ( input_data->len +
366 tag_length ) );
367 data_true_size = input_data->len;
368 }
369 else
370 {
371 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
372 ( input_data->len -
373 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 /* Do not want to attempt to decrypt tag. */
376 data_true_size = input_data->len - tag_length;
377 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100378
379 ASSERT_ALLOC( output_data, output_size );
380
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 if( is_encrypt )
382 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100383 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
384 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385 }
386 else
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100391
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 operation = psa_aead_operation_init( );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396
397 if( is_encrypt )
398 status = psa_aead_encrypt_setup( &operation, key, alg );
399 else
400 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100401
402 /* If the operation is not supported, just skip and not fail in case the
403 * encryption involves a common limitation of cryptography hardwares and
404 * an alternative implementation. */
405 if( status == PSA_ERROR_NOT_SUPPORTED )
406 {
407 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
408 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
409 }
410
411 PSA_ASSERT( status );
412
Paul Elliott33746aa2021-09-15 16:40:40 +0100413 if( set_lengths_method == DO_NOT_SET_LENGTHS )
414 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
415 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100416 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100417 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
418 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100421 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100422 {
423 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
426 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100427 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100428
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100429 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100430 {
431 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100432 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100434 for( part_offset = 0, part_count = 0;
435 part_offset < additional_data->len;
436 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100437 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100438 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 {
Paul Elliott329d5382021-07-22 17:10:45 +0100440 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100441 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100442 else if( additional_data->len - part_offset < ad_part_len )
443 {
444 part_length = additional_data->len - part_offset;
445 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 else
447 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100448 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 }
450
451 PSA_ASSERT( psa_aead_update_ad( &operation,
452 additional_data->x + part_offset,
453 part_length ) );
454
Paul Elliottd3f82412021-06-16 16:52:21 +0100455 }
456 }
457 else
458 {
459 /* Pass additional data in one go. */
460 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
461 additional_data->len ) );
462 }
463
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100464 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 {
466 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100467 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100468 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100469 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100470
471 ASSERT_ALLOC( part_data, part_data_size );
472
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100473 for( part_offset = 0, part_count = 0;
474 part_offset < data_true_size;
475 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100477 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 {
Paul Elliott329d5382021-07-22 17:10:45 +0100479 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100481 else if( ( data_true_size - part_offset ) < data_part_len )
482 {
483 part_length = ( data_true_size - part_offset );
484 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 else
486 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100487 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 }
489
490 PSA_ASSERT( psa_aead_update( &operation,
491 ( input_data->x + part_offset ),
492 part_length, part_data,
493 part_data_size,
494 &output_part_length ) );
495
496 if( output_data && output_part_length )
497 {
498 memcpy( ( output_data + part_offset ), part_data,
499 output_part_length );
500 }
501
Paul Elliottd3f82412021-06-16 16:52:21 +0100502 output_length += output_part_length;
503 }
504 }
505 else
506 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100507 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100508 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100509 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100510 output_size, &output_length ) );
511 }
512
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100513 if( is_encrypt )
514 PSA_ASSERT( psa_aead_finish( &operation, final_data,
515 final_output_size,
516 &output_part_length,
517 tag_buffer, tag_length,
518 &tag_size ) );
519 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 {
Paul Elliott9961a662021-09-17 19:19:02 +0100521 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100522 final_output_size,
523 &output_part_length,
524 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100525 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100526 }
527
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100528 if( output_data && output_part_length )
529 memcpy( ( output_data + output_length ), final_data,
530 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100531
532 output_length += output_part_length;
533
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534
535 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
536 * should be exact.*/
537 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100539 TEST_EQUAL( tag_length, tag_size );
540
541 if( output_data && tag_length )
542 memcpy( ( output_data + output_length ), tag_buffer,
543 tag_length );
544
545 output_length += tag_length;
546
547 TEST_EQUAL( output_length,
548 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
549 input_data->len ) );
550 TEST_ASSERT( output_length <=
551 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
552 }
553 else
554 {
555 TEST_EQUAL( output_length,
556 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
557 input_data->len ) );
558 TEST_ASSERT( output_length <=
559 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100560 }
561
Paul Elliottd3f82412021-06-16 16:52:21 +0100562
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100563 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100564 output_data, output_length );
565
Paul Elliottd3f82412021-06-16 16:52:21 +0100566
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100567 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100568
569exit:
570 psa_destroy_key( key );
571 psa_aead_abort( &operation );
572 mbedtls_free( output_data );
573 mbedtls_free( part_data );
574 mbedtls_free( final_data );
575 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100576
577 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100578}
579
Gilles Peskinee59236f2018-01-27 23:32:46 +0100580/* END_HEADER */
581
582/* BEGIN_DEPENDENCIES
583 * depends_on:MBEDTLS_PSA_CRYPTO_C
584 * END_DEPENDENCIES
585 */
586
587/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200588void static_checks( )
589{
590 size_t max_truncated_mac_size =
591 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
592
593 /* Check that the length for a truncated MAC always fits in the algorithm
594 * encoding. The shifted mask is the maximum truncated value. The
595 * untruncated algorithm may be one byte larger. */
596 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
597}
598/* END_CASE */
599
600/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200601void import_with_policy( int type_arg,
602 int usage_arg, int alg_arg,
603 int expected_status_arg )
604{
605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
606 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200608 psa_key_type_t type = type_arg;
609 psa_key_usage_t usage = usage_arg;
610 psa_algorithm_t alg = alg_arg;
611 psa_status_t expected_status = expected_status_arg;
612 const uint8_t key_material[16] = {0};
613 psa_status_t status;
614
615 PSA_ASSERT( psa_crypto_init( ) );
616
617 psa_set_key_type( &attributes, type );
618 psa_set_key_usage_flags( &attributes, usage );
619 psa_set_key_algorithm( &attributes, alg );
620
621 status = psa_import_key( &attributes,
622 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200623 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200624 TEST_EQUAL( status, expected_status );
625 if( status != PSA_SUCCESS )
626 goto exit;
627
Ronald Cron5425a212020-08-04 14:58:35 +0200628 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200629 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
630 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
631 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200632 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200633
Ronald Cron5425a212020-08-04 14:58:35 +0200634 PSA_ASSERT( psa_destroy_key( key ) );
635 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636
637exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100638 /*
639 * Key attributes may have been returned by psa_get_key_attributes()
640 * thus reset them as required.
641 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200642 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100643
644 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200645 PSA_DONE( );
646}
647/* END_CASE */
648
649/* BEGIN_CASE */
650void import_with_data( data_t *data, int type_arg,
651 int attr_bits_arg,
652 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200653{
654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
655 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200657 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200658 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200659 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661
Gilles Peskine8817f612018-12-18 00:18:46 +0100662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663
Gilles Peskine4747d192019-04-17 15:05:45 +0200664 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200665 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200666
Ronald Cron5425a212020-08-04 14:58:35 +0200667 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100668 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200669 if( status != PSA_SUCCESS )
670 goto exit;
671
Ronald Cron5425a212020-08-04 14:58:35 +0200672 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200673 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200674 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200675 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200676 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200677
Ronald Cron5425a212020-08-04 14:58:35 +0200678 PSA_ASSERT( psa_destroy_key( key ) );
679 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680
681exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100682 /*
683 * Key attributes may have been returned by psa_get_key_attributes()
684 * thus reset them as required.
685 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200686 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100687
688 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200689 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690}
691/* END_CASE */
692
693/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200694void import_large_key( int type_arg, int byte_size_arg,
695 int expected_status_arg )
696{
697 psa_key_type_t type = type_arg;
698 size_t byte_size = byte_size_arg;
699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
700 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200701 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200702 psa_status_t status;
703 uint8_t *buffer = NULL;
704 size_t buffer_size = byte_size + 1;
705 size_t n;
706
Steven Cooreman69967ce2021-01-18 18:01:08 +0100707 /* Skip the test case if the target running the test cannot
708 * accomodate large keys due to heap size constraints */
709 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200710 memset( buffer, 'K', byte_size );
711
712 PSA_ASSERT( psa_crypto_init( ) );
713
714 /* Try importing the key */
715 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
716 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200717 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100718 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200719 TEST_EQUAL( status, expected_status );
720
721 if( status == PSA_SUCCESS )
722 {
Ronald Cron5425a212020-08-04 14:58:35 +0200723 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200724 TEST_EQUAL( psa_get_key_type( &attributes ), type );
725 TEST_EQUAL( psa_get_key_bits( &attributes ),
726 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200727 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200728 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200729 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200730 for( n = 0; n < byte_size; n++ )
731 TEST_EQUAL( buffer[n], 'K' );
732 for( n = byte_size; n < buffer_size; n++ )
733 TEST_EQUAL( buffer[n], 0 );
734 }
735
736exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100737 /*
738 * Key attributes may have been returned by psa_get_key_attributes()
739 * thus reset them as required.
740 */
741 psa_reset_key_attributes( &attributes );
742
Ronald Cron5425a212020-08-04 14:58:35 +0200743 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200744 PSA_DONE( );
745 mbedtls_free( buffer );
746}
747/* END_CASE */
748
749/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200750void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
751{
Ronald Cron5425a212020-08-04 14:58:35 +0200752 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200753 size_t bits = bits_arg;
754 psa_status_t expected_status = expected_status_arg;
755 psa_status_t status;
756 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200757 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200758 size_t buffer_size = /* Slight overapproximations */
759 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200760 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200761 unsigned char *p;
762 int ret;
763 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200765
Gilles Peskine8817f612018-12-18 00:18:46 +0100766 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200767 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200768
769 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
770 bits, keypair ) ) >= 0 );
771 length = ret;
772
773 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200774 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200775 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100776 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200777
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200779 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200780
781exit:
782 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200783 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200784}
785/* END_CASE */
786
787/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300788void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300789 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200790 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100791 int expected_bits,
792 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200793 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100794 int canonical_input )
795{
Ronald Cron5425a212020-08-04 14:58:35 +0200796 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200798 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200799 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801 unsigned char *exported = NULL;
802 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100804 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200807 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808
Moran Pekercb088e72018-07-17 17:36:59 +0300809 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200810 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200812 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814
Gilles Peskine4747d192019-04-17 15:05:45 +0200815 psa_set_key_usage_flags( &attributes, usage_arg );
816 psa_set_key_algorithm( &attributes, alg );
817 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700818
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200820 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821
822 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200823 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200824 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
825 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200826 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100827
828 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200829 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100830 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100831
832 /* The exported length must be set by psa_export_key() to a value between 0
833 * and export_size. On errors, the exported length must be 0. */
834 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
835 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
836 TEST_ASSERT( exported_length <= export_size );
837
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200838 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200839 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200841 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100842 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200844 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845
Gilles Peskineea38a922021-02-13 00:05:16 +0100846 /* Run sanity checks on the exported key. For non-canonical inputs,
847 * this validates the canonical representations. For canonical inputs,
848 * this doesn't directly validate the implementation, but it still helps
849 * by cross-validating the test data with the sanity check code. */
850 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200851 goto exit;
852
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100853 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200854 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 else
856 {
Ronald Cron5425a212020-08-04 14:58:35 +0200857 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200858 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200859 &key2 ) );
860 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100861 reexported,
862 export_size,
863 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200864 ASSERT_COMPARE( exported, exported_length,
865 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200866 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100868 TEST_ASSERT( exported_length <=
869 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
870 psa_get_key_bits( &got_attributes ) ) );
871 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872
873destroy:
874 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200875 PSA_ASSERT( psa_destroy_key( key ) );
876 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877
878exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100879 /*
880 * Key attributes may have been returned by psa_get_key_attributes()
881 * thus reset them as required.
882 */
883 psa_reset_key_attributes( &got_attributes );
884
itayzafrir3e02b3b2018-06-12 17:06:52 +0300885 mbedtls_free( exported );
886 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200887 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888}
889/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100890
Moran Pekerf709f4a2018-06-06 17:26:04 +0300891/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300892void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200893 int type_arg,
894 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100895 int export_size_delta,
896 int expected_export_status_arg,
897 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300898{
Ronald Cron5425a212020-08-04 14:58:35 +0200899 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300900 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200901 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200902 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300903 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300904 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100905 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100906 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300908
Gilles Peskine8817f612018-12-18 00:18:46 +0100909 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910
Gilles Peskine4747d192019-04-17 15:05:45 +0200911 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
912 psa_set_key_algorithm( &attributes, alg );
913 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300914
915 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200916 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300917
Gilles Peskine49c25912018-10-29 15:15:31 +0100918 /* Export the public key */
919 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200920 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200921 exported, export_size,
922 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100923 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100924 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100925 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200926 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100927 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200928 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200929 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100930 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100931 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100932 TEST_ASSERT( expected_public_key->len <=
933 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
934 TEST_ASSERT( expected_public_key->len <=
935 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100936 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
937 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100938 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300939
940exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100941 /*
942 * Key attributes may have been returned by psa_get_key_attributes()
943 * thus reset them as required.
944 */
945 psa_reset_key_attributes( &attributes );
946
itayzafrir3e02b3b2018-06-12 17:06:52 +0300947 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200948 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200949 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300950}
951/* END_CASE */
952
Gilles Peskine20035e32018-02-03 22:44:14 +0100953/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200954void import_and_exercise_key( data_t *data,
955 int type_arg,
956 int bits_arg,
957 int alg_arg )
958{
Ronald Cron5425a212020-08-04 14:58:35 +0200959 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200960 psa_key_type_t type = type_arg;
961 size_t bits = bits_arg;
962 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100963 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200965 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200966
Gilles Peskine8817f612018-12-18 00:18:46 +0100967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200968
Gilles Peskine4747d192019-04-17 15:05:45 +0200969 psa_set_key_usage_flags( &attributes, usage );
970 psa_set_key_algorithm( &attributes, alg );
971 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200972
973 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200974 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975
976 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200977 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200978 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
979 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980
981 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100982 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200983 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200984
Ronald Cron5425a212020-08-04 14:58:35 +0200985 PSA_ASSERT( psa_destroy_key( key ) );
986 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200987
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200988exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100989 /*
990 * Key attributes may have been returned by psa_get_key_attributes()
991 * thus reset them as required.
992 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200993 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100994
995 psa_reset_key_attributes( &attributes );
996 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200997 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200998}
999/* END_CASE */
1000
1001/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001002void effective_key_attributes( int type_arg, int expected_type_arg,
1003 int bits_arg, int expected_bits_arg,
1004 int usage_arg, int expected_usage_arg,
1005 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001006{
Ronald Cron5425a212020-08-04 14:58:35 +02001007 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001008 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001009 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001010 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001011 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001012 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001013 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001014 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001015 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001017
Gilles Peskine8817f612018-12-18 00:18:46 +01001018 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001019
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001020 psa_set_key_usage_flags( &attributes, usage );
1021 psa_set_key_algorithm( &attributes, alg );
1022 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001023 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001024
Ronald Cron5425a212020-08-04 14:58:35 +02001025 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001026 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001027
Ronald Cron5425a212020-08-04 14:58:35 +02001028 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001029 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1030 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1031 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1032 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001033
1034exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001035 /*
1036 * Key attributes may have been returned by psa_get_key_attributes()
1037 * thus reset them as required.
1038 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001039 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001040
1041 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001042 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001043}
1044/* END_CASE */
1045
1046/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001047void check_key_policy( int type_arg, int bits_arg,
1048 int usage_arg, int alg_arg )
1049{
1050 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1051 usage_arg, usage_arg, alg_arg, alg_arg );
1052 goto exit;
1053}
1054/* END_CASE */
1055
1056/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001057void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001058{
1059 /* Test each valid way of initializing the object, except for `= {0}`, as
1060 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1061 * though it's OK by the C standard. We could test for this, but we'd need
1062 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001063 psa_key_attributes_t func = psa_key_attributes_init( );
1064 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1065 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001066
1067 memset( &zero, 0, sizeof( zero ) );
1068
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001069 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1070 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1071 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001072
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001073 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1074 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1075 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1076
1077 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1078 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1079 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1080
1081 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1082 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1083 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1084
1085 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1086 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1087 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001088}
1089/* END_CASE */
1090
1091/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001092void mac_key_policy( int policy_usage,
1093 int policy_alg,
1094 int key_type,
1095 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001096 int exercise_alg,
1097 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001098{
Ronald Cron5425a212020-08-04 14:58:35 +02001099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001101 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001102 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001103 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001104 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001105
Gilles Peskine8817f612018-12-18 00:18:46 +01001106 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001107
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001108 psa_set_key_usage_flags( &attributes, policy_usage );
1109 psa_set_key_algorithm( &attributes, policy_alg );
1110 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001111
Gilles Peskine049c7532019-05-15 20:22:09 +02001112 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001113 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001114
Ronald Cron5425a212020-08-04 14:58:35 +02001115 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001116 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001117 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001118 else
1119 TEST_EQUAL( status, expected_status );
1120
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001121 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001122
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001123 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001124 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001125 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001126 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001127 else
1128 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001129
1130exit:
1131 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001132 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001133 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001134}
1135/* END_CASE */
1136
1137/* BEGIN_CASE */
1138void cipher_key_policy( int policy_usage,
1139 int policy_alg,
1140 int key_type,
1141 data_t *key_data,
1142 int exercise_alg )
1143{
Ronald Cron5425a212020-08-04 14:58:35 +02001144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001146 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001147 psa_status_t status;
1148
Gilles Peskine8817f612018-12-18 00:18:46 +01001149 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001150
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001151 psa_set_key_usage_flags( &attributes, policy_usage );
1152 psa_set_key_algorithm( &attributes, policy_alg );
1153 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001154
Gilles Peskine049c7532019-05-15 20:22:09 +02001155 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001156 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001157
Ronald Cron5425a212020-08-04 14:58:35 +02001158 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159 if( policy_alg == exercise_alg &&
1160 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001162 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001163 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164 psa_cipher_abort( &operation );
1165
Ronald Cron5425a212020-08-04 14:58:35 +02001166 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 if( policy_alg == exercise_alg &&
1168 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001169 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001170 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001171 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
1173exit:
1174 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001176 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177}
1178/* END_CASE */
1179
1180/* BEGIN_CASE */
1181void aead_key_policy( int policy_usage,
1182 int policy_alg,
1183 int key_type,
1184 data_t *key_data,
1185 int nonce_length_arg,
1186 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001187 int exercise_alg,
1188 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001189{
Ronald Cron5425a212020-08-04 14:58:35 +02001190 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001192 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001193 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194 unsigned char nonce[16] = {0};
1195 size_t nonce_length = nonce_length_arg;
1196 unsigned char tag[16];
1197 size_t tag_length = tag_length_arg;
1198 size_t output_length;
1199
1200 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1201 TEST_ASSERT( tag_length <= sizeof( tag ) );
1202
Gilles Peskine8817f612018-12-18 00:18:46 +01001203 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001205 psa_set_key_usage_flags( &attributes, policy_usage );
1206 psa_set_key_algorithm( &attributes, policy_alg );
1207 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208
Gilles Peskine049c7532019-05-15 20:22:09 +02001209 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001210 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211
Ronald Cron5425a212020-08-04 14:58:35 +02001212 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213 nonce, nonce_length,
1214 NULL, 0,
1215 NULL, 0,
1216 tag, tag_length,
1217 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001218 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1219 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001221 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222
1223 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001224 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225 nonce, nonce_length,
1226 NULL, 0,
1227 tag, tag_length,
1228 NULL, 0,
1229 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001230 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1231 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1232 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001233 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001234 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001235 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001236
1237exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001239 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
1244void asymmetric_encryption_key_policy( int policy_usage,
1245 int policy_alg,
1246 int key_type,
1247 data_t *key_data,
1248 int exercise_alg )
1249{
Ronald Cron5425a212020-08-04 14:58:35 +02001250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252 psa_status_t status;
1253 size_t key_bits;
1254 size_t buffer_length;
1255 unsigned char *buffer = NULL;
1256 size_t output_length;
1257
Gilles Peskine8817f612018-12-18 00:18:46 +01001258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001260 psa_set_key_usage_flags( &attributes, policy_usage );
1261 psa_set_key_algorithm( &attributes, policy_alg );
1262 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001263
Gilles Peskine049c7532019-05-15 20:22:09 +02001264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001265 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266
Ronald Cron5425a212020-08-04 14:58:35 +02001267 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001268 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001269 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1270 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001271 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001272
Ronald Cron5425a212020-08-04 14:58:35 +02001273 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001274 NULL, 0,
1275 NULL, 0,
1276 buffer, buffer_length,
1277 &output_length );
1278 if( policy_alg == exercise_alg &&
1279 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001280 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001281 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001282 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001283
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001284 if( buffer_length != 0 )
1285 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001286 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 buffer, buffer_length,
1288 NULL, 0,
1289 buffer, buffer_length,
1290 &output_length );
1291 if( policy_alg == exercise_alg &&
1292 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001293 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001294 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001295 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296
1297exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001298 /*
1299 * Key attributes may have been returned by psa_get_key_attributes()
1300 * thus reset them as required.
1301 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001302 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001303
1304 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001305 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001306 mbedtls_free( buffer );
1307}
1308/* END_CASE */
1309
1310/* BEGIN_CASE */
1311void asymmetric_signature_key_policy( int policy_usage,
1312 int policy_alg,
1313 int key_type,
1314 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001315 int exercise_alg,
1316 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317{
Ronald Cron5425a212020-08-04 14:58:35 +02001318 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001321 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1322 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1323 * compatible with the policy and `payload_length_arg` is supposed to be
1324 * a valid input length to sign. If `payload_length_arg <= 0`,
1325 * `exercise_alg` is supposed to be forbidden by the policy. */
1326 int compatible_alg = payload_length_arg > 0;
1327 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001328 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001329 size_t signature_length;
1330
Gilles Peskine8817f612018-12-18 00:18:46 +01001331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001333 psa_set_key_usage_flags( &attributes, policy_usage );
1334 psa_set_key_algorithm( &attributes, policy_alg );
1335 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001336
Gilles Peskine049c7532019-05-15 20:22:09 +02001337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001338 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339
Ronald Cron5425a212020-08-04 14:58:35 +02001340 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001341 payload, payload_length,
1342 signature, sizeof( signature ),
1343 &signature_length );
1344 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001345 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001347 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001348
1349 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001350 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001351 payload, payload_length,
1352 signature, sizeof( signature ) );
1353 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001354 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001355 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001356 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001357
1358exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001359 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001360 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001361}
1362/* END_CASE */
1363
Janos Follathba3fab92019-06-11 14:50:16 +01001364/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001365void derive_key_policy( int policy_usage,
1366 int policy_alg,
1367 int key_type,
1368 data_t *key_data,
1369 int exercise_alg )
1370{
Ronald Cron5425a212020-08-04 14:58:35 +02001371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001373 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001374 psa_status_t status;
1375
Gilles Peskine8817f612018-12-18 00:18:46 +01001376 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001377
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001378 psa_set_key_usage_flags( &attributes, policy_usage );
1379 psa_set_key_algorithm( &attributes, policy_alg );
1380 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001381
Gilles Peskine049c7532019-05-15 20:22:09 +02001382 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001383 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001384
Janos Follathba3fab92019-06-11 14:50:16 +01001385 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1386
1387 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1388 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001389 {
Janos Follathba3fab92019-06-11 14:50:16 +01001390 PSA_ASSERT( psa_key_derivation_input_bytes(
1391 &operation,
1392 PSA_KEY_DERIVATION_INPUT_SEED,
1393 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001394 }
Janos Follathba3fab92019-06-11 14:50:16 +01001395
1396 status = psa_key_derivation_input_key( &operation,
1397 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001398 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001399
Gilles Peskineea0fb492018-07-12 17:17:20 +02001400 if( policy_alg == exercise_alg &&
1401 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001402 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001403 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001404 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001405
1406exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001407 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001408 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001409 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001410}
1411/* END_CASE */
1412
1413/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001414void agreement_key_policy( int policy_usage,
1415 int policy_alg,
1416 int key_type_arg,
1417 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001418 int exercise_alg,
1419 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001420{
Ronald Cron5425a212020-08-04 14:58:35 +02001421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001423 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001424 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001425 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001426 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001427
Gilles Peskine8817f612018-12-18 00:18:46 +01001428 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001429
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001430 psa_set_key_usage_flags( &attributes, policy_usage );
1431 psa_set_key_algorithm( &attributes, policy_alg );
1432 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001433
Gilles Peskine049c7532019-05-15 20:22:09 +02001434 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001435 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001436
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001437 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001438 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001439
Steven Cooremance48e852020-10-05 16:02:45 +02001440 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441
1442exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001443 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001444 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001445 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001446}
1447/* END_CASE */
1448
1449/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001450void key_policy_alg2( int key_type_arg, data_t *key_data,
1451 int usage_arg, int alg_arg, int alg2_arg )
1452{
Ronald Cron5425a212020-08-04 14:58:35 +02001453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001454 psa_key_type_t key_type = key_type_arg;
1455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1456 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1457 psa_key_usage_t usage = usage_arg;
1458 psa_algorithm_t alg = alg_arg;
1459 psa_algorithm_t alg2 = alg2_arg;
1460
1461 PSA_ASSERT( psa_crypto_init( ) );
1462
1463 psa_set_key_usage_flags( &attributes, usage );
1464 psa_set_key_algorithm( &attributes, alg );
1465 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1466 psa_set_key_type( &attributes, key_type );
1467 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001468 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001469
Ronald Cron5425a212020-08-04 14:58:35 +02001470 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001471 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1472 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1473 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1474
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001475 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001476 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001477 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001478 goto exit;
1479
1480exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001481 /*
1482 * Key attributes may have been returned by psa_get_key_attributes()
1483 * thus reset them as required.
1484 */
1485 psa_reset_key_attributes( &got_attributes );
1486
Ronald Cron5425a212020-08-04 14:58:35 +02001487 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001488 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001489}
1490/* END_CASE */
1491
1492/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001493void raw_agreement_key_policy( int policy_usage,
1494 int policy_alg,
1495 int key_type_arg,
1496 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001497 int exercise_alg,
1498 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001499{
Ronald Cron5425a212020-08-04 14:58:35 +02001500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001502 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001503 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001504 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001505 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001506
1507 PSA_ASSERT( psa_crypto_init( ) );
1508
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001509 psa_set_key_usage_flags( &attributes, policy_usage );
1510 psa_set_key_algorithm( &attributes, policy_alg );
1511 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001512
Gilles Peskine049c7532019-05-15 20:22:09 +02001513 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001514 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001515
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001516 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517
Steven Cooremance48e852020-10-05 16:02:45 +02001518 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001519
1520exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001521 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001522 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001523 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001524}
1525/* END_CASE */
1526
1527/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001528void copy_success( int source_usage_arg,
1529 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001530 int type_arg, data_t *material,
1531 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001532 int target_usage_arg,
1533 int target_alg_arg, int target_alg2_arg,
1534 int expected_usage_arg,
1535 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001536{
Gilles Peskineca25db92019-04-19 11:43:08 +02001537 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1538 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001539 psa_key_usage_t expected_usage = expected_usage_arg;
1540 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001541 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001542 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1543 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001544 uint8_t *export_buffer = NULL;
1545
Gilles Peskine57ab7212019-01-28 13:03:09 +01001546 PSA_ASSERT( psa_crypto_init( ) );
1547
Gilles Peskineca25db92019-04-19 11:43:08 +02001548 /* Prepare the source key. */
1549 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1550 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001551 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001552 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001553 PSA_ASSERT( psa_import_key( &source_attributes,
1554 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001555 &source_key ) );
1556 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001557
Gilles Peskineca25db92019-04-19 11:43:08 +02001558 /* Prepare the target attributes. */
1559 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001560 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001561 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001562 /* Set volatile lifetime to reset the key identifier to 0. */
1563 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1564 }
1565
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 if( target_usage_arg != -1 )
1567 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1568 if( target_alg_arg != -1 )
1569 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001570 if( target_alg2_arg != -1 )
1571 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001572
1573 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001574 PSA_ASSERT( psa_copy_key( source_key,
1575 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001576
1577 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001578 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001579
1580 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001581 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001582 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1583 psa_get_key_type( &target_attributes ) );
1584 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1585 psa_get_key_bits( &target_attributes ) );
1586 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1587 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001588 TEST_EQUAL( expected_alg2,
1589 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001590 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1591 {
1592 size_t length;
1593 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001594 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001595 material->len, &length ) );
1596 ASSERT_COMPARE( material->x, material->len,
1597 export_buffer, length );
1598 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001599
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001600 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001601 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001602 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001603 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001604
Ronald Cron5425a212020-08-04 14:58:35 +02001605 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001606
1607exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001608 /*
1609 * Source and target key attributes may have been returned by
1610 * psa_get_key_attributes() thus reset them as required.
1611 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001612 psa_reset_key_attributes( &source_attributes );
1613 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001614
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001615 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001616 mbedtls_free( export_buffer );
1617}
1618/* END_CASE */
1619
1620/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001621void copy_fail( int source_usage_arg,
1622 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001623 int type_arg, data_t *material,
1624 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001625 int target_usage_arg,
1626 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001627 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001628 int expected_status_arg )
1629{
1630 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1631 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001632 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1633 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001634 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001635
1636 PSA_ASSERT( psa_crypto_init( ) );
1637
1638 /* Prepare the source key. */
1639 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1640 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001641 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001642 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001643 PSA_ASSERT( psa_import_key( &source_attributes,
1644 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001645 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001646
1647 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001648 psa_set_key_id( &target_attributes, key_id );
1649 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001650 psa_set_key_type( &target_attributes, target_type_arg );
1651 psa_set_key_bits( &target_attributes, target_bits_arg );
1652 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1653 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001654 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001655
1656 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001657 TEST_EQUAL( psa_copy_key( source_key,
1658 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001659 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001660
Ronald Cron5425a212020-08-04 14:58:35 +02001661 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001662
Gilles Peskine4a644642019-05-03 17:14:08 +02001663exit:
1664 psa_reset_key_attributes( &source_attributes );
1665 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001666 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001667}
1668/* END_CASE */
1669
1670/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001671void hash_operation_init( )
1672{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001673 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001674 /* Test each valid way of initializing the object, except for `= {0}`, as
1675 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1676 * though it's OK by the C standard. We could test for this, but we'd need
1677 * to supress the Clang warning for the test. */
1678 psa_hash_operation_t func = psa_hash_operation_init( );
1679 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1680 psa_hash_operation_t zero;
1681
1682 memset( &zero, 0, sizeof( zero ) );
1683
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001684 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001685 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1686 PSA_ERROR_BAD_STATE );
1687 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1688 PSA_ERROR_BAD_STATE );
1689 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1690 PSA_ERROR_BAD_STATE );
1691
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001692 /* A default hash operation should be abortable without error. */
1693 PSA_ASSERT( psa_hash_abort( &func ) );
1694 PSA_ASSERT( psa_hash_abort( &init ) );
1695 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001696}
1697/* END_CASE */
1698
1699/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001700void hash_setup( int alg_arg,
1701 int expected_status_arg )
1702{
1703 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001704 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001705 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001706 psa_status_t status;
1707
Gilles Peskine8817f612018-12-18 00:18:46 +01001708 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001709
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001710 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001711 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001712
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001713 /* Whether setup succeeded or failed, abort must succeed. */
1714 PSA_ASSERT( psa_hash_abort( &operation ) );
1715
1716 /* If setup failed, reproduce the failure, so as to
1717 * test the resulting state of the operation object. */
1718 if( status != PSA_SUCCESS )
1719 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1720
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001721 /* Now the operation object should be reusable. */
1722#if defined(KNOWN_SUPPORTED_HASH_ALG)
1723 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1724 PSA_ASSERT( psa_hash_abort( &operation ) );
1725#endif
1726
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001727exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001728 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001729}
1730/* END_CASE */
1731
1732/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001733void hash_compute_fail( int alg_arg, data_t *input,
1734 int output_size_arg, int expected_status_arg )
1735{
1736 psa_algorithm_t alg = alg_arg;
1737 uint8_t *output = NULL;
1738 size_t output_size = output_size_arg;
1739 size_t output_length = INVALID_EXPORT_LENGTH;
1740 psa_status_t expected_status = expected_status_arg;
1741 psa_status_t status;
1742
1743 ASSERT_ALLOC( output, output_size );
1744
1745 PSA_ASSERT( psa_crypto_init( ) );
1746
1747 status = psa_hash_compute( alg, input->x, input->len,
1748 output, output_size, &output_length );
1749 TEST_EQUAL( status, expected_status );
1750 TEST_ASSERT( output_length <= output_size );
1751
1752exit:
1753 mbedtls_free( output );
1754 PSA_DONE( );
1755}
1756/* END_CASE */
1757
1758/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001759void hash_compare_fail( int alg_arg, data_t *input,
1760 data_t *reference_hash,
1761 int expected_status_arg )
1762{
1763 psa_algorithm_t alg = alg_arg;
1764 psa_status_t expected_status = expected_status_arg;
1765 psa_status_t status;
1766
1767 PSA_ASSERT( psa_crypto_init( ) );
1768
1769 status = psa_hash_compare( alg, input->x, input->len,
1770 reference_hash->x, reference_hash->len );
1771 TEST_EQUAL( status, expected_status );
1772
1773exit:
1774 PSA_DONE( );
1775}
1776/* END_CASE */
1777
1778/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001779void hash_compute_compare( int alg_arg, data_t *input,
1780 data_t *expected_output )
1781{
1782 psa_algorithm_t alg = alg_arg;
1783 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1784 size_t output_length = INVALID_EXPORT_LENGTH;
1785 size_t i;
1786
1787 PSA_ASSERT( psa_crypto_init( ) );
1788
1789 /* Compute with tight buffer */
1790 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001791 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001792 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001793 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001794 ASSERT_COMPARE( output, output_length,
1795 expected_output->x, expected_output->len );
1796
1797 /* Compute with larger buffer */
1798 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1799 output, sizeof( output ),
1800 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001801 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001802 ASSERT_COMPARE( output, output_length,
1803 expected_output->x, expected_output->len );
1804
1805 /* Compare with correct hash */
1806 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1807 output, output_length ) );
1808
1809 /* Compare with trailing garbage */
1810 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1811 output, output_length + 1 ),
1812 PSA_ERROR_INVALID_SIGNATURE );
1813
1814 /* Compare with truncated hash */
1815 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1816 output, output_length - 1 ),
1817 PSA_ERROR_INVALID_SIGNATURE );
1818
1819 /* Compare with corrupted value */
1820 for( i = 0; i < output_length; i++ )
1821 {
Chris Jones9634bb12021-01-20 15:56:42 +00001822 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001823 output[i] ^= 1;
1824 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1825 output, output_length ),
1826 PSA_ERROR_INVALID_SIGNATURE );
1827 output[i] ^= 1;
1828 }
1829
1830exit:
1831 PSA_DONE( );
1832}
1833/* END_CASE */
1834
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001835/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001836void hash_bad_order( )
1837{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001838 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001839 unsigned char input[] = "";
1840 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001841 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001842 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1843 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1844 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001845 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001846 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001847 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001848
Gilles Peskine8817f612018-12-18 00:18:46 +01001849 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001850
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001851 /* Call setup twice in a row. */
1852 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1853 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1854 PSA_ERROR_BAD_STATE );
1855 PSA_ASSERT( psa_hash_abort( &operation ) );
1856
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001857 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001858 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001859 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001860 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001861
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001862 /* Call update after finish. */
1863 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1864 PSA_ASSERT( psa_hash_finish( &operation,
1865 hash, sizeof( hash ), &hash_len ) );
1866 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001867 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001868 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001869
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001870 /* Call verify without calling setup beforehand. */
1871 TEST_EQUAL( psa_hash_verify( &operation,
1872 valid_hash, sizeof( valid_hash ) ),
1873 PSA_ERROR_BAD_STATE );
1874 PSA_ASSERT( psa_hash_abort( &operation ) );
1875
1876 /* Call verify after finish. */
1877 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1878 PSA_ASSERT( psa_hash_finish( &operation,
1879 hash, sizeof( hash ), &hash_len ) );
1880 TEST_EQUAL( psa_hash_verify( &operation,
1881 valid_hash, sizeof( valid_hash ) ),
1882 PSA_ERROR_BAD_STATE );
1883 PSA_ASSERT( psa_hash_abort( &operation ) );
1884
1885 /* Call verify twice in a row. */
1886 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1887 PSA_ASSERT( psa_hash_verify( &operation,
1888 valid_hash, sizeof( valid_hash ) ) );
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 finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001895 TEST_EQUAL( psa_hash_finish( &operation,
1896 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001897 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001898 PSA_ASSERT( psa_hash_abort( &operation ) );
1899
1900 /* Call finish twice in a row. */
1901 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1902 PSA_ASSERT( psa_hash_finish( &operation,
1903 hash, sizeof( hash ), &hash_len ) );
1904 TEST_EQUAL( psa_hash_finish( &operation,
1905 hash, sizeof( hash ), &hash_len ),
1906 PSA_ERROR_BAD_STATE );
1907 PSA_ASSERT( psa_hash_abort( &operation ) );
1908
1909 /* Call finish after calling verify. */
1910 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1911 PSA_ASSERT( psa_hash_verify( &operation,
1912 valid_hash, sizeof( valid_hash ) ) );
1913 TEST_EQUAL( psa_hash_finish( &operation,
1914 hash, sizeof( hash ), &hash_len ),
1915 PSA_ERROR_BAD_STATE );
1916 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001917
1918exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001919 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001920}
1921/* END_CASE */
1922
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001923/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001924void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001925{
1926 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001927 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1928 * appended to it */
1929 unsigned char hash[] = {
1930 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1931 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1932 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001933 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001934 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001935
Gilles Peskine8817f612018-12-18 00:18:46 +01001936 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001937
itayzafrir27e69452018-11-01 14:26:34 +02001938 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001939 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001940 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001941 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001942
itayzafrir27e69452018-11-01 14:26:34 +02001943 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001944 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001945 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001946 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001947
itayzafrir27e69452018-11-01 14:26:34 +02001948 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001950 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001951 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001952
itayzafrirec93d302018-10-18 18:01:10 +03001953exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001954 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001955}
1956/* END_CASE */
1957
Ronald Cronee414c72021-03-18 18:50:08 +01001958/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001959void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001960{
1961 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001962 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001963 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001964 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001965 size_t hash_len;
1966
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001968
itayzafrir58028322018-10-25 10:22:01 +03001969 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001970 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001971 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001972 hash, expected_size - 1, &hash_len ),
1973 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001974
1975exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001976 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001977}
1978/* END_CASE */
1979
Ronald Cronee414c72021-03-18 18:50:08 +01001980/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001981void hash_clone_source_state( )
1982{
1983 psa_algorithm_t alg = PSA_ALG_SHA_256;
1984 unsigned char hash[PSA_HASH_MAX_SIZE];
1985 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1986 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1987 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1988 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1989 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1990 size_t hash_len;
1991
1992 PSA_ASSERT( psa_crypto_init( ) );
1993 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1994
1995 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1996 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1997 PSA_ASSERT( psa_hash_finish( &op_finished,
1998 hash, sizeof( hash ), &hash_len ) );
1999 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2000 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2001
2002 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2003 PSA_ERROR_BAD_STATE );
2004
2005 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2006 PSA_ASSERT( psa_hash_finish( &op_init,
2007 hash, sizeof( hash ), &hash_len ) );
2008 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2009 PSA_ASSERT( psa_hash_finish( &op_finished,
2010 hash, sizeof( hash ), &hash_len ) );
2011 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2012 PSA_ASSERT( psa_hash_finish( &op_aborted,
2013 hash, sizeof( hash ), &hash_len ) );
2014
2015exit:
2016 psa_hash_abort( &op_source );
2017 psa_hash_abort( &op_init );
2018 psa_hash_abort( &op_setup );
2019 psa_hash_abort( &op_finished );
2020 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002021 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002022}
2023/* END_CASE */
2024
Ronald Cronee414c72021-03-18 18:50:08 +01002025/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002026void hash_clone_target_state( )
2027{
2028 psa_algorithm_t alg = PSA_ALG_SHA_256;
2029 unsigned char hash[PSA_HASH_MAX_SIZE];
2030 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2031 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2032 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2033 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2034 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2035 size_t hash_len;
2036
2037 PSA_ASSERT( psa_crypto_init( ) );
2038
2039 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2040 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2041 PSA_ASSERT( psa_hash_finish( &op_finished,
2042 hash, sizeof( hash ), &hash_len ) );
2043 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2044 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2045
2046 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2047 PSA_ASSERT( psa_hash_finish( &op_target,
2048 hash, sizeof( hash ), &hash_len ) );
2049
2050 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2051 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2052 PSA_ERROR_BAD_STATE );
2053 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2054 PSA_ERROR_BAD_STATE );
2055
2056exit:
2057 psa_hash_abort( &op_target );
2058 psa_hash_abort( &op_init );
2059 psa_hash_abort( &op_setup );
2060 psa_hash_abort( &op_finished );
2061 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002062 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002063}
2064/* END_CASE */
2065
itayzafrir58028322018-10-25 10:22:01 +03002066/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002067void mac_operation_init( )
2068{
Jaeden Amero252ef282019-02-15 14:05:35 +00002069 const uint8_t input[1] = { 0 };
2070
Jaeden Amero769ce272019-01-04 11:48:03 +00002071 /* Test each valid way of initializing the object, except for `= {0}`, as
2072 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2073 * though it's OK by the C standard. We could test for this, but we'd need
2074 * to supress the Clang warning for the test. */
2075 psa_mac_operation_t func = psa_mac_operation_init( );
2076 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2077 psa_mac_operation_t zero;
2078
2079 memset( &zero, 0, sizeof( zero ) );
2080
Jaeden Amero252ef282019-02-15 14:05:35 +00002081 /* A freshly-initialized MAC operation should not be usable. */
2082 TEST_EQUAL( psa_mac_update( &func,
2083 input, sizeof( input ) ),
2084 PSA_ERROR_BAD_STATE );
2085 TEST_EQUAL( psa_mac_update( &init,
2086 input, sizeof( input ) ),
2087 PSA_ERROR_BAD_STATE );
2088 TEST_EQUAL( psa_mac_update( &zero,
2089 input, sizeof( input ) ),
2090 PSA_ERROR_BAD_STATE );
2091
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002092 /* A default MAC operation should be abortable without error. */
2093 PSA_ASSERT( psa_mac_abort( &func ) );
2094 PSA_ASSERT( psa_mac_abort( &init ) );
2095 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002096}
2097/* END_CASE */
2098
2099/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002100void mac_setup( int key_type_arg,
2101 data_t *key,
2102 int alg_arg,
2103 int expected_status_arg )
2104{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105 psa_key_type_t key_type = key_type_arg;
2106 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002107 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002108 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002109 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2110#if defined(KNOWN_SUPPORTED_MAC_ALG)
2111 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2112#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002113
Gilles Peskine8817f612018-12-18 00:18:46 +01002114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002115
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002116 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2117 &operation, &status ) )
2118 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002119 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002120
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002121 /* The operation object should be reusable. */
2122#if defined(KNOWN_SUPPORTED_MAC_ALG)
2123 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2124 smoke_test_key_data,
2125 sizeof( smoke_test_key_data ),
2126 KNOWN_SUPPORTED_MAC_ALG,
2127 &operation, &status ) )
2128 goto exit;
2129 TEST_EQUAL( status, PSA_SUCCESS );
2130#endif
2131
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002132exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002133 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134}
2135/* END_CASE */
2136
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002137/* 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 +00002138void mac_bad_order( )
2139{
Ronald Cron5425a212020-08-04 14:58:35 +02002140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002141 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2142 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002143 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002144 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2145 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2146 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002148 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2149 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2150 size_t sign_mac_length = 0;
2151 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2152 const uint8_t verify_mac[] = {
2153 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2154 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2155 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2156
2157 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002158 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002159 psa_set_key_algorithm( &attributes, alg );
2160 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002161
Ronald Cron5425a212020-08-04 14:58:35 +02002162 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2163 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002164
Jaeden Amero252ef282019-02-15 14:05:35 +00002165 /* Call update without calling setup beforehand. */
2166 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2167 PSA_ERROR_BAD_STATE );
2168 PSA_ASSERT( psa_mac_abort( &operation ) );
2169
2170 /* Call sign finish without calling setup beforehand. */
2171 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2172 &sign_mac_length),
2173 PSA_ERROR_BAD_STATE );
2174 PSA_ASSERT( psa_mac_abort( &operation ) );
2175
2176 /* Call verify finish without calling setup beforehand. */
2177 TEST_EQUAL( psa_mac_verify_finish( &operation,
2178 verify_mac, sizeof( verify_mac ) ),
2179 PSA_ERROR_BAD_STATE );
2180 PSA_ASSERT( psa_mac_abort( &operation ) );
2181
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002182 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002183 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2184 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002185 PSA_ERROR_BAD_STATE );
2186 PSA_ASSERT( psa_mac_abort( &operation ) );
2187
Jaeden Amero252ef282019-02-15 14:05:35 +00002188 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002189 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002190 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2191 PSA_ASSERT( psa_mac_sign_finish( &operation,
2192 sign_mac, sizeof( sign_mac ),
2193 &sign_mac_length ) );
2194 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2195 PSA_ERROR_BAD_STATE );
2196 PSA_ASSERT( psa_mac_abort( &operation ) );
2197
2198 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002199 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002200 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2201 PSA_ASSERT( psa_mac_verify_finish( &operation,
2202 verify_mac, sizeof( verify_mac ) ) );
2203 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2204 PSA_ERROR_BAD_STATE );
2205 PSA_ASSERT( psa_mac_abort( &operation ) );
2206
2207 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002208 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002209 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2210 PSA_ASSERT( psa_mac_sign_finish( &operation,
2211 sign_mac, sizeof( sign_mac ),
2212 &sign_mac_length ) );
2213 TEST_EQUAL( psa_mac_sign_finish( &operation,
2214 sign_mac, sizeof( sign_mac ),
2215 &sign_mac_length ),
2216 PSA_ERROR_BAD_STATE );
2217 PSA_ASSERT( psa_mac_abort( &operation ) );
2218
2219 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002220 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002221 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2222 PSA_ASSERT( psa_mac_verify_finish( &operation,
2223 verify_mac, sizeof( verify_mac ) ) );
2224 TEST_EQUAL( psa_mac_verify_finish( &operation,
2225 verify_mac, sizeof( verify_mac ) ),
2226 PSA_ERROR_BAD_STATE );
2227 PSA_ASSERT( psa_mac_abort( &operation ) );
2228
2229 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002230 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002231 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2232 TEST_EQUAL( psa_mac_verify_finish( &operation,
2233 verify_mac, sizeof( verify_mac ) ),
2234 PSA_ERROR_BAD_STATE );
2235 PSA_ASSERT( psa_mac_abort( &operation ) );
2236
2237 /* Setup verify but try sign. */
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 TEST_EQUAL( psa_mac_sign_finish( &operation,
2241 sign_mac, sizeof( sign_mac ),
2242 &sign_mac_length ),
2243 PSA_ERROR_BAD_STATE );
2244 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002245
Ronald Cron5425a212020-08-04 14:58:35 +02002246 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002247
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002248exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002249 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002250}
2251/* END_CASE */
2252
2253/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002254void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002255 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002256 int alg_arg,
2257 data_t *input,
2258 data_t *expected_mac )
2259{
Ronald Cron5425a212020-08-04 14:58:35 +02002260 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002261 psa_key_type_t key_type = key_type_arg;
2262 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002263 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002265 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002266 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002267 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002268 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002269 const size_t output_sizes_to_test[] = {
2270 0,
2271 1,
2272 expected_mac->len - 1,
2273 expected_mac->len,
2274 expected_mac->len + 1,
2275 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002276
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002277 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002278 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002279 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002280
Gilles Peskine8817f612018-12-18 00:18:46 +01002281 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002282
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002283 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002284 psa_set_key_algorithm( &attributes, alg );
2285 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002286
Ronald Cron5425a212020-08-04 14:58:35 +02002287 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2288 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002289
Gilles Peskine8b356b52020-08-25 23:44:59 +02002290 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2291 {
2292 const size_t output_size = output_sizes_to_test[i];
2293 psa_status_t expected_status =
2294 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2295 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002296
Chris Jones9634bb12021-01-20 15:56:42 +00002297 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002298 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002299
Gilles Peskine8b356b52020-08-25 23:44:59 +02002300 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002301 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002302 PSA_ASSERT( psa_mac_update( &operation,
2303 input->x, input->len ) );
2304 TEST_EQUAL( psa_mac_sign_finish( &operation,
2305 actual_mac, output_size,
2306 &mac_length ),
2307 expected_status );
2308 PSA_ASSERT( psa_mac_abort( &operation ) );
2309
2310 if( expected_status == PSA_SUCCESS )
2311 {
2312 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2313 actual_mac, mac_length );
2314 }
2315 mbedtls_free( actual_mac );
2316 actual_mac = NULL;
2317 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002318
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002319exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002320 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002321 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002322 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002323 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002324}
2325/* END_CASE */
2326
2327/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002328void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002329 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002330 int alg_arg,
2331 data_t *input,
2332 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333{
Ronald Cron5425a212020-08-04 14:58:35 +02002334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002335 psa_key_type_t key_type = key_type_arg;
2336 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002337 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002339 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002340
Gilles Peskine69c12672018-06-28 00:07:19 +02002341 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2342
Gilles Peskine8817f612018-12-18 00:18:46 +01002343 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002344
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002345 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002346 psa_set_key_algorithm( &attributes, alg );
2347 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002348
Ronald Cron5425a212020-08-04 14:58:35 +02002349 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2350 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002351
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002352 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002353 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002354 PSA_ASSERT( psa_mac_update( &operation,
2355 input->x, input->len ) );
2356 PSA_ASSERT( psa_mac_verify_finish( &operation,
2357 expected_mac->x,
2358 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002359
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002360 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002361 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002362 PSA_ASSERT( psa_mac_update( &operation,
2363 input->x, input->len ) );
2364 TEST_EQUAL( psa_mac_verify_finish( &operation,
2365 expected_mac->x,
2366 expected_mac->len - 1 ),
2367 PSA_ERROR_INVALID_SIGNATURE );
2368
2369 /* Test a MAC that's too long. */
2370 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2371 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002372 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002373 PSA_ASSERT( psa_mac_update( &operation,
2374 input->x, input->len ) );
2375 TEST_EQUAL( psa_mac_verify_finish( &operation,
2376 perturbed_mac,
2377 expected_mac->len + 1 ),
2378 PSA_ERROR_INVALID_SIGNATURE );
2379
2380 /* Test changing one byte. */
2381 for( size_t i = 0; i < expected_mac->len; i++ )
2382 {
Chris Jones9634bb12021-01-20 15:56:42 +00002383 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002384 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002386 PSA_ASSERT( psa_mac_update( &operation,
2387 input->x, input->len ) );
2388 TEST_EQUAL( psa_mac_verify_finish( &operation,
2389 perturbed_mac,
2390 expected_mac->len ),
2391 PSA_ERROR_INVALID_SIGNATURE );
2392 perturbed_mac[i] ^= 1;
2393 }
2394
Gilles Peskine8c9def32018-02-08 10:02:12 +01002395exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002396 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002397 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002398 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002399 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400}
2401/* END_CASE */
2402
2403/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002404void cipher_operation_init( )
2405{
Jaeden Ameroab439972019-02-15 14:12:05 +00002406 const uint8_t input[1] = { 0 };
2407 unsigned char output[1] = { 0 };
2408 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002409 /* Test each valid way of initializing the object, except for `= {0}`, as
2410 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2411 * though it's OK by the C standard. We could test for this, but we'd need
2412 * to supress the Clang warning for the test. */
2413 psa_cipher_operation_t func = psa_cipher_operation_init( );
2414 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2415 psa_cipher_operation_t zero;
2416
2417 memset( &zero, 0, sizeof( zero ) );
2418
Jaeden Ameroab439972019-02-15 14:12:05 +00002419 /* A freshly-initialized cipher operation should not be usable. */
2420 TEST_EQUAL( psa_cipher_update( &func,
2421 input, sizeof( input ),
2422 output, sizeof( output ),
2423 &output_length ),
2424 PSA_ERROR_BAD_STATE );
2425 TEST_EQUAL( psa_cipher_update( &init,
2426 input, sizeof( input ),
2427 output, sizeof( output ),
2428 &output_length ),
2429 PSA_ERROR_BAD_STATE );
2430 TEST_EQUAL( psa_cipher_update( &zero,
2431 input, sizeof( input ),
2432 output, sizeof( output ),
2433 &output_length ),
2434 PSA_ERROR_BAD_STATE );
2435
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002436 /* A default cipher operation should be abortable without error. */
2437 PSA_ASSERT( psa_cipher_abort( &func ) );
2438 PSA_ASSERT( psa_cipher_abort( &init ) );
2439 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002440}
2441/* END_CASE */
2442
2443/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444void cipher_setup( int key_type_arg,
2445 data_t *key,
2446 int alg_arg,
2447 int expected_status_arg )
2448{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449 psa_key_type_t key_type = key_type_arg;
2450 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002451 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002452 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002453 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002454#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002455 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2456#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002457
Gilles Peskine8817f612018-12-18 00:18:46 +01002458 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002459
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002460 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2461 &operation, &status ) )
2462 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002463 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002464
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002465 /* The operation object should be reusable. */
2466#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2467 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2468 smoke_test_key_data,
2469 sizeof( smoke_test_key_data ),
2470 KNOWN_SUPPORTED_CIPHER_ALG,
2471 &operation, &status ) )
2472 goto exit;
2473 TEST_EQUAL( status, PSA_SUCCESS );
2474#endif
2475
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002477 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002478 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002479}
2480/* END_CASE */
2481
Ronald Cronee414c72021-03-18 18:50:08 +01002482/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002483void cipher_bad_order( )
2484{
Ronald Cron5425a212020-08-04 14:58:35 +02002485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002486 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2487 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002489 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002490 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002491 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002492 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2493 0xaa, 0xaa, 0xaa, 0xaa };
2494 const uint8_t text[] = {
2495 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2496 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002497 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002498 size_t length = 0;
2499
2500 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002501 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2502 psa_set_key_algorithm( &attributes, alg );
2503 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002504 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2505 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002506
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002507 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002508 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2509 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002510 PSA_ERROR_BAD_STATE );
2511 PSA_ASSERT( psa_cipher_abort( &operation ) );
2512
2513 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002514 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2515 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002516 PSA_ERROR_BAD_STATE );
2517 PSA_ASSERT( psa_cipher_abort( &operation ) );
2518
Jaeden Ameroab439972019-02-15 14:12:05 +00002519 /* Generate an IV without calling setup beforehand. */
2520 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2521 buffer, sizeof( buffer ),
2522 &length ),
2523 PSA_ERROR_BAD_STATE );
2524 PSA_ASSERT( psa_cipher_abort( &operation ) );
2525
2526 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002527 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002528 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2529 buffer, sizeof( buffer ),
2530 &length ) );
2531 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2532 buffer, sizeof( buffer ),
2533 &length ),
2534 PSA_ERROR_BAD_STATE );
2535 PSA_ASSERT( psa_cipher_abort( &operation ) );
2536
2537 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002538 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002539 PSA_ASSERT( psa_cipher_set_iv( &operation,
2540 iv, sizeof( iv ) ) );
2541 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2542 buffer, sizeof( buffer ),
2543 &length ),
2544 PSA_ERROR_BAD_STATE );
2545 PSA_ASSERT( psa_cipher_abort( &operation ) );
2546
2547 /* Set an IV without calling setup beforehand. */
2548 TEST_EQUAL( psa_cipher_set_iv( &operation,
2549 iv, sizeof( iv ) ),
2550 PSA_ERROR_BAD_STATE );
2551 PSA_ASSERT( psa_cipher_abort( &operation ) );
2552
2553 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002554 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002555 PSA_ASSERT( psa_cipher_set_iv( &operation,
2556 iv, sizeof( iv ) ) );
2557 TEST_EQUAL( psa_cipher_set_iv( &operation,
2558 iv, sizeof( iv ) ),
2559 PSA_ERROR_BAD_STATE );
2560 PSA_ASSERT( psa_cipher_abort( &operation ) );
2561
2562 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002563 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002564 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2565 buffer, sizeof( buffer ),
2566 &length ) );
2567 TEST_EQUAL( psa_cipher_set_iv( &operation,
2568 iv, sizeof( iv ) ),
2569 PSA_ERROR_BAD_STATE );
2570 PSA_ASSERT( psa_cipher_abort( &operation ) );
2571
2572 /* Call update without calling setup beforehand. */
2573 TEST_EQUAL( psa_cipher_update( &operation,
2574 text, sizeof( text ),
2575 buffer, sizeof( buffer ),
2576 &length ),
2577 PSA_ERROR_BAD_STATE );
2578 PSA_ASSERT( psa_cipher_abort( &operation ) );
2579
2580 /* Call update without an IV where an IV is required. */
2581 TEST_EQUAL( psa_cipher_update( &operation,
2582 text, sizeof( text ),
2583 buffer, sizeof( buffer ),
2584 &length ),
2585 PSA_ERROR_BAD_STATE );
2586 PSA_ASSERT( psa_cipher_abort( &operation ) );
2587
2588 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002589 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002590 PSA_ASSERT( psa_cipher_set_iv( &operation,
2591 iv, sizeof( iv ) ) );
2592 PSA_ASSERT( psa_cipher_finish( &operation,
2593 buffer, sizeof( buffer ), &length ) );
2594 TEST_EQUAL( psa_cipher_update( &operation,
2595 text, sizeof( text ),
2596 buffer, sizeof( buffer ),
2597 &length ),
2598 PSA_ERROR_BAD_STATE );
2599 PSA_ASSERT( psa_cipher_abort( &operation ) );
2600
2601 /* Call finish without calling setup beforehand. */
2602 TEST_EQUAL( psa_cipher_finish( &operation,
2603 buffer, sizeof( buffer ), &length ),
2604 PSA_ERROR_BAD_STATE );
2605 PSA_ASSERT( psa_cipher_abort( &operation ) );
2606
2607 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002608 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002609 /* Not calling update means we are encrypting an empty buffer, which is OK
2610 * for cipher modes with padding. */
2611 TEST_EQUAL( psa_cipher_finish( &operation,
2612 buffer, sizeof( buffer ), &length ),
2613 PSA_ERROR_BAD_STATE );
2614 PSA_ASSERT( psa_cipher_abort( &operation ) );
2615
2616 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002617 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002618 PSA_ASSERT( psa_cipher_set_iv( &operation,
2619 iv, sizeof( iv ) ) );
2620 PSA_ASSERT( psa_cipher_finish( &operation,
2621 buffer, sizeof( buffer ), &length ) );
2622 TEST_EQUAL( psa_cipher_finish( &operation,
2623 buffer, sizeof( buffer ), &length ),
2624 PSA_ERROR_BAD_STATE );
2625 PSA_ASSERT( psa_cipher_abort( &operation ) );
2626
Ronald Cron5425a212020-08-04 14:58:35 +02002627 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002628
Jaeden Ameroab439972019-02-15 14:12:05 +00002629exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002630 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002631 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002632}
2633/* END_CASE */
2634
2635/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002636void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002637 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002638 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002639 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002640{
Ronald Cron5425a212020-08-04 14:58:35 +02002641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642 psa_status_t status;
2643 psa_key_type_t key_type = key_type_arg;
2644 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002645 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002646 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647 size_t output_buffer_size = 0;
2648 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002649 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002650 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652
Gilles Peskine8817f612018-12-18 00:18:46 +01002653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002655 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2656 psa_set_key_algorithm( &attributes, alg );
2657 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002658
Ronald Cron5425a212020-08-04 14:58:35 +02002659 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2660 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661
Ronald Cron5425a212020-08-04 14:58:35 +02002662 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002664 if( iv->len > 0 )
2665 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002666 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002667 }
2668
gabor-mezei-armceface22021-01-21 12:26:17 +01002669 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2670 TEST_ASSERT( output_buffer_size <=
2671 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002672 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Gilles Peskine8817f612018-12-18 00:18:46 +01002674 PSA_ASSERT( psa_cipher_update( &operation,
2675 input->x, input->len,
2676 output, output_buffer_size,
2677 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002678 TEST_ASSERT( function_output_length <=
2679 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2680 TEST_ASSERT( function_output_length <=
2681 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002682 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002683
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002685 ( output_buffer_size == 0 ? NULL :
2686 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002687 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002688 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002689 TEST_ASSERT( function_output_length <=
2690 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2691 TEST_ASSERT( function_output_length <=
2692 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002693 total_output_length += function_output_length;
2694
Gilles Peskinefe11b722018-12-18 00:24:04 +01002695 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696 if( expected_status == PSA_SUCCESS )
2697 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002698 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002699 ASSERT_COMPARE( expected_output->x, expected_output->len,
2700 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002701 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002702
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002704 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002705 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002706 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002707 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708}
2709/* END_CASE */
2710
2711/* BEGIN_CASE */
2712void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002713 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002714 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002715 int first_part_size_arg,
2716 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002717 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002718{
Ronald Cron5425a212020-08-04 14:58:35 +02002719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720 psa_key_type_t key_type = key_type_arg;
2721 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002722 size_t first_part_size = first_part_size_arg;
2723 size_t output1_length = output1_length_arg;
2724 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002725 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002726 size_t output_buffer_size = 0;
2727 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002728 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002729 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002730 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002731
Gilles Peskine8817f612018-12-18 00:18:46 +01002732 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002733
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002734 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2735 psa_set_key_algorithm( &attributes, alg );
2736 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002737
Ronald Cron5425a212020-08-04 14:58:35 +02002738 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2739 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740
Ronald Cron5425a212020-08-04 14:58:35 +02002741 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002742
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002743 if( iv->len > 0 )
2744 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002745 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002746 }
2747
gabor-mezei-armceface22021-01-21 12:26:17 +01002748 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2749 TEST_ASSERT( output_buffer_size <=
2750 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002751 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752
Gilles Peskinee0866522019-02-19 19:44:00 +01002753 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002754 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2755 output, output_buffer_size,
2756 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002757 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002758 TEST_ASSERT( function_output_length <=
2759 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2760 TEST_ASSERT( function_output_length <=
2761 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002762 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002763
Gilles Peskine8817f612018-12-18 00:18:46 +01002764 PSA_ASSERT( psa_cipher_update( &operation,
2765 input->x + first_part_size,
2766 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002767 ( output_buffer_size == 0 ? NULL :
2768 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002769 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002770 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002771 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002772 TEST_ASSERT( function_output_length <=
2773 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2774 alg,
2775 input->len - first_part_size ) );
2776 TEST_ASSERT( function_output_length <=
2777 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002778 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002779
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002781 ( output_buffer_size == 0 ? NULL :
2782 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002783 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002784 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002785 TEST_ASSERT( function_output_length <=
2786 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2787 TEST_ASSERT( function_output_length <=
2788 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002789 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002790 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002791
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002792 ASSERT_COMPARE( expected_output->x, expected_output->len,
2793 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002794
2795exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002796 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002797 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002798 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002799 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002800}
2801/* END_CASE */
2802
2803/* BEGIN_CASE */
2804void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002805 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002806 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002807 int first_part_size_arg,
2808 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002809 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002810{
Ronald Cron5425a212020-08-04 14:58:35 +02002811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812 psa_key_type_t key_type = key_type_arg;
2813 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002814 size_t first_part_size = first_part_size_arg;
2815 size_t output1_length = output1_length_arg;
2816 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002817 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818 size_t output_buffer_size = 0;
2819 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002820 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002821 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823
Gilles Peskine8817f612018-12-18 00:18:46 +01002824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2827 psa_set_key_algorithm( &attributes, alg );
2828 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002829
Ronald Cron5425a212020-08-04 14:58:35 +02002830 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2831 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002832
Ronald Cron5425a212020-08-04 14:58:35 +02002833 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834
Steven Cooreman177deba2020-09-07 17:14:14 +02002835 if( iv->len > 0 )
2836 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002837 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002838 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839
gabor-mezei-armceface22021-01-21 12:26:17 +01002840 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2841 TEST_ASSERT( output_buffer_size <=
2842 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002843 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
Gilles Peskinee0866522019-02-19 19:44:00 +01002845 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002846 PSA_ASSERT( psa_cipher_update( &operation,
2847 input->x, first_part_size,
2848 output, output_buffer_size,
2849 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002850 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002851 TEST_ASSERT( function_output_length <=
2852 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2853 TEST_ASSERT( function_output_length <=
2854 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002855 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002856
Gilles Peskine8817f612018-12-18 00:18:46 +01002857 PSA_ASSERT( psa_cipher_update( &operation,
2858 input->x + first_part_size,
2859 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002860 ( output_buffer_size == 0 ? NULL :
2861 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002862 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002863 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002864 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002865 TEST_ASSERT( function_output_length <=
2866 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2867 alg,
2868 input->len - first_part_size ) );
2869 TEST_ASSERT( function_output_length <=
2870 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002871 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002872
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002874 ( output_buffer_size == 0 ? NULL :
2875 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002876 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002877 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002878 TEST_ASSERT( function_output_length <=
2879 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2880 TEST_ASSERT( function_output_length <=
2881 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002882 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002883 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002885 ASSERT_COMPARE( expected_output->x, expected_output->len,
2886 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002887
2888exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002889 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002890 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002891 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002892 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002893}
2894/* END_CASE */
2895
Gilles Peskine50e586b2018-06-08 14:28:46 +02002896/* BEGIN_CASE */
2897void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002898 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002899 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002900 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901{
Ronald Cron5425a212020-08-04 14:58:35 +02002902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903 psa_status_t status;
2904 psa_key_type_t key_type = key_type_arg;
2905 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002906 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002907 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908 size_t output_buffer_size = 0;
2909 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002910 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002911 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913
Gilles Peskine8817f612018-12-18 00:18:46 +01002914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2917 psa_set_key_algorithm( &attributes, alg );
2918 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002919
Ronald Cron5425a212020-08-04 14:58:35 +02002920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2921 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922
Ronald Cron5425a212020-08-04 14:58:35 +02002923 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924
Steven Cooreman177deba2020-09-07 17:14:14 +02002925 if( iv->len > 0 )
2926 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002927 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002928 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929
gabor-mezei-armceface22021-01-21 12:26:17 +01002930 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2931 TEST_ASSERT( output_buffer_size <=
2932 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002933 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934
Gilles Peskine8817f612018-12-18 00:18:46 +01002935 PSA_ASSERT( psa_cipher_update( &operation,
2936 input->x, input->len,
2937 output, output_buffer_size,
2938 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002939 TEST_ASSERT( function_output_length <=
2940 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2941 TEST_ASSERT( function_output_length <=
2942 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002943 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002944
Gilles Peskine50e586b2018-06-08 14:28:46 +02002945 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002946 ( output_buffer_size == 0 ? NULL :
2947 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002948 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002949 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002950 TEST_ASSERT( function_output_length <=
2951 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2952 TEST_ASSERT( function_output_length <=
2953 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002954 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002955 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002956
2957 if( expected_status == PSA_SUCCESS )
2958 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002959 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002960 ASSERT_COMPARE( expected_output->x, expected_output->len,
2961 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002962 }
2963
Gilles Peskine50e586b2018-06-08 14:28:46 +02002964exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002965 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002966 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002967 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002968 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969}
2970/* END_CASE */
2971
Gilles Peskine50e586b2018-06-08 14:28:46 +02002972/* BEGIN_CASE */
2973void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002974 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002975 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002976{
Ronald Cron5425a212020-08-04 14:58:35 +02002977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002978 psa_key_type_t key_type = key_type_arg;
2979 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002980 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981 size_t iv_size = 16;
2982 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002983 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002984 size_t output1_size = 0;
2985 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002986 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002987 size_t output2_size = 0;
2988 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002989 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002990 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2991 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002993
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002995
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2997 psa_set_key_algorithm( &attributes, alg );
2998 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002999
Ronald Cron5425a212020-08-04 14:58:35 +02003000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3001 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003002
Ronald Cron5425a212020-08-04 14:58:35 +02003003 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3004 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003005
Steven Cooreman177deba2020-09-07 17:14:14 +02003006 if( alg != PSA_ALG_ECB_NO_PADDING )
3007 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003008 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3009 iv, iv_size,
3010 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003011 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003012 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3013 TEST_ASSERT( output1_size <=
3014 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003015 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003016
Gilles Peskine8817f612018-12-18 00:18:46 +01003017 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3018 output1, output1_size,
3019 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003020 TEST_ASSERT( output1_length <=
3021 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3022 TEST_ASSERT( output1_length <=
3023 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3024
Gilles Peskine8817f612018-12-18 00:18:46 +01003025 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003026 output1 + output1_length,
3027 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003029 TEST_ASSERT( function_output_length <=
3030 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3031 TEST_ASSERT( function_output_length <=
3032 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003033
Gilles Peskine048b7f02018-06-08 14:20:49 +02003034 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003035
Gilles Peskine8817f612018-12-18 00:18:46 +01003036 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003037
3038 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003039 TEST_ASSERT( output2_size <=
3040 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3041 TEST_ASSERT( output2_size <=
3042 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003043 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003044
Steven Cooreman177deba2020-09-07 17:14:14 +02003045 if( iv_length > 0 )
3046 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003047 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3048 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003049 }
3050
Gilles Peskine8817f612018-12-18 00:18:46 +01003051 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3052 output2, output2_size,
3053 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003054 TEST_ASSERT( output2_length <=
3055 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3056 TEST_ASSERT( output2_length <=
3057 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3058
Gilles Peskine048b7f02018-06-08 14:20:49 +02003059 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 PSA_ASSERT( psa_cipher_finish( &operation2,
3061 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003062 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003064 TEST_ASSERT( function_output_length <=
3065 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3066 TEST_ASSERT( function_output_length <=
3067 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003068
Gilles Peskine048b7f02018-06-08 14:20:49 +02003069 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003070
Gilles Peskine8817f612018-12-18 00:18:46 +01003071 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003072
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003073 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003074
3075exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003076 psa_cipher_abort( &operation1 );
3077 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003078 mbedtls_free( output1 );
3079 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003080 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003081 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003082}
3083/* END_CASE */
3084
3085/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003086void cipher_verify_output_multipart( int alg_arg,
3087 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003088 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003089 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003090 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003091{
Ronald Cron5425a212020-08-04 14:58:35 +02003092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003093 psa_key_type_t key_type = key_type_arg;
3094 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003095 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003096 unsigned char iv[16] = {0};
3097 size_t iv_size = 16;
3098 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003099 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003100 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003101 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003102 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003103 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003104 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003105 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003106 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3107 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003109
Gilles Peskine8817f612018-12-18 00:18:46 +01003110 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003111
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003112 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3113 psa_set_key_algorithm( &attributes, alg );
3114 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003115
Ronald Cron5425a212020-08-04 14:58:35 +02003116 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3117 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003118
Ronald Cron5425a212020-08-04 14:58:35 +02003119 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3120 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003121
Steven Cooreman177deba2020-09-07 17:14:14 +02003122 if( alg != PSA_ALG_ECB_NO_PADDING )
3123 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003124 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3125 iv, iv_size,
3126 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003127 }
3128
gabor-mezei-armceface22021-01-21 12:26:17 +01003129 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3130 TEST_ASSERT( output1_buffer_size <=
3131 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003132 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003133
Gilles Peskinee0866522019-02-19 19:44:00 +01003134 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003135
Gilles Peskine8817f612018-12-18 00:18:46 +01003136 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3137 output1, output1_buffer_size,
3138 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003139 TEST_ASSERT( function_output_length <=
3140 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3141 TEST_ASSERT( function_output_length <=
3142 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003143 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003144
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 PSA_ASSERT( psa_cipher_update( &operation1,
3146 input->x + first_part_size,
3147 input->len - first_part_size,
3148 output1, output1_buffer_size,
3149 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003150 TEST_ASSERT( function_output_length <=
3151 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3152 alg,
3153 input->len - first_part_size ) );
3154 TEST_ASSERT( function_output_length <=
3155 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003156 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_cipher_finish( &operation1,
3159 output1 + output1_length,
3160 output1_buffer_size - output1_length,
3161 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003162 TEST_ASSERT( function_output_length <=
3163 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3164 TEST_ASSERT( function_output_length <=
3165 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003166 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003167
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003169
Gilles Peskine048b7f02018-06-08 14:20:49 +02003170 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003171 TEST_ASSERT( output2_buffer_size <=
3172 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3173 TEST_ASSERT( output2_buffer_size <=
3174 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003175 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003176
Steven Cooreman177deba2020-09-07 17:14:14 +02003177 if( iv_length > 0 )
3178 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003179 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3180 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003181 }
Moran Pekerded84402018-06-06 16:36:50 +03003182
Gilles Peskine8817f612018-12-18 00:18:46 +01003183 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3184 output2, output2_buffer_size,
3185 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003186 TEST_ASSERT( function_output_length <=
3187 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3188 TEST_ASSERT( function_output_length <=
3189 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003190 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_cipher_update( &operation2,
3193 output1 + first_part_size,
3194 output1_length - first_part_size,
3195 output2, output2_buffer_size,
3196 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003197 TEST_ASSERT( function_output_length <=
3198 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3199 alg,
3200 output1_length - first_part_size ) );
3201 TEST_ASSERT( function_output_length <=
3202 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003203 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003204
Gilles Peskine8817f612018-12-18 00:18:46 +01003205 PSA_ASSERT( psa_cipher_finish( &operation2,
3206 output2 + output2_length,
3207 output2_buffer_size - output2_length,
3208 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003209 TEST_ASSERT( function_output_length <=
3210 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3211 TEST_ASSERT( function_output_length <=
3212 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003213 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003216
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003217 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003218
3219exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003220 psa_cipher_abort( &operation1 );
3221 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003222 mbedtls_free( output1 );
3223 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003224 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003225 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003226}
3227/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003228
Gilles Peskine20035e32018-02-03 22:44:14 +01003229/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003230void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003231 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003232 data_t *nonce,
3233 data_t *additional_data,
3234 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003235 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003236{
Ronald Cron5425a212020-08-04 14:58:35 +02003237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003238 psa_key_type_t key_type = key_type_arg;
3239 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003240 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003241 unsigned char *output_data = NULL;
3242 size_t output_size = 0;
3243 size_t output_length = 0;
3244 unsigned char *output_data2 = NULL;
3245 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003246 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003247 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003252 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3253 psa_set_key_algorithm( &attributes, alg );
3254 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003255
Gilles Peskine049c7532019-05-15 20:22:09 +02003256 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003257 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003258 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3259 key_bits = psa_get_key_bits( &attributes );
3260
3261 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3262 alg );
3263 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3264 * should be exact. */
3265 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3266 expected_result != PSA_ERROR_NOT_SUPPORTED )
3267 {
3268 TEST_EQUAL( output_size,
3269 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3270 TEST_ASSERT( output_size <=
3271 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3272 }
3273 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003274
Steven Cooremanf49478b2021-02-15 15:19:25 +01003275 status = psa_aead_encrypt( key, alg,
3276 nonce->x, nonce->len,
3277 additional_data->x,
3278 additional_data->len,
3279 input_data->x, input_data->len,
3280 output_data, output_size,
3281 &output_length );
3282
3283 /* If the operation is not supported, just skip and not fail in case the
3284 * encryption involves a common limitation of cryptography hardwares and
3285 * an alternative implementation. */
3286 if( status == PSA_ERROR_NOT_SUPPORTED )
3287 {
3288 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3289 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3290 }
3291
3292 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003293
3294 if( PSA_SUCCESS == expected_result )
3295 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003296 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003297
Gilles Peskine003a4a92019-05-14 16:09:40 +02003298 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3299 * should be exact. */
3300 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003301 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003302
gabor-mezei-armceface22021-01-21 12:26:17 +01003303 TEST_ASSERT( input_data->len <=
3304 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3305
Ronald Cron5425a212020-08-04 14:58:35 +02003306 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003307 nonce->x, nonce->len,
3308 additional_data->x,
3309 additional_data->len,
3310 output_data, output_length,
3311 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003312 &output_length2 ),
3313 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003314
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003315 ASSERT_COMPARE( input_data->x, input_data->len,
3316 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003318
Gilles Peskinea1cac842018-06-11 19:33:02 +02003319exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003320 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003321 mbedtls_free( output_data );
3322 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003323 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003324}
3325/* END_CASE */
3326
3327/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003328void aead_encrypt( int key_type_arg, data_t *key_data,
3329 int alg_arg,
3330 data_t *nonce,
3331 data_t *additional_data,
3332 data_t *input_data,
3333 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334{
Ronald Cron5425a212020-08-04 14:58:35 +02003335 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336 psa_key_type_t key_type = key_type_arg;
3337 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003338 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339 unsigned char *output_data = NULL;
3340 size_t output_size = 0;
3341 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003343 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344
Gilles Peskine8817f612018-12-18 00:18:46 +01003345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003346
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3348 psa_set_key_algorithm( &attributes, alg );
3349 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003350
Gilles Peskine049c7532019-05-15 20:22:09 +02003351 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003352 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003353 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3354 key_bits = psa_get_key_bits( &attributes );
3355
3356 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3357 alg );
3358 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3359 * should be exact. */
3360 TEST_EQUAL( output_size,
3361 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3362 TEST_ASSERT( output_size <=
3363 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3364 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365
Steven Cooremand588ea12021-01-11 19:36:04 +01003366 status = psa_aead_encrypt( key, alg,
3367 nonce->x, nonce->len,
3368 additional_data->x, additional_data->len,
3369 input_data->x, input_data->len,
3370 output_data, output_size,
3371 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003372
Ronald Cron28a45ed2021-02-09 20:35:42 +01003373 /* If the operation is not supported, just skip and not fail in case the
3374 * encryption involves a common limitation of cryptography hardwares and
3375 * an alternative implementation. */
3376 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003377 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003378 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3379 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003380 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003381
3382 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003383 ASSERT_COMPARE( expected_result->x, expected_result->len,
3384 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003385
Gilles Peskinea1cac842018-06-11 19:33:02 +02003386exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003387 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003388 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003389 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390}
3391/* END_CASE */
3392
3393/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003394void aead_decrypt( int key_type_arg, data_t *key_data,
3395 int alg_arg,
3396 data_t *nonce,
3397 data_t *additional_data,
3398 data_t *input_data,
3399 data_t *expected_data,
3400 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003401{
Ronald Cron5425a212020-08-04 14:58:35 +02003402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403 psa_key_type_t key_type = key_type_arg;
3404 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003405 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406 unsigned char *output_data = NULL;
3407 size_t output_size = 0;
3408 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003410 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003411 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003412
Gilles Peskine8817f612018-12-18 00:18:46 +01003413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003415 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3416 psa_set_key_algorithm( &attributes, alg );
3417 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418
Gilles Peskine049c7532019-05-15 20:22:09 +02003419 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003420 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003421 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3422 key_bits = psa_get_key_bits( &attributes );
3423
3424 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3425 alg );
3426 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3427 expected_result != PSA_ERROR_NOT_SUPPORTED )
3428 {
3429 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3430 * should be exact. */
3431 TEST_EQUAL( output_size,
3432 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3433 TEST_ASSERT( output_size <=
3434 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3435 }
3436 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003437
Steven Cooremand588ea12021-01-11 19:36:04 +01003438 status = psa_aead_decrypt( key, alg,
3439 nonce->x, nonce->len,
3440 additional_data->x,
3441 additional_data->len,
3442 input_data->x, input_data->len,
3443 output_data, output_size,
3444 &output_length );
3445
Ronald Cron28a45ed2021-02-09 20:35:42 +01003446 /* If the operation is not supported, just skip and not fail in case the
3447 * decryption involves a common limitation of cryptography hardwares and
3448 * an alternative implementation. */
3449 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003450 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003451 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3452 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003453 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003454
3455 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456
Gilles Peskine2d277862018-06-18 15:41:12 +02003457 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003458 ASSERT_COMPARE( expected_data->x, expected_data->len,
3459 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003462 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003463 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003464 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465}
3466/* END_CASE */
3467
3468/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003469void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3470 int alg_arg,
3471 data_t *nonce,
3472 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003473 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003474 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003475 int do_test_data_chunked,
3476 int do_set_lengths,
3477 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003478{
Paul Elliottd3f82412021-06-16 16:52:21 +01003479 size_t ad_part_len = 0;
3480 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003481 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003482
Paul Elliotte64deda2021-09-09 14:07:23 +01003483 /* Ensure that either one part of the test or the other is done, i.e this
3484 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003485 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3486
3487 /* Temporary whilst we have algorithms that cannot support chunking */
3488 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003489 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003490 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3491 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003492 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003493 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003494
Paul Elliott33746aa2021-09-15 16:40:40 +01003495 if( do_set_lengths )
3496 {
3497 if( ad_part_len & 0x01 )
3498 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3499 else
3500 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3501 }
3502
Paul Elliott329d5382021-07-22 17:10:45 +01003503 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003504 if( !aead_multipart_internal_func( key_type_arg, key_data,
3505 alg_arg, nonce,
3506 additional_data,
3507 ad_part_len,
3508 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003509 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003510 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003511 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003512 break;
3513
3514 /* length(0) part, length(ad_part_len) part, length(0) part... */
3515 mbedtls_test_set_step( 1000 + ad_part_len );
3516
3517 if( !aead_multipart_internal_func( key_type_arg, key_data,
3518 alg_arg, nonce,
3519 additional_data,
3520 ad_part_len,
3521 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003522 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003523 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003524 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003525 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003526 }
3527 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003528
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003529 /* Temporary whilst we have algorithms that cannot support chunking */
3530 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003531 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003532 for( data_part_len = 1; data_part_len <= input_data->len;
3533 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003534 {
Paul Elliott329d5382021-07-22 17:10:45 +01003535 /* Split data into length(data_part_len) parts. */
3536 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003537
Paul Elliott33746aa2021-09-15 16:40:40 +01003538 if( do_set_lengths )
3539 {
3540 if( data_part_len & 0x01 )
3541 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3542 else
3543 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3544 }
3545
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003546 if( !aead_multipart_internal_func( key_type_arg, key_data,
3547 alg_arg, nonce,
3548 additional_data, -1,
3549 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003550 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003551 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003552 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003553 break;
3554
3555 /* length(0) part, length(data_part_len) part, length(0) part... */
3556 mbedtls_test_set_step( 3000 + data_part_len );
3557
3558 if( !aead_multipart_internal_func( key_type_arg, key_data,
3559 alg_arg, nonce,
3560 additional_data, -1,
3561 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003562 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003563 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003564 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003565 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003566 }
3567 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003568
Paul Elliott0023e0a2021-04-27 10:06:22 +01003569
Paul Elliott8fc45162021-06-23 16:06:01 +01003570 /* Goto is required to silence warnings about unused labels, as we
3571 * don't actually do any test assertions in this function. */
3572 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003573}
3574/* END_CASE */
3575
3576/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003577void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3578 int alg_arg,
3579 data_t *nonce,
3580 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003581 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003583 int do_test_data_chunked,
3584 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003585 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003586{
Paul Elliottd3f82412021-06-16 16:52:21 +01003587 size_t ad_part_len = 0;
3588 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003589 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003590
Paul Elliotte64deda2021-09-09 14:07:23 +01003591 /* Ensure that either one part of the test or the other is done, i.e this
3592 * test does something. */
3593 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3594
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003595 /* Temporary whilst we have algorithms that cannot support chunking */
3596 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003597 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003598 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3599 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003600 {
Paul Elliott329d5382021-07-22 17:10:45 +01003601 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003602 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003603
Paul Elliott33746aa2021-09-15 16:40:40 +01003604 if( do_set_lengths )
3605 {
3606 if( ad_part_len & 0x01 )
3607 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3608 else
3609 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3610 }
3611
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003612 if( !aead_multipart_internal_func( key_type_arg, key_data,
3613 alg_arg, nonce,
3614 additional_data,
3615 ad_part_len,
3616 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003617 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003618 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003619 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003620 break;
3621
3622 /* length(0) part, length(ad_part_len) part, length(0) part... */
3623 mbedtls_test_set_step( 1000 + ad_part_len );
3624
3625 if( !aead_multipart_internal_func( key_type_arg, key_data,
3626 alg_arg, nonce,
3627 additional_data,
3628 ad_part_len,
3629 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003630 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003631 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003632 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003633 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003634 }
3635 }
3636
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003637 /* Temporary whilst we have algorithms that cannot support chunking */
3638 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003639 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003640 for( data_part_len = 1; data_part_len <= input_data->len;
3641 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003642 {
Paul Elliott329d5382021-07-22 17:10:45 +01003643 /* Split data into length(data_part_len) parts. */
3644 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003645
Paul Elliott33746aa2021-09-15 16:40:40 +01003646 if( do_set_lengths )
3647 {
3648 if( data_part_len & 0x01 )
3649 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3650 else
3651 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3652 }
3653
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003654 if( !aead_multipart_internal_func( key_type_arg, key_data,
3655 alg_arg, nonce,
3656 additional_data, -1,
3657 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003658 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003659 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003660 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003661 break;
3662
3663 /* length(0) part, length(data_part_len) part, length(0) part... */
3664 mbedtls_test_set_step( 3000 + data_part_len );
3665
3666 if( !aead_multipart_internal_func( key_type_arg, key_data,
3667 alg_arg, nonce,
3668 additional_data, -1,
3669 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003670 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003671 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003672 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003673 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003674 }
3675 }
3676
Paul Elliott8fc45162021-06-23 16:06:01 +01003677 /* Goto is required to silence warnings about unused labels, as we
3678 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003679 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003680}
3681/* END_CASE */
3682
3683/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003684void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3685 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003686 int nonce_length,
3687 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003688 data_t *additional_data,
3689 data_t *input_data,
3690 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003691{
3692
3693 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3694 psa_key_type_t key_type = key_type_arg;
3695 psa_algorithm_t alg = alg_arg;
3696 psa_aead_operation_t operation;
3697 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3699 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003700 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003701 size_t actual_nonce_length = 0;
3702 size_t expected_nonce_length = expected_nonce_length_arg;
3703 unsigned char *output = NULL;
3704 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003705 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003706 size_t ciphertext_size = 0;
3707 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003708 size_t tag_length = 0;
3709 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003710
3711 PSA_ASSERT( psa_crypto_init( ) );
3712
3713 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3714 psa_set_key_algorithm( & attributes, alg );
3715 psa_set_key_type( & attributes, key_type );
3716
3717 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3718 &key ) );
3719
3720 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3721
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003722 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3723
Paul Elliottf1277632021-08-24 18:11:37 +01003724 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003725
Paul Elliottf1277632021-08-24 18:11:37 +01003726 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003727
Paul Elliottf1277632021-08-24 18:11:37 +01003728 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003729
Paul Elliottf1277632021-08-24 18:11:37 +01003730 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003731
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003732 operation = psa_aead_operation_init( );
3733
3734 status = psa_aead_encrypt_setup( &operation, key, alg );
3735
3736 /* If the operation is not supported, just skip and not fail in case the
3737 * encryption involves a common limitation of cryptography hardwares and
3738 * an alternative implementation. */
3739 if( status == PSA_ERROR_NOT_SUPPORTED )
3740 {
3741 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003742 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003743 }
3744
3745 PSA_ASSERT( status );
3746
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003747 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003748 nonce_length,
3749 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003750
Paul Elliott693bf312021-07-23 17:40:41 +01003751 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003752
Paul Elliottf1277632021-08-24 18:11:37 +01003753 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003754
Paul Elliottf1277632021-08-24 18:11:37 +01003755 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003756
Paul Elliott693bf312021-07-23 17:40:41 +01003757 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003758 {
3759
3760 /* Ensure we can still complete operation. */
3761
3762 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3763 additional_data->len ) );
3764
3765 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003766 output, output_size,
3767 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003768
Paul Elliottf1277632021-08-24 18:11:37 +01003769 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3770 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003771 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3772 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003773
3774exit:
3775 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003776 mbedtls_free( output );
3777 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003778 psa_aead_abort( &operation );
3779 PSA_DONE( );
3780}
3781/* END_CASE */
3782
3783/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003784void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3785 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003786 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003787 data_t *additional_data,
3788 data_t *input_data,
3789 int expected_status_arg )
3790{
3791
3792 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3793 psa_key_type_t key_type = key_type_arg;
3794 psa_algorithm_t alg = alg_arg;
3795 psa_aead_operation_t operation;
3796 uint8_t *nonce_buffer = NULL;
3797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3798 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3799 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003800 unsigned char *output = NULL;
3801 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003802 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003803 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003804 size_t ciphertext_size = 0;
3805 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003806 size_t tag_length = 0;
3807 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003808 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003809
3810 PSA_ASSERT( psa_crypto_init( ) );
3811
3812 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3813 psa_set_key_algorithm( &attributes, alg );
3814 psa_set_key_type( &attributes, key_type );
3815
3816 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3817 &key ) );
3818
3819 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3820
3821 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3822
Paul Elliott6f0e7202021-08-25 12:57:18 +01003823 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003824
Paul Elliott6f0e7202021-08-25 12:57:18 +01003825 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003826
Paul Elliott6f0e7202021-08-25 12:57:18 +01003827 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003828
Paul Elliott6f0e7202021-08-25 12:57:18 +01003829 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003830
3831 operation = psa_aead_operation_init( );
3832
3833 status = psa_aead_encrypt_setup( &operation, key, alg );
3834
3835 /* If the operation is not supported, just skip and not fail in case the
3836 * encryption involves a common limitation of cryptography hardwares and
3837 * an alternative implementation. */
3838 if( status == PSA_ERROR_NOT_SUPPORTED )
3839 {
3840 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003841 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003842 }
3843
3844 PSA_ASSERT( status );
3845
Paul Elliott4023ffd2021-09-10 16:21:22 +01003846 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3847 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003848 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003849 /* Arbitrary size buffer, to test zero length valid buffer. */
3850 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003851 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003852 }
3853 else
3854 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003855 /* If length is zero, then this will return NULL. */
3856 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003857 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003858
Paul Elliott4023ffd2021-09-10 16:21:22 +01003859 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003860 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003861 for( index = 0; index < nonce_length - 1; ++index )
3862 {
3863 nonce_buffer[index] = 'a' + index;
3864 }
Paul Elliott66696b52021-08-16 18:42:41 +01003865 }
Paul Elliott863864a2021-07-23 17:28:31 +01003866 }
3867
Paul Elliott6f0e7202021-08-25 12:57:18 +01003868 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003869
Paul Elliott693bf312021-07-23 17:40:41 +01003870 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003871
3872 if( expected_status == PSA_SUCCESS )
3873 {
3874 /* Ensure we can still complete operation. */
3875
3876 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3877 additional_data->len ) );
3878
3879 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003880 output, output_size,
3881 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003882
Paul Elliott6f0e7202021-08-25 12:57:18 +01003883 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3884 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003885 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3886 }
3887
3888exit:
3889 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003890 mbedtls_free( output );
3891 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003892 mbedtls_free( nonce_buffer );
3893 psa_aead_abort( &operation );
3894 PSA_DONE( );
3895}
3896/* END_CASE */
3897
3898/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003899void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3900 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003901 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003902 data_t *nonce,
3903 data_t *additional_data,
3904 data_t *input_data,
3905 int expected_status_arg )
3906{
3907
3908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3909 psa_key_type_t key_type = key_type_arg;
3910 psa_algorithm_t alg = alg_arg;
3911 psa_aead_operation_t operation;
3912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3913 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3914 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003915 unsigned char *output = NULL;
3916 unsigned char *ciphertext = NULL;
3917 size_t output_size = output_size_arg;
3918 size_t ciphertext_size = 0;
3919 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003920 size_t tag_length = 0;
3921 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3922
3923 PSA_ASSERT( psa_crypto_init( ) );
3924
3925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3926 psa_set_key_algorithm( &attributes, alg );
3927 psa_set_key_type( &attributes, key_type );
3928
3929 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3930 &key ) );
3931
3932 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3933
Paul Elliottc6d11d02021-09-01 12:04:23 +01003934 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003935
Paul Elliottc6d11d02021-09-01 12:04:23 +01003936 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003937
Paul Elliottc6d11d02021-09-01 12:04:23 +01003938 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003939
3940 operation = psa_aead_operation_init( );
3941
3942 status = psa_aead_encrypt_setup( &operation, key, alg );
3943
3944 /* If the operation is not supported, just skip and not fail in case the
3945 * encryption involves a common limitation of cryptography hardwares and
3946 * an alternative implementation. */
3947 if( status == PSA_ERROR_NOT_SUPPORTED )
3948 {
3949 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3950 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3951 }
3952
3953 PSA_ASSERT( status );
3954
3955 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3956
3957 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3958 additional_data->len ) );
3959
3960 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003961 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003962
3963 TEST_EQUAL( status, expected_status );
3964
3965 if( expected_status == PSA_SUCCESS )
3966 {
3967 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003968 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3969 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003970 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3971 }
3972
3973exit:
3974 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003975 mbedtls_free( output );
3976 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003977 psa_aead_abort( &operation );
3978 PSA_DONE( );
3979}
3980/* END_CASE */
3981
Paul Elliott91b021e2021-07-23 18:52:31 +01003982/* BEGIN_CASE */
3983void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3984 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003985 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003986 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003987 data_t *nonce,
3988 data_t *additional_data,
3989 data_t *input_data,
3990 int expected_status_arg )
3991{
3992
3993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3994 psa_key_type_t key_type = key_type_arg;
3995 psa_algorithm_t alg = alg_arg;
3996 psa_aead_operation_t operation;
3997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3998 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3999 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004000 unsigned char *ciphertext = NULL;
4001 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004002 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004003 size_t ciphertext_size = 0;
4004 size_t ciphertext_length = 0;
4005 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004006 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004007 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004008
4009 PSA_ASSERT( psa_crypto_init( ) );
4010
4011 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4012 psa_set_key_algorithm( &attributes, alg );
4013 psa_set_key_type( &attributes, key_type );
4014
4015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4016 &key ) );
4017
4018 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4019
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004020 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004021
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004022 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004023
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004024 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004025
Paul Elliott719c1322021-09-13 18:27:22 +01004026 ASSERT_ALLOC( tag_buffer, tag_size );
4027
Paul Elliott91b021e2021-07-23 18:52:31 +01004028 operation = psa_aead_operation_init( );
4029
4030 status = psa_aead_encrypt_setup( &operation, key, alg );
4031
4032 /* If the operation is not supported, just skip and not fail in case the
4033 * encryption involves a common limitation of cryptography hardwares and
4034 * an alternative implementation. */
4035 if( status == PSA_ERROR_NOT_SUPPORTED )
4036 {
4037 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4038 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4039 }
4040
4041 PSA_ASSERT( status );
4042
4043 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4044
4045 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4046 additional_data->len ) );
4047
4048 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004049 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004050
4051 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004052 status = psa_aead_finish( &operation, finish_ciphertext,
4053 finish_ciphertext_size,
4054 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004055 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004056
4057 TEST_EQUAL( status, expected_status );
4058
4059exit:
4060 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004061 mbedtls_free( ciphertext );
4062 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004063 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004064 psa_aead_abort( &operation );
4065 PSA_DONE( );
4066}
4067/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004068
4069/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004070void aead_multipart_verify( int key_type_arg, data_t *key_data,
4071 int alg_arg,
4072 data_t *nonce,
4073 data_t *additional_data,
4074 data_t *input_data,
4075 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004076 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004077 int expected_status_arg )
4078{
4079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4080 psa_key_type_t key_type = key_type_arg;
4081 psa_algorithm_t alg = alg_arg;
4082 psa_aead_operation_t operation;
4083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4084 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4085 psa_status_t expected_status = expected_status_arg;
4086 unsigned char *plaintext = NULL;
4087 unsigned char *finish_plaintext = NULL;
4088 size_t plaintext_size = 0;
4089 size_t plaintext_length = 0;
4090 size_t verify_plaintext_size = 0;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004091 tagusage_method tag_usage = tag_usage_arg;
4092 unsigned char *tag_buffer = NULL;
4093 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004094
4095 PSA_ASSERT( psa_crypto_init( ) );
4096
4097 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4098 psa_set_key_algorithm( &attributes, alg );
4099 psa_set_key_type( &attributes, key_type );
4100
4101 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4102 &key ) );
4103
4104 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4105
4106 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4107 input_data->len );
4108
4109 ASSERT_ALLOC( plaintext, plaintext_size );
4110
4111 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4112
4113 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4114
4115 operation = psa_aead_operation_init( );
4116
4117 status = psa_aead_decrypt_setup( &operation, key, alg );
4118
4119 /* If the operation is not supported, just skip and not fail in case the
4120 * encryption involves a common limitation of cryptography hardwares and
4121 * an alternative implementation. */
4122 if( status == PSA_ERROR_NOT_SUPPORTED )
4123 {
4124 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4125 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4126 }
4127
4128 PSA_ASSERT( status );
4129
4130 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4131
4132 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4133 additional_data->len ) );
4134
4135 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4136 input_data->len,
4137 plaintext, plaintext_size,
4138 &plaintext_length ) );
4139
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004140 if( tag_usage == USE_GIVEN_TAG )
4141 {
4142 tag_buffer = tag->x;
4143 tag_size = tag->len;
4144 }
4145
Paul Elliott9961a662021-09-17 19:19:02 +01004146 status = psa_aead_verify( &operation, finish_plaintext,
4147 verify_plaintext_size,
4148 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004149 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004150
4151 TEST_EQUAL( status, expected_status );
4152
4153exit:
4154 psa_destroy_key( key );
4155 mbedtls_free( plaintext );
4156 mbedtls_free( finish_plaintext );
4157 psa_aead_abort( &operation );
4158 PSA_DONE( );
4159}
4160/* END_CASE */
4161
Paul Elliott9961a662021-09-17 19:19:02 +01004162/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004163void aead_multipart_setup( int key_type_arg, data_t *key_data,
4164 int alg_arg, int expected_status_arg )
4165{
4166 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4167 psa_key_type_t key_type = key_type_arg;
4168 psa_algorithm_t alg = alg_arg;
4169 psa_aead_operation_t operation;
4170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4171 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4172 psa_status_t expected_status = expected_status_arg;
4173
4174 PSA_ASSERT( psa_crypto_init( ) );
4175
4176 psa_set_key_usage_flags( &attributes,
4177 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4178 psa_set_key_algorithm( &attributes, alg );
4179 psa_set_key_type( &attributes, key_type );
4180
4181 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4182 &key ) );
4183
Paul Elliott64555bd2021-09-20 16:44:44 +01004184 operation = psa_aead_operation_init( );
4185
Paul Elliott5221ef62021-09-19 17:33:03 +01004186 mbedtls_test_set_step( 0 );
4187
4188 status = psa_aead_encrypt_setup( &operation, key, alg );
4189
4190 TEST_EQUAL( status, expected_status );
4191
4192 psa_aead_abort( &operation );
4193
4194 operation = psa_aead_operation_init( );
4195
4196 mbedtls_test_set_step( 1 );
4197
4198 status = psa_aead_decrypt_setup( &operation, key, alg );
4199
4200 TEST_EQUAL(status, expected_status );
4201
4202exit:
4203 psa_destroy_key( key );
4204 psa_aead_abort( &operation );
4205 PSA_DONE( );
4206}
4207/* END_CASE */
4208
4209/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004210void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4211 int alg_arg,
4212 data_t *nonce,
4213 data_t *additional_data,
4214 data_t *input_data )
4215{
4216 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4217 psa_key_type_t key_type = key_type_arg;
4218 psa_algorithm_t alg = alg_arg;
4219 psa_aead_operation_t operation;
4220 unsigned char *output_data = NULL;
4221 unsigned char *final_data = NULL;
4222 size_t output_size = 0;
4223 size_t finish_output_size = 0;
4224 size_t output_length = 0;
4225 size_t key_bits = 0;
4226 size_t tag_length = 0;
4227 size_t tag_size = 0;
4228 size_t nonce_length = 0;
4229 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4230 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4231 size_t output_part_length = 0;
4232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4233
4234 PSA_ASSERT( psa_crypto_init( ) );
4235
4236 psa_set_key_usage_flags( & attributes,
4237 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4238 psa_set_key_algorithm( & attributes, alg );
4239 psa_set_key_type( & attributes, key_type );
4240
4241 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4242 &key ) );
4243
4244 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4245 key_bits = psa_get_key_bits( &attributes );
4246
4247 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4248
4249 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4250
4251 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4252
4253 ASSERT_ALLOC( output_data, output_size );
4254
4255 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4256
4257 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4258
4259 ASSERT_ALLOC( final_data, finish_output_size );
4260
4261 /* Test all operations error without calling setup first. */
4262
4263 operation = psa_aead_operation_init( );
4264
4265 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4266 PSA_ERROR_BAD_STATE );
4267
4268 psa_aead_abort( &operation );
4269
4270 operation = psa_aead_operation_init( );
4271
4272 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4273 PSA_AEAD_NONCE_MAX_SIZE,
4274 &nonce_length ),
4275 PSA_ERROR_BAD_STATE );
4276
4277 psa_aead_abort( &operation );
4278
Paul Elliott481be342021-07-16 17:38:47 +01004279 /* ------------------------------------------------------- */
4280
Paul Elliottc23a9a02021-06-21 18:32:46 +01004281 operation = psa_aead_operation_init( );
4282
4283 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4284 input_data->len ),
4285 PSA_ERROR_BAD_STATE );
4286
4287 psa_aead_abort( &operation );
4288
Paul Elliott481be342021-07-16 17:38:47 +01004289 /* ------------------------------------------------------- */
4290
Paul Elliottc23a9a02021-06-21 18:32:46 +01004291 operation = psa_aead_operation_init( );
4292
4293 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4294 additional_data->len ),
4295 PSA_ERROR_BAD_STATE );
4296
4297 psa_aead_abort( &operation );
4298
Paul Elliott481be342021-07-16 17:38:47 +01004299 /* ------------------------------------------------------- */
4300
Paul Elliottc23a9a02021-06-21 18:32:46 +01004301 operation = psa_aead_operation_init( );
4302
4303 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4304 input_data->len, output_data,
4305 output_size, &output_length ),
4306 PSA_ERROR_BAD_STATE );
4307
4308 psa_aead_abort( &operation );
4309
Paul Elliott481be342021-07-16 17:38:47 +01004310 /* ------------------------------------------------------- */
4311
Paul Elliottc23a9a02021-06-21 18:32:46 +01004312 operation = psa_aead_operation_init( );
4313
4314 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4315 finish_output_size,
4316 &output_part_length,
4317 tag_buffer, tag_length,
4318 &tag_size ),
4319 PSA_ERROR_BAD_STATE );
4320
4321 psa_aead_abort( &operation );
4322
Paul Elliott481be342021-07-16 17:38:47 +01004323 /* ------------------------------------------------------- */
4324
Paul Elliottc23a9a02021-06-21 18:32:46 +01004325 operation = psa_aead_operation_init( );
4326
4327 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4328 finish_output_size,
4329 &output_part_length,
4330 tag_buffer,
4331 tag_length ),
4332 PSA_ERROR_BAD_STATE );
4333
4334 psa_aead_abort( &operation );
4335
4336 /* Test for double setups. */
4337
4338 operation = psa_aead_operation_init( );
4339
4340 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4341
4342 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4343 PSA_ERROR_BAD_STATE );
4344
4345 psa_aead_abort( &operation );
4346
Paul Elliott481be342021-07-16 17:38:47 +01004347 /* ------------------------------------------------------- */
4348
Paul Elliottc23a9a02021-06-21 18:32:46 +01004349 operation = psa_aead_operation_init( );
4350
4351 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4352
4353 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4354 PSA_ERROR_BAD_STATE );
4355
4356 psa_aead_abort( &operation );
4357
Paul Elliott374a2be2021-07-16 17:53:40 +01004358 /* ------------------------------------------------------- */
4359
4360 operation = psa_aead_operation_init( );
4361
4362 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4363
4364 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4365 PSA_ERROR_BAD_STATE );
4366
4367 psa_aead_abort( &operation );
4368
4369 /* ------------------------------------------------------- */
4370
4371 operation = psa_aead_operation_init( );
4372
4373 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4374
4375 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4376 PSA_ERROR_BAD_STATE );
4377
4378 psa_aead_abort( &operation );
4379
Paul Elliottc23a9a02021-06-21 18:32:46 +01004380 /* Test for not setting a nonce. */
4381
4382 operation = psa_aead_operation_init( );
4383
4384 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4385
4386 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4387 additional_data->len ),
4388 PSA_ERROR_BAD_STATE );
4389
4390 psa_aead_abort( &operation );
4391
Paul Elliott7f628422021-09-01 12:08:29 +01004392 /* ------------------------------------------------------- */
4393
4394 operation = psa_aead_operation_init( );
4395
4396 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4397
4398 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4399 input_data->len, output_data,
4400 output_size, &output_length ),
4401 PSA_ERROR_BAD_STATE );
4402
4403 psa_aead_abort( &operation );
4404
Paul Elliottc23a9a02021-06-21 18:32:46 +01004405 /* Test for double setting nonce. */
4406
4407 operation = psa_aead_operation_init( );
4408
4409 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4410
4411 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4412
4413 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4414 PSA_ERROR_BAD_STATE );
4415
4416 psa_aead_abort( &operation );
4417
Paul Elliott374a2be2021-07-16 17:53:40 +01004418 /* Test for double generating nonce. */
4419
4420 operation = psa_aead_operation_init( );
4421
4422 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4423
4424 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4425 PSA_AEAD_NONCE_MAX_SIZE,
4426 &nonce_length ) );
4427
4428 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4429 PSA_AEAD_NONCE_MAX_SIZE,
4430 &nonce_length ),
4431 PSA_ERROR_BAD_STATE );
4432
4433
4434 psa_aead_abort( &operation );
4435
4436 /* Test for generate nonce then set and vice versa */
4437
4438 operation = psa_aead_operation_init( );
4439
4440 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4441
4442 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4443 PSA_AEAD_NONCE_MAX_SIZE,
4444 &nonce_length ) );
4445
4446 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4447 PSA_ERROR_BAD_STATE );
4448
4449 psa_aead_abort( &operation );
4450
4451 /* ------------------------------------------------------- */
4452
4453 operation = psa_aead_operation_init( );
4454
4455 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4456
4457 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4458
4459 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4460 PSA_AEAD_NONCE_MAX_SIZE,
4461 &nonce_length ),
4462 PSA_ERROR_BAD_STATE );
4463
4464 psa_aead_abort( &operation );
4465
Paul Elliott7220cae2021-06-22 17:25:57 +01004466 /* Test for generating nonce in decrypt setup. */
4467
4468 operation = psa_aead_operation_init( );
4469
4470 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4471
4472 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4473 PSA_AEAD_NONCE_MAX_SIZE,
4474 &nonce_length ),
4475 PSA_ERROR_BAD_STATE );
4476
4477 psa_aead_abort( &operation );
4478
Paul Elliottc23a9a02021-06-21 18:32:46 +01004479 /* Test for setting lengths twice. */
4480
4481 operation = psa_aead_operation_init( );
4482
4483 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4484
4485 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4486
4487 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4488 input_data->len ) );
4489
4490 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4491 input_data->len ),
4492 PSA_ERROR_BAD_STATE );
4493
4494 psa_aead_abort( &operation );
4495
4496 /* Test for setting lengths after already starting data. */
4497
4498 operation = psa_aead_operation_init( );
4499
4500 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4501
4502 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4503
Paul Elliottf94bd992021-09-19 18:15:59 +01004504 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4505 additional_data->len ) );
4506
4507 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4508 input_data->len ),
4509 PSA_ERROR_BAD_STATE );
4510
4511 psa_aead_abort( &operation );
4512
4513 /* ------------------------------------------------------- */
4514
4515 operation = psa_aead_operation_init( );
4516
4517 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4518
4519 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4520
Paul Elliottc23a9a02021-06-21 18:32:46 +01004521 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4522 input_data->len, output_data,
4523 output_size, &output_length ) );
4524
4525 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4526 input_data->len ),
4527 PSA_ERROR_BAD_STATE );
4528
4529 psa_aead_abort( &operation );
4530
Paul Elliott243080c2021-07-21 19:01:17 +01004531 /* Test for not sending any additional data or data after setting non zero
4532 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004533
4534 operation = psa_aead_operation_init( );
4535
4536 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4537
4538 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4539
4540 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4541 input_data->len ) );
4542
4543 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4544 finish_output_size,
4545 &output_part_length,
4546 tag_buffer, tag_length,
4547 &tag_size ),
4548 PSA_ERROR_INVALID_ARGUMENT );
4549
4550 psa_aead_abort( &operation );
4551
Paul Elliott243080c2021-07-21 19:01:17 +01004552 /* Test for not sending any additional data or data after setting non-zero
4553 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004554
4555 operation = psa_aead_operation_init( );
4556
4557 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4558
4559 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4560
4561 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4562 input_data->len ) );
4563
4564 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4565 finish_output_size,
4566 &output_part_length,
4567 tag_buffer,
4568 tag_length ),
4569 PSA_ERROR_INVALID_ARGUMENT );
4570
4571 psa_aead_abort( &operation );
4572
Paul Elliott243080c2021-07-21 19:01:17 +01004573 /* Test for not sending any additional data after setting a non-zero length
4574 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004575
4576 operation = psa_aead_operation_init( );
4577
4578 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4579
4580 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4581
4582 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4583 input_data->len ) );
4584
4585 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4586 input_data->len, output_data,
4587 output_size, &output_length ),
4588 PSA_ERROR_INVALID_ARGUMENT );
4589
4590 psa_aead_abort( &operation );
4591
Paul Elliottf94bd992021-09-19 18:15:59 +01004592 /* Test for not sending any data after setting a non-zero length for it.*/
4593
4594 operation = psa_aead_operation_init( );
4595
4596 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4597
4598 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4599
4600 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4601 input_data->len ) );
4602
4603 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4604 additional_data->len ) );
4605
4606 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4607 finish_output_size,
4608 &output_part_length,
4609 tag_buffer, tag_length,
4610 &tag_size ),
4611 PSA_ERROR_INVALID_ARGUMENT );
4612
4613 psa_aead_abort( &operation );
4614
Paul Elliottb0450fe2021-09-01 15:06:26 +01004615 /* Test for sending too much additional data after setting lengths. */
4616
4617 operation = psa_aead_operation_init( );
4618
4619 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4620
4621 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4622
4623 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4624
4625
4626 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4627 additional_data->len ),
4628 PSA_ERROR_INVALID_ARGUMENT );
4629
4630 psa_aead_abort( &operation );
4631
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004632 operation = psa_aead_operation_init( );
4633
4634 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4635
4636 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4637
4638 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4639 input_data->len ) );
4640
4641 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4642 additional_data->len ) );
4643
4644 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4645 1 ),
4646 PSA_ERROR_INVALID_ARGUMENT );
4647
4648 psa_aead_abort( &operation );
4649
Paul Elliottb0450fe2021-09-01 15:06:26 +01004650 /* Test for sending too much data after setting lengths. */
4651
4652 operation = psa_aead_operation_init( );
4653
4654 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4655
4656 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4657
4658 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4659
4660 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4661 input_data->len, output_data,
4662 output_size, &output_length ),
4663 PSA_ERROR_INVALID_ARGUMENT );
4664
4665 psa_aead_abort( &operation );
4666
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004667 operation = psa_aead_operation_init( );
4668
4669 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4670
4671 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4672
4673 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4674 input_data->len ) );
4675
4676 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4677 additional_data->len ) );
4678
4679 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4680 input_data->len, output_data,
4681 output_size, &output_length ) );
4682
4683 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4684 1, output_data,
4685 output_size, &output_length ),
4686 PSA_ERROR_INVALID_ARGUMENT );
4687
4688 psa_aead_abort( &operation );
4689
Paul Elliottc23a9a02021-06-21 18:32:46 +01004690 /* Test sending additional data after data. */
4691
4692 operation = psa_aead_operation_init( );
4693
4694 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4695
4696 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4697
4698 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4699 input_data->len, output_data,
4700 output_size, &output_length ) );
4701
4702 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4703 additional_data->len ),
4704 PSA_ERROR_BAD_STATE );
4705
4706 psa_aead_abort( &operation );
4707
Paul Elliott534d0b42021-06-22 19:15:20 +01004708 /* Test calling finish on decryption. */
4709
4710 operation = psa_aead_operation_init( );
4711
4712 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4713
4714 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4715
4716 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4717 finish_output_size,
4718 &output_part_length,
4719 tag_buffer, tag_length,
4720 &tag_size ),
4721 PSA_ERROR_BAD_STATE );
4722
4723 psa_aead_abort( &operation );
4724
4725 /* Test calling verify on encryption. */
4726
4727 operation = psa_aead_operation_init( );
4728
4729 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4730
4731 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4732
4733 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4734 finish_output_size,
4735 &output_part_length,
4736 tag_buffer,
4737 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004738 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004739
4740 psa_aead_abort( &operation );
4741
4742
Paul Elliottc23a9a02021-06-21 18:32:46 +01004743exit:
4744 psa_destroy_key( key );
4745 psa_aead_abort( &operation );
4746 mbedtls_free( output_data );
4747 mbedtls_free( final_data );
4748 PSA_DONE( );
4749}
4750/* END_CASE */
4751
4752/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004753void signature_size( int type_arg,
4754 int bits,
4755 int alg_arg,
4756 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004757{
4758 psa_key_type_t type = type_arg;
4759 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004760 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004761
Gilles Peskinefe11b722018-12-18 00:24:04 +01004762 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004763
Gilles Peskinee59236f2018-01-27 23:32:46 +01004764exit:
4765 ;
4766}
4767/* END_CASE */
4768
4769/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004770void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4771 int alg_arg, data_t *input_data,
4772 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004773{
Ronald Cron5425a212020-08-04 14:58:35 +02004774 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004775 psa_key_type_t key_type = key_type_arg;
4776 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004777 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004778 unsigned char *signature = NULL;
4779 size_t signature_size;
4780 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004781 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004782
Gilles Peskine8817f612018-12-18 00:18:46 +01004783 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004784
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004785 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004786 psa_set_key_algorithm( &attributes, alg );
4787 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004788
Gilles Peskine049c7532019-05-15 20:22:09 +02004789 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004790 &key ) );
4791 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004792 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004793
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004794 /* Allocate a buffer which has the size advertized by the
4795 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004796 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004797 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004798 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004799 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004800 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004801
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004802 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004803 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004804 input_data->x, input_data->len,
4805 signature, signature_size,
4806 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004807 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004808 ASSERT_COMPARE( output_data->x, output_data->len,
4809 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004810
4811exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004812 /*
4813 * Key attributes may have been returned by psa_get_key_attributes()
4814 * thus reset them as required.
4815 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004816 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004817
Ronald Cron5425a212020-08-04 14:58:35 +02004818 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004819 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004820 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004821}
4822/* END_CASE */
4823
4824/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004825void sign_hash_fail( int key_type_arg, data_t *key_data,
4826 int alg_arg, data_t *input_data,
4827 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004828{
Ronald Cron5425a212020-08-04 14:58:35 +02004829 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004830 psa_key_type_t key_type = key_type_arg;
4831 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004832 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004833 psa_status_t actual_status;
4834 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004835 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004836 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004838
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004839 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004840
Gilles Peskine8817f612018-12-18 00:18:46 +01004841 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004842
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004844 psa_set_key_algorithm( &attributes, alg );
4845 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004846
Gilles Peskine049c7532019-05-15 20:22:09 +02004847 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004848 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004849
Ronald Cron5425a212020-08-04 14:58:35 +02004850 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004851 input_data->x, input_data->len,
4852 signature, signature_size,
4853 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004854 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004855 /* The value of *signature_length is unspecified on error, but
4856 * whatever it is, it should be less than signature_size, so that
4857 * if the caller tries to read *signature_length bytes without
4858 * checking the error code then they don't overflow a buffer. */
4859 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004860
4861exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004862 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004863 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004864 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004865 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004866}
4867/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004868
4869/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004870void sign_verify_hash( int key_type_arg, data_t *key_data,
4871 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004872{
Ronald Cron5425a212020-08-04 14:58:35 +02004873 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004874 psa_key_type_t key_type = key_type_arg;
4875 psa_algorithm_t alg = alg_arg;
4876 size_t key_bits;
4877 unsigned char *signature = NULL;
4878 size_t signature_size;
4879 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004881
Gilles Peskine8817f612018-12-18 00:18:46 +01004882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004883
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004884 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004885 psa_set_key_algorithm( &attributes, alg );
4886 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004887
Gilles Peskine049c7532019-05-15 20:22:09 +02004888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004889 &key ) );
4890 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004891 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004892
4893 /* Allocate a buffer which has the size advertized by the
4894 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004895 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004896 key_bits, alg );
4897 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004898 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004899 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004900
4901 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004902 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004903 input_data->x, input_data->len,
4904 signature, signature_size,
4905 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004906 /* Check that the signature length looks sensible. */
4907 TEST_ASSERT( signature_length <= signature_size );
4908 TEST_ASSERT( signature_length > 0 );
4909
4910 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004911 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004912 input_data->x, input_data->len,
4913 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004914
4915 if( input_data->len != 0 )
4916 {
4917 /* Flip a bit in the input and verify that the signature is now
4918 * detected as invalid. Flip a bit at the beginning, not at the end,
4919 * because ECDSA may ignore the last few bits of the input. */
4920 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004921 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004922 input_data->x, input_data->len,
4923 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004924 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004925 }
4926
4927exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004928 /*
4929 * Key attributes may have been returned by psa_get_key_attributes()
4930 * thus reset them as required.
4931 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004932 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004933
Ronald Cron5425a212020-08-04 14:58:35 +02004934 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004935 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004936 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004937}
4938/* END_CASE */
4939
4940/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004941void verify_hash( int key_type_arg, data_t *key_data,
4942 int alg_arg, data_t *hash_data,
4943 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004944{
Ronald Cron5425a212020-08-04 14:58:35 +02004945 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004946 psa_key_type_t key_type = key_type_arg;
4947 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004949
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004950 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004951
Gilles Peskine8817f612018-12-18 00:18:46 +01004952 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004953
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004954 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004955 psa_set_key_algorithm( &attributes, alg );
4956 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004957
Gilles Peskine049c7532019-05-15 20:22:09 +02004958 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004959 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004960
Ronald Cron5425a212020-08-04 14:58:35 +02004961 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004962 hash_data->x, hash_data->len,
4963 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004964
itayzafrir5c753392018-05-08 11:18:38 +03004965exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004966 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004967 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004968 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004969}
4970/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004971
4972/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004973void verify_hash_fail( int key_type_arg, data_t *key_data,
4974 int alg_arg, data_t *hash_data,
4975 data_t *signature_data,
4976 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004977{
Ronald Cron5425a212020-08-04 14:58:35 +02004978 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004979 psa_key_type_t key_type = key_type_arg;
4980 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004981 psa_status_t actual_status;
4982 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004983 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004984
Gilles Peskine8817f612018-12-18 00:18:46 +01004985 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004986
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004987 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004988 psa_set_key_algorithm( &attributes, alg );
4989 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004990
Gilles Peskine049c7532019-05-15 20:22:09 +02004991 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004992 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004993
Ronald Cron5425a212020-08-04 14:58:35 +02004994 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004995 hash_data->x, hash_data->len,
4996 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004997 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004998
4999exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005000 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005001 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005002 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005003}
5004/* END_CASE */
5005
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005006/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005007void sign_message_deterministic( int key_type_arg,
5008 data_t *key_data,
5009 int alg_arg,
5010 data_t *input_data,
5011 data_t *output_data )
5012{
5013 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5014 psa_key_type_t key_type = key_type_arg;
5015 psa_algorithm_t alg = alg_arg;
5016 size_t key_bits;
5017 unsigned char *signature = NULL;
5018 size_t signature_size;
5019 size_t signature_length = 0xdeadbeef;
5020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5021
5022 PSA_ASSERT( psa_crypto_init( ) );
5023
5024 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5025 psa_set_key_algorithm( &attributes, alg );
5026 psa_set_key_type( &attributes, key_type );
5027
5028 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5029 &key ) );
5030 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5031 key_bits = psa_get_key_bits( &attributes );
5032
5033 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5034 TEST_ASSERT( signature_size != 0 );
5035 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5036 ASSERT_ALLOC( signature, signature_size );
5037
5038 PSA_ASSERT( psa_sign_message( key, alg,
5039 input_data->x, input_data->len,
5040 signature, signature_size,
5041 &signature_length ) );
5042
5043 ASSERT_COMPARE( output_data->x, output_data->len,
5044 signature, signature_length );
5045
5046exit:
5047 psa_reset_key_attributes( &attributes );
5048
5049 psa_destroy_key( key );
5050 mbedtls_free( signature );
5051 PSA_DONE( );
5052
5053}
5054/* END_CASE */
5055
5056/* BEGIN_CASE */
5057void sign_message_fail( int key_type_arg,
5058 data_t *key_data,
5059 int alg_arg,
5060 data_t *input_data,
5061 int signature_size_arg,
5062 int expected_status_arg )
5063{
5064 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5065 psa_key_type_t key_type = key_type_arg;
5066 psa_algorithm_t alg = alg_arg;
5067 size_t signature_size = signature_size_arg;
5068 psa_status_t actual_status;
5069 psa_status_t expected_status = expected_status_arg;
5070 unsigned char *signature = NULL;
5071 size_t signature_length = 0xdeadbeef;
5072 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5073
5074 ASSERT_ALLOC( signature, signature_size );
5075
5076 PSA_ASSERT( psa_crypto_init( ) );
5077
5078 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5079 psa_set_key_algorithm( &attributes, alg );
5080 psa_set_key_type( &attributes, key_type );
5081
5082 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5083 &key ) );
5084
5085 actual_status = psa_sign_message( key, alg,
5086 input_data->x, input_data->len,
5087 signature, signature_size,
5088 &signature_length );
5089 TEST_EQUAL( actual_status, expected_status );
5090 /* The value of *signature_length is unspecified on error, but
5091 * whatever it is, it should be less than signature_size, so that
5092 * if the caller tries to read *signature_length bytes without
5093 * checking the error code then they don't overflow a buffer. */
5094 TEST_ASSERT( signature_length <= signature_size );
5095
5096exit:
5097 psa_reset_key_attributes( &attributes );
5098 psa_destroy_key( key );
5099 mbedtls_free( signature );
5100 PSA_DONE( );
5101}
5102/* END_CASE */
5103
5104/* BEGIN_CASE */
5105void sign_verify_message( int key_type_arg,
5106 data_t *key_data,
5107 int alg_arg,
5108 data_t *input_data )
5109{
5110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5111 psa_key_type_t key_type = key_type_arg;
5112 psa_algorithm_t alg = alg_arg;
5113 size_t key_bits;
5114 unsigned char *signature = NULL;
5115 size_t signature_size;
5116 size_t signature_length = 0xdeadbeef;
5117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5118
5119 PSA_ASSERT( psa_crypto_init( ) );
5120
5121 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5122 PSA_KEY_USAGE_VERIFY_MESSAGE );
5123 psa_set_key_algorithm( &attributes, alg );
5124 psa_set_key_type( &attributes, key_type );
5125
5126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5127 &key ) );
5128 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5129 key_bits = psa_get_key_bits( &attributes );
5130
5131 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5132 TEST_ASSERT( signature_size != 0 );
5133 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5134 ASSERT_ALLOC( signature, signature_size );
5135
5136 PSA_ASSERT( psa_sign_message( key, alg,
5137 input_data->x, input_data->len,
5138 signature, signature_size,
5139 &signature_length ) );
5140 TEST_ASSERT( signature_length <= signature_size );
5141 TEST_ASSERT( signature_length > 0 );
5142
5143 PSA_ASSERT( psa_verify_message( key, alg,
5144 input_data->x, input_data->len,
5145 signature, signature_length ) );
5146
5147 if( input_data->len != 0 )
5148 {
5149 /* Flip a bit in the input and verify that the signature is now
5150 * detected as invalid. Flip a bit at the beginning, not at the end,
5151 * because ECDSA may ignore the last few bits of the input. */
5152 input_data->x[0] ^= 1;
5153 TEST_EQUAL( psa_verify_message( key, alg,
5154 input_data->x, input_data->len,
5155 signature, signature_length ),
5156 PSA_ERROR_INVALID_SIGNATURE );
5157 }
5158
5159exit:
5160 psa_reset_key_attributes( &attributes );
5161
5162 psa_destroy_key( key );
5163 mbedtls_free( signature );
5164 PSA_DONE( );
5165}
5166/* END_CASE */
5167
5168/* BEGIN_CASE */
5169void verify_message( int key_type_arg,
5170 data_t *key_data,
5171 int alg_arg,
5172 data_t *input_data,
5173 data_t *signature_data )
5174{
5175 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5176 psa_key_type_t key_type = key_type_arg;
5177 psa_algorithm_t alg = alg_arg;
5178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5179
5180 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5181
5182 PSA_ASSERT( psa_crypto_init( ) );
5183
5184 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5185 psa_set_key_algorithm( &attributes, alg );
5186 psa_set_key_type( &attributes, key_type );
5187
5188 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5189 &key ) );
5190
5191 PSA_ASSERT( psa_verify_message( key, alg,
5192 input_data->x, input_data->len,
5193 signature_data->x, signature_data->len ) );
5194
5195exit:
5196 psa_reset_key_attributes( &attributes );
5197 psa_destroy_key( key );
5198 PSA_DONE( );
5199}
5200/* END_CASE */
5201
5202/* BEGIN_CASE */
5203void verify_message_fail( int key_type_arg,
5204 data_t *key_data,
5205 int alg_arg,
5206 data_t *hash_data,
5207 data_t *signature_data,
5208 int expected_status_arg )
5209{
5210 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5211 psa_key_type_t key_type = key_type_arg;
5212 psa_algorithm_t alg = alg_arg;
5213 psa_status_t actual_status;
5214 psa_status_t expected_status = expected_status_arg;
5215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5216
5217 PSA_ASSERT( psa_crypto_init( ) );
5218
5219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5220 psa_set_key_algorithm( &attributes, alg );
5221 psa_set_key_type( &attributes, key_type );
5222
5223 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5224 &key ) );
5225
5226 actual_status = psa_verify_message( key, alg,
5227 hash_data->x, hash_data->len,
5228 signature_data->x,
5229 signature_data->len );
5230 TEST_EQUAL( actual_status, expected_status );
5231
5232exit:
5233 psa_reset_key_attributes( &attributes );
5234 psa_destroy_key( key );
5235 PSA_DONE( );
5236}
5237/* END_CASE */
5238
5239/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005240void asymmetric_encrypt( int key_type_arg,
5241 data_t *key_data,
5242 int alg_arg,
5243 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005244 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005245 int expected_output_length_arg,
5246 int expected_status_arg )
5247{
Ronald Cron5425a212020-08-04 14:58:35 +02005248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005249 psa_key_type_t key_type = key_type_arg;
5250 psa_algorithm_t alg = alg_arg;
5251 size_t expected_output_length = expected_output_length_arg;
5252 size_t key_bits;
5253 unsigned char *output = NULL;
5254 size_t output_size;
5255 size_t output_length = ~0;
5256 psa_status_t actual_status;
5257 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005259
Gilles Peskine8817f612018-12-18 00:18:46 +01005260 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005261
Gilles Peskine656896e2018-06-29 19:12:28 +02005262 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005263 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5264 psa_set_key_algorithm( &attributes, alg );
5265 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005266 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005267 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005268
5269 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005270 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005271 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005272
Gilles Peskine656896e2018-06-29 19:12:28 +02005273 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005274 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005275 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005276
5277 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005278 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005279 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005280 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005281 output, output_size,
5282 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005283 TEST_EQUAL( actual_status, expected_status );
5284 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005285
Gilles Peskine68428122018-06-30 18:42:41 +02005286 /* If the label is empty, the test framework puts a non-null pointer
5287 * in label->x. Test that a null pointer works as well. */
5288 if( label->len == 0 )
5289 {
5290 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005291 if( output_size != 0 )
5292 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005293 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005294 input_data->x, input_data->len,
5295 NULL, label->len,
5296 output, output_size,
5297 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005298 TEST_EQUAL( actual_status, expected_status );
5299 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005300 }
5301
Gilles Peskine656896e2018-06-29 19:12:28 +02005302exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005303 /*
5304 * Key attributes may have been returned by psa_get_key_attributes()
5305 * thus reset them as required.
5306 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005307 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005308
Ronald Cron5425a212020-08-04 14:58:35 +02005309 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005310 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005311 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005312}
5313/* END_CASE */
5314
5315/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005316void asymmetric_encrypt_decrypt( int key_type_arg,
5317 data_t *key_data,
5318 int alg_arg,
5319 data_t *input_data,
5320 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005321{
Ronald Cron5425a212020-08-04 14:58:35 +02005322 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005323 psa_key_type_t key_type = key_type_arg;
5324 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005325 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005326 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005327 size_t output_size;
5328 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005329 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005330 size_t output2_size;
5331 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005332 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005333
Gilles Peskine8817f612018-12-18 00:18:46 +01005334 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005335
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005336 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5337 psa_set_key_algorithm( &attributes, alg );
5338 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005339
Gilles Peskine049c7532019-05-15 20:22:09 +02005340 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005341 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005342
5343 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005344 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005345 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005346
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005347 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005348 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005349 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005350
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005351 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005352 TEST_ASSERT( output2_size <=
5353 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5354 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005355 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005356
Gilles Peskineeebd7382018-06-08 18:11:54 +02005357 /* We test encryption by checking that encrypt-then-decrypt gives back
5358 * the original plaintext because of the non-optional random
5359 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005360 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005361 input_data->x, input_data->len,
5362 label->x, label->len,
5363 output, output_size,
5364 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005365 /* We don't know what ciphertext length to expect, but check that
5366 * it looks sensible. */
5367 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005368
Ronald Cron5425a212020-08-04 14:58:35 +02005369 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005370 output, output_length,
5371 label->x, label->len,
5372 output2, output2_size,
5373 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005374 ASSERT_COMPARE( input_data->x, input_data->len,
5375 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005376
5377exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005378 /*
5379 * Key attributes may have been returned by psa_get_key_attributes()
5380 * thus reset them as required.
5381 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005382 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005383
Ronald Cron5425a212020-08-04 14:58:35 +02005384 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005385 mbedtls_free( output );
5386 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005387 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005388}
5389/* END_CASE */
5390
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005391/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005392void asymmetric_decrypt( int key_type_arg,
5393 data_t *key_data,
5394 int alg_arg,
5395 data_t *input_data,
5396 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005397 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005398{
Ronald Cron5425a212020-08-04 14:58:35 +02005399 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005400 psa_key_type_t key_type = key_type_arg;
5401 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005402 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005403 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005404 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005405 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005407
Gilles Peskine8817f612018-12-18 00:18:46 +01005408 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005409
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005410 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5411 psa_set_key_algorithm( &attributes, alg );
5412 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005413
Gilles Peskine049c7532019-05-15 20:22:09 +02005414 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005415 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005416
gabor-mezei-armceface22021-01-21 12:26:17 +01005417 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5418 key_bits = psa_get_key_bits( &attributes );
5419
5420 /* Determine the maximum ciphertext length */
5421 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5422 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5423 ASSERT_ALLOC( output, output_size );
5424
Ronald Cron5425a212020-08-04 14:58:35 +02005425 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005426 input_data->x, input_data->len,
5427 label->x, label->len,
5428 output,
5429 output_size,
5430 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005431 ASSERT_COMPARE( expected_data->x, expected_data->len,
5432 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005433
Gilles Peskine68428122018-06-30 18:42:41 +02005434 /* If the label is empty, the test framework puts a non-null pointer
5435 * in label->x. Test that a null pointer works as well. */
5436 if( label->len == 0 )
5437 {
5438 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005439 if( output_size != 0 )
5440 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005441 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005442 input_data->x, input_data->len,
5443 NULL, label->len,
5444 output,
5445 output_size,
5446 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005447 ASSERT_COMPARE( expected_data->x, expected_data->len,
5448 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005449 }
5450
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005451exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005452 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005453 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005454 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005455 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005456}
5457/* END_CASE */
5458
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005459/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005460void asymmetric_decrypt_fail( int key_type_arg,
5461 data_t *key_data,
5462 int alg_arg,
5463 data_t *input_data,
5464 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005465 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005466 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005467{
Ronald Cron5425a212020-08-04 14:58:35 +02005468 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005469 psa_key_type_t key_type = key_type_arg;
5470 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005471 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005472 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005473 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005474 psa_status_t actual_status;
5475 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005477
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005478 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005479
Gilles Peskine8817f612018-12-18 00:18:46 +01005480 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005481
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005482 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5483 psa_set_key_algorithm( &attributes, alg );
5484 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005485
Gilles Peskine049c7532019-05-15 20:22:09 +02005486 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005487 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005488
Ronald Cron5425a212020-08-04 14:58:35 +02005489 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005490 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005491 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005492 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005493 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005494 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005495 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005496
Gilles Peskine68428122018-06-30 18:42:41 +02005497 /* If the label is empty, the test framework puts a non-null pointer
5498 * in label->x. Test that a null pointer works as well. */
5499 if( label->len == 0 )
5500 {
5501 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005502 if( output_size != 0 )
5503 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005504 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005505 input_data->x, input_data->len,
5506 NULL, label->len,
5507 output, output_size,
5508 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005509 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005510 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005511 }
5512
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005513exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005514 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005515 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005516 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005517 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005518}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005519/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005520
5521/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005522void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005523{
5524 /* Test each valid way of initializing the object, except for `= {0}`, as
5525 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5526 * though it's OK by the C standard. We could test for this, but we'd need
5527 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005528 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005529 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5530 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5531 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005532
5533 memset( &zero, 0, sizeof( zero ) );
5534
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005535 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005536 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005537 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005538 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005539 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005540 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005541 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005542
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005543 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005544 PSA_ASSERT( psa_key_derivation_abort(&func) );
5545 PSA_ASSERT( psa_key_derivation_abort(&init) );
5546 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005547}
5548/* END_CASE */
5549
Janos Follath16de4a42019-06-13 16:32:24 +01005550/* BEGIN_CASE */
5551void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005552{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005553 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005554 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005556
Gilles Peskine8817f612018-12-18 00:18:46 +01005557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005558
Janos Follath16de4a42019-06-13 16:32:24 +01005559 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005560 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005561
5562exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005563 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005564 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005565}
5566/* END_CASE */
5567
Janos Follathaf3c2a02019-06-12 12:34:34 +01005568/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005569void derive_set_capacity( int alg_arg, int capacity_arg,
5570 int expected_status_arg )
5571{
5572 psa_algorithm_t alg = alg_arg;
5573 size_t capacity = capacity_arg;
5574 psa_status_t expected_status = expected_status_arg;
5575 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5576
5577 PSA_ASSERT( psa_crypto_init( ) );
5578
5579 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5580
5581 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5582 expected_status );
5583
5584exit:
5585 psa_key_derivation_abort( &operation );
5586 PSA_DONE( );
5587}
5588/* END_CASE */
5589
5590/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005591void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005592 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005593 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005594 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005595 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005596 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005597 int expected_status_arg3,
5598 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005599{
5600 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005601 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5602 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005603 psa_status_t expected_statuses[] = {expected_status_arg1,
5604 expected_status_arg2,
5605 expected_status_arg3};
5606 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005607 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5608 MBEDTLS_SVC_KEY_ID_INIT,
5609 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005610 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5612 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005613 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005614 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005615 psa_status_t expected_output_status = expected_output_status_arg;
5616 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005617
5618 PSA_ASSERT( psa_crypto_init( ) );
5619
5620 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5621 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005622
5623 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5624
5625 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5626 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005627 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005628 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005629 psa_set_key_type( &attributes, key_types[i] );
5630 PSA_ASSERT( psa_import_key( &attributes,
5631 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005632 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005633 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5634 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5635 {
5636 // When taking a private key as secret input, use key agreement
5637 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005638 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5639 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005640 expected_statuses[i] );
5641 }
5642 else
5643 {
5644 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005645 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005646 expected_statuses[i] );
5647 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005648 }
5649 else
5650 {
5651 TEST_EQUAL( psa_key_derivation_input_bytes(
5652 &operation, steps[i],
5653 inputs[i]->x, inputs[i]->len ),
5654 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005655 }
5656 }
5657
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005658 if( output_key_type != PSA_KEY_TYPE_NONE )
5659 {
5660 psa_reset_key_attributes( &attributes );
5661 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5662 psa_set_key_bits( &attributes, 8 );
5663 actual_output_status =
5664 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005665 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005666 }
5667 else
5668 {
5669 uint8_t buffer[1];
5670 actual_output_status =
5671 psa_key_derivation_output_bytes( &operation,
5672 buffer, sizeof( buffer ) );
5673 }
5674 TEST_EQUAL( actual_output_status, expected_output_status );
5675
Janos Follathaf3c2a02019-06-12 12:34:34 +01005676exit:
5677 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005678 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5679 psa_destroy_key( keys[i] );
5680 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005681 PSA_DONE( );
5682}
5683/* END_CASE */
5684
Janos Follathd958bb72019-07-03 15:02:16 +01005685/* BEGIN_CASE */
5686void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005687{
Janos Follathd958bb72019-07-03 15:02:16 +01005688 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005689 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005690 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005691 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005692 unsigned char input1[] = "Input 1";
5693 size_t input1_length = sizeof( input1 );
5694 unsigned char input2[] = "Input 2";
5695 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005696 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005697 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005698 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5699 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5700 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005702
Gilles Peskine8817f612018-12-18 00:18:46 +01005703 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005704
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005705 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5706 psa_set_key_algorithm( &attributes, alg );
5707 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005708
Gilles Peskine73676cb2019-05-15 20:15:10 +02005709 PSA_ASSERT( psa_import_key( &attributes,
5710 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005711 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005712
5713 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005714 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5715 input1, input1_length,
5716 input2, input2_length,
5717 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005718 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005719
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005721 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005722 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005723
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005724 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005725
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005726 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005727 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005728
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005729exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005730 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005731 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005732 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005733}
5734/* END_CASE */
5735
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005736/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005737void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005738{
5739 uint8_t output_buffer[16];
5740 size_t buffer_size = 16;
5741 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005742 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005743
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005744 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5745 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005746 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005747
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005748 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005749 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005750
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005751 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005752
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005753 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5754 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005755 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005756
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005757 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005758 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005759
5760exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005761 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005762}
5763/* END_CASE */
5764
5765/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005766void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005767 int step1_arg, data_t *input1,
5768 int step2_arg, data_t *input2,
5769 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005770 int requested_capacity_arg,
5771 data_t *expected_output1,
5772 data_t *expected_output2 )
5773{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005774 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005775 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5776 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005777 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5778 MBEDTLS_SVC_KEY_ID_INIT,
5779 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005780 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005781 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005782 uint8_t *expected_outputs[2] =
5783 {expected_output1->x, expected_output2->x};
5784 size_t output_sizes[2] =
5785 {expected_output1->len, expected_output2->len};
5786 size_t output_buffer_size = 0;
5787 uint8_t *output_buffer = NULL;
5788 size_t expected_capacity;
5789 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005791 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005792 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005793
5794 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5795 {
5796 if( output_sizes[i] > output_buffer_size )
5797 output_buffer_size = output_sizes[i];
5798 if( output_sizes[i] == 0 )
5799 expected_outputs[i] = NULL;
5800 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005801 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005802 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005803
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005804 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5805 psa_set_key_algorithm( &attributes, alg );
5806 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005807
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005808 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005809 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5810 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5811 requested_capacity ) );
5812 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005813 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005814 switch( steps[i] )
5815 {
5816 case 0:
5817 break;
5818 case PSA_KEY_DERIVATION_INPUT_SECRET:
5819 PSA_ASSERT( psa_import_key( &attributes,
5820 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005821 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005822
5823 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5824 {
5825 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5826 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5827 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5828 }
5829
Gilles Peskine1468da72019-05-29 17:35:49 +02005830 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005831 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005832 break;
5833 default:
5834 PSA_ASSERT( psa_key_derivation_input_bytes(
5835 &operation, steps[i],
5836 inputs[i]->x, inputs[i]->len ) );
5837 break;
5838 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005839 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005840
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005841 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005842 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005843 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005844 expected_capacity = requested_capacity;
5845
5846 /* Expansion phase. */
5847 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5848 {
5849 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005850 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005851 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005852 if( expected_capacity == 0 && output_sizes[i] == 0 )
5853 {
5854 /* Reading 0 bytes when 0 bytes are available can go either way. */
5855 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005856 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005857 continue;
5858 }
5859 else if( expected_capacity == 0 ||
5860 output_sizes[i] > expected_capacity )
5861 {
5862 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005863 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005864 expected_capacity = 0;
5865 continue;
5866 }
5867 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005868 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005869 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005870 ASSERT_COMPARE( output_buffer, output_sizes[i],
5871 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005872 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005873 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005874 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005875 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005876 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005877 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005878 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005879
5880exit:
5881 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005882 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005883 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5884 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005885 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005886}
5887/* END_CASE */
5888
5889/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005890void derive_full( int alg_arg,
5891 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005892 data_t *input1,
5893 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005894 int requested_capacity_arg )
5895{
Ronald Cron5425a212020-08-04 14:58:35 +02005896 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005897 psa_algorithm_t alg = alg_arg;
5898 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005899 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005900 unsigned char output_buffer[16];
5901 size_t expected_capacity = requested_capacity;
5902 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005904
Gilles Peskine8817f612018-12-18 00:18:46 +01005905 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005906
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005907 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5908 psa_set_key_algorithm( &attributes, alg );
5909 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005910
Gilles Peskine049c7532019-05-15 20:22:09 +02005911 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005912 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005913
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005914 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5915 input1->x, input1->len,
5916 input2->x, input2->len,
5917 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005918 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005919
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005920 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005921 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005922 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005923
5924 /* Expansion phase. */
5925 while( current_capacity > 0 )
5926 {
5927 size_t read_size = sizeof( output_buffer );
5928 if( read_size > current_capacity )
5929 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005930 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005931 output_buffer,
5932 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005933 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005934 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005935 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005936 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005937 }
5938
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005939 /* Check that the operation refuses to go over capacity. */
5940 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005941 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005942
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005943 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005944
5945exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005946 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005947 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005948 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005949}
5950/* END_CASE */
5951
Janos Follathe60c9052019-07-03 13:51:30 +01005952/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005953void derive_key_exercise( int alg_arg,
5954 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005955 data_t *input1,
5956 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005957 int derived_type_arg,
5958 int derived_bits_arg,
5959 int derived_usage_arg,
5960 int derived_alg_arg )
5961{
Ronald Cron5425a212020-08-04 14:58:35 +02005962 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5963 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005964 psa_algorithm_t alg = alg_arg;
5965 psa_key_type_t derived_type = derived_type_arg;
5966 size_t derived_bits = derived_bits_arg;
5967 psa_key_usage_t derived_usage = derived_usage_arg;
5968 psa_algorithm_t derived_alg = derived_alg_arg;
5969 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005970 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005971 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005972 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005973
Gilles Peskine8817f612018-12-18 00:18:46 +01005974 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005975
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005976 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5977 psa_set_key_algorithm( &attributes, alg );
5978 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005979 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005980 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005981
5982 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005983 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5984 input1->x, input1->len,
5985 input2->x, input2->len,
5986 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005987 goto exit;
5988
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005989 psa_set_key_usage_flags( &attributes, derived_usage );
5990 psa_set_key_algorithm( &attributes, derived_alg );
5991 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005992 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005993 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005994 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005995
5996 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005997 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005998 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5999 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006000
6001 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006002 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006003 goto exit;
6004
6005exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006006 /*
6007 * Key attributes may have been returned by psa_get_key_attributes()
6008 * thus reset them as required.
6009 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006010 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006011
6012 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006013 psa_destroy_key( base_key );
6014 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006015 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006016}
6017/* END_CASE */
6018
Janos Follath42fd8882019-07-03 14:17:09 +01006019/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006020void derive_key_export( int alg_arg,
6021 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006022 data_t *input1,
6023 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006024 int bytes1_arg,
6025 int bytes2_arg )
6026{
Ronald Cron5425a212020-08-04 14:58:35 +02006027 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6028 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006029 psa_algorithm_t alg = alg_arg;
6030 size_t bytes1 = bytes1_arg;
6031 size_t bytes2 = bytes2_arg;
6032 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006033 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006034 uint8_t *output_buffer = NULL;
6035 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006036 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6037 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006038 size_t length;
6039
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006040 ASSERT_ALLOC( output_buffer, capacity );
6041 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006042 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006043
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006044 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6045 psa_set_key_algorithm( &base_attributes, alg );
6046 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006047 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006048 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006049
6050 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006051 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6052 input1->x, input1->len,
6053 input2->x, input2->len,
6054 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006055 goto exit;
6056
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006057 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006058 output_buffer,
6059 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006060 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006061
6062 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006063 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6064 input1->x, input1->len,
6065 input2->x, input2->len,
6066 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006067 goto exit;
6068
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006069 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6070 psa_set_key_algorithm( &derived_attributes, 0 );
6071 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006072 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006073 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006074 &derived_key ) );
6075 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006076 export_buffer, bytes1,
6077 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006078 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006079 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006080 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006081 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006082 &derived_key ) );
6083 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006084 export_buffer + bytes1, bytes2,
6085 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006086 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006087
6088 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006089 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6090 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006091
6092exit:
6093 mbedtls_free( output_buffer );
6094 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006095 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006096 psa_destroy_key( base_key );
6097 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006098 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006099}
6100/* END_CASE */
6101
6102/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006103void derive_key( int alg_arg,
6104 data_t *key_data, data_t *input1, data_t *input2,
6105 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006106 int expected_status_arg,
6107 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006108{
Ronald Cron5425a212020-08-04 14:58:35 +02006109 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6110 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006111 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006112 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006113 size_t bits = bits_arg;
6114 psa_status_t expected_status = expected_status_arg;
6115 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6116 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6117 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6118
6119 PSA_ASSERT( psa_crypto_init( ) );
6120
6121 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6122 psa_set_key_algorithm( &base_attributes, alg );
6123 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6124 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006125 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006126
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006127 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6128 input1->x, input1->len,
6129 input2->x, input2->len,
6130 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006131 goto exit;
6132
6133 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6134 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006135 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006136 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006137
6138 psa_status_t status =
6139 psa_key_derivation_output_key( &derived_attributes,
6140 &operation,
6141 &derived_key );
6142 if( is_large_output > 0 )
6143 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6144 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006145
6146exit:
6147 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006148 psa_destroy_key( base_key );
6149 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006150 PSA_DONE( );
6151}
6152/* END_CASE */
6153
6154/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006155void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006156 int our_key_type_arg, int our_key_alg_arg,
6157 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006158 int expected_status_arg )
6159{
Ronald Cron5425a212020-08-04 14:58:35 +02006160 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006161 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006162 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006163 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006164 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006166 psa_status_t expected_status = expected_status_arg;
6167 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006168
Gilles Peskine8817f612018-12-18 00:18:46 +01006169 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006170
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006172 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006173 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006174 PSA_ASSERT( psa_import_key( &attributes,
6175 our_key_data->x, our_key_data->len,
6176 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006177
Gilles Peskine77f40d82019-04-11 21:27:06 +02006178 /* The tests currently include inputs that should fail at either step.
6179 * Test cases that fail at the setup step should be changed to call
6180 * key_derivation_setup instead, and this function should be renamed
6181 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006182 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006183 if( status == PSA_SUCCESS )
6184 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006185 TEST_EQUAL( psa_key_derivation_key_agreement(
6186 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6187 our_key,
6188 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006189 expected_status );
6190 }
6191 else
6192 {
6193 TEST_ASSERT( status == expected_status );
6194 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006195
6196exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006197 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006198 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006199 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006200}
6201/* END_CASE */
6202
6203/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006204void raw_key_agreement( int alg_arg,
6205 int our_key_type_arg, data_t *our_key_data,
6206 data_t *peer_key_data,
6207 data_t *expected_output )
6208{
Ronald Cron5425a212020-08-04 14:58:35 +02006209 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006210 psa_algorithm_t alg = alg_arg;
6211 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006213 unsigned char *output = NULL;
6214 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006215 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006216
6217 ASSERT_ALLOC( output, expected_output->len );
6218 PSA_ASSERT( psa_crypto_init( ) );
6219
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6221 psa_set_key_algorithm( &attributes, alg );
6222 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006223 PSA_ASSERT( psa_import_key( &attributes,
6224 our_key_data->x, our_key_data->len,
6225 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006226
gabor-mezei-armceface22021-01-21 12:26:17 +01006227 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6228 key_bits = psa_get_key_bits( &attributes );
6229
Gilles Peskinebe697d82019-05-16 18:00:41 +02006230 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6231 peer_key_data->x, peer_key_data->len,
6232 output, expected_output->len,
6233 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006234 ASSERT_COMPARE( output, output_length,
6235 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006236 TEST_ASSERT( output_length <=
6237 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6238 TEST_ASSERT( output_length <=
6239 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006240
6241exit:
6242 mbedtls_free( output );
6243 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006244 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006245}
6246/* END_CASE */
6247
6248/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006249void key_agreement_capacity( int alg_arg,
6250 int our_key_type_arg, data_t *our_key_data,
6251 data_t *peer_key_data,
6252 int expected_capacity_arg )
6253{
Ronald Cron5425a212020-08-04 14:58:35 +02006254 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006255 psa_algorithm_t alg = alg_arg;
6256 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006259 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006260 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006261
Gilles Peskine8817f612018-12-18 00:18:46 +01006262 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006263
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006264 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6265 psa_set_key_algorithm( &attributes, alg );
6266 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006267 PSA_ASSERT( psa_import_key( &attributes,
6268 our_key_data->x, our_key_data->len,
6269 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006270
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006271 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006272 PSA_ASSERT( psa_key_derivation_key_agreement(
6273 &operation,
6274 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6275 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006276 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6277 {
6278 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006279 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006280 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006281 NULL, 0 ) );
6282 }
Gilles Peskine59685592018-09-18 12:11:34 +02006283
Gilles Peskinebf491972018-10-25 22:36:12 +02006284 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006285 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006286 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006287 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006288
Gilles Peskinebf491972018-10-25 22:36:12 +02006289 /* Test the actual capacity by reading the output. */
6290 while( actual_capacity > sizeof( output ) )
6291 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006292 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006293 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006294 actual_capacity -= sizeof( output );
6295 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006296 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006297 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006298 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006299 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006300
Gilles Peskine59685592018-09-18 12:11:34 +02006301exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006302 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006303 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006304 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006305}
6306/* END_CASE */
6307
6308/* BEGIN_CASE */
6309void key_agreement_output( int alg_arg,
6310 int our_key_type_arg, data_t *our_key_data,
6311 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006312 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006313{
Ronald Cron5425a212020-08-04 14:58:35 +02006314 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006315 psa_algorithm_t alg = alg_arg;
6316 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006317 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006319 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006320
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006321 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6322 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006323
Gilles Peskine8817f612018-12-18 00:18:46 +01006324 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006325
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006326 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6327 psa_set_key_algorithm( &attributes, alg );
6328 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006329 PSA_ASSERT( psa_import_key( &attributes,
6330 our_key_data->x, our_key_data->len,
6331 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006332
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006333 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006334 PSA_ASSERT( psa_key_derivation_key_agreement(
6335 &operation,
6336 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6337 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006338 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6339 {
6340 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006341 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006342 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006343 NULL, 0 ) );
6344 }
Gilles Peskine59685592018-09-18 12:11:34 +02006345
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006346 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006347 actual_output,
6348 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006349 ASSERT_COMPARE( actual_output, expected_output1->len,
6350 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006351 if( expected_output2->len != 0 )
6352 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006353 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006354 actual_output,
6355 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006356 ASSERT_COMPARE( actual_output, expected_output2->len,
6357 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006358 }
Gilles Peskine59685592018-09-18 12:11:34 +02006359
6360exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006361 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006362 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006363 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006364 mbedtls_free( actual_output );
6365}
6366/* END_CASE */
6367
6368/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006369void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006370{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006371 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006372 unsigned char *output = NULL;
6373 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006374 size_t i;
6375 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006376
Simon Butcher49f8e312020-03-03 15:51:50 +00006377 TEST_ASSERT( bytes_arg >= 0 );
6378
Gilles Peskine91892022021-02-08 19:50:26 +01006379 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006380 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006381
Gilles Peskine8817f612018-12-18 00:18:46 +01006382 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006383
Gilles Peskinea50d7392018-06-21 10:22:13 +02006384 /* Run several times, to ensure that every output byte will be
6385 * nonzero at least once with overwhelming probability
6386 * (2^(-8*number_of_runs)). */
6387 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006388 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006389 if( bytes != 0 )
6390 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006391 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006392
Gilles Peskinea50d7392018-06-21 10:22:13 +02006393 for( i = 0; i < bytes; i++ )
6394 {
6395 if( output[i] != 0 )
6396 ++changed[i];
6397 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006398 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006399
6400 /* Check that every byte was changed to nonzero at least once. This
6401 * validates that psa_generate_random is overwriting every byte of
6402 * the output buffer. */
6403 for( i = 0; i < bytes; i++ )
6404 {
6405 TEST_ASSERT( changed[i] != 0 );
6406 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006407
6408exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006409 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006410 mbedtls_free( output );
6411 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006412}
6413/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006414
6415/* BEGIN_CASE */
6416void generate_key( int type_arg,
6417 int bits_arg,
6418 int usage_arg,
6419 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006420 int expected_status_arg,
6421 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006422{
Ronald Cron5425a212020-08-04 14:58:35 +02006423 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006424 psa_key_type_t type = type_arg;
6425 psa_key_usage_t usage = usage_arg;
6426 size_t bits = bits_arg;
6427 psa_algorithm_t alg = alg_arg;
6428 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006429 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006430 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006431
Gilles Peskine8817f612018-12-18 00:18:46 +01006432 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006433
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006434 psa_set_key_usage_flags( &attributes, usage );
6435 psa_set_key_algorithm( &attributes, alg );
6436 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006437 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006438
6439 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006440 psa_status_t status = psa_generate_key( &attributes, &key );
6441
6442 if( is_large_key > 0 )
6443 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6444 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006445 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006446 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006447
6448 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006449 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006450 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6451 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006452
Gilles Peskine818ca122018-06-20 18:16:48 +02006453 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006454 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006455 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006456
6457exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006458 /*
6459 * Key attributes may have been returned by psa_get_key_attributes()
6460 * thus reset them as required.
6461 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006462 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006463
Ronald Cron5425a212020-08-04 14:58:35 +02006464 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006465 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006466}
6467/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006468
Ronald Cronee414c72021-03-18 18:50:08 +01006469/* 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 +02006470void generate_key_rsa( int bits_arg,
6471 data_t *e_arg,
6472 int expected_status_arg )
6473{
Ronald Cron5425a212020-08-04 14:58:35 +02006474 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006475 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006476 size_t bits = bits_arg;
6477 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6478 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6479 psa_status_t expected_status = expected_status_arg;
6480 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6481 uint8_t *exported = NULL;
6482 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006483 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006484 size_t exported_length = SIZE_MAX;
6485 uint8_t *e_read_buffer = NULL;
6486 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006487 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006488 size_t e_read_length = SIZE_MAX;
6489
6490 if( e_arg->len == 0 ||
6491 ( e_arg->len == 3 &&
6492 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6493 {
6494 is_default_public_exponent = 1;
6495 e_read_size = 0;
6496 }
6497 ASSERT_ALLOC( e_read_buffer, e_read_size );
6498 ASSERT_ALLOC( exported, exported_size );
6499
6500 PSA_ASSERT( psa_crypto_init( ) );
6501
6502 psa_set_key_usage_flags( &attributes, usage );
6503 psa_set_key_algorithm( &attributes, alg );
6504 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6505 e_arg->x, e_arg->len ) );
6506 psa_set_key_bits( &attributes, bits );
6507
6508 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006509 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006510 if( expected_status != PSA_SUCCESS )
6511 goto exit;
6512
6513 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006514 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006515 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6516 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6517 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6518 e_read_buffer, e_read_size,
6519 &e_read_length ) );
6520 if( is_default_public_exponent )
6521 TEST_EQUAL( e_read_length, 0 );
6522 else
6523 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6524
6525 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006526 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006527 goto exit;
6528
6529 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006530 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006531 exported, exported_size,
6532 &exported_length ) );
6533 {
6534 uint8_t *p = exported;
6535 uint8_t *end = exported + exported_length;
6536 size_t len;
6537 /* RSAPublicKey ::= SEQUENCE {
6538 * modulus INTEGER, -- n
6539 * publicExponent INTEGER } -- e
6540 */
6541 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006542 MBEDTLS_ASN1_SEQUENCE |
6543 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006544 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006545 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6546 MBEDTLS_ASN1_INTEGER ) );
6547 if( len >= 1 && p[0] == 0 )
6548 {
6549 ++p;
6550 --len;
6551 }
6552 if( e_arg->len == 0 )
6553 {
6554 TEST_EQUAL( len, 3 );
6555 TEST_EQUAL( p[0], 1 );
6556 TEST_EQUAL( p[1], 0 );
6557 TEST_EQUAL( p[2], 1 );
6558 }
6559 else
6560 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6561 }
6562
6563exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006564 /*
6565 * Key attributes may have been returned by psa_get_key_attributes() or
6566 * set by psa_set_key_domain_parameters() thus reset them as required.
6567 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006568 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006569
Ronald Cron5425a212020-08-04 14:58:35 +02006570 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006571 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006572 mbedtls_free( e_read_buffer );
6573 mbedtls_free( exported );
6574}
6575/* END_CASE */
6576
Darryl Greend49a4992018-06-18 17:27:26 +01006577/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006578void persistent_key_load_key_from_storage( data_t *data,
6579 int type_arg, int bits_arg,
6580 int usage_flags_arg, int alg_arg,
6581 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006582{
Ronald Cron71016a92020-08-28 19:01:50 +02006583 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006585 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6586 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006587 psa_key_type_t type = type_arg;
6588 size_t bits = bits_arg;
6589 psa_key_usage_t usage_flags = usage_flags_arg;
6590 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006591 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006592 unsigned char *first_export = NULL;
6593 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006594 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006595 size_t first_exported_length;
6596 size_t second_exported_length;
6597
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006598 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6599 {
6600 ASSERT_ALLOC( first_export, export_size );
6601 ASSERT_ALLOC( second_export, export_size );
6602 }
Darryl Greend49a4992018-06-18 17:27:26 +01006603
Gilles Peskine8817f612018-12-18 00:18:46 +01006604 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006605
Gilles Peskinec87af662019-05-15 16:12:22 +02006606 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006607 psa_set_key_usage_flags( &attributes, usage_flags );
6608 psa_set_key_algorithm( &attributes, alg );
6609 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006610 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006611
Darryl Green0c6575a2018-11-07 16:05:30 +00006612 switch( generation_method )
6613 {
6614 case IMPORT_KEY:
6615 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006616 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006617 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006618 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006619
Darryl Green0c6575a2018-11-07 16:05:30 +00006620 case GENERATE_KEY:
6621 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006622 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006623 break;
6624
6625 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006626#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006627 {
6628 /* Create base key */
6629 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6630 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6631 psa_set_key_usage_flags( &base_attributes,
6632 PSA_KEY_USAGE_DERIVE );
6633 psa_set_key_algorithm( &base_attributes, derive_alg );
6634 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006635 PSA_ASSERT( psa_import_key( &base_attributes,
6636 data->x, data->len,
6637 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006638 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006639 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006640 PSA_ASSERT( psa_key_derivation_input_key(
6641 &operation,
6642 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006643 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006644 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006645 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006646 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6647 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006648 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006649 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006650 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006651 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006652 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006653#else
6654 TEST_ASSUME( ! "KDF not supported in this configuration" );
6655#endif
6656 break;
6657
6658 default:
6659 TEST_ASSERT( ! "generation_method not implemented in test" );
6660 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006661 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006662 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006663
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006664 /* Export the key if permitted by the key policy. */
6665 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6666 {
Ronald Cron5425a212020-08-04 14:58:35 +02006667 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006668 first_export, export_size,
6669 &first_exported_length ) );
6670 if( generation_method == IMPORT_KEY )
6671 ASSERT_COMPARE( data->x, data->len,
6672 first_export, first_exported_length );
6673 }
Darryl Greend49a4992018-06-18 17:27:26 +01006674
6675 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006676 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006677 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006678 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006679
Darryl Greend49a4992018-06-18 17:27:26 +01006680 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006681 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006682 TEST_ASSERT( mbedtls_svc_key_id_equal(
6683 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006684 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6685 PSA_KEY_LIFETIME_PERSISTENT );
6686 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6687 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6688 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6689 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006690
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006691 /* Export the key again if permitted by the key policy. */
6692 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006693 {
Ronald Cron5425a212020-08-04 14:58:35 +02006694 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006695 second_export, export_size,
6696 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006697 ASSERT_COMPARE( first_export, first_exported_length,
6698 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006699 }
6700
6701 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006702 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006703 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006704
6705exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006706 /*
6707 * Key attributes may have been returned by psa_get_key_attributes()
6708 * thus reset them as required.
6709 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006710 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006711
Darryl Greend49a4992018-06-18 17:27:26 +01006712 mbedtls_free( first_export );
6713 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006714 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006715 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006716 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006717 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006718}
6719/* END_CASE */