blob: d23ef4d5a544adaa6a5540f8a775fb00416e303c [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.
282 *
283 * \param key_type_arg Type of key passed in
284 * \param key_data The encryption / decryption key data
285 * \param alg_arg The type of algorithm used
286 * \param nonce Nonce data
287 * \param additional_data Additional data
288 * \param ad_part_len If not -1, the length of chunks to
289 * feed additional data in to be encrypted /
290 * decrypted. If -1, no chunking.
291 * \param input_data Data to encrypt / decrypt
292 * \param data_part_len If not -1, the length of chunks to feed the
293 * data in to be encrypted / decrypted. If -1,
294 * no chunking
295 * \param do_set_lengths If non-zero, then set lengths prior to
296 * calling encryption / decryption.
297 * \param expected_output Expected output
Paul Elliott41ffae12021-07-22 21:52:01 +0100298 * \param expect_valid_signature If non zero, we expect the signature to be
299 * valid
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100300 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100301 * \param do_zero_parts If non-zero, interleave zero length chunks
302 * with normal length chunks
303 * \param swap_set_functions If non-zero, swap the order of set lengths
304 * and set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100305 *
306 * \return int Zero on failure, non-zero on success.
307 *
308 */
309static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
310 int alg_arg,
311 data_t *nonce,
312 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100313 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100315 int data_part_len_arg,
Paul Elliott33746aa2021-09-15 16:40:40 +0100316 setlengths_method set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100318 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100319 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100320{
321 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
322 psa_key_type_t key_type = key_type_arg;
323 psa_algorithm_t alg = alg_arg;
324 psa_aead_operation_t operation;
325 unsigned char *output_data = NULL;
326 unsigned char *part_data = NULL;
327 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100328 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100329 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100330 size_t output_size = 0;
331 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100332 size_t output_length = 0;
333 size_t key_bits = 0;
334 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100335 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100336 size_t part_length = 0;
337 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100339 size_t ad_part_len = 0;
340 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
343 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100346 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100347
Paul Elliottd3f82412021-06-16 16:52:21 +0100348 PSA_ASSERT( psa_crypto_init( ) );
349
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100350 if( is_encrypt )
351 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
352 else
353 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
354
Paul Elliottd3f82412021-06-16 16:52:21 +0100355 psa_set_key_algorithm( &attributes, alg );
356 psa_set_key_type( &attributes, key_type );
357
358 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
359 &key ) );
360
361 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
362 key_bits = psa_get_key_bits( &attributes );
363
364 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
365
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100366 if( is_encrypt )
367 {
368 /* Tag gets written at end of buffer. */
369 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
370 ( input_data->len +
371 tag_length ) );
372 data_true_size = input_data->len;
373 }
374 else
375 {
376 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
377 ( input_data->len -
378 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100379
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100380 /* Do not want to attempt to decrypt tag. */
381 data_true_size = input_data->len - tag_length;
382 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100383
384 ASSERT_ALLOC( output_data, output_size );
385
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100386 if( is_encrypt )
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
391 else
392 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100393 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
394 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100395 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100396
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100397 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100398
399 operation = psa_aead_operation_init( );
400
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100401
402 if( is_encrypt )
403 status = psa_aead_encrypt_setup( &operation, key, alg );
404 else
405 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
407 /* If the operation is not supported, just skip and not fail in case the
408 * encryption involves a common limitation of cryptography hardwares and
409 * an alternative implementation. */
410 if( status == PSA_ERROR_NOT_SUPPORTED )
411 {
412 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
413 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
414 }
415
416 PSA_ASSERT( status );
417
Paul Elliott33746aa2021-09-15 16:40:40 +0100418 if( set_lengths_method == DO_NOT_SET_LENGTHS )
419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100421 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100422 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
423 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100424 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
425 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100426 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100427 {
428 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
429
Paul Elliott33746aa2021-09-15 16:40:40 +0100430 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
431 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100432 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100434 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100435 {
436 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100437 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100438
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100439 for( part_offset = 0, part_count = 0;
440 part_offset < additional_data->len;
441 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100443 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100444 {
Paul Elliott329d5382021-07-22 17:10:45 +0100445 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100447 else if( additional_data->len - part_offset < ad_part_len )
448 {
449 part_length = additional_data->len - part_offset;
450 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 else
452 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100453 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100454 }
455
456 PSA_ASSERT( psa_aead_update_ad( &operation,
457 additional_data->x + part_offset,
458 part_length ) );
459
Paul Elliottd3f82412021-06-16 16:52:21 +0100460 }
461 }
462 else
463 {
464 /* Pass additional data in one go. */
465 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
466 additional_data->len ) );
467 }
468
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100469 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100470 {
471 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100472 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100473 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100474 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100475
476 ASSERT_ALLOC( part_data, part_data_size );
477
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100478 for( part_offset = 0, part_count = 0;
479 part_offset < data_true_size;
480 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100482 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 {
Paul Elliott329d5382021-07-22 17:10:45 +0100484 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100486 else if( ( data_true_size - part_offset ) < data_part_len )
487 {
488 part_length = ( data_true_size - part_offset );
489 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 else
491 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100492 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100493 }
494
495 PSA_ASSERT( psa_aead_update( &operation,
496 ( input_data->x + part_offset ),
497 part_length, part_data,
498 part_data_size,
499 &output_part_length ) );
500
501 if( output_data && output_part_length )
502 {
503 memcpy( ( output_data + part_offset ), part_data,
504 output_part_length );
505 }
506
Paul Elliottd3f82412021-06-16 16:52:21 +0100507 output_length += output_part_length;
508 }
509 }
510 else
511 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100512 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100514 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100515 output_size, &output_length ) );
516 }
517
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100518 if( is_encrypt )
519 PSA_ASSERT( psa_aead_finish( &operation, final_data,
520 final_output_size,
521 &output_part_length,
522 tag_buffer, tag_length,
523 &tag_size ) );
524 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100525 {
Paul Elliott9961a662021-09-17 19:19:02 +0100526 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100527 final_output_size,
528 &output_part_length,
529 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100530 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100531 }
532
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100533 if( output_data && output_part_length )
534 memcpy( ( output_data + output_length ), final_data,
535 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100536
537 output_length += output_part_length;
538
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100539
540 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
541 * should be exact.*/
542 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100543 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100544 TEST_EQUAL( tag_length, tag_size );
545
546 if( output_data && tag_length )
547 memcpy( ( output_data + output_length ), tag_buffer,
548 tag_length );
549
550 output_length += tag_length;
551
552 TEST_EQUAL( output_length,
553 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
554 input_data->len ) );
555 TEST_ASSERT( output_length <=
556 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
557 }
558 else
559 {
560 TEST_EQUAL( output_length,
561 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
562 input_data->len ) );
563 TEST_ASSERT( output_length <=
564 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100565 }
566
Paul Elliottd3f82412021-06-16 16:52:21 +0100567
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100568 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100569 output_data, output_length );
570
Paul Elliottd3f82412021-06-16 16:52:21 +0100571
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100572 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100573
574exit:
575 psa_destroy_key( key );
576 psa_aead_abort( &operation );
577 mbedtls_free( output_data );
578 mbedtls_free( part_data );
579 mbedtls_free( final_data );
580 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581
582 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100583}
584
Gilles Peskinee59236f2018-01-27 23:32:46 +0100585/* END_HEADER */
586
587/* BEGIN_DEPENDENCIES
588 * depends_on:MBEDTLS_PSA_CRYPTO_C
589 * END_DEPENDENCIES
590 */
591
592/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200593void static_checks( )
594{
595 size_t max_truncated_mac_size =
596 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
597
598 /* Check that the length for a truncated MAC always fits in the algorithm
599 * encoding. The shifted mask is the maximum truncated value. The
600 * untruncated algorithm may be one byte larger. */
601 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
602}
603/* END_CASE */
604
605/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200606void import_with_policy( int type_arg,
607 int usage_arg, int alg_arg,
608 int expected_status_arg )
609{
610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
611 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200613 psa_key_type_t type = type_arg;
614 psa_key_usage_t usage = usage_arg;
615 psa_algorithm_t alg = alg_arg;
616 psa_status_t expected_status = expected_status_arg;
617 const uint8_t key_material[16] = {0};
618 psa_status_t status;
619
620 PSA_ASSERT( psa_crypto_init( ) );
621
622 psa_set_key_type( &attributes, type );
623 psa_set_key_usage_flags( &attributes, usage );
624 psa_set_key_algorithm( &attributes, alg );
625
626 status = psa_import_key( &attributes,
627 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200628 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200629 TEST_EQUAL( status, expected_status );
630 if( status != PSA_SUCCESS )
631 goto exit;
632
Ronald Cron5425a212020-08-04 14:58:35 +0200633 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200634 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
635 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
636 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200637 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200638
Ronald Cron5425a212020-08-04 14:58:35 +0200639 PSA_ASSERT( psa_destroy_key( key ) );
640 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641
642exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100643 /*
644 * Key attributes may have been returned by psa_get_key_attributes()
645 * thus reset them as required.
646 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200647 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100648
649 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200650 PSA_DONE( );
651}
652/* END_CASE */
653
654/* BEGIN_CASE */
655void import_with_data( data_t *data, int type_arg,
656 int attr_bits_arg,
657 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200658{
659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
660 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200662 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200663 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200664 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666
Gilles Peskine8817f612018-12-18 00:18:46 +0100667 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668
Gilles Peskine4747d192019-04-17 15:05:45 +0200669 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200670 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200671
Ronald Cron5425a212020-08-04 14:58:35 +0200672 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100673 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 if( status != PSA_SUCCESS )
675 goto exit;
676
Ronald Cron5425a212020-08-04 14:58:35 +0200677 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200678 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200679 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200680 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200681 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200682
Ronald Cron5425a212020-08-04 14:58:35 +0200683 PSA_ASSERT( psa_destroy_key( key ) );
684 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100685
686exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100687 /*
688 * Key attributes may have been returned by psa_get_key_attributes()
689 * thus reset them as required.
690 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200691 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100692
693 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200694 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100695}
696/* END_CASE */
697
698/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200699void import_large_key( int type_arg, int byte_size_arg,
700 int expected_status_arg )
701{
702 psa_key_type_t type = type_arg;
703 size_t byte_size = byte_size_arg;
704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
705 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200706 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200707 psa_status_t status;
708 uint8_t *buffer = NULL;
709 size_t buffer_size = byte_size + 1;
710 size_t n;
711
Steven Cooreman69967ce2021-01-18 18:01:08 +0100712 /* Skip the test case if the target running the test cannot
713 * accomodate large keys due to heap size constraints */
714 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200715 memset( buffer, 'K', byte_size );
716
717 PSA_ASSERT( psa_crypto_init( ) );
718
719 /* Try importing the key */
720 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
721 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200722 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100723 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200724 TEST_EQUAL( status, expected_status );
725
726 if( status == PSA_SUCCESS )
727 {
Ronald Cron5425a212020-08-04 14:58:35 +0200728 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200729 TEST_EQUAL( psa_get_key_type( &attributes ), type );
730 TEST_EQUAL( psa_get_key_bits( &attributes ),
731 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200732 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200733 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200734 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200735 for( n = 0; n < byte_size; n++ )
736 TEST_EQUAL( buffer[n], 'K' );
737 for( n = byte_size; n < buffer_size; n++ )
738 TEST_EQUAL( buffer[n], 0 );
739 }
740
741exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100742 /*
743 * Key attributes may have been returned by psa_get_key_attributes()
744 * thus reset them as required.
745 */
746 psa_reset_key_attributes( &attributes );
747
Ronald Cron5425a212020-08-04 14:58:35 +0200748 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200749 PSA_DONE( );
750 mbedtls_free( buffer );
751}
752/* END_CASE */
753
754/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200755void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
756{
Ronald Cron5425a212020-08-04 14:58:35 +0200757 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200758 size_t bits = bits_arg;
759 psa_status_t expected_status = expected_status_arg;
760 psa_status_t status;
761 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200762 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763 size_t buffer_size = /* Slight overapproximations */
764 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200765 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200766 unsigned char *p;
767 int ret;
768 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200769 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200770
Gilles Peskine8817f612018-12-18 00:18:46 +0100771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200772 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200773
774 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
775 bits, keypair ) ) >= 0 );
776 length = ret;
777
778 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200779 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200780 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100781 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200782
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200783 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200784 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200785
786exit:
787 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200788 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200789}
790/* END_CASE */
791
792/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300793void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300794 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200795 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100796 int expected_bits,
797 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200798 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100799 int canonical_input )
800{
Ronald Cron5425a212020-08-04 14:58:35 +0200801 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100802 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200803 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200804 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100806 unsigned char *exported = NULL;
807 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100809 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100810 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200812 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100813
Moran Pekercb088e72018-07-17 17:36:59 +0300814 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200815 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200817 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819
Gilles Peskine4747d192019-04-17 15:05:45 +0200820 psa_set_key_usage_flags( &attributes, usage_arg );
821 psa_set_key_algorithm( &attributes, alg );
822 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700823
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200825 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826
827 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200828 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200829 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
830 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200831 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100832
833 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200834 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100835 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100836
837 /* The exported length must be set by psa_export_key() to a value between 0
838 * and export_size. On errors, the exported length must be 0. */
839 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
840 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
841 TEST_ASSERT( exported_length <= export_size );
842
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200843 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200844 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200846 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100847 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100848 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200849 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850
Gilles Peskineea38a922021-02-13 00:05:16 +0100851 /* Run sanity checks on the exported key. For non-canonical inputs,
852 * this validates the canonical representations. For canonical inputs,
853 * this doesn't directly validate the implementation, but it still helps
854 * by cross-validating the test data with the sanity check code. */
855 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200856 goto exit;
857
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200859 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100860 else
861 {
Ronald Cron5425a212020-08-04 14:58:35 +0200862 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200863 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200864 &key2 ) );
865 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100866 reexported,
867 export_size,
868 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200869 ASSERT_COMPARE( exported, exported_length,
870 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200871 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100873 TEST_ASSERT( exported_length <=
874 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
875 psa_get_key_bits( &got_attributes ) ) );
876 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877
878destroy:
879 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200880 PSA_ASSERT( psa_destroy_key( key ) );
881 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100882
883exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100884 /*
885 * Key attributes may have been returned by psa_get_key_attributes()
886 * thus reset them as required.
887 */
888 psa_reset_key_attributes( &got_attributes );
889
itayzafrir3e02b3b2018-06-12 17:06:52 +0300890 mbedtls_free( exported );
891 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200892 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893}
894/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100895
Moran Pekerf709f4a2018-06-06 17:26:04 +0300896/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300897void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200898 int type_arg,
899 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100900 int export_size_delta,
901 int expected_export_status_arg,
902 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300903{
Ronald Cron5425a212020-08-04 14:58:35 +0200904 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300905 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200906 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200907 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300908 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300909 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100910 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100911 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300913
Gilles Peskine8817f612018-12-18 00:18:46 +0100914 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300915
Gilles Peskine4747d192019-04-17 15:05:45 +0200916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
917 psa_set_key_algorithm( &attributes, alg );
918 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300919
920 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200921 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300922
Gilles Peskine49c25912018-10-29 15:15:31 +0100923 /* Export the public key */
924 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200925 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200926 exported, export_size,
927 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100928 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100929 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100930 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200931 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100932 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200933 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200934 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100935 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100936 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100937 TEST_ASSERT( expected_public_key->len <=
938 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
939 TEST_ASSERT( expected_public_key->len <=
940 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100941 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
942 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100943 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300944
945exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100946 /*
947 * Key attributes may have been returned by psa_get_key_attributes()
948 * thus reset them as required.
949 */
950 psa_reset_key_attributes( &attributes );
951
itayzafrir3e02b3b2018-06-12 17:06:52 +0300952 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200953 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200954 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300955}
956/* END_CASE */
957
Gilles Peskine20035e32018-02-03 22:44:14 +0100958/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200959void import_and_exercise_key( data_t *data,
960 int type_arg,
961 int bits_arg,
962 int alg_arg )
963{
Ronald Cron5425a212020-08-04 14:58:35 +0200964 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200965 psa_key_type_t type = type_arg;
966 size_t bits = bits_arg;
967 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100968 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200969 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200970 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200971
Gilles Peskine8817f612018-12-18 00:18:46 +0100972 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200973
Gilles Peskine4747d192019-04-17 15:05:45 +0200974 psa_set_key_usage_flags( &attributes, usage );
975 psa_set_key_algorithm( &attributes, alg );
976 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200977
978 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200979 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980
981 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200982 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200983 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
984 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200985
986 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100987 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200988 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
Ronald Cron5425a212020-08-04 14:58:35 +0200990 PSA_ASSERT( psa_destroy_key( key ) );
991 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200992
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100994 /*
995 * Key attributes may have been returned by psa_get_key_attributes()
996 * thus reset them as required.
997 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200998 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100999
1000 psa_reset_key_attributes( &attributes );
1001 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001002 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001003}
1004/* END_CASE */
1005
1006/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001007void effective_key_attributes( int type_arg, int expected_type_arg,
1008 int bits_arg, int expected_bits_arg,
1009 int usage_arg, int expected_usage_arg,
1010 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001011{
Ronald Cron5425a212020-08-04 14:58:35 +02001012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001013 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001014 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001015 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001016 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001017 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001018 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001019 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001020 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001022
Gilles Peskine8817f612018-12-18 00:18:46 +01001023 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001024
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001025 psa_set_key_usage_flags( &attributes, usage );
1026 psa_set_key_algorithm( &attributes, alg );
1027 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001028 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001029
Ronald Cron5425a212020-08-04 14:58:35 +02001030 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001031 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001032
Ronald Cron5425a212020-08-04 14:58:35 +02001033 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001034 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1035 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1036 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1037 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
1039exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001040 /*
1041 * Key attributes may have been returned by psa_get_key_attributes()
1042 * thus reset them as required.
1043 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001044 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001045
1046 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001047 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048}
1049/* END_CASE */
1050
1051/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001052void check_key_policy( int type_arg, int bits_arg,
1053 int usage_arg, int alg_arg )
1054{
1055 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1056 usage_arg, usage_arg, alg_arg, alg_arg );
1057 goto exit;
1058}
1059/* END_CASE */
1060
1061/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001062void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001063{
1064 /* Test each valid way of initializing the object, except for `= {0}`, as
1065 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1066 * though it's OK by the C standard. We could test for this, but we'd need
1067 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001068 psa_key_attributes_t func = psa_key_attributes_init( );
1069 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1070 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001071
1072 memset( &zero, 0, sizeof( zero ) );
1073
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001074 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1075 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1076 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001077
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001078 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1079 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1080 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1081
1082 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1083 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1084 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1085
1086 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1087 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1088 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1089
1090 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1091 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1092 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001093}
1094/* END_CASE */
1095
1096/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001097void mac_key_policy( int policy_usage,
1098 int policy_alg,
1099 int key_type,
1100 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001101 int exercise_alg,
1102 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001103{
Ronald Cron5425a212020-08-04 14:58:35 +02001104 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001106 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001107 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001108 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001109 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001110
Gilles Peskine8817f612018-12-18 00:18:46 +01001111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001112
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001113 psa_set_key_usage_flags( &attributes, policy_usage );
1114 psa_set_key_algorithm( &attributes, policy_alg );
1115 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001116
Gilles Peskine049c7532019-05-15 20:22:09 +02001117 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001118 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001119
Ronald Cron5425a212020-08-04 14:58:35 +02001120 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001121 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001122 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001123 else
1124 TEST_EQUAL( status, expected_status );
1125
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001126 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001127
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001128 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001129 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001130 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001131 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001132 else
1133 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001134
1135exit:
1136 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001137 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001138 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139}
1140/* END_CASE */
1141
1142/* BEGIN_CASE */
1143void cipher_key_policy( int policy_usage,
1144 int policy_alg,
1145 int key_type,
1146 data_t *key_data,
1147 int exercise_alg )
1148{
Ronald Cron5425a212020-08-04 14:58:35 +02001149 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001151 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001152 psa_status_t status;
1153
Gilles Peskine8817f612018-12-18 00:18:46 +01001154 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001155
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001156 psa_set_key_usage_flags( &attributes, policy_usage );
1157 psa_set_key_algorithm( &attributes, policy_alg );
1158 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159
Gilles Peskine049c7532019-05-15 20:22:09 +02001160 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001161 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001162
Ronald Cron5425a212020-08-04 14:58:35 +02001163 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164 if( policy_alg == exercise_alg &&
1165 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001166 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001168 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169 psa_cipher_abort( &operation );
1170
Ronald Cron5425a212020-08-04 14:58:35 +02001171 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172 if( policy_alg == exercise_alg &&
1173 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001174 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001175 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001176 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177
1178exit:
1179 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001180 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001181 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182}
1183/* END_CASE */
1184
1185/* BEGIN_CASE */
1186void aead_key_policy( int policy_usage,
1187 int policy_alg,
1188 int key_type,
1189 data_t *key_data,
1190 int nonce_length_arg,
1191 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001192 int exercise_alg,
1193 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194{
Ronald Cron5425a212020-08-04 14:58:35 +02001195 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001197 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001198 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001199 unsigned char nonce[16] = {0};
1200 size_t nonce_length = nonce_length_arg;
1201 unsigned char tag[16];
1202 size_t tag_length = tag_length_arg;
1203 size_t output_length;
1204
1205 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1206 TEST_ASSERT( tag_length <= sizeof( tag ) );
1207
Gilles Peskine8817f612018-12-18 00:18:46 +01001208 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001210 psa_set_key_usage_flags( &attributes, policy_usage );
1211 psa_set_key_algorithm( &attributes, policy_alg );
1212 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213
Gilles Peskine049c7532019-05-15 20:22:09 +02001214 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001215 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001216
Ronald Cron5425a212020-08-04 14:58:35 +02001217 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218 nonce, nonce_length,
1219 NULL, 0,
1220 NULL, 0,
1221 tag, tag_length,
1222 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001223 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1224 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001226 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001227
1228 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001229 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 nonce, nonce_length,
1231 NULL, 0,
1232 tag, tag_length,
1233 NULL, 0,
1234 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001235 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1236 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1237 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001238 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001239 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001240 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001241
1242exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001243 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001244 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001245}
1246/* END_CASE */
1247
1248/* BEGIN_CASE */
1249void asymmetric_encryption_key_policy( int policy_usage,
1250 int policy_alg,
1251 int key_type,
1252 data_t *key_data,
1253 int exercise_alg )
1254{
Ronald Cron5425a212020-08-04 14:58:35 +02001255 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001257 psa_status_t status;
1258 size_t key_bits;
1259 size_t buffer_length;
1260 unsigned char *buffer = NULL;
1261 size_t output_length;
1262
Gilles Peskine8817f612018-12-18 00:18:46 +01001263 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001264
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001265 psa_set_key_usage_flags( &attributes, policy_usage );
1266 psa_set_key_algorithm( &attributes, policy_alg );
1267 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001268
Gilles Peskine049c7532019-05-15 20:22:09 +02001269 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001270 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001271
Ronald Cron5425a212020-08-04 14:58:35 +02001272 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001273 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001274 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1275 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001276 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277
Ronald Cron5425a212020-08-04 14:58:35 +02001278 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001279 NULL, 0,
1280 NULL, 0,
1281 buffer, buffer_length,
1282 &output_length );
1283 if( policy_alg == exercise_alg &&
1284 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001285 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001286 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001287 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001288
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001289 if( buffer_length != 0 )
1290 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001291 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001292 buffer, buffer_length,
1293 NULL, 0,
1294 buffer, buffer_length,
1295 &output_length );
1296 if( policy_alg == exercise_alg &&
1297 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001298 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001299 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001300 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001301
1302exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001303 /*
1304 * Key attributes may have been returned by psa_get_key_attributes()
1305 * thus reset them as required.
1306 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001307 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001308
1309 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001310 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001311 mbedtls_free( buffer );
1312}
1313/* END_CASE */
1314
1315/* BEGIN_CASE */
1316void asymmetric_signature_key_policy( int policy_usage,
1317 int policy_alg,
1318 int key_type,
1319 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001320 int exercise_alg,
1321 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001322{
Ronald Cron5425a212020-08-04 14:58:35 +02001323 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001325 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001326 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1327 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1328 * compatible with the policy and `payload_length_arg` is supposed to be
1329 * a valid input length to sign. If `payload_length_arg <= 0`,
1330 * `exercise_alg` is supposed to be forbidden by the policy. */
1331 int compatible_alg = payload_length_arg > 0;
1332 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001333 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001334 size_t signature_length;
1335
Gilles Peskine8817f612018-12-18 00:18:46 +01001336 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001337
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001338 psa_set_key_usage_flags( &attributes, policy_usage );
1339 psa_set_key_algorithm( &attributes, policy_alg );
1340 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001341
Gilles Peskine049c7532019-05-15 20:22:09 +02001342 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001343 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001344
Ronald Cron5425a212020-08-04 14:58:35 +02001345 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001346 payload, payload_length,
1347 signature, sizeof( signature ),
1348 &signature_length );
1349 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001350 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001351 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001352 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001353
1354 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001355 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001356 payload, payload_length,
1357 signature, sizeof( signature ) );
1358 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001359 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001360 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001361 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001362
1363exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001364 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001365 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001366}
1367/* END_CASE */
1368
Janos Follathba3fab92019-06-11 14:50:16 +01001369/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001370void derive_key_policy( int policy_usage,
1371 int policy_alg,
1372 int key_type,
1373 data_t *key_data,
1374 int exercise_alg )
1375{
Ronald Cron5425a212020-08-04 14:58:35 +02001376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001378 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001379 psa_status_t status;
1380
Gilles Peskine8817f612018-12-18 00:18:46 +01001381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001382
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001383 psa_set_key_usage_flags( &attributes, policy_usage );
1384 psa_set_key_algorithm( &attributes, policy_alg );
1385 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001386
Gilles Peskine049c7532019-05-15 20:22:09 +02001387 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001388 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001389
Janos Follathba3fab92019-06-11 14:50:16 +01001390 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1391
1392 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1393 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001394 {
Janos Follathba3fab92019-06-11 14:50:16 +01001395 PSA_ASSERT( psa_key_derivation_input_bytes(
1396 &operation,
1397 PSA_KEY_DERIVATION_INPUT_SEED,
1398 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001399 }
Janos Follathba3fab92019-06-11 14:50:16 +01001400
1401 status = psa_key_derivation_input_key( &operation,
1402 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001403 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001404
Gilles Peskineea0fb492018-07-12 17:17:20 +02001405 if( policy_alg == exercise_alg &&
1406 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001407 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001408 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001409 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001410
1411exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001412 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001413 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001414 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001415}
1416/* END_CASE */
1417
1418/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001419void agreement_key_policy( int policy_usage,
1420 int policy_alg,
1421 int key_type_arg,
1422 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001423 int exercise_alg,
1424 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001425{
Ronald Cron5425a212020-08-04 14:58:35 +02001426 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001428 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001429 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001430 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001431 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001432
Gilles Peskine8817f612018-12-18 00:18:46 +01001433 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001434
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001435 psa_set_key_usage_flags( &attributes, policy_usage );
1436 psa_set_key_algorithm( &attributes, policy_alg );
1437 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001438
Gilles Peskine049c7532019-05-15 20:22:09 +02001439 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001440 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001442 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001443 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001444
Steven Cooremance48e852020-10-05 16:02:45 +02001445 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001446
1447exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001448 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001449 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001450 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001451}
1452/* END_CASE */
1453
1454/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001455void key_policy_alg2( int key_type_arg, data_t *key_data,
1456 int usage_arg, int alg_arg, int alg2_arg )
1457{
Ronald Cron5425a212020-08-04 14:58:35 +02001458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001459 psa_key_type_t key_type = key_type_arg;
1460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1461 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1462 psa_key_usage_t usage = usage_arg;
1463 psa_algorithm_t alg = alg_arg;
1464 psa_algorithm_t alg2 = alg2_arg;
1465
1466 PSA_ASSERT( psa_crypto_init( ) );
1467
1468 psa_set_key_usage_flags( &attributes, usage );
1469 psa_set_key_algorithm( &attributes, alg );
1470 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1471 psa_set_key_type( &attributes, key_type );
1472 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001473 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001474
Ronald Cron5425a212020-08-04 14:58:35 +02001475 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001476 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1477 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1478 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1479
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001480 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001481 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001482 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001483 goto exit;
1484
1485exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001486 /*
1487 * Key attributes may have been returned by psa_get_key_attributes()
1488 * thus reset them as required.
1489 */
1490 psa_reset_key_attributes( &got_attributes );
1491
Ronald Cron5425a212020-08-04 14:58:35 +02001492 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001493 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001494}
1495/* END_CASE */
1496
1497/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001498void raw_agreement_key_policy( int policy_usage,
1499 int policy_alg,
1500 int key_type_arg,
1501 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001502 int exercise_alg,
1503 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001504{
Ronald Cron5425a212020-08-04 14:58:35 +02001505 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001507 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001508 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001509 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001510 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001511
1512 PSA_ASSERT( psa_crypto_init( ) );
1513
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001514 psa_set_key_usage_flags( &attributes, policy_usage );
1515 psa_set_key_algorithm( &attributes, policy_alg );
1516 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517
Gilles Peskine049c7532019-05-15 20:22:09 +02001518 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001519 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001520
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001521 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001522
Steven Cooremance48e852020-10-05 16:02:45 +02001523 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001524
1525exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001526 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001527 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001528 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001529}
1530/* END_CASE */
1531
1532/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001533void copy_success( int source_usage_arg,
1534 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001535 int type_arg, data_t *material,
1536 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001537 int target_usage_arg,
1538 int target_alg_arg, int target_alg2_arg,
1539 int expected_usage_arg,
1540 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001541{
Gilles Peskineca25db92019-04-19 11:43:08 +02001542 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1543 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001544 psa_key_usage_t expected_usage = expected_usage_arg;
1545 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001546 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001547 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1548 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001549 uint8_t *export_buffer = NULL;
1550
Gilles Peskine57ab7212019-01-28 13:03:09 +01001551 PSA_ASSERT( psa_crypto_init( ) );
1552
Gilles Peskineca25db92019-04-19 11:43:08 +02001553 /* Prepare the source key. */
1554 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1555 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001556 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001557 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001558 PSA_ASSERT( psa_import_key( &source_attributes,
1559 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001560 &source_key ) );
1561 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001562
Gilles Peskineca25db92019-04-19 11:43:08 +02001563 /* Prepare the target attributes. */
1564 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001565 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001567 /* Set volatile lifetime to reset the key identifier to 0. */
1568 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1569 }
1570
Gilles Peskineca25db92019-04-19 11:43:08 +02001571 if( target_usage_arg != -1 )
1572 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1573 if( target_alg_arg != -1 )
1574 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001575 if( target_alg2_arg != -1 )
1576 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001577
1578 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001579 PSA_ASSERT( psa_copy_key( source_key,
1580 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001581
1582 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001583 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001584
1585 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001586 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001587 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1588 psa_get_key_type( &target_attributes ) );
1589 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1590 psa_get_key_bits( &target_attributes ) );
1591 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1592 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001593 TEST_EQUAL( expected_alg2,
1594 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001595 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1596 {
1597 size_t length;
1598 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001599 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001600 material->len, &length ) );
1601 ASSERT_COMPARE( material->x, material->len,
1602 export_buffer, length );
1603 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001604
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001605 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001606 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001607 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001608 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001609
Ronald Cron5425a212020-08-04 14:58:35 +02001610 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001611
1612exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001613 /*
1614 * Source and target key attributes may have been returned by
1615 * psa_get_key_attributes() thus reset them as required.
1616 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001617 psa_reset_key_attributes( &source_attributes );
1618 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001619
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001620 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001621 mbedtls_free( export_buffer );
1622}
1623/* END_CASE */
1624
1625/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001626void copy_fail( int source_usage_arg,
1627 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001628 int type_arg, data_t *material,
1629 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001630 int target_usage_arg,
1631 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001632 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001633 int expected_status_arg )
1634{
1635 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1636 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001637 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1638 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001639 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001640
1641 PSA_ASSERT( psa_crypto_init( ) );
1642
1643 /* Prepare the source key. */
1644 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1645 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001646 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001647 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001648 PSA_ASSERT( psa_import_key( &source_attributes,
1649 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001650 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001651
1652 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001653 psa_set_key_id( &target_attributes, key_id );
1654 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001655 psa_set_key_type( &target_attributes, target_type_arg );
1656 psa_set_key_bits( &target_attributes, target_bits_arg );
1657 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1658 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001659 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001660
1661 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001662 TEST_EQUAL( psa_copy_key( source_key,
1663 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001664 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001665
Ronald Cron5425a212020-08-04 14:58:35 +02001666 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001667
Gilles Peskine4a644642019-05-03 17:14:08 +02001668exit:
1669 psa_reset_key_attributes( &source_attributes );
1670 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001671 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001672}
1673/* END_CASE */
1674
1675/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001676void hash_operation_init( )
1677{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001678 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001679 /* Test each valid way of initializing the object, except for `= {0}`, as
1680 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1681 * though it's OK by the C standard. We could test for this, but we'd need
1682 * to supress the Clang warning for the test. */
1683 psa_hash_operation_t func = psa_hash_operation_init( );
1684 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1685 psa_hash_operation_t zero;
1686
1687 memset( &zero, 0, sizeof( zero ) );
1688
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001689 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001690 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1691 PSA_ERROR_BAD_STATE );
1692 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1693 PSA_ERROR_BAD_STATE );
1694 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1695 PSA_ERROR_BAD_STATE );
1696
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001697 /* A default hash operation should be abortable without error. */
1698 PSA_ASSERT( psa_hash_abort( &func ) );
1699 PSA_ASSERT( psa_hash_abort( &init ) );
1700 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001701}
1702/* END_CASE */
1703
1704/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001705void hash_setup( int alg_arg,
1706 int expected_status_arg )
1707{
1708 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001709 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001710 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001711 psa_status_t status;
1712
Gilles Peskine8817f612018-12-18 00:18:46 +01001713 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001714
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001715 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001716 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001717
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001718 /* Whether setup succeeded or failed, abort must succeed. */
1719 PSA_ASSERT( psa_hash_abort( &operation ) );
1720
1721 /* If setup failed, reproduce the failure, so as to
1722 * test the resulting state of the operation object. */
1723 if( status != PSA_SUCCESS )
1724 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1725
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001726 /* Now the operation object should be reusable. */
1727#if defined(KNOWN_SUPPORTED_HASH_ALG)
1728 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1729 PSA_ASSERT( psa_hash_abort( &operation ) );
1730#endif
1731
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001732exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001733 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001734}
1735/* END_CASE */
1736
1737/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001738void hash_compute_fail( int alg_arg, data_t *input,
1739 int output_size_arg, int expected_status_arg )
1740{
1741 psa_algorithm_t alg = alg_arg;
1742 uint8_t *output = NULL;
1743 size_t output_size = output_size_arg;
1744 size_t output_length = INVALID_EXPORT_LENGTH;
1745 psa_status_t expected_status = expected_status_arg;
1746 psa_status_t status;
1747
1748 ASSERT_ALLOC( output, output_size );
1749
1750 PSA_ASSERT( psa_crypto_init( ) );
1751
1752 status = psa_hash_compute( alg, input->x, input->len,
1753 output, output_size, &output_length );
1754 TEST_EQUAL( status, expected_status );
1755 TEST_ASSERT( output_length <= output_size );
1756
1757exit:
1758 mbedtls_free( output );
1759 PSA_DONE( );
1760}
1761/* END_CASE */
1762
1763/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001764void hash_compare_fail( int alg_arg, data_t *input,
1765 data_t *reference_hash,
1766 int expected_status_arg )
1767{
1768 psa_algorithm_t alg = alg_arg;
1769 psa_status_t expected_status = expected_status_arg;
1770 psa_status_t status;
1771
1772 PSA_ASSERT( psa_crypto_init( ) );
1773
1774 status = psa_hash_compare( alg, input->x, input->len,
1775 reference_hash->x, reference_hash->len );
1776 TEST_EQUAL( status, expected_status );
1777
1778exit:
1779 PSA_DONE( );
1780}
1781/* END_CASE */
1782
1783/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001784void hash_compute_compare( int alg_arg, data_t *input,
1785 data_t *expected_output )
1786{
1787 psa_algorithm_t alg = alg_arg;
1788 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1789 size_t output_length = INVALID_EXPORT_LENGTH;
1790 size_t i;
1791
1792 PSA_ASSERT( psa_crypto_init( ) );
1793
1794 /* Compute with tight buffer */
1795 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001796 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001797 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001798 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001799 ASSERT_COMPARE( output, output_length,
1800 expected_output->x, expected_output->len );
1801
1802 /* Compute with larger buffer */
1803 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1804 output, sizeof( output ),
1805 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001806 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001807 ASSERT_COMPARE( output, output_length,
1808 expected_output->x, expected_output->len );
1809
1810 /* Compare with correct hash */
1811 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1812 output, output_length ) );
1813
1814 /* Compare with trailing garbage */
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 truncated hash */
1820 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1821 output, output_length - 1 ),
1822 PSA_ERROR_INVALID_SIGNATURE );
1823
1824 /* Compare with corrupted value */
1825 for( i = 0; i < output_length; i++ )
1826 {
Chris Jones9634bb12021-01-20 15:56:42 +00001827 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001828 output[i] ^= 1;
1829 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1830 output, output_length ),
1831 PSA_ERROR_INVALID_SIGNATURE );
1832 output[i] ^= 1;
1833 }
1834
1835exit:
1836 PSA_DONE( );
1837}
1838/* END_CASE */
1839
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001840/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001841void hash_bad_order( )
1842{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001843 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001844 unsigned char input[] = "";
1845 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001846 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001847 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1848 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1849 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001850 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001851 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001852 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001853
Gilles Peskine8817f612018-12-18 00:18:46 +01001854 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001855
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001856 /* Call setup twice in a row. */
1857 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1858 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1859 PSA_ERROR_BAD_STATE );
1860 PSA_ASSERT( psa_hash_abort( &operation ) );
1861
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001862 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001863 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001864 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001865 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001866
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001867 /* Call update after finish. */
1868 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1869 PSA_ASSERT( psa_hash_finish( &operation,
1870 hash, sizeof( hash ), &hash_len ) );
1871 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001872 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001873 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001874
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001875 /* Call verify without calling setup beforehand. */
1876 TEST_EQUAL( psa_hash_verify( &operation,
1877 valid_hash, sizeof( valid_hash ) ),
1878 PSA_ERROR_BAD_STATE );
1879 PSA_ASSERT( psa_hash_abort( &operation ) );
1880
1881 /* Call verify after finish. */
1882 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1883 PSA_ASSERT( psa_hash_finish( &operation,
1884 hash, sizeof( hash ), &hash_len ) );
1885 TEST_EQUAL( psa_hash_verify( &operation,
1886 valid_hash, sizeof( valid_hash ) ),
1887 PSA_ERROR_BAD_STATE );
1888 PSA_ASSERT( psa_hash_abort( &operation ) );
1889
1890 /* Call verify twice in a row. */
1891 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1892 PSA_ASSERT( psa_hash_verify( &operation,
1893 valid_hash, sizeof( valid_hash ) ) );
1894 TEST_EQUAL( psa_hash_verify( &operation,
1895 valid_hash, sizeof( valid_hash ) ),
1896 PSA_ERROR_BAD_STATE );
1897 PSA_ASSERT( psa_hash_abort( &operation ) );
1898
1899 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001900 TEST_EQUAL( psa_hash_finish( &operation,
1901 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001902 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001903 PSA_ASSERT( psa_hash_abort( &operation ) );
1904
1905 /* Call finish twice in a row. */
1906 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1907 PSA_ASSERT( psa_hash_finish( &operation,
1908 hash, sizeof( hash ), &hash_len ) );
1909 TEST_EQUAL( psa_hash_finish( &operation,
1910 hash, sizeof( hash ), &hash_len ),
1911 PSA_ERROR_BAD_STATE );
1912 PSA_ASSERT( psa_hash_abort( &operation ) );
1913
1914 /* Call finish after calling verify. */
1915 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1916 PSA_ASSERT( psa_hash_verify( &operation,
1917 valid_hash, sizeof( valid_hash ) ) );
1918 TEST_EQUAL( psa_hash_finish( &operation,
1919 hash, sizeof( hash ), &hash_len ),
1920 PSA_ERROR_BAD_STATE );
1921 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001922
1923exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001924 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001925}
1926/* END_CASE */
1927
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001928/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001929void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001930{
1931 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001932 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1933 * appended to it */
1934 unsigned char hash[] = {
1935 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1936 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1937 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001938 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001939 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001940
Gilles Peskine8817f612018-12-18 00:18:46 +01001941 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001942
itayzafrir27e69452018-11-01 14:26:34 +02001943 /* psa_hash_verify with a smaller hash than expected */
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, expected_size - 1 ),
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 non-matching hash */
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 + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001951 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001952
itayzafrir27e69452018-11-01 14:26:34 +02001953 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001954 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001955 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001956 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001957
itayzafrirec93d302018-10-18 18:01:10 +03001958exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001959 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001960}
1961/* END_CASE */
1962
Ronald Cronee414c72021-03-18 18:50:08 +01001963/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001964void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001965{
1966 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001967 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001968 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001969 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001970 size_t hash_len;
1971
Gilles Peskine8817f612018-12-18 00:18:46 +01001972 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001973
itayzafrir58028322018-10-25 10:22:01 +03001974 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001975 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001976 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001977 hash, expected_size - 1, &hash_len ),
1978 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001979
1980exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001981 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001982}
1983/* END_CASE */
1984
Ronald Cronee414c72021-03-18 18:50:08 +01001985/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001986void hash_clone_source_state( )
1987{
1988 psa_algorithm_t alg = PSA_ALG_SHA_256;
1989 unsigned char hash[PSA_HASH_MAX_SIZE];
1990 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1991 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1992 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1993 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1994 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1995 size_t hash_len;
1996
1997 PSA_ASSERT( psa_crypto_init( ) );
1998 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1999
2000 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2001 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2002 PSA_ASSERT( psa_hash_finish( &op_finished,
2003 hash, sizeof( hash ), &hash_len ) );
2004 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2005 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2006
2007 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2008 PSA_ERROR_BAD_STATE );
2009
2010 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2011 PSA_ASSERT( psa_hash_finish( &op_init,
2012 hash, sizeof( hash ), &hash_len ) );
2013 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2014 PSA_ASSERT( psa_hash_finish( &op_finished,
2015 hash, sizeof( hash ), &hash_len ) );
2016 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2017 PSA_ASSERT( psa_hash_finish( &op_aborted,
2018 hash, sizeof( hash ), &hash_len ) );
2019
2020exit:
2021 psa_hash_abort( &op_source );
2022 psa_hash_abort( &op_init );
2023 psa_hash_abort( &op_setup );
2024 psa_hash_abort( &op_finished );
2025 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002026 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002027}
2028/* END_CASE */
2029
Ronald Cronee414c72021-03-18 18:50:08 +01002030/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002031void hash_clone_target_state( )
2032{
2033 psa_algorithm_t alg = PSA_ALG_SHA_256;
2034 unsigned char hash[PSA_HASH_MAX_SIZE];
2035 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2036 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2037 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2038 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2039 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2040 size_t hash_len;
2041
2042 PSA_ASSERT( psa_crypto_init( ) );
2043
2044 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2045 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2046 PSA_ASSERT( psa_hash_finish( &op_finished,
2047 hash, sizeof( hash ), &hash_len ) );
2048 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2049 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2050
2051 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2052 PSA_ASSERT( psa_hash_finish( &op_target,
2053 hash, sizeof( hash ), &hash_len ) );
2054
2055 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2056 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2057 PSA_ERROR_BAD_STATE );
2058 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2059 PSA_ERROR_BAD_STATE );
2060
2061exit:
2062 psa_hash_abort( &op_target );
2063 psa_hash_abort( &op_init );
2064 psa_hash_abort( &op_setup );
2065 psa_hash_abort( &op_finished );
2066 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002067 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002068}
2069/* END_CASE */
2070
itayzafrir58028322018-10-25 10:22:01 +03002071/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002072void mac_operation_init( )
2073{
Jaeden Amero252ef282019-02-15 14:05:35 +00002074 const uint8_t input[1] = { 0 };
2075
Jaeden Amero769ce272019-01-04 11:48:03 +00002076 /* Test each valid way of initializing the object, except for `= {0}`, as
2077 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2078 * though it's OK by the C standard. We could test for this, but we'd need
2079 * to supress the Clang warning for the test. */
2080 psa_mac_operation_t func = psa_mac_operation_init( );
2081 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2082 psa_mac_operation_t zero;
2083
2084 memset( &zero, 0, sizeof( zero ) );
2085
Jaeden Amero252ef282019-02-15 14:05:35 +00002086 /* A freshly-initialized MAC operation should not be usable. */
2087 TEST_EQUAL( psa_mac_update( &func,
2088 input, sizeof( input ) ),
2089 PSA_ERROR_BAD_STATE );
2090 TEST_EQUAL( psa_mac_update( &init,
2091 input, sizeof( input ) ),
2092 PSA_ERROR_BAD_STATE );
2093 TEST_EQUAL( psa_mac_update( &zero,
2094 input, sizeof( input ) ),
2095 PSA_ERROR_BAD_STATE );
2096
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002097 /* A default MAC operation should be abortable without error. */
2098 PSA_ASSERT( psa_mac_abort( &func ) );
2099 PSA_ASSERT( psa_mac_abort( &init ) );
2100 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002101}
2102/* END_CASE */
2103
2104/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105void mac_setup( int key_type_arg,
2106 data_t *key,
2107 int alg_arg,
2108 int expected_status_arg )
2109{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002110 psa_key_type_t key_type = key_type_arg;
2111 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002112 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002113 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002114 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2115#if defined(KNOWN_SUPPORTED_MAC_ALG)
2116 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2117#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002118
Gilles Peskine8817f612018-12-18 00:18:46 +01002119 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002120
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002121 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2122 &operation, &status ) )
2123 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002124 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002125
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002126 /* The operation object should be reusable. */
2127#if defined(KNOWN_SUPPORTED_MAC_ALG)
2128 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2129 smoke_test_key_data,
2130 sizeof( smoke_test_key_data ),
2131 KNOWN_SUPPORTED_MAC_ALG,
2132 &operation, &status ) )
2133 goto exit;
2134 TEST_EQUAL( status, PSA_SUCCESS );
2135#endif
2136
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002137exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002138 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002139}
2140/* END_CASE */
2141
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002142/* 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 +00002143void mac_bad_order( )
2144{
Ronald Cron5425a212020-08-04 14:58:35 +02002145 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002146 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2147 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002148 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002149 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2150 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2151 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002153 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2154 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2155 size_t sign_mac_length = 0;
2156 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2157 const uint8_t verify_mac[] = {
2158 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2159 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2160 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2161
2162 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002163 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002164 psa_set_key_algorithm( &attributes, alg );
2165 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002166
Ronald Cron5425a212020-08-04 14:58:35 +02002167 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2168 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002169
Jaeden Amero252ef282019-02-15 14:05:35 +00002170 /* Call update without calling setup beforehand. */
2171 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2172 PSA_ERROR_BAD_STATE );
2173 PSA_ASSERT( psa_mac_abort( &operation ) );
2174
2175 /* Call sign finish without calling setup beforehand. */
2176 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2177 &sign_mac_length),
2178 PSA_ERROR_BAD_STATE );
2179 PSA_ASSERT( psa_mac_abort( &operation ) );
2180
2181 /* Call verify finish without calling setup beforehand. */
2182 TEST_EQUAL( psa_mac_verify_finish( &operation,
2183 verify_mac, sizeof( verify_mac ) ),
2184 PSA_ERROR_BAD_STATE );
2185 PSA_ASSERT( psa_mac_abort( &operation ) );
2186
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002187 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002188 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2189 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002190 PSA_ERROR_BAD_STATE );
2191 PSA_ASSERT( psa_mac_abort( &operation ) );
2192
Jaeden Amero252ef282019-02-15 14:05:35 +00002193 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002194 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002195 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2196 PSA_ASSERT( psa_mac_sign_finish( &operation,
2197 sign_mac, sizeof( sign_mac ),
2198 &sign_mac_length ) );
2199 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2200 PSA_ERROR_BAD_STATE );
2201 PSA_ASSERT( psa_mac_abort( &operation ) );
2202
2203 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002204 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002205 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2206 PSA_ASSERT( psa_mac_verify_finish( &operation,
2207 verify_mac, sizeof( verify_mac ) ) );
2208 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2209 PSA_ERROR_BAD_STATE );
2210 PSA_ASSERT( psa_mac_abort( &operation ) );
2211
2212 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002213 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002214 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2215 PSA_ASSERT( psa_mac_sign_finish( &operation,
2216 sign_mac, sizeof( sign_mac ),
2217 &sign_mac_length ) );
2218 TEST_EQUAL( psa_mac_sign_finish( &operation,
2219 sign_mac, sizeof( sign_mac ),
2220 &sign_mac_length ),
2221 PSA_ERROR_BAD_STATE );
2222 PSA_ASSERT( psa_mac_abort( &operation ) );
2223
2224 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002225 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002226 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2227 PSA_ASSERT( psa_mac_verify_finish( &operation,
2228 verify_mac, sizeof( verify_mac ) ) );
2229 TEST_EQUAL( psa_mac_verify_finish( &operation,
2230 verify_mac, sizeof( verify_mac ) ),
2231 PSA_ERROR_BAD_STATE );
2232 PSA_ASSERT( psa_mac_abort( &operation ) );
2233
2234 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002235 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002236 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2237 TEST_EQUAL( psa_mac_verify_finish( &operation,
2238 verify_mac, sizeof( verify_mac ) ),
2239 PSA_ERROR_BAD_STATE );
2240 PSA_ASSERT( psa_mac_abort( &operation ) );
2241
2242 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002243 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002244 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2245 TEST_EQUAL( psa_mac_sign_finish( &operation,
2246 sign_mac, sizeof( sign_mac ),
2247 &sign_mac_length ),
2248 PSA_ERROR_BAD_STATE );
2249 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002250
Ronald Cron5425a212020-08-04 14:58:35 +02002251 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002252
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002253exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002254 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002255}
2256/* END_CASE */
2257
2258/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002259void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002260 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002261 int alg_arg,
2262 data_t *input,
2263 data_t *expected_mac )
2264{
Ronald Cron5425a212020-08-04 14:58:35 +02002265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002266 psa_key_type_t key_type = key_type_arg;
2267 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002268 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002270 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002271 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002272 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002273 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002274 const size_t output_sizes_to_test[] = {
2275 0,
2276 1,
2277 expected_mac->len - 1,
2278 expected_mac->len,
2279 expected_mac->len + 1,
2280 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002281
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002282 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002283 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002284 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002285
Gilles Peskine8817f612018-12-18 00:18:46 +01002286 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002287
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002288 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_set_key_algorithm( &attributes, alg );
2290 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002291
Ronald Cron5425a212020-08-04 14:58:35 +02002292 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2293 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002294
Gilles Peskine8b356b52020-08-25 23:44:59 +02002295 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2296 {
2297 const size_t output_size = output_sizes_to_test[i];
2298 psa_status_t expected_status =
2299 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2300 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002301
Chris Jones9634bb12021-01-20 15:56:42 +00002302 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002303 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002304
Gilles Peskine8b356b52020-08-25 23:44:59 +02002305 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002306 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002307 PSA_ASSERT( psa_mac_update( &operation,
2308 input->x, input->len ) );
2309 TEST_EQUAL( psa_mac_sign_finish( &operation,
2310 actual_mac, output_size,
2311 &mac_length ),
2312 expected_status );
2313 PSA_ASSERT( psa_mac_abort( &operation ) );
2314
2315 if( expected_status == PSA_SUCCESS )
2316 {
2317 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2318 actual_mac, mac_length );
2319 }
2320 mbedtls_free( actual_mac );
2321 actual_mac = NULL;
2322 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002323
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002324exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002325 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002326 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002327 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002328 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002329}
2330/* END_CASE */
2331
2332/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002333void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002334 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002335 int alg_arg,
2336 data_t *input,
2337 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002338{
Ronald Cron5425a212020-08-04 14:58:35 +02002339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002340 psa_key_type_t key_type = key_type_arg;
2341 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002342 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002344 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002345
Gilles Peskine69c12672018-06-28 00:07:19 +02002346 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2347
Gilles Peskine8817f612018-12-18 00:18:46 +01002348 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002349
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002350 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002351 psa_set_key_algorithm( &attributes, alg );
2352 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002353
Ronald Cron5425a212020-08-04 14:58:35 +02002354 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2355 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002356
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002357 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002358 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002359 PSA_ASSERT( psa_mac_update( &operation,
2360 input->x, input->len ) );
2361 PSA_ASSERT( psa_mac_verify_finish( &operation,
2362 expected_mac->x,
2363 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002364
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002365 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002366 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002367 PSA_ASSERT( psa_mac_update( &operation,
2368 input->x, input->len ) );
2369 TEST_EQUAL( psa_mac_verify_finish( &operation,
2370 expected_mac->x,
2371 expected_mac->len - 1 ),
2372 PSA_ERROR_INVALID_SIGNATURE );
2373
2374 /* Test a MAC that's too long. */
2375 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2376 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002377 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002378 PSA_ASSERT( psa_mac_update( &operation,
2379 input->x, input->len ) );
2380 TEST_EQUAL( psa_mac_verify_finish( &operation,
2381 perturbed_mac,
2382 expected_mac->len + 1 ),
2383 PSA_ERROR_INVALID_SIGNATURE );
2384
2385 /* Test changing one byte. */
2386 for( size_t i = 0; i < expected_mac->len; i++ )
2387 {
Chris Jones9634bb12021-01-20 15:56:42 +00002388 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002389 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002390 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002391 PSA_ASSERT( psa_mac_update( &operation,
2392 input->x, input->len ) );
2393 TEST_EQUAL( psa_mac_verify_finish( &operation,
2394 perturbed_mac,
2395 expected_mac->len ),
2396 PSA_ERROR_INVALID_SIGNATURE );
2397 perturbed_mac[i] ^= 1;
2398 }
2399
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002401 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002402 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002403 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002404 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002405}
2406/* END_CASE */
2407
2408/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002409void cipher_operation_init( )
2410{
Jaeden Ameroab439972019-02-15 14:12:05 +00002411 const uint8_t input[1] = { 0 };
2412 unsigned char output[1] = { 0 };
2413 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002414 /* Test each valid way of initializing the object, except for `= {0}`, as
2415 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2416 * though it's OK by the C standard. We could test for this, but we'd need
2417 * to supress the Clang warning for the test. */
2418 psa_cipher_operation_t func = psa_cipher_operation_init( );
2419 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2420 psa_cipher_operation_t zero;
2421
2422 memset( &zero, 0, sizeof( zero ) );
2423
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 /* A freshly-initialized cipher operation should not be usable. */
2425 TEST_EQUAL( psa_cipher_update( &func,
2426 input, sizeof( input ),
2427 output, sizeof( output ),
2428 &output_length ),
2429 PSA_ERROR_BAD_STATE );
2430 TEST_EQUAL( psa_cipher_update( &init,
2431 input, sizeof( input ),
2432 output, sizeof( output ),
2433 &output_length ),
2434 PSA_ERROR_BAD_STATE );
2435 TEST_EQUAL( psa_cipher_update( &zero,
2436 input, sizeof( input ),
2437 output, sizeof( output ),
2438 &output_length ),
2439 PSA_ERROR_BAD_STATE );
2440
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002441 /* A default cipher operation should be abortable without error. */
2442 PSA_ASSERT( psa_cipher_abort( &func ) );
2443 PSA_ASSERT( psa_cipher_abort( &init ) );
2444 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002445}
2446/* END_CASE */
2447
2448/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449void cipher_setup( int key_type_arg,
2450 data_t *key,
2451 int alg_arg,
2452 int expected_status_arg )
2453{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002454 psa_key_type_t key_type = key_type_arg;
2455 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002456 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002457 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002458 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002459#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002460 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2461#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002462
Gilles Peskine8817f612018-12-18 00:18:46 +01002463 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002464
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002465 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2466 &operation, &status ) )
2467 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002468 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002469
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002470 /* The operation object should be reusable. */
2471#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2472 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2473 smoke_test_key_data,
2474 sizeof( smoke_test_key_data ),
2475 KNOWN_SUPPORTED_CIPHER_ALG,
2476 &operation, &status ) )
2477 goto exit;
2478 TEST_EQUAL( status, PSA_SUCCESS );
2479#endif
2480
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002481exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002482 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002483 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002484}
2485/* END_CASE */
2486
Ronald Cronee414c72021-03-18 18:50:08 +01002487/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002488void cipher_bad_order( )
2489{
Ronald Cron5425a212020-08-04 14:58:35 +02002490 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002491 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2492 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002494 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002495 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002496 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002497 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2498 0xaa, 0xaa, 0xaa, 0xaa };
2499 const uint8_t text[] = {
2500 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2501 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002502 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002503 size_t length = 0;
2504
2505 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002506 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2507 psa_set_key_algorithm( &attributes, alg );
2508 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002509 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2510 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002511
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002512 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002513 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2514 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002515 PSA_ERROR_BAD_STATE );
2516 PSA_ASSERT( psa_cipher_abort( &operation ) );
2517
2518 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002519 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2520 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002521 PSA_ERROR_BAD_STATE );
2522 PSA_ASSERT( psa_cipher_abort( &operation ) );
2523
Jaeden Ameroab439972019-02-15 14:12:05 +00002524 /* Generate an IV without calling setup beforehand. */
2525 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2526 buffer, sizeof( buffer ),
2527 &length ),
2528 PSA_ERROR_BAD_STATE );
2529 PSA_ASSERT( psa_cipher_abort( &operation ) );
2530
2531 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002532 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002533 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2534 buffer, sizeof( buffer ),
2535 &length ) );
2536 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2537 buffer, sizeof( buffer ),
2538 &length ),
2539 PSA_ERROR_BAD_STATE );
2540 PSA_ASSERT( psa_cipher_abort( &operation ) );
2541
2542 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002543 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002544 PSA_ASSERT( psa_cipher_set_iv( &operation,
2545 iv, sizeof( iv ) ) );
2546 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2547 buffer, sizeof( buffer ),
2548 &length ),
2549 PSA_ERROR_BAD_STATE );
2550 PSA_ASSERT( psa_cipher_abort( &operation ) );
2551
2552 /* Set an IV without calling setup beforehand. */
2553 TEST_EQUAL( psa_cipher_set_iv( &operation,
2554 iv, sizeof( iv ) ),
2555 PSA_ERROR_BAD_STATE );
2556 PSA_ASSERT( psa_cipher_abort( &operation ) );
2557
2558 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002559 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002560 PSA_ASSERT( psa_cipher_set_iv( &operation,
2561 iv, sizeof( iv ) ) );
2562 TEST_EQUAL( psa_cipher_set_iv( &operation,
2563 iv, sizeof( iv ) ),
2564 PSA_ERROR_BAD_STATE );
2565 PSA_ASSERT( psa_cipher_abort( &operation ) );
2566
2567 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002568 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002569 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2570 buffer, sizeof( buffer ),
2571 &length ) );
2572 TEST_EQUAL( psa_cipher_set_iv( &operation,
2573 iv, sizeof( iv ) ),
2574 PSA_ERROR_BAD_STATE );
2575 PSA_ASSERT( psa_cipher_abort( &operation ) );
2576
2577 /* Call update without calling setup beforehand. */
2578 TEST_EQUAL( psa_cipher_update( &operation,
2579 text, sizeof( text ),
2580 buffer, sizeof( buffer ),
2581 &length ),
2582 PSA_ERROR_BAD_STATE );
2583 PSA_ASSERT( psa_cipher_abort( &operation ) );
2584
2585 /* Call update without an IV where an IV is required. */
2586 TEST_EQUAL( psa_cipher_update( &operation,
2587 text, sizeof( text ),
2588 buffer, sizeof( buffer ),
2589 &length ),
2590 PSA_ERROR_BAD_STATE );
2591 PSA_ASSERT( psa_cipher_abort( &operation ) );
2592
2593 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002594 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002595 PSA_ASSERT( psa_cipher_set_iv( &operation,
2596 iv, sizeof( iv ) ) );
2597 PSA_ASSERT( psa_cipher_finish( &operation,
2598 buffer, sizeof( buffer ), &length ) );
2599 TEST_EQUAL( psa_cipher_update( &operation,
2600 text, sizeof( text ),
2601 buffer, sizeof( buffer ),
2602 &length ),
2603 PSA_ERROR_BAD_STATE );
2604 PSA_ASSERT( psa_cipher_abort( &operation ) );
2605
2606 /* Call finish without calling setup beforehand. */
2607 TEST_EQUAL( psa_cipher_finish( &operation,
2608 buffer, sizeof( buffer ), &length ),
2609 PSA_ERROR_BAD_STATE );
2610 PSA_ASSERT( psa_cipher_abort( &operation ) );
2611
2612 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002613 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002614 /* Not calling update means we are encrypting an empty buffer, which is OK
2615 * for cipher modes with padding. */
2616 TEST_EQUAL( psa_cipher_finish( &operation,
2617 buffer, sizeof( buffer ), &length ),
2618 PSA_ERROR_BAD_STATE );
2619 PSA_ASSERT( psa_cipher_abort( &operation ) );
2620
2621 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002622 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002623 PSA_ASSERT( psa_cipher_set_iv( &operation,
2624 iv, sizeof( iv ) ) );
2625 PSA_ASSERT( psa_cipher_finish( &operation,
2626 buffer, sizeof( buffer ), &length ) );
2627 TEST_EQUAL( psa_cipher_finish( &operation,
2628 buffer, sizeof( buffer ), &length ),
2629 PSA_ERROR_BAD_STATE );
2630 PSA_ASSERT( psa_cipher_abort( &operation ) );
2631
Ronald Cron5425a212020-08-04 14:58:35 +02002632 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002633
Jaeden Ameroab439972019-02-15 14:12:05 +00002634exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002635 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002636 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002637}
2638/* END_CASE */
2639
2640/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002641void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002642 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002643 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002644 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002645{
Ronald Cron5425a212020-08-04 14:58:35 +02002646 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647 psa_status_t status;
2648 psa_key_type_t key_type = key_type_arg;
2649 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002650 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002651 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652 size_t output_buffer_size = 0;
2653 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002654 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002655 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657
Gilles Peskine8817f612018-12-18 00:18:46 +01002658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002660 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2661 psa_set_key_algorithm( &attributes, alg );
2662 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002663
Ronald Cron5425a212020-08-04 14:58:35 +02002664 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2665 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666
Ronald Cron5425a212020-08-04 14:58:35 +02002667 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002668
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002669 if( iv->len > 0 )
2670 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002671 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002672 }
2673
gabor-mezei-armceface22021-01-21 12:26:17 +01002674 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2675 TEST_ASSERT( output_buffer_size <=
2676 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002677 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678
Gilles Peskine8817f612018-12-18 00:18:46 +01002679 PSA_ASSERT( psa_cipher_update( &operation,
2680 input->x, input->len,
2681 output, output_buffer_size,
2682 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002683 TEST_ASSERT( function_output_length <=
2684 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2685 TEST_ASSERT( function_output_length <=
2686 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002687 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002688
Gilles Peskine50e586b2018-06-08 14:28:46 +02002689 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002690 ( output_buffer_size == 0 ? NULL :
2691 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002692 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002693 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002694 TEST_ASSERT( function_output_length <=
2695 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2696 TEST_ASSERT( function_output_length <=
2697 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002698 total_output_length += function_output_length;
2699
Gilles Peskinefe11b722018-12-18 00:24:04 +01002700 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002701 if( expected_status == PSA_SUCCESS )
2702 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002704 ASSERT_COMPARE( expected_output->x, expected_output->len,
2705 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002707
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002709 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002710 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002711 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002712 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713}
2714/* END_CASE */
2715
2716/* BEGIN_CASE */
2717void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002718 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002719 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002720 int first_part_size_arg,
2721 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002722 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002723{
Ronald Cron5425a212020-08-04 14:58:35 +02002724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002725 psa_key_type_t key_type = key_type_arg;
2726 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002727 size_t first_part_size = first_part_size_arg;
2728 size_t output1_length = output1_length_arg;
2729 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002730 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002731 size_t output_buffer_size = 0;
2732 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002733 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002734 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002735 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736
Gilles Peskine8817f612018-12-18 00:18:46 +01002737 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002739 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2740 psa_set_key_algorithm( &attributes, alg );
2741 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002742
Ronald Cron5425a212020-08-04 14:58:35 +02002743 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2744 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002745
Ronald Cron5425a212020-08-04 14:58:35 +02002746 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002747
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002748 if( iv->len > 0 )
2749 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002750 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002751 }
2752
gabor-mezei-armceface22021-01-21 12:26:17 +01002753 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2754 TEST_ASSERT( output_buffer_size <=
2755 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002756 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002757
Gilles Peskinee0866522019-02-19 19:44:00 +01002758 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002759 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2760 output, output_buffer_size,
2761 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002762 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002763 TEST_ASSERT( function_output_length <=
2764 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2765 TEST_ASSERT( function_output_length <=
2766 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002767 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002768
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 PSA_ASSERT( psa_cipher_update( &operation,
2770 input->x + first_part_size,
2771 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002772 ( output_buffer_size == 0 ? NULL :
2773 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002774 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002775 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002776 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002777 TEST_ASSERT( function_output_length <=
2778 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2779 alg,
2780 input->len - first_part_size ) );
2781 TEST_ASSERT( function_output_length <=
2782 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002783 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002784
Gilles Peskine8817f612018-12-18 00:18:46 +01002785 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002786 ( output_buffer_size == 0 ? NULL :
2787 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002788 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002790 TEST_ASSERT( function_output_length <=
2791 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2792 TEST_ASSERT( function_output_length <=
2793 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002794 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002796
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002797 ASSERT_COMPARE( expected_output->x, expected_output->len,
2798 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002799
2800exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002801 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002802 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002803 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002804 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805}
2806/* END_CASE */
2807
2808/* BEGIN_CASE */
2809void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002810 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002811 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002812 int first_part_size_arg,
2813 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815{
Ronald Cron5425a212020-08-04 14:58:35 +02002816 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002817 psa_key_type_t key_type = key_type_arg;
2818 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002819 size_t first_part_size = first_part_size_arg;
2820 size_t output1_length = output1_length_arg;
2821 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002822 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823 size_t output_buffer_size = 0;
2824 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002825 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002826 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828
Gilles Peskine8817f612018-12-18 00:18:46 +01002829 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2832 psa_set_key_algorithm( &attributes, alg );
2833 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002834
Ronald Cron5425a212020-08-04 14:58:35 +02002835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2836 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837
Ronald Cron5425a212020-08-04 14:58:35 +02002838 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839
Steven Cooreman177deba2020-09-07 17:14:14 +02002840 if( iv->len > 0 )
2841 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002842 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002843 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
gabor-mezei-armceface22021-01-21 12:26:17 +01002845 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2846 TEST_ASSERT( output_buffer_size <=
2847 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002848 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002849
Gilles Peskinee0866522019-02-19 19:44:00 +01002850 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002851 PSA_ASSERT( psa_cipher_update( &operation,
2852 input->x, first_part_size,
2853 output, output_buffer_size,
2854 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002855 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002856 TEST_ASSERT( function_output_length <=
2857 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2858 TEST_ASSERT( function_output_length <=
2859 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002860 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002861
Gilles Peskine8817f612018-12-18 00:18:46 +01002862 PSA_ASSERT( psa_cipher_update( &operation,
2863 input->x + first_part_size,
2864 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002865 ( output_buffer_size == 0 ? NULL :
2866 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002867 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002868 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002869 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002870 TEST_ASSERT( function_output_length <=
2871 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2872 alg,
2873 input->len - first_part_size ) );
2874 TEST_ASSERT( function_output_length <=
2875 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002876 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002877
Gilles Peskine8817f612018-12-18 00:18:46 +01002878 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002879 ( output_buffer_size == 0 ? NULL :
2880 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002881 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002882 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002883 TEST_ASSERT( function_output_length <=
2884 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2885 TEST_ASSERT( function_output_length <=
2886 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002887 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002888 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002890 ASSERT_COMPARE( expected_output->x, expected_output->len,
2891 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892
2893exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002894 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002895 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002896 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002897 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898}
2899/* END_CASE */
2900
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901/* BEGIN_CASE */
2902void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002903 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002904 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002905 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002906{
Ronald Cron5425a212020-08-04 14:58:35 +02002907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908 psa_status_t status;
2909 psa_key_type_t key_type = key_type_arg;
2910 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002911 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002912 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913 size_t output_buffer_size = 0;
2914 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002915 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002916 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002918
Gilles Peskine8817f612018-12-18 00:18:46 +01002919 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002921 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2922 psa_set_key_algorithm( &attributes, alg );
2923 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002924
Ronald Cron5425a212020-08-04 14:58:35 +02002925 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2926 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927
Ronald Cron5425a212020-08-04 14:58:35 +02002928 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929
Steven Cooreman177deba2020-09-07 17:14:14 +02002930 if( iv->len > 0 )
2931 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002932 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002933 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934
gabor-mezei-armceface22021-01-21 12:26:17 +01002935 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2936 TEST_ASSERT( output_buffer_size <=
2937 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002938 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002939
Gilles Peskine8817f612018-12-18 00:18:46 +01002940 PSA_ASSERT( psa_cipher_update( &operation,
2941 input->x, input->len,
2942 output, output_buffer_size,
2943 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002944 TEST_ASSERT( function_output_length <=
2945 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2946 TEST_ASSERT( function_output_length <=
2947 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002948 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002949
Gilles Peskine50e586b2018-06-08 14:28:46 +02002950 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002951 ( output_buffer_size == 0 ? NULL :
2952 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002953 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002954 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002955 TEST_ASSERT( function_output_length <=
2956 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2957 TEST_ASSERT( function_output_length <=
2958 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002959 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002960 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961
2962 if( expected_status == PSA_SUCCESS )
2963 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002964 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002965 ASSERT_COMPARE( expected_output->x, expected_output->len,
2966 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967 }
2968
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002970 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002971 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002972 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002973 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974}
2975/* END_CASE */
2976
Gilles Peskine50e586b2018-06-08 14:28:46 +02002977/* BEGIN_CASE */
2978void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002979 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002980 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981{
Ronald Cron5425a212020-08-04 14:58:35 +02002982 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002983 psa_key_type_t key_type = key_type_arg;
2984 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002985 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002986 size_t iv_size = 16;
2987 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002988 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002989 size_t output1_size = 0;
2990 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002991 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002992 size_t output2_size = 0;
2993 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002994 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002995 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2996 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002998
Gilles Peskine8817f612018-12-18 00:18:46 +01002999 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003000
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003001 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3002 psa_set_key_algorithm( &attributes, alg );
3003 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003004
Ronald Cron5425a212020-08-04 14:58:35 +02003005 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3006 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003007
Ronald Cron5425a212020-08-04 14:58:35 +02003008 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3009 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003010
Steven Cooreman177deba2020-09-07 17:14:14 +02003011 if( alg != PSA_ALG_ECB_NO_PADDING )
3012 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003013 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3014 iv, iv_size,
3015 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003016 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003017 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3018 TEST_ASSERT( output1_size <=
3019 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003020 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003021
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3023 output1, output1_size,
3024 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003025 TEST_ASSERT( output1_length <=
3026 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3027 TEST_ASSERT( output1_length <=
3028 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3029
Gilles Peskine8817f612018-12-18 00:18:46 +01003030 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003031 output1 + output1_length,
3032 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003033 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003034 TEST_ASSERT( function_output_length <=
3035 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3036 TEST_ASSERT( function_output_length <=
3037 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003038
Gilles Peskine048b7f02018-06-08 14:20:49 +02003039 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003040
Gilles Peskine8817f612018-12-18 00:18:46 +01003041 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003042
3043 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003044 TEST_ASSERT( output2_size <=
3045 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3046 TEST_ASSERT( output2_size <=
3047 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003048 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003049
Steven Cooreman177deba2020-09-07 17:14:14 +02003050 if( iv_length > 0 )
3051 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003052 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3053 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003054 }
3055
Gilles Peskine8817f612018-12-18 00:18:46 +01003056 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3057 output2, output2_size,
3058 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003059 TEST_ASSERT( output2_length <=
3060 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3061 TEST_ASSERT( output2_length <=
3062 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3063
Gilles Peskine048b7f02018-06-08 14:20:49 +02003064 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003065 PSA_ASSERT( psa_cipher_finish( &operation2,
3066 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003067 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003068 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003069 TEST_ASSERT( function_output_length <=
3070 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3071 TEST_ASSERT( function_output_length <=
3072 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003073
Gilles Peskine048b7f02018-06-08 14:20:49 +02003074 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003075
Gilles Peskine8817f612018-12-18 00:18:46 +01003076 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003077
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003078 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003079
3080exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003081 psa_cipher_abort( &operation1 );
3082 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003083 mbedtls_free( output1 );
3084 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003085 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003086 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003087}
3088/* END_CASE */
3089
3090/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003091void cipher_verify_output_multipart( int alg_arg,
3092 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003093 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003094 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003095 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003096{
Ronald Cron5425a212020-08-04 14:58:35 +02003097 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003098 psa_key_type_t key_type = key_type_arg;
3099 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003100 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003101 unsigned char iv[16] = {0};
3102 size_t iv_size = 16;
3103 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003104 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003105 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003106 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003107 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003108 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003109 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003110 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003111 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3112 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003114
Gilles Peskine8817f612018-12-18 00:18:46 +01003115 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003116
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003117 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3118 psa_set_key_algorithm( &attributes, alg );
3119 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003120
Ronald Cron5425a212020-08-04 14:58:35 +02003121 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3122 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003123
Ronald Cron5425a212020-08-04 14:58:35 +02003124 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3125 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003126
Steven Cooreman177deba2020-09-07 17:14:14 +02003127 if( alg != PSA_ALG_ECB_NO_PADDING )
3128 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003129 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3130 iv, iv_size,
3131 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003132 }
3133
gabor-mezei-armceface22021-01-21 12:26:17 +01003134 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3135 TEST_ASSERT( output1_buffer_size <=
3136 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003137 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003138
Gilles Peskinee0866522019-02-19 19:44:00 +01003139 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003140
Gilles Peskine8817f612018-12-18 00:18:46 +01003141 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3142 output1, output1_buffer_size,
3143 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003144 TEST_ASSERT( function_output_length <=
3145 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3146 TEST_ASSERT( function_output_length <=
3147 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003148 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003149
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 PSA_ASSERT( psa_cipher_update( &operation1,
3151 input->x + first_part_size,
3152 input->len - first_part_size,
3153 output1, output1_buffer_size,
3154 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003155 TEST_ASSERT( function_output_length <=
3156 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3157 alg,
3158 input->len - first_part_size ) );
3159 TEST_ASSERT( function_output_length <=
3160 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003161 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003162
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 PSA_ASSERT( psa_cipher_finish( &operation1,
3164 output1 + output1_length,
3165 output1_buffer_size - output1_length,
3166 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003167 TEST_ASSERT( function_output_length <=
3168 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3169 TEST_ASSERT( function_output_length <=
3170 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003171 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003172
Gilles Peskine8817f612018-12-18 00:18:46 +01003173 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003174
Gilles Peskine048b7f02018-06-08 14:20:49 +02003175 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003176 TEST_ASSERT( output2_buffer_size <=
3177 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3178 TEST_ASSERT( output2_buffer_size <=
3179 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003180 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003181
Steven Cooreman177deba2020-09-07 17:14:14 +02003182 if( iv_length > 0 )
3183 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003184 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3185 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003186 }
Moran Pekerded84402018-06-06 16:36:50 +03003187
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3189 output2, output2_buffer_size,
3190 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003191 TEST_ASSERT( function_output_length <=
3192 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3193 TEST_ASSERT( function_output_length <=
3194 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003195 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003196
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_cipher_update( &operation2,
3198 output1 + first_part_size,
3199 output1_length - first_part_size,
3200 output2, output2_buffer_size,
3201 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003202 TEST_ASSERT( function_output_length <=
3203 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3204 alg,
3205 output1_length - first_part_size ) );
3206 TEST_ASSERT( function_output_length <=
3207 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003208 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003209
Gilles Peskine8817f612018-12-18 00:18:46 +01003210 PSA_ASSERT( psa_cipher_finish( &operation2,
3211 output2 + output2_length,
3212 output2_buffer_size - output2_length,
3213 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003214 TEST_ASSERT( function_output_length <=
3215 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3216 TEST_ASSERT( function_output_length <=
3217 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003218 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003219
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003221
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003222 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003223
3224exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003225 psa_cipher_abort( &operation1 );
3226 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003227 mbedtls_free( output1 );
3228 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003229 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003230 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003231}
3232/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003233
Gilles Peskine20035e32018-02-03 22:44:14 +01003234/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003235void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003236 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003237 data_t *nonce,
3238 data_t *additional_data,
3239 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003240 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003241{
Ronald Cron5425a212020-08-04 14:58:35 +02003242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003243 psa_key_type_t key_type = key_type_arg;
3244 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003245 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003246 unsigned char *output_data = NULL;
3247 size_t output_size = 0;
3248 size_t output_length = 0;
3249 unsigned char *output_data2 = NULL;
3250 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003251 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003252 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254
Gilles Peskine8817f612018-12-18 00:18:46 +01003255 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003256
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3258 psa_set_key_algorithm( &attributes, alg );
3259 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003260
Gilles Peskine049c7532019-05-15 20:22:09 +02003261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003262 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003263 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3264 key_bits = psa_get_key_bits( &attributes );
3265
3266 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3267 alg );
3268 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3269 * should be exact. */
3270 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3271 expected_result != PSA_ERROR_NOT_SUPPORTED )
3272 {
3273 TEST_EQUAL( output_size,
3274 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3275 TEST_ASSERT( output_size <=
3276 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3277 }
3278 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003279
Steven Cooremanf49478b2021-02-15 15:19:25 +01003280 status = psa_aead_encrypt( key, alg,
3281 nonce->x, nonce->len,
3282 additional_data->x,
3283 additional_data->len,
3284 input_data->x, input_data->len,
3285 output_data, output_size,
3286 &output_length );
3287
3288 /* If the operation is not supported, just skip and not fail in case the
3289 * encryption involves a common limitation of cryptography hardwares and
3290 * an alternative implementation. */
3291 if( status == PSA_ERROR_NOT_SUPPORTED )
3292 {
3293 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3294 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3295 }
3296
3297 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003298
3299 if( PSA_SUCCESS == expected_result )
3300 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003301 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003302
Gilles Peskine003a4a92019-05-14 16:09:40 +02003303 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3304 * should be exact. */
3305 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003306 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003307
gabor-mezei-armceface22021-01-21 12:26:17 +01003308 TEST_ASSERT( input_data->len <=
3309 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3310
Ronald Cron5425a212020-08-04 14:58:35 +02003311 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003312 nonce->x, nonce->len,
3313 additional_data->x,
3314 additional_data->len,
3315 output_data, output_length,
3316 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003317 &output_length2 ),
3318 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003319
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003320 ASSERT_COMPARE( input_data->x, input_data->len,
3321 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003322 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003323
Gilles Peskinea1cac842018-06-11 19:33:02 +02003324exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003325 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003326 mbedtls_free( output_data );
3327 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003328 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329}
3330/* END_CASE */
3331
3332/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003333void aead_encrypt( int key_type_arg, data_t *key_data,
3334 int alg_arg,
3335 data_t *nonce,
3336 data_t *additional_data,
3337 data_t *input_data,
3338 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339{
Ronald Cron5425a212020-08-04 14:58:35 +02003340 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341 psa_key_type_t key_type = key_type_arg;
3342 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003343 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344 unsigned char *output_data = NULL;
3345 size_t output_size = 0;
3346 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003348 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003349
Gilles Peskine8817f612018-12-18 00:18:46 +01003350 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3353 psa_set_key_algorithm( &attributes, alg );
3354 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003355
Gilles Peskine049c7532019-05-15 20:22:09 +02003356 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003357 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003358 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3359 key_bits = psa_get_key_bits( &attributes );
3360
3361 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3362 alg );
3363 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3364 * should be exact. */
3365 TEST_EQUAL( output_size,
3366 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3367 TEST_ASSERT( output_size <=
3368 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3369 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003370
Steven Cooremand588ea12021-01-11 19:36:04 +01003371 status = psa_aead_encrypt( key, alg,
3372 nonce->x, nonce->len,
3373 additional_data->x, additional_data->len,
3374 input_data->x, input_data->len,
3375 output_data, output_size,
3376 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377
Ronald Cron28a45ed2021-02-09 20:35:42 +01003378 /* If the operation is not supported, just skip and not fail in case the
3379 * encryption involves a common limitation of cryptography hardwares and
3380 * an alternative implementation. */
3381 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003382 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003383 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3384 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003385 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003386
3387 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003388 ASSERT_COMPARE( expected_result->x, expected_result->len,
3389 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003390
Gilles Peskinea1cac842018-06-11 19:33:02 +02003391exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003392 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003393 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003394 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003395}
3396/* END_CASE */
3397
3398/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003399void aead_decrypt( int key_type_arg, data_t *key_data,
3400 int alg_arg,
3401 data_t *nonce,
3402 data_t *additional_data,
3403 data_t *input_data,
3404 data_t *expected_data,
3405 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406{
Ronald Cron5425a212020-08-04 14:58:35 +02003407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003408 psa_key_type_t key_type = key_type_arg;
3409 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003410 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003411 unsigned char *output_data = NULL;
3412 size_t output_size = 0;
3413 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003415 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003416 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417
Gilles Peskine8817f612018-12-18 00:18:46 +01003418 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003419
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003420 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3421 psa_set_key_algorithm( &attributes, alg );
3422 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003423
Gilles Peskine049c7532019-05-15 20:22:09 +02003424 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003425 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003426 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3427 key_bits = psa_get_key_bits( &attributes );
3428
3429 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3430 alg );
3431 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3432 expected_result != PSA_ERROR_NOT_SUPPORTED )
3433 {
3434 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3435 * should be exact. */
3436 TEST_EQUAL( output_size,
3437 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3438 TEST_ASSERT( output_size <=
3439 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3440 }
3441 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442
Steven Cooremand588ea12021-01-11 19:36:04 +01003443 status = psa_aead_decrypt( key, alg,
3444 nonce->x, nonce->len,
3445 additional_data->x,
3446 additional_data->len,
3447 input_data->x, input_data->len,
3448 output_data, output_size,
3449 &output_length );
3450
Ronald Cron28a45ed2021-02-09 20:35:42 +01003451 /* If the operation is not supported, just skip and not fail in case the
3452 * decryption involves a common limitation of cryptography hardwares and
3453 * an alternative implementation. */
3454 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003455 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003456 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3457 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003458 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003459
3460 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461
Gilles Peskine2d277862018-06-18 15:41:12 +02003462 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003463 ASSERT_COMPARE( expected_data->x, expected_data->len,
3464 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465
Gilles Peskinea1cac842018-06-11 19:33:02 +02003466exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003467 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003468 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003469 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470}
3471/* END_CASE */
3472
3473/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003474void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3475 int alg_arg,
3476 data_t *nonce,
3477 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003478 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003479 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003480 int do_test_data_chunked,
3481 int do_set_lengths,
3482 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003483{
Paul Elliottd3f82412021-06-16 16:52:21 +01003484 size_t ad_part_len = 0;
3485 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003486 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003487
Paul Elliotte64deda2021-09-09 14:07:23 +01003488 /* Ensure that either one part of the test or the other is done, i.e this
3489 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003490 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3491
3492 /* Temporary whilst we have algorithms that cannot support chunking */
3493 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003494 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003495 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3496 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003497 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003498 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003499
Paul Elliott33746aa2021-09-15 16:40:40 +01003500 if( do_set_lengths )
3501 {
3502 if( ad_part_len & 0x01 )
3503 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3504 else
3505 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3506 }
3507
Paul Elliott329d5382021-07-22 17:10:45 +01003508 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003509 if( !aead_multipart_internal_func( key_type_arg, key_data,
3510 alg_arg, nonce,
3511 additional_data,
3512 ad_part_len,
3513 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003514 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003515 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003516 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003517 break;
3518
3519 /* length(0) part, length(ad_part_len) part, length(0) part... */
3520 mbedtls_test_set_step( 1000 + ad_part_len );
3521
3522 if( !aead_multipart_internal_func( key_type_arg, key_data,
3523 alg_arg, nonce,
3524 additional_data,
3525 ad_part_len,
3526 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003527 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003528 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003529 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003530 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003531 }
3532 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003533
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003534 /* Temporary whilst we have algorithms that cannot support chunking */
3535 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003536 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003537 for( data_part_len = 1; data_part_len <= input_data->len;
3538 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003539 {
Paul Elliott329d5382021-07-22 17:10:45 +01003540 /* Split data into length(data_part_len) parts. */
3541 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003542
Paul Elliott33746aa2021-09-15 16:40:40 +01003543 if( do_set_lengths )
3544 {
3545 if( data_part_len & 0x01 )
3546 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3547 else
3548 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3549 }
3550
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003551 if( !aead_multipart_internal_func( key_type_arg, key_data,
3552 alg_arg, nonce,
3553 additional_data, -1,
3554 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003555 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003556 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003557 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003558 break;
3559
3560 /* length(0) part, length(data_part_len) part, length(0) part... */
3561 mbedtls_test_set_step( 3000 + data_part_len );
3562
3563 if( !aead_multipart_internal_func( key_type_arg, key_data,
3564 alg_arg, nonce,
3565 additional_data, -1,
3566 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003567 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003568 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003569 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003570 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003571 }
3572 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003573
Paul Elliott0023e0a2021-04-27 10:06:22 +01003574
Paul Elliott8fc45162021-06-23 16:06:01 +01003575 /* Goto is required to silence warnings about unused labels, as we
3576 * don't actually do any test assertions in this function. */
3577 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003578}
3579/* END_CASE */
3580
3581/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3583 int alg_arg,
3584 data_t *nonce,
3585 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003586 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003587 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003588 int do_test_data_chunked,
3589 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003590 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003591{
Paul Elliottd3f82412021-06-16 16:52:21 +01003592 size_t ad_part_len = 0;
3593 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003594 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003595
Paul Elliotte64deda2021-09-09 14:07:23 +01003596 /* Ensure that either one part of the test or the other is done, i.e this
3597 * test does something. */
3598 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3599
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003600 /* Temporary whilst we have algorithms that cannot support chunking */
3601 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003602 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003603 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3604 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003605 {
Paul Elliott329d5382021-07-22 17:10:45 +01003606 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003607 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003608
Paul Elliott33746aa2021-09-15 16:40:40 +01003609 if( do_set_lengths )
3610 {
3611 if( ad_part_len & 0x01 )
3612 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3613 else
3614 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3615 }
3616
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003617 if( !aead_multipart_internal_func( key_type_arg, key_data,
3618 alg_arg, nonce,
3619 additional_data,
3620 ad_part_len,
3621 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003622 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003623 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003624 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003625 break;
3626
3627 /* length(0) part, length(ad_part_len) part, length(0) part... */
3628 mbedtls_test_set_step( 1000 + ad_part_len );
3629
3630 if( !aead_multipart_internal_func( key_type_arg, key_data,
3631 alg_arg, nonce,
3632 additional_data,
3633 ad_part_len,
3634 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003635 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003636 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003637 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003638 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003639 }
3640 }
3641
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003642 /* Temporary whilst we have algorithms that cannot support chunking */
3643 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003644 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003645 for( data_part_len = 1; data_part_len <= input_data->len;
3646 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003647 {
Paul Elliott329d5382021-07-22 17:10:45 +01003648 /* Split data into length(data_part_len) parts. */
3649 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003650
Paul Elliott33746aa2021-09-15 16:40:40 +01003651 if( do_set_lengths )
3652 {
3653 if( data_part_len & 0x01 )
3654 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3655 else
3656 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3657 }
3658
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003659 if( !aead_multipart_internal_func( key_type_arg, key_data,
3660 alg_arg, nonce,
3661 additional_data, -1,
3662 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003663 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003664 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003665 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003666 break;
3667
3668 /* length(0) part, length(data_part_len) part, length(0) part... */
3669 mbedtls_test_set_step( 3000 + data_part_len );
3670
3671 if( !aead_multipart_internal_func( key_type_arg, key_data,
3672 alg_arg, nonce,
3673 additional_data, -1,
3674 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003675 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003676 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003677 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003678 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003679 }
3680 }
3681
Paul Elliott8fc45162021-06-23 16:06:01 +01003682 /* Goto is required to silence warnings about unused labels, as we
3683 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003684 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003685}
3686/* END_CASE */
3687
3688/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003689void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3690 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003691 int nonce_length,
3692 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003693 data_t *additional_data,
3694 data_t *input_data,
3695 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003696{
3697
3698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3699 psa_key_type_t key_type = key_type_arg;
3700 psa_algorithm_t alg = alg_arg;
3701 psa_aead_operation_t operation;
3702 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3704 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003705 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003706 size_t actual_nonce_length = 0;
3707 size_t expected_nonce_length = expected_nonce_length_arg;
3708 unsigned char *output = NULL;
3709 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003710 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003711 size_t ciphertext_size = 0;
3712 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003713 size_t tag_length = 0;
3714 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003715
3716 PSA_ASSERT( psa_crypto_init( ) );
3717
3718 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3719 psa_set_key_algorithm( & attributes, alg );
3720 psa_set_key_type( & attributes, key_type );
3721
3722 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3723 &key ) );
3724
3725 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3726
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003727 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3728
Paul Elliottf1277632021-08-24 18:11:37 +01003729 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003730
Paul Elliottf1277632021-08-24 18:11:37 +01003731 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003732
Paul Elliottf1277632021-08-24 18:11:37 +01003733 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003734
Paul Elliottf1277632021-08-24 18:11:37 +01003735 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003736
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003737 operation = psa_aead_operation_init( );
3738
3739 status = psa_aead_encrypt_setup( &operation, key, alg );
3740
3741 /* If the operation is not supported, just skip and not fail in case the
3742 * encryption involves a common limitation of cryptography hardwares and
3743 * an alternative implementation. */
3744 if( status == PSA_ERROR_NOT_SUPPORTED )
3745 {
3746 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003747 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003748 }
3749
3750 PSA_ASSERT( status );
3751
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003752 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003753 nonce_length,
3754 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003755
Paul Elliott693bf312021-07-23 17:40:41 +01003756 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003757
Paul Elliottf1277632021-08-24 18:11:37 +01003758 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003759
Paul Elliottf1277632021-08-24 18:11:37 +01003760 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003761
Paul Elliott693bf312021-07-23 17:40:41 +01003762 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003763 {
3764
3765 /* Ensure we can still complete operation. */
3766
3767 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3768 additional_data->len ) );
3769
3770 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003771 output, output_size,
3772 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003773
Paul Elliottf1277632021-08-24 18:11:37 +01003774 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3775 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003776 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3777 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003778
3779exit:
3780 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003781 mbedtls_free( output );
3782 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003783 psa_aead_abort( &operation );
3784 PSA_DONE( );
3785}
3786/* END_CASE */
3787
3788/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003789void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3790 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003791 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003792 data_t *additional_data,
3793 data_t *input_data,
3794 int expected_status_arg )
3795{
3796
3797 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3798 psa_key_type_t key_type = key_type_arg;
3799 psa_algorithm_t alg = alg_arg;
3800 psa_aead_operation_t operation;
3801 uint8_t *nonce_buffer = NULL;
3802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3803 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3804 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003805 unsigned char *output = NULL;
3806 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003807 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003808 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003809 size_t ciphertext_size = 0;
3810 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003811 size_t tag_length = 0;
3812 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003813 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003814
3815 PSA_ASSERT( psa_crypto_init( ) );
3816
3817 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3818 psa_set_key_algorithm( &attributes, alg );
3819 psa_set_key_type( &attributes, key_type );
3820
3821 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3822 &key ) );
3823
3824 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3825
3826 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3827
Paul Elliott6f0e7202021-08-25 12:57:18 +01003828 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003829
Paul Elliott6f0e7202021-08-25 12:57:18 +01003830 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003831
Paul Elliott6f0e7202021-08-25 12:57:18 +01003832 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003833
Paul Elliott6f0e7202021-08-25 12:57:18 +01003834 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003835
3836 operation = psa_aead_operation_init( );
3837
3838 status = psa_aead_encrypt_setup( &operation, key, alg );
3839
3840 /* If the operation is not supported, just skip and not fail in case the
3841 * encryption involves a common limitation of cryptography hardwares and
3842 * an alternative implementation. */
3843 if( status == PSA_ERROR_NOT_SUPPORTED )
3844 {
3845 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003846 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003847 }
3848
3849 PSA_ASSERT( status );
3850
Paul Elliott4023ffd2021-09-10 16:21:22 +01003851 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3852 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003853 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003854 /* Arbitrary size buffer, to test zero length valid buffer. */
3855 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003856 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003857 }
3858 else
3859 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003860 /* If length is zero, then this will return NULL. */
3861 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003862 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003863
Paul Elliott4023ffd2021-09-10 16:21:22 +01003864 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003865 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003866 for( index = 0; index < nonce_length - 1; ++index )
3867 {
3868 nonce_buffer[index] = 'a' + index;
3869 }
Paul Elliott66696b52021-08-16 18:42:41 +01003870 }
Paul Elliott863864a2021-07-23 17:28:31 +01003871 }
3872
Paul Elliott6f0e7202021-08-25 12:57:18 +01003873 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003874
Paul Elliott693bf312021-07-23 17:40:41 +01003875 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003876
3877 if( expected_status == PSA_SUCCESS )
3878 {
3879 /* Ensure we can still complete operation. */
3880
3881 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3882 additional_data->len ) );
3883
3884 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003885 output, output_size,
3886 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003887
Paul Elliott6f0e7202021-08-25 12:57:18 +01003888 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3889 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003890 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3891 }
3892
3893exit:
3894 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003895 mbedtls_free( output );
3896 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003897 mbedtls_free( nonce_buffer );
3898 psa_aead_abort( &operation );
3899 PSA_DONE( );
3900}
3901/* END_CASE */
3902
3903/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003904void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3905 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003906 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003907 data_t *nonce,
3908 data_t *additional_data,
3909 data_t *input_data,
3910 int expected_status_arg )
3911{
3912
3913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3914 psa_key_type_t key_type = key_type_arg;
3915 psa_algorithm_t alg = alg_arg;
3916 psa_aead_operation_t operation;
3917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3918 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3919 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003920 unsigned char *output = NULL;
3921 unsigned char *ciphertext = NULL;
3922 size_t output_size = output_size_arg;
3923 size_t ciphertext_size = 0;
3924 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003925 size_t tag_length = 0;
3926 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3927
3928 PSA_ASSERT( psa_crypto_init( ) );
3929
3930 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3931 psa_set_key_algorithm( &attributes, alg );
3932 psa_set_key_type( &attributes, key_type );
3933
3934 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3935 &key ) );
3936
3937 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3938
Paul Elliottc6d11d02021-09-01 12:04:23 +01003939 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003940
Paul Elliottc6d11d02021-09-01 12:04:23 +01003941 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003942
Paul Elliottc6d11d02021-09-01 12:04:23 +01003943 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003944
3945 operation = psa_aead_operation_init( );
3946
3947 status = psa_aead_encrypt_setup( &operation, key, alg );
3948
3949 /* If the operation is not supported, just skip and not fail in case the
3950 * encryption involves a common limitation of cryptography hardwares and
3951 * an alternative implementation. */
3952 if( status == PSA_ERROR_NOT_SUPPORTED )
3953 {
3954 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3955 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3956 }
3957
3958 PSA_ASSERT( status );
3959
3960 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3961
3962 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3963 additional_data->len ) );
3964
3965 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003966 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003967
3968 TEST_EQUAL( status, expected_status );
3969
3970 if( expected_status == PSA_SUCCESS )
3971 {
3972 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003973 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3974 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003975 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3976 }
3977
3978exit:
3979 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003980 mbedtls_free( output );
3981 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003982 psa_aead_abort( &operation );
3983 PSA_DONE( );
3984}
3985/* END_CASE */
3986
Paul Elliott91b021e2021-07-23 18:52:31 +01003987/* BEGIN_CASE */
3988void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3989 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003990 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003991 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003992 data_t *nonce,
3993 data_t *additional_data,
3994 data_t *input_data,
3995 int expected_status_arg )
3996{
3997
3998 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3999 psa_key_type_t key_type = key_type_arg;
4000 psa_algorithm_t alg = alg_arg;
4001 psa_aead_operation_t operation;
4002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4003 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4004 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004005 unsigned char *ciphertext = NULL;
4006 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004007 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004008 size_t ciphertext_size = 0;
4009 size_t ciphertext_length = 0;
4010 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004011 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004012 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004013
4014 PSA_ASSERT( psa_crypto_init( ) );
4015
4016 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4017 psa_set_key_algorithm( &attributes, alg );
4018 psa_set_key_type( &attributes, key_type );
4019
4020 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4021 &key ) );
4022
4023 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4024
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004025 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004026
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004027 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004028
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004029 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004030
Paul Elliott719c1322021-09-13 18:27:22 +01004031 ASSERT_ALLOC( tag_buffer, tag_size );
4032
Paul Elliott91b021e2021-07-23 18:52:31 +01004033 operation = psa_aead_operation_init( );
4034
4035 status = psa_aead_encrypt_setup( &operation, key, alg );
4036
4037 /* If the operation is not supported, just skip and not fail in case the
4038 * encryption involves a common limitation of cryptography hardwares and
4039 * an alternative implementation. */
4040 if( status == PSA_ERROR_NOT_SUPPORTED )
4041 {
4042 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4043 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4044 }
4045
4046 PSA_ASSERT( status );
4047
4048 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4049
4050 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4051 additional_data->len ) );
4052
4053 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004054 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004055
4056 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004057 status = psa_aead_finish( &operation, finish_ciphertext,
4058 finish_ciphertext_size,
4059 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004060 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004061
4062 TEST_EQUAL( status, expected_status );
4063
4064exit:
4065 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004066 mbedtls_free( ciphertext );
4067 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004068 psa_aead_abort( &operation );
4069 PSA_DONE( );
4070}
4071/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004072
4073/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004074void aead_multipart_verify( int key_type_arg, data_t *key_data,
4075 int alg_arg,
4076 data_t *nonce,
4077 data_t *additional_data,
4078 data_t *input_data,
4079 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004080 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004081 int expected_status_arg )
4082{
4083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4084 psa_key_type_t key_type = key_type_arg;
4085 psa_algorithm_t alg = alg_arg;
4086 psa_aead_operation_t operation;
4087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4088 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4089 psa_status_t expected_status = expected_status_arg;
4090 unsigned char *plaintext = NULL;
4091 unsigned char *finish_plaintext = NULL;
4092 size_t plaintext_size = 0;
4093 size_t plaintext_length = 0;
4094 size_t verify_plaintext_size = 0;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004095 tagusage_method tag_usage = tag_usage_arg;
4096 unsigned char *tag_buffer = NULL;
4097 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004098
4099 PSA_ASSERT( psa_crypto_init( ) );
4100
4101 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4102 psa_set_key_algorithm( &attributes, alg );
4103 psa_set_key_type( &attributes, key_type );
4104
4105 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4106 &key ) );
4107
4108 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4109
4110 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4111 input_data->len );
4112
4113 ASSERT_ALLOC( plaintext, plaintext_size );
4114
4115 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4116
4117 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4118
4119 operation = psa_aead_operation_init( );
4120
4121 status = psa_aead_decrypt_setup( &operation, key, alg );
4122
4123 /* If the operation is not supported, just skip and not fail in case the
4124 * encryption involves a common limitation of cryptography hardwares and
4125 * an alternative implementation. */
4126 if( status == PSA_ERROR_NOT_SUPPORTED )
4127 {
4128 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4129 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4130 }
4131
4132 PSA_ASSERT( status );
4133
4134 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4135
4136 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4137 additional_data->len ) );
4138
4139 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4140 input_data->len,
4141 plaintext, plaintext_size,
4142 &plaintext_length ) );
4143
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004144 if( tag_usage == USE_GIVEN_TAG )
4145 {
4146 tag_buffer = tag->x;
4147 tag_size = tag->len;
4148 }
4149
Paul Elliott9961a662021-09-17 19:19:02 +01004150 status = psa_aead_verify( &operation, finish_plaintext,
4151 verify_plaintext_size,
4152 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004153 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004154
4155 TEST_EQUAL( status, expected_status );
4156
4157exit:
4158 psa_destroy_key( key );
4159 mbedtls_free( plaintext );
4160 mbedtls_free( finish_plaintext );
4161 psa_aead_abort( &operation );
4162 PSA_DONE( );
4163}
4164/* END_CASE */
4165
Paul Elliott9961a662021-09-17 19:19:02 +01004166/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004167void aead_multipart_setup( int key_type_arg, data_t *key_data,
4168 int alg_arg, int expected_status_arg )
4169{
4170 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4171 psa_key_type_t key_type = key_type_arg;
4172 psa_algorithm_t alg = alg_arg;
4173 psa_aead_operation_t operation;
4174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4175 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4176 psa_status_t expected_status = expected_status_arg;
4177
4178 PSA_ASSERT( psa_crypto_init( ) );
4179
4180 psa_set_key_usage_flags( &attributes,
4181 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4182 psa_set_key_algorithm( &attributes, alg );
4183 psa_set_key_type( &attributes, key_type );
4184
4185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4186 &key ) );
4187
4188 mbedtls_test_set_step( 0 );
4189
4190 status = psa_aead_encrypt_setup( &operation, key, alg );
4191
4192 TEST_EQUAL( status, expected_status );
4193
4194 psa_aead_abort( &operation );
4195
4196 operation = psa_aead_operation_init( );
4197
4198 mbedtls_test_set_step( 1 );
4199
4200 status = psa_aead_decrypt_setup( &operation, key, alg );
4201
4202 TEST_EQUAL(status, expected_status );
4203
4204exit:
4205 psa_destroy_key( key );
4206 psa_aead_abort( &operation );
4207 PSA_DONE( );
4208}
4209/* END_CASE */
4210
4211/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004212void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4213 int alg_arg,
4214 data_t *nonce,
4215 data_t *additional_data,
4216 data_t *input_data )
4217{
4218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4219 psa_key_type_t key_type = key_type_arg;
4220 psa_algorithm_t alg = alg_arg;
4221 psa_aead_operation_t operation;
4222 unsigned char *output_data = NULL;
4223 unsigned char *final_data = NULL;
4224 size_t output_size = 0;
4225 size_t finish_output_size = 0;
4226 size_t output_length = 0;
4227 size_t key_bits = 0;
4228 size_t tag_length = 0;
4229 size_t tag_size = 0;
4230 size_t nonce_length = 0;
4231 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4232 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4233 size_t output_part_length = 0;
4234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4235
4236 PSA_ASSERT( psa_crypto_init( ) );
4237
4238 psa_set_key_usage_flags( & attributes,
4239 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4240 psa_set_key_algorithm( & attributes, alg );
4241 psa_set_key_type( & attributes, key_type );
4242
4243 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4244 &key ) );
4245
4246 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4247 key_bits = psa_get_key_bits( &attributes );
4248
4249 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4250
4251 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4252
4253 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4254
4255 ASSERT_ALLOC( output_data, output_size );
4256
4257 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4258
4259 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4260
4261 ASSERT_ALLOC( final_data, finish_output_size );
4262
4263 /* Test all operations error without calling setup first. */
4264
4265 operation = psa_aead_operation_init( );
4266
4267 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4268 PSA_ERROR_BAD_STATE );
4269
4270 psa_aead_abort( &operation );
4271
4272 operation = psa_aead_operation_init( );
4273
4274 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4275 PSA_AEAD_NONCE_MAX_SIZE,
4276 &nonce_length ),
4277 PSA_ERROR_BAD_STATE );
4278
4279 psa_aead_abort( &operation );
4280
Paul Elliott481be342021-07-16 17:38:47 +01004281 /* ------------------------------------------------------- */
4282
Paul Elliottc23a9a02021-06-21 18:32:46 +01004283 operation = psa_aead_operation_init( );
4284
4285 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4286 input_data->len ),
4287 PSA_ERROR_BAD_STATE );
4288
4289 psa_aead_abort( &operation );
4290
Paul Elliott481be342021-07-16 17:38:47 +01004291 /* ------------------------------------------------------- */
4292
Paul Elliottc23a9a02021-06-21 18:32:46 +01004293 operation = psa_aead_operation_init( );
4294
4295 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4296 additional_data->len ),
4297 PSA_ERROR_BAD_STATE );
4298
4299 psa_aead_abort( &operation );
4300
Paul Elliott481be342021-07-16 17:38:47 +01004301 /* ------------------------------------------------------- */
4302
Paul Elliottc23a9a02021-06-21 18:32:46 +01004303 operation = psa_aead_operation_init( );
4304
4305 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4306 input_data->len, output_data,
4307 output_size, &output_length ),
4308 PSA_ERROR_BAD_STATE );
4309
4310 psa_aead_abort( &operation );
4311
Paul Elliott481be342021-07-16 17:38:47 +01004312 /* ------------------------------------------------------- */
4313
Paul Elliottc23a9a02021-06-21 18:32:46 +01004314 operation = psa_aead_operation_init( );
4315
4316 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4317 finish_output_size,
4318 &output_part_length,
4319 tag_buffer, tag_length,
4320 &tag_size ),
4321 PSA_ERROR_BAD_STATE );
4322
4323 psa_aead_abort( &operation );
4324
Paul Elliott481be342021-07-16 17:38:47 +01004325 /* ------------------------------------------------------- */
4326
Paul Elliottc23a9a02021-06-21 18:32:46 +01004327 operation = psa_aead_operation_init( );
4328
4329 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4330 finish_output_size,
4331 &output_part_length,
4332 tag_buffer,
4333 tag_length ),
4334 PSA_ERROR_BAD_STATE );
4335
4336 psa_aead_abort( &operation );
4337
4338 /* Test for double setups. */
4339
4340 operation = psa_aead_operation_init( );
4341
4342 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4343
4344 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4345 PSA_ERROR_BAD_STATE );
4346
4347 psa_aead_abort( &operation );
4348
Paul Elliott481be342021-07-16 17:38:47 +01004349 /* ------------------------------------------------------- */
4350
Paul Elliottc23a9a02021-06-21 18:32:46 +01004351 operation = psa_aead_operation_init( );
4352
4353 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4354
4355 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4356 PSA_ERROR_BAD_STATE );
4357
4358 psa_aead_abort( &operation );
4359
Paul Elliott374a2be2021-07-16 17:53:40 +01004360 /* ------------------------------------------------------- */
4361
4362 operation = psa_aead_operation_init( );
4363
4364 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4365
4366 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4367 PSA_ERROR_BAD_STATE );
4368
4369 psa_aead_abort( &operation );
4370
4371 /* ------------------------------------------------------- */
4372
4373 operation = psa_aead_operation_init( );
4374
4375 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4376
4377 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4378 PSA_ERROR_BAD_STATE );
4379
4380 psa_aead_abort( &operation );
4381
Paul Elliottc23a9a02021-06-21 18:32:46 +01004382 /* Test for not setting a nonce. */
4383
4384 operation = psa_aead_operation_init( );
4385
4386 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4387
4388 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4389 additional_data->len ),
4390 PSA_ERROR_BAD_STATE );
4391
4392 psa_aead_abort( &operation );
4393
Paul Elliott7f628422021-09-01 12:08:29 +01004394 /* ------------------------------------------------------- */
4395
4396 operation = psa_aead_operation_init( );
4397
4398 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4399
4400 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4401 input_data->len, output_data,
4402 output_size, &output_length ),
4403 PSA_ERROR_BAD_STATE );
4404
4405 psa_aead_abort( &operation );
4406
Paul Elliottc23a9a02021-06-21 18:32:46 +01004407 /* Test for double setting nonce. */
4408
4409 operation = psa_aead_operation_init( );
4410
4411 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4412
4413 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4414
4415 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4416 PSA_ERROR_BAD_STATE );
4417
4418 psa_aead_abort( &operation );
4419
Paul Elliott374a2be2021-07-16 17:53:40 +01004420 /* Test for double generating nonce. */
4421
4422 operation = psa_aead_operation_init( );
4423
4424 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4425
4426 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4427 PSA_AEAD_NONCE_MAX_SIZE,
4428 &nonce_length ) );
4429
4430 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4431 PSA_AEAD_NONCE_MAX_SIZE,
4432 &nonce_length ),
4433 PSA_ERROR_BAD_STATE );
4434
4435
4436 psa_aead_abort( &operation );
4437
4438 /* Test for generate nonce then set and vice versa */
4439
4440 operation = psa_aead_operation_init( );
4441
4442 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4443
4444 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4445 PSA_AEAD_NONCE_MAX_SIZE,
4446 &nonce_length ) );
4447
4448 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4449 PSA_ERROR_BAD_STATE );
4450
4451 psa_aead_abort( &operation );
4452
4453 /* ------------------------------------------------------- */
4454
4455 operation = psa_aead_operation_init( );
4456
4457 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4458
4459 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4460
4461 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4462 PSA_AEAD_NONCE_MAX_SIZE,
4463 &nonce_length ),
4464 PSA_ERROR_BAD_STATE );
4465
4466 psa_aead_abort( &operation );
4467
Paul Elliott7220cae2021-06-22 17:25:57 +01004468 /* Test for generating nonce in decrypt setup. */
4469
4470 operation = psa_aead_operation_init( );
4471
4472 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4473
4474 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4475 PSA_AEAD_NONCE_MAX_SIZE,
4476 &nonce_length ),
4477 PSA_ERROR_BAD_STATE );
4478
4479 psa_aead_abort( &operation );
4480
Paul Elliottc23a9a02021-06-21 18:32:46 +01004481 /* Test for setting lengths twice. */
4482
4483 operation = psa_aead_operation_init( );
4484
4485 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4486
4487 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4488
4489 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4490 input_data->len ) );
4491
4492 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4493 input_data->len ),
4494 PSA_ERROR_BAD_STATE );
4495
4496 psa_aead_abort( &operation );
4497
4498 /* Test for setting lengths after already starting data. */
4499
4500 operation = psa_aead_operation_init( );
4501
4502 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4503
4504 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4505
Paul Elliottf94bd992021-09-19 18:15:59 +01004506 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4507 additional_data->len ) );
4508
4509 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4510 input_data->len ),
4511 PSA_ERROR_BAD_STATE );
4512
4513 psa_aead_abort( &operation );
4514
4515 /* ------------------------------------------------------- */
4516
4517 operation = psa_aead_operation_init( );
4518
4519 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4520
4521 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4522
Paul Elliottc23a9a02021-06-21 18:32:46 +01004523 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4524 input_data->len, output_data,
4525 output_size, &output_length ) );
4526
4527 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4528 input_data->len ),
4529 PSA_ERROR_BAD_STATE );
4530
4531 psa_aead_abort( &operation );
4532
Paul Elliott243080c2021-07-21 19:01:17 +01004533 /* Test for not sending any additional data or data after setting non zero
4534 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004535
4536 operation = psa_aead_operation_init( );
4537
4538 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4539
4540 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4541
4542 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4543 input_data->len ) );
4544
4545 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4546 finish_output_size,
4547 &output_part_length,
4548 tag_buffer, tag_length,
4549 &tag_size ),
4550 PSA_ERROR_INVALID_ARGUMENT );
4551
4552 psa_aead_abort( &operation );
4553
Paul Elliott243080c2021-07-21 19:01:17 +01004554 /* Test for not sending any additional data or data after setting non-zero
4555 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004556
4557 operation = psa_aead_operation_init( );
4558
4559 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4560
4561 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4562
4563 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4564 input_data->len ) );
4565
4566 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4567 finish_output_size,
4568 &output_part_length,
4569 tag_buffer,
4570 tag_length ),
4571 PSA_ERROR_INVALID_ARGUMENT );
4572
4573 psa_aead_abort( &operation );
4574
Paul Elliott243080c2021-07-21 19:01:17 +01004575 /* Test for not sending any additional data after setting a non-zero length
4576 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004577
4578 operation = psa_aead_operation_init( );
4579
4580 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4581
4582 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4583
4584 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4585 input_data->len ) );
4586
4587 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4588 input_data->len, output_data,
4589 output_size, &output_length ),
4590 PSA_ERROR_INVALID_ARGUMENT );
4591
4592 psa_aead_abort( &operation );
4593
Paul Elliottf94bd992021-09-19 18:15:59 +01004594 /* Test for not sending any data after setting a non-zero length for it.*/
4595
4596 operation = psa_aead_operation_init( );
4597
4598 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4599
4600 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4601
4602 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4603 input_data->len ) );
4604
4605 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4606 additional_data->len ) );
4607
4608 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4609 finish_output_size,
4610 &output_part_length,
4611 tag_buffer, tag_length,
4612 &tag_size ),
4613 PSA_ERROR_INVALID_ARGUMENT );
4614
4615 psa_aead_abort( &operation );
4616
Paul Elliottb0450fe2021-09-01 15:06:26 +01004617 /* Test for sending too much additional data after setting lengths. */
4618
4619 operation = psa_aead_operation_init( );
4620
4621 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4622
4623 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4624
4625 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4626
4627
4628 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4629 additional_data->len ),
4630 PSA_ERROR_INVALID_ARGUMENT );
4631
4632 psa_aead_abort( &operation );
4633
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004634 operation = psa_aead_operation_init( );
4635
4636 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4637
4638 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4639
4640 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4641 input_data->len ) );
4642
4643 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4644 additional_data->len ) );
4645
4646 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4647 1 ),
4648 PSA_ERROR_INVALID_ARGUMENT );
4649
4650 psa_aead_abort( &operation );
4651
Paul Elliottb0450fe2021-09-01 15:06:26 +01004652 /* Test for sending too much data after setting lengths. */
4653
4654 operation = psa_aead_operation_init( );
4655
4656 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4657
4658 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4659
4660 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4661
4662 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4663 input_data->len, output_data,
4664 output_size, &output_length ),
4665 PSA_ERROR_INVALID_ARGUMENT );
4666
4667 psa_aead_abort( &operation );
4668
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004669 operation = psa_aead_operation_init( );
4670
4671 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4672
4673 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4674
4675 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4676 input_data->len ) );
4677
4678 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4679 additional_data->len ) );
4680
4681 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4682 input_data->len, output_data,
4683 output_size, &output_length ) );
4684
4685 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4686 1, output_data,
4687 output_size, &output_length ),
4688 PSA_ERROR_INVALID_ARGUMENT );
4689
4690 psa_aead_abort( &operation );
4691
Paul Elliottc23a9a02021-06-21 18:32:46 +01004692 /* Test sending additional data after data. */
4693
4694 operation = psa_aead_operation_init( );
4695
4696 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4697
4698 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4699
4700 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4701 input_data->len, output_data,
4702 output_size, &output_length ) );
4703
4704 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4705 additional_data->len ),
4706 PSA_ERROR_BAD_STATE );
4707
4708 psa_aead_abort( &operation );
4709
Paul Elliott534d0b42021-06-22 19:15:20 +01004710 /* Test calling finish on decryption. */
4711
4712 operation = psa_aead_operation_init( );
4713
4714 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4715
4716 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4717
4718 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4719 finish_output_size,
4720 &output_part_length,
4721 tag_buffer, tag_length,
4722 &tag_size ),
4723 PSA_ERROR_BAD_STATE );
4724
4725 psa_aead_abort( &operation );
4726
4727 /* Test calling verify on encryption. */
4728
4729 operation = psa_aead_operation_init( );
4730
4731 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4732
4733 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4734
4735 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4736 finish_output_size,
4737 &output_part_length,
4738 tag_buffer,
4739 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004740 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004741
4742 psa_aead_abort( &operation );
4743
4744
Paul Elliottc23a9a02021-06-21 18:32:46 +01004745exit:
4746 psa_destroy_key( key );
4747 psa_aead_abort( &operation );
4748 mbedtls_free( output_data );
4749 mbedtls_free( final_data );
4750 PSA_DONE( );
4751}
4752/* END_CASE */
4753
4754/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004755void signature_size( int type_arg,
4756 int bits,
4757 int alg_arg,
4758 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004759{
4760 psa_key_type_t type = type_arg;
4761 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004762 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004763
Gilles Peskinefe11b722018-12-18 00:24:04 +01004764 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004765
Gilles Peskinee59236f2018-01-27 23:32:46 +01004766exit:
4767 ;
4768}
4769/* END_CASE */
4770
4771/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004772void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4773 int alg_arg, data_t *input_data,
4774 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004775{
Ronald Cron5425a212020-08-04 14:58:35 +02004776 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004777 psa_key_type_t key_type = key_type_arg;
4778 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004779 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004780 unsigned char *signature = NULL;
4781 size_t signature_size;
4782 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004783 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004784
Gilles Peskine8817f612018-12-18 00:18:46 +01004785 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004786
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004787 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004788 psa_set_key_algorithm( &attributes, alg );
4789 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004790
Gilles Peskine049c7532019-05-15 20:22:09 +02004791 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004792 &key ) );
4793 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004794 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004795
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004796 /* Allocate a buffer which has the size advertized by the
4797 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004798 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004799 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004800 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004801 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004802 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004803
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004804 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004805 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004806 input_data->x, input_data->len,
4807 signature, signature_size,
4808 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004809 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004810 ASSERT_COMPARE( output_data->x, output_data->len,
4811 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004812
4813exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004814 /*
4815 * Key attributes may have been returned by psa_get_key_attributes()
4816 * thus reset them as required.
4817 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004818 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004819
Ronald Cron5425a212020-08-04 14:58:35 +02004820 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004821 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004822 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004823}
4824/* END_CASE */
4825
4826/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004827void sign_hash_fail( int key_type_arg, data_t *key_data,
4828 int alg_arg, data_t *input_data,
4829 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004830{
Ronald Cron5425a212020-08-04 14:58:35 +02004831 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004832 psa_key_type_t key_type = key_type_arg;
4833 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004834 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004835 psa_status_t actual_status;
4836 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004837 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004838 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004839 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004840
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004841 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004842
Gilles Peskine8817f612018-12-18 00:18:46 +01004843 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004844
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004845 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004846 psa_set_key_algorithm( &attributes, alg );
4847 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004848
Gilles Peskine049c7532019-05-15 20:22:09 +02004849 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004850 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004851
Ronald Cron5425a212020-08-04 14:58:35 +02004852 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004853 input_data->x, input_data->len,
4854 signature, signature_size,
4855 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004856 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004857 /* The value of *signature_length is unspecified on error, but
4858 * whatever it is, it should be less than signature_size, so that
4859 * if the caller tries to read *signature_length bytes without
4860 * checking the error code then they don't overflow a buffer. */
4861 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004862
4863exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004864 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004865 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004866 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004867 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004868}
4869/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004870
4871/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004872void sign_verify_hash( int key_type_arg, data_t *key_data,
4873 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004874{
Ronald Cron5425a212020-08-04 14:58:35 +02004875 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004876 psa_key_type_t key_type = key_type_arg;
4877 psa_algorithm_t alg = alg_arg;
4878 size_t key_bits;
4879 unsigned char *signature = NULL;
4880 size_t signature_size;
4881 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004883
Gilles Peskine8817f612018-12-18 00:18:46 +01004884 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004885
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004886 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004887 psa_set_key_algorithm( &attributes, alg );
4888 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004889
Gilles Peskine049c7532019-05-15 20:22:09 +02004890 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004891 &key ) );
4892 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004893 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004894
4895 /* Allocate a buffer which has the size advertized by the
4896 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004897 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004898 key_bits, alg );
4899 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004900 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004901 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004902
4903 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004904 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004905 input_data->x, input_data->len,
4906 signature, signature_size,
4907 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004908 /* Check that the signature length looks sensible. */
4909 TEST_ASSERT( signature_length <= signature_size );
4910 TEST_ASSERT( signature_length > 0 );
4911
4912 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004913 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004914 input_data->x, input_data->len,
4915 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004916
4917 if( input_data->len != 0 )
4918 {
4919 /* Flip a bit in the input and verify that the signature is now
4920 * detected as invalid. Flip a bit at the beginning, not at the end,
4921 * because ECDSA may ignore the last few bits of the input. */
4922 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004923 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004924 input_data->x, input_data->len,
4925 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004926 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004927 }
4928
4929exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004930 /*
4931 * Key attributes may have been returned by psa_get_key_attributes()
4932 * thus reset them as required.
4933 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004934 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004935
Ronald Cron5425a212020-08-04 14:58:35 +02004936 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004937 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004938 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004939}
4940/* END_CASE */
4941
4942/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004943void verify_hash( int key_type_arg, data_t *key_data,
4944 int alg_arg, data_t *hash_data,
4945 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004946{
Ronald Cron5425a212020-08-04 14:58:35 +02004947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004948 psa_key_type_t key_type = key_type_arg;
4949 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004950 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004951
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004952 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004953
Gilles Peskine8817f612018-12-18 00:18:46 +01004954 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004955
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004956 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004957 psa_set_key_algorithm( &attributes, alg );
4958 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004959
Gilles Peskine049c7532019-05-15 20:22:09 +02004960 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004961 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004962
Ronald Cron5425a212020-08-04 14:58:35 +02004963 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004964 hash_data->x, hash_data->len,
4965 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004966
itayzafrir5c753392018-05-08 11:18:38 +03004967exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004968 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004969 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004970 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004971}
4972/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004973
4974/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004975void verify_hash_fail( int key_type_arg, data_t *key_data,
4976 int alg_arg, data_t *hash_data,
4977 data_t *signature_data,
4978 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004979{
Ronald Cron5425a212020-08-04 14:58:35 +02004980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004981 psa_key_type_t key_type = key_type_arg;
4982 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004983 psa_status_t actual_status;
4984 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004986
Gilles Peskine8817f612018-12-18 00:18:46 +01004987 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004988
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004989 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004990 psa_set_key_algorithm( &attributes, alg );
4991 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004992
Gilles Peskine049c7532019-05-15 20:22:09 +02004993 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004994 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004995
Ronald Cron5425a212020-08-04 14:58:35 +02004996 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004997 hash_data->x, hash_data->len,
4998 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004999 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005000
5001exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005002 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005003 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005004 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005005}
5006/* END_CASE */
5007
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005008/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005009void sign_message_deterministic( int key_type_arg,
5010 data_t *key_data,
5011 int alg_arg,
5012 data_t *input_data,
5013 data_t *output_data )
5014{
5015 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5016 psa_key_type_t key_type = key_type_arg;
5017 psa_algorithm_t alg = alg_arg;
5018 size_t key_bits;
5019 unsigned char *signature = NULL;
5020 size_t signature_size;
5021 size_t signature_length = 0xdeadbeef;
5022 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5023
5024 PSA_ASSERT( psa_crypto_init( ) );
5025
5026 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5027 psa_set_key_algorithm( &attributes, alg );
5028 psa_set_key_type( &attributes, key_type );
5029
5030 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5031 &key ) );
5032 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5033 key_bits = psa_get_key_bits( &attributes );
5034
5035 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5036 TEST_ASSERT( signature_size != 0 );
5037 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5038 ASSERT_ALLOC( signature, signature_size );
5039
5040 PSA_ASSERT( psa_sign_message( key, alg,
5041 input_data->x, input_data->len,
5042 signature, signature_size,
5043 &signature_length ) );
5044
5045 ASSERT_COMPARE( output_data->x, output_data->len,
5046 signature, signature_length );
5047
5048exit:
5049 psa_reset_key_attributes( &attributes );
5050
5051 psa_destroy_key( key );
5052 mbedtls_free( signature );
5053 PSA_DONE( );
5054
5055}
5056/* END_CASE */
5057
5058/* BEGIN_CASE */
5059void sign_message_fail( int key_type_arg,
5060 data_t *key_data,
5061 int alg_arg,
5062 data_t *input_data,
5063 int signature_size_arg,
5064 int expected_status_arg )
5065{
5066 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5067 psa_key_type_t key_type = key_type_arg;
5068 psa_algorithm_t alg = alg_arg;
5069 size_t signature_size = signature_size_arg;
5070 psa_status_t actual_status;
5071 psa_status_t expected_status = expected_status_arg;
5072 unsigned char *signature = NULL;
5073 size_t signature_length = 0xdeadbeef;
5074 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5075
5076 ASSERT_ALLOC( signature, signature_size );
5077
5078 PSA_ASSERT( psa_crypto_init( ) );
5079
5080 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5081 psa_set_key_algorithm( &attributes, alg );
5082 psa_set_key_type( &attributes, key_type );
5083
5084 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5085 &key ) );
5086
5087 actual_status = psa_sign_message( key, alg,
5088 input_data->x, input_data->len,
5089 signature, signature_size,
5090 &signature_length );
5091 TEST_EQUAL( actual_status, expected_status );
5092 /* The value of *signature_length is unspecified on error, but
5093 * whatever it is, it should be less than signature_size, so that
5094 * if the caller tries to read *signature_length bytes without
5095 * checking the error code then they don't overflow a buffer. */
5096 TEST_ASSERT( signature_length <= signature_size );
5097
5098exit:
5099 psa_reset_key_attributes( &attributes );
5100 psa_destroy_key( key );
5101 mbedtls_free( signature );
5102 PSA_DONE( );
5103}
5104/* END_CASE */
5105
5106/* BEGIN_CASE */
5107void sign_verify_message( int key_type_arg,
5108 data_t *key_data,
5109 int alg_arg,
5110 data_t *input_data )
5111{
5112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5113 psa_key_type_t key_type = key_type_arg;
5114 psa_algorithm_t alg = alg_arg;
5115 size_t key_bits;
5116 unsigned char *signature = NULL;
5117 size_t signature_size;
5118 size_t signature_length = 0xdeadbeef;
5119 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5120
5121 PSA_ASSERT( psa_crypto_init( ) );
5122
5123 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5124 PSA_KEY_USAGE_VERIFY_MESSAGE );
5125 psa_set_key_algorithm( &attributes, alg );
5126 psa_set_key_type( &attributes, key_type );
5127
5128 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5129 &key ) );
5130 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5131 key_bits = psa_get_key_bits( &attributes );
5132
5133 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5134 TEST_ASSERT( signature_size != 0 );
5135 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5136 ASSERT_ALLOC( signature, signature_size );
5137
5138 PSA_ASSERT( psa_sign_message( key, alg,
5139 input_data->x, input_data->len,
5140 signature, signature_size,
5141 &signature_length ) );
5142 TEST_ASSERT( signature_length <= signature_size );
5143 TEST_ASSERT( signature_length > 0 );
5144
5145 PSA_ASSERT( psa_verify_message( key, alg,
5146 input_data->x, input_data->len,
5147 signature, signature_length ) );
5148
5149 if( input_data->len != 0 )
5150 {
5151 /* Flip a bit in the input and verify that the signature is now
5152 * detected as invalid. Flip a bit at the beginning, not at the end,
5153 * because ECDSA may ignore the last few bits of the input. */
5154 input_data->x[0] ^= 1;
5155 TEST_EQUAL( psa_verify_message( key, alg,
5156 input_data->x, input_data->len,
5157 signature, signature_length ),
5158 PSA_ERROR_INVALID_SIGNATURE );
5159 }
5160
5161exit:
5162 psa_reset_key_attributes( &attributes );
5163
5164 psa_destroy_key( key );
5165 mbedtls_free( signature );
5166 PSA_DONE( );
5167}
5168/* END_CASE */
5169
5170/* BEGIN_CASE */
5171void verify_message( int key_type_arg,
5172 data_t *key_data,
5173 int alg_arg,
5174 data_t *input_data,
5175 data_t *signature_data )
5176{
5177 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5178 psa_key_type_t key_type = key_type_arg;
5179 psa_algorithm_t alg = alg_arg;
5180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5181
5182 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5183
5184 PSA_ASSERT( psa_crypto_init( ) );
5185
5186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5187 psa_set_key_algorithm( &attributes, alg );
5188 psa_set_key_type( &attributes, key_type );
5189
5190 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5191 &key ) );
5192
5193 PSA_ASSERT( psa_verify_message( key, alg,
5194 input_data->x, input_data->len,
5195 signature_data->x, signature_data->len ) );
5196
5197exit:
5198 psa_reset_key_attributes( &attributes );
5199 psa_destroy_key( key );
5200 PSA_DONE( );
5201}
5202/* END_CASE */
5203
5204/* BEGIN_CASE */
5205void verify_message_fail( int key_type_arg,
5206 data_t *key_data,
5207 int alg_arg,
5208 data_t *hash_data,
5209 data_t *signature_data,
5210 int expected_status_arg )
5211{
5212 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5213 psa_key_type_t key_type = key_type_arg;
5214 psa_algorithm_t alg = alg_arg;
5215 psa_status_t actual_status;
5216 psa_status_t expected_status = expected_status_arg;
5217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5218
5219 PSA_ASSERT( psa_crypto_init( ) );
5220
5221 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5222 psa_set_key_algorithm( &attributes, alg );
5223 psa_set_key_type( &attributes, key_type );
5224
5225 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5226 &key ) );
5227
5228 actual_status = psa_verify_message( key, alg,
5229 hash_data->x, hash_data->len,
5230 signature_data->x,
5231 signature_data->len );
5232 TEST_EQUAL( actual_status, expected_status );
5233
5234exit:
5235 psa_reset_key_attributes( &attributes );
5236 psa_destroy_key( key );
5237 PSA_DONE( );
5238}
5239/* END_CASE */
5240
5241/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005242void asymmetric_encrypt( int key_type_arg,
5243 data_t *key_data,
5244 int alg_arg,
5245 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005246 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005247 int expected_output_length_arg,
5248 int expected_status_arg )
5249{
Ronald Cron5425a212020-08-04 14:58:35 +02005250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005251 psa_key_type_t key_type = key_type_arg;
5252 psa_algorithm_t alg = alg_arg;
5253 size_t expected_output_length = expected_output_length_arg;
5254 size_t key_bits;
5255 unsigned char *output = NULL;
5256 size_t output_size;
5257 size_t output_length = ~0;
5258 psa_status_t actual_status;
5259 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005261
Gilles Peskine8817f612018-12-18 00:18:46 +01005262 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005263
Gilles Peskine656896e2018-06-29 19:12:28 +02005264 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005265 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5266 psa_set_key_algorithm( &attributes, alg );
5267 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005268 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005269 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005270
5271 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005272 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005273 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005274
Gilles Peskine656896e2018-06-29 19:12:28 +02005275 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005276 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005277 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005278
5279 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005280 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005281 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005282 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005283 output, output_size,
5284 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005285 TEST_EQUAL( actual_status, expected_status );
5286 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005287
Gilles Peskine68428122018-06-30 18:42:41 +02005288 /* If the label is empty, the test framework puts a non-null pointer
5289 * in label->x. Test that a null pointer works as well. */
5290 if( label->len == 0 )
5291 {
5292 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005293 if( output_size != 0 )
5294 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005295 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005296 input_data->x, input_data->len,
5297 NULL, label->len,
5298 output, output_size,
5299 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005300 TEST_EQUAL( actual_status, expected_status );
5301 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005302 }
5303
Gilles Peskine656896e2018-06-29 19:12:28 +02005304exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005305 /*
5306 * Key attributes may have been returned by psa_get_key_attributes()
5307 * thus reset them as required.
5308 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005309 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005310
Ronald Cron5425a212020-08-04 14:58:35 +02005311 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005312 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005313 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005314}
5315/* END_CASE */
5316
5317/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005318void asymmetric_encrypt_decrypt( int key_type_arg,
5319 data_t *key_data,
5320 int alg_arg,
5321 data_t *input_data,
5322 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005323{
Ronald Cron5425a212020-08-04 14:58:35 +02005324 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005325 psa_key_type_t key_type = key_type_arg;
5326 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005327 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005328 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005329 size_t output_size;
5330 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005331 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005332 size_t output2_size;
5333 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005335
Gilles Peskine8817f612018-12-18 00:18:46 +01005336 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005337
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005338 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5339 psa_set_key_algorithm( &attributes, alg );
5340 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005341
Gilles Peskine049c7532019-05-15 20:22:09 +02005342 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005343 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005344
5345 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005346 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005347 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005348
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005349 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005350 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005351 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005352
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005353 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005354 TEST_ASSERT( output2_size <=
5355 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5356 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005357 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005358
Gilles Peskineeebd7382018-06-08 18:11:54 +02005359 /* We test encryption by checking that encrypt-then-decrypt gives back
5360 * the original plaintext because of the non-optional random
5361 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005362 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005363 input_data->x, input_data->len,
5364 label->x, label->len,
5365 output, output_size,
5366 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005367 /* We don't know what ciphertext length to expect, but check that
5368 * it looks sensible. */
5369 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005370
Ronald Cron5425a212020-08-04 14:58:35 +02005371 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005372 output, output_length,
5373 label->x, label->len,
5374 output2, output2_size,
5375 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005376 ASSERT_COMPARE( input_data->x, input_data->len,
5377 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005378
5379exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005380 /*
5381 * Key attributes may have been returned by psa_get_key_attributes()
5382 * thus reset them as required.
5383 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005384 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005385
Ronald Cron5425a212020-08-04 14:58:35 +02005386 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005387 mbedtls_free( output );
5388 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005389 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005390}
5391/* END_CASE */
5392
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005393/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005394void asymmetric_decrypt( int key_type_arg,
5395 data_t *key_data,
5396 int alg_arg,
5397 data_t *input_data,
5398 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005399 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005400{
Ronald Cron5425a212020-08-04 14:58:35 +02005401 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005402 psa_key_type_t key_type = key_type_arg;
5403 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005404 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005405 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005406 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005407 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005409
Gilles Peskine8817f612018-12-18 00:18:46 +01005410 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005411
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5413 psa_set_key_algorithm( &attributes, alg );
5414 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005415
Gilles Peskine049c7532019-05-15 20:22:09 +02005416 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005417 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005418
gabor-mezei-armceface22021-01-21 12:26:17 +01005419 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5420 key_bits = psa_get_key_bits( &attributes );
5421
5422 /* Determine the maximum ciphertext length */
5423 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5424 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5425 ASSERT_ALLOC( output, output_size );
5426
Ronald Cron5425a212020-08-04 14:58:35 +02005427 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005428 input_data->x, input_data->len,
5429 label->x, label->len,
5430 output,
5431 output_size,
5432 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005433 ASSERT_COMPARE( expected_data->x, expected_data->len,
5434 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005435
Gilles Peskine68428122018-06-30 18:42:41 +02005436 /* If the label is empty, the test framework puts a non-null pointer
5437 * in label->x. Test that a null pointer works as well. */
5438 if( label->len == 0 )
5439 {
5440 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005441 if( output_size != 0 )
5442 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005443 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005444 input_data->x, input_data->len,
5445 NULL, label->len,
5446 output,
5447 output_size,
5448 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005449 ASSERT_COMPARE( expected_data->x, expected_data->len,
5450 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005451 }
5452
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005453exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005454 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005455 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005456 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005457 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005458}
5459/* END_CASE */
5460
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005461/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005462void asymmetric_decrypt_fail( int key_type_arg,
5463 data_t *key_data,
5464 int alg_arg,
5465 data_t *input_data,
5466 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005467 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005468 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005469{
Ronald Cron5425a212020-08-04 14:58:35 +02005470 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005471 psa_key_type_t key_type = key_type_arg;
5472 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005473 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005474 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005475 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005476 psa_status_t actual_status;
5477 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005479
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005480 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005481
Gilles Peskine8817f612018-12-18 00:18:46 +01005482 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005483
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005484 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5485 psa_set_key_algorithm( &attributes, alg );
5486 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005487
Gilles Peskine049c7532019-05-15 20:22:09 +02005488 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005489 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005490
Ronald Cron5425a212020-08-04 14:58:35 +02005491 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005492 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005493 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005494 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005495 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005496 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005497 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005498
Gilles Peskine68428122018-06-30 18:42:41 +02005499 /* If the label is empty, the test framework puts a non-null pointer
5500 * in label->x. Test that a null pointer works as well. */
5501 if( label->len == 0 )
5502 {
5503 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005504 if( output_size != 0 )
5505 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005506 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005507 input_data->x, input_data->len,
5508 NULL, label->len,
5509 output, output_size,
5510 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005511 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005512 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005513 }
5514
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005515exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005516 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005517 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005518 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005519 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005520}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005521/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005522
5523/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005524void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005525{
5526 /* Test each valid way of initializing the object, except for `= {0}`, as
5527 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5528 * though it's OK by the C standard. We could test for this, but we'd need
5529 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005530 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005531 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5532 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5533 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005534
5535 memset( &zero, 0, sizeof( zero ) );
5536
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005538 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005539 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005540 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005541 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005542 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005543 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005544
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005545 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005546 PSA_ASSERT( psa_key_derivation_abort(&func) );
5547 PSA_ASSERT( psa_key_derivation_abort(&init) );
5548 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005549}
5550/* END_CASE */
5551
Janos Follath16de4a42019-06-13 16:32:24 +01005552/* BEGIN_CASE */
5553void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005554{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005555 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005556 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005557 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005558
Gilles Peskine8817f612018-12-18 00:18:46 +01005559 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005560
Janos Follath16de4a42019-06-13 16:32:24 +01005561 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005562 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005563
5564exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005566 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005567}
5568/* END_CASE */
5569
Janos Follathaf3c2a02019-06-12 12:34:34 +01005570/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005571void derive_set_capacity( int alg_arg, int capacity_arg,
5572 int expected_status_arg )
5573{
5574 psa_algorithm_t alg = alg_arg;
5575 size_t capacity = capacity_arg;
5576 psa_status_t expected_status = expected_status_arg;
5577 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5578
5579 PSA_ASSERT( psa_crypto_init( ) );
5580
5581 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5582
5583 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5584 expected_status );
5585
5586exit:
5587 psa_key_derivation_abort( &operation );
5588 PSA_DONE( );
5589}
5590/* END_CASE */
5591
5592/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005593void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005594 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005595 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005596 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005597 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005598 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005599 int expected_status_arg3,
5600 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005601{
5602 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005603 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5604 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005605 psa_status_t expected_statuses[] = {expected_status_arg1,
5606 expected_status_arg2,
5607 expected_status_arg3};
5608 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005609 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5610 MBEDTLS_SVC_KEY_ID_INIT,
5611 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005612 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5614 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005615 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005616 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005617 psa_status_t expected_output_status = expected_output_status_arg;
5618 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005619
5620 PSA_ASSERT( psa_crypto_init( ) );
5621
5622 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5623 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005624
5625 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5626
5627 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5628 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005629 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005630 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005631 psa_set_key_type( &attributes, key_types[i] );
5632 PSA_ASSERT( psa_import_key( &attributes,
5633 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005634 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005635 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5636 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5637 {
5638 // When taking a private key as secret input, use key agreement
5639 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005640 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5641 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005642 expected_statuses[i] );
5643 }
5644 else
5645 {
5646 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005647 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005648 expected_statuses[i] );
5649 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005650 }
5651 else
5652 {
5653 TEST_EQUAL( psa_key_derivation_input_bytes(
5654 &operation, steps[i],
5655 inputs[i]->x, inputs[i]->len ),
5656 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005657 }
5658 }
5659
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005660 if( output_key_type != PSA_KEY_TYPE_NONE )
5661 {
5662 psa_reset_key_attributes( &attributes );
5663 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5664 psa_set_key_bits( &attributes, 8 );
5665 actual_output_status =
5666 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005667 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005668 }
5669 else
5670 {
5671 uint8_t buffer[1];
5672 actual_output_status =
5673 psa_key_derivation_output_bytes( &operation,
5674 buffer, sizeof( buffer ) );
5675 }
5676 TEST_EQUAL( actual_output_status, expected_output_status );
5677
Janos Follathaf3c2a02019-06-12 12:34:34 +01005678exit:
5679 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005680 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5681 psa_destroy_key( keys[i] );
5682 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005683 PSA_DONE( );
5684}
5685/* END_CASE */
5686
Janos Follathd958bb72019-07-03 15:02:16 +01005687/* BEGIN_CASE */
5688void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005689{
Janos Follathd958bb72019-07-03 15:02:16 +01005690 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005691 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005692 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005693 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005694 unsigned char input1[] = "Input 1";
5695 size_t input1_length = sizeof( input1 );
5696 unsigned char input2[] = "Input 2";
5697 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005698 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005699 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005700 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5701 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5702 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005704
Gilles Peskine8817f612018-12-18 00:18:46 +01005705 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005706
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005707 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5708 psa_set_key_algorithm( &attributes, alg );
5709 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005710
Gilles Peskine73676cb2019-05-15 20:15:10 +02005711 PSA_ASSERT( psa_import_key( &attributes,
5712 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005713 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005714
5715 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005716 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5717 input1, input1_length,
5718 input2, input2_length,
5719 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005720 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005721
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005722 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005723 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005724 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005725
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005726 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005727
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005728 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005729 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005730
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005731exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005732 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005733 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005734 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005735}
5736/* END_CASE */
5737
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005738/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005739void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005740{
5741 uint8_t output_buffer[16];
5742 size_t buffer_size = 16;
5743 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005745
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005746 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5747 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005748 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005749
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005750 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005751 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005752
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005753 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005754
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005755 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5756 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005757 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005758
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005759 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005760 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005761
5762exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005763 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005764}
5765/* END_CASE */
5766
5767/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005768void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005769 int step1_arg, data_t *input1,
5770 int step2_arg, data_t *input2,
5771 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005772 int requested_capacity_arg,
5773 data_t *expected_output1,
5774 data_t *expected_output2 )
5775{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005776 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005777 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5778 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005779 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5780 MBEDTLS_SVC_KEY_ID_INIT,
5781 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005782 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005783 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005784 uint8_t *expected_outputs[2] =
5785 {expected_output1->x, expected_output2->x};
5786 size_t output_sizes[2] =
5787 {expected_output1->len, expected_output2->len};
5788 size_t output_buffer_size = 0;
5789 uint8_t *output_buffer = NULL;
5790 size_t expected_capacity;
5791 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005792 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005793 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005794 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005795
5796 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5797 {
5798 if( output_sizes[i] > output_buffer_size )
5799 output_buffer_size = output_sizes[i];
5800 if( output_sizes[i] == 0 )
5801 expected_outputs[i] = NULL;
5802 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005803 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005804 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005805
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005806 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5807 psa_set_key_algorithm( &attributes, alg );
5808 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005809
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005810 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005811 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5812 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5813 requested_capacity ) );
5814 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005815 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005816 switch( steps[i] )
5817 {
5818 case 0:
5819 break;
5820 case PSA_KEY_DERIVATION_INPUT_SECRET:
5821 PSA_ASSERT( psa_import_key( &attributes,
5822 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005823 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005824
5825 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5826 {
5827 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5828 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5829 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5830 }
5831
Gilles Peskine1468da72019-05-29 17:35:49 +02005832 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005833 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005834 break;
5835 default:
5836 PSA_ASSERT( psa_key_derivation_input_bytes(
5837 &operation, steps[i],
5838 inputs[i]->x, inputs[i]->len ) );
5839 break;
5840 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005841 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005842
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005843 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005844 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005845 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005846 expected_capacity = requested_capacity;
5847
5848 /* Expansion phase. */
5849 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5850 {
5851 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005852 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005853 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005854 if( expected_capacity == 0 && output_sizes[i] == 0 )
5855 {
5856 /* Reading 0 bytes when 0 bytes are available can go either way. */
5857 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005858 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005859 continue;
5860 }
5861 else if( expected_capacity == 0 ||
5862 output_sizes[i] > expected_capacity )
5863 {
5864 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005865 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005866 expected_capacity = 0;
5867 continue;
5868 }
5869 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005870 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005871 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005872 ASSERT_COMPARE( output_buffer, output_sizes[i],
5873 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005874 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005875 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005876 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005877 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005878 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005879 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005880 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005881
5882exit:
5883 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005884 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005885 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5886 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005887 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005888}
5889/* END_CASE */
5890
5891/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005892void derive_full( int alg_arg,
5893 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005894 data_t *input1,
5895 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005896 int requested_capacity_arg )
5897{
Ronald Cron5425a212020-08-04 14:58:35 +02005898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005899 psa_algorithm_t alg = alg_arg;
5900 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005901 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005902 unsigned char output_buffer[16];
5903 size_t expected_capacity = requested_capacity;
5904 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005906
Gilles Peskine8817f612018-12-18 00:18:46 +01005907 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005908
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005909 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5910 psa_set_key_algorithm( &attributes, alg );
5911 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005912
Gilles Peskine049c7532019-05-15 20:22:09 +02005913 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005914 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005915
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005916 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5917 input1->x, input1->len,
5918 input2->x, input2->len,
5919 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005920 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005921
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005922 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005923 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005924 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005925
5926 /* Expansion phase. */
5927 while( current_capacity > 0 )
5928 {
5929 size_t read_size = sizeof( output_buffer );
5930 if( read_size > current_capacity )
5931 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005932 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005933 output_buffer,
5934 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005935 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005936 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005937 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005938 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005939 }
5940
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005941 /* Check that the operation refuses to go over capacity. */
5942 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005943 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005944
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005945 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005946
5947exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005948 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005949 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005950 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005951}
5952/* END_CASE */
5953
Janos Follathe60c9052019-07-03 13:51:30 +01005954/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005955void derive_key_exercise( int alg_arg,
5956 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005957 data_t *input1,
5958 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005959 int derived_type_arg,
5960 int derived_bits_arg,
5961 int derived_usage_arg,
5962 int derived_alg_arg )
5963{
Ronald Cron5425a212020-08-04 14:58:35 +02005964 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5965 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005966 psa_algorithm_t alg = alg_arg;
5967 psa_key_type_t derived_type = derived_type_arg;
5968 size_t derived_bits = derived_bits_arg;
5969 psa_key_usage_t derived_usage = derived_usage_arg;
5970 psa_algorithm_t derived_alg = derived_alg_arg;
5971 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005972 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005973 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005974 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005975
Gilles Peskine8817f612018-12-18 00:18:46 +01005976 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005977
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005978 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5979 psa_set_key_algorithm( &attributes, alg );
5980 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005981 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005982 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005983
5984 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005985 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5986 input1->x, input1->len,
5987 input2->x, input2->len,
5988 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005989 goto exit;
5990
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005991 psa_set_key_usage_flags( &attributes, derived_usage );
5992 psa_set_key_algorithm( &attributes, derived_alg );
5993 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005994 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005995 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005996 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005997
5998 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005999 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006000 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6001 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006002
6003 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006004 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006005 goto exit;
6006
6007exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006008 /*
6009 * Key attributes may have been returned by psa_get_key_attributes()
6010 * thus reset them as required.
6011 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006012 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006013
6014 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006015 psa_destroy_key( base_key );
6016 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006017 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006018}
6019/* END_CASE */
6020
Janos Follath42fd8882019-07-03 14:17:09 +01006021/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006022void derive_key_export( int alg_arg,
6023 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006024 data_t *input1,
6025 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006026 int bytes1_arg,
6027 int bytes2_arg )
6028{
Ronald Cron5425a212020-08-04 14:58:35 +02006029 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6030 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006031 psa_algorithm_t alg = alg_arg;
6032 size_t bytes1 = bytes1_arg;
6033 size_t bytes2 = bytes2_arg;
6034 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006035 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006036 uint8_t *output_buffer = NULL;
6037 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006038 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6039 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006040 size_t length;
6041
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006042 ASSERT_ALLOC( output_buffer, capacity );
6043 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006044 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006045
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006046 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6047 psa_set_key_algorithm( &base_attributes, alg );
6048 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006049 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006050 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006051
6052 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006053 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6054 input1->x, input1->len,
6055 input2->x, input2->len,
6056 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006057 goto exit;
6058
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006059 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006060 output_buffer,
6061 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006062 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006063
6064 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006065 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6066 input1->x, input1->len,
6067 input2->x, input2->len,
6068 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006069 goto exit;
6070
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006071 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6072 psa_set_key_algorithm( &derived_attributes, 0 );
6073 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006074 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006075 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006076 &derived_key ) );
6077 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006078 export_buffer, bytes1,
6079 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006080 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006081 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006082 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006083 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006084 &derived_key ) );
6085 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006086 export_buffer + bytes1, bytes2,
6087 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006088 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006089
6090 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006091 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6092 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006093
6094exit:
6095 mbedtls_free( output_buffer );
6096 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006097 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006098 psa_destroy_key( base_key );
6099 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006100 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006101}
6102/* END_CASE */
6103
6104/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006105void derive_key( int alg_arg,
6106 data_t *key_data, data_t *input1, data_t *input2,
6107 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006108 int expected_status_arg,
6109 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006110{
Ronald Cron5425a212020-08-04 14:58:35 +02006111 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6112 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006113 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006114 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006115 size_t bits = bits_arg;
6116 psa_status_t expected_status = expected_status_arg;
6117 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6118 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6119 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6120
6121 PSA_ASSERT( psa_crypto_init( ) );
6122
6123 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6124 psa_set_key_algorithm( &base_attributes, alg );
6125 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6126 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006127 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006128
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006129 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6130 input1->x, input1->len,
6131 input2->x, input2->len,
6132 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006133 goto exit;
6134
6135 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6136 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006137 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006138 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006139
6140 psa_status_t status =
6141 psa_key_derivation_output_key( &derived_attributes,
6142 &operation,
6143 &derived_key );
6144 if( is_large_output > 0 )
6145 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6146 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006147
6148exit:
6149 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006150 psa_destroy_key( base_key );
6151 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006152 PSA_DONE( );
6153}
6154/* END_CASE */
6155
6156/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006157void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006158 int our_key_type_arg, int our_key_alg_arg,
6159 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006160 int expected_status_arg )
6161{
Ronald Cron5425a212020-08-04 14:58:35 +02006162 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006163 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006164 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006165 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006166 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006168 psa_status_t expected_status = expected_status_arg;
6169 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006170
Gilles Peskine8817f612018-12-18 00:18:46 +01006171 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006172
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006173 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006174 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006175 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006176 PSA_ASSERT( psa_import_key( &attributes,
6177 our_key_data->x, our_key_data->len,
6178 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006179
Gilles Peskine77f40d82019-04-11 21:27:06 +02006180 /* The tests currently include inputs that should fail at either step.
6181 * Test cases that fail at the setup step should be changed to call
6182 * key_derivation_setup instead, and this function should be renamed
6183 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006184 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006185 if( status == PSA_SUCCESS )
6186 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006187 TEST_EQUAL( psa_key_derivation_key_agreement(
6188 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6189 our_key,
6190 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006191 expected_status );
6192 }
6193 else
6194 {
6195 TEST_ASSERT( status == expected_status );
6196 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006197
6198exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006199 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006200 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006201 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006202}
6203/* END_CASE */
6204
6205/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006206void raw_key_agreement( int alg_arg,
6207 int our_key_type_arg, data_t *our_key_data,
6208 data_t *peer_key_data,
6209 data_t *expected_output )
6210{
Ronald Cron5425a212020-08-04 14:58:35 +02006211 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006212 psa_algorithm_t alg = alg_arg;
6213 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006215 unsigned char *output = NULL;
6216 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006217 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006218
6219 ASSERT_ALLOC( output, expected_output->len );
6220 PSA_ASSERT( psa_crypto_init( ) );
6221
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006222 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6223 psa_set_key_algorithm( &attributes, alg );
6224 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006225 PSA_ASSERT( psa_import_key( &attributes,
6226 our_key_data->x, our_key_data->len,
6227 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006228
gabor-mezei-armceface22021-01-21 12:26:17 +01006229 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6230 key_bits = psa_get_key_bits( &attributes );
6231
Gilles Peskinebe697d82019-05-16 18:00:41 +02006232 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6233 peer_key_data->x, peer_key_data->len,
6234 output, expected_output->len,
6235 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006236 ASSERT_COMPARE( output, output_length,
6237 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006238 TEST_ASSERT( output_length <=
6239 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6240 TEST_ASSERT( output_length <=
6241 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006242
6243exit:
6244 mbedtls_free( output );
6245 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006246 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006247}
6248/* END_CASE */
6249
6250/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006251void key_agreement_capacity( int alg_arg,
6252 int our_key_type_arg, data_t *our_key_data,
6253 data_t *peer_key_data,
6254 int expected_capacity_arg )
6255{
Ronald Cron5425a212020-08-04 14:58:35 +02006256 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006257 psa_algorithm_t alg = alg_arg;
6258 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006259 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006261 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006262 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006263
Gilles Peskine8817f612018-12-18 00:18:46 +01006264 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006265
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006266 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6267 psa_set_key_algorithm( &attributes, alg );
6268 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006269 PSA_ASSERT( psa_import_key( &attributes,
6270 our_key_data->x, our_key_data->len,
6271 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006272
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006273 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006274 PSA_ASSERT( psa_key_derivation_key_agreement(
6275 &operation,
6276 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6277 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006278 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6279 {
6280 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006281 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006282 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006283 NULL, 0 ) );
6284 }
Gilles Peskine59685592018-09-18 12:11:34 +02006285
Gilles Peskinebf491972018-10-25 22:36:12 +02006286 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006287 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006288 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006289 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006290
Gilles Peskinebf491972018-10-25 22:36:12 +02006291 /* Test the actual capacity by reading the output. */
6292 while( 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, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006296 actual_capacity -= sizeof( output );
6297 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006298 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006299 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006300 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006301 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006302
Gilles Peskine59685592018-09-18 12:11:34 +02006303exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006304 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006305 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006306 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006307}
6308/* END_CASE */
6309
6310/* BEGIN_CASE */
6311void key_agreement_output( int alg_arg,
6312 int our_key_type_arg, data_t *our_key_data,
6313 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006314 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006315{
Ronald Cron5425a212020-08-04 14:58:35 +02006316 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006317 psa_algorithm_t alg = alg_arg;
6318 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006319 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006320 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006321 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006322
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006323 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6324 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006325
Gilles Peskine8817f612018-12-18 00:18:46 +01006326 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006327
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006328 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6329 psa_set_key_algorithm( &attributes, alg );
6330 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006331 PSA_ASSERT( psa_import_key( &attributes,
6332 our_key_data->x, our_key_data->len,
6333 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006334
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006335 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006336 PSA_ASSERT( psa_key_derivation_key_agreement(
6337 &operation,
6338 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6339 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006340 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6341 {
6342 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006343 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006344 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006345 NULL, 0 ) );
6346 }
Gilles Peskine59685592018-09-18 12:11:34 +02006347
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006348 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006349 actual_output,
6350 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006351 ASSERT_COMPARE( actual_output, expected_output1->len,
6352 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006353 if( expected_output2->len != 0 )
6354 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006355 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006356 actual_output,
6357 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006358 ASSERT_COMPARE( actual_output, expected_output2->len,
6359 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006360 }
Gilles Peskine59685592018-09-18 12:11:34 +02006361
6362exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006363 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006364 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006365 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006366 mbedtls_free( actual_output );
6367}
6368/* END_CASE */
6369
6370/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006371void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006372{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006373 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006374 unsigned char *output = NULL;
6375 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006376 size_t i;
6377 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006378
Simon Butcher49f8e312020-03-03 15:51:50 +00006379 TEST_ASSERT( bytes_arg >= 0 );
6380
Gilles Peskine91892022021-02-08 19:50:26 +01006381 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006382 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006383
Gilles Peskine8817f612018-12-18 00:18:46 +01006384 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006385
Gilles Peskinea50d7392018-06-21 10:22:13 +02006386 /* Run several times, to ensure that every output byte will be
6387 * nonzero at least once with overwhelming probability
6388 * (2^(-8*number_of_runs)). */
6389 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006390 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006391 if( bytes != 0 )
6392 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006393 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006394
Gilles Peskinea50d7392018-06-21 10:22:13 +02006395 for( i = 0; i < bytes; i++ )
6396 {
6397 if( output[i] != 0 )
6398 ++changed[i];
6399 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006400 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006401
6402 /* Check that every byte was changed to nonzero at least once. This
6403 * validates that psa_generate_random is overwriting every byte of
6404 * the output buffer. */
6405 for( i = 0; i < bytes; i++ )
6406 {
6407 TEST_ASSERT( changed[i] != 0 );
6408 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006409
6410exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006411 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006412 mbedtls_free( output );
6413 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006414}
6415/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006416
6417/* BEGIN_CASE */
6418void generate_key( int type_arg,
6419 int bits_arg,
6420 int usage_arg,
6421 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006422 int expected_status_arg,
6423 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006424{
Ronald Cron5425a212020-08-04 14:58:35 +02006425 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006426 psa_key_type_t type = type_arg;
6427 psa_key_usage_t usage = usage_arg;
6428 size_t bits = bits_arg;
6429 psa_algorithm_t alg = alg_arg;
6430 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006432 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006433
Gilles Peskine8817f612018-12-18 00:18:46 +01006434 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006435
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006436 psa_set_key_usage_flags( &attributes, usage );
6437 psa_set_key_algorithm( &attributes, alg );
6438 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006439 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006440
6441 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006442 psa_status_t status = psa_generate_key( &attributes, &key );
6443
6444 if( is_large_key > 0 )
6445 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6446 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006447 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006448 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006449
6450 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006451 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006452 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6453 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006454
Gilles Peskine818ca122018-06-20 18:16:48 +02006455 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006456 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006457 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006458
6459exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006460 /*
6461 * Key attributes may have been returned by psa_get_key_attributes()
6462 * thus reset them as required.
6463 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006464 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006465
Ronald Cron5425a212020-08-04 14:58:35 +02006466 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006467 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006468}
6469/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006470
Ronald Cronee414c72021-03-18 18:50:08 +01006471/* 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 +02006472void generate_key_rsa( int bits_arg,
6473 data_t *e_arg,
6474 int expected_status_arg )
6475{
Ronald Cron5425a212020-08-04 14:58:35 +02006476 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006477 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006478 size_t bits = bits_arg;
6479 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6480 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6481 psa_status_t expected_status = expected_status_arg;
6482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6483 uint8_t *exported = NULL;
6484 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006485 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006486 size_t exported_length = SIZE_MAX;
6487 uint8_t *e_read_buffer = NULL;
6488 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006489 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006490 size_t e_read_length = SIZE_MAX;
6491
6492 if( e_arg->len == 0 ||
6493 ( e_arg->len == 3 &&
6494 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6495 {
6496 is_default_public_exponent = 1;
6497 e_read_size = 0;
6498 }
6499 ASSERT_ALLOC( e_read_buffer, e_read_size );
6500 ASSERT_ALLOC( exported, exported_size );
6501
6502 PSA_ASSERT( psa_crypto_init( ) );
6503
6504 psa_set_key_usage_flags( &attributes, usage );
6505 psa_set_key_algorithm( &attributes, alg );
6506 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6507 e_arg->x, e_arg->len ) );
6508 psa_set_key_bits( &attributes, bits );
6509
6510 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006511 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006512 if( expected_status != PSA_SUCCESS )
6513 goto exit;
6514
6515 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006516 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006517 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6518 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6519 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6520 e_read_buffer, e_read_size,
6521 &e_read_length ) );
6522 if( is_default_public_exponent )
6523 TEST_EQUAL( e_read_length, 0 );
6524 else
6525 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6526
6527 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006528 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006529 goto exit;
6530
6531 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006532 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006533 exported, exported_size,
6534 &exported_length ) );
6535 {
6536 uint8_t *p = exported;
6537 uint8_t *end = exported + exported_length;
6538 size_t len;
6539 /* RSAPublicKey ::= SEQUENCE {
6540 * modulus INTEGER, -- n
6541 * publicExponent INTEGER } -- e
6542 */
6543 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006544 MBEDTLS_ASN1_SEQUENCE |
6545 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006546 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006547 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6548 MBEDTLS_ASN1_INTEGER ) );
6549 if( len >= 1 && p[0] == 0 )
6550 {
6551 ++p;
6552 --len;
6553 }
6554 if( e_arg->len == 0 )
6555 {
6556 TEST_EQUAL( len, 3 );
6557 TEST_EQUAL( p[0], 1 );
6558 TEST_EQUAL( p[1], 0 );
6559 TEST_EQUAL( p[2], 1 );
6560 }
6561 else
6562 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6563 }
6564
6565exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006566 /*
6567 * Key attributes may have been returned by psa_get_key_attributes() or
6568 * set by psa_set_key_domain_parameters() thus reset them as required.
6569 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006570 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006571
Ronald Cron5425a212020-08-04 14:58:35 +02006572 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006573 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006574 mbedtls_free( e_read_buffer );
6575 mbedtls_free( exported );
6576}
6577/* END_CASE */
6578
Darryl Greend49a4992018-06-18 17:27:26 +01006579/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006580void persistent_key_load_key_from_storage( data_t *data,
6581 int type_arg, int bits_arg,
6582 int usage_flags_arg, int alg_arg,
6583 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006584{
Ronald Cron71016a92020-08-28 19:01:50 +02006585 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6588 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006589 psa_key_type_t type = type_arg;
6590 size_t bits = bits_arg;
6591 psa_key_usage_t usage_flags = usage_flags_arg;
6592 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006593 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006594 unsigned char *first_export = NULL;
6595 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006596 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006597 size_t first_exported_length;
6598 size_t second_exported_length;
6599
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006600 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6601 {
6602 ASSERT_ALLOC( first_export, export_size );
6603 ASSERT_ALLOC( second_export, export_size );
6604 }
Darryl Greend49a4992018-06-18 17:27:26 +01006605
Gilles Peskine8817f612018-12-18 00:18:46 +01006606 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006607
Gilles Peskinec87af662019-05-15 16:12:22 +02006608 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006609 psa_set_key_usage_flags( &attributes, usage_flags );
6610 psa_set_key_algorithm( &attributes, alg );
6611 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006612 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006613
Darryl Green0c6575a2018-11-07 16:05:30 +00006614 switch( generation_method )
6615 {
6616 case IMPORT_KEY:
6617 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006618 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006619 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006620 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006621
Darryl Green0c6575a2018-11-07 16:05:30 +00006622 case GENERATE_KEY:
6623 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006624 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006625 break;
6626
6627 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006628#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006629 {
6630 /* Create base key */
6631 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6632 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6633 psa_set_key_usage_flags( &base_attributes,
6634 PSA_KEY_USAGE_DERIVE );
6635 psa_set_key_algorithm( &base_attributes, derive_alg );
6636 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006637 PSA_ASSERT( psa_import_key( &base_attributes,
6638 data->x, data->len,
6639 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006640 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006641 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006642 PSA_ASSERT( psa_key_derivation_input_key(
6643 &operation,
6644 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006645 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006646 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006647 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006648 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6649 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006650 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006651 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006652 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006653 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006654 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006655#else
6656 TEST_ASSUME( ! "KDF not supported in this configuration" );
6657#endif
6658 break;
6659
6660 default:
6661 TEST_ASSERT( ! "generation_method not implemented in test" );
6662 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006663 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006664 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006665
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006666 /* Export the key if permitted by the key policy. */
6667 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6668 {
Ronald Cron5425a212020-08-04 14:58:35 +02006669 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006670 first_export, export_size,
6671 &first_exported_length ) );
6672 if( generation_method == IMPORT_KEY )
6673 ASSERT_COMPARE( data->x, data->len,
6674 first_export, first_exported_length );
6675 }
Darryl Greend49a4992018-06-18 17:27:26 +01006676
6677 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006678 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006679 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006680 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006681
Darryl Greend49a4992018-06-18 17:27:26 +01006682 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006683 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006684 TEST_ASSERT( mbedtls_svc_key_id_equal(
6685 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006686 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6687 PSA_KEY_LIFETIME_PERSISTENT );
6688 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6689 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6690 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6691 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006692
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006693 /* Export the key again if permitted by the key policy. */
6694 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006695 {
Ronald Cron5425a212020-08-04 14:58:35 +02006696 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006697 second_export, export_size,
6698 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006699 ASSERT_COMPARE( first_export, first_exported_length,
6700 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006701 }
6702
6703 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006704 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006705 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006706
6707exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006708 /*
6709 * Key attributes may have been returned by psa_get_key_attributes()
6710 * thus reset them as required.
6711 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006712 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006713
Darryl Greend49a4992018-06-18 17:27:26 +01006714 mbedtls_free( first_export );
6715 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006716 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006717 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006718 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006719 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006720}
6721/* END_CASE */