blob: 2feadf800e7891a76ae144e9176e1bcfde48f89c [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Paul Elliott33746aa2021-09-15 16:40:40 +0100267typedef enum
268{
269 DO_NOT_SET_LENGTHS = 0,
270 SET_LENGTHS_BEFORE_NONCE = 1,
271 SET_LENGTHS_AFTER_NONCE = 2
272} setlengths_method;
273
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100274typedef enum
275{
276 USE_NULL_TAG = 0,
277 USE_GIVEN_TAG = 1,
278} tagusage_method;
279
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100280/*!
281 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100282 * \param key_type_arg Type of key passed in
283 * \param key_data The encryption / decryption key data
284 * \param alg_arg The type of algorithm used
285 * \param nonce Nonce data
286 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100287 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100288 * feed additional data in to be encrypted /
289 * decrypted. If -1, no chunking.
290 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100291 * \param data_part_len_arg If not -1, the length of chunks to feed
292 * the data in to be encrypted / decrypted. If
293 * -1, no chunking
294 * \param set_lengths_method A member of the setlengths_method enum is
295 * expected here, this controls whether or not
296 * to set lengths, and in what order with
297 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100298 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100299 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100300 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100301 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100302 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 */
304static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
305 int alg_arg,
306 data_t *nonce,
307 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100308 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100309 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100310 int data_part_len_arg,
Paul Elliott33746aa2021-09-15 16:40:40 +0100311 setlengths_method set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100312 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100313 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100314 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100315{
316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
317 psa_key_type_t key_type = key_type_arg;
318 psa_algorithm_t alg = alg_arg;
319 psa_aead_operation_t operation;
320 unsigned char *output_data = NULL;
321 unsigned char *part_data = NULL;
322 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100324 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 size_t output_size = 0;
326 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100327 size_t output_length = 0;
328 size_t key_bits = 0;
329 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100330 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100331 size_t part_length = 0;
332 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100333 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100334 size_t ad_part_len = 0;
335 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
338 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
339
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100341 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342
Paul Elliottd3f82412021-06-16 16:52:21 +0100343 PSA_ASSERT( psa_crypto_init( ) );
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 if( is_encrypt )
346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
347 else
348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
349
Paul Elliottd3f82412021-06-16 16:52:21 +0100350 psa_set_key_algorithm( &attributes, alg );
351 psa_set_key_type( &attributes, key_type );
352
353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
354 &key ) );
355
356 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
357 key_bits = psa_get_key_bits( &attributes );
358
359 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 if( is_encrypt )
362 {
363 /* Tag gets written at end of buffer. */
364 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
365 ( input_data->len +
366 tag_length ) );
367 data_true_size = input_data->len;
368 }
369 else
370 {
371 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
372 ( input_data->len -
373 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 /* Do not want to attempt to decrypt tag. */
376 data_true_size = input_data->len - tag_length;
377 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100378
379 ASSERT_ALLOC( output_data, output_size );
380
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 if( is_encrypt )
382 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100383 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
384 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385 }
386 else
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100391
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 operation = psa_aead_operation_init( );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396
397 if( is_encrypt )
398 status = psa_aead_encrypt_setup( &operation, key, alg );
399 else
400 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100401
402 /* If the operation is not supported, just skip and not fail in case the
403 * encryption involves a common limitation of cryptography hardwares and
404 * an alternative implementation. */
405 if( status == PSA_ERROR_NOT_SUPPORTED )
406 {
407 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
408 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
409 }
410
411 PSA_ASSERT( status );
412
Paul Elliott33746aa2021-09-15 16:40:40 +0100413 if( set_lengths_method == DO_NOT_SET_LENGTHS )
414 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
415 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100416 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100417 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
418 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100421 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100422 {
423 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
426 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100427 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100428
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100429 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100430 {
431 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100432 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100434 for( part_offset = 0, part_count = 0;
435 part_offset < additional_data->len;
436 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100437 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100438 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 {
Paul Elliott329d5382021-07-22 17:10:45 +0100440 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100441 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100442 else if( additional_data->len - part_offset < ad_part_len )
443 {
444 part_length = additional_data->len - part_offset;
445 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 else
447 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100448 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 }
450
451 PSA_ASSERT( psa_aead_update_ad( &operation,
452 additional_data->x + part_offset,
453 part_length ) );
454
Paul Elliottd3f82412021-06-16 16:52:21 +0100455 }
456 }
457 else
458 {
459 /* Pass additional data in one go. */
460 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
461 additional_data->len ) );
462 }
463
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100464 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 {
466 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100467 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100468 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100469 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100470
471 ASSERT_ALLOC( part_data, part_data_size );
472
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100473 for( part_offset = 0, part_count = 0;
474 part_offset < data_true_size;
475 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100477 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 {
Paul Elliott329d5382021-07-22 17:10:45 +0100479 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100481 else if( ( data_true_size - part_offset ) < data_part_len )
482 {
483 part_length = ( data_true_size - part_offset );
484 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 else
486 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100487 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 }
489
490 PSA_ASSERT( psa_aead_update( &operation,
491 ( input_data->x + part_offset ),
492 part_length, part_data,
493 part_data_size,
494 &output_part_length ) );
495
496 if( output_data && output_part_length )
497 {
498 memcpy( ( output_data + part_offset ), part_data,
499 output_part_length );
500 }
501
Paul Elliottd3f82412021-06-16 16:52:21 +0100502 output_length += output_part_length;
503 }
504 }
505 else
506 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100507 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100508 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100509 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100510 output_size, &output_length ) );
511 }
512
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100513 if( is_encrypt )
514 PSA_ASSERT( psa_aead_finish( &operation, final_data,
515 final_output_size,
516 &output_part_length,
517 tag_buffer, tag_length,
518 &tag_size ) );
519 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 {
Paul Elliott9961a662021-09-17 19:19:02 +0100521 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100522 final_output_size,
523 &output_part_length,
524 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100525 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100526 }
527
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100528 if( output_data && output_part_length )
529 memcpy( ( output_data + output_length ), final_data,
530 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100531
532 output_length += output_part_length;
533
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534
535 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
536 * should be exact.*/
537 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100539 TEST_EQUAL( tag_length, tag_size );
540
541 if( output_data && tag_length )
542 memcpy( ( output_data + output_length ), tag_buffer,
543 tag_length );
544
545 output_length += tag_length;
546
547 TEST_EQUAL( output_length,
548 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
549 input_data->len ) );
550 TEST_ASSERT( output_length <=
551 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
552 }
553 else
554 {
555 TEST_EQUAL( output_length,
556 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
557 input_data->len ) );
558 TEST_ASSERT( output_length <=
559 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100560 }
561
Paul Elliottd3f82412021-06-16 16:52:21 +0100562
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100563 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100564 output_data, output_length );
565
Paul Elliottd3f82412021-06-16 16:52:21 +0100566
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100567 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100568
569exit:
570 psa_destroy_key( key );
571 psa_aead_abort( &operation );
572 mbedtls_free( output_data );
573 mbedtls_free( part_data );
574 mbedtls_free( final_data );
575 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100576
577 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100578}
579
Gilles Peskinee59236f2018-01-27 23:32:46 +0100580/* END_HEADER */
581
582/* BEGIN_DEPENDENCIES
583 * depends_on:MBEDTLS_PSA_CRYPTO_C
584 * END_DEPENDENCIES
585 */
586
587/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200588void static_checks( )
589{
590 size_t max_truncated_mac_size =
591 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
592
593 /* Check that the length for a truncated MAC always fits in the algorithm
594 * encoding. The shifted mask is the maximum truncated value. The
595 * untruncated algorithm may be one byte larger. */
596 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
597}
598/* END_CASE */
599
600/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200601void import_with_policy( int type_arg,
602 int usage_arg, int alg_arg,
603 int expected_status_arg )
604{
605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
606 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200608 psa_key_type_t type = type_arg;
609 psa_key_usage_t usage = usage_arg;
610 psa_algorithm_t alg = alg_arg;
611 psa_status_t expected_status = expected_status_arg;
612 const uint8_t key_material[16] = {0};
613 psa_status_t status;
614
615 PSA_ASSERT( psa_crypto_init( ) );
616
617 psa_set_key_type( &attributes, type );
618 psa_set_key_usage_flags( &attributes, usage );
619 psa_set_key_algorithm( &attributes, alg );
620
621 status = psa_import_key( &attributes,
622 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200623 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200624 TEST_EQUAL( status, expected_status );
625 if( status != PSA_SUCCESS )
626 goto exit;
627
Ronald Cron5425a212020-08-04 14:58:35 +0200628 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200629 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
630 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
631 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200632 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200633
Ronald Cron5425a212020-08-04 14:58:35 +0200634 PSA_ASSERT( psa_destroy_key( key ) );
635 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636
637exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100638 /*
639 * Key attributes may have been returned by psa_get_key_attributes()
640 * thus reset them as required.
641 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200642 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100643
644 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200645 PSA_DONE( );
646}
647/* END_CASE */
648
649/* BEGIN_CASE */
650void import_with_data( data_t *data, int type_arg,
651 int attr_bits_arg,
652 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200653{
654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
655 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200657 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200658 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200659 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661
Gilles Peskine8817f612018-12-18 00:18:46 +0100662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663
Gilles Peskine4747d192019-04-17 15:05:45 +0200664 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200665 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200666
Ronald Cron5425a212020-08-04 14:58:35 +0200667 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100668 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200669 if( status != PSA_SUCCESS )
670 goto exit;
671
Ronald Cron5425a212020-08-04 14:58:35 +0200672 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200673 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200674 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200675 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200676 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200677
Ronald Cron5425a212020-08-04 14:58:35 +0200678 PSA_ASSERT( psa_destroy_key( key ) );
679 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680
681exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100682 /*
683 * Key attributes may have been returned by psa_get_key_attributes()
684 * thus reset them as required.
685 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200686 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100687
688 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200689 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690}
691/* END_CASE */
692
693/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200694void import_large_key( int type_arg, int byte_size_arg,
695 int expected_status_arg )
696{
697 psa_key_type_t type = type_arg;
698 size_t byte_size = byte_size_arg;
699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
700 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200701 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200702 psa_status_t status;
703 uint8_t *buffer = NULL;
704 size_t buffer_size = byte_size + 1;
705 size_t n;
706
Steven Cooreman69967ce2021-01-18 18:01:08 +0100707 /* Skip the test case if the target running the test cannot
708 * accomodate large keys due to heap size constraints */
709 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200710 memset( buffer, 'K', byte_size );
711
712 PSA_ASSERT( psa_crypto_init( ) );
713
714 /* Try importing the key */
715 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
716 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200717 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100718 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200719 TEST_EQUAL( status, expected_status );
720
721 if( status == PSA_SUCCESS )
722 {
Ronald Cron5425a212020-08-04 14:58:35 +0200723 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200724 TEST_EQUAL( psa_get_key_type( &attributes ), type );
725 TEST_EQUAL( psa_get_key_bits( &attributes ),
726 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200727 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200728 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200729 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200730 for( n = 0; n < byte_size; n++ )
731 TEST_EQUAL( buffer[n], 'K' );
732 for( n = byte_size; n < buffer_size; n++ )
733 TEST_EQUAL( buffer[n], 0 );
734 }
735
736exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100737 /*
738 * Key attributes may have been returned by psa_get_key_attributes()
739 * thus reset them as required.
740 */
741 psa_reset_key_attributes( &attributes );
742
Ronald Cron5425a212020-08-04 14:58:35 +0200743 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200744 PSA_DONE( );
745 mbedtls_free( buffer );
746}
747/* END_CASE */
748
749/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200750void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
751{
Ronald Cron5425a212020-08-04 14:58:35 +0200752 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200753 size_t bits = bits_arg;
754 psa_status_t expected_status = expected_status_arg;
755 psa_status_t status;
756 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200757 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200758 size_t buffer_size = /* Slight overapproximations */
759 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200760 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200761 unsigned char *p;
762 int ret;
763 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200765
Gilles Peskine8817f612018-12-18 00:18:46 +0100766 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200767 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200768
769 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
770 bits, keypair ) ) >= 0 );
771 length = ret;
772
773 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200774 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200775 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100776 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200777
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200779 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200780
781exit:
782 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200783 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200784}
785/* END_CASE */
786
787/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300788void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300789 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200790 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100791 int expected_bits,
792 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200793 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100794 int canonical_input )
795{
Ronald Cron5425a212020-08-04 14:58:35 +0200796 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200798 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200799 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801 unsigned char *exported = NULL;
802 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100804 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200807 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808
Moran Pekercb088e72018-07-17 17:36:59 +0300809 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200810 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200812 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814
Gilles Peskine4747d192019-04-17 15:05:45 +0200815 psa_set_key_usage_flags( &attributes, usage_arg );
816 psa_set_key_algorithm( &attributes, alg );
817 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700818
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200820 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821
822 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200823 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200824 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
825 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200826 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100827
828 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200829 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100830 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100831
832 /* The exported length must be set by psa_export_key() to a value between 0
833 * and export_size. On errors, the exported length must be 0. */
834 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
835 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
836 TEST_ASSERT( exported_length <= export_size );
837
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200838 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200839 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200841 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100842 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200844 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845
Gilles Peskineea38a922021-02-13 00:05:16 +0100846 /* Run sanity checks on the exported key. For non-canonical inputs,
847 * this validates the canonical representations. For canonical inputs,
848 * this doesn't directly validate the implementation, but it still helps
849 * by cross-validating the test data with the sanity check code. */
850 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200851 goto exit;
852
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100853 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200854 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 else
856 {
Ronald Cron5425a212020-08-04 14:58:35 +0200857 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200858 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200859 &key2 ) );
860 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100861 reexported,
862 export_size,
863 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200864 ASSERT_COMPARE( exported, exported_length,
865 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200866 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100868 TEST_ASSERT( exported_length <=
869 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
870 psa_get_key_bits( &got_attributes ) ) );
871 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872
873destroy:
874 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200875 PSA_ASSERT( psa_destroy_key( key ) );
876 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877
878exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100879 /*
880 * Key attributes may have been returned by psa_get_key_attributes()
881 * thus reset them as required.
882 */
883 psa_reset_key_attributes( &got_attributes );
884
itayzafrir3e02b3b2018-06-12 17:06:52 +0300885 mbedtls_free( exported );
886 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200887 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888}
889/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100890
Moran Pekerf709f4a2018-06-06 17:26:04 +0300891/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300892void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200893 int type_arg,
894 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100895 int export_size_delta,
896 int expected_export_status_arg,
897 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300898{
Ronald Cron5425a212020-08-04 14:58:35 +0200899 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300900 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200901 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200902 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300903 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300904 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100905 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100906 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300908
Gilles Peskine8817f612018-12-18 00:18:46 +0100909 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910
Gilles Peskine4747d192019-04-17 15:05:45 +0200911 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
912 psa_set_key_algorithm( &attributes, alg );
913 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300914
915 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200916 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300917
Gilles Peskine49c25912018-10-29 15:15:31 +0100918 /* Export the public key */
919 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200920 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200921 exported, export_size,
922 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100923 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100924 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100925 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200926 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100927 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200928 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200929 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100930 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100931 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100932 TEST_ASSERT( expected_public_key->len <=
933 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
934 TEST_ASSERT( expected_public_key->len <=
935 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100936 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
937 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100938 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300939
940exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100941 /*
942 * Key attributes may have been returned by psa_get_key_attributes()
943 * thus reset them as required.
944 */
945 psa_reset_key_attributes( &attributes );
946
itayzafrir3e02b3b2018-06-12 17:06:52 +0300947 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200948 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200949 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300950}
951/* END_CASE */
952
Gilles Peskine20035e32018-02-03 22:44:14 +0100953/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200954void import_and_exercise_key( data_t *data,
955 int type_arg,
956 int bits_arg,
957 int alg_arg )
958{
Ronald Cron5425a212020-08-04 14:58:35 +0200959 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200960 psa_key_type_t type = type_arg;
961 size_t bits = bits_arg;
962 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100963 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200965 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200966
Gilles Peskine8817f612018-12-18 00:18:46 +0100967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200968
Gilles Peskine4747d192019-04-17 15:05:45 +0200969 psa_set_key_usage_flags( &attributes, usage );
970 psa_set_key_algorithm( &attributes, alg );
971 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200972
973 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200974 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975
976 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200977 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200978 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
979 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980
981 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100982 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200983 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200984
Ronald Cron5425a212020-08-04 14:58:35 +0200985 PSA_ASSERT( psa_destroy_key( key ) );
986 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200987
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200988exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100989 /*
990 * Key attributes may have been returned by psa_get_key_attributes()
991 * thus reset them as required.
992 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200993 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100994
995 psa_reset_key_attributes( &attributes );
996 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200997 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200998}
999/* END_CASE */
1000
1001/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001002void effective_key_attributes( int type_arg, int expected_type_arg,
1003 int bits_arg, int expected_bits_arg,
1004 int usage_arg, int expected_usage_arg,
1005 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001006{
Ronald Cron5425a212020-08-04 14:58:35 +02001007 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001008 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001009 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001010 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001011 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001012 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001013 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001014 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001015 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001017
Gilles Peskine8817f612018-12-18 00:18:46 +01001018 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001019
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001020 psa_set_key_usage_flags( &attributes, usage );
1021 psa_set_key_algorithm( &attributes, alg );
1022 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001023 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001024
Ronald Cron5425a212020-08-04 14:58:35 +02001025 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001026 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001027
Ronald Cron5425a212020-08-04 14:58:35 +02001028 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001029 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1030 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1031 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1032 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001033
1034exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001035 /*
1036 * Key attributes may have been returned by psa_get_key_attributes()
1037 * thus reset them as required.
1038 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001039 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001040
1041 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001042 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001043}
1044/* END_CASE */
1045
1046/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001047void check_key_policy( int type_arg, int bits_arg,
1048 int usage_arg, int alg_arg )
1049{
1050 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1051 usage_arg, usage_arg, alg_arg, alg_arg );
1052 goto exit;
1053}
1054/* END_CASE */
1055
1056/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001057void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001058{
1059 /* Test each valid way of initializing the object, except for `= {0}`, as
1060 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1061 * though it's OK by the C standard. We could test for this, but we'd need
1062 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001063 psa_key_attributes_t func = psa_key_attributes_init( );
1064 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1065 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001066
1067 memset( &zero, 0, sizeof( zero ) );
1068
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001069 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1070 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1071 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001072
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001073 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1074 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1075 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1076
1077 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1078 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1079 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1080
1081 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1082 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1083 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1084
1085 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1086 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1087 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001088}
1089/* END_CASE */
1090
1091/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001092void mac_key_policy( int policy_usage,
1093 int policy_alg,
1094 int key_type,
1095 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001096 int exercise_alg,
1097 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001098{
Ronald Cron5425a212020-08-04 14:58:35 +02001099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001101 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001102 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001103 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001104 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001105
Gilles Peskine8817f612018-12-18 00:18:46 +01001106 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001107
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001108 psa_set_key_usage_flags( &attributes, policy_usage );
1109 psa_set_key_algorithm( &attributes, policy_alg );
1110 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001111
Gilles Peskine049c7532019-05-15 20:22:09 +02001112 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001113 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001114
Ronald Cron5425a212020-08-04 14:58:35 +02001115 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001116 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001117 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001118 else
1119 TEST_EQUAL( status, expected_status );
1120
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001121 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001122
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001123 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001124 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001125 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001126 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001127 else
1128 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001129
1130exit:
1131 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001132 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001133 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001134}
1135/* END_CASE */
1136
1137/* BEGIN_CASE */
1138void cipher_key_policy( int policy_usage,
1139 int policy_alg,
1140 int key_type,
1141 data_t *key_data,
1142 int exercise_alg )
1143{
Ronald Cron5425a212020-08-04 14:58:35 +02001144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001146 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001147 psa_status_t status;
1148
Gilles Peskine8817f612018-12-18 00:18:46 +01001149 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001150
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001151 psa_set_key_usage_flags( &attributes, policy_usage );
1152 psa_set_key_algorithm( &attributes, policy_alg );
1153 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001154
Gilles Peskine049c7532019-05-15 20:22:09 +02001155 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001156 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001157
Ronald Cron5425a212020-08-04 14:58:35 +02001158 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159 if( policy_alg == exercise_alg &&
1160 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001162 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001163 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164 psa_cipher_abort( &operation );
1165
Ronald Cron5425a212020-08-04 14:58:35 +02001166 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 if( policy_alg == exercise_alg &&
1168 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001169 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001170 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001171 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
1173exit:
1174 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001176 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177}
1178/* END_CASE */
1179
1180/* BEGIN_CASE */
1181void aead_key_policy( int policy_usage,
1182 int policy_alg,
1183 int key_type,
1184 data_t *key_data,
1185 int nonce_length_arg,
1186 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001187 int exercise_alg,
1188 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001189{
Ronald Cron5425a212020-08-04 14:58:35 +02001190 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001192 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001193 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194 unsigned char nonce[16] = {0};
1195 size_t nonce_length = nonce_length_arg;
1196 unsigned char tag[16];
1197 size_t tag_length = tag_length_arg;
1198 size_t output_length;
1199
1200 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1201 TEST_ASSERT( tag_length <= sizeof( tag ) );
1202
Gilles Peskine8817f612018-12-18 00:18:46 +01001203 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001205 psa_set_key_usage_flags( &attributes, policy_usage );
1206 psa_set_key_algorithm( &attributes, policy_alg );
1207 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208
Gilles Peskine049c7532019-05-15 20:22:09 +02001209 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001210 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211
Ronald Cron5425a212020-08-04 14:58:35 +02001212 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213 nonce, nonce_length,
1214 NULL, 0,
1215 NULL, 0,
1216 tag, tag_length,
1217 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001218 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1219 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001221 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222
1223 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001224 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225 nonce, nonce_length,
1226 NULL, 0,
1227 tag, tag_length,
1228 NULL, 0,
1229 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001230 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1231 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1232 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001233 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001234 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001235 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001236
1237exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001239 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
1244void asymmetric_encryption_key_policy( int policy_usage,
1245 int policy_alg,
1246 int key_type,
1247 data_t *key_data,
1248 int exercise_alg )
1249{
Ronald Cron5425a212020-08-04 14:58:35 +02001250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252 psa_status_t status;
1253 size_t key_bits;
1254 size_t buffer_length;
1255 unsigned char *buffer = NULL;
1256 size_t output_length;
1257
Gilles Peskine8817f612018-12-18 00:18:46 +01001258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001260 psa_set_key_usage_flags( &attributes, policy_usage );
1261 psa_set_key_algorithm( &attributes, policy_alg );
1262 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001263
Gilles Peskine049c7532019-05-15 20:22:09 +02001264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001265 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266
Ronald Cron5425a212020-08-04 14:58:35 +02001267 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001268 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001269 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1270 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001271 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001272
Ronald Cron5425a212020-08-04 14:58:35 +02001273 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001274 NULL, 0,
1275 NULL, 0,
1276 buffer, buffer_length,
1277 &output_length );
1278 if( policy_alg == exercise_alg &&
1279 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001280 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001281 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001282 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001283
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001284 if( buffer_length != 0 )
1285 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001286 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 buffer, buffer_length,
1288 NULL, 0,
1289 buffer, buffer_length,
1290 &output_length );
1291 if( policy_alg == exercise_alg &&
1292 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001293 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001294 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001295 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296
1297exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001298 /*
1299 * Key attributes may have been returned by psa_get_key_attributes()
1300 * thus reset them as required.
1301 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001302 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001303
1304 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001305 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001306 mbedtls_free( buffer );
1307}
1308/* END_CASE */
1309
1310/* BEGIN_CASE */
1311void asymmetric_signature_key_policy( int policy_usage,
1312 int policy_alg,
1313 int key_type,
1314 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001315 int exercise_alg,
1316 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317{
Ronald Cron5425a212020-08-04 14:58:35 +02001318 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001321 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1322 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1323 * compatible with the policy and `payload_length_arg` is supposed to be
1324 * a valid input length to sign. If `payload_length_arg <= 0`,
1325 * `exercise_alg` is supposed to be forbidden by the policy. */
1326 int compatible_alg = payload_length_arg > 0;
1327 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001328 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001329 size_t signature_length;
1330
Gilles Peskine8817f612018-12-18 00:18:46 +01001331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001333 psa_set_key_usage_flags( &attributes, policy_usage );
1334 psa_set_key_algorithm( &attributes, policy_alg );
1335 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001336
Gilles Peskine049c7532019-05-15 20:22:09 +02001337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001338 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339
Ronald Cron5425a212020-08-04 14:58:35 +02001340 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001341 payload, payload_length,
1342 signature, sizeof( signature ),
1343 &signature_length );
1344 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001345 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001347 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001348
1349 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001350 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001351 payload, payload_length,
1352 signature, sizeof( signature ) );
1353 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001354 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001355 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001356 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001357
1358exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001359 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001360 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001361}
1362/* END_CASE */
1363
Janos Follathba3fab92019-06-11 14:50:16 +01001364/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001365void derive_key_policy( int policy_usage,
1366 int policy_alg,
1367 int key_type,
1368 data_t *key_data,
1369 int exercise_alg )
1370{
Ronald Cron5425a212020-08-04 14:58:35 +02001371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001373 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001374 psa_status_t status;
1375
Gilles Peskine8817f612018-12-18 00:18:46 +01001376 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001377
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001378 psa_set_key_usage_flags( &attributes, policy_usage );
1379 psa_set_key_algorithm( &attributes, policy_alg );
1380 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001381
Gilles Peskine049c7532019-05-15 20:22:09 +02001382 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001383 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001384
Janos Follathba3fab92019-06-11 14:50:16 +01001385 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1386
1387 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1388 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001389 {
Janos Follathba3fab92019-06-11 14:50:16 +01001390 PSA_ASSERT( psa_key_derivation_input_bytes(
1391 &operation,
1392 PSA_KEY_DERIVATION_INPUT_SEED,
1393 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001394 }
Janos Follathba3fab92019-06-11 14:50:16 +01001395
1396 status = psa_key_derivation_input_key( &operation,
1397 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001398 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001399
Gilles Peskineea0fb492018-07-12 17:17:20 +02001400 if( policy_alg == exercise_alg &&
1401 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001402 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001403 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001404 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001405
1406exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001407 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001408 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001409 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001410}
1411/* END_CASE */
1412
1413/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001414void agreement_key_policy( int policy_usage,
1415 int policy_alg,
1416 int key_type_arg,
1417 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001418 int exercise_alg,
1419 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001420{
Ronald Cron5425a212020-08-04 14:58:35 +02001421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001423 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001424 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001425 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001426 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001427
Gilles Peskine8817f612018-12-18 00:18:46 +01001428 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001429
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001430 psa_set_key_usage_flags( &attributes, policy_usage );
1431 psa_set_key_algorithm( &attributes, policy_alg );
1432 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001433
Gilles Peskine049c7532019-05-15 20:22:09 +02001434 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001435 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001436
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001437 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001438 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001439
Steven Cooremance48e852020-10-05 16:02:45 +02001440 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441
1442exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001443 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001444 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001445 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001446}
1447/* END_CASE */
1448
1449/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001450void key_policy_alg2( int key_type_arg, data_t *key_data,
1451 int usage_arg, int alg_arg, int alg2_arg )
1452{
Ronald Cron5425a212020-08-04 14:58:35 +02001453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001454 psa_key_type_t key_type = key_type_arg;
1455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1456 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1457 psa_key_usage_t usage = usage_arg;
1458 psa_algorithm_t alg = alg_arg;
1459 psa_algorithm_t alg2 = alg2_arg;
1460
1461 PSA_ASSERT( psa_crypto_init( ) );
1462
1463 psa_set_key_usage_flags( &attributes, usage );
1464 psa_set_key_algorithm( &attributes, alg );
1465 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1466 psa_set_key_type( &attributes, key_type );
1467 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001468 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001469
Ronald Cron5425a212020-08-04 14:58:35 +02001470 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001471 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1472 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1473 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1474
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001475 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001476 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001477 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001478 goto exit;
1479
1480exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001481 /*
1482 * Key attributes may have been returned by psa_get_key_attributes()
1483 * thus reset them as required.
1484 */
1485 psa_reset_key_attributes( &got_attributes );
1486
Ronald Cron5425a212020-08-04 14:58:35 +02001487 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001488 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001489}
1490/* END_CASE */
1491
1492/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001493void raw_agreement_key_policy( int policy_usage,
1494 int policy_alg,
1495 int key_type_arg,
1496 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001497 int exercise_alg,
1498 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001499{
Ronald Cron5425a212020-08-04 14:58:35 +02001500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001502 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001503 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001504 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001505 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001506
1507 PSA_ASSERT( psa_crypto_init( ) );
1508
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001509 psa_set_key_usage_flags( &attributes, policy_usage );
1510 psa_set_key_algorithm( &attributes, policy_alg );
1511 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001512
Gilles Peskine049c7532019-05-15 20:22:09 +02001513 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001514 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001515
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001516 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517
Steven Cooremance48e852020-10-05 16:02:45 +02001518 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001519
1520exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001521 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001522 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001523 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001524}
1525/* END_CASE */
1526
1527/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001528void copy_success( int source_usage_arg,
1529 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001530 int type_arg, data_t *material,
1531 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001532 int target_usage_arg,
1533 int target_alg_arg, int target_alg2_arg,
1534 int expected_usage_arg,
1535 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001536{
Gilles Peskineca25db92019-04-19 11:43:08 +02001537 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1538 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001539 psa_key_usage_t expected_usage = expected_usage_arg;
1540 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001541 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001542 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1543 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001544 uint8_t *export_buffer = NULL;
1545
Gilles Peskine57ab7212019-01-28 13:03:09 +01001546 PSA_ASSERT( psa_crypto_init( ) );
1547
Gilles Peskineca25db92019-04-19 11:43:08 +02001548 /* Prepare the source key. */
1549 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1550 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001551 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001552 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001553 PSA_ASSERT( psa_import_key( &source_attributes,
1554 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001555 &source_key ) );
1556 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001557
Gilles Peskineca25db92019-04-19 11:43:08 +02001558 /* Prepare the target attributes. */
1559 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001560 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001561 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001562 /* Set volatile lifetime to reset the key identifier to 0. */
1563 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1564 }
1565
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 if( target_usage_arg != -1 )
1567 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1568 if( target_alg_arg != -1 )
1569 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001570 if( target_alg2_arg != -1 )
1571 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001572
1573 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001574 PSA_ASSERT( psa_copy_key( source_key,
1575 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001576
1577 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001578 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001579
1580 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001581 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001582 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1583 psa_get_key_type( &target_attributes ) );
1584 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1585 psa_get_key_bits( &target_attributes ) );
1586 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1587 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001588 TEST_EQUAL( expected_alg2,
1589 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001590 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1591 {
1592 size_t length;
1593 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001594 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001595 material->len, &length ) );
1596 ASSERT_COMPARE( material->x, material->len,
1597 export_buffer, length );
1598 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001599
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001600 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001601 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001602 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001603 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001604
Ronald Cron5425a212020-08-04 14:58:35 +02001605 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001606
1607exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001608 /*
1609 * Source and target key attributes may have been returned by
1610 * psa_get_key_attributes() thus reset them as required.
1611 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001612 psa_reset_key_attributes( &source_attributes );
1613 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001614
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001615 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001616 mbedtls_free( export_buffer );
1617}
1618/* END_CASE */
1619
1620/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001621void copy_fail( int source_usage_arg,
1622 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001623 int type_arg, data_t *material,
1624 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001625 int target_usage_arg,
1626 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001627 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001628 int expected_status_arg )
1629{
1630 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1631 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001632 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1633 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001634 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001635
1636 PSA_ASSERT( psa_crypto_init( ) );
1637
1638 /* Prepare the source key. */
1639 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1640 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001641 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001642 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001643 PSA_ASSERT( psa_import_key( &source_attributes,
1644 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001645 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001646
1647 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001648 psa_set_key_id( &target_attributes, key_id );
1649 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001650 psa_set_key_type( &target_attributes, target_type_arg );
1651 psa_set_key_bits( &target_attributes, target_bits_arg );
1652 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1653 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001654 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001655
1656 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001657 TEST_EQUAL( psa_copy_key( source_key,
1658 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001659 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001660
Ronald Cron5425a212020-08-04 14:58:35 +02001661 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001662
Gilles Peskine4a644642019-05-03 17:14:08 +02001663exit:
1664 psa_reset_key_attributes( &source_attributes );
1665 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001666 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001667}
1668/* END_CASE */
1669
1670/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001671void hash_operation_init( )
1672{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001673 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001674 /* Test each valid way of initializing the object, except for `= {0}`, as
1675 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1676 * though it's OK by the C standard. We could test for this, but we'd need
1677 * to supress the Clang warning for the test. */
1678 psa_hash_operation_t func = psa_hash_operation_init( );
1679 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1680 psa_hash_operation_t zero;
1681
1682 memset( &zero, 0, sizeof( zero ) );
1683
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001684 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001685 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1686 PSA_ERROR_BAD_STATE );
1687 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1688 PSA_ERROR_BAD_STATE );
1689 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1690 PSA_ERROR_BAD_STATE );
1691
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001692 /* A default hash operation should be abortable without error. */
1693 PSA_ASSERT( psa_hash_abort( &func ) );
1694 PSA_ASSERT( psa_hash_abort( &init ) );
1695 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001696}
1697/* END_CASE */
1698
1699/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001700void hash_setup( int alg_arg,
1701 int expected_status_arg )
1702{
1703 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001704 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001705 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001706 psa_status_t status;
1707
Gilles Peskine8817f612018-12-18 00:18:46 +01001708 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001709
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001710 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001711 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001712
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001713 /* Whether setup succeeded or failed, abort must succeed. */
1714 PSA_ASSERT( psa_hash_abort( &operation ) );
1715
1716 /* If setup failed, reproduce the failure, so as to
1717 * test the resulting state of the operation object. */
1718 if( status != PSA_SUCCESS )
1719 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1720
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001721 /* Now the operation object should be reusable. */
1722#if defined(KNOWN_SUPPORTED_HASH_ALG)
1723 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1724 PSA_ASSERT( psa_hash_abort( &operation ) );
1725#endif
1726
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001727exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001728 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001729}
1730/* END_CASE */
1731
1732/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001733void hash_compute_fail( int alg_arg, data_t *input,
1734 int output_size_arg, int expected_status_arg )
1735{
1736 psa_algorithm_t alg = alg_arg;
1737 uint8_t *output = NULL;
1738 size_t output_size = output_size_arg;
1739 size_t output_length = INVALID_EXPORT_LENGTH;
1740 psa_status_t expected_status = expected_status_arg;
1741 psa_status_t status;
1742
1743 ASSERT_ALLOC( output, output_size );
1744
1745 PSA_ASSERT( psa_crypto_init( ) );
1746
1747 status = psa_hash_compute( alg, input->x, input->len,
1748 output, output_size, &output_length );
1749 TEST_EQUAL( status, expected_status );
1750 TEST_ASSERT( output_length <= output_size );
1751
1752exit:
1753 mbedtls_free( output );
1754 PSA_DONE( );
1755}
1756/* END_CASE */
1757
1758/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001759void hash_compare_fail( int alg_arg, data_t *input,
1760 data_t *reference_hash,
1761 int expected_status_arg )
1762{
1763 psa_algorithm_t alg = alg_arg;
1764 psa_status_t expected_status = expected_status_arg;
1765 psa_status_t status;
1766
1767 PSA_ASSERT( psa_crypto_init( ) );
1768
1769 status = psa_hash_compare( alg, input->x, input->len,
1770 reference_hash->x, reference_hash->len );
1771 TEST_EQUAL( status, expected_status );
1772
1773exit:
1774 PSA_DONE( );
1775}
1776/* END_CASE */
1777
1778/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001779void hash_compute_compare( int alg_arg, data_t *input,
1780 data_t *expected_output )
1781{
1782 psa_algorithm_t alg = alg_arg;
1783 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1784 size_t output_length = INVALID_EXPORT_LENGTH;
1785 size_t i;
1786
1787 PSA_ASSERT( psa_crypto_init( ) );
1788
1789 /* Compute with tight buffer */
1790 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001791 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001792 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001793 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001794 ASSERT_COMPARE( output, output_length,
1795 expected_output->x, expected_output->len );
1796
1797 /* Compute with larger buffer */
1798 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1799 output, sizeof( output ),
1800 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001801 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001802 ASSERT_COMPARE( output, output_length,
1803 expected_output->x, expected_output->len );
1804
1805 /* Compare with correct hash */
1806 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1807 output, output_length ) );
1808
1809 /* Compare with trailing garbage */
1810 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1811 output, output_length + 1 ),
1812 PSA_ERROR_INVALID_SIGNATURE );
1813
1814 /* Compare with truncated hash */
1815 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1816 output, output_length - 1 ),
1817 PSA_ERROR_INVALID_SIGNATURE );
1818
1819 /* Compare with corrupted value */
1820 for( i = 0; i < output_length; i++ )
1821 {
Chris Jones9634bb12021-01-20 15:56:42 +00001822 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001823 output[i] ^= 1;
1824 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1825 output, output_length ),
1826 PSA_ERROR_INVALID_SIGNATURE );
1827 output[i] ^= 1;
1828 }
1829
1830exit:
1831 PSA_DONE( );
1832}
1833/* END_CASE */
1834
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001835/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001836void hash_bad_order( )
1837{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001838 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001839 unsigned char input[] = "";
1840 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001841 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001842 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1843 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1844 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001845 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001846 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001847 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001848
Gilles Peskine8817f612018-12-18 00:18:46 +01001849 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001850
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001851 /* Call setup twice in a row. */
1852 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1853 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1854 PSA_ERROR_BAD_STATE );
1855 PSA_ASSERT( psa_hash_abort( &operation ) );
1856
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001857 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001858 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001859 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001860 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001861
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001862 /* Call update after finish. */
1863 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1864 PSA_ASSERT( psa_hash_finish( &operation,
1865 hash, sizeof( hash ), &hash_len ) );
1866 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001867 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001868 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001869
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001870 /* Call verify without calling setup beforehand. */
1871 TEST_EQUAL( psa_hash_verify( &operation,
1872 valid_hash, sizeof( valid_hash ) ),
1873 PSA_ERROR_BAD_STATE );
1874 PSA_ASSERT( psa_hash_abort( &operation ) );
1875
1876 /* Call verify after finish. */
1877 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1878 PSA_ASSERT( psa_hash_finish( &operation,
1879 hash, sizeof( hash ), &hash_len ) );
1880 TEST_EQUAL( psa_hash_verify( &operation,
1881 valid_hash, sizeof( valid_hash ) ),
1882 PSA_ERROR_BAD_STATE );
1883 PSA_ASSERT( psa_hash_abort( &operation ) );
1884
1885 /* Call verify twice in a row. */
1886 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1887 PSA_ASSERT( psa_hash_verify( &operation,
1888 valid_hash, sizeof( valid_hash ) ) );
1889 TEST_EQUAL( psa_hash_verify( &operation,
1890 valid_hash, sizeof( valid_hash ) ),
1891 PSA_ERROR_BAD_STATE );
1892 PSA_ASSERT( psa_hash_abort( &operation ) );
1893
1894 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001895 TEST_EQUAL( psa_hash_finish( &operation,
1896 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001897 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001898 PSA_ASSERT( psa_hash_abort( &operation ) );
1899
1900 /* Call finish twice in a row. */
1901 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1902 PSA_ASSERT( psa_hash_finish( &operation,
1903 hash, sizeof( hash ), &hash_len ) );
1904 TEST_EQUAL( psa_hash_finish( &operation,
1905 hash, sizeof( hash ), &hash_len ),
1906 PSA_ERROR_BAD_STATE );
1907 PSA_ASSERT( psa_hash_abort( &operation ) );
1908
1909 /* Call finish after calling verify. */
1910 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1911 PSA_ASSERT( psa_hash_verify( &operation,
1912 valid_hash, sizeof( valid_hash ) ) );
1913 TEST_EQUAL( psa_hash_finish( &operation,
1914 hash, sizeof( hash ), &hash_len ),
1915 PSA_ERROR_BAD_STATE );
1916 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001917
1918exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001919 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001920}
1921/* END_CASE */
1922
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001923/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001924void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001925{
1926 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001927 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1928 * appended to it */
1929 unsigned char hash[] = {
1930 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1931 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1932 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001933 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001934 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001935
Gilles Peskine8817f612018-12-18 00:18:46 +01001936 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001937
itayzafrir27e69452018-11-01 14:26:34 +02001938 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001939 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001940 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001941 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001942
itayzafrir27e69452018-11-01 14:26:34 +02001943 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001944 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001945 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001946 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001947
itayzafrir27e69452018-11-01 14:26:34 +02001948 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001950 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001951 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001952
itayzafrirec93d302018-10-18 18:01:10 +03001953exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001954 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001955}
1956/* END_CASE */
1957
Ronald Cronee414c72021-03-18 18:50:08 +01001958/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001959void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001960{
1961 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001962 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001963 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001964 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001965 size_t hash_len;
1966
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001968
itayzafrir58028322018-10-25 10:22:01 +03001969 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001970 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001971 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001972 hash, expected_size - 1, &hash_len ),
1973 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001974
1975exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001976 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001977}
1978/* END_CASE */
1979
Ronald Cronee414c72021-03-18 18:50:08 +01001980/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001981void hash_clone_source_state( )
1982{
1983 psa_algorithm_t alg = PSA_ALG_SHA_256;
1984 unsigned char hash[PSA_HASH_MAX_SIZE];
1985 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1986 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1987 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1988 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1989 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1990 size_t hash_len;
1991
1992 PSA_ASSERT( psa_crypto_init( ) );
1993 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1994
1995 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1996 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1997 PSA_ASSERT( psa_hash_finish( &op_finished,
1998 hash, sizeof( hash ), &hash_len ) );
1999 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2000 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2001
2002 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2003 PSA_ERROR_BAD_STATE );
2004
2005 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2006 PSA_ASSERT( psa_hash_finish( &op_init,
2007 hash, sizeof( hash ), &hash_len ) );
2008 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2009 PSA_ASSERT( psa_hash_finish( &op_finished,
2010 hash, sizeof( hash ), &hash_len ) );
2011 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2012 PSA_ASSERT( psa_hash_finish( &op_aborted,
2013 hash, sizeof( hash ), &hash_len ) );
2014
2015exit:
2016 psa_hash_abort( &op_source );
2017 psa_hash_abort( &op_init );
2018 psa_hash_abort( &op_setup );
2019 psa_hash_abort( &op_finished );
2020 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002021 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002022}
2023/* END_CASE */
2024
Ronald Cronee414c72021-03-18 18:50:08 +01002025/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002026void hash_clone_target_state( )
2027{
2028 psa_algorithm_t alg = PSA_ALG_SHA_256;
2029 unsigned char hash[PSA_HASH_MAX_SIZE];
2030 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2031 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2032 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2033 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2034 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2035 size_t hash_len;
2036
2037 PSA_ASSERT( psa_crypto_init( ) );
2038
2039 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2040 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2041 PSA_ASSERT( psa_hash_finish( &op_finished,
2042 hash, sizeof( hash ), &hash_len ) );
2043 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2044 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2045
2046 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2047 PSA_ASSERT( psa_hash_finish( &op_target,
2048 hash, sizeof( hash ), &hash_len ) );
2049
2050 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2051 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2052 PSA_ERROR_BAD_STATE );
2053 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2054 PSA_ERROR_BAD_STATE );
2055
2056exit:
2057 psa_hash_abort( &op_target );
2058 psa_hash_abort( &op_init );
2059 psa_hash_abort( &op_setup );
2060 psa_hash_abort( &op_finished );
2061 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002062 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002063}
2064/* END_CASE */
2065
itayzafrir58028322018-10-25 10:22:01 +03002066/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002067void mac_operation_init( )
2068{
Jaeden Amero252ef282019-02-15 14:05:35 +00002069 const uint8_t input[1] = { 0 };
2070
Jaeden Amero769ce272019-01-04 11:48:03 +00002071 /* Test each valid way of initializing the object, except for `= {0}`, as
2072 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2073 * though it's OK by the C standard. We could test for this, but we'd need
2074 * to supress the Clang warning for the test. */
2075 psa_mac_operation_t func = psa_mac_operation_init( );
2076 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2077 psa_mac_operation_t zero;
2078
2079 memset( &zero, 0, sizeof( zero ) );
2080
Jaeden Amero252ef282019-02-15 14:05:35 +00002081 /* A freshly-initialized MAC operation should not be usable. */
2082 TEST_EQUAL( psa_mac_update( &func,
2083 input, sizeof( input ) ),
2084 PSA_ERROR_BAD_STATE );
2085 TEST_EQUAL( psa_mac_update( &init,
2086 input, sizeof( input ) ),
2087 PSA_ERROR_BAD_STATE );
2088 TEST_EQUAL( psa_mac_update( &zero,
2089 input, sizeof( input ) ),
2090 PSA_ERROR_BAD_STATE );
2091
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002092 /* A default MAC operation should be abortable without error. */
2093 PSA_ASSERT( psa_mac_abort( &func ) );
2094 PSA_ASSERT( psa_mac_abort( &init ) );
2095 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002096}
2097/* END_CASE */
2098
2099/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002100void mac_setup( int key_type_arg,
2101 data_t *key,
2102 int alg_arg,
2103 int expected_status_arg )
2104{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105 psa_key_type_t key_type = key_type_arg;
2106 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002107 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002108 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002109 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2110#if defined(KNOWN_SUPPORTED_MAC_ALG)
2111 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2112#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002113
Gilles Peskine8817f612018-12-18 00:18:46 +01002114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002115
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002116 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2117 &operation, &status ) )
2118 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002119 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002120
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002121 /* The operation object should be reusable. */
2122#if defined(KNOWN_SUPPORTED_MAC_ALG)
2123 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2124 smoke_test_key_data,
2125 sizeof( smoke_test_key_data ),
2126 KNOWN_SUPPORTED_MAC_ALG,
2127 &operation, &status ) )
2128 goto exit;
2129 TEST_EQUAL( status, PSA_SUCCESS );
2130#endif
2131
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002132exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002133 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134}
2135/* END_CASE */
2136
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002137/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00002138void mac_bad_order( )
2139{
Ronald Cron5425a212020-08-04 14:58:35 +02002140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002141 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2142 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002143 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002144 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2145 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2146 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002148 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2149 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2150 size_t sign_mac_length = 0;
2151 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2152 const uint8_t verify_mac[] = {
2153 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2154 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2155 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2156
2157 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002158 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002159 psa_set_key_algorithm( &attributes, alg );
2160 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002161
Ronald Cron5425a212020-08-04 14:58:35 +02002162 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2163 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002164
Jaeden Amero252ef282019-02-15 14:05:35 +00002165 /* Call update without calling setup beforehand. */
2166 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2167 PSA_ERROR_BAD_STATE );
2168 PSA_ASSERT( psa_mac_abort( &operation ) );
2169
2170 /* Call sign finish without calling setup beforehand. */
2171 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2172 &sign_mac_length),
2173 PSA_ERROR_BAD_STATE );
2174 PSA_ASSERT( psa_mac_abort( &operation ) );
2175
2176 /* Call verify finish without calling setup beforehand. */
2177 TEST_EQUAL( psa_mac_verify_finish( &operation,
2178 verify_mac, sizeof( verify_mac ) ),
2179 PSA_ERROR_BAD_STATE );
2180 PSA_ASSERT( psa_mac_abort( &operation ) );
2181
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002182 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002183 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2184 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002185 PSA_ERROR_BAD_STATE );
2186 PSA_ASSERT( psa_mac_abort( &operation ) );
2187
Jaeden Amero252ef282019-02-15 14:05:35 +00002188 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002189 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002190 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2191 PSA_ASSERT( psa_mac_sign_finish( &operation,
2192 sign_mac, sizeof( sign_mac ),
2193 &sign_mac_length ) );
2194 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2195 PSA_ERROR_BAD_STATE );
2196 PSA_ASSERT( psa_mac_abort( &operation ) );
2197
2198 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002199 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002200 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2201 PSA_ASSERT( psa_mac_verify_finish( &operation,
2202 verify_mac, sizeof( verify_mac ) ) );
2203 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2204 PSA_ERROR_BAD_STATE );
2205 PSA_ASSERT( psa_mac_abort( &operation ) );
2206
2207 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002208 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002209 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2210 PSA_ASSERT( psa_mac_sign_finish( &operation,
2211 sign_mac, sizeof( sign_mac ),
2212 &sign_mac_length ) );
2213 TEST_EQUAL( psa_mac_sign_finish( &operation,
2214 sign_mac, sizeof( sign_mac ),
2215 &sign_mac_length ),
2216 PSA_ERROR_BAD_STATE );
2217 PSA_ASSERT( psa_mac_abort( &operation ) );
2218
2219 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002220 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002221 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2222 PSA_ASSERT( psa_mac_verify_finish( &operation,
2223 verify_mac, sizeof( verify_mac ) ) );
2224 TEST_EQUAL( psa_mac_verify_finish( &operation,
2225 verify_mac, sizeof( verify_mac ) ),
2226 PSA_ERROR_BAD_STATE );
2227 PSA_ASSERT( psa_mac_abort( &operation ) );
2228
2229 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002230 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002231 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2232 TEST_EQUAL( psa_mac_verify_finish( &operation,
2233 verify_mac, sizeof( verify_mac ) ),
2234 PSA_ERROR_BAD_STATE );
2235 PSA_ASSERT( psa_mac_abort( &operation ) );
2236
2237 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002238 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002239 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2240 TEST_EQUAL( psa_mac_sign_finish( &operation,
2241 sign_mac, sizeof( sign_mac ),
2242 &sign_mac_length ),
2243 PSA_ERROR_BAD_STATE );
2244 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002245
Ronald Cron5425a212020-08-04 14:58:35 +02002246 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002247
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002248exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002249 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002250}
2251/* END_CASE */
2252
2253/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002254void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002255 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002256 int alg_arg,
2257 data_t *input,
2258 data_t *expected_mac )
2259{
Ronald Cron5425a212020-08-04 14:58:35 +02002260 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002261 psa_key_type_t key_type = key_type_arg;
2262 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002263 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002265 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002266 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002267 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002268 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002269 const size_t output_sizes_to_test[] = {
2270 0,
2271 1,
2272 expected_mac->len - 1,
2273 expected_mac->len,
2274 expected_mac->len + 1,
2275 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002276
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002277 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002278 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002279 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002280
Gilles Peskine8817f612018-12-18 00:18:46 +01002281 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002282
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002283 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002284 psa_set_key_algorithm( &attributes, alg );
2285 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002286
Ronald Cron5425a212020-08-04 14:58:35 +02002287 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2288 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002289
Gilles Peskine8b356b52020-08-25 23:44:59 +02002290 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2291 {
2292 const size_t output_size = output_sizes_to_test[i];
2293 psa_status_t expected_status =
2294 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2295 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002296
Chris Jones9634bb12021-01-20 15:56:42 +00002297 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002298 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002299
Gilles Peskine8b356b52020-08-25 23:44:59 +02002300 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002301 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002302 PSA_ASSERT( psa_mac_update( &operation,
2303 input->x, input->len ) );
2304 TEST_EQUAL( psa_mac_sign_finish( &operation,
2305 actual_mac, output_size,
2306 &mac_length ),
2307 expected_status );
2308 PSA_ASSERT( psa_mac_abort( &operation ) );
2309
2310 if( expected_status == PSA_SUCCESS )
2311 {
2312 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2313 actual_mac, mac_length );
2314 }
2315 mbedtls_free( actual_mac );
2316 actual_mac = NULL;
2317 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002318
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002319exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002320 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002321 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002322 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002323 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002324}
2325/* END_CASE */
2326
2327/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002328void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002329 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002330 int alg_arg,
2331 data_t *input,
2332 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333{
Ronald Cron5425a212020-08-04 14:58:35 +02002334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002335 psa_key_type_t key_type = key_type_arg;
2336 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002337 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002339 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002340
Gilles Peskine69c12672018-06-28 00:07:19 +02002341 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2342
Gilles Peskine8817f612018-12-18 00:18:46 +01002343 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002344
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002345 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002346 psa_set_key_algorithm( &attributes, alg );
2347 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002348
Ronald Cron5425a212020-08-04 14:58:35 +02002349 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2350 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002351
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002352 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002353 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002354 PSA_ASSERT( psa_mac_update( &operation,
2355 input->x, input->len ) );
2356 PSA_ASSERT( psa_mac_verify_finish( &operation,
2357 expected_mac->x,
2358 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002359
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002360 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002361 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002362 PSA_ASSERT( psa_mac_update( &operation,
2363 input->x, input->len ) );
2364 TEST_EQUAL( psa_mac_verify_finish( &operation,
2365 expected_mac->x,
2366 expected_mac->len - 1 ),
2367 PSA_ERROR_INVALID_SIGNATURE );
2368
2369 /* Test a MAC that's too long. */
2370 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2371 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002372 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002373 PSA_ASSERT( psa_mac_update( &operation,
2374 input->x, input->len ) );
2375 TEST_EQUAL( psa_mac_verify_finish( &operation,
2376 perturbed_mac,
2377 expected_mac->len + 1 ),
2378 PSA_ERROR_INVALID_SIGNATURE );
2379
2380 /* Test changing one byte. */
2381 for( size_t i = 0; i < expected_mac->len; i++ )
2382 {
Chris Jones9634bb12021-01-20 15:56:42 +00002383 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002384 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002386 PSA_ASSERT( psa_mac_update( &operation,
2387 input->x, input->len ) );
2388 TEST_EQUAL( psa_mac_verify_finish( &operation,
2389 perturbed_mac,
2390 expected_mac->len ),
2391 PSA_ERROR_INVALID_SIGNATURE );
2392 perturbed_mac[i] ^= 1;
2393 }
2394
Gilles Peskine8c9def32018-02-08 10:02:12 +01002395exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002396 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002397 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002398 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002399 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400}
2401/* END_CASE */
2402
2403/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002404void cipher_operation_init( )
2405{
Jaeden Ameroab439972019-02-15 14:12:05 +00002406 const uint8_t input[1] = { 0 };
2407 unsigned char output[1] = { 0 };
2408 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002409 /* Test each valid way of initializing the object, except for `= {0}`, as
2410 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2411 * though it's OK by the C standard. We could test for this, but we'd need
2412 * to supress the Clang warning for the test. */
2413 psa_cipher_operation_t func = psa_cipher_operation_init( );
2414 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2415 psa_cipher_operation_t zero;
2416
2417 memset( &zero, 0, sizeof( zero ) );
2418
Jaeden Ameroab439972019-02-15 14:12:05 +00002419 /* A freshly-initialized cipher operation should not be usable. */
2420 TEST_EQUAL( psa_cipher_update( &func,
2421 input, sizeof( input ),
2422 output, sizeof( output ),
2423 &output_length ),
2424 PSA_ERROR_BAD_STATE );
2425 TEST_EQUAL( psa_cipher_update( &init,
2426 input, sizeof( input ),
2427 output, sizeof( output ),
2428 &output_length ),
2429 PSA_ERROR_BAD_STATE );
2430 TEST_EQUAL( psa_cipher_update( &zero,
2431 input, sizeof( input ),
2432 output, sizeof( output ),
2433 &output_length ),
2434 PSA_ERROR_BAD_STATE );
2435
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002436 /* A default cipher operation should be abortable without error. */
2437 PSA_ASSERT( psa_cipher_abort( &func ) );
2438 PSA_ASSERT( psa_cipher_abort( &init ) );
2439 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002440}
2441/* END_CASE */
2442
2443/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444void cipher_setup( int key_type_arg,
2445 data_t *key,
2446 int alg_arg,
2447 int expected_status_arg )
2448{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449 psa_key_type_t key_type = key_type_arg;
2450 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002451 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002452 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002453 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002454#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002455 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2456#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002457
Gilles Peskine8817f612018-12-18 00:18:46 +01002458 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002459
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002460 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2461 &operation, &status ) )
2462 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002463 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002464
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002465 /* The operation object should be reusable. */
2466#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2467 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2468 smoke_test_key_data,
2469 sizeof( smoke_test_key_data ),
2470 KNOWN_SUPPORTED_CIPHER_ALG,
2471 &operation, &status ) )
2472 goto exit;
2473 TEST_EQUAL( status, PSA_SUCCESS );
2474#endif
2475
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002477 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002478 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002479}
2480/* END_CASE */
2481
Ronald Cronee414c72021-03-18 18:50:08 +01002482/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002483void cipher_bad_order( )
2484{
Ronald Cron5425a212020-08-04 14:58:35 +02002485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002486 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2487 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002489 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002490 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002491 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002492 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2493 0xaa, 0xaa, 0xaa, 0xaa };
2494 const uint8_t text[] = {
2495 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2496 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002497 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002498 size_t length = 0;
2499
2500 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002501 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2502 psa_set_key_algorithm( &attributes, alg );
2503 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002504 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2505 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002506
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002507 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002508 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2509 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002510 PSA_ERROR_BAD_STATE );
2511 PSA_ASSERT( psa_cipher_abort( &operation ) );
2512
2513 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002514 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2515 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002516 PSA_ERROR_BAD_STATE );
2517 PSA_ASSERT( psa_cipher_abort( &operation ) );
2518
Jaeden Ameroab439972019-02-15 14:12:05 +00002519 /* Generate an IV without calling setup beforehand. */
2520 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2521 buffer, sizeof( buffer ),
2522 &length ),
2523 PSA_ERROR_BAD_STATE );
2524 PSA_ASSERT( psa_cipher_abort( &operation ) );
2525
2526 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002527 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002528 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2529 buffer, sizeof( buffer ),
2530 &length ) );
2531 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2532 buffer, sizeof( buffer ),
2533 &length ),
2534 PSA_ERROR_BAD_STATE );
2535 PSA_ASSERT( psa_cipher_abort( &operation ) );
2536
2537 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002538 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002539 PSA_ASSERT( psa_cipher_set_iv( &operation,
2540 iv, sizeof( iv ) ) );
2541 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2542 buffer, sizeof( buffer ),
2543 &length ),
2544 PSA_ERROR_BAD_STATE );
2545 PSA_ASSERT( psa_cipher_abort( &operation ) );
2546
2547 /* Set an IV without calling setup beforehand. */
2548 TEST_EQUAL( psa_cipher_set_iv( &operation,
2549 iv, sizeof( iv ) ),
2550 PSA_ERROR_BAD_STATE );
2551 PSA_ASSERT( psa_cipher_abort( &operation ) );
2552
2553 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002554 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002555 PSA_ASSERT( psa_cipher_set_iv( &operation,
2556 iv, sizeof( iv ) ) );
2557 TEST_EQUAL( psa_cipher_set_iv( &operation,
2558 iv, sizeof( iv ) ),
2559 PSA_ERROR_BAD_STATE );
2560 PSA_ASSERT( psa_cipher_abort( &operation ) );
2561
2562 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002563 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002564 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2565 buffer, sizeof( buffer ),
2566 &length ) );
2567 TEST_EQUAL( psa_cipher_set_iv( &operation,
2568 iv, sizeof( iv ) ),
2569 PSA_ERROR_BAD_STATE );
2570 PSA_ASSERT( psa_cipher_abort( &operation ) );
2571
2572 /* Call update without calling setup beforehand. */
2573 TEST_EQUAL( psa_cipher_update( &operation,
2574 text, sizeof( text ),
2575 buffer, sizeof( buffer ),
2576 &length ),
2577 PSA_ERROR_BAD_STATE );
2578 PSA_ASSERT( psa_cipher_abort( &operation ) );
2579
2580 /* Call update without an IV where an IV is required. */
2581 TEST_EQUAL( psa_cipher_update( &operation,
2582 text, sizeof( text ),
2583 buffer, sizeof( buffer ),
2584 &length ),
2585 PSA_ERROR_BAD_STATE );
2586 PSA_ASSERT( psa_cipher_abort( &operation ) );
2587
2588 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002589 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002590 PSA_ASSERT( psa_cipher_set_iv( &operation,
2591 iv, sizeof( iv ) ) );
2592 PSA_ASSERT( psa_cipher_finish( &operation,
2593 buffer, sizeof( buffer ), &length ) );
2594 TEST_EQUAL( psa_cipher_update( &operation,
2595 text, sizeof( text ),
2596 buffer, sizeof( buffer ),
2597 &length ),
2598 PSA_ERROR_BAD_STATE );
2599 PSA_ASSERT( psa_cipher_abort( &operation ) );
2600
2601 /* Call finish without calling setup beforehand. */
2602 TEST_EQUAL( psa_cipher_finish( &operation,
2603 buffer, sizeof( buffer ), &length ),
2604 PSA_ERROR_BAD_STATE );
2605 PSA_ASSERT( psa_cipher_abort( &operation ) );
2606
2607 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002608 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002609 /* Not calling update means we are encrypting an empty buffer, which is OK
2610 * for cipher modes with padding. */
2611 TEST_EQUAL( psa_cipher_finish( &operation,
2612 buffer, sizeof( buffer ), &length ),
2613 PSA_ERROR_BAD_STATE );
2614 PSA_ASSERT( psa_cipher_abort( &operation ) );
2615
2616 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002617 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002618 PSA_ASSERT( psa_cipher_set_iv( &operation,
2619 iv, sizeof( iv ) ) );
2620 PSA_ASSERT( psa_cipher_finish( &operation,
2621 buffer, sizeof( buffer ), &length ) );
2622 TEST_EQUAL( psa_cipher_finish( &operation,
2623 buffer, sizeof( buffer ), &length ),
2624 PSA_ERROR_BAD_STATE );
2625 PSA_ASSERT( psa_cipher_abort( &operation ) );
2626
Ronald Cron5425a212020-08-04 14:58:35 +02002627 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002628
Jaeden Ameroab439972019-02-15 14:12:05 +00002629exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002630 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002631 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002632}
2633/* END_CASE */
2634
2635/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002636void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002637 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002638 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002639 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002640{
Ronald Cron5425a212020-08-04 14:58:35 +02002641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642 psa_status_t status;
2643 psa_key_type_t key_type = key_type_arg;
2644 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002645 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002646 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647 size_t output_buffer_size = 0;
2648 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002649 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002650 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652
Gilles Peskine8817f612018-12-18 00:18:46 +01002653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002655 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2656 psa_set_key_algorithm( &attributes, alg );
2657 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002658
Ronald Cron5425a212020-08-04 14:58:35 +02002659 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2660 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661
Ronald Cron5425a212020-08-04 14:58:35 +02002662 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002664 if( iv->len > 0 )
2665 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002666 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002667 }
2668
gabor-mezei-armceface22021-01-21 12:26:17 +01002669 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2670 TEST_ASSERT( output_buffer_size <=
2671 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002672 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Gilles Peskine8817f612018-12-18 00:18:46 +01002674 PSA_ASSERT( psa_cipher_update( &operation,
2675 input->x, input->len,
2676 output, output_buffer_size,
2677 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002678 TEST_ASSERT( function_output_length <=
2679 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2680 TEST_ASSERT( function_output_length <=
2681 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002682 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002683
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002685 ( output_buffer_size == 0 ? NULL :
2686 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002687 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002688 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002689 TEST_ASSERT( function_output_length <=
2690 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2691 TEST_ASSERT( function_output_length <=
2692 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002693 total_output_length += function_output_length;
2694
Gilles Peskinefe11b722018-12-18 00:24:04 +01002695 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696 if( expected_status == PSA_SUCCESS )
2697 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002698 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002699 ASSERT_COMPARE( expected_output->x, expected_output->len,
2700 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002701 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002702
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002704 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002705 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002706 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002707 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708}
2709/* END_CASE */
2710
2711/* BEGIN_CASE */
2712void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002713 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002714 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002715 int first_part_size_arg,
2716 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002717 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002718{
Ronald Cron5425a212020-08-04 14:58:35 +02002719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720 psa_key_type_t key_type = key_type_arg;
2721 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002722 size_t first_part_size = first_part_size_arg;
2723 size_t output1_length = output1_length_arg;
2724 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002725 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002726 size_t output_buffer_size = 0;
2727 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002728 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002729 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002730 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002731
Gilles Peskine8817f612018-12-18 00:18:46 +01002732 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002733
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002734 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2735 psa_set_key_algorithm( &attributes, alg );
2736 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002737
Ronald Cron5425a212020-08-04 14:58:35 +02002738 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2739 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740
Ronald Cron5425a212020-08-04 14:58:35 +02002741 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002742
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002743 if( iv->len > 0 )
2744 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002745 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002746 }
2747
gabor-mezei-armceface22021-01-21 12:26:17 +01002748 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2749 TEST_ASSERT( output_buffer_size <=
2750 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002751 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752
Gilles Peskinee0866522019-02-19 19:44:00 +01002753 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002754 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2755 output, output_buffer_size,
2756 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002757 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002758 TEST_ASSERT( function_output_length <=
2759 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2760 TEST_ASSERT( function_output_length <=
2761 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002762 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002763
Gilles Peskine8817f612018-12-18 00:18:46 +01002764 PSA_ASSERT( psa_cipher_update( &operation,
2765 input->x + first_part_size,
2766 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002767 ( output_buffer_size == 0 ? NULL :
2768 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002769 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002770 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002771 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002772 TEST_ASSERT( function_output_length <=
2773 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2774 alg,
2775 input->len - first_part_size ) );
2776 TEST_ASSERT( function_output_length <=
2777 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002778 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002779
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002781 ( output_buffer_size == 0 ? NULL :
2782 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002783 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002784 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002785 TEST_ASSERT( function_output_length <=
2786 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2787 TEST_ASSERT( function_output_length <=
2788 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002789 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002790 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002791
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002792 ASSERT_COMPARE( expected_output->x, expected_output->len,
2793 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002794
2795exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002796 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002797 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002798 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002799 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002800}
2801/* END_CASE */
2802
2803/* BEGIN_CASE */
2804void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002805 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002806 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002807 int first_part_size_arg,
2808 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002809 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002810{
Ronald Cron5425a212020-08-04 14:58:35 +02002811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812 psa_key_type_t key_type = key_type_arg;
2813 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002814 size_t first_part_size = first_part_size_arg;
2815 size_t output1_length = output1_length_arg;
2816 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002817 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818 size_t output_buffer_size = 0;
2819 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002820 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002821 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823
Gilles Peskine8817f612018-12-18 00:18:46 +01002824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2827 psa_set_key_algorithm( &attributes, alg );
2828 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002829
Ronald Cron5425a212020-08-04 14:58:35 +02002830 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2831 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002832
Ronald Cron5425a212020-08-04 14:58:35 +02002833 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834
Steven Cooreman177deba2020-09-07 17:14:14 +02002835 if( iv->len > 0 )
2836 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002837 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002838 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839
gabor-mezei-armceface22021-01-21 12:26:17 +01002840 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2841 TEST_ASSERT( output_buffer_size <=
2842 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002843 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
Gilles Peskinee0866522019-02-19 19:44:00 +01002845 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002846 PSA_ASSERT( psa_cipher_update( &operation,
2847 input->x, first_part_size,
2848 output, output_buffer_size,
2849 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002850 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002851 TEST_ASSERT( function_output_length <=
2852 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2853 TEST_ASSERT( function_output_length <=
2854 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002855 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002856
Gilles Peskine8817f612018-12-18 00:18:46 +01002857 PSA_ASSERT( psa_cipher_update( &operation,
2858 input->x + first_part_size,
2859 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002860 ( output_buffer_size == 0 ? NULL :
2861 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002862 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002863 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002864 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002865 TEST_ASSERT( function_output_length <=
2866 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2867 alg,
2868 input->len - first_part_size ) );
2869 TEST_ASSERT( function_output_length <=
2870 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002871 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002872
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002874 ( output_buffer_size == 0 ? NULL :
2875 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002876 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002877 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002878 TEST_ASSERT( function_output_length <=
2879 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2880 TEST_ASSERT( function_output_length <=
2881 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002882 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002883 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002885 ASSERT_COMPARE( expected_output->x, expected_output->len,
2886 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002887
2888exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002889 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002890 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002891 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002892 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002893}
2894/* END_CASE */
2895
Gilles Peskine50e586b2018-06-08 14:28:46 +02002896/* BEGIN_CASE */
2897void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002898 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002899 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002900 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901{
Ronald Cron5425a212020-08-04 14:58:35 +02002902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903 psa_status_t status;
2904 psa_key_type_t key_type = key_type_arg;
2905 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002906 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002907 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908 size_t output_buffer_size = 0;
2909 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002910 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002911 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913
Gilles Peskine8817f612018-12-18 00:18:46 +01002914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2917 psa_set_key_algorithm( &attributes, alg );
2918 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002919
Ronald Cron5425a212020-08-04 14:58:35 +02002920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2921 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922
Ronald Cron5425a212020-08-04 14:58:35 +02002923 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924
Steven Cooreman177deba2020-09-07 17:14:14 +02002925 if( iv->len > 0 )
2926 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002927 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002928 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929
gabor-mezei-armceface22021-01-21 12:26:17 +01002930 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2931 TEST_ASSERT( output_buffer_size <=
2932 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002933 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934
Gilles Peskine8817f612018-12-18 00:18:46 +01002935 PSA_ASSERT( psa_cipher_update( &operation,
2936 input->x, input->len,
2937 output, output_buffer_size,
2938 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002939 TEST_ASSERT( function_output_length <=
2940 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2941 TEST_ASSERT( function_output_length <=
2942 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002943 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002944
Gilles Peskine50e586b2018-06-08 14:28:46 +02002945 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002946 ( output_buffer_size == 0 ? NULL :
2947 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002948 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002949 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002950 TEST_ASSERT( function_output_length <=
2951 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2952 TEST_ASSERT( function_output_length <=
2953 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002954 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002955 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002956
2957 if( expected_status == PSA_SUCCESS )
2958 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002959 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002960 ASSERT_COMPARE( expected_output->x, expected_output->len,
2961 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002962 }
2963
Gilles Peskine50e586b2018-06-08 14:28:46 +02002964exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002965 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002966 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002967 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002968 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969}
2970/* END_CASE */
2971
Gilles Peskine50e586b2018-06-08 14:28:46 +02002972/* BEGIN_CASE */
2973void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002974 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002975 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002976{
Ronald Cron5425a212020-08-04 14:58:35 +02002977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002978 psa_key_type_t key_type = key_type_arg;
2979 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002980 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981 size_t iv_size = 16;
2982 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002983 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002984 size_t output1_size = 0;
2985 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002986 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002987 size_t output2_size = 0;
2988 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002989 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002990 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2991 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002993
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002995
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2997 psa_set_key_algorithm( &attributes, alg );
2998 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002999
Ronald Cron5425a212020-08-04 14:58:35 +02003000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3001 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003002
Ronald Cron5425a212020-08-04 14:58:35 +02003003 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3004 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003005
Steven Cooreman177deba2020-09-07 17:14:14 +02003006 if( alg != PSA_ALG_ECB_NO_PADDING )
3007 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003008 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3009 iv, iv_size,
3010 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003011 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003012 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3013 TEST_ASSERT( output1_size <=
3014 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003015 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003016
Gilles Peskine8817f612018-12-18 00:18:46 +01003017 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3018 output1, output1_size,
3019 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003020 TEST_ASSERT( output1_length <=
3021 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3022 TEST_ASSERT( output1_length <=
3023 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3024
Gilles Peskine8817f612018-12-18 00:18:46 +01003025 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003026 output1 + output1_length,
3027 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003029 TEST_ASSERT( function_output_length <=
3030 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3031 TEST_ASSERT( function_output_length <=
3032 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003033
Gilles Peskine048b7f02018-06-08 14:20:49 +02003034 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003035
Gilles Peskine8817f612018-12-18 00:18:46 +01003036 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003037
3038 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003039 TEST_ASSERT( output2_size <=
3040 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3041 TEST_ASSERT( output2_size <=
3042 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003043 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003044
Steven Cooreman177deba2020-09-07 17:14:14 +02003045 if( iv_length > 0 )
3046 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003047 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3048 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003049 }
3050
Gilles Peskine8817f612018-12-18 00:18:46 +01003051 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3052 output2, output2_size,
3053 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003054 TEST_ASSERT( output2_length <=
3055 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3056 TEST_ASSERT( output2_length <=
3057 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3058
Gilles Peskine048b7f02018-06-08 14:20:49 +02003059 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 PSA_ASSERT( psa_cipher_finish( &operation2,
3061 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003062 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003064 TEST_ASSERT( function_output_length <=
3065 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3066 TEST_ASSERT( function_output_length <=
3067 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003068
Gilles Peskine048b7f02018-06-08 14:20:49 +02003069 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003070
Gilles Peskine8817f612018-12-18 00:18:46 +01003071 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003072
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003073 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003074
3075exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003076 psa_cipher_abort( &operation1 );
3077 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003078 mbedtls_free( output1 );
3079 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003080 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003081 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003082}
3083/* END_CASE */
3084
3085/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003086void cipher_verify_output_multipart( int alg_arg,
3087 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003088 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003089 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003090 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003091{
Ronald Cron5425a212020-08-04 14:58:35 +02003092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003093 psa_key_type_t key_type = key_type_arg;
3094 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003095 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003096 unsigned char iv[16] = {0};
3097 size_t iv_size = 16;
3098 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003099 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003100 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003101 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003102 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003103 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003104 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003105 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003106 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3107 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003109
Gilles Peskine8817f612018-12-18 00:18:46 +01003110 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003111
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003112 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3113 psa_set_key_algorithm( &attributes, alg );
3114 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003115
Ronald Cron5425a212020-08-04 14:58:35 +02003116 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3117 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003118
Ronald Cron5425a212020-08-04 14:58:35 +02003119 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3120 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003121
Steven Cooreman177deba2020-09-07 17:14:14 +02003122 if( alg != PSA_ALG_ECB_NO_PADDING )
3123 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003124 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3125 iv, iv_size,
3126 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003127 }
3128
gabor-mezei-armceface22021-01-21 12:26:17 +01003129 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3130 TEST_ASSERT( output1_buffer_size <=
3131 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003132 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003133
Gilles Peskinee0866522019-02-19 19:44:00 +01003134 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003135
Gilles Peskine8817f612018-12-18 00:18:46 +01003136 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3137 output1, output1_buffer_size,
3138 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003139 TEST_ASSERT( function_output_length <=
3140 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3141 TEST_ASSERT( function_output_length <=
3142 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003143 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003144
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 PSA_ASSERT( psa_cipher_update( &operation1,
3146 input->x + first_part_size,
3147 input->len - first_part_size,
3148 output1, output1_buffer_size,
3149 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003150 TEST_ASSERT( function_output_length <=
3151 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3152 alg,
3153 input->len - first_part_size ) );
3154 TEST_ASSERT( function_output_length <=
3155 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003156 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_cipher_finish( &operation1,
3159 output1 + output1_length,
3160 output1_buffer_size - output1_length,
3161 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003162 TEST_ASSERT( function_output_length <=
3163 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3164 TEST_ASSERT( function_output_length <=
3165 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003166 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003167
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003169
Gilles Peskine048b7f02018-06-08 14:20:49 +02003170 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003171 TEST_ASSERT( output2_buffer_size <=
3172 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3173 TEST_ASSERT( output2_buffer_size <=
3174 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003175 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003176
Steven Cooreman177deba2020-09-07 17:14:14 +02003177 if( iv_length > 0 )
3178 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003179 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3180 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003181 }
Moran Pekerded84402018-06-06 16:36:50 +03003182
Gilles Peskine8817f612018-12-18 00:18:46 +01003183 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3184 output2, output2_buffer_size,
3185 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003186 TEST_ASSERT( function_output_length <=
3187 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3188 TEST_ASSERT( function_output_length <=
3189 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003190 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_cipher_update( &operation2,
3193 output1 + first_part_size,
3194 output1_length - first_part_size,
3195 output2, output2_buffer_size,
3196 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003197 TEST_ASSERT( function_output_length <=
3198 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3199 alg,
3200 output1_length - first_part_size ) );
3201 TEST_ASSERT( function_output_length <=
3202 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003203 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003204
Gilles Peskine8817f612018-12-18 00:18:46 +01003205 PSA_ASSERT( psa_cipher_finish( &operation2,
3206 output2 + output2_length,
3207 output2_buffer_size - output2_length,
3208 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003209 TEST_ASSERT( function_output_length <=
3210 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3211 TEST_ASSERT( function_output_length <=
3212 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003213 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003216
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003217 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003218
3219exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003220 psa_cipher_abort( &operation1 );
3221 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003222 mbedtls_free( output1 );
3223 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003224 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003225 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003226}
3227/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003228
Gilles Peskine20035e32018-02-03 22:44:14 +01003229/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003230void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003231 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003232 data_t *nonce,
3233 data_t *additional_data,
3234 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003235 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003236{
Ronald Cron5425a212020-08-04 14:58:35 +02003237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003238 psa_key_type_t key_type = key_type_arg;
3239 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003240 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003241 unsigned char *output_data = NULL;
3242 size_t output_size = 0;
3243 size_t output_length = 0;
3244 unsigned char *output_data2 = NULL;
3245 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003246 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003247 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003252 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3253 psa_set_key_algorithm( &attributes, alg );
3254 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003255
Gilles Peskine049c7532019-05-15 20:22:09 +02003256 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003257 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003258 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3259 key_bits = psa_get_key_bits( &attributes );
3260
3261 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3262 alg );
3263 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3264 * should be exact. */
3265 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3266 expected_result != PSA_ERROR_NOT_SUPPORTED )
3267 {
3268 TEST_EQUAL( output_size,
3269 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3270 TEST_ASSERT( output_size <=
3271 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3272 }
3273 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003274
Steven Cooremanf49478b2021-02-15 15:19:25 +01003275 status = psa_aead_encrypt( key, alg,
3276 nonce->x, nonce->len,
3277 additional_data->x,
3278 additional_data->len,
3279 input_data->x, input_data->len,
3280 output_data, output_size,
3281 &output_length );
3282
3283 /* If the operation is not supported, just skip and not fail in case the
3284 * encryption involves a common limitation of cryptography hardwares and
3285 * an alternative implementation. */
3286 if( status == PSA_ERROR_NOT_SUPPORTED )
3287 {
3288 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3289 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3290 }
3291
3292 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003293
3294 if( PSA_SUCCESS == expected_result )
3295 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003296 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003297
Gilles Peskine003a4a92019-05-14 16:09:40 +02003298 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3299 * should be exact. */
3300 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003301 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003302
gabor-mezei-armceface22021-01-21 12:26:17 +01003303 TEST_ASSERT( input_data->len <=
3304 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3305
Ronald Cron5425a212020-08-04 14:58:35 +02003306 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003307 nonce->x, nonce->len,
3308 additional_data->x,
3309 additional_data->len,
3310 output_data, output_length,
3311 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003312 &output_length2 ),
3313 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003314
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003315 ASSERT_COMPARE( input_data->x, input_data->len,
3316 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003318
Gilles Peskinea1cac842018-06-11 19:33:02 +02003319exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003320 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003321 mbedtls_free( output_data );
3322 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003323 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003324}
3325/* END_CASE */
3326
3327/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003328void aead_encrypt( int key_type_arg, data_t *key_data,
3329 int alg_arg,
3330 data_t *nonce,
3331 data_t *additional_data,
3332 data_t *input_data,
3333 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334{
Ronald Cron5425a212020-08-04 14:58:35 +02003335 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336 psa_key_type_t key_type = key_type_arg;
3337 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003338 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339 unsigned char *output_data = NULL;
3340 size_t output_size = 0;
3341 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003343 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344
Gilles Peskine8817f612018-12-18 00:18:46 +01003345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003346
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3348 psa_set_key_algorithm( &attributes, alg );
3349 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003350
Gilles Peskine049c7532019-05-15 20:22:09 +02003351 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003352 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003353 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3354 key_bits = psa_get_key_bits( &attributes );
3355
3356 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3357 alg );
3358 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3359 * should be exact. */
3360 TEST_EQUAL( output_size,
3361 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3362 TEST_ASSERT( output_size <=
3363 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3364 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365
Steven Cooremand588ea12021-01-11 19:36:04 +01003366 status = psa_aead_encrypt( key, alg,
3367 nonce->x, nonce->len,
3368 additional_data->x, additional_data->len,
3369 input_data->x, input_data->len,
3370 output_data, output_size,
3371 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003372
Ronald Cron28a45ed2021-02-09 20:35:42 +01003373 /* If the operation is not supported, just skip and not fail in case the
3374 * encryption involves a common limitation of cryptography hardwares and
3375 * an alternative implementation. */
3376 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003377 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003378 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3379 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003380 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003381
3382 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003383 ASSERT_COMPARE( expected_result->x, expected_result->len,
3384 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003385
Gilles Peskinea1cac842018-06-11 19:33:02 +02003386exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003387 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003388 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003389 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390}
3391/* END_CASE */
3392
3393/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003394void aead_decrypt( int key_type_arg, data_t *key_data,
3395 int alg_arg,
3396 data_t *nonce,
3397 data_t *additional_data,
3398 data_t *input_data,
3399 data_t *expected_data,
3400 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003401{
Ronald Cron5425a212020-08-04 14:58:35 +02003402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403 psa_key_type_t key_type = key_type_arg;
3404 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003405 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406 unsigned char *output_data = NULL;
3407 size_t output_size = 0;
3408 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003410 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003411 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003412
Gilles Peskine8817f612018-12-18 00:18:46 +01003413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003415 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3416 psa_set_key_algorithm( &attributes, alg );
3417 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418
Gilles Peskine049c7532019-05-15 20:22:09 +02003419 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003420 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003421 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3422 key_bits = psa_get_key_bits( &attributes );
3423
3424 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3425 alg );
3426 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3427 expected_result != PSA_ERROR_NOT_SUPPORTED )
3428 {
3429 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3430 * should be exact. */
3431 TEST_EQUAL( output_size,
3432 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3433 TEST_ASSERT( output_size <=
3434 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3435 }
3436 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003437
Steven Cooremand588ea12021-01-11 19:36:04 +01003438 status = psa_aead_decrypt( key, alg,
3439 nonce->x, nonce->len,
3440 additional_data->x,
3441 additional_data->len,
3442 input_data->x, input_data->len,
3443 output_data, output_size,
3444 &output_length );
3445
Ronald Cron28a45ed2021-02-09 20:35:42 +01003446 /* If the operation is not supported, just skip and not fail in case the
3447 * decryption involves a common limitation of cryptography hardwares and
3448 * an alternative implementation. */
3449 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003450 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003451 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3452 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003453 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003454
3455 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456
Gilles Peskine2d277862018-06-18 15:41:12 +02003457 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003458 ASSERT_COMPARE( expected_data->x, expected_data->len,
3459 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003462 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003463 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003464 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465}
3466/* END_CASE */
3467
3468/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003469void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3470 int alg_arg,
3471 data_t *nonce,
3472 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003473 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003474 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003475 int do_test_data_chunked,
3476 int do_set_lengths,
3477 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003478{
Paul Elliottd3f82412021-06-16 16:52:21 +01003479 size_t ad_part_len = 0;
3480 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003481 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003482
Paul Elliotte64deda2021-09-09 14:07:23 +01003483 /* Ensure that either one part of the test or the other is done, i.e this
3484 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003485 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3486
3487 /* Temporary whilst we have algorithms that cannot support chunking */
3488 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003489 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003490 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3491 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003492 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003493 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003494
Paul Elliott33746aa2021-09-15 16:40:40 +01003495 if( do_set_lengths )
3496 {
3497 if( ad_part_len & 0x01 )
3498 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3499 else
3500 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3501 }
3502
Paul Elliott329d5382021-07-22 17:10:45 +01003503 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003504 if( !aead_multipart_internal_func( key_type_arg, key_data,
3505 alg_arg, nonce,
3506 additional_data,
3507 ad_part_len,
3508 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003509 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003510 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003511 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003512 break;
3513
3514 /* length(0) part, length(ad_part_len) part, length(0) part... */
3515 mbedtls_test_set_step( 1000 + ad_part_len );
3516
3517 if( !aead_multipart_internal_func( key_type_arg, key_data,
3518 alg_arg, nonce,
3519 additional_data,
3520 ad_part_len,
3521 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003522 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003523 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003524 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003525 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003526 }
3527 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003528
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003529 /* Temporary whilst we have algorithms that cannot support chunking */
3530 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003531 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003532 for( data_part_len = 1; data_part_len <= input_data->len;
3533 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003534 {
Paul Elliott329d5382021-07-22 17:10:45 +01003535 /* Split data into length(data_part_len) parts. */
3536 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003537
Paul Elliott33746aa2021-09-15 16:40:40 +01003538 if( do_set_lengths )
3539 {
3540 if( data_part_len & 0x01 )
3541 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3542 else
3543 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3544 }
3545
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003546 if( !aead_multipart_internal_func( key_type_arg, key_data,
3547 alg_arg, nonce,
3548 additional_data, -1,
3549 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003550 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003551 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003552 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003553 break;
3554
3555 /* length(0) part, length(data_part_len) part, length(0) part... */
3556 mbedtls_test_set_step( 3000 + data_part_len );
3557
3558 if( !aead_multipart_internal_func( key_type_arg, key_data,
3559 alg_arg, nonce,
3560 additional_data, -1,
3561 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003562 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003563 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003564 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003565 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003566 }
3567 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003568
Paul Elliott0023e0a2021-04-27 10:06:22 +01003569
Paul Elliott8fc45162021-06-23 16:06:01 +01003570 /* Goto is required to silence warnings about unused labels, as we
3571 * don't actually do any test assertions in this function. */
3572 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003573}
3574/* END_CASE */
3575
3576/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003577void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3578 int alg_arg,
3579 data_t *nonce,
3580 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003581 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003583 int do_test_data_chunked,
3584 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003585 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003586{
Paul Elliottd3f82412021-06-16 16:52:21 +01003587 size_t ad_part_len = 0;
3588 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003589 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003590
Paul Elliotte64deda2021-09-09 14:07:23 +01003591 /* Ensure that either one part of the test or the other is done, i.e this
3592 * test does something. */
3593 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3594
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003595 /* Temporary whilst we have algorithms that cannot support chunking */
3596 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003597 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003598 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3599 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003600 {
Paul Elliott329d5382021-07-22 17:10:45 +01003601 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003602 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003603
Paul Elliott33746aa2021-09-15 16:40:40 +01003604 if( do_set_lengths )
3605 {
3606 if( ad_part_len & 0x01 )
3607 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3608 else
3609 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3610 }
3611
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003612 if( !aead_multipart_internal_func( key_type_arg, key_data,
3613 alg_arg, nonce,
3614 additional_data,
3615 ad_part_len,
3616 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003617 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003618 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003619 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003620 break;
3621
3622 /* length(0) part, length(ad_part_len) part, length(0) part... */
3623 mbedtls_test_set_step( 1000 + ad_part_len );
3624
3625 if( !aead_multipart_internal_func( key_type_arg, key_data,
3626 alg_arg, nonce,
3627 additional_data,
3628 ad_part_len,
3629 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003630 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003631 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003632 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003633 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003634 }
3635 }
3636
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003637 /* Temporary whilst we have algorithms that cannot support chunking */
3638 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003639 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003640 for( data_part_len = 1; data_part_len <= input_data->len;
3641 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003642 {
Paul Elliott329d5382021-07-22 17:10:45 +01003643 /* Split data into length(data_part_len) parts. */
3644 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003645
Paul Elliott33746aa2021-09-15 16:40:40 +01003646 if( do_set_lengths )
3647 {
3648 if( data_part_len & 0x01 )
3649 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3650 else
3651 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3652 }
3653
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003654 if( !aead_multipart_internal_func( key_type_arg, key_data,
3655 alg_arg, nonce,
3656 additional_data, -1,
3657 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003658 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003659 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003660 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003661 break;
3662
3663 /* length(0) part, length(data_part_len) part, length(0) part... */
3664 mbedtls_test_set_step( 3000 + data_part_len );
3665
3666 if( !aead_multipart_internal_func( key_type_arg, key_data,
3667 alg_arg, nonce,
3668 additional_data, -1,
3669 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003670 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003671 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003672 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003673 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003674 }
3675 }
3676
Paul Elliott8fc45162021-06-23 16:06:01 +01003677 /* Goto is required to silence warnings about unused labels, as we
3678 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003679 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003680}
3681/* END_CASE */
3682
3683/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003684void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3685 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003686 int nonce_length,
3687 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003688 data_t *additional_data,
3689 data_t *input_data,
3690 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003691{
3692
3693 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3694 psa_key_type_t key_type = key_type_arg;
3695 psa_algorithm_t alg = alg_arg;
3696 psa_aead_operation_t operation;
3697 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3699 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003700 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003701 size_t actual_nonce_length = 0;
3702 size_t expected_nonce_length = expected_nonce_length_arg;
3703 unsigned char *output = NULL;
3704 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003705 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003706 size_t ciphertext_size = 0;
3707 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003708 size_t tag_length = 0;
3709 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003710
3711 PSA_ASSERT( psa_crypto_init( ) );
3712
3713 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3714 psa_set_key_algorithm( & attributes, alg );
3715 psa_set_key_type( & attributes, key_type );
3716
3717 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3718 &key ) );
3719
3720 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3721
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003722 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3723
Paul Elliottf1277632021-08-24 18:11:37 +01003724 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003725
Paul Elliottf1277632021-08-24 18:11:37 +01003726 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003727
Paul Elliottf1277632021-08-24 18:11:37 +01003728 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003729
Paul Elliottf1277632021-08-24 18:11:37 +01003730 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003731
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003732 operation = psa_aead_operation_init( );
3733
3734 status = psa_aead_encrypt_setup( &operation, key, alg );
3735
3736 /* If the operation is not supported, just skip and not fail in case the
3737 * encryption involves a common limitation of cryptography hardwares and
3738 * an alternative implementation. */
3739 if( status == PSA_ERROR_NOT_SUPPORTED )
3740 {
3741 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003742 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003743 }
3744
3745 PSA_ASSERT( status );
3746
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003747 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003748 nonce_length,
3749 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003750
Paul Elliott693bf312021-07-23 17:40:41 +01003751 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003752
Paul Elliottf1277632021-08-24 18:11:37 +01003753 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003754
Paul Elliottf1277632021-08-24 18:11:37 +01003755 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003756
Paul Elliott693bf312021-07-23 17:40:41 +01003757 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003758 {
3759
3760 /* Ensure we can still complete operation. */
3761
3762 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3763 additional_data->len ) );
3764
3765 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003766 output, output_size,
3767 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003768
Paul Elliottf1277632021-08-24 18:11:37 +01003769 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3770 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003771 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3772 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003773
3774exit:
3775 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003776 mbedtls_free( output );
3777 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003778 psa_aead_abort( &operation );
3779 PSA_DONE( );
3780}
3781/* END_CASE */
3782
3783/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003784void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3785 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003786 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003787 data_t *additional_data,
3788 data_t *input_data,
3789 int expected_status_arg )
3790{
3791
3792 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3793 psa_key_type_t key_type = key_type_arg;
3794 psa_algorithm_t alg = alg_arg;
3795 psa_aead_operation_t operation;
3796 uint8_t *nonce_buffer = NULL;
3797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3798 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3799 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003800 unsigned char *output = NULL;
3801 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003802 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003803 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003804 size_t ciphertext_size = 0;
3805 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003806 size_t tag_length = 0;
3807 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003808 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003809
3810 PSA_ASSERT( psa_crypto_init( ) );
3811
3812 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3813 psa_set_key_algorithm( &attributes, alg );
3814 psa_set_key_type( &attributes, key_type );
3815
3816 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3817 &key ) );
3818
3819 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3820
3821 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3822
Paul Elliott6f0e7202021-08-25 12:57:18 +01003823 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003824
Paul Elliott6f0e7202021-08-25 12:57:18 +01003825 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003826
Paul Elliott6f0e7202021-08-25 12:57:18 +01003827 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003828
Paul Elliott6f0e7202021-08-25 12:57:18 +01003829 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003830
3831 operation = psa_aead_operation_init( );
3832
3833 status = psa_aead_encrypt_setup( &operation, key, alg );
3834
3835 /* If the operation is not supported, just skip and not fail in case the
3836 * encryption involves a common limitation of cryptography hardwares and
3837 * an alternative implementation. */
3838 if( status == PSA_ERROR_NOT_SUPPORTED )
3839 {
3840 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003841 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003842 }
3843
3844 PSA_ASSERT( status );
3845
Paul Elliott4023ffd2021-09-10 16:21:22 +01003846 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3847 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003848 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003849 /* Arbitrary size buffer, to test zero length valid buffer. */
3850 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003851 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003852 }
3853 else
3854 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003855 /* If length is zero, then this will return NULL. */
3856 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003857 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003858
Paul Elliott4023ffd2021-09-10 16:21:22 +01003859 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003860 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003861 for( index = 0; index < nonce_length - 1; ++index )
3862 {
3863 nonce_buffer[index] = 'a' + index;
3864 }
Paul Elliott66696b52021-08-16 18:42:41 +01003865 }
Paul Elliott863864a2021-07-23 17:28:31 +01003866 }
3867
Paul Elliott6f0e7202021-08-25 12:57:18 +01003868 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003869
Paul Elliott693bf312021-07-23 17:40:41 +01003870 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003871
3872 if( expected_status == PSA_SUCCESS )
3873 {
3874 /* Ensure we can still complete operation. */
3875
3876 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3877 additional_data->len ) );
3878
3879 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003880 output, output_size,
3881 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003882
Paul Elliott6f0e7202021-08-25 12:57:18 +01003883 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3884 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003885 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3886 }
3887
3888exit:
3889 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003890 mbedtls_free( output );
3891 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003892 mbedtls_free( nonce_buffer );
3893 psa_aead_abort( &operation );
3894 PSA_DONE( );
3895}
3896/* END_CASE */
3897
3898/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003899void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3900 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003901 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003902 data_t *nonce,
3903 data_t *additional_data,
3904 data_t *input_data,
3905 int expected_status_arg )
3906{
3907
3908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3909 psa_key_type_t key_type = key_type_arg;
3910 psa_algorithm_t alg = alg_arg;
3911 psa_aead_operation_t operation;
3912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3913 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3914 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003915 unsigned char *output = NULL;
3916 unsigned char *ciphertext = NULL;
3917 size_t output_size = output_size_arg;
3918 size_t ciphertext_size = 0;
3919 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003920 size_t tag_length = 0;
3921 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3922
3923 PSA_ASSERT( psa_crypto_init( ) );
3924
3925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3926 psa_set_key_algorithm( &attributes, alg );
3927 psa_set_key_type( &attributes, key_type );
3928
3929 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3930 &key ) );
3931
3932 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3933
Paul Elliottc6d11d02021-09-01 12:04:23 +01003934 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003935
Paul Elliottc6d11d02021-09-01 12:04:23 +01003936 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003937
Paul Elliottc6d11d02021-09-01 12:04:23 +01003938 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003939
3940 operation = psa_aead_operation_init( );
3941
3942 status = psa_aead_encrypt_setup( &operation, key, alg );
3943
3944 /* If the operation is not supported, just skip and not fail in case the
3945 * encryption involves a common limitation of cryptography hardwares and
3946 * an alternative implementation. */
3947 if( status == PSA_ERROR_NOT_SUPPORTED )
3948 {
3949 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3950 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3951 }
3952
3953 PSA_ASSERT( status );
3954
3955 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3956
3957 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3958 additional_data->len ) );
3959
3960 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003961 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003962
3963 TEST_EQUAL( status, expected_status );
3964
3965 if( expected_status == PSA_SUCCESS )
3966 {
3967 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003968 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3969 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003970 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3971 }
3972
3973exit:
3974 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003975 mbedtls_free( output );
3976 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003977 psa_aead_abort( &operation );
3978 PSA_DONE( );
3979}
3980/* END_CASE */
3981
Paul Elliott91b021e2021-07-23 18:52:31 +01003982/* BEGIN_CASE */
3983void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3984 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003985 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003986 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003987 data_t *nonce,
3988 data_t *additional_data,
3989 data_t *input_data,
3990 int expected_status_arg )
3991{
3992
3993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3994 psa_key_type_t key_type = key_type_arg;
3995 psa_algorithm_t alg = alg_arg;
3996 psa_aead_operation_t operation;
3997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3998 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3999 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004000 unsigned char *ciphertext = NULL;
4001 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004002 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004003 size_t ciphertext_size = 0;
4004 size_t ciphertext_length = 0;
4005 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004006 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004007 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004008
4009 PSA_ASSERT( psa_crypto_init( ) );
4010
4011 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4012 psa_set_key_algorithm( &attributes, alg );
4013 psa_set_key_type( &attributes, key_type );
4014
4015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4016 &key ) );
4017
4018 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4019
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004020 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004021
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004022 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004023
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004024 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004025
Paul Elliott719c1322021-09-13 18:27:22 +01004026 ASSERT_ALLOC( tag_buffer, tag_size );
4027
Paul Elliott91b021e2021-07-23 18:52:31 +01004028 operation = psa_aead_operation_init( );
4029
4030 status = psa_aead_encrypt_setup( &operation, key, alg );
4031
4032 /* If the operation is not supported, just skip and not fail in case the
4033 * encryption involves a common limitation of cryptography hardwares and
4034 * an alternative implementation. */
4035 if( status == PSA_ERROR_NOT_SUPPORTED )
4036 {
4037 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4038 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4039 }
4040
4041 PSA_ASSERT( status );
4042
4043 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4044
4045 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4046 additional_data->len ) );
4047
4048 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004049 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004050
4051 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004052 status = psa_aead_finish( &operation, finish_ciphertext,
4053 finish_ciphertext_size,
4054 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004055 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004056
4057 TEST_EQUAL( status, expected_status );
4058
4059exit:
4060 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004061 mbedtls_free( ciphertext );
4062 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004063 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004064 psa_aead_abort( &operation );
4065 PSA_DONE( );
4066}
4067/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004068
4069/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004070void aead_multipart_verify( int key_type_arg, data_t *key_data,
4071 int alg_arg,
4072 data_t *nonce,
4073 data_t *additional_data,
4074 data_t *input_data,
4075 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004076 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004077 int expected_status_arg )
4078{
4079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4080 psa_key_type_t key_type = key_type_arg;
4081 psa_algorithm_t alg = alg_arg;
4082 psa_aead_operation_t operation;
4083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4084 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4085 psa_status_t expected_status = expected_status_arg;
4086 unsigned char *plaintext = NULL;
4087 unsigned char *finish_plaintext = NULL;
4088 size_t plaintext_size = 0;
4089 size_t plaintext_length = 0;
4090 size_t verify_plaintext_size = 0;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004091 tagusage_method tag_usage = tag_usage_arg;
4092 unsigned char *tag_buffer = NULL;
4093 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004094
4095 PSA_ASSERT( psa_crypto_init( ) );
4096
4097 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4098 psa_set_key_algorithm( &attributes, alg );
4099 psa_set_key_type( &attributes, key_type );
4100
4101 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4102 &key ) );
4103
4104 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4105
4106 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4107 input_data->len );
4108
4109 ASSERT_ALLOC( plaintext, plaintext_size );
4110
4111 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4112
4113 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4114
4115 operation = psa_aead_operation_init( );
4116
4117 status = psa_aead_decrypt_setup( &operation, key, alg );
4118
4119 /* If the operation is not supported, just skip and not fail in case the
4120 * encryption involves a common limitation of cryptography hardwares and
4121 * an alternative implementation. */
4122 if( status == PSA_ERROR_NOT_SUPPORTED )
4123 {
4124 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4125 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4126 }
4127
4128 PSA_ASSERT( status );
4129
4130 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4131
4132 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4133 additional_data->len ) );
4134
4135 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4136 input_data->len,
4137 plaintext, plaintext_size,
4138 &plaintext_length ) );
4139
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004140 if( tag_usage == USE_GIVEN_TAG )
4141 {
4142 tag_buffer = tag->x;
4143 tag_size = tag->len;
4144 }
4145
Paul Elliott9961a662021-09-17 19:19:02 +01004146 status = psa_aead_verify( &operation, finish_plaintext,
4147 verify_plaintext_size,
4148 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004149 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004150
4151 TEST_EQUAL( status, expected_status );
4152
4153exit:
4154 psa_destroy_key( key );
4155 mbedtls_free( plaintext );
4156 mbedtls_free( finish_plaintext );
4157 psa_aead_abort( &operation );
4158 PSA_DONE( );
4159}
4160/* END_CASE */
4161
Paul Elliott9961a662021-09-17 19:19:02 +01004162/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004163void aead_multipart_setup( int key_type_arg, data_t *key_data,
4164 int alg_arg, int expected_status_arg )
4165{
4166 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4167 psa_key_type_t key_type = key_type_arg;
4168 psa_algorithm_t alg = alg_arg;
4169 psa_aead_operation_t operation;
4170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4171 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4172 psa_status_t expected_status = expected_status_arg;
4173
4174 PSA_ASSERT( psa_crypto_init( ) );
4175
4176 psa_set_key_usage_flags( &attributes,
4177 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4178 psa_set_key_algorithm( &attributes, alg );
4179 psa_set_key_type( &attributes, key_type );
4180
4181 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4182 &key ) );
4183
4184 mbedtls_test_set_step( 0 );
4185
4186 status = psa_aead_encrypt_setup( &operation, key, alg );
4187
4188 TEST_EQUAL( status, expected_status );
4189
4190 psa_aead_abort( &operation );
4191
4192 operation = psa_aead_operation_init( );
4193
4194 mbedtls_test_set_step( 1 );
4195
4196 status = psa_aead_decrypt_setup( &operation, key, alg );
4197
4198 TEST_EQUAL(status, expected_status );
4199
4200exit:
4201 psa_destroy_key( key );
4202 psa_aead_abort( &operation );
4203 PSA_DONE( );
4204}
4205/* END_CASE */
4206
4207/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004208void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4209 int alg_arg,
4210 data_t *nonce,
4211 data_t *additional_data,
4212 data_t *input_data )
4213{
4214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4215 psa_key_type_t key_type = key_type_arg;
4216 psa_algorithm_t alg = alg_arg;
4217 psa_aead_operation_t operation;
4218 unsigned char *output_data = NULL;
4219 unsigned char *final_data = NULL;
4220 size_t output_size = 0;
4221 size_t finish_output_size = 0;
4222 size_t output_length = 0;
4223 size_t key_bits = 0;
4224 size_t tag_length = 0;
4225 size_t tag_size = 0;
4226 size_t nonce_length = 0;
4227 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4228 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4229 size_t output_part_length = 0;
4230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4231
4232 PSA_ASSERT( psa_crypto_init( ) );
4233
4234 psa_set_key_usage_flags( & attributes,
4235 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4236 psa_set_key_algorithm( & attributes, alg );
4237 psa_set_key_type( & attributes, key_type );
4238
4239 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4240 &key ) );
4241
4242 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4243 key_bits = psa_get_key_bits( &attributes );
4244
4245 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4246
4247 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4248
4249 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4250
4251 ASSERT_ALLOC( output_data, output_size );
4252
4253 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4254
4255 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4256
4257 ASSERT_ALLOC( final_data, finish_output_size );
4258
4259 /* Test all operations error without calling setup first. */
4260
4261 operation = psa_aead_operation_init( );
4262
4263 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4264 PSA_ERROR_BAD_STATE );
4265
4266 psa_aead_abort( &operation );
4267
4268 operation = psa_aead_operation_init( );
4269
4270 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4271 PSA_AEAD_NONCE_MAX_SIZE,
4272 &nonce_length ),
4273 PSA_ERROR_BAD_STATE );
4274
4275 psa_aead_abort( &operation );
4276
Paul Elliott481be342021-07-16 17:38:47 +01004277 /* ------------------------------------------------------- */
4278
Paul Elliottc23a9a02021-06-21 18:32:46 +01004279 operation = psa_aead_operation_init( );
4280
4281 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4282 input_data->len ),
4283 PSA_ERROR_BAD_STATE );
4284
4285 psa_aead_abort( &operation );
4286
Paul Elliott481be342021-07-16 17:38:47 +01004287 /* ------------------------------------------------------- */
4288
Paul Elliottc23a9a02021-06-21 18:32:46 +01004289 operation = psa_aead_operation_init( );
4290
4291 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4292 additional_data->len ),
4293 PSA_ERROR_BAD_STATE );
4294
4295 psa_aead_abort( &operation );
4296
Paul Elliott481be342021-07-16 17:38:47 +01004297 /* ------------------------------------------------------- */
4298
Paul Elliottc23a9a02021-06-21 18:32:46 +01004299 operation = psa_aead_operation_init( );
4300
4301 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4302 input_data->len, output_data,
4303 output_size, &output_length ),
4304 PSA_ERROR_BAD_STATE );
4305
4306 psa_aead_abort( &operation );
4307
Paul Elliott481be342021-07-16 17:38:47 +01004308 /* ------------------------------------------------------- */
4309
Paul Elliottc23a9a02021-06-21 18:32:46 +01004310 operation = psa_aead_operation_init( );
4311
4312 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4313 finish_output_size,
4314 &output_part_length,
4315 tag_buffer, tag_length,
4316 &tag_size ),
4317 PSA_ERROR_BAD_STATE );
4318
4319 psa_aead_abort( &operation );
4320
Paul Elliott481be342021-07-16 17:38:47 +01004321 /* ------------------------------------------------------- */
4322
Paul Elliottc23a9a02021-06-21 18:32:46 +01004323 operation = psa_aead_operation_init( );
4324
4325 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4326 finish_output_size,
4327 &output_part_length,
4328 tag_buffer,
4329 tag_length ),
4330 PSA_ERROR_BAD_STATE );
4331
4332 psa_aead_abort( &operation );
4333
4334 /* Test for double setups. */
4335
4336 operation = psa_aead_operation_init( );
4337
4338 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4339
4340 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4341 PSA_ERROR_BAD_STATE );
4342
4343 psa_aead_abort( &operation );
4344
Paul Elliott481be342021-07-16 17:38:47 +01004345 /* ------------------------------------------------------- */
4346
Paul Elliottc23a9a02021-06-21 18:32:46 +01004347 operation = psa_aead_operation_init( );
4348
4349 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4350
4351 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4352 PSA_ERROR_BAD_STATE );
4353
4354 psa_aead_abort( &operation );
4355
Paul Elliott374a2be2021-07-16 17:53:40 +01004356 /* ------------------------------------------------------- */
4357
4358 operation = psa_aead_operation_init( );
4359
4360 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4361
4362 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4363 PSA_ERROR_BAD_STATE );
4364
4365 psa_aead_abort( &operation );
4366
4367 /* ------------------------------------------------------- */
4368
4369 operation = psa_aead_operation_init( );
4370
4371 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4372
4373 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4374 PSA_ERROR_BAD_STATE );
4375
4376 psa_aead_abort( &operation );
4377
Paul Elliottc23a9a02021-06-21 18:32:46 +01004378 /* Test for not setting a nonce. */
4379
4380 operation = psa_aead_operation_init( );
4381
4382 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4383
4384 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4385 additional_data->len ),
4386 PSA_ERROR_BAD_STATE );
4387
4388 psa_aead_abort( &operation );
4389
Paul Elliott7f628422021-09-01 12:08:29 +01004390 /* ------------------------------------------------------- */
4391
4392 operation = psa_aead_operation_init( );
4393
4394 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4395
4396 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4397 input_data->len, output_data,
4398 output_size, &output_length ),
4399 PSA_ERROR_BAD_STATE );
4400
4401 psa_aead_abort( &operation );
4402
Paul Elliottc23a9a02021-06-21 18:32:46 +01004403 /* Test for double setting nonce. */
4404
4405 operation = psa_aead_operation_init( );
4406
4407 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4408
4409 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4410
4411 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4412 PSA_ERROR_BAD_STATE );
4413
4414 psa_aead_abort( &operation );
4415
Paul Elliott374a2be2021-07-16 17:53:40 +01004416 /* Test for double generating nonce. */
4417
4418 operation = psa_aead_operation_init( );
4419
4420 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4421
4422 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4423 PSA_AEAD_NONCE_MAX_SIZE,
4424 &nonce_length ) );
4425
4426 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4427 PSA_AEAD_NONCE_MAX_SIZE,
4428 &nonce_length ),
4429 PSA_ERROR_BAD_STATE );
4430
4431
4432 psa_aead_abort( &operation );
4433
4434 /* Test for generate nonce then set and vice versa */
4435
4436 operation = psa_aead_operation_init( );
4437
4438 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4439
4440 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4441 PSA_AEAD_NONCE_MAX_SIZE,
4442 &nonce_length ) );
4443
4444 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4445 PSA_ERROR_BAD_STATE );
4446
4447 psa_aead_abort( &operation );
4448
4449 /* ------------------------------------------------------- */
4450
4451 operation = psa_aead_operation_init( );
4452
4453 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4454
4455 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4456
4457 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4458 PSA_AEAD_NONCE_MAX_SIZE,
4459 &nonce_length ),
4460 PSA_ERROR_BAD_STATE );
4461
4462 psa_aead_abort( &operation );
4463
Paul Elliott7220cae2021-06-22 17:25:57 +01004464 /* Test for generating nonce in decrypt setup. */
4465
4466 operation = psa_aead_operation_init( );
4467
4468 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4469
4470 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4471 PSA_AEAD_NONCE_MAX_SIZE,
4472 &nonce_length ),
4473 PSA_ERROR_BAD_STATE );
4474
4475 psa_aead_abort( &operation );
4476
Paul Elliottc23a9a02021-06-21 18:32:46 +01004477 /* Test for setting lengths twice. */
4478
4479 operation = psa_aead_operation_init( );
4480
4481 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4482
4483 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4484
4485 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4486 input_data->len ) );
4487
4488 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4489 input_data->len ),
4490 PSA_ERROR_BAD_STATE );
4491
4492 psa_aead_abort( &operation );
4493
4494 /* Test for setting lengths after already starting data. */
4495
4496 operation = psa_aead_operation_init( );
4497
4498 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4499
4500 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4501
Paul Elliottf94bd992021-09-19 18:15:59 +01004502 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4503 additional_data->len ) );
4504
4505 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4506 input_data->len ),
4507 PSA_ERROR_BAD_STATE );
4508
4509 psa_aead_abort( &operation );
4510
4511 /* ------------------------------------------------------- */
4512
4513 operation = psa_aead_operation_init( );
4514
4515 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4516
4517 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4518
Paul Elliottc23a9a02021-06-21 18:32:46 +01004519 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4520 input_data->len, output_data,
4521 output_size, &output_length ) );
4522
4523 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4524 input_data->len ),
4525 PSA_ERROR_BAD_STATE );
4526
4527 psa_aead_abort( &operation );
4528
Paul Elliott243080c2021-07-21 19:01:17 +01004529 /* Test for not sending any additional data or data after setting non zero
4530 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004531
4532 operation = psa_aead_operation_init( );
4533
4534 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4535
4536 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4537
4538 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4539 input_data->len ) );
4540
4541 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4542 finish_output_size,
4543 &output_part_length,
4544 tag_buffer, tag_length,
4545 &tag_size ),
4546 PSA_ERROR_INVALID_ARGUMENT );
4547
4548 psa_aead_abort( &operation );
4549
Paul Elliott243080c2021-07-21 19:01:17 +01004550 /* Test for not sending any additional data or data after setting non-zero
4551 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004552
4553 operation = psa_aead_operation_init( );
4554
4555 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4556
4557 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4558
4559 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4560 input_data->len ) );
4561
4562 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4563 finish_output_size,
4564 &output_part_length,
4565 tag_buffer,
4566 tag_length ),
4567 PSA_ERROR_INVALID_ARGUMENT );
4568
4569 psa_aead_abort( &operation );
4570
Paul Elliott243080c2021-07-21 19:01:17 +01004571 /* Test for not sending any additional data after setting a non-zero length
4572 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004573
4574 operation = psa_aead_operation_init( );
4575
4576 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4577
4578 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4579
4580 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4581 input_data->len ) );
4582
4583 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4584 input_data->len, output_data,
4585 output_size, &output_length ),
4586 PSA_ERROR_INVALID_ARGUMENT );
4587
4588 psa_aead_abort( &operation );
4589
Paul Elliottf94bd992021-09-19 18:15:59 +01004590 /* Test for not sending any data after setting a non-zero length for it.*/
4591
4592 operation = psa_aead_operation_init( );
4593
4594 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4595
4596 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4597
4598 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4599 input_data->len ) );
4600
4601 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4602 additional_data->len ) );
4603
4604 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4605 finish_output_size,
4606 &output_part_length,
4607 tag_buffer, tag_length,
4608 &tag_size ),
4609 PSA_ERROR_INVALID_ARGUMENT );
4610
4611 psa_aead_abort( &operation );
4612
Paul Elliottb0450fe2021-09-01 15:06:26 +01004613 /* Test for sending too much additional data after setting lengths. */
4614
4615 operation = psa_aead_operation_init( );
4616
4617 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4618
4619 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4620
4621 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4622
4623
4624 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4625 additional_data->len ),
4626 PSA_ERROR_INVALID_ARGUMENT );
4627
4628 psa_aead_abort( &operation );
4629
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004630 operation = psa_aead_operation_init( );
4631
4632 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4633
4634 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4635
4636 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4637 input_data->len ) );
4638
4639 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4640 additional_data->len ) );
4641
4642 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4643 1 ),
4644 PSA_ERROR_INVALID_ARGUMENT );
4645
4646 psa_aead_abort( &operation );
4647
Paul Elliottb0450fe2021-09-01 15:06:26 +01004648 /* Test for sending too much data after setting lengths. */
4649
4650 operation = psa_aead_operation_init( );
4651
4652 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4653
4654 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4655
4656 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4657
4658 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4659 input_data->len, output_data,
4660 output_size, &output_length ),
4661 PSA_ERROR_INVALID_ARGUMENT );
4662
4663 psa_aead_abort( &operation );
4664
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004665 operation = psa_aead_operation_init( );
4666
4667 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4668
4669 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4670
4671 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4672 input_data->len ) );
4673
4674 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4675 additional_data->len ) );
4676
4677 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4678 input_data->len, output_data,
4679 output_size, &output_length ) );
4680
4681 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4682 1, output_data,
4683 output_size, &output_length ),
4684 PSA_ERROR_INVALID_ARGUMENT );
4685
4686 psa_aead_abort( &operation );
4687
Paul Elliottc23a9a02021-06-21 18:32:46 +01004688 /* Test sending additional data after data. */
4689
4690 operation = psa_aead_operation_init( );
4691
4692 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4693
4694 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4695
4696 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4697 input_data->len, output_data,
4698 output_size, &output_length ) );
4699
4700 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4701 additional_data->len ),
4702 PSA_ERROR_BAD_STATE );
4703
4704 psa_aead_abort( &operation );
4705
Paul Elliott534d0b42021-06-22 19:15:20 +01004706 /* Test calling finish on decryption. */
4707
4708 operation = psa_aead_operation_init( );
4709
4710 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4711
4712 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4713
4714 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4715 finish_output_size,
4716 &output_part_length,
4717 tag_buffer, tag_length,
4718 &tag_size ),
4719 PSA_ERROR_BAD_STATE );
4720
4721 psa_aead_abort( &operation );
4722
4723 /* Test calling verify on encryption. */
4724
4725 operation = psa_aead_operation_init( );
4726
4727 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4728
4729 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4730
4731 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4732 finish_output_size,
4733 &output_part_length,
4734 tag_buffer,
4735 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004736 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004737
4738 psa_aead_abort( &operation );
4739
4740
Paul Elliottc23a9a02021-06-21 18:32:46 +01004741exit:
4742 psa_destroy_key( key );
4743 psa_aead_abort( &operation );
4744 mbedtls_free( output_data );
4745 mbedtls_free( final_data );
4746 PSA_DONE( );
4747}
4748/* END_CASE */
4749
4750/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004751void signature_size( int type_arg,
4752 int bits,
4753 int alg_arg,
4754 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004755{
4756 psa_key_type_t type = type_arg;
4757 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004758 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004759
Gilles Peskinefe11b722018-12-18 00:24:04 +01004760 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004761
Gilles Peskinee59236f2018-01-27 23:32:46 +01004762exit:
4763 ;
4764}
4765/* END_CASE */
4766
4767/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004768void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4769 int alg_arg, data_t *input_data,
4770 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004771{
Ronald Cron5425a212020-08-04 14:58:35 +02004772 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004773 psa_key_type_t key_type = key_type_arg;
4774 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004775 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004776 unsigned char *signature = NULL;
4777 size_t signature_size;
4778 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004779 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004780
Gilles Peskine8817f612018-12-18 00:18:46 +01004781 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004782
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004783 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004784 psa_set_key_algorithm( &attributes, alg );
4785 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004786
Gilles Peskine049c7532019-05-15 20:22:09 +02004787 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004788 &key ) );
4789 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004790 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004791
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004792 /* Allocate a buffer which has the size advertized by the
4793 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004794 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004795 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004796 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004797 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004798 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004799
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004800 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004801 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004802 input_data->x, input_data->len,
4803 signature, signature_size,
4804 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004805 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004806 ASSERT_COMPARE( output_data->x, output_data->len,
4807 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004808
4809exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004810 /*
4811 * Key attributes may have been returned by psa_get_key_attributes()
4812 * thus reset them as required.
4813 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004814 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004815
Ronald Cron5425a212020-08-04 14:58:35 +02004816 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004817 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004818 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004819}
4820/* END_CASE */
4821
4822/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004823void sign_hash_fail( int key_type_arg, data_t *key_data,
4824 int alg_arg, data_t *input_data,
4825 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004826{
Ronald Cron5425a212020-08-04 14:58:35 +02004827 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004828 psa_key_type_t key_type = key_type_arg;
4829 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004830 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004831 psa_status_t actual_status;
4832 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004833 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004834 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004835 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004836
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004837 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004838
Gilles Peskine8817f612018-12-18 00:18:46 +01004839 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004840
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004841 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004842 psa_set_key_algorithm( &attributes, alg );
4843 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004844
Gilles Peskine049c7532019-05-15 20:22:09 +02004845 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004846 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004847
Ronald Cron5425a212020-08-04 14:58:35 +02004848 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004849 input_data->x, input_data->len,
4850 signature, signature_size,
4851 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004852 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004853 /* The value of *signature_length is unspecified on error, but
4854 * whatever it is, it should be less than signature_size, so that
4855 * if the caller tries to read *signature_length bytes without
4856 * checking the error code then they don't overflow a buffer. */
4857 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004858
4859exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004860 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004861 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004862 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004863 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004864}
4865/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004866
4867/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004868void sign_verify_hash( int key_type_arg, data_t *key_data,
4869 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004870{
Ronald Cron5425a212020-08-04 14:58:35 +02004871 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004872 psa_key_type_t key_type = key_type_arg;
4873 psa_algorithm_t alg = alg_arg;
4874 size_t key_bits;
4875 unsigned char *signature = NULL;
4876 size_t signature_size;
4877 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004879
Gilles Peskine8817f612018-12-18 00:18:46 +01004880 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004881
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004882 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004883 psa_set_key_algorithm( &attributes, alg );
4884 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004885
Gilles Peskine049c7532019-05-15 20:22:09 +02004886 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004887 &key ) );
4888 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004889 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004890
4891 /* Allocate a buffer which has the size advertized by the
4892 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004893 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004894 key_bits, alg );
4895 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004896 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004897 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004898
4899 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004900 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004901 input_data->x, input_data->len,
4902 signature, signature_size,
4903 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004904 /* Check that the signature length looks sensible. */
4905 TEST_ASSERT( signature_length <= signature_size );
4906 TEST_ASSERT( signature_length > 0 );
4907
4908 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004909 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004910 input_data->x, input_data->len,
4911 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004912
4913 if( input_data->len != 0 )
4914 {
4915 /* Flip a bit in the input and verify that the signature is now
4916 * detected as invalid. Flip a bit at the beginning, not at the end,
4917 * because ECDSA may ignore the last few bits of the input. */
4918 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004919 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004920 input_data->x, input_data->len,
4921 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004922 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004923 }
4924
4925exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004926 /*
4927 * Key attributes may have been returned by psa_get_key_attributes()
4928 * thus reset them as required.
4929 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004930 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004931
Ronald Cron5425a212020-08-04 14:58:35 +02004932 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004933 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004934 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004935}
4936/* END_CASE */
4937
4938/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004939void verify_hash( int key_type_arg, data_t *key_data,
4940 int alg_arg, data_t *hash_data,
4941 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004942{
Ronald Cron5425a212020-08-04 14:58:35 +02004943 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004944 psa_key_type_t key_type = key_type_arg;
4945 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004946 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004947
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004948 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004949
Gilles Peskine8817f612018-12-18 00:18:46 +01004950 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004951
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004952 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004953 psa_set_key_algorithm( &attributes, alg );
4954 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004955
Gilles Peskine049c7532019-05-15 20:22:09 +02004956 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004957 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004958
Ronald Cron5425a212020-08-04 14:58:35 +02004959 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004960 hash_data->x, hash_data->len,
4961 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004962
itayzafrir5c753392018-05-08 11:18:38 +03004963exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004964 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004965 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004966 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004967}
4968/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004969
4970/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004971void verify_hash_fail( int key_type_arg, data_t *key_data,
4972 int alg_arg, data_t *hash_data,
4973 data_t *signature_data,
4974 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004975{
Ronald Cron5425a212020-08-04 14:58:35 +02004976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004977 psa_key_type_t key_type = key_type_arg;
4978 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004979 psa_status_t actual_status;
4980 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004981 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004982
Gilles Peskine8817f612018-12-18 00:18:46 +01004983 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004984
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004985 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004986 psa_set_key_algorithm( &attributes, alg );
4987 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004988
Gilles Peskine049c7532019-05-15 20:22:09 +02004989 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004990 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004991
Ronald Cron5425a212020-08-04 14:58:35 +02004992 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004993 hash_data->x, hash_data->len,
4994 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004995 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004996
4997exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004998 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004999 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005000 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005001}
5002/* END_CASE */
5003
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005004/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005005void sign_message_deterministic( int key_type_arg,
5006 data_t *key_data,
5007 int alg_arg,
5008 data_t *input_data,
5009 data_t *output_data )
5010{
5011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5012 psa_key_type_t key_type = key_type_arg;
5013 psa_algorithm_t alg = alg_arg;
5014 size_t key_bits;
5015 unsigned char *signature = NULL;
5016 size_t signature_size;
5017 size_t signature_length = 0xdeadbeef;
5018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5019
5020 PSA_ASSERT( psa_crypto_init( ) );
5021
5022 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5023 psa_set_key_algorithm( &attributes, alg );
5024 psa_set_key_type( &attributes, key_type );
5025
5026 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5027 &key ) );
5028 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5029 key_bits = psa_get_key_bits( &attributes );
5030
5031 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5032 TEST_ASSERT( signature_size != 0 );
5033 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5034 ASSERT_ALLOC( signature, signature_size );
5035
5036 PSA_ASSERT( psa_sign_message( key, alg,
5037 input_data->x, input_data->len,
5038 signature, signature_size,
5039 &signature_length ) );
5040
5041 ASSERT_COMPARE( output_data->x, output_data->len,
5042 signature, signature_length );
5043
5044exit:
5045 psa_reset_key_attributes( &attributes );
5046
5047 psa_destroy_key( key );
5048 mbedtls_free( signature );
5049 PSA_DONE( );
5050
5051}
5052/* END_CASE */
5053
5054/* BEGIN_CASE */
5055void sign_message_fail( int key_type_arg,
5056 data_t *key_data,
5057 int alg_arg,
5058 data_t *input_data,
5059 int signature_size_arg,
5060 int expected_status_arg )
5061{
5062 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5063 psa_key_type_t key_type = key_type_arg;
5064 psa_algorithm_t alg = alg_arg;
5065 size_t signature_size = signature_size_arg;
5066 psa_status_t actual_status;
5067 psa_status_t expected_status = expected_status_arg;
5068 unsigned char *signature = NULL;
5069 size_t signature_length = 0xdeadbeef;
5070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5071
5072 ASSERT_ALLOC( signature, signature_size );
5073
5074 PSA_ASSERT( psa_crypto_init( ) );
5075
5076 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5077 psa_set_key_algorithm( &attributes, alg );
5078 psa_set_key_type( &attributes, key_type );
5079
5080 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5081 &key ) );
5082
5083 actual_status = psa_sign_message( key, alg,
5084 input_data->x, input_data->len,
5085 signature, signature_size,
5086 &signature_length );
5087 TEST_EQUAL( actual_status, expected_status );
5088 /* The value of *signature_length is unspecified on error, but
5089 * whatever it is, it should be less than signature_size, so that
5090 * if the caller tries to read *signature_length bytes without
5091 * checking the error code then they don't overflow a buffer. */
5092 TEST_ASSERT( signature_length <= signature_size );
5093
5094exit:
5095 psa_reset_key_attributes( &attributes );
5096 psa_destroy_key( key );
5097 mbedtls_free( signature );
5098 PSA_DONE( );
5099}
5100/* END_CASE */
5101
5102/* BEGIN_CASE */
5103void sign_verify_message( int key_type_arg,
5104 data_t *key_data,
5105 int alg_arg,
5106 data_t *input_data )
5107{
5108 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5109 psa_key_type_t key_type = key_type_arg;
5110 psa_algorithm_t alg = alg_arg;
5111 size_t key_bits;
5112 unsigned char *signature = NULL;
5113 size_t signature_size;
5114 size_t signature_length = 0xdeadbeef;
5115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5116
5117 PSA_ASSERT( psa_crypto_init( ) );
5118
5119 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5120 PSA_KEY_USAGE_VERIFY_MESSAGE );
5121 psa_set_key_algorithm( &attributes, alg );
5122 psa_set_key_type( &attributes, key_type );
5123
5124 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5125 &key ) );
5126 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5127 key_bits = psa_get_key_bits( &attributes );
5128
5129 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5130 TEST_ASSERT( signature_size != 0 );
5131 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5132 ASSERT_ALLOC( signature, signature_size );
5133
5134 PSA_ASSERT( psa_sign_message( key, alg,
5135 input_data->x, input_data->len,
5136 signature, signature_size,
5137 &signature_length ) );
5138 TEST_ASSERT( signature_length <= signature_size );
5139 TEST_ASSERT( signature_length > 0 );
5140
5141 PSA_ASSERT( psa_verify_message( key, alg,
5142 input_data->x, input_data->len,
5143 signature, signature_length ) );
5144
5145 if( input_data->len != 0 )
5146 {
5147 /* Flip a bit in the input and verify that the signature is now
5148 * detected as invalid. Flip a bit at the beginning, not at the end,
5149 * because ECDSA may ignore the last few bits of the input. */
5150 input_data->x[0] ^= 1;
5151 TEST_EQUAL( psa_verify_message( key, alg,
5152 input_data->x, input_data->len,
5153 signature, signature_length ),
5154 PSA_ERROR_INVALID_SIGNATURE );
5155 }
5156
5157exit:
5158 psa_reset_key_attributes( &attributes );
5159
5160 psa_destroy_key( key );
5161 mbedtls_free( signature );
5162 PSA_DONE( );
5163}
5164/* END_CASE */
5165
5166/* BEGIN_CASE */
5167void verify_message( int key_type_arg,
5168 data_t *key_data,
5169 int alg_arg,
5170 data_t *input_data,
5171 data_t *signature_data )
5172{
5173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5174 psa_key_type_t key_type = key_type_arg;
5175 psa_algorithm_t alg = alg_arg;
5176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5177
5178 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5179
5180 PSA_ASSERT( psa_crypto_init( ) );
5181
5182 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5183 psa_set_key_algorithm( &attributes, alg );
5184 psa_set_key_type( &attributes, key_type );
5185
5186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5187 &key ) );
5188
5189 PSA_ASSERT( psa_verify_message( key, alg,
5190 input_data->x, input_data->len,
5191 signature_data->x, signature_data->len ) );
5192
5193exit:
5194 psa_reset_key_attributes( &attributes );
5195 psa_destroy_key( key );
5196 PSA_DONE( );
5197}
5198/* END_CASE */
5199
5200/* BEGIN_CASE */
5201void verify_message_fail( int key_type_arg,
5202 data_t *key_data,
5203 int alg_arg,
5204 data_t *hash_data,
5205 data_t *signature_data,
5206 int expected_status_arg )
5207{
5208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5209 psa_key_type_t key_type = key_type_arg;
5210 psa_algorithm_t alg = alg_arg;
5211 psa_status_t actual_status;
5212 psa_status_t expected_status = expected_status_arg;
5213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5214
5215 PSA_ASSERT( psa_crypto_init( ) );
5216
5217 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5218 psa_set_key_algorithm( &attributes, alg );
5219 psa_set_key_type( &attributes, key_type );
5220
5221 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5222 &key ) );
5223
5224 actual_status = psa_verify_message( key, alg,
5225 hash_data->x, hash_data->len,
5226 signature_data->x,
5227 signature_data->len );
5228 TEST_EQUAL( actual_status, expected_status );
5229
5230exit:
5231 psa_reset_key_attributes( &attributes );
5232 psa_destroy_key( key );
5233 PSA_DONE( );
5234}
5235/* END_CASE */
5236
5237/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005238void asymmetric_encrypt( int key_type_arg,
5239 data_t *key_data,
5240 int alg_arg,
5241 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005242 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005243 int expected_output_length_arg,
5244 int expected_status_arg )
5245{
Ronald Cron5425a212020-08-04 14:58:35 +02005246 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005247 psa_key_type_t key_type = key_type_arg;
5248 psa_algorithm_t alg = alg_arg;
5249 size_t expected_output_length = expected_output_length_arg;
5250 size_t key_bits;
5251 unsigned char *output = NULL;
5252 size_t output_size;
5253 size_t output_length = ~0;
5254 psa_status_t actual_status;
5255 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005257
Gilles Peskine8817f612018-12-18 00:18:46 +01005258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005259
Gilles Peskine656896e2018-06-29 19:12:28 +02005260 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005261 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5262 psa_set_key_algorithm( &attributes, alg );
5263 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005265 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005266
5267 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005268 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005269 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005270
Gilles Peskine656896e2018-06-29 19:12:28 +02005271 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005272 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005273 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005274
5275 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005276 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005277 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005278 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005279 output, output_size,
5280 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005281 TEST_EQUAL( actual_status, expected_status );
5282 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005283
Gilles Peskine68428122018-06-30 18:42:41 +02005284 /* If the label is empty, the test framework puts a non-null pointer
5285 * in label->x. Test that a null pointer works as well. */
5286 if( label->len == 0 )
5287 {
5288 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005289 if( output_size != 0 )
5290 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005291 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005292 input_data->x, input_data->len,
5293 NULL, label->len,
5294 output, output_size,
5295 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005296 TEST_EQUAL( actual_status, expected_status );
5297 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005298 }
5299
Gilles Peskine656896e2018-06-29 19:12:28 +02005300exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005301 /*
5302 * Key attributes may have been returned by psa_get_key_attributes()
5303 * thus reset them as required.
5304 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005305 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005306
Ronald Cron5425a212020-08-04 14:58:35 +02005307 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005308 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005309 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005310}
5311/* END_CASE */
5312
5313/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005314void asymmetric_encrypt_decrypt( int key_type_arg,
5315 data_t *key_data,
5316 int alg_arg,
5317 data_t *input_data,
5318 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005319{
Ronald Cron5425a212020-08-04 14:58:35 +02005320 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005321 psa_key_type_t key_type = key_type_arg;
5322 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005323 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005324 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005325 size_t output_size;
5326 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005327 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005328 size_t output2_size;
5329 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005330 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005331
Gilles Peskine8817f612018-12-18 00:18:46 +01005332 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005333
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005334 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5335 psa_set_key_algorithm( &attributes, alg );
5336 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005337
Gilles Peskine049c7532019-05-15 20:22:09 +02005338 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005339 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005340
5341 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005342 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005343 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005344
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005345 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005346 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005347 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005348
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005349 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005350 TEST_ASSERT( output2_size <=
5351 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5352 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005353 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005354
Gilles Peskineeebd7382018-06-08 18:11:54 +02005355 /* We test encryption by checking that encrypt-then-decrypt gives back
5356 * the original plaintext because of the non-optional random
5357 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005358 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005359 input_data->x, input_data->len,
5360 label->x, label->len,
5361 output, output_size,
5362 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005363 /* We don't know what ciphertext length to expect, but check that
5364 * it looks sensible. */
5365 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005366
Ronald Cron5425a212020-08-04 14:58:35 +02005367 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005368 output, output_length,
5369 label->x, label->len,
5370 output2, output2_size,
5371 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005372 ASSERT_COMPARE( input_data->x, input_data->len,
5373 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005374
5375exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005376 /*
5377 * Key attributes may have been returned by psa_get_key_attributes()
5378 * thus reset them as required.
5379 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005380 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005381
Ronald Cron5425a212020-08-04 14:58:35 +02005382 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005383 mbedtls_free( output );
5384 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005385 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005386}
5387/* END_CASE */
5388
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005389/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005390void asymmetric_decrypt( int key_type_arg,
5391 data_t *key_data,
5392 int alg_arg,
5393 data_t *input_data,
5394 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005395 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005396{
Ronald Cron5425a212020-08-04 14:58:35 +02005397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005398 psa_key_type_t key_type = key_type_arg;
5399 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005400 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005401 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005402 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005403 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005404 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005405
Gilles Peskine8817f612018-12-18 00:18:46 +01005406 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005407
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005408 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5409 psa_set_key_algorithm( &attributes, alg );
5410 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005411
Gilles Peskine049c7532019-05-15 20:22:09 +02005412 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005413 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005414
gabor-mezei-armceface22021-01-21 12:26:17 +01005415 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5416 key_bits = psa_get_key_bits( &attributes );
5417
5418 /* Determine the maximum ciphertext length */
5419 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5420 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5421 ASSERT_ALLOC( output, output_size );
5422
Ronald Cron5425a212020-08-04 14:58:35 +02005423 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005424 input_data->x, input_data->len,
5425 label->x, label->len,
5426 output,
5427 output_size,
5428 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005429 ASSERT_COMPARE( expected_data->x, expected_data->len,
5430 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005431
Gilles Peskine68428122018-06-30 18:42:41 +02005432 /* If the label is empty, the test framework puts a non-null pointer
5433 * in label->x. Test that a null pointer works as well. */
5434 if( label->len == 0 )
5435 {
5436 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005437 if( output_size != 0 )
5438 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005439 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005440 input_data->x, input_data->len,
5441 NULL, label->len,
5442 output,
5443 output_size,
5444 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005445 ASSERT_COMPARE( expected_data->x, expected_data->len,
5446 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005447 }
5448
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005449exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005450 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005451 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005452 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005453 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005454}
5455/* END_CASE */
5456
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005457/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005458void asymmetric_decrypt_fail( int key_type_arg,
5459 data_t *key_data,
5460 int alg_arg,
5461 data_t *input_data,
5462 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005463 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005464 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005465{
Ronald Cron5425a212020-08-04 14:58:35 +02005466 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005467 psa_key_type_t key_type = key_type_arg;
5468 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005469 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005470 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005471 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005472 psa_status_t actual_status;
5473 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005474 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005475
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005476 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005477
Gilles Peskine8817f612018-12-18 00:18:46 +01005478 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005479
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5481 psa_set_key_algorithm( &attributes, alg );
5482 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005483
Gilles Peskine049c7532019-05-15 20:22:09 +02005484 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005485 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005486
Ronald Cron5425a212020-08-04 14:58:35 +02005487 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005488 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005489 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005490 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005491 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005492 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005493 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005494
Gilles Peskine68428122018-06-30 18:42:41 +02005495 /* If the label is empty, the test framework puts a non-null pointer
5496 * in label->x. Test that a null pointer works as well. */
5497 if( label->len == 0 )
5498 {
5499 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005500 if( output_size != 0 )
5501 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005502 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005503 input_data->x, input_data->len,
5504 NULL, label->len,
5505 output, output_size,
5506 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005507 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005508 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005509 }
5510
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005511exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005512 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005513 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005514 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005515 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005516}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005517/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005518
5519/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005520void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005521{
5522 /* Test each valid way of initializing the object, except for `= {0}`, as
5523 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5524 * though it's OK by the C standard. We could test for this, but we'd need
5525 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005526 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005527 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5528 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5529 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005530
5531 memset( &zero, 0, sizeof( zero ) );
5532
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005533 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005534 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005535 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005536 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005537 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005538 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005539 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005540
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005541 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005542 PSA_ASSERT( psa_key_derivation_abort(&func) );
5543 PSA_ASSERT( psa_key_derivation_abort(&init) );
5544 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005545}
5546/* END_CASE */
5547
Janos Follath16de4a42019-06-13 16:32:24 +01005548/* BEGIN_CASE */
5549void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005550{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005551 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005552 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005553 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005554
Gilles Peskine8817f612018-12-18 00:18:46 +01005555 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005556
Janos Follath16de4a42019-06-13 16:32:24 +01005557 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005558 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005559
5560exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005561 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005562 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005563}
5564/* END_CASE */
5565
Janos Follathaf3c2a02019-06-12 12:34:34 +01005566/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005567void derive_set_capacity( int alg_arg, int capacity_arg,
5568 int expected_status_arg )
5569{
5570 psa_algorithm_t alg = alg_arg;
5571 size_t capacity = capacity_arg;
5572 psa_status_t expected_status = expected_status_arg;
5573 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5574
5575 PSA_ASSERT( psa_crypto_init( ) );
5576
5577 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5578
5579 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5580 expected_status );
5581
5582exit:
5583 psa_key_derivation_abort( &operation );
5584 PSA_DONE( );
5585}
5586/* END_CASE */
5587
5588/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005589void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005590 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005591 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005592 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005593 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005594 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005595 int expected_status_arg3,
5596 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005597{
5598 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005599 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5600 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005601 psa_status_t expected_statuses[] = {expected_status_arg1,
5602 expected_status_arg2,
5603 expected_status_arg3};
5604 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005605 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5606 MBEDTLS_SVC_KEY_ID_INIT,
5607 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005608 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5610 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005611 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005612 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005613 psa_status_t expected_output_status = expected_output_status_arg;
5614 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005615
5616 PSA_ASSERT( psa_crypto_init( ) );
5617
5618 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5619 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005620
5621 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5622
5623 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5624 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005625 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005626 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005627 psa_set_key_type( &attributes, key_types[i] );
5628 PSA_ASSERT( psa_import_key( &attributes,
5629 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005630 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005631 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5632 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5633 {
5634 // When taking a private key as secret input, use key agreement
5635 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005636 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5637 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005638 expected_statuses[i] );
5639 }
5640 else
5641 {
5642 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005643 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005644 expected_statuses[i] );
5645 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005646 }
5647 else
5648 {
5649 TEST_EQUAL( psa_key_derivation_input_bytes(
5650 &operation, steps[i],
5651 inputs[i]->x, inputs[i]->len ),
5652 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005653 }
5654 }
5655
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005656 if( output_key_type != PSA_KEY_TYPE_NONE )
5657 {
5658 psa_reset_key_attributes( &attributes );
5659 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5660 psa_set_key_bits( &attributes, 8 );
5661 actual_output_status =
5662 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005663 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005664 }
5665 else
5666 {
5667 uint8_t buffer[1];
5668 actual_output_status =
5669 psa_key_derivation_output_bytes( &operation,
5670 buffer, sizeof( buffer ) );
5671 }
5672 TEST_EQUAL( actual_output_status, expected_output_status );
5673
Janos Follathaf3c2a02019-06-12 12:34:34 +01005674exit:
5675 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005676 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5677 psa_destroy_key( keys[i] );
5678 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005679 PSA_DONE( );
5680}
5681/* END_CASE */
5682
Janos Follathd958bb72019-07-03 15:02:16 +01005683/* BEGIN_CASE */
5684void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005685{
Janos Follathd958bb72019-07-03 15:02:16 +01005686 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005687 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005688 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005689 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005690 unsigned char input1[] = "Input 1";
5691 size_t input1_length = sizeof( input1 );
5692 unsigned char input2[] = "Input 2";
5693 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005694 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005695 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005696 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5697 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5698 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005700
Gilles Peskine8817f612018-12-18 00:18:46 +01005701 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005702
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005703 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5704 psa_set_key_algorithm( &attributes, alg );
5705 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005706
Gilles Peskine73676cb2019-05-15 20:15:10 +02005707 PSA_ASSERT( psa_import_key( &attributes,
5708 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005709 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005710
5711 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005712 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5713 input1, input1_length,
5714 input2, input2_length,
5715 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005716 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005717
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005718 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005719 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005720 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005721
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005722 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005723
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005724 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005725 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005726
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005727exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005728 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005729 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005730 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005731}
5732/* END_CASE */
5733
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005734/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005735void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005736{
5737 uint8_t output_buffer[16];
5738 size_t buffer_size = 16;
5739 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005741
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005742 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5743 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005744 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005745
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005746 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005747 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005748
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005749 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005750
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005751 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5752 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005753 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005754
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005756 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005757
5758exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005759 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005760}
5761/* END_CASE */
5762
5763/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005764void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005765 int step1_arg, data_t *input1,
5766 int step2_arg, data_t *input2,
5767 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005768 int requested_capacity_arg,
5769 data_t *expected_output1,
5770 data_t *expected_output2 )
5771{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005772 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005773 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5774 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005775 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5776 MBEDTLS_SVC_KEY_ID_INIT,
5777 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005778 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005779 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005780 uint8_t *expected_outputs[2] =
5781 {expected_output1->x, expected_output2->x};
5782 size_t output_sizes[2] =
5783 {expected_output1->len, expected_output2->len};
5784 size_t output_buffer_size = 0;
5785 uint8_t *output_buffer = NULL;
5786 size_t expected_capacity;
5787 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005788 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005789 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005790 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005791
5792 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5793 {
5794 if( output_sizes[i] > output_buffer_size )
5795 output_buffer_size = output_sizes[i];
5796 if( output_sizes[i] == 0 )
5797 expected_outputs[i] = NULL;
5798 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005799 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005800 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005801
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005802 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5803 psa_set_key_algorithm( &attributes, alg );
5804 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005805
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005806 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005807 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5808 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5809 requested_capacity ) );
5810 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005811 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005812 switch( steps[i] )
5813 {
5814 case 0:
5815 break;
5816 case PSA_KEY_DERIVATION_INPUT_SECRET:
5817 PSA_ASSERT( psa_import_key( &attributes,
5818 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005819 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005820
5821 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5822 {
5823 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5824 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5825 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5826 }
5827
Gilles Peskine1468da72019-05-29 17:35:49 +02005828 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005829 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005830 break;
5831 default:
5832 PSA_ASSERT( psa_key_derivation_input_bytes(
5833 &operation, steps[i],
5834 inputs[i]->x, inputs[i]->len ) );
5835 break;
5836 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005837 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005838
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005839 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005840 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005841 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005842 expected_capacity = requested_capacity;
5843
5844 /* Expansion phase. */
5845 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5846 {
5847 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005848 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005849 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005850 if( expected_capacity == 0 && output_sizes[i] == 0 )
5851 {
5852 /* Reading 0 bytes when 0 bytes are available can go either way. */
5853 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005854 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005855 continue;
5856 }
5857 else if( expected_capacity == 0 ||
5858 output_sizes[i] > expected_capacity )
5859 {
5860 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005861 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005862 expected_capacity = 0;
5863 continue;
5864 }
5865 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005866 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005867 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005868 ASSERT_COMPARE( output_buffer, output_sizes[i],
5869 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005870 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005871 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005872 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005873 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005874 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005875 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005876 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005877
5878exit:
5879 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005880 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005881 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5882 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005883 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005884}
5885/* END_CASE */
5886
5887/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005888void derive_full( int alg_arg,
5889 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005890 data_t *input1,
5891 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005892 int requested_capacity_arg )
5893{
Ronald Cron5425a212020-08-04 14:58:35 +02005894 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005895 psa_algorithm_t alg = alg_arg;
5896 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005897 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005898 unsigned char output_buffer[16];
5899 size_t expected_capacity = requested_capacity;
5900 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005902
Gilles Peskine8817f612018-12-18 00:18:46 +01005903 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005904
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005905 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5906 psa_set_key_algorithm( &attributes, alg );
5907 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005908
Gilles Peskine049c7532019-05-15 20:22:09 +02005909 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005910 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005911
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005912 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5913 input1->x, input1->len,
5914 input2->x, input2->len,
5915 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005916 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005917
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005918 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005919 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005920 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005921
5922 /* Expansion phase. */
5923 while( current_capacity > 0 )
5924 {
5925 size_t read_size = sizeof( output_buffer );
5926 if( read_size > current_capacity )
5927 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005928 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005929 output_buffer,
5930 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005931 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005932 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005933 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005934 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005935 }
5936
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005937 /* Check that the operation refuses to go over capacity. */
5938 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005939 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005940
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005941 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005942
5943exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005944 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005945 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005946 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005947}
5948/* END_CASE */
5949
Janos Follathe60c9052019-07-03 13:51:30 +01005950/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005951void derive_key_exercise( int alg_arg,
5952 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005953 data_t *input1,
5954 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005955 int derived_type_arg,
5956 int derived_bits_arg,
5957 int derived_usage_arg,
5958 int derived_alg_arg )
5959{
Ronald Cron5425a212020-08-04 14:58:35 +02005960 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5961 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005962 psa_algorithm_t alg = alg_arg;
5963 psa_key_type_t derived_type = derived_type_arg;
5964 size_t derived_bits = derived_bits_arg;
5965 psa_key_usage_t derived_usage = derived_usage_arg;
5966 psa_algorithm_t derived_alg = derived_alg_arg;
5967 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005968 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005969 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005970 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005971
Gilles Peskine8817f612018-12-18 00:18:46 +01005972 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005973
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005974 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5975 psa_set_key_algorithm( &attributes, alg );
5976 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005977 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005978 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005979
5980 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005981 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5982 input1->x, input1->len,
5983 input2->x, input2->len,
5984 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005985 goto exit;
5986
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005987 psa_set_key_usage_flags( &attributes, derived_usage );
5988 psa_set_key_algorithm( &attributes, derived_alg );
5989 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005990 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005991 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005992 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005993
5994 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005995 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005996 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5997 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005998
5999 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006000 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006001 goto exit;
6002
6003exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006004 /*
6005 * Key attributes may have been returned by psa_get_key_attributes()
6006 * thus reset them as required.
6007 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006008 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006009
6010 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006011 psa_destroy_key( base_key );
6012 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006013 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006014}
6015/* END_CASE */
6016
Janos Follath42fd8882019-07-03 14:17:09 +01006017/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006018void derive_key_export( int alg_arg,
6019 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006020 data_t *input1,
6021 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006022 int bytes1_arg,
6023 int bytes2_arg )
6024{
Ronald Cron5425a212020-08-04 14:58:35 +02006025 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6026 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006027 psa_algorithm_t alg = alg_arg;
6028 size_t bytes1 = bytes1_arg;
6029 size_t bytes2 = bytes2_arg;
6030 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006031 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006032 uint8_t *output_buffer = NULL;
6033 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006034 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6035 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006036 size_t length;
6037
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006038 ASSERT_ALLOC( output_buffer, capacity );
6039 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006040 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006041
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006042 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6043 psa_set_key_algorithm( &base_attributes, alg );
6044 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006045 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006046 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006047
6048 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006049 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6050 input1->x, input1->len,
6051 input2->x, input2->len,
6052 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006053 goto exit;
6054
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006055 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006056 output_buffer,
6057 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006058 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006059
6060 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006061 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6062 input1->x, input1->len,
6063 input2->x, input2->len,
6064 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006065 goto exit;
6066
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006067 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6068 psa_set_key_algorithm( &derived_attributes, 0 );
6069 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006070 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006071 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006072 &derived_key ) );
6073 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006074 export_buffer, bytes1,
6075 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006076 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006077 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006078 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006079 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006080 &derived_key ) );
6081 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006082 export_buffer + bytes1, bytes2,
6083 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006084 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006085
6086 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006087 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6088 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006089
6090exit:
6091 mbedtls_free( output_buffer );
6092 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006093 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006094 psa_destroy_key( base_key );
6095 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006096 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006097}
6098/* END_CASE */
6099
6100/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006101void derive_key( int alg_arg,
6102 data_t *key_data, data_t *input1, data_t *input2,
6103 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006104 int expected_status_arg,
6105 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006106{
Ronald Cron5425a212020-08-04 14:58:35 +02006107 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6108 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006109 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006110 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006111 size_t bits = bits_arg;
6112 psa_status_t expected_status = expected_status_arg;
6113 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6114 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6115 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6116
6117 PSA_ASSERT( psa_crypto_init( ) );
6118
6119 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6120 psa_set_key_algorithm( &base_attributes, alg );
6121 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6122 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006123 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006124
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006125 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6126 input1->x, input1->len,
6127 input2->x, input2->len,
6128 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006129 goto exit;
6130
6131 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6132 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006133 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006134 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006135
6136 psa_status_t status =
6137 psa_key_derivation_output_key( &derived_attributes,
6138 &operation,
6139 &derived_key );
6140 if( is_large_output > 0 )
6141 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6142 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006143
6144exit:
6145 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006146 psa_destroy_key( base_key );
6147 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006148 PSA_DONE( );
6149}
6150/* END_CASE */
6151
6152/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006153void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006154 int our_key_type_arg, int our_key_alg_arg,
6155 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006156 int expected_status_arg )
6157{
Ronald Cron5425a212020-08-04 14:58:35 +02006158 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006159 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006160 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006161 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006162 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006164 psa_status_t expected_status = expected_status_arg;
6165 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006166
Gilles Peskine8817f612018-12-18 00:18:46 +01006167 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006168
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006169 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006170 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006171 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006172 PSA_ASSERT( psa_import_key( &attributes,
6173 our_key_data->x, our_key_data->len,
6174 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006175
Gilles Peskine77f40d82019-04-11 21:27:06 +02006176 /* The tests currently include inputs that should fail at either step.
6177 * Test cases that fail at the setup step should be changed to call
6178 * key_derivation_setup instead, and this function should be renamed
6179 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006180 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006181 if( status == PSA_SUCCESS )
6182 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006183 TEST_EQUAL( psa_key_derivation_key_agreement(
6184 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6185 our_key,
6186 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006187 expected_status );
6188 }
6189 else
6190 {
6191 TEST_ASSERT( status == expected_status );
6192 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006193
6194exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006195 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006196 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006197 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006198}
6199/* END_CASE */
6200
6201/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006202void raw_key_agreement( int alg_arg,
6203 int our_key_type_arg, data_t *our_key_data,
6204 data_t *peer_key_data,
6205 data_t *expected_output )
6206{
Ronald Cron5425a212020-08-04 14:58:35 +02006207 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006208 psa_algorithm_t alg = alg_arg;
6209 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006210 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006211 unsigned char *output = NULL;
6212 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006213 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006214
6215 ASSERT_ALLOC( output, expected_output->len );
6216 PSA_ASSERT( psa_crypto_init( ) );
6217
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006218 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6219 psa_set_key_algorithm( &attributes, alg );
6220 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006221 PSA_ASSERT( psa_import_key( &attributes,
6222 our_key_data->x, our_key_data->len,
6223 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006224
gabor-mezei-armceface22021-01-21 12:26:17 +01006225 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6226 key_bits = psa_get_key_bits( &attributes );
6227
Gilles Peskinebe697d82019-05-16 18:00:41 +02006228 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6229 peer_key_data->x, peer_key_data->len,
6230 output, expected_output->len,
6231 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006232 ASSERT_COMPARE( output, output_length,
6233 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006234 TEST_ASSERT( output_length <=
6235 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6236 TEST_ASSERT( output_length <=
6237 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006238
6239exit:
6240 mbedtls_free( output );
6241 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006242 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006243}
6244/* END_CASE */
6245
6246/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006247void key_agreement_capacity( int alg_arg,
6248 int our_key_type_arg, data_t *our_key_data,
6249 data_t *peer_key_data,
6250 int expected_capacity_arg )
6251{
Ronald Cron5425a212020-08-04 14:58:35 +02006252 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006253 psa_algorithm_t alg = alg_arg;
6254 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006255 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006257 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006258 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006259
Gilles Peskine8817f612018-12-18 00:18:46 +01006260 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006261
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006262 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6263 psa_set_key_algorithm( &attributes, alg );
6264 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006265 PSA_ASSERT( psa_import_key( &attributes,
6266 our_key_data->x, our_key_data->len,
6267 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006268
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006269 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006270 PSA_ASSERT( psa_key_derivation_key_agreement(
6271 &operation,
6272 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6273 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006274 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6275 {
6276 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006277 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006278 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006279 NULL, 0 ) );
6280 }
Gilles Peskine59685592018-09-18 12:11:34 +02006281
Gilles Peskinebf491972018-10-25 22:36:12 +02006282 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006283 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006284 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006285 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006286
Gilles Peskinebf491972018-10-25 22:36:12 +02006287 /* Test the actual capacity by reading the output. */
6288 while( actual_capacity > sizeof( output ) )
6289 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006290 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006291 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006292 actual_capacity -= sizeof( output );
6293 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006294 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006295 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006296 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006297 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006298
Gilles Peskine59685592018-09-18 12:11:34 +02006299exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006300 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006301 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006302 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006303}
6304/* END_CASE */
6305
6306/* BEGIN_CASE */
6307void key_agreement_output( int alg_arg,
6308 int our_key_type_arg, data_t *our_key_data,
6309 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006310 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006311{
Ronald Cron5425a212020-08-04 14:58:35 +02006312 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006313 psa_algorithm_t alg = alg_arg;
6314 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006315 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006317 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006318
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006319 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6320 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006321
Gilles Peskine8817f612018-12-18 00:18:46 +01006322 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006323
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006324 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6325 psa_set_key_algorithm( &attributes, alg );
6326 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006327 PSA_ASSERT( psa_import_key( &attributes,
6328 our_key_data->x, our_key_data->len,
6329 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006330
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006331 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006332 PSA_ASSERT( psa_key_derivation_key_agreement(
6333 &operation,
6334 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6335 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006336 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6337 {
6338 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006339 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006340 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006341 NULL, 0 ) );
6342 }
Gilles Peskine59685592018-09-18 12:11:34 +02006343
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006344 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006345 actual_output,
6346 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006347 ASSERT_COMPARE( actual_output, expected_output1->len,
6348 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006349 if( expected_output2->len != 0 )
6350 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006351 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006352 actual_output,
6353 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006354 ASSERT_COMPARE( actual_output, expected_output2->len,
6355 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006356 }
Gilles Peskine59685592018-09-18 12:11:34 +02006357
6358exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006359 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006360 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006361 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006362 mbedtls_free( actual_output );
6363}
6364/* END_CASE */
6365
6366/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006367void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006368{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006369 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006370 unsigned char *output = NULL;
6371 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006372 size_t i;
6373 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006374
Simon Butcher49f8e312020-03-03 15:51:50 +00006375 TEST_ASSERT( bytes_arg >= 0 );
6376
Gilles Peskine91892022021-02-08 19:50:26 +01006377 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006378 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006379
Gilles Peskine8817f612018-12-18 00:18:46 +01006380 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006381
Gilles Peskinea50d7392018-06-21 10:22:13 +02006382 /* Run several times, to ensure that every output byte will be
6383 * nonzero at least once with overwhelming probability
6384 * (2^(-8*number_of_runs)). */
6385 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006386 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006387 if( bytes != 0 )
6388 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006389 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006390
Gilles Peskinea50d7392018-06-21 10:22:13 +02006391 for( i = 0; i < bytes; i++ )
6392 {
6393 if( output[i] != 0 )
6394 ++changed[i];
6395 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006396 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006397
6398 /* Check that every byte was changed to nonzero at least once. This
6399 * validates that psa_generate_random is overwriting every byte of
6400 * the output buffer. */
6401 for( i = 0; i < bytes; i++ )
6402 {
6403 TEST_ASSERT( changed[i] != 0 );
6404 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006405
6406exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006407 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006408 mbedtls_free( output );
6409 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006410}
6411/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006412
6413/* BEGIN_CASE */
6414void generate_key( int type_arg,
6415 int bits_arg,
6416 int usage_arg,
6417 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006418 int expected_status_arg,
6419 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006420{
Ronald Cron5425a212020-08-04 14:58:35 +02006421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006422 psa_key_type_t type = type_arg;
6423 psa_key_usage_t usage = usage_arg;
6424 size_t bits = bits_arg;
6425 psa_algorithm_t alg = alg_arg;
6426 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006428 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006429
Gilles Peskine8817f612018-12-18 00:18:46 +01006430 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006431
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006432 psa_set_key_usage_flags( &attributes, usage );
6433 psa_set_key_algorithm( &attributes, alg );
6434 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006435 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006436
6437 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006438 psa_status_t status = psa_generate_key( &attributes, &key );
6439
6440 if( is_large_key > 0 )
6441 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6442 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006443 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006444 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006445
6446 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006447 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006448 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6449 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006450
Gilles Peskine818ca122018-06-20 18:16:48 +02006451 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006452 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006453 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006454
6455exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006456 /*
6457 * Key attributes may have been returned by psa_get_key_attributes()
6458 * thus reset them as required.
6459 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006460 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006461
Ronald Cron5425a212020-08-04 14:58:35 +02006462 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006463 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006464}
6465/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006466
Ronald Cronee414c72021-03-18 18:50:08 +01006467/* 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 +02006468void generate_key_rsa( int bits_arg,
6469 data_t *e_arg,
6470 int expected_status_arg )
6471{
Ronald Cron5425a212020-08-04 14:58:35 +02006472 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006473 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006474 size_t bits = bits_arg;
6475 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6476 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6477 psa_status_t expected_status = expected_status_arg;
6478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6479 uint8_t *exported = NULL;
6480 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006481 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006482 size_t exported_length = SIZE_MAX;
6483 uint8_t *e_read_buffer = NULL;
6484 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006485 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006486 size_t e_read_length = SIZE_MAX;
6487
6488 if( e_arg->len == 0 ||
6489 ( e_arg->len == 3 &&
6490 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6491 {
6492 is_default_public_exponent = 1;
6493 e_read_size = 0;
6494 }
6495 ASSERT_ALLOC( e_read_buffer, e_read_size );
6496 ASSERT_ALLOC( exported, exported_size );
6497
6498 PSA_ASSERT( psa_crypto_init( ) );
6499
6500 psa_set_key_usage_flags( &attributes, usage );
6501 psa_set_key_algorithm( &attributes, alg );
6502 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6503 e_arg->x, e_arg->len ) );
6504 psa_set_key_bits( &attributes, bits );
6505
6506 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006507 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006508 if( expected_status != PSA_SUCCESS )
6509 goto exit;
6510
6511 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006512 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006513 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6514 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6515 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6516 e_read_buffer, e_read_size,
6517 &e_read_length ) );
6518 if( is_default_public_exponent )
6519 TEST_EQUAL( e_read_length, 0 );
6520 else
6521 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6522
6523 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006524 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006525 goto exit;
6526
6527 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006528 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006529 exported, exported_size,
6530 &exported_length ) );
6531 {
6532 uint8_t *p = exported;
6533 uint8_t *end = exported + exported_length;
6534 size_t len;
6535 /* RSAPublicKey ::= SEQUENCE {
6536 * modulus INTEGER, -- n
6537 * publicExponent INTEGER } -- e
6538 */
6539 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006540 MBEDTLS_ASN1_SEQUENCE |
6541 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006542 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006543 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6544 MBEDTLS_ASN1_INTEGER ) );
6545 if( len >= 1 && p[0] == 0 )
6546 {
6547 ++p;
6548 --len;
6549 }
6550 if( e_arg->len == 0 )
6551 {
6552 TEST_EQUAL( len, 3 );
6553 TEST_EQUAL( p[0], 1 );
6554 TEST_EQUAL( p[1], 0 );
6555 TEST_EQUAL( p[2], 1 );
6556 }
6557 else
6558 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6559 }
6560
6561exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006562 /*
6563 * Key attributes may have been returned by psa_get_key_attributes() or
6564 * set by psa_set_key_domain_parameters() thus reset them as required.
6565 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006566 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006567
Ronald Cron5425a212020-08-04 14:58:35 +02006568 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006569 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006570 mbedtls_free( e_read_buffer );
6571 mbedtls_free( exported );
6572}
6573/* END_CASE */
6574
Darryl Greend49a4992018-06-18 17:27:26 +01006575/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006576void persistent_key_load_key_from_storage( data_t *data,
6577 int type_arg, int bits_arg,
6578 int usage_flags_arg, int alg_arg,
6579 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006580{
Ronald Cron71016a92020-08-28 19:01:50 +02006581 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006582 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006583 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6584 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006585 psa_key_type_t type = type_arg;
6586 size_t bits = bits_arg;
6587 psa_key_usage_t usage_flags = usage_flags_arg;
6588 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006589 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006590 unsigned char *first_export = NULL;
6591 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006592 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006593 size_t first_exported_length;
6594 size_t second_exported_length;
6595
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006596 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6597 {
6598 ASSERT_ALLOC( first_export, export_size );
6599 ASSERT_ALLOC( second_export, export_size );
6600 }
Darryl Greend49a4992018-06-18 17:27:26 +01006601
Gilles Peskine8817f612018-12-18 00:18:46 +01006602 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006603
Gilles Peskinec87af662019-05-15 16:12:22 +02006604 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006605 psa_set_key_usage_flags( &attributes, usage_flags );
6606 psa_set_key_algorithm( &attributes, alg );
6607 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006608 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006609
Darryl Green0c6575a2018-11-07 16:05:30 +00006610 switch( generation_method )
6611 {
6612 case IMPORT_KEY:
6613 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006614 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006615 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006616 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006617
Darryl Green0c6575a2018-11-07 16:05:30 +00006618 case GENERATE_KEY:
6619 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006620 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006621 break;
6622
6623 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006624#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006625 {
6626 /* Create base key */
6627 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6628 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6629 psa_set_key_usage_flags( &base_attributes,
6630 PSA_KEY_USAGE_DERIVE );
6631 psa_set_key_algorithm( &base_attributes, derive_alg );
6632 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006633 PSA_ASSERT( psa_import_key( &base_attributes,
6634 data->x, data->len,
6635 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006636 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006637 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006638 PSA_ASSERT( psa_key_derivation_input_key(
6639 &operation,
6640 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006641 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006642 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006643 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006644 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6645 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006646 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006647 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006648 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006649 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006650 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006651#else
6652 TEST_ASSUME( ! "KDF not supported in this configuration" );
6653#endif
6654 break;
6655
6656 default:
6657 TEST_ASSERT( ! "generation_method not implemented in test" );
6658 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006659 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006660 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006661
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006662 /* Export the key if permitted by the key policy. */
6663 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6664 {
Ronald Cron5425a212020-08-04 14:58:35 +02006665 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006666 first_export, export_size,
6667 &first_exported_length ) );
6668 if( generation_method == IMPORT_KEY )
6669 ASSERT_COMPARE( data->x, data->len,
6670 first_export, first_exported_length );
6671 }
Darryl Greend49a4992018-06-18 17:27:26 +01006672
6673 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006674 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006675 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006676 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006677
Darryl Greend49a4992018-06-18 17:27:26 +01006678 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006679 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006680 TEST_ASSERT( mbedtls_svc_key_id_equal(
6681 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006682 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6683 PSA_KEY_LIFETIME_PERSISTENT );
6684 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6685 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6686 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6687 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006688
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006689 /* Export the key again if permitted by the key policy. */
6690 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006691 {
Ronald Cron5425a212020-08-04 14:58:35 +02006692 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006693 second_export, export_size,
6694 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006695 ASSERT_COMPARE( first_export, first_exported_length,
6696 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006697 }
6698
6699 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006700 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006701 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006702
6703exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006704 /*
6705 * Key attributes may have been returned by psa_get_key_attributes()
6706 * thus reset them as required.
6707 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006708 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006709
Darryl Greend49a4992018-06-18 17:27:26 +01006710 mbedtls_free( first_export );
6711 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006712 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006713 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006714 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006715 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006716}
6717/* END_CASE */