blob: a240df7bda88b82038586b2eba026590aab772ac [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 Elliott91b021e2021-07-23 18:52:31 +01004063 psa_aead_abort( &operation );
4064 PSA_DONE( );
4065}
4066/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004067
4068/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004069void aead_multipart_verify( int key_type_arg, data_t *key_data,
4070 int alg_arg,
4071 data_t *nonce,
4072 data_t *additional_data,
4073 data_t *input_data,
4074 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004075 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004076 int expected_status_arg )
4077{
4078 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4079 psa_key_type_t key_type = key_type_arg;
4080 psa_algorithm_t alg = alg_arg;
4081 psa_aead_operation_t operation;
4082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4083 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4084 psa_status_t expected_status = expected_status_arg;
4085 unsigned char *plaintext = NULL;
4086 unsigned char *finish_plaintext = NULL;
4087 size_t plaintext_size = 0;
4088 size_t plaintext_length = 0;
4089 size_t verify_plaintext_size = 0;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004090 tagusage_method tag_usage = tag_usage_arg;
4091 unsigned char *tag_buffer = NULL;
4092 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004093
4094 PSA_ASSERT( psa_crypto_init( ) );
4095
4096 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4097 psa_set_key_algorithm( &attributes, alg );
4098 psa_set_key_type( &attributes, key_type );
4099
4100 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4101 &key ) );
4102
4103 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4104
4105 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4106 input_data->len );
4107
4108 ASSERT_ALLOC( plaintext, plaintext_size );
4109
4110 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4111
4112 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4113
4114 operation = psa_aead_operation_init( );
4115
4116 status = psa_aead_decrypt_setup( &operation, key, alg );
4117
4118 /* If the operation is not supported, just skip and not fail in case the
4119 * encryption involves a common limitation of cryptography hardwares and
4120 * an alternative implementation. */
4121 if( status == PSA_ERROR_NOT_SUPPORTED )
4122 {
4123 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4124 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4125 }
4126
4127 PSA_ASSERT( status );
4128
4129 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4130
4131 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4132 additional_data->len ) );
4133
4134 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4135 input_data->len,
4136 plaintext, plaintext_size,
4137 &plaintext_length ) );
4138
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004139 if( tag_usage == USE_GIVEN_TAG )
4140 {
4141 tag_buffer = tag->x;
4142 tag_size = tag->len;
4143 }
4144
Paul Elliott9961a662021-09-17 19:19:02 +01004145 status = psa_aead_verify( &operation, finish_plaintext,
4146 verify_plaintext_size,
4147 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004148 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004149
4150 TEST_EQUAL( status, expected_status );
4151
4152exit:
4153 psa_destroy_key( key );
4154 mbedtls_free( plaintext );
4155 mbedtls_free( finish_plaintext );
4156 psa_aead_abort( &operation );
4157 PSA_DONE( );
4158}
4159/* END_CASE */
4160
Paul Elliott9961a662021-09-17 19:19:02 +01004161/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004162void aead_multipart_setup( int key_type_arg, data_t *key_data,
4163 int alg_arg, int expected_status_arg )
4164{
4165 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4166 psa_key_type_t key_type = key_type_arg;
4167 psa_algorithm_t alg = alg_arg;
4168 psa_aead_operation_t operation;
4169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4170 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4171 psa_status_t expected_status = expected_status_arg;
4172
4173 PSA_ASSERT( psa_crypto_init( ) );
4174
4175 psa_set_key_usage_flags( &attributes,
4176 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4177 psa_set_key_algorithm( &attributes, alg );
4178 psa_set_key_type( &attributes, key_type );
4179
4180 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4181 &key ) );
4182
4183 mbedtls_test_set_step( 0 );
4184
4185 status = psa_aead_encrypt_setup( &operation, key, alg );
4186
4187 TEST_EQUAL( status, expected_status );
4188
4189 psa_aead_abort( &operation );
4190
4191 operation = psa_aead_operation_init( );
4192
4193 mbedtls_test_set_step( 1 );
4194
4195 status = psa_aead_decrypt_setup( &operation, key, alg );
4196
4197 TEST_EQUAL(status, expected_status );
4198
4199exit:
4200 psa_destroy_key( key );
4201 psa_aead_abort( &operation );
4202 PSA_DONE( );
4203}
4204/* END_CASE */
4205
4206/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004207void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4208 int alg_arg,
4209 data_t *nonce,
4210 data_t *additional_data,
4211 data_t *input_data )
4212{
4213 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4214 psa_key_type_t key_type = key_type_arg;
4215 psa_algorithm_t alg = alg_arg;
4216 psa_aead_operation_t operation;
4217 unsigned char *output_data = NULL;
4218 unsigned char *final_data = NULL;
4219 size_t output_size = 0;
4220 size_t finish_output_size = 0;
4221 size_t output_length = 0;
4222 size_t key_bits = 0;
4223 size_t tag_length = 0;
4224 size_t tag_size = 0;
4225 size_t nonce_length = 0;
4226 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4227 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4228 size_t output_part_length = 0;
4229 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4230
4231 PSA_ASSERT( psa_crypto_init( ) );
4232
4233 psa_set_key_usage_flags( & attributes,
4234 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4235 psa_set_key_algorithm( & attributes, alg );
4236 psa_set_key_type( & attributes, key_type );
4237
4238 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4239 &key ) );
4240
4241 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4242 key_bits = psa_get_key_bits( &attributes );
4243
4244 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4245
4246 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4247
4248 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4249
4250 ASSERT_ALLOC( output_data, output_size );
4251
4252 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4253
4254 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4255
4256 ASSERT_ALLOC( final_data, finish_output_size );
4257
4258 /* Test all operations error without calling setup first. */
4259
4260 operation = psa_aead_operation_init( );
4261
4262 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4263 PSA_ERROR_BAD_STATE );
4264
4265 psa_aead_abort( &operation );
4266
4267 operation = psa_aead_operation_init( );
4268
4269 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4270 PSA_AEAD_NONCE_MAX_SIZE,
4271 &nonce_length ),
4272 PSA_ERROR_BAD_STATE );
4273
4274 psa_aead_abort( &operation );
4275
Paul Elliott481be342021-07-16 17:38:47 +01004276 /* ------------------------------------------------------- */
4277
Paul Elliottc23a9a02021-06-21 18:32:46 +01004278 operation = psa_aead_operation_init( );
4279
4280 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4281 input_data->len ),
4282 PSA_ERROR_BAD_STATE );
4283
4284 psa_aead_abort( &operation );
4285
Paul Elliott481be342021-07-16 17:38:47 +01004286 /* ------------------------------------------------------- */
4287
Paul Elliottc23a9a02021-06-21 18:32:46 +01004288 operation = psa_aead_operation_init( );
4289
4290 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4291 additional_data->len ),
4292 PSA_ERROR_BAD_STATE );
4293
4294 psa_aead_abort( &operation );
4295
Paul Elliott481be342021-07-16 17:38:47 +01004296 /* ------------------------------------------------------- */
4297
Paul Elliottc23a9a02021-06-21 18:32:46 +01004298 operation = psa_aead_operation_init( );
4299
4300 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4301 input_data->len, output_data,
4302 output_size, &output_length ),
4303 PSA_ERROR_BAD_STATE );
4304
4305 psa_aead_abort( &operation );
4306
Paul Elliott481be342021-07-16 17:38:47 +01004307 /* ------------------------------------------------------- */
4308
Paul Elliottc23a9a02021-06-21 18:32:46 +01004309 operation = psa_aead_operation_init( );
4310
4311 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4312 finish_output_size,
4313 &output_part_length,
4314 tag_buffer, tag_length,
4315 &tag_size ),
4316 PSA_ERROR_BAD_STATE );
4317
4318 psa_aead_abort( &operation );
4319
Paul Elliott481be342021-07-16 17:38:47 +01004320 /* ------------------------------------------------------- */
4321
Paul Elliottc23a9a02021-06-21 18:32:46 +01004322 operation = psa_aead_operation_init( );
4323
4324 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4325 finish_output_size,
4326 &output_part_length,
4327 tag_buffer,
4328 tag_length ),
4329 PSA_ERROR_BAD_STATE );
4330
4331 psa_aead_abort( &operation );
4332
4333 /* Test for double setups. */
4334
4335 operation = psa_aead_operation_init( );
4336
4337 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4338
4339 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4340 PSA_ERROR_BAD_STATE );
4341
4342 psa_aead_abort( &operation );
4343
Paul Elliott481be342021-07-16 17:38:47 +01004344 /* ------------------------------------------------------- */
4345
Paul Elliottc23a9a02021-06-21 18:32:46 +01004346 operation = psa_aead_operation_init( );
4347
4348 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4349
4350 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4351 PSA_ERROR_BAD_STATE );
4352
4353 psa_aead_abort( &operation );
4354
Paul Elliott374a2be2021-07-16 17:53:40 +01004355 /* ------------------------------------------------------- */
4356
4357 operation = psa_aead_operation_init( );
4358
4359 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4360
4361 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4362 PSA_ERROR_BAD_STATE );
4363
4364 psa_aead_abort( &operation );
4365
4366 /* ------------------------------------------------------- */
4367
4368 operation = psa_aead_operation_init( );
4369
4370 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4371
4372 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4373 PSA_ERROR_BAD_STATE );
4374
4375 psa_aead_abort( &operation );
4376
Paul Elliottc23a9a02021-06-21 18:32:46 +01004377 /* Test for not setting a nonce. */
4378
4379 operation = psa_aead_operation_init( );
4380
4381 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4382
4383 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4384 additional_data->len ),
4385 PSA_ERROR_BAD_STATE );
4386
4387 psa_aead_abort( &operation );
4388
Paul Elliott7f628422021-09-01 12:08:29 +01004389 /* ------------------------------------------------------- */
4390
4391 operation = psa_aead_operation_init( );
4392
4393 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4394
4395 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4396 input_data->len, output_data,
4397 output_size, &output_length ),
4398 PSA_ERROR_BAD_STATE );
4399
4400 psa_aead_abort( &operation );
4401
Paul Elliottc23a9a02021-06-21 18:32:46 +01004402 /* Test for double setting nonce. */
4403
4404 operation = psa_aead_operation_init( );
4405
4406 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4407
4408 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4409
4410 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4411 PSA_ERROR_BAD_STATE );
4412
4413 psa_aead_abort( &operation );
4414
Paul Elliott374a2be2021-07-16 17:53:40 +01004415 /* Test for double generating nonce. */
4416
4417 operation = psa_aead_operation_init( );
4418
4419 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4420
4421 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4422 PSA_AEAD_NONCE_MAX_SIZE,
4423 &nonce_length ) );
4424
4425 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4426 PSA_AEAD_NONCE_MAX_SIZE,
4427 &nonce_length ),
4428 PSA_ERROR_BAD_STATE );
4429
4430
4431 psa_aead_abort( &operation );
4432
4433 /* Test for generate nonce then set and vice versa */
4434
4435 operation = psa_aead_operation_init( );
4436
4437 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4438
4439 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4440 PSA_AEAD_NONCE_MAX_SIZE,
4441 &nonce_length ) );
4442
4443 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4444 PSA_ERROR_BAD_STATE );
4445
4446 psa_aead_abort( &operation );
4447
4448 /* ------------------------------------------------------- */
4449
4450 operation = psa_aead_operation_init( );
4451
4452 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4453
4454 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4455
4456 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4457 PSA_AEAD_NONCE_MAX_SIZE,
4458 &nonce_length ),
4459 PSA_ERROR_BAD_STATE );
4460
4461 psa_aead_abort( &operation );
4462
Paul Elliott7220cae2021-06-22 17:25:57 +01004463 /* Test for generating nonce in decrypt setup. */
4464
4465 operation = psa_aead_operation_init( );
4466
4467 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4468
4469 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4470 PSA_AEAD_NONCE_MAX_SIZE,
4471 &nonce_length ),
4472 PSA_ERROR_BAD_STATE );
4473
4474 psa_aead_abort( &operation );
4475
Paul Elliottc23a9a02021-06-21 18:32:46 +01004476 /* Test for setting lengths twice. */
4477
4478 operation = psa_aead_operation_init( );
4479
4480 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4481
4482 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4483
4484 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4485 input_data->len ) );
4486
4487 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4488 input_data->len ),
4489 PSA_ERROR_BAD_STATE );
4490
4491 psa_aead_abort( &operation );
4492
4493 /* Test for setting lengths after already starting data. */
4494
4495 operation = psa_aead_operation_init( );
4496
4497 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4498
4499 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4500
Paul Elliottf94bd992021-09-19 18:15:59 +01004501 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4502 additional_data->len ) );
4503
4504 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4505 input_data->len ),
4506 PSA_ERROR_BAD_STATE );
4507
4508 psa_aead_abort( &operation );
4509
4510 /* ------------------------------------------------------- */
4511
4512 operation = psa_aead_operation_init( );
4513
4514 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4515
4516 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4517
Paul Elliottc23a9a02021-06-21 18:32:46 +01004518 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4519 input_data->len, output_data,
4520 output_size, &output_length ) );
4521
4522 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4523 input_data->len ),
4524 PSA_ERROR_BAD_STATE );
4525
4526 psa_aead_abort( &operation );
4527
Paul Elliott243080c2021-07-21 19:01:17 +01004528 /* Test for not sending any additional data or data after setting non zero
4529 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004530
4531 operation = psa_aead_operation_init( );
4532
4533 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4534
4535 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4536
4537 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4538 input_data->len ) );
4539
4540 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4541 finish_output_size,
4542 &output_part_length,
4543 tag_buffer, tag_length,
4544 &tag_size ),
4545 PSA_ERROR_INVALID_ARGUMENT );
4546
4547 psa_aead_abort( &operation );
4548
Paul Elliott243080c2021-07-21 19:01:17 +01004549 /* Test for not sending any additional data or data after setting non-zero
4550 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004551
4552 operation = psa_aead_operation_init( );
4553
4554 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4555
4556 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4557
4558 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4559 input_data->len ) );
4560
4561 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4562 finish_output_size,
4563 &output_part_length,
4564 tag_buffer,
4565 tag_length ),
4566 PSA_ERROR_INVALID_ARGUMENT );
4567
4568 psa_aead_abort( &operation );
4569
Paul Elliott243080c2021-07-21 19:01:17 +01004570 /* Test for not sending any additional data after setting a non-zero length
4571 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004572
4573 operation = psa_aead_operation_init( );
4574
4575 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4576
4577 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4578
4579 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4580 input_data->len ) );
4581
4582 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4583 input_data->len, output_data,
4584 output_size, &output_length ),
4585 PSA_ERROR_INVALID_ARGUMENT );
4586
4587 psa_aead_abort( &operation );
4588
Paul Elliottf94bd992021-09-19 18:15:59 +01004589 /* Test for not sending any data after setting a non-zero length for it.*/
4590
4591 operation = psa_aead_operation_init( );
4592
4593 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4594
4595 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4596
4597 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4598 input_data->len ) );
4599
4600 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4601 additional_data->len ) );
4602
4603 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4604 finish_output_size,
4605 &output_part_length,
4606 tag_buffer, tag_length,
4607 &tag_size ),
4608 PSA_ERROR_INVALID_ARGUMENT );
4609
4610 psa_aead_abort( &operation );
4611
Paul Elliottb0450fe2021-09-01 15:06:26 +01004612 /* Test for sending too much additional data after setting lengths. */
4613
4614 operation = psa_aead_operation_init( );
4615
4616 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4617
4618 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4619
4620 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4621
4622
4623 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4624 additional_data->len ),
4625 PSA_ERROR_INVALID_ARGUMENT );
4626
4627 psa_aead_abort( &operation );
4628
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004629 operation = psa_aead_operation_init( );
4630
4631 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4632
4633 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4634
4635 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4636 input_data->len ) );
4637
4638 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4639 additional_data->len ) );
4640
4641 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4642 1 ),
4643 PSA_ERROR_INVALID_ARGUMENT );
4644
4645 psa_aead_abort( &operation );
4646
Paul Elliottb0450fe2021-09-01 15:06:26 +01004647 /* Test for sending too much data after setting lengths. */
4648
4649 operation = psa_aead_operation_init( );
4650
4651 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4652
4653 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4654
4655 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4656
4657 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4658 input_data->len, output_data,
4659 output_size, &output_length ),
4660 PSA_ERROR_INVALID_ARGUMENT );
4661
4662 psa_aead_abort( &operation );
4663
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004664 operation = psa_aead_operation_init( );
4665
4666 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4667
4668 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4669
4670 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4671 input_data->len ) );
4672
4673 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4674 additional_data->len ) );
4675
4676 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4677 input_data->len, output_data,
4678 output_size, &output_length ) );
4679
4680 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4681 1, output_data,
4682 output_size, &output_length ),
4683 PSA_ERROR_INVALID_ARGUMENT );
4684
4685 psa_aead_abort( &operation );
4686
Paul Elliottc23a9a02021-06-21 18:32:46 +01004687 /* Test sending additional data after data. */
4688
4689 operation = psa_aead_operation_init( );
4690
4691 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4692
4693 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4694
4695 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4696 input_data->len, output_data,
4697 output_size, &output_length ) );
4698
4699 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4700 additional_data->len ),
4701 PSA_ERROR_BAD_STATE );
4702
4703 psa_aead_abort( &operation );
4704
Paul Elliott534d0b42021-06-22 19:15:20 +01004705 /* Test calling finish on decryption. */
4706
4707 operation = psa_aead_operation_init( );
4708
4709 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4710
4711 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4712
4713 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4714 finish_output_size,
4715 &output_part_length,
4716 tag_buffer, tag_length,
4717 &tag_size ),
4718 PSA_ERROR_BAD_STATE );
4719
4720 psa_aead_abort( &operation );
4721
4722 /* Test calling verify on encryption. */
4723
4724 operation = psa_aead_operation_init( );
4725
4726 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4727
4728 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4729
4730 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4731 finish_output_size,
4732 &output_part_length,
4733 tag_buffer,
4734 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004735 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004736
4737 psa_aead_abort( &operation );
4738
4739
Paul Elliottc23a9a02021-06-21 18:32:46 +01004740exit:
4741 psa_destroy_key( key );
4742 psa_aead_abort( &operation );
4743 mbedtls_free( output_data );
4744 mbedtls_free( final_data );
4745 PSA_DONE( );
4746}
4747/* END_CASE */
4748
4749/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004750void signature_size( int type_arg,
4751 int bits,
4752 int alg_arg,
4753 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004754{
4755 psa_key_type_t type = type_arg;
4756 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004757 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004758
Gilles Peskinefe11b722018-12-18 00:24:04 +01004759 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004760
Gilles Peskinee59236f2018-01-27 23:32:46 +01004761exit:
4762 ;
4763}
4764/* END_CASE */
4765
4766/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004767void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4768 int alg_arg, data_t *input_data,
4769 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004770{
Ronald Cron5425a212020-08-04 14:58:35 +02004771 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004772 psa_key_type_t key_type = key_type_arg;
4773 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004774 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004775 unsigned char *signature = NULL;
4776 size_t signature_size;
4777 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004778 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004779
Gilles Peskine8817f612018-12-18 00:18:46 +01004780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004781
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004782 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004783 psa_set_key_algorithm( &attributes, alg );
4784 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004785
Gilles Peskine049c7532019-05-15 20:22:09 +02004786 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004787 &key ) );
4788 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004789 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004790
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004791 /* Allocate a buffer which has the size advertized by the
4792 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004793 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004794 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004795 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004796 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004797 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004798
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004799 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004800 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004801 input_data->x, input_data->len,
4802 signature, signature_size,
4803 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004804 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004805 ASSERT_COMPARE( output_data->x, output_data->len,
4806 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004807
4808exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004809 /*
4810 * Key attributes may have been returned by psa_get_key_attributes()
4811 * thus reset them as required.
4812 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004813 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004814
Ronald Cron5425a212020-08-04 14:58:35 +02004815 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004816 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004817 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004818}
4819/* END_CASE */
4820
4821/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004822void sign_hash_fail( int key_type_arg, data_t *key_data,
4823 int alg_arg, data_t *input_data,
4824 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004825{
Ronald Cron5425a212020-08-04 14:58:35 +02004826 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004827 psa_key_type_t key_type = key_type_arg;
4828 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004829 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004830 psa_status_t actual_status;
4831 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004832 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004833 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004834 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004835
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004836 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004837
Gilles Peskine8817f612018-12-18 00:18:46 +01004838 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004839
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004840 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004841 psa_set_key_algorithm( &attributes, alg );
4842 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004843
Gilles Peskine049c7532019-05-15 20:22:09 +02004844 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004845 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004846
Ronald Cron5425a212020-08-04 14:58:35 +02004847 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004848 input_data->x, input_data->len,
4849 signature, signature_size,
4850 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004851 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004852 /* The value of *signature_length is unspecified on error, but
4853 * whatever it is, it should be less than signature_size, so that
4854 * if the caller tries to read *signature_length bytes without
4855 * checking the error code then they don't overflow a buffer. */
4856 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004857
4858exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004859 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004860 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004861 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004862 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004863}
4864/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004865
4866/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004867void sign_verify_hash( int key_type_arg, data_t *key_data,
4868 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004869{
Ronald Cron5425a212020-08-04 14:58:35 +02004870 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004871 psa_key_type_t key_type = key_type_arg;
4872 psa_algorithm_t alg = alg_arg;
4873 size_t key_bits;
4874 unsigned char *signature = NULL;
4875 size_t signature_size;
4876 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004878
Gilles Peskine8817f612018-12-18 00:18:46 +01004879 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004880
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004881 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004882 psa_set_key_algorithm( &attributes, alg );
4883 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004884
Gilles Peskine049c7532019-05-15 20:22:09 +02004885 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004886 &key ) );
4887 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004888 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004889
4890 /* Allocate a buffer which has the size advertized by the
4891 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004892 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004893 key_bits, alg );
4894 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004895 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004896 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004897
4898 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004899 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004900 input_data->x, input_data->len,
4901 signature, signature_size,
4902 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004903 /* Check that the signature length looks sensible. */
4904 TEST_ASSERT( signature_length <= signature_size );
4905 TEST_ASSERT( signature_length > 0 );
4906
4907 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004908 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004909 input_data->x, input_data->len,
4910 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004911
4912 if( input_data->len != 0 )
4913 {
4914 /* Flip a bit in the input and verify that the signature is now
4915 * detected as invalid. Flip a bit at the beginning, not at the end,
4916 * because ECDSA may ignore the last few bits of the input. */
4917 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004918 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004919 input_data->x, input_data->len,
4920 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004921 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004922 }
4923
4924exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004925 /*
4926 * Key attributes may have been returned by psa_get_key_attributes()
4927 * thus reset them as required.
4928 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004929 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004930
Ronald Cron5425a212020-08-04 14:58:35 +02004931 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004932 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004933 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004934}
4935/* END_CASE */
4936
4937/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004938void verify_hash( int key_type_arg, data_t *key_data,
4939 int alg_arg, data_t *hash_data,
4940 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004941{
Ronald Cron5425a212020-08-04 14:58:35 +02004942 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004943 psa_key_type_t key_type = key_type_arg;
4944 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004945 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004946
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004947 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004948
Gilles Peskine8817f612018-12-18 00:18:46 +01004949 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004950
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004951 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004952 psa_set_key_algorithm( &attributes, alg );
4953 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004954
Gilles Peskine049c7532019-05-15 20:22:09 +02004955 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004956 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004957
Ronald Cron5425a212020-08-04 14:58:35 +02004958 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004959 hash_data->x, hash_data->len,
4960 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004961
itayzafrir5c753392018-05-08 11:18:38 +03004962exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004963 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004964 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004965 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004966}
4967/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004968
4969/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004970void verify_hash_fail( int key_type_arg, data_t *key_data,
4971 int alg_arg, data_t *hash_data,
4972 data_t *signature_data,
4973 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004974{
Ronald Cron5425a212020-08-04 14:58:35 +02004975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004976 psa_key_type_t key_type = key_type_arg;
4977 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004978 psa_status_t actual_status;
4979 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004981
Gilles Peskine8817f612018-12-18 00:18:46 +01004982 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004983
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004984 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004985 psa_set_key_algorithm( &attributes, alg );
4986 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004987
Gilles Peskine049c7532019-05-15 20:22:09 +02004988 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004989 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004990
Ronald Cron5425a212020-08-04 14:58:35 +02004991 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004992 hash_data->x, hash_data->len,
4993 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004994 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004995
4996exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004997 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004998 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004999 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005000}
5001/* END_CASE */
5002
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005003/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005004void sign_message_deterministic( int key_type_arg,
5005 data_t *key_data,
5006 int alg_arg,
5007 data_t *input_data,
5008 data_t *output_data )
5009{
5010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5011 psa_key_type_t key_type = key_type_arg;
5012 psa_algorithm_t alg = alg_arg;
5013 size_t key_bits;
5014 unsigned char *signature = NULL;
5015 size_t signature_size;
5016 size_t signature_length = 0xdeadbeef;
5017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5018
5019 PSA_ASSERT( psa_crypto_init( ) );
5020
5021 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5022 psa_set_key_algorithm( &attributes, alg );
5023 psa_set_key_type( &attributes, key_type );
5024
5025 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5026 &key ) );
5027 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5028 key_bits = psa_get_key_bits( &attributes );
5029
5030 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5031 TEST_ASSERT( signature_size != 0 );
5032 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5033 ASSERT_ALLOC( signature, signature_size );
5034
5035 PSA_ASSERT( psa_sign_message( key, alg,
5036 input_data->x, input_data->len,
5037 signature, signature_size,
5038 &signature_length ) );
5039
5040 ASSERT_COMPARE( output_data->x, output_data->len,
5041 signature, signature_length );
5042
5043exit:
5044 psa_reset_key_attributes( &attributes );
5045
5046 psa_destroy_key( key );
5047 mbedtls_free( signature );
5048 PSA_DONE( );
5049
5050}
5051/* END_CASE */
5052
5053/* BEGIN_CASE */
5054void sign_message_fail( int key_type_arg,
5055 data_t *key_data,
5056 int alg_arg,
5057 data_t *input_data,
5058 int signature_size_arg,
5059 int expected_status_arg )
5060{
5061 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5062 psa_key_type_t key_type = key_type_arg;
5063 psa_algorithm_t alg = alg_arg;
5064 size_t signature_size = signature_size_arg;
5065 psa_status_t actual_status;
5066 psa_status_t expected_status = expected_status_arg;
5067 unsigned char *signature = NULL;
5068 size_t signature_length = 0xdeadbeef;
5069 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5070
5071 ASSERT_ALLOC( signature, signature_size );
5072
5073 PSA_ASSERT( psa_crypto_init( ) );
5074
5075 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5076 psa_set_key_algorithm( &attributes, alg );
5077 psa_set_key_type( &attributes, key_type );
5078
5079 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5080 &key ) );
5081
5082 actual_status = psa_sign_message( key, alg,
5083 input_data->x, input_data->len,
5084 signature, signature_size,
5085 &signature_length );
5086 TEST_EQUAL( actual_status, expected_status );
5087 /* The value of *signature_length is unspecified on error, but
5088 * whatever it is, it should be less than signature_size, so that
5089 * if the caller tries to read *signature_length bytes without
5090 * checking the error code then they don't overflow a buffer. */
5091 TEST_ASSERT( signature_length <= signature_size );
5092
5093exit:
5094 psa_reset_key_attributes( &attributes );
5095 psa_destroy_key( key );
5096 mbedtls_free( signature );
5097 PSA_DONE( );
5098}
5099/* END_CASE */
5100
5101/* BEGIN_CASE */
5102void sign_verify_message( int key_type_arg,
5103 data_t *key_data,
5104 int alg_arg,
5105 data_t *input_data )
5106{
5107 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5108 psa_key_type_t key_type = key_type_arg;
5109 psa_algorithm_t alg = alg_arg;
5110 size_t key_bits;
5111 unsigned char *signature = NULL;
5112 size_t signature_size;
5113 size_t signature_length = 0xdeadbeef;
5114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5115
5116 PSA_ASSERT( psa_crypto_init( ) );
5117
5118 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5119 PSA_KEY_USAGE_VERIFY_MESSAGE );
5120 psa_set_key_algorithm( &attributes, alg );
5121 psa_set_key_type( &attributes, key_type );
5122
5123 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5124 &key ) );
5125 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5126 key_bits = psa_get_key_bits( &attributes );
5127
5128 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5129 TEST_ASSERT( signature_size != 0 );
5130 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5131 ASSERT_ALLOC( signature, signature_size );
5132
5133 PSA_ASSERT( psa_sign_message( key, alg,
5134 input_data->x, input_data->len,
5135 signature, signature_size,
5136 &signature_length ) );
5137 TEST_ASSERT( signature_length <= signature_size );
5138 TEST_ASSERT( signature_length > 0 );
5139
5140 PSA_ASSERT( psa_verify_message( key, alg,
5141 input_data->x, input_data->len,
5142 signature, signature_length ) );
5143
5144 if( input_data->len != 0 )
5145 {
5146 /* Flip a bit in the input and verify that the signature is now
5147 * detected as invalid. Flip a bit at the beginning, not at the end,
5148 * because ECDSA may ignore the last few bits of the input. */
5149 input_data->x[0] ^= 1;
5150 TEST_EQUAL( psa_verify_message( key, alg,
5151 input_data->x, input_data->len,
5152 signature, signature_length ),
5153 PSA_ERROR_INVALID_SIGNATURE );
5154 }
5155
5156exit:
5157 psa_reset_key_attributes( &attributes );
5158
5159 psa_destroy_key( key );
5160 mbedtls_free( signature );
5161 PSA_DONE( );
5162}
5163/* END_CASE */
5164
5165/* BEGIN_CASE */
5166void verify_message( int key_type_arg,
5167 data_t *key_data,
5168 int alg_arg,
5169 data_t *input_data,
5170 data_t *signature_data )
5171{
5172 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5173 psa_key_type_t key_type = key_type_arg;
5174 psa_algorithm_t alg = alg_arg;
5175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5176
5177 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5178
5179 PSA_ASSERT( psa_crypto_init( ) );
5180
5181 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5182 psa_set_key_algorithm( &attributes, alg );
5183 psa_set_key_type( &attributes, key_type );
5184
5185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5186 &key ) );
5187
5188 PSA_ASSERT( psa_verify_message( key, alg,
5189 input_data->x, input_data->len,
5190 signature_data->x, signature_data->len ) );
5191
5192exit:
5193 psa_reset_key_attributes( &attributes );
5194 psa_destroy_key( key );
5195 PSA_DONE( );
5196}
5197/* END_CASE */
5198
5199/* BEGIN_CASE */
5200void verify_message_fail( int key_type_arg,
5201 data_t *key_data,
5202 int alg_arg,
5203 data_t *hash_data,
5204 data_t *signature_data,
5205 int expected_status_arg )
5206{
5207 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5208 psa_key_type_t key_type = key_type_arg;
5209 psa_algorithm_t alg = alg_arg;
5210 psa_status_t actual_status;
5211 psa_status_t expected_status = expected_status_arg;
5212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5213
5214 PSA_ASSERT( psa_crypto_init( ) );
5215
5216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5217 psa_set_key_algorithm( &attributes, alg );
5218 psa_set_key_type( &attributes, key_type );
5219
5220 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5221 &key ) );
5222
5223 actual_status = psa_verify_message( key, alg,
5224 hash_data->x, hash_data->len,
5225 signature_data->x,
5226 signature_data->len );
5227 TEST_EQUAL( actual_status, expected_status );
5228
5229exit:
5230 psa_reset_key_attributes( &attributes );
5231 psa_destroy_key( key );
5232 PSA_DONE( );
5233}
5234/* END_CASE */
5235
5236/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005237void asymmetric_encrypt( int key_type_arg,
5238 data_t *key_data,
5239 int alg_arg,
5240 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005241 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005242 int expected_output_length_arg,
5243 int expected_status_arg )
5244{
Ronald Cron5425a212020-08-04 14:58:35 +02005245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005246 psa_key_type_t key_type = key_type_arg;
5247 psa_algorithm_t alg = alg_arg;
5248 size_t expected_output_length = expected_output_length_arg;
5249 size_t key_bits;
5250 unsigned char *output = NULL;
5251 size_t output_size;
5252 size_t output_length = ~0;
5253 psa_status_t actual_status;
5254 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005256
Gilles Peskine8817f612018-12-18 00:18:46 +01005257 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005258
Gilles Peskine656896e2018-06-29 19:12:28 +02005259 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5261 psa_set_key_algorithm( &attributes, alg );
5262 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005263 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005264 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005265
5266 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005267 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005268 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005269
Gilles Peskine656896e2018-06-29 19:12:28 +02005270 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005271 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005272 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005273
5274 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005275 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005276 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005277 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005278 output, output_size,
5279 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005280 TEST_EQUAL( actual_status, expected_status );
5281 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005282
Gilles Peskine68428122018-06-30 18:42:41 +02005283 /* If the label is empty, the test framework puts a non-null pointer
5284 * in label->x. Test that a null pointer works as well. */
5285 if( label->len == 0 )
5286 {
5287 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005288 if( output_size != 0 )
5289 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005290 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005291 input_data->x, input_data->len,
5292 NULL, label->len,
5293 output, output_size,
5294 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005295 TEST_EQUAL( actual_status, expected_status );
5296 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005297 }
5298
Gilles Peskine656896e2018-06-29 19:12:28 +02005299exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005300 /*
5301 * Key attributes may have been returned by psa_get_key_attributes()
5302 * thus reset them as required.
5303 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005304 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005305
Ronald Cron5425a212020-08-04 14:58:35 +02005306 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005307 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005308 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005309}
5310/* END_CASE */
5311
5312/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005313void asymmetric_encrypt_decrypt( int key_type_arg,
5314 data_t *key_data,
5315 int alg_arg,
5316 data_t *input_data,
5317 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005318{
Ronald Cron5425a212020-08-04 14:58:35 +02005319 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005320 psa_key_type_t key_type = key_type_arg;
5321 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005322 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005323 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005324 size_t output_size;
5325 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005326 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005327 size_t output2_size;
5328 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005330
Gilles Peskine8817f612018-12-18 00:18:46 +01005331 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005332
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005333 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5334 psa_set_key_algorithm( &attributes, alg );
5335 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005336
Gilles Peskine049c7532019-05-15 20:22:09 +02005337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005338 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005339
5340 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005341 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005342 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005343
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005344 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005345 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005346 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005347
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005348 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005349 TEST_ASSERT( output2_size <=
5350 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5351 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005352 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005353
Gilles Peskineeebd7382018-06-08 18:11:54 +02005354 /* We test encryption by checking that encrypt-then-decrypt gives back
5355 * the original plaintext because of the non-optional random
5356 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005357 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005358 input_data->x, input_data->len,
5359 label->x, label->len,
5360 output, output_size,
5361 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005362 /* We don't know what ciphertext length to expect, but check that
5363 * it looks sensible. */
5364 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005365
Ronald Cron5425a212020-08-04 14:58:35 +02005366 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005367 output, output_length,
5368 label->x, label->len,
5369 output2, output2_size,
5370 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005371 ASSERT_COMPARE( input_data->x, input_data->len,
5372 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005373
5374exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005375 /*
5376 * Key attributes may have been returned by psa_get_key_attributes()
5377 * thus reset them as required.
5378 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005379 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005380
Ronald Cron5425a212020-08-04 14:58:35 +02005381 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005382 mbedtls_free( output );
5383 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005384 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005385}
5386/* END_CASE */
5387
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005388/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005389void asymmetric_decrypt( int key_type_arg,
5390 data_t *key_data,
5391 int alg_arg,
5392 data_t *input_data,
5393 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005394 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005395{
Ronald Cron5425a212020-08-04 14:58:35 +02005396 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005397 psa_key_type_t key_type = key_type_arg;
5398 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005399 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005400 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005401 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005402 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005403 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005404
Gilles Peskine8817f612018-12-18 00:18:46 +01005405 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005406
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005407 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5408 psa_set_key_algorithm( &attributes, alg );
5409 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005410
Gilles Peskine049c7532019-05-15 20:22:09 +02005411 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005412 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005413
gabor-mezei-armceface22021-01-21 12:26:17 +01005414 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5415 key_bits = psa_get_key_bits( &attributes );
5416
5417 /* Determine the maximum ciphertext length */
5418 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5419 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5420 ASSERT_ALLOC( output, output_size );
5421
Ronald Cron5425a212020-08-04 14:58:35 +02005422 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005423 input_data->x, input_data->len,
5424 label->x, label->len,
5425 output,
5426 output_size,
5427 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005428 ASSERT_COMPARE( expected_data->x, expected_data->len,
5429 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005430
Gilles Peskine68428122018-06-30 18:42:41 +02005431 /* If the label is empty, the test framework puts a non-null pointer
5432 * in label->x. Test that a null pointer works as well. */
5433 if( label->len == 0 )
5434 {
5435 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005436 if( output_size != 0 )
5437 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005438 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005439 input_data->x, input_data->len,
5440 NULL, label->len,
5441 output,
5442 output_size,
5443 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005444 ASSERT_COMPARE( expected_data->x, expected_data->len,
5445 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005446 }
5447
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005448exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005449 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005450 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005451 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005452 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005453}
5454/* END_CASE */
5455
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005456/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005457void asymmetric_decrypt_fail( int key_type_arg,
5458 data_t *key_data,
5459 int alg_arg,
5460 data_t *input_data,
5461 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005462 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005463 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005464{
Ronald Cron5425a212020-08-04 14:58:35 +02005465 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005466 psa_key_type_t key_type = key_type_arg;
5467 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005468 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005469 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005470 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005471 psa_status_t actual_status;
5472 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005473 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005474
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005475 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005476
Gilles Peskine8817f612018-12-18 00:18:46 +01005477 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005478
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005479 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5480 psa_set_key_algorithm( &attributes, alg );
5481 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005482
Gilles Peskine049c7532019-05-15 20:22:09 +02005483 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005484 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005485
Ronald Cron5425a212020-08-04 14:58:35 +02005486 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005487 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005488 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005489 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005490 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005491 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005492 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005493
Gilles Peskine68428122018-06-30 18:42:41 +02005494 /* If the label is empty, the test framework puts a non-null pointer
5495 * in label->x. Test that a null pointer works as well. */
5496 if( label->len == 0 )
5497 {
5498 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005499 if( output_size != 0 )
5500 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005501 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005502 input_data->x, input_data->len,
5503 NULL, label->len,
5504 output, output_size,
5505 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005506 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005507 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005508 }
5509
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005510exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005511 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005512 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005513 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005514 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005515}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005516/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005517
5518/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005519void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005520{
5521 /* Test each valid way of initializing the object, except for `= {0}`, as
5522 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5523 * though it's OK by the C standard. We could test for this, but we'd need
5524 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005525 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005526 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5527 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5528 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005529
5530 memset( &zero, 0, sizeof( zero ) );
5531
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005532 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005533 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005534 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005535 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005536 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005537 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005538 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005539
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005540 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005541 PSA_ASSERT( psa_key_derivation_abort(&func) );
5542 PSA_ASSERT( psa_key_derivation_abort(&init) );
5543 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005544}
5545/* END_CASE */
5546
Janos Follath16de4a42019-06-13 16:32:24 +01005547/* BEGIN_CASE */
5548void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005549{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005550 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005551 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005552 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005553
Gilles Peskine8817f612018-12-18 00:18:46 +01005554 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005555
Janos Follath16de4a42019-06-13 16:32:24 +01005556 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005557 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005558
5559exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005560 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005561 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005562}
5563/* END_CASE */
5564
Janos Follathaf3c2a02019-06-12 12:34:34 +01005565/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005566void derive_set_capacity( int alg_arg, int capacity_arg,
5567 int expected_status_arg )
5568{
5569 psa_algorithm_t alg = alg_arg;
5570 size_t capacity = capacity_arg;
5571 psa_status_t expected_status = expected_status_arg;
5572 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5573
5574 PSA_ASSERT( psa_crypto_init( ) );
5575
5576 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5577
5578 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5579 expected_status );
5580
5581exit:
5582 psa_key_derivation_abort( &operation );
5583 PSA_DONE( );
5584}
5585/* END_CASE */
5586
5587/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005588void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005589 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005590 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005591 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005592 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005593 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005594 int expected_status_arg3,
5595 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005596{
5597 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005598 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5599 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005600 psa_status_t expected_statuses[] = {expected_status_arg1,
5601 expected_status_arg2,
5602 expected_status_arg3};
5603 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005604 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5605 MBEDTLS_SVC_KEY_ID_INIT,
5606 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005607 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5609 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005610 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005611 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005612 psa_status_t expected_output_status = expected_output_status_arg;
5613 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005614
5615 PSA_ASSERT( psa_crypto_init( ) );
5616
5617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5618 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005619
5620 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5621
5622 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5623 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005624 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005625 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005626 psa_set_key_type( &attributes, key_types[i] );
5627 PSA_ASSERT( psa_import_key( &attributes,
5628 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005629 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005630 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5631 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5632 {
5633 // When taking a private key as secret input, use key agreement
5634 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005635 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5636 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005637 expected_statuses[i] );
5638 }
5639 else
5640 {
5641 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005642 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005643 expected_statuses[i] );
5644 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005645 }
5646 else
5647 {
5648 TEST_EQUAL( psa_key_derivation_input_bytes(
5649 &operation, steps[i],
5650 inputs[i]->x, inputs[i]->len ),
5651 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005652 }
5653 }
5654
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005655 if( output_key_type != PSA_KEY_TYPE_NONE )
5656 {
5657 psa_reset_key_attributes( &attributes );
5658 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5659 psa_set_key_bits( &attributes, 8 );
5660 actual_output_status =
5661 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005662 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005663 }
5664 else
5665 {
5666 uint8_t buffer[1];
5667 actual_output_status =
5668 psa_key_derivation_output_bytes( &operation,
5669 buffer, sizeof( buffer ) );
5670 }
5671 TEST_EQUAL( actual_output_status, expected_output_status );
5672
Janos Follathaf3c2a02019-06-12 12:34:34 +01005673exit:
5674 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005675 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5676 psa_destroy_key( keys[i] );
5677 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005678 PSA_DONE( );
5679}
5680/* END_CASE */
5681
Janos Follathd958bb72019-07-03 15:02:16 +01005682/* BEGIN_CASE */
5683void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005684{
Janos Follathd958bb72019-07-03 15:02:16 +01005685 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005686 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005687 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005688 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005689 unsigned char input1[] = "Input 1";
5690 size_t input1_length = sizeof( input1 );
5691 unsigned char input2[] = "Input 2";
5692 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005693 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005694 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005695 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5696 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5697 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005699
Gilles Peskine8817f612018-12-18 00:18:46 +01005700 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005701
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005702 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5703 psa_set_key_algorithm( &attributes, alg );
5704 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005705
Gilles Peskine73676cb2019-05-15 20:15:10 +02005706 PSA_ASSERT( psa_import_key( &attributes,
5707 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005708 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005709
5710 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005711 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5712 input1, input1_length,
5713 input2, input2_length,
5714 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005715 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005716
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005717 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005718 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005719 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005720
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005721 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005722
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005724 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005725
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005726exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005727 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005728 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005729 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005730}
5731/* END_CASE */
5732
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005733/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005734void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005735{
5736 uint8_t output_buffer[16];
5737 size_t buffer_size = 16;
5738 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005739 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005740
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005741 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5742 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005743 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005744
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
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 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005749
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005750 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5751 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005752 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005753
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005754 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005755 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005756
5757exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005758 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005759}
5760/* END_CASE */
5761
5762/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005763void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005764 int step1_arg, data_t *input1,
5765 int step2_arg, data_t *input2,
5766 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005767 int requested_capacity_arg,
5768 data_t *expected_output1,
5769 data_t *expected_output2 )
5770{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005771 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005772 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5773 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005774 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5775 MBEDTLS_SVC_KEY_ID_INIT,
5776 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005777 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005778 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005779 uint8_t *expected_outputs[2] =
5780 {expected_output1->x, expected_output2->x};
5781 size_t output_sizes[2] =
5782 {expected_output1->len, expected_output2->len};
5783 size_t output_buffer_size = 0;
5784 uint8_t *output_buffer = NULL;
5785 size_t expected_capacity;
5786 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005788 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005789 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005790
5791 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5792 {
5793 if( output_sizes[i] > output_buffer_size )
5794 output_buffer_size = output_sizes[i];
5795 if( output_sizes[i] == 0 )
5796 expected_outputs[i] = NULL;
5797 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005798 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005799 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005800
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005801 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5802 psa_set_key_algorithm( &attributes, alg );
5803 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005804
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005805 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005806 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5807 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5808 requested_capacity ) );
5809 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005810 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005811 switch( steps[i] )
5812 {
5813 case 0:
5814 break;
5815 case PSA_KEY_DERIVATION_INPUT_SECRET:
5816 PSA_ASSERT( psa_import_key( &attributes,
5817 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005818 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005819
5820 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5821 {
5822 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5823 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5824 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5825 }
5826
Gilles Peskine1468da72019-05-29 17:35:49 +02005827 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005828 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005829 break;
5830 default:
5831 PSA_ASSERT( psa_key_derivation_input_bytes(
5832 &operation, steps[i],
5833 inputs[i]->x, inputs[i]->len ) );
5834 break;
5835 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005836 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005837
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005838 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005839 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005840 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005841 expected_capacity = requested_capacity;
5842
5843 /* Expansion phase. */
5844 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5845 {
5846 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005847 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005848 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005849 if( expected_capacity == 0 && output_sizes[i] == 0 )
5850 {
5851 /* Reading 0 bytes when 0 bytes are available can go either way. */
5852 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005853 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005854 continue;
5855 }
5856 else if( expected_capacity == 0 ||
5857 output_sizes[i] > expected_capacity )
5858 {
5859 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005860 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005861 expected_capacity = 0;
5862 continue;
5863 }
5864 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005865 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005866 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005867 ASSERT_COMPARE( output_buffer, output_sizes[i],
5868 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005869 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005870 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005871 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005872 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005873 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005874 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005875 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005876
5877exit:
5878 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005879 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005880 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5881 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005882 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005883}
5884/* END_CASE */
5885
5886/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005887void derive_full( int alg_arg,
5888 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005889 data_t *input1,
5890 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005891 int requested_capacity_arg )
5892{
Ronald Cron5425a212020-08-04 14:58:35 +02005893 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005894 psa_algorithm_t alg = alg_arg;
5895 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005896 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005897 unsigned char output_buffer[16];
5898 size_t expected_capacity = requested_capacity;
5899 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005900 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005901
Gilles Peskine8817f612018-12-18 00:18:46 +01005902 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005903
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005904 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5905 psa_set_key_algorithm( &attributes, alg );
5906 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005907
Gilles Peskine049c7532019-05-15 20:22:09 +02005908 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005909 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005910
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005911 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5912 input1->x, input1->len,
5913 input2->x, input2->len,
5914 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005915 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005916
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005917 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005918 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005919 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005920
5921 /* Expansion phase. */
5922 while( current_capacity > 0 )
5923 {
5924 size_t read_size = sizeof( output_buffer );
5925 if( read_size > current_capacity )
5926 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005927 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005928 output_buffer,
5929 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005930 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005931 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005932 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005933 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005934 }
5935
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005936 /* Check that the operation refuses to go over capacity. */
5937 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005938 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005939
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005940 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005941
5942exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005943 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005944 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005945 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005946}
5947/* END_CASE */
5948
Janos Follathe60c9052019-07-03 13:51:30 +01005949/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005950void derive_key_exercise( int alg_arg,
5951 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005952 data_t *input1,
5953 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005954 int derived_type_arg,
5955 int derived_bits_arg,
5956 int derived_usage_arg,
5957 int derived_alg_arg )
5958{
Ronald Cron5425a212020-08-04 14:58:35 +02005959 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5960 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005961 psa_algorithm_t alg = alg_arg;
5962 psa_key_type_t derived_type = derived_type_arg;
5963 size_t derived_bits = derived_bits_arg;
5964 psa_key_usage_t derived_usage = derived_usage_arg;
5965 psa_algorithm_t derived_alg = derived_alg_arg;
5966 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005967 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005968 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005969 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005970
Gilles Peskine8817f612018-12-18 00:18:46 +01005971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005972
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005973 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5974 psa_set_key_algorithm( &attributes, alg );
5975 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005976 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005977 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005978
5979 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005980 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5981 input1->x, input1->len,
5982 input2->x, input2->len,
5983 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005984 goto exit;
5985
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005986 psa_set_key_usage_flags( &attributes, derived_usage );
5987 psa_set_key_algorithm( &attributes, derived_alg );
5988 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005989 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005990 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005991 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005992
5993 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005994 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005995 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5996 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005997
5998 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005999 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006000 goto exit;
6001
6002exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006003 /*
6004 * Key attributes may have been returned by psa_get_key_attributes()
6005 * thus reset them as required.
6006 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006007 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006008
6009 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006010 psa_destroy_key( base_key );
6011 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006012 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006013}
6014/* END_CASE */
6015
Janos Follath42fd8882019-07-03 14:17:09 +01006016/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006017void derive_key_export( int alg_arg,
6018 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006019 data_t *input1,
6020 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006021 int bytes1_arg,
6022 int bytes2_arg )
6023{
Ronald Cron5425a212020-08-04 14:58:35 +02006024 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6025 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006026 psa_algorithm_t alg = alg_arg;
6027 size_t bytes1 = bytes1_arg;
6028 size_t bytes2 = bytes2_arg;
6029 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006030 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006031 uint8_t *output_buffer = NULL;
6032 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006033 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6034 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006035 size_t length;
6036
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006037 ASSERT_ALLOC( output_buffer, capacity );
6038 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006040
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006041 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6042 psa_set_key_algorithm( &base_attributes, alg );
6043 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006044 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006045 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006046
6047 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006048 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6049 input1->x, input1->len,
6050 input2->x, input2->len,
6051 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006052 goto exit;
6053
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006054 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006055 output_buffer,
6056 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006057 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006058
6059 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006060 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6061 input1->x, input1->len,
6062 input2->x, input2->len,
6063 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006064 goto exit;
6065
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006066 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6067 psa_set_key_algorithm( &derived_attributes, 0 );
6068 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006069 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006070 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006071 &derived_key ) );
6072 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006073 export_buffer, bytes1,
6074 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006075 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006076 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006077 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006078 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006079 &derived_key ) );
6080 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006081 export_buffer + bytes1, bytes2,
6082 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006083 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006084
6085 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006086 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6087 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006088
6089exit:
6090 mbedtls_free( output_buffer );
6091 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006092 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006093 psa_destroy_key( base_key );
6094 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006095 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006096}
6097/* END_CASE */
6098
6099/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006100void derive_key( int alg_arg,
6101 data_t *key_data, data_t *input1, data_t *input2,
6102 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006103 int expected_status_arg,
6104 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006105{
Ronald Cron5425a212020-08-04 14:58:35 +02006106 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6107 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006108 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006109 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006110 size_t bits = bits_arg;
6111 psa_status_t expected_status = expected_status_arg;
6112 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6113 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6114 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6115
6116 PSA_ASSERT( psa_crypto_init( ) );
6117
6118 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6119 psa_set_key_algorithm( &base_attributes, alg );
6120 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6121 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006122 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006123
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006124 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6125 input1->x, input1->len,
6126 input2->x, input2->len,
6127 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006128 goto exit;
6129
6130 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6131 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006132 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006133 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006134
6135 psa_status_t status =
6136 psa_key_derivation_output_key( &derived_attributes,
6137 &operation,
6138 &derived_key );
6139 if( is_large_output > 0 )
6140 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6141 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006142
6143exit:
6144 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006145 psa_destroy_key( base_key );
6146 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006147 PSA_DONE( );
6148}
6149/* END_CASE */
6150
6151/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006152void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006153 int our_key_type_arg, int our_key_alg_arg,
6154 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006155 int expected_status_arg )
6156{
Ronald Cron5425a212020-08-04 14:58:35 +02006157 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006158 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006159 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006160 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006161 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006163 psa_status_t expected_status = expected_status_arg;
6164 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006165
Gilles Peskine8817f612018-12-18 00:18:46 +01006166 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006167
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006168 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006169 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006170 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006171 PSA_ASSERT( psa_import_key( &attributes,
6172 our_key_data->x, our_key_data->len,
6173 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006174
Gilles Peskine77f40d82019-04-11 21:27:06 +02006175 /* The tests currently include inputs that should fail at either step.
6176 * Test cases that fail at the setup step should be changed to call
6177 * key_derivation_setup instead, and this function should be renamed
6178 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006179 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006180 if( status == PSA_SUCCESS )
6181 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006182 TEST_EQUAL( psa_key_derivation_key_agreement(
6183 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6184 our_key,
6185 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006186 expected_status );
6187 }
6188 else
6189 {
6190 TEST_ASSERT( status == expected_status );
6191 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006192
6193exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006194 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006195 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006196 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006197}
6198/* END_CASE */
6199
6200/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006201void raw_key_agreement( int alg_arg,
6202 int our_key_type_arg, data_t *our_key_data,
6203 data_t *peer_key_data,
6204 data_t *expected_output )
6205{
Ronald Cron5425a212020-08-04 14:58:35 +02006206 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006207 psa_algorithm_t alg = alg_arg;
6208 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006210 unsigned char *output = NULL;
6211 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006212 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006213
6214 ASSERT_ALLOC( output, expected_output->len );
6215 PSA_ASSERT( psa_crypto_init( ) );
6216
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006217 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6218 psa_set_key_algorithm( &attributes, alg );
6219 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006220 PSA_ASSERT( psa_import_key( &attributes,
6221 our_key_data->x, our_key_data->len,
6222 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006223
gabor-mezei-armceface22021-01-21 12:26:17 +01006224 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6225 key_bits = psa_get_key_bits( &attributes );
6226
Gilles Peskinebe697d82019-05-16 18:00:41 +02006227 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6228 peer_key_data->x, peer_key_data->len,
6229 output, expected_output->len,
6230 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006231 ASSERT_COMPARE( output, output_length,
6232 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006233 TEST_ASSERT( output_length <=
6234 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6235 TEST_ASSERT( output_length <=
6236 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006237
6238exit:
6239 mbedtls_free( output );
6240 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006241 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006242}
6243/* END_CASE */
6244
6245/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006246void key_agreement_capacity( int alg_arg,
6247 int our_key_type_arg, data_t *our_key_data,
6248 data_t *peer_key_data,
6249 int expected_capacity_arg )
6250{
Ronald Cron5425a212020-08-04 14:58:35 +02006251 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006252 psa_algorithm_t alg = alg_arg;
6253 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006254 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006256 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006257 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006258
Gilles Peskine8817f612018-12-18 00:18:46 +01006259 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006260
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006261 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6262 psa_set_key_algorithm( &attributes, alg );
6263 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006264 PSA_ASSERT( psa_import_key( &attributes,
6265 our_key_data->x, our_key_data->len,
6266 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006267
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006268 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006269 PSA_ASSERT( psa_key_derivation_key_agreement(
6270 &operation,
6271 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6272 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006273 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6274 {
6275 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006276 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006277 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006278 NULL, 0 ) );
6279 }
Gilles Peskine59685592018-09-18 12:11:34 +02006280
Gilles Peskinebf491972018-10-25 22:36:12 +02006281 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006282 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006283 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006284 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006285
Gilles Peskinebf491972018-10-25 22:36:12 +02006286 /* Test the actual capacity by reading the output. */
6287 while( actual_capacity > sizeof( output ) )
6288 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006289 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006290 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006291 actual_capacity -= sizeof( output );
6292 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006293 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006294 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006295 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006296 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006297
Gilles Peskine59685592018-09-18 12:11:34 +02006298exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006299 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006300 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006301 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006302}
6303/* END_CASE */
6304
6305/* BEGIN_CASE */
6306void key_agreement_output( int alg_arg,
6307 int our_key_type_arg, data_t *our_key_data,
6308 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006309 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006310{
Ronald Cron5425a212020-08-04 14:58:35 +02006311 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006312 psa_algorithm_t alg = alg_arg;
6313 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006314 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006316 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006317
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006318 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6319 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006320
Gilles Peskine8817f612018-12-18 00:18:46 +01006321 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006322
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006323 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6324 psa_set_key_algorithm( &attributes, alg );
6325 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006326 PSA_ASSERT( psa_import_key( &attributes,
6327 our_key_data->x, our_key_data->len,
6328 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006329
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006330 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006331 PSA_ASSERT( psa_key_derivation_key_agreement(
6332 &operation,
6333 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6334 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006335 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6336 {
6337 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006338 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006339 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006340 NULL, 0 ) );
6341 }
Gilles Peskine59685592018-09-18 12:11:34 +02006342
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006343 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006344 actual_output,
6345 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006346 ASSERT_COMPARE( actual_output, expected_output1->len,
6347 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006348 if( expected_output2->len != 0 )
6349 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006350 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006351 actual_output,
6352 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006353 ASSERT_COMPARE( actual_output, expected_output2->len,
6354 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006355 }
Gilles Peskine59685592018-09-18 12:11:34 +02006356
6357exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006358 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006359 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006360 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006361 mbedtls_free( actual_output );
6362}
6363/* END_CASE */
6364
6365/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006366void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006367{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006368 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006369 unsigned char *output = NULL;
6370 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006371 size_t i;
6372 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006373
Simon Butcher49f8e312020-03-03 15:51:50 +00006374 TEST_ASSERT( bytes_arg >= 0 );
6375
Gilles Peskine91892022021-02-08 19:50:26 +01006376 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006377 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006378
Gilles Peskine8817f612018-12-18 00:18:46 +01006379 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006380
Gilles Peskinea50d7392018-06-21 10:22:13 +02006381 /* Run several times, to ensure that every output byte will be
6382 * nonzero at least once with overwhelming probability
6383 * (2^(-8*number_of_runs)). */
6384 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006385 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006386 if( bytes != 0 )
6387 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006388 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006389
Gilles Peskinea50d7392018-06-21 10:22:13 +02006390 for( i = 0; i < bytes; i++ )
6391 {
6392 if( output[i] != 0 )
6393 ++changed[i];
6394 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006395 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006396
6397 /* Check that every byte was changed to nonzero at least once. This
6398 * validates that psa_generate_random is overwriting every byte of
6399 * the output buffer. */
6400 for( i = 0; i < bytes; i++ )
6401 {
6402 TEST_ASSERT( changed[i] != 0 );
6403 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006404
6405exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006406 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006407 mbedtls_free( output );
6408 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006409}
6410/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006411
6412/* BEGIN_CASE */
6413void generate_key( int type_arg,
6414 int bits_arg,
6415 int usage_arg,
6416 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006417 int expected_status_arg,
6418 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006419{
Ronald Cron5425a212020-08-04 14:58:35 +02006420 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006421 psa_key_type_t type = type_arg;
6422 psa_key_usage_t usage = usage_arg;
6423 size_t bits = bits_arg;
6424 psa_algorithm_t alg = alg_arg;
6425 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006426 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006427 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006428
Gilles Peskine8817f612018-12-18 00:18:46 +01006429 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006430
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006431 psa_set_key_usage_flags( &attributes, usage );
6432 psa_set_key_algorithm( &attributes, alg );
6433 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006434 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006435
6436 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006437 psa_status_t status = psa_generate_key( &attributes, &key );
6438
6439 if( is_large_key > 0 )
6440 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6441 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006442 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006443 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006444
6445 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006446 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006447 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6448 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006449
Gilles Peskine818ca122018-06-20 18:16:48 +02006450 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006451 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006452 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006453
6454exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006455 /*
6456 * Key attributes may have been returned by psa_get_key_attributes()
6457 * thus reset them as required.
6458 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006459 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006460
Ronald Cron5425a212020-08-04 14:58:35 +02006461 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006462 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006463}
6464/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006465
Ronald Cronee414c72021-03-18 18:50:08 +01006466/* 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 +02006467void generate_key_rsa( int bits_arg,
6468 data_t *e_arg,
6469 int expected_status_arg )
6470{
Ronald Cron5425a212020-08-04 14:58:35 +02006471 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006472 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006473 size_t bits = bits_arg;
6474 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6475 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6476 psa_status_t expected_status = expected_status_arg;
6477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6478 uint8_t *exported = NULL;
6479 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006480 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006481 size_t exported_length = SIZE_MAX;
6482 uint8_t *e_read_buffer = NULL;
6483 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006484 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006485 size_t e_read_length = SIZE_MAX;
6486
6487 if( e_arg->len == 0 ||
6488 ( e_arg->len == 3 &&
6489 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6490 {
6491 is_default_public_exponent = 1;
6492 e_read_size = 0;
6493 }
6494 ASSERT_ALLOC( e_read_buffer, e_read_size );
6495 ASSERT_ALLOC( exported, exported_size );
6496
6497 PSA_ASSERT( psa_crypto_init( ) );
6498
6499 psa_set_key_usage_flags( &attributes, usage );
6500 psa_set_key_algorithm( &attributes, alg );
6501 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6502 e_arg->x, e_arg->len ) );
6503 psa_set_key_bits( &attributes, bits );
6504
6505 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006506 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006507 if( expected_status != PSA_SUCCESS )
6508 goto exit;
6509
6510 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006511 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006512 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6513 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6514 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6515 e_read_buffer, e_read_size,
6516 &e_read_length ) );
6517 if( is_default_public_exponent )
6518 TEST_EQUAL( e_read_length, 0 );
6519 else
6520 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6521
6522 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006523 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006524 goto exit;
6525
6526 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006527 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006528 exported, exported_size,
6529 &exported_length ) );
6530 {
6531 uint8_t *p = exported;
6532 uint8_t *end = exported + exported_length;
6533 size_t len;
6534 /* RSAPublicKey ::= SEQUENCE {
6535 * modulus INTEGER, -- n
6536 * publicExponent INTEGER } -- e
6537 */
6538 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006539 MBEDTLS_ASN1_SEQUENCE |
6540 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006541 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006542 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6543 MBEDTLS_ASN1_INTEGER ) );
6544 if( len >= 1 && p[0] == 0 )
6545 {
6546 ++p;
6547 --len;
6548 }
6549 if( e_arg->len == 0 )
6550 {
6551 TEST_EQUAL( len, 3 );
6552 TEST_EQUAL( p[0], 1 );
6553 TEST_EQUAL( p[1], 0 );
6554 TEST_EQUAL( p[2], 1 );
6555 }
6556 else
6557 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6558 }
6559
6560exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006561 /*
6562 * Key attributes may have been returned by psa_get_key_attributes() or
6563 * set by psa_set_key_domain_parameters() thus reset them as required.
6564 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006565 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006566
Ronald Cron5425a212020-08-04 14:58:35 +02006567 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006568 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006569 mbedtls_free( e_read_buffer );
6570 mbedtls_free( exported );
6571}
6572/* END_CASE */
6573
Darryl Greend49a4992018-06-18 17:27:26 +01006574/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006575void persistent_key_load_key_from_storage( data_t *data,
6576 int type_arg, int bits_arg,
6577 int usage_flags_arg, int alg_arg,
6578 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006579{
Ronald Cron71016a92020-08-28 19:01:50 +02006580 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006581 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006582 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6583 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006584 psa_key_type_t type = type_arg;
6585 size_t bits = bits_arg;
6586 psa_key_usage_t usage_flags = usage_flags_arg;
6587 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006588 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006589 unsigned char *first_export = NULL;
6590 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006591 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006592 size_t first_exported_length;
6593 size_t second_exported_length;
6594
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006595 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6596 {
6597 ASSERT_ALLOC( first_export, export_size );
6598 ASSERT_ALLOC( second_export, export_size );
6599 }
Darryl Greend49a4992018-06-18 17:27:26 +01006600
Gilles Peskine8817f612018-12-18 00:18:46 +01006601 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006602
Gilles Peskinec87af662019-05-15 16:12:22 +02006603 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006604 psa_set_key_usage_flags( &attributes, usage_flags );
6605 psa_set_key_algorithm( &attributes, alg );
6606 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006607 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006608
Darryl Green0c6575a2018-11-07 16:05:30 +00006609 switch( generation_method )
6610 {
6611 case IMPORT_KEY:
6612 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006613 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006614 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006615 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006616
Darryl Green0c6575a2018-11-07 16:05:30 +00006617 case GENERATE_KEY:
6618 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006619 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006620 break;
6621
6622 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006623#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006624 {
6625 /* Create base key */
6626 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6627 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6628 psa_set_key_usage_flags( &base_attributes,
6629 PSA_KEY_USAGE_DERIVE );
6630 psa_set_key_algorithm( &base_attributes, derive_alg );
6631 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006632 PSA_ASSERT( psa_import_key( &base_attributes,
6633 data->x, data->len,
6634 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006635 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006636 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006637 PSA_ASSERT( psa_key_derivation_input_key(
6638 &operation,
6639 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006640 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006641 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006642 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006643 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6644 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006645 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006646 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006647 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006648 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006649 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006650#else
6651 TEST_ASSUME( ! "KDF not supported in this configuration" );
6652#endif
6653 break;
6654
6655 default:
6656 TEST_ASSERT( ! "generation_method not implemented in test" );
6657 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006658 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006659 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006660
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006661 /* Export the key if permitted by the key policy. */
6662 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6663 {
Ronald Cron5425a212020-08-04 14:58:35 +02006664 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006665 first_export, export_size,
6666 &first_exported_length ) );
6667 if( generation_method == IMPORT_KEY )
6668 ASSERT_COMPARE( data->x, data->len,
6669 first_export, first_exported_length );
6670 }
Darryl Greend49a4992018-06-18 17:27:26 +01006671
6672 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006673 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006674 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006675 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006676
Darryl Greend49a4992018-06-18 17:27:26 +01006677 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006678 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006679 TEST_ASSERT( mbedtls_svc_key_id_equal(
6680 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006681 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6682 PSA_KEY_LIFETIME_PERSISTENT );
6683 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6684 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6685 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6686 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006687
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006688 /* Export the key again if permitted by the key policy. */
6689 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006690 {
Ronald Cron5425a212020-08-04 14:58:35 +02006691 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006692 second_export, export_size,
6693 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006694 ASSERT_COMPARE( first_export, first_exported_length,
6695 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006696 }
6697
6698 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006699 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006700 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006701
6702exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006703 /*
6704 * Key attributes may have been returned by psa_get_key_attributes()
6705 * thus reset them as required.
6706 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006707 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006708
Darryl Greend49a4992018-06-18 17:27:26 +01006709 mbedtls_free( first_export );
6710 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006711 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006712 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006713 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006714 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006715}
6716/* END_CASE */