blob: e9ca8d268a11466a4873b946ffa8c82ba9bf2cfa [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Paul Elliott33746aa2021-09-15 16:40:40 +0100267typedef enum
268{
269 DO_NOT_SET_LENGTHS = 0,
270 SET_LENGTHS_BEFORE_NONCE = 1,
271 SET_LENGTHS_AFTER_NONCE = 2
272} setlengths_method;
273
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100274typedef enum
275{
276 USE_NULL_TAG = 0,
277 USE_GIVEN_TAG = 1,
278} tagusage_method;
279
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100280/*!
281 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100282 * \param key_type_arg Type of key passed in
283 * \param key_data The encryption / decryption key data
284 * \param alg_arg The type of algorithm used
285 * \param nonce Nonce data
286 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100287 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100288 * feed additional data in to be encrypted /
289 * decrypted. If -1, no chunking.
290 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100291 * \param data_part_len_arg If not -1, the length of chunks to feed
292 * the data in to be encrypted / decrypted. If
293 * -1, no chunking
294 * \param set_lengths_method A member of the setlengths_method enum is
295 * expected here, this controls whether or not
296 * to set lengths, and in what order with
297 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100298 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100299 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100300 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100301 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100302 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 */
304static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
305 int alg_arg,
306 data_t *nonce,
307 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100308 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100309 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100310 int data_part_len_arg,
Paul Elliott33746aa2021-09-15 16:40:40 +0100311 setlengths_method set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100312 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100313 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100314 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100315{
316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
317 psa_key_type_t key_type = key_type_arg;
318 psa_algorithm_t alg = alg_arg;
319 psa_aead_operation_t operation;
320 unsigned char *output_data = NULL;
321 unsigned char *part_data = NULL;
322 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100324 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 size_t output_size = 0;
326 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100327 size_t output_length = 0;
328 size_t key_bits = 0;
329 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100330 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100331 size_t part_length = 0;
332 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100333 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100334 size_t ad_part_len = 0;
335 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
338 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
339
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100341 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342
Paul Elliottd3f82412021-06-16 16:52:21 +0100343 PSA_ASSERT( psa_crypto_init( ) );
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 if( is_encrypt )
346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
347 else
348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
349
Paul Elliottd3f82412021-06-16 16:52:21 +0100350 psa_set_key_algorithm( &attributes, alg );
351 psa_set_key_type( &attributes, key_type );
352
353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
354 &key ) );
355
356 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
357 key_bits = psa_get_key_bits( &attributes );
358
359 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 if( is_encrypt )
362 {
363 /* Tag gets written at end of buffer. */
364 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
365 ( input_data->len +
366 tag_length ) );
367 data_true_size = input_data->len;
368 }
369 else
370 {
371 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
372 ( input_data->len -
373 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 /* Do not want to attempt to decrypt tag. */
376 data_true_size = input_data->len - tag_length;
377 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100378
379 ASSERT_ALLOC( output_data, output_size );
380
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 if( is_encrypt )
382 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100383 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
384 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385 }
386 else
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100391
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 operation = psa_aead_operation_init( );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396
397 if( is_encrypt )
398 status = psa_aead_encrypt_setup( &operation, key, alg );
399 else
400 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100401
402 /* If the operation is not supported, just skip and not fail in case the
403 * encryption involves a common limitation of cryptography hardwares and
404 * an alternative implementation. */
405 if( status == PSA_ERROR_NOT_SUPPORTED )
406 {
407 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
408 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
409 }
410
411 PSA_ASSERT( status );
412
Paul Elliott33746aa2021-09-15 16:40:40 +0100413 if( set_lengths_method == DO_NOT_SET_LENGTHS )
414 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
415 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100416 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100417 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
418 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100421 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100422 {
423 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
426 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100427 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100428
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100429 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100430 {
431 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100432 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100434 for( part_offset = 0, part_count = 0;
435 part_offset < additional_data->len;
436 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100437 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100438 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 {
Paul Elliott329d5382021-07-22 17:10:45 +0100440 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100441 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100442 else if( additional_data->len - part_offset < ad_part_len )
443 {
444 part_length = additional_data->len - part_offset;
445 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 else
447 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100448 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 }
450
451 PSA_ASSERT( psa_aead_update_ad( &operation,
452 additional_data->x + part_offset,
453 part_length ) );
454
Paul Elliottd3f82412021-06-16 16:52:21 +0100455 }
456 }
457 else
458 {
459 /* Pass additional data in one go. */
460 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
461 additional_data->len ) );
462 }
463
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100464 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 {
466 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100467 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100468 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100469 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100470
471 ASSERT_ALLOC( part_data, part_data_size );
472
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100473 for( part_offset = 0, part_count = 0;
474 part_offset < data_true_size;
475 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100477 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 {
Paul Elliott329d5382021-07-22 17:10:45 +0100479 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100481 else if( ( data_true_size - part_offset ) < data_part_len )
482 {
483 part_length = ( data_true_size - part_offset );
484 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 else
486 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100487 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 }
489
490 PSA_ASSERT( psa_aead_update( &operation,
491 ( input_data->x + part_offset ),
492 part_length, part_data,
493 part_data_size,
494 &output_part_length ) );
495
496 if( output_data && output_part_length )
497 {
498 memcpy( ( output_data + part_offset ), part_data,
499 output_part_length );
500 }
501
Paul Elliottd3f82412021-06-16 16:52:21 +0100502 output_length += output_part_length;
503 }
504 }
505 else
506 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100507 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100508 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100509 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100510 output_size, &output_length ) );
511 }
512
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100513 if( is_encrypt )
514 PSA_ASSERT( psa_aead_finish( &operation, final_data,
515 final_output_size,
516 &output_part_length,
517 tag_buffer, tag_length,
518 &tag_size ) );
519 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 {
Paul Elliott9961a662021-09-17 19:19:02 +0100521 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100522 final_output_size,
523 &output_part_length,
524 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100525 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100526 }
527
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100528 if( output_data && output_part_length )
529 memcpy( ( output_data + output_length ), final_data,
530 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100531
532 output_length += output_part_length;
533
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534
535 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
536 * should be exact.*/
537 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100539 TEST_EQUAL( tag_length, tag_size );
540
541 if( output_data && tag_length )
542 memcpy( ( output_data + output_length ), tag_buffer,
543 tag_length );
544
545 output_length += tag_length;
546
547 TEST_EQUAL( output_length,
548 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
549 input_data->len ) );
550 TEST_ASSERT( output_length <=
551 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
552 }
553 else
554 {
555 TEST_EQUAL( output_length,
556 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
557 input_data->len ) );
558 TEST_ASSERT( output_length <=
559 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100560 }
561
Paul Elliottd3f82412021-06-16 16:52:21 +0100562
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100563 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100564 output_data, output_length );
565
Paul Elliottd3f82412021-06-16 16:52:21 +0100566
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100567 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100568
569exit:
570 psa_destroy_key( key );
571 psa_aead_abort( &operation );
572 mbedtls_free( output_data );
573 mbedtls_free( part_data );
574 mbedtls_free( final_data );
575 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100576
577 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100578}
579
Gilles Peskinee59236f2018-01-27 23:32:46 +0100580/* END_HEADER */
581
582/* BEGIN_DEPENDENCIES
583 * depends_on:MBEDTLS_PSA_CRYPTO_C
584 * END_DEPENDENCIES
585 */
586
587/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200588void static_checks( )
589{
590 size_t max_truncated_mac_size =
591 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
592
593 /* Check that the length for a truncated MAC always fits in the algorithm
594 * encoding. The shifted mask is the maximum truncated value. The
595 * untruncated algorithm may be one byte larger. */
596 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
597}
598/* END_CASE */
599
600/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200601void import_with_policy( int type_arg,
602 int usage_arg, int alg_arg,
603 int expected_status_arg )
604{
605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
606 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200608 psa_key_type_t type = type_arg;
609 psa_key_usage_t usage = usage_arg;
610 psa_algorithm_t alg = alg_arg;
611 psa_status_t expected_status = expected_status_arg;
612 const uint8_t key_material[16] = {0};
613 psa_status_t status;
614
615 PSA_ASSERT( psa_crypto_init( ) );
616
617 psa_set_key_type( &attributes, type );
618 psa_set_key_usage_flags( &attributes, usage );
619 psa_set_key_algorithm( &attributes, alg );
620
621 status = psa_import_key( &attributes,
622 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200623 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200624 TEST_EQUAL( status, expected_status );
625 if( status != PSA_SUCCESS )
626 goto exit;
627
Ronald Cron5425a212020-08-04 14:58:35 +0200628 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200629 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
630 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
631 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200632 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200633
Ronald Cron5425a212020-08-04 14:58:35 +0200634 PSA_ASSERT( psa_destroy_key( key ) );
635 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636
637exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100638 /*
639 * Key attributes may have been returned by psa_get_key_attributes()
640 * thus reset them as required.
641 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200642 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100643
644 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200645 PSA_DONE( );
646}
647/* END_CASE */
648
649/* BEGIN_CASE */
650void import_with_data( data_t *data, int type_arg,
651 int attr_bits_arg,
652 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200653{
654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
655 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200657 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200658 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200659 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661
Gilles Peskine8817f612018-12-18 00:18:46 +0100662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663
Gilles Peskine4747d192019-04-17 15:05:45 +0200664 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200665 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200666
Ronald Cron5425a212020-08-04 14:58:35 +0200667 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100668 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200669 if( status != PSA_SUCCESS )
670 goto exit;
671
Ronald Cron5425a212020-08-04 14:58:35 +0200672 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200673 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200674 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200675 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200676 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200677
Ronald Cron5425a212020-08-04 14:58:35 +0200678 PSA_ASSERT( psa_destroy_key( key ) );
679 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680
681exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100682 /*
683 * Key attributes may have been returned by psa_get_key_attributes()
684 * thus reset them as required.
685 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200686 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100687
688 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200689 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690}
691/* END_CASE */
692
693/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200694void import_large_key( int type_arg, int byte_size_arg,
695 int expected_status_arg )
696{
697 psa_key_type_t type = type_arg;
698 size_t byte_size = byte_size_arg;
699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
700 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200701 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200702 psa_status_t status;
703 uint8_t *buffer = NULL;
704 size_t buffer_size = byte_size + 1;
705 size_t n;
706
Steven Cooreman69967ce2021-01-18 18:01:08 +0100707 /* Skip the test case if the target running the test cannot
708 * accomodate large keys due to heap size constraints */
709 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200710 memset( buffer, 'K', byte_size );
711
712 PSA_ASSERT( psa_crypto_init( ) );
713
714 /* Try importing the key */
715 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
716 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200717 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100718 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200719 TEST_EQUAL( status, expected_status );
720
721 if( status == PSA_SUCCESS )
722 {
Ronald Cron5425a212020-08-04 14:58:35 +0200723 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200724 TEST_EQUAL( psa_get_key_type( &attributes ), type );
725 TEST_EQUAL( psa_get_key_bits( &attributes ),
726 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200727 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200728 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200729 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200730 for( n = 0; n < byte_size; n++ )
731 TEST_EQUAL( buffer[n], 'K' );
732 for( n = byte_size; n < buffer_size; n++ )
733 TEST_EQUAL( buffer[n], 0 );
734 }
735
736exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100737 /*
738 * Key attributes may have been returned by psa_get_key_attributes()
739 * thus reset them as required.
740 */
741 psa_reset_key_attributes( &attributes );
742
Ronald Cron5425a212020-08-04 14:58:35 +0200743 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200744 PSA_DONE( );
745 mbedtls_free( buffer );
746}
747/* END_CASE */
748
749/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200750void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
751{
Ronald Cron5425a212020-08-04 14:58:35 +0200752 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200753 size_t bits = bits_arg;
754 psa_status_t expected_status = expected_status_arg;
755 psa_status_t status;
756 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200757 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200758 size_t buffer_size = /* Slight overapproximations */
759 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200760 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200761 unsigned char *p;
762 int ret;
763 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200765
Gilles Peskine8817f612018-12-18 00:18:46 +0100766 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200767 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200768
769 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
770 bits, keypair ) ) >= 0 );
771 length = ret;
772
773 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200774 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200775 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100776 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200777
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200779 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200780
781exit:
782 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200783 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200784}
785/* END_CASE */
786
787/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300788void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300789 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200790 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100791 int expected_bits,
792 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200793 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100794 int canonical_input )
795{
Ronald Cron5425a212020-08-04 14:58:35 +0200796 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200798 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200799 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801 unsigned char *exported = NULL;
802 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100804 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200807 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808
Moran Pekercb088e72018-07-17 17:36:59 +0300809 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200810 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200812 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814
Gilles Peskine4747d192019-04-17 15:05:45 +0200815 psa_set_key_usage_flags( &attributes, usage_arg );
816 psa_set_key_algorithm( &attributes, alg );
817 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700818
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200820 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821
822 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200823 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200824 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
825 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200826 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100827
828 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200829 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100830 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100831
832 /* The exported length must be set by psa_export_key() to a value between 0
833 * and export_size. On errors, the exported length must be 0. */
834 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
835 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
836 TEST_ASSERT( exported_length <= export_size );
837
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200838 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200839 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200841 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100842 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200844 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845
Gilles Peskineea38a922021-02-13 00:05:16 +0100846 /* Run sanity checks on the exported key. For non-canonical inputs,
847 * this validates the canonical representations. For canonical inputs,
848 * this doesn't directly validate the implementation, but it still helps
849 * by cross-validating the test data with the sanity check code. */
850 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200851 goto exit;
852
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100853 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200854 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 else
856 {
Ronald Cron5425a212020-08-04 14:58:35 +0200857 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200858 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200859 &key2 ) );
860 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100861 reexported,
862 export_size,
863 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200864 ASSERT_COMPARE( exported, exported_length,
865 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200866 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100868 TEST_ASSERT( exported_length <=
869 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
870 psa_get_key_bits( &got_attributes ) ) );
871 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872
873destroy:
874 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200875 PSA_ASSERT( psa_destroy_key( key ) );
876 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877
878exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100879 /*
880 * Key attributes may have been returned by psa_get_key_attributes()
881 * thus reset them as required.
882 */
883 psa_reset_key_attributes( &got_attributes );
884
itayzafrir3e02b3b2018-06-12 17:06:52 +0300885 mbedtls_free( exported );
886 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200887 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888}
889/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100890
Moran Pekerf709f4a2018-06-06 17:26:04 +0300891/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300892void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200893 int type_arg,
894 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100895 int export_size_delta,
896 int expected_export_status_arg,
897 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300898{
Ronald Cron5425a212020-08-04 14:58:35 +0200899 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300900 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200901 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200902 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300903 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300904 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100905 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100906 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300908
Gilles Peskine8817f612018-12-18 00:18:46 +0100909 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910
Gilles Peskine4747d192019-04-17 15:05:45 +0200911 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
912 psa_set_key_algorithm( &attributes, alg );
913 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300914
915 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200916 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300917
Gilles Peskine49c25912018-10-29 15:15:31 +0100918 /* Export the public key */
919 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200920 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200921 exported, export_size,
922 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100923 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100924 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100925 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200926 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100927 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200928 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200929 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100930 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100931 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100932 TEST_ASSERT( expected_public_key->len <=
933 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
934 TEST_ASSERT( expected_public_key->len <=
935 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100936 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
937 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100938 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300939
940exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100941 /*
942 * Key attributes may have been returned by psa_get_key_attributes()
943 * thus reset them as required.
944 */
945 psa_reset_key_attributes( &attributes );
946
itayzafrir3e02b3b2018-06-12 17:06:52 +0300947 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200948 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200949 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300950}
951/* END_CASE */
952
Gilles Peskine20035e32018-02-03 22:44:14 +0100953/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200954void import_and_exercise_key( data_t *data,
955 int type_arg,
956 int bits_arg,
957 int alg_arg )
958{
Ronald Cron5425a212020-08-04 14:58:35 +0200959 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200960 psa_key_type_t type = type_arg;
961 size_t bits = bits_arg;
962 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100963 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200965 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200966
Gilles Peskine8817f612018-12-18 00:18:46 +0100967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200968
Gilles Peskine4747d192019-04-17 15:05:45 +0200969 psa_set_key_usage_flags( &attributes, usage );
970 psa_set_key_algorithm( &attributes, alg );
971 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200972
973 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200974 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975
976 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200977 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200978 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
979 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980
981 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100982 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200983 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200984
Ronald Cron5425a212020-08-04 14:58:35 +0200985 PSA_ASSERT( psa_destroy_key( key ) );
986 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200987
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200988exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100989 /*
990 * Key attributes may have been returned by psa_get_key_attributes()
991 * thus reset them as required.
992 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200993 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100994
995 psa_reset_key_attributes( &attributes );
996 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200997 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200998}
999/* END_CASE */
1000
1001/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001002void effective_key_attributes( int type_arg, int expected_type_arg,
1003 int bits_arg, int expected_bits_arg,
1004 int usage_arg, int expected_usage_arg,
1005 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001006{
Ronald Cron5425a212020-08-04 14:58:35 +02001007 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001008 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001009 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001010 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001011 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001012 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001013 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001014 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001015 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001017
Gilles Peskine8817f612018-12-18 00:18:46 +01001018 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001019
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001020 psa_set_key_usage_flags( &attributes, usage );
1021 psa_set_key_algorithm( &attributes, alg );
1022 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001023 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001024
Ronald Cron5425a212020-08-04 14:58:35 +02001025 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001026 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001027
Ronald Cron5425a212020-08-04 14:58:35 +02001028 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001029 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1030 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1031 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1032 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001033
1034exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001035 /*
1036 * Key attributes may have been returned by psa_get_key_attributes()
1037 * thus reset them as required.
1038 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001039 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001040
1041 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001042 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001043}
1044/* END_CASE */
1045
1046/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001047void check_key_policy( int type_arg, int bits_arg,
1048 int usage_arg, int alg_arg )
1049{
1050 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1051 usage_arg, usage_arg, alg_arg, alg_arg );
1052 goto exit;
1053}
1054/* END_CASE */
1055
1056/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001057void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001058{
1059 /* Test each valid way of initializing the object, except for `= {0}`, as
1060 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1061 * though it's OK by the C standard. We could test for this, but we'd need
1062 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001063 psa_key_attributes_t func = psa_key_attributes_init( );
1064 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1065 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001066
1067 memset( &zero, 0, sizeof( zero ) );
1068
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001069 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1070 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1071 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001072
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001073 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1074 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1075 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1076
1077 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1078 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1079 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1080
1081 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1082 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1083 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1084
1085 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1086 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1087 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001088}
1089/* END_CASE */
1090
1091/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001092void mac_key_policy( int policy_usage,
1093 int policy_alg,
1094 int key_type,
1095 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001096 int exercise_alg,
1097 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001098{
Ronald Cron5425a212020-08-04 14:58:35 +02001099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001101 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001102 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001103 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001104 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001105
Gilles Peskine8817f612018-12-18 00:18:46 +01001106 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001107
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001108 psa_set_key_usage_flags( &attributes, policy_usage );
1109 psa_set_key_algorithm( &attributes, policy_alg );
1110 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001111
Gilles Peskine049c7532019-05-15 20:22:09 +02001112 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001113 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001114
Ronald Cron5425a212020-08-04 14:58:35 +02001115 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001116 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001117 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001118 else
1119 TEST_EQUAL( status, expected_status );
1120
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001121 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001122
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001123 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001124 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001125 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001126 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001127 else
1128 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001129
1130exit:
1131 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001132 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001133 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001134}
1135/* END_CASE */
1136
1137/* BEGIN_CASE */
1138void cipher_key_policy( int policy_usage,
1139 int policy_alg,
1140 int key_type,
1141 data_t *key_data,
1142 int exercise_alg )
1143{
Ronald Cron5425a212020-08-04 14:58:35 +02001144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001146 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001147 psa_status_t status;
1148
Gilles Peskine8817f612018-12-18 00:18:46 +01001149 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001150
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001151 psa_set_key_usage_flags( &attributes, policy_usage );
1152 psa_set_key_algorithm( &attributes, policy_alg );
1153 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001154
Gilles Peskine049c7532019-05-15 20:22:09 +02001155 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001156 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001157
Ronald Cron5425a212020-08-04 14:58:35 +02001158 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159 if( policy_alg == exercise_alg &&
1160 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001162 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001163 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164 psa_cipher_abort( &operation );
1165
Ronald Cron5425a212020-08-04 14:58:35 +02001166 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 if( policy_alg == exercise_alg &&
1168 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001169 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001170 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001171 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
1173exit:
1174 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001176 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177}
1178/* END_CASE */
1179
1180/* BEGIN_CASE */
1181void aead_key_policy( int policy_usage,
1182 int policy_alg,
1183 int key_type,
1184 data_t *key_data,
1185 int nonce_length_arg,
1186 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001187 int exercise_alg,
1188 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001189{
Ronald Cron5425a212020-08-04 14:58:35 +02001190 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001192 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001193 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194 unsigned char nonce[16] = {0};
1195 size_t nonce_length = nonce_length_arg;
1196 unsigned char tag[16];
1197 size_t tag_length = tag_length_arg;
1198 size_t output_length;
1199
1200 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1201 TEST_ASSERT( tag_length <= sizeof( tag ) );
1202
Gilles Peskine8817f612018-12-18 00:18:46 +01001203 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001205 psa_set_key_usage_flags( &attributes, policy_usage );
1206 psa_set_key_algorithm( &attributes, policy_alg );
1207 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208
Gilles Peskine049c7532019-05-15 20:22:09 +02001209 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001210 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211
Ronald Cron5425a212020-08-04 14:58:35 +02001212 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213 nonce, nonce_length,
1214 NULL, 0,
1215 NULL, 0,
1216 tag, tag_length,
1217 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001218 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1219 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001221 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222
1223 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001224 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225 nonce, nonce_length,
1226 NULL, 0,
1227 tag, tag_length,
1228 NULL, 0,
1229 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001230 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1231 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1232 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001233 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001234 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001235 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001236
1237exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001239 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
1244void asymmetric_encryption_key_policy( int policy_usage,
1245 int policy_alg,
1246 int key_type,
1247 data_t *key_data,
1248 int exercise_alg )
1249{
Ronald Cron5425a212020-08-04 14:58:35 +02001250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252 psa_status_t status;
1253 size_t key_bits;
1254 size_t buffer_length;
1255 unsigned char *buffer = NULL;
1256 size_t output_length;
1257
Gilles Peskine8817f612018-12-18 00:18:46 +01001258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001260 psa_set_key_usage_flags( &attributes, policy_usage );
1261 psa_set_key_algorithm( &attributes, policy_alg );
1262 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001263
Gilles Peskine049c7532019-05-15 20:22:09 +02001264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001265 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266
Ronald Cron5425a212020-08-04 14:58:35 +02001267 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001268 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001269 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1270 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001271 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001272
Ronald Cron5425a212020-08-04 14:58:35 +02001273 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001274 NULL, 0,
1275 NULL, 0,
1276 buffer, buffer_length,
1277 &output_length );
1278 if( policy_alg == exercise_alg &&
1279 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001280 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001281 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001282 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001283
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001284 if( buffer_length != 0 )
1285 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001286 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 buffer, buffer_length,
1288 NULL, 0,
1289 buffer, buffer_length,
1290 &output_length );
1291 if( policy_alg == exercise_alg &&
1292 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001293 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001294 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001295 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296
1297exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001298 /*
1299 * Key attributes may have been returned by psa_get_key_attributes()
1300 * thus reset them as required.
1301 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001302 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001303
1304 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001305 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001306 mbedtls_free( buffer );
1307}
1308/* END_CASE */
1309
1310/* BEGIN_CASE */
1311void asymmetric_signature_key_policy( int policy_usage,
1312 int policy_alg,
1313 int key_type,
1314 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001315 int exercise_alg,
1316 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317{
Ronald Cron5425a212020-08-04 14:58:35 +02001318 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001321 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1322 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1323 * compatible with the policy and `payload_length_arg` is supposed to be
1324 * a valid input length to sign. If `payload_length_arg <= 0`,
1325 * `exercise_alg` is supposed to be forbidden by the policy. */
1326 int compatible_alg = payload_length_arg > 0;
1327 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001328 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001329 size_t signature_length;
1330
Gilles Peskine8817f612018-12-18 00:18:46 +01001331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001333 psa_set_key_usage_flags( &attributes, policy_usage );
1334 psa_set_key_algorithm( &attributes, policy_alg );
1335 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001336
Gilles Peskine049c7532019-05-15 20:22:09 +02001337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001338 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339
Ronald Cron5425a212020-08-04 14:58:35 +02001340 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001341 payload, payload_length,
1342 signature, sizeof( signature ),
1343 &signature_length );
1344 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001345 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001347 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001348
1349 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001350 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001351 payload, payload_length,
1352 signature, sizeof( signature ) );
1353 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001354 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001355 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001356 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001357
1358exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001359 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001360 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001361}
1362/* END_CASE */
1363
Janos Follathba3fab92019-06-11 14:50:16 +01001364/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001365void derive_key_policy( int policy_usage,
1366 int policy_alg,
1367 int key_type,
1368 data_t *key_data,
1369 int exercise_alg )
1370{
Ronald Cron5425a212020-08-04 14:58:35 +02001371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001373 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001374 psa_status_t status;
1375
Gilles Peskine8817f612018-12-18 00:18:46 +01001376 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001377
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001378 psa_set_key_usage_flags( &attributes, policy_usage );
1379 psa_set_key_algorithm( &attributes, policy_alg );
1380 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001381
Gilles Peskine049c7532019-05-15 20:22:09 +02001382 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001383 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001384
Janos Follathba3fab92019-06-11 14:50:16 +01001385 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1386
1387 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1388 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001389 {
Janos Follathba3fab92019-06-11 14:50:16 +01001390 PSA_ASSERT( psa_key_derivation_input_bytes(
1391 &operation,
1392 PSA_KEY_DERIVATION_INPUT_SEED,
1393 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001394 }
Janos Follathba3fab92019-06-11 14:50:16 +01001395
1396 status = psa_key_derivation_input_key( &operation,
1397 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001398 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001399
Gilles Peskineea0fb492018-07-12 17:17:20 +02001400 if( policy_alg == exercise_alg &&
1401 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001402 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001403 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001404 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001405
1406exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001407 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001408 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001409 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001410}
1411/* END_CASE */
1412
1413/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001414void agreement_key_policy( int policy_usage,
1415 int policy_alg,
1416 int key_type_arg,
1417 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001418 int exercise_alg,
1419 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001420{
Ronald Cron5425a212020-08-04 14:58:35 +02001421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001423 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001424 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001425 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001426 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001427
Gilles Peskine8817f612018-12-18 00:18:46 +01001428 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001429
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001430 psa_set_key_usage_flags( &attributes, policy_usage );
1431 psa_set_key_algorithm( &attributes, policy_alg );
1432 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001433
Gilles Peskine049c7532019-05-15 20:22:09 +02001434 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001435 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001436
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001437 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001438 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001439
Steven Cooremance48e852020-10-05 16:02:45 +02001440 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441
1442exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001443 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001444 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001445 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001446}
1447/* END_CASE */
1448
1449/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001450void key_policy_alg2( int key_type_arg, data_t *key_data,
1451 int usage_arg, int alg_arg, int alg2_arg )
1452{
Ronald Cron5425a212020-08-04 14:58:35 +02001453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001454 psa_key_type_t key_type = key_type_arg;
1455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1456 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1457 psa_key_usage_t usage = usage_arg;
1458 psa_algorithm_t alg = alg_arg;
1459 psa_algorithm_t alg2 = alg2_arg;
1460
1461 PSA_ASSERT( psa_crypto_init( ) );
1462
1463 psa_set_key_usage_flags( &attributes, usage );
1464 psa_set_key_algorithm( &attributes, alg );
1465 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1466 psa_set_key_type( &attributes, key_type );
1467 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001468 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001469
Ronald Cron5425a212020-08-04 14:58:35 +02001470 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001471 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1472 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1473 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1474
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001475 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001476 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001477 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001478 goto exit;
1479
1480exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001481 /*
1482 * Key attributes may have been returned by psa_get_key_attributes()
1483 * thus reset them as required.
1484 */
1485 psa_reset_key_attributes( &got_attributes );
1486
Ronald Cron5425a212020-08-04 14:58:35 +02001487 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001488 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001489}
1490/* END_CASE */
1491
1492/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001493void raw_agreement_key_policy( int policy_usage,
1494 int policy_alg,
1495 int key_type_arg,
1496 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001497 int exercise_alg,
1498 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001499{
Ronald Cron5425a212020-08-04 14:58:35 +02001500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001502 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001503 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001504 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001505 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001506
1507 PSA_ASSERT( psa_crypto_init( ) );
1508
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001509 psa_set_key_usage_flags( &attributes, policy_usage );
1510 psa_set_key_algorithm( &attributes, policy_alg );
1511 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001512
Gilles Peskine049c7532019-05-15 20:22:09 +02001513 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001514 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001515
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001516 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517
Steven Cooremance48e852020-10-05 16:02:45 +02001518 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001519
1520exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001521 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001522 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001523 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001524}
1525/* END_CASE */
1526
1527/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001528void copy_success( int source_usage_arg,
1529 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001530 int type_arg, data_t *material,
1531 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001532 int target_usage_arg,
1533 int target_alg_arg, int target_alg2_arg,
1534 int expected_usage_arg,
1535 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001536{
Gilles Peskineca25db92019-04-19 11:43:08 +02001537 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1538 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001539 psa_key_usage_t expected_usage = expected_usage_arg;
1540 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001541 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001542 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1543 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001544 uint8_t *export_buffer = NULL;
1545
Gilles Peskine57ab7212019-01-28 13:03:09 +01001546 PSA_ASSERT( psa_crypto_init( ) );
1547
Gilles Peskineca25db92019-04-19 11:43:08 +02001548 /* Prepare the source key. */
1549 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1550 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001551 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001552 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001553 PSA_ASSERT( psa_import_key( &source_attributes,
1554 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001555 &source_key ) );
1556 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001557
Gilles Peskineca25db92019-04-19 11:43:08 +02001558 /* Prepare the target attributes. */
1559 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001560 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001561 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001562 /* Set volatile lifetime to reset the key identifier to 0. */
1563 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1564 }
1565
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 if( target_usage_arg != -1 )
1567 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1568 if( target_alg_arg != -1 )
1569 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001570 if( target_alg2_arg != -1 )
1571 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001572
1573 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001574 PSA_ASSERT( psa_copy_key( source_key,
1575 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001576
1577 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001578 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001579
1580 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001581 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001582 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1583 psa_get_key_type( &target_attributes ) );
1584 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1585 psa_get_key_bits( &target_attributes ) );
1586 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1587 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001588 TEST_EQUAL( expected_alg2,
1589 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001590 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1591 {
1592 size_t length;
1593 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001594 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001595 material->len, &length ) );
1596 ASSERT_COMPARE( material->x, material->len,
1597 export_buffer, length );
1598 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001599
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001600 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001601 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001602 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001603 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001604
Ronald Cron5425a212020-08-04 14:58:35 +02001605 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001606
1607exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001608 /*
1609 * Source and target key attributes may have been returned by
1610 * psa_get_key_attributes() thus reset them as required.
1611 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001612 psa_reset_key_attributes( &source_attributes );
1613 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001614
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001615 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001616 mbedtls_free( export_buffer );
1617}
1618/* END_CASE */
1619
1620/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001621void copy_fail( int source_usage_arg,
1622 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001623 int type_arg, data_t *material,
1624 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001625 int target_usage_arg,
1626 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001627 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001628 int expected_status_arg )
1629{
1630 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1631 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001632 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1633 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001634 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001635
1636 PSA_ASSERT( psa_crypto_init( ) );
1637
1638 /* Prepare the source key. */
1639 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1640 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001641 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001642 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001643 PSA_ASSERT( psa_import_key( &source_attributes,
1644 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001645 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001646
1647 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001648 psa_set_key_id( &target_attributes, key_id );
1649 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001650 psa_set_key_type( &target_attributes, target_type_arg );
1651 psa_set_key_bits( &target_attributes, target_bits_arg );
1652 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1653 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001654 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001655
1656 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001657 TEST_EQUAL( psa_copy_key( source_key,
1658 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001659 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001660
Ronald Cron5425a212020-08-04 14:58:35 +02001661 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001662
Gilles Peskine4a644642019-05-03 17:14:08 +02001663exit:
1664 psa_reset_key_attributes( &source_attributes );
1665 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001666 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001667}
1668/* END_CASE */
1669
1670/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001671void hash_operation_init( )
1672{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001673 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001674 /* Test each valid way of initializing the object, except for `= {0}`, as
1675 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1676 * though it's OK by the C standard. We could test for this, but we'd need
1677 * to supress the Clang warning for the test. */
1678 psa_hash_operation_t func = psa_hash_operation_init( );
1679 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1680 psa_hash_operation_t zero;
1681
1682 memset( &zero, 0, sizeof( zero ) );
1683
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001684 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001685 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1686 PSA_ERROR_BAD_STATE );
1687 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1688 PSA_ERROR_BAD_STATE );
1689 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1690 PSA_ERROR_BAD_STATE );
1691
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001692 /* A default hash operation should be abortable without error. */
1693 PSA_ASSERT( psa_hash_abort( &func ) );
1694 PSA_ASSERT( psa_hash_abort( &init ) );
1695 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001696}
1697/* END_CASE */
1698
1699/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001700void hash_setup( int alg_arg,
1701 int expected_status_arg )
1702{
1703 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001704 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001705 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001706 psa_status_t status;
1707
Gilles Peskine8817f612018-12-18 00:18:46 +01001708 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001709
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001710 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001711 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001712
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001713 /* Whether setup succeeded or failed, abort must succeed. */
1714 PSA_ASSERT( psa_hash_abort( &operation ) );
1715
1716 /* If setup failed, reproduce the failure, so as to
1717 * test the resulting state of the operation object. */
1718 if( status != PSA_SUCCESS )
1719 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1720
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001721 /* Now the operation object should be reusable. */
1722#if defined(KNOWN_SUPPORTED_HASH_ALG)
1723 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1724 PSA_ASSERT( psa_hash_abort( &operation ) );
1725#endif
1726
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001727exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001728 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001729}
1730/* END_CASE */
1731
1732/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001733void hash_compute_fail( int alg_arg, data_t *input,
1734 int output_size_arg, int expected_status_arg )
1735{
1736 psa_algorithm_t alg = alg_arg;
1737 uint8_t *output = NULL;
1738 size_t output_size = output_size_arg;
1739 size_t output_length = INVALID_EXPORT_LENGTH;
1740 psa_status_t expected_status = expected_status_arg;
1741 psa_status_t status;
1742
1743 ASSERT_ALLOC( output, output_size );
1744
1745 PSA_ASSERT( psa_crypto_init( ) );
1746
1747 status = psa_hash_compute( alg, input->x, input->len,
1748 output, output_size, &output_length );
1749 TEST_EQUAL( status, expected_status );
1750 TEST_ASSERT( output_length <= output_size );
1751
1752exit:
1753 mbedtls_free( output );
1754 PSA_DONE( );
1755}
1756/* END_CASE */
1757
1758/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001759void hash_compare_fail( int alg_arg, data_t *input,
1760 data_t *reference_hash,
1761 int expected_status_arg )
1762{
1763 psa_algorithm_t alg = alg_arg;
1764 psa_status_t expected_status = expected_status_arg;
1765 psa_status_t status;
1766
1767 PSA_ASSERT( psa_crypto_init( ) );
1768
1769 status = psa_hash_compare( alg, input->x, input->len,
1770 reference_hash->x, reference_hash->len );
1771 TEST_EQUAL( status, expected_status );
1772
1773exit:
1774 PSA_DONE( );
1775}
1776/* END_CASE */
1777
1778/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001779void hash_compute_compare( int alg_arg, data_t *input,
1780 data_t *expected_output )
1781{
1782 psa_algorithm_t alg = alg_arg;
1783 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1784 size_t output_length = INVALID_EXPORT_LENGTH;
1785 size_t i;
1786
1787 PSA_ASSERT( psa_crypto_init( ) );
1788
1789 /* Compute with tight buffer */
1790 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001791 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001792 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001793 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001794 ASSERT_COMPARE( output, output_length,
1795 expected_output->x, expected_output->len );
1796
1797 /* Compute with larger buffer */
1798 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1799 output, sizeof( output ),
1800 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001801 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001802 ASSERT_COMPARE( output, output_length,
1803 expected_output->x, expected_output->len );
1804
1805 /* Compare with correct hash */
1806 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1807 output, output_length ) );
1808
1809 /* Compare with trailing garbage */
1810 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1811 output, output_length + 1 ),
1812 PSA_ERROR_INVALID_SIGNATURE );
1813
1814 /* Compare with truncated hash */
1815 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1816 output, output_length - 1 ),
1817 PSA_ERROR_INVALID_SIGNATURE );
1818
1819 /* Compare with corrupted value */
1820 for( i = 0; i < output_length; i++ )
1821 {
Chris Jones9634bb12021-01-20 15:56:42 +00001822 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001823 output[i] ^= 1;
1824 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1825 output, output_length ),
1826 PSA_ERROR_INVALID_SIGNATURE );
1827 output[i] ^= 1;
1828 }
1829
1830exit:
1831 PSA_DONE( );
1832}
1833/* END_CASE */
1834
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001835/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001836void hash_bad_order( )
1837{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001838 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001839 unsigned char input[] = "";
1840 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001841 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001842 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1843 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1844 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001845 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001846 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001847 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001848
Gilles Peskine8817f612018-12-18 00:18:46 +01001849 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001850
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001851 /* Call setup twice in a row. */
1852 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1853 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1854 PSA_ERROR_BAD_STATE );
1855 PSA_ASSERT( psa_hash_abort( &operation ) );
1856
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001857 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001858 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001859 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001860 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001861
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001862 /* Call update after finish. */
1863 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1864 PSA_ASSERT( psa_hash_finish( &operation,
1865 hash, sizeof( hash ), &hash_len ) );
1866 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001867 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001868 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001869
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001870 /* Call verify without calling setup beforehand. */
1871 TEST_EQUAL( psa_hash_verify( &operation,
1872 valid_hash, sizeof( valid_hash ) ),
1873 PSA_ERROR_BAD_STATE );
1874 PSA_ASSERT( psa_hash_abort( &operation ) );
1875
1876 /* Call verify after finish. */
1877 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1878 PSA_ASSERT( psa_hash_finish( &operation,
1879 hash, sizeof( hash ), &hash_len ) );
1880 TEST_EQUAL( psa_hash_verify( &operation,
1881 valid_hash, sizeof( valid_hash ) ),
1882 PSA_ERROR_BAD_STATE );
1883 PSA_ASSERT( psa_hash_abort( &operation ) );
1884
1885 /* Call verify twice in a row. */
1886 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1887 PSA_ASSERT( psa_hash_verify( &operation,
1888 valid_hash, sizeof( valid_hash ) ) );
1889 TEST_EQUAL( psa_hash_verify( &operation,
1890 valid_hash, sizeof( valid_hash ) ),
1891 PSA_ERROR_BAD_STATE );
1892 PSA_ASSERT( psa_hash_abort( &operation ) );
1893
1894 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001895 TEST_EQUAL( psa_hash_finish( &operation,
1896 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001897 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001898 PSA_ASSERT( psa_hash_abort( &operation ) );
1899
1900 /* Call finish twice in a row. */
1901 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1902 PSA_ASSERT( psa_hash_finish( &operation,
1903 hash, sizeof( hash ), &hash_len ) );
1904 TEST_EQUAL( psa_hash_finish( &operation,
1905 hash, sizeof( hash ), &hash_len ),
1906 PSA_ERROR_BAD_STATE );
1907 PSA_ASSERT( psa_hash_abort( &operation ) );
1908
1909 /* Call finish after calling verify. */
1910 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1911 PSA_ASSERT( psa_hash_verify( &operation,
1912 valid_hash, sizeof( valid_hash ) ) );
1913 TEST_EQUAL( psa_hash_finish( &operation,
1914 hash, sizeof( hash ), &hash_len ),
1915 PSA_ERROR_BAD_STATE );
1916 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001917
1918exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001919 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001920}
1921/* END_CASE */
1922
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001923/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001924void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001925{
1926 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001927 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1928 * appended to it */
1929 unsigned char hash[] = {
1930 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1931 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1932 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001933 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001934 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001935
Gilles Peskine8817f612018-12-18 00:18:46 +01001936 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001937
itayzafrir27e69452018-11-01 14:26:34 +02001938 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001939 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001940 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001941 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001942
itayzafrir27e69452018-11-01 14:26:34 +02001943 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001944 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001945 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001946 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001947
itayzafrir27e69452018-11-01 14:26:34 +02001948 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001950 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001951 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001952
itayzafrirec93d302018-10-18 18:01:10 +03001953exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001954 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001955}
1956/* END_CASE */
1957
Ronald Cronee414c72021-03-18 18:50:08 +01001958/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001959void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001960{
1961 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001962 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001963 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001964 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001965 size_t hash_len;
1966
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001968
itayzafrir58028322018-10-25 10:22:01 +03001969 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001970 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001971 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001972 hash, expected_size - 1, &hash_len ),
1973 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001974
1975exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001976 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001977}
1978/* END_CASE */
1979
Ronald Cronee414c72021-03-18 18:50:08 +01001980/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001981void hash_clone_source_state( )
1982{
1983 psa_algorithm_t alg = PSA_ALG_SHA_256;
1984 unsigned char hash[PSA_HASH_MAX_SIZE];
1985 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1986 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1987 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1988 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1989 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1990 size_t hash_len;
1991
1992 PSA_ASSERT( psa_crypto_init( ) );
1993 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1994
1995 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1996 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1997 PSA_ASSERT( psa_hash_finish( &op_finished,
1998 hash, sizeof( hash ), &hash_len ) );
1999 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2000 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2001
2002 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2003 PSA_ERROR_BAD_STATE );
2004
2005 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2006 PSA_ASSERT( psa_hash_finish( &op_init,
2007 hash, sizeof( hash ), &hash_len ) );
2008 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2009 PSA_ASSERT( psa_hash_finish( &op_finished,
2010 hash, sizeof( hash ), &hash_len ) );
2011 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2012 PSA_ASSERT( psa_hash_finish( &op_aborted,
2013 hash, sizeof( hash ), &hash_len ) );
2014
2015exit:
2016 psa_hash_abort( &op_source );
2017 psa_hash_abort( &op_init );
2018 psa_hash_abort( &op_setup );
2019 psa_hash_abort( &op_finished );
2020 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002021 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002022}
2023/* END_CASE */
2024
Ronald Cronee414c72021-03-18 18:50:08 +01002025/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002026void hash_clone_target_state( )
2027{
2028 psa_algorithm_t alg = PSA_ALG_SHA_256;
2029 unsigned char hash[PSA_HASH_MAX_SIZE];
2030 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2031 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2032 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2033 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2034 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2035 size_t hash_len;
2036
2037 PSA_ASSERT( psa_crypto_init( ) );
2038
2039 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2040 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2041 PSA_ASSERT( psa_hash_finish( &op_finished,
2042 hash, sizeof( hash ), &hash_len ) );
2043 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2044 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2045
2046 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2047 PSA_ASSERT( psa_hash_finish( &op_target,
2048 hash, sizeof( hash ), &hash_len ) );
2049
2050 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2051 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2052 PSA_ERROR_BAD_STATE );
2053 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2054 PSA_ERROR_BAD_STATE );
2055
2056exit:
2057 psa_hash_abort( &op_target );
2058 psa_hash_abort( &op_init );
2059 psa_hash_abort( &op_setup );
2060 psa_hash_abort( &op_finished );
2061 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002062 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002063}
2064/* END_CASE */
2065
itayzafrir58028322018-10-25 10:22:01 +03002066/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002067void mac_operation_init( )
2068{
Jaeden Amero252ef282019-02-15 14:05:35 +00002069 const uint8_t input[1] = { 0 };
2070
Jaeden Amero769ce272019-01-04 11:48:03 +00002071 /* Test each valid way of initializing the object, except for `= {0}`, as
2072 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2073 * though it's OK by the C standard. We could test for this, but we'd need
2074 * to supress the Clang warning for the test. */
2075 psa_mac_operation_t func = psa_mac_operation_init( );
2076 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2077 psa_mac_operation_t zero;
2078
2079 memset( &zero, 0, sizeof( zero ) );
2080
Jaeden Amero252ef282019-02-15 14:05:35 +00002081 /* A freshly-initialized MAC operation should not be usable. */
2082 TEST_EQUAL( psa_mac_update( &func,
2083 input, sizeof( input ) ),
2084 PSA_ERROR_BAD_STATE );
2085 TEST_EQUAL( psa_mac_update( &init,
2086 input, sizeof( input ) ),
2087 PSA_ERROR_BAD_STATE );
2088 TEST_EQUAL( psa_mac_update( &zero,
2089 input, sizeof( input ) ),
2090 PSA_ERROR_BAD_STATE );
2091
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002092 /* A default MAC operation should be abortable without error. */
2093 PSA_ASSERT( psa_mac_abort( &func ) );
2094 PSA_ASSERT( psa_mac_abort( &init ) );
2095 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002096}
2097/* END_CASE */
2098
2099/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002100void mac_setup( int key_type_arg,
2101 data_t *key,
2102 int alg_arg,
2103 int expected_status_arg )
2104{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105 psa_key_type_t key_type = key_type_arg;
2106 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002107 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002108 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002109 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2110#if defined(KNOWN_SUPPORTED_MAC_ALG)
2111 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2112#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002113
Gilles Peskine8817f612018-12-18 00:18:46 +01002114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002115
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002116 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2117 &operation, &status ) )
2118 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002119 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002120
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002121 /* The operation object should be reusable. */
2122#if defined(KNOWN_SUPPORTED_MAC_ALG)
2123 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2124 smoke_test_key_data,
2125 sizeof( smoke_test_key_data ),
2126 KNOWN_SUPPORTED_MAC_ALG,
2127 &operation, &status ) )
2128 goto exit;
2129 TEST_EQUAL( status, PSA_SUCCESS );
2130#endif
2131
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002132exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002133 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134}
2135/* END_CASE */
2136
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002137/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00002138void mac_bad_order( )
2139{
Ronald Cron5425a212020-08-04 14:58:35 +02002140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002141 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2142 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002143 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002144 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2145 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2146 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002148 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2149 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2150 size_t sign_mac_length = 0;
2151 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2152 const uint8_t verify_mac[] = {
2153 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2154 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2155 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2156
2157 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002158 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002159 psa_set_key_algorithm( &attributes, alg );
2160 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002161
Ronald Cron5425a212020-08-04 14:58:35 +02002162 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2163 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002164
Jaeden Amero252ef282019-02-15 14:05:35 +00002165 /* Call update without calling setup beforehand. */
2166 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2167 PSA_ERROR_BAD_STATE );
2168 PSA_ASSERT( psa_mac_abort( &operation ) );
2169
2170 /* Call sign finish without calling setup beforehand. */
2171 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2172 &sign_mac_length),
2173 PSA_ERROR_BAD_STATE );
2174 PSA_ASSERT( psa_mac_abort( &operation ) );
2175
2176 /* Call verify finish without calling setup beforehand. */
2177 TEST_EQUAL( psa_mac_verify_finish( &operation,
2178 verify_mac, sizeof( verify_mac ) ),
2179 PSA_ERROR_BAD_STATE );
2180 PSA_ASSERT( psa_mac_abort( &operation ) );
2181
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002182 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002183 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2184 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002185 PSA_ERROR_BAD_STATE );
2186 PSA_ASSERT( psa_mac_abort( &operation ) );
2187
Jaeden Amero252ef282019-02-15 14:05:35 +00002188 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002189 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002190 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2191 PSA_ASSERT( psa_mac_sign_finish( &operation,
2192 sign_mac, sizeof( sign_mac ),
2193 &sign_mac_length ) );
2194 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2195 PSA_ERROR_BAD_STATE );
2196 PSA_ASSERT( psa_mac_abort( &operation ) );
2197
2198 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002199 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002200 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2201 PSA_ASSERT( psa_mac_verify_finish( &operation,
2202 verify_mac, sizeof( verify_mac ) ) );
2203 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2204 PSA_ERROR_BAD_STATE );
2205 PSA_ASSERT( psa_mac_abort( &operation ) );
2206
2207 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002208 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002209 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2210 PSA_ASSERT( psa_mac_sign_finish( &operation,
2211 sign_mac, sizeof( sign_mac ),
2212 &sign_mac_length ) );
2213 TEST_EQUAL( psa_mac_sign_finish( &operation,
2214 sign_mac, sizeof( sign_mac ),
2215 &sign_mac_length ),
2216 PSA_ERROR_BAD_STATE );
2217 PSA_ASSERT( psa_mac_abort( &operation ) );
2218
2219 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002220 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002221 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2222 PSA_ASSERT( psa_mac_verify_finish( &operation,
2223 verify_mac, sizeof( verify_mac ) ) );
2224 TEST_EQUAL( psa_mac_verify_finish( &operation,
2225 verify_mac, sizeof( verify_mac ) ),
2226 PSA_ERROR_BAD_STATE );
2227 PSA_ASSERT( psa_mac_abort( &operation ) );
2228
2229 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002230 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002231 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2232 TEST_EQUAL( psa_mac_verify_finish( &operation,
2233 verify_mac, sizeof( verify_mac ) ),
2234 PSA_ERROR_BAD_STATE );
2235 PSA_ASSERT( psa_mac_abort( &operation ) );
2236
2237 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002238 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002239 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2240 TEST_EQUAL( psa_mac_sign_finish( &operation,
2241 sign_mac, sizeof( sign_mac ),
2242 &sign_mac_length ),
2243 PSA_ERROR_BAD_STATE );
2244 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002245
Ronald Cron5425a212020-08-04 14:58:35 +02002246 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002247
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002248exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002249 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002250}
2251/* END_CASE */
2252
2253/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002254void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002255 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002256 int alg_arg,
2257 data_t *input,
2258 data_t *expected_mac )
2259{
Ronald Cron5425a212020-08-04 14:58:35 +02002260 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002261 psa_key_type_t key_type = key_type_arg;
2262 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002263 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002265 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002266 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002267 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002268 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002269 const size_t output_sizes_to_test[] = {
2270 0,
2271 1,
2272 expected_mac->len - 1,
2273 expected_mac->len,
2274 expected_mac->len + 1,
2275 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002276
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002277 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002278 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002279 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002280
Gilles Peskine8817f612018-12-18 00:18:46 +01002281 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002282
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002283 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002284 psa_set_key_algorithm( &attributes, alg );
2285 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002286
Ronald Cron5425a212020-08-04 14:58:35 +02002287 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2288 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002289
Gilles Peskine8b356b52020-08-25 23:44:59 +02002290 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2291 {
2292 const size_t output_size = output_sizes_to_test[i];
2293 psa_status_t expected_status =
2294 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2295 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002296
Chris Jones9634bb12021-01-20 15:56:42 +00002297 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002298 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002299
Gilles Peskine8b356b52020-08-25 23:44:59 +02002300 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002301 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002302 PSA_ASSERT( psa_mac_update( &operation,
2303 input->x, input->len ) );
2304 TEST_EQUAL( psa_mac_sign_finish( &operation,
2305 actual_mac, output_size,
2306 &mac_length ),
2307 expected_status );
2308 PSA_ASSERT( psa_mac_abort( &operation ) );
2309
2310 if( expected_status == PSA_SUCCESS )
2311 {
2312 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2313 actual_mac, mac_length );
2314 }
2315 mbedtls_free( actual_mac );
2316 actual_mac = NULL;
2317 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002318
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002319exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002320 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002321 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002322 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002323 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002324}
2325/* END_CASE */
2326
2327/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002328void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002329 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002330 int alg_arg,
2331 data_t *input,
2332 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333{
Ronald Cron5425a212020-08-04 14:58:35 +02002334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002335 psa_key_type_t key_type = key_type_arg;
2336 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002337 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002339 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002340
Gilles Peskine69c12672018-06-28 00:07:19 +02002341 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2342
Gilles Peskine8817f612018-12-18 00:18:46 +01002343 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002344
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002345 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002346 psa_set_key_algorithm( &attributes, alg );
2347 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002348
Ronald Cron5425a212020-08-04 14:58:35 +02002349 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2350 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002351
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002352 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002353 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002354 PSA_ASSERT( psa_mac_update( &operation,
2355 input->x, input->len ) );
2356 PSA_ASSERT( psa_mac_verify_finish( &operation,
2357 expected_mac->x,
2358 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002359
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002360 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002361 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002362 PSA_ASSERT( psa_mac_update( &operation,
2363 input->x, input->len ) );
2364 TEST_EQUAL( psa_mac_verify_finish( &operation,
2365 expected_mac->x,
2366 expected_mac->len - 1 ),
2367 PSA_ERROR_INVALID_SIGNATURE );
2368
2369 /* Test a MAC that's too long. */
2370 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2371 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002372 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002373 PSA_ASSERT( psa_mac_update( &operation,
2374 input->x, input->len ) );
2375 TEST_EQUAL( psa_mac_verify_finish( &operation,
2376 perturbed_mac,
2377 expected_mac->len + 1 ),
2378 PSA_ERROR_INVALID_SIGNATURE );
2379
2380 /* Test changing one byte. */
2381 for( size_t i = 0; i < expected_mac->len; i++ )
2382 {
Chris Jones9634bb12021-01-20 15:56:42 +00002383 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002384 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002386 PSA_ASSERT( psa_mac_update( &operation,
2387 input->x, input->len ) );
2388 TEST_EQUAL( psa_mac_verify_finish( &operation,
2389 perturbed_mac,
2390 expected_mac->len ),
2391 PSA_ERROR_INVALID_SIGNATURE );
2392 perturbed_mac[i] ^= 1;
2393 }
2394
Gilles Peskine8c9def32018-02-08 10:02:12 +01002395exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002396 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002397 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002398 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002399 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400}
2401/* END_CASE */
2402
2403/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002404void cipher_operation_init( )
2405{
Jaeden Ameroab439972019-02-15 14:12:05 +00002406 const uint8_t input[1] = { 0 };
2407 unsigned char output[1] = { 0 };
2408 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002409 /* Test each valid way of initializing the object, except for `= {0}`, as
2410 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2411 * though it's OK by the C standard. We could test for this, but we'd need
2412 * to supress the Clang warning for the test. */
2413 psa_cipher_operation_t func = psa_cipher_operation_init( );
2414 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2415 psa_cipher_operation_t zero;
2416
2417 memset( &zero, 0, sizeof( zero ) );
2418
Jaeden Ameroab439972019-02-15 14:12:05 +00002419 /* A freshly-initialized cipher operation should not be usable. */
2420 TEST_EQUAL( psa_cipher_update( &func,
2421 input, sizeof( input ),
2422 output, sizeof( output ),
2423 &output_length ),
2424 PSA_ERROR_BAD_STATE );
2425 TEST_EQUAL( psa_cipher_update( &init,
2426 input, sizeof( input ),
2427 output, sizeof( output ),
2428 &output_length ),
2429 PSA_ERROR_BAD_STATE );
2430 TEST_EQUAL( psa_cipher_update( &zero,
2431 input, sizeof( input ),
2432 output, sizeof( output ),
2433 &output_length ),
2434 PSA_ERROR_BAD_STATE );
2435
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002436 /* A default cipher operation should be abortable without error. */
2437 PSA_ASSERT( psa_cipher_abort( &func ) );
2438 PSA_ASSERT( psa_cipher_abort( &init ) );
2439 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002440}
2441/* END_CASE */
2442
2443/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444void cipher_setup( int key_type_arg,
2445 data_t *key,
2446 int alg_arg,
2447 int expected_status_arg )
2448{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449 psa_key_type_t key_type = key_type_arg;
2450 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002451 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002452 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002453 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002454#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002455 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2456#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002457
Gilles Peskine8817f612018-12-18 00:18:46 +01002458 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002459
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002460 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2461 &operation, &status ) )
2462 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002463 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002464
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002465 /* The operation object should be reusable. */
2466#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2467 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2468 smoke_test_key_data,
2469 sizeof( smoke_test_key_data ),
2470 KNOWN_SUPPORTED_CIPHER_ALG,
2471 &operation, &status ) )
2472 goto exit;
2473 TEST_EQUAL( status, PSA_SUCCESS );
2474#endif
2475
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002477 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002478 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002479}
2480/* END_CASE */
2481
Ronald Cronee414c72021-03-18 18:50:08 +01002482/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002483void cipher_bad_order( )
2484{
Ronald Cron5425a212020-08-04 14:58:35 +02002485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002486 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2487 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002489 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002490 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002491 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002492 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2493 0xaa, 0xaa, 0xaa, 0xaa };
2494 const uint8_t text[] = {
2495 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2496 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002497 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002498 size_t length = 0;
2499
2500 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002501 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2502 psa_set_key_algorithm( &attributes, alg );
2503 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002504 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2505 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002506
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002507 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002508 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2509 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002510 PSA_ERROR_BAD_STATE );
2511 PSA_ASSERT( psa_cipher_abort( &operation ) );
2512
2513 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002514 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2515 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002516 PSA_ERROR_BAD_STATE );
2517 PSA_ASSERT( psa_cipher_abort( &operation ) );
2518
Jaeden Ameroab439972019-02-15 14:12:05 +00002519 /* Generate an IV without calling setup beforehand. */
2520 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2521 buffer, sizeof( buffer ),
2522 &length ),
2523 PSA_ERROR_BAD_STATE );
2524 PSA_ASSERT( psa_cipher_abort( &operation ) );
2525
2526 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002527 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002528 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2529 buffer, sizeof( buffer ),
2530 &length ) );
2531 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2532 buffer, sizeof( buffer ),
2533 &length ),
2534 PSA_ERROR_BAD_STATE );
2535 PSA_ASSERT( psa_cipher_abort( &operation ) );
2536
2537 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002538 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002539 PSA_ASSERT( psa_cipher_set_iv( &operation,
2540 iv, sizeof( iv ) ) );
2541 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2542 buffer, sizeof( buffer ),
2543 &length ),
2544 PSA_ERROR_BAD_STATE );
2545 PSA_ASSERT( psa_cipher_abort( &operation ) );
2546
2547 /* Set an IV without calling setup beforehand. */
2548 TEST_EQUAL( psa_cipher_set_iv( &operation,
2549 iv, sizeof( iv ) ),
2550 PSA_ERROR_BAD_STATE );
2551 PSA_ASSERT( psa_cipher_abort( &operation ) );
2552
2553 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002554 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002555 PSA_ASSERT( psa_cipher_set_iv( &operation,
2556 iv, sizeof( iv ) ) );
2557 TEST_EQUAL( psa_cipher_set_iv( &operation,
2558 iv, sizeof( iv ) ),
2559 PSA_ERROR_BAD_STATE );
2560 PSA_ASSERT( psa_cipher_abort( &operation ) );
2561
2562 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002563 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002564 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2565 buffer, sizeof( buffer ),
2566 &length ) );
2567 TEST_EQUAL( psa_cipher_set_iv( &operation,
2568 iv, sizeof( iv ) ),
2569 PSA_ERROR_BAD_STATE );
2570 PSA_ASSERT( psa_cipher_abort( &operation ) );
2571
2572 /* Call update without calling setup beforehand. */
2573 TEST_EQUAL( psa_cipher_update( &operation,
2574 text, sizeof( text ),
2575 buffer, sizeof( buffer ),
2576 &length ),
2577 PSA_ERROR_BAD_STATE );
2578 PSA_ASSERT( psa_cipher_abort( &operation ) );
2579
2580 /* Call update without an IV where an IV is required. */
2581 TEST_EQUAL( psa_cipher_update( &operation,
2582 text, sizeof( text ),
2583 buffer, sizeof( buffer ),
2584 &length ),
2585 PSA_ERROR_BAD_STATE );
2586 PSA_ASSERT( psa_cipher_abort( &operation ) );
2587
2588 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002589 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002590 PSA_ASSERT( psa_cipher_set_iv( &operation,
2591 iv, sizeof( iv ) ) );
2592 PSA_ASSERT( psa_cipher_finish( &operation,
2593 buffer, sizeof( buffer ), &length ) );
2594 TEST_EQUAL( psa_cipher_update( &operation,
2595 text, sizeof( text ),
2596 buffer, sizeof( buffer ),
2597 &length ),
2598 PSA_ERROR_BAD_STATE );
2599 PSA_ASSERT( psa_cipher_abort( &operation ) );
2600
2601 /* Call finish without calling setup beforehand. */
2602 TEST_EQUAL( psa_cipher_finish( &operation,
2603 buffer, sizeof( buffer ), &length ),
2604 PSA_ERROR_BAD_STATE );
2605 PSA_ASSERT( psa_cipher_abort( &operation ) );
2606
2607 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002608 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002609 /* Not calling update means we are encrypting an empty buffer, which is OK
2610 * for cipher modes with padding. */
2611 TEST_EQUAL( psa_cipher_finish( &operation,
2612 buffer, sizeof( buffer ), &length ),
2613 PSA_ERROR_BAD_STATE );
2614 PSA_ASSERT( psa_cipher_abort( &operation ) );
2615
2616 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002617 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002618 PSA_ASSERT( psa_cipher_set_iv( &operation,
2619 iv, sizeof( iv ) ) );
2620 PSA_ASSERT( psa_cipher_finish( &operation,
2621 buffer, sizeof( buffer ), &length ) );
2622 TEST_EQUAL( psa_cipher_finish( &operation,
2623 buffer, sizeof( buffer ), &length ),
2624 PSA_ERROR_BAD_STATE );
2625 PSA_ASSERT( psa_cipher_abort( &operation ) );
2626
Ronald Cron5425a212020-08-04 14:58:35 +02002627 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002628
Jaeden Ameroab439972019-02-15 14:12:05 +00002629exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002630 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002631 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002632}
2633/* END_CASE */
2634
2635/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002636void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002637 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002638 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002639 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002640{
Ronald Cron5425a212020-08-04 14:58:35 +02002641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642 psa_status_t status;
2643 psa_key_type_t key_type = key_type_arg;
2644 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002645 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002646 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647 size_t output_buffer_size = 0;
2648 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002649 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002650 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652
Gilles Peskine8817f612018-12-18 00:18:46 +01002653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002655 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2656 psa_set_key_algorithm( &attributes, alg );
2657 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002658
Ronald Cron5425a212020-08-04 14:58:35 +02002659 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2660 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661
Ronald Cron5425a212020-08-04 14:58:35 +02002662 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002664 if( iv->len > 0 )
2665 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002666 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002667 }
2668
gabor-mezei-armceface22021-01-21 12:26:17 +01002669 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2670 TEST_ASSERT( output_buffer_size <=
2671 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002672 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Gilles Peskine8817f612018-12-18 00:18:46 +01002674 PSA_ASSERT( psa_cipher_update( &operation,
2675 input->x, input->len,
2676 output, output_buffer_size,
2677 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002678 TEST_ASSERT( function_output_length <=
2679 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2680 TEST_ASSERT( function_output_length <=
2681 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002682 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002683
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002685 ( output_buffer_size == 0 ? NULL :
2686 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002687 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002688 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002689 TEST_ASSERT( function_output_length <=
2690 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2691 TEST_ASSERT( function_output_length <=
2692 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002693 total_output_length += function_output_length;
2694
Gilles Peskinefe11b722018-12-18 00:24:04 +01002695 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696 if( expected_status == PSA_SUCCESS )
2697 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002698 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002699 ASSERT_COMPARE( expected_output->x, expected_output->len,
2700 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002701 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002702
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002704 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002705 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002706 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002707 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708}
2709/* END_CASE */
2710
2711/* BEGIN_CASE */
2712void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002713 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002714 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002715 int first_part_size_arg,
2716 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002717 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002718{
Ronald Cron5425a212020-08-04 14:58:35 +02002719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720 psa_key_type_t key_type = key_type_arg;
2721 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002722 size_t first_part_size = first_part_size_arg;
2723 size_t output1_length = output1_length_arg;
2724 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002725 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002726 size_t output_buffer_size = 0;
2727 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002728 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002729 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002730 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002731
Gilles Peskine8817f612018-12-18 00:18:46 +01002732 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002733
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002734 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2735 psa_set_key_algorithm( &attributes, alg );
2736 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002737
Ronald Cron5425a212020-08-04 14:58:35 +02002738 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2739 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740
Ronald Cron5425a212020-08-04 14:58:35 +02002741 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002742
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002743 if( iv->len > 0 )
2744 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002745 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002746 }
2747
gabor-mezei-armceface22021-01-21 12:26:17 +01002748 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2749 TEST_ASSERT( output_buffer_size <=
2750 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002751 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752
Gilles Peskinee0866522019-02-19 19:44:00 +01002753 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002754 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2755 output, output_buffer_size,
2756 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002757 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002758 TEST_ASSERT( function_output_length <=
2759 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2760 TEST_ASSERT( function_output_length <=
2761 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002762 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002763
Gilles Peskine8817f612018-12-18 00:18:46 +01002764 PSA_ASSERT( psa_cipher_update( &operation,
2765 input->x + first_part_size,
2766 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002767 ( output_buffer_size == 0 ? NULL :
2768 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002769 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002770 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002771 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002772 TEST_ASSERT( function_output_length <=
2773 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2774 alg,
2775 input->len - first_part_size ) );
2776 TEST_ASSERT( function_output_length <=
2777 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002778 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002779
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002781 ( output_buffer_size == 0 ? NULL :
2782 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002783 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002784 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002785 TEST_ASSERT( function_output_length <=
2786 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2787 TEST_ASSERT( function_output_length <=
2788 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002789 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002790 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002791
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002792 ASSERT_COMPARE( expected_output->x, expected_output->len,
2793 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002794
2795exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002796 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002797 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002798 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002799 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002800}
2801/* END_CASE */
2802
2803/* BEGIN_CASE */
2804void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002805 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002806 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002807 int first_part_size_arg,
2808 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002809 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002810{
Ronald Cron5425a212020-08-04 14:58:35 +02002811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812 psa_key_type_t key_type = key_type_arg;
2813 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002814 size_t first_part_size = first_part_size_arg;
2815 size_t output1_length = output1_length_arg;
2816 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002817 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818 size_t output_buffer_size = 0;
2819 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002820 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002821 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823
Gilles Peskine8817f612018-12-18 00:18:46 +01002824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2827 psa_set_key_algorithm( &attributes, alg );
2828 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002829
Ronald Cron5425a212020-08-04 14:58:35 +02002830 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2831 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002832
Ronald Cron5425a212020-08-04 14:58:35 +02002833 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834
Steven Cooreman177deba2020-09-07 17:14:14 +02002835 if( iv->len > 0 )
2836 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002837 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002838 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839
gabor-mezei-armceface22021-01-21 12:26:17 +01002840 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2841 TEST_ASSERT( output_buffer_size <=
2842 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002843 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
Gilles Peskinee0866522019-02-19 19:44:00 +01002845 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002846 PSA_ASSERT( psa_cipher_update( &operation,
2847 input->x, first_part_size,
2848 output, output_buffer_size,
2849 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002850 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002851 TEST_ASSERT( function_output_length <=
2852 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2853 TEST_ASSERT( function_output_length <=
2854 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002855 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002856
Gilles Peskine8817f612018-12-18 00:18:46 +01002857 PSA_ASSERT( psa_cipher_update( &operation,
2858 input->x + first_part_size,
2859 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002860 ( output_buffer_size == 0 ? NULL :
2861 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002862 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002863 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002864 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002865 TEST_ASSERT( function_output_length <=
2866 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2867 alg,
2868 input->len - first_part_size ) );
2869 TEST_ASSERT( function_output_length <=
2870 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002871 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002872
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002874 ( output_buffer_size == 0 ? NULL :
2875 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002876 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002877 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002878 TEST_ASSERT( function_output_length <=
2879 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2880 TEST_ASSERT( function_output_length <=
2881 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002882 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002883 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002885 ASSERT_COMPARE( expected_output->x, expected_output->len,
2886 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002887
2888exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002889 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002890 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002891 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002892 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002893}
2894/* END_CASE */
2895
Gilles Peskine50e586b2018-06-08 14:28:46 +02002896/* BEGIN_CASE */
2897void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002898 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002899 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002900 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901{
Ronald Cron5425a212020-08-04 14:58:35 +02002902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903 psa_status_t status;
2904 psa_key_type_t key_type = key_type_arg;
2905 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002906 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002907 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908 size_t output_buffer_size = 0;
2909 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002910 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002911 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913
Gilles Peskine8817f612018-12-18 00:18:46 +01002914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2917 psa_set_key_algorithm( &attributes, alg );
2918 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002919
Ronald Cron5425a212020-08-04 14:58:35 +02002920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2921 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922
Ronald Cron5425a212020-08-04 14:58:35 +02002923 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924
Steven Cooreman177deba2020-09-07 17:14:14 +02002925 if( iv->len > 0 )
2926 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002927 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002928 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929
gabor-mezei-armceface22021-01-21 12:26:17 +01002930 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2931 TEST_ASSERT( output_buffer_size <=
2932 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002933 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934
Gilles Peskine8817f612018-12-18 00:18:46 +01002935 PSA_ASSERT( psa_cipher_update( &operation,
2936 input->x, input->len,
2937 output, output_buffer_size,
2938 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002939 TEST_ASSERT( function_output_length <=
2940 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2941 TEST_ASSERT( function_output_length <=
2942 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002943 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002944
Gilles Peskine50e586b2018-06-08 14:28:46 +02002945 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002946 ( output_buffer_size == 0 ? NULL :
2947 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002948 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002949 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002950 TEST_ASSERT( function_output_length <=
2951 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2952 TEST_ASSERT( function_output_length <=
2953 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002954 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002955 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002956
2957 if( expected_status == PSA_SUCCESS )
2958 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002959 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002960 ASSERT_COMPARE( expected_output->x, expected_output->len,
2961 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002962 }
2963
Gilles Peskine50e586b2018-06-08 14:28:46 +02002964exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002965 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002966 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002967 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002968 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969}
2970/* END_CASE */
2971
Gilles Peskine50e586b2018-06-08 14:28:46 +02002972/* BEGIN_CASE */
2973void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002974 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002975 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002976{
Ronald Cron5425a212020-08-04 14:58:35 +02002977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002978 psa_key_type_t key_type = key_type_arg;
2979 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002980 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981 size_t iv_size = 16;
2982 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002983 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002984 size_t output1_size = 0;
2985 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002986 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002987 size_t output2_size = 0;
2988 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002989 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002990 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2991 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002993
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002995
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2997 psa_set_key_algorithm( &attributes, alg );
2998 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002999
Ronald Cron5425a212020-08-04 14:58:35 +02003000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3001 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003002
Ronald Cron5425a212020-08-04 14:58:35 +02003003 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3004 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003005
Steven Cooreman177deba2020-09-07 17:14:14 +02003006 if( alg != PSA_ALG_ECB_NO_PADDING )
3007 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003008 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3009 iv, iv_size,
3010 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003011 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003012 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3013 TEST_ASSERT( output1_size <=
3014 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003015 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003016
Gilles Peskine8817f612018-12-18 00:18:46 +01003017 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3018 output1, output1_size,
3019 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003020 TEST_ASSERT( output1_length <=
3021 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3022 TEST_ASSERT( output1_length <=
3023 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3024
Gilles Peskine8817f612018-12-18 00:18:46 +01003025 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003026 output1 + output1_length,
3027 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003029 TEST_ASSERT( function_output_length <=
3030 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3031 TEST_ASSERT( function_output_length <=
3032 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003033
Gilles Peskine048b7f02018-06-08 14:20:49 +02003034 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003035
Gilles Peskine8817f612018-12-18 00:18:46 +01003036 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003037
3038 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003039 TEST_ASSERT( output2_size <=
3040 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3041 TEST_ASSERT( output2_size <=
3042 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003043 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003044
Steven Cooreman177deba2020-09-07 17:14:14 +02003045 if( iv_length > 0 )
3046 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003047 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3048 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003049 }
3050
Gilles Peskine8817f612018-12-18 00:18:46 +01003051 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3052 output2, output2_size,
3053 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003054 TEST_ASSERT( output2_length <=
3055 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3056 TEST_ASSERT( output2_length <=
3057 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3058
Gilles Peskine048b7f02018-06-08 14:20:49 +02003059 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 PSA_ASSERT( psa_cipher_finish( &operation2,
3061 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003062 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003064 TEST_ASSERT( function_output_length <=
3065 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3066 TEST_ASSERT( function_output_length <=
3067 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003068
Gilles Peskine048b7f02018-06-08 14:20:49 +02003069 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003070
Gilles Peskine8817f612018-12-18 00:18:46 +01003071 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003072
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003073 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003074
3075exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003076 psa_cipher_abort( &operation1 );
3077 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003078 mbedtls_free( output1 );
3079 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003080 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003081 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003082}
3083/* END_CASE */
3084
3085/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003086void cipher_verify_output_multipart( int alg_arg,
3087 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003088 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003089 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003090 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003091{
Ronald Cron5425a212020-08-04 14:58:35 +02003092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003093 psa_key_type_t key_type = key_type_arg;
3094 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003095 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003096 unsigned char iv[16] = {0};
3097 size_t iv_size = 16;
3098 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003099 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003100 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003101 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003102 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003103 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003104 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003105 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003106 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3107 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003109
Gilles Peskine8817f612018-12-18 00:18:46 +01003110 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003111
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003112 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3113 psa_set_key_algorithm( &attributes, alg );
3114 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003115
Ronald Cron5425a212020-08-04 14:58:35 +02003116 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3117 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003118
Ronald Cron5425a212020-08-04 14:58:35 +02003119 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3120 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003121
Steven Cooreman177deba2020-09-07 17:14:14 +02003122 if( alg != PSA_ALG_ECB_NO_PADDING )
3123 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003124 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3125 iv, iv_size,
3126 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003127 }
3128
gabor-mezei-armceface22021-01-21 12:26:17 +01003129 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3130 TEST_ASSERT( output1_buffer_size <=
3131 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003132 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003133
Gilles Peskinee0866522019-02-19 19:44:00 +01003134 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003135
Gilles Peskine8817f612018-12-18 00:18:46 +01003136 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3137 output1, output1_buffer_size,
3138 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003139 TEST_ASSERT( function_output_length <=
3140 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3141 TEST_ASSERT( function_output_length <=
3142 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003143 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003144
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 PSA_ASSERT( psa_cipher_update( &operation1,
3146 input->x + first_part_size,
3147 input->len - first_part_size,
3148 output1, output1_buffer_size,
3149 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003150 TEST_ASSERT( function_output_length <=
3151 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3152 alg,
3153 input->len - first_part_size ) );
3154 TEST_ASSERT( function_output_length <=
3155 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003156 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_cipher_finish( &operation1,
3159 output1 + output1_length,
3160 output1_buffer_size - output1_length,
3161 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003162 TEST_ASSERT( function_output_length <=
3163 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3164 TEST_ASSERT( function_output_length <=
3165 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003166 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003167
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003169
Gilles Peskine048b7f02018-06-08 14:20:49 +02003170 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003171 TEST_ASSERT( output2_buffer_size <=
3172 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3173 TEST_ASSERT( output2_buffer_size <=
3174 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003175 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003176
Steven Cooreman177deba2020-09-07 17:14:14 +02003177 if( iv_length > 0 )
3178 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003179 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3180 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003181 }
Moran Pekerded84402018-06-06 16:36:50 +03003182
Gilles Peskine8817f612018-12-18 00:18:46 +01003183 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3184 output2, output2_buffer_size,
3185 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003186 TEST_ASSERT( function_output_length <=
3187 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3188 TEST_ASSERT( function_output_length <=
3189 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003190 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_cipher_update( &operation2,
3193 output1 + first_part_size,
3194 output1_length - first_part_size,
3195 output2, output2_buffer_size,
3196 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003197 TEST_ASSERT( function_output_length <=
3198 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3199 alg,
3200 output1_length - first_part_size ) );
3201 TEST_ASSERT( function_output_length <=
3202 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003203 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003204
Gilles Peskine8817f612018-12-18 00:18:46 +01003205 PSA_ASSERT( psa_cipher_finish( &operation2,
3206 output2 + output2_length,
3207 output2_buffer_size - output2_length,
3208 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003209 TEST_ASSERT( function_output_length <=
3210 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3211 TEST_ASSERT( function_output_length <=
3212 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003213 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003216
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003217 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003218
3219exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003220 psa_cipher_abort( &operation1 );
3221 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003222 mbedtls_free( output1 );
3223 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003224 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003225 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003226}
3227/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003228
Gilles Peskine20035e32018-02-03 22:44:14 +01003229/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003230void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003231 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003232 data_t *nonce,
3233 data_t *additional_data,
3234 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003235 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003236{
Ronald Cron5425a212020-08-04 14:58:35 +02003237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003238 psa_key_type_t key_type = key_type_arg;
3239 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003240 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003241 unsigned char *output_data = NULL;
3242 size_t output_size = 0;
3243 size_t output_length = 0;
3244 unsigned char *output_data2 = NULL;
3245 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003246 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003247 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003252 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3253 psa_set_key_algorithm( &attributes, alg );
3254 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003255
Gilles Peskine049c7532019-05-15 20:22:09 +02003256 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003257 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003258 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3259 key_bits = psa_get_key_bits( &attributes );
3260
3261 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3262 alg );
3263 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3264 * should be exact. */
3265 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3266 expected_result != PSA_ERROR_NOT_SUPPORTED )
3267 {
3268 TEST_EQUAL( output_size,
3269 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3270 TEST_ASSERT( output_size <=
3271 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3272 }
3273 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003274
Steven Cooremanf49478b2021-02-15 15:19:25 +01003275 status = psa_aead_encrypt( key, alg,
3276 nonce->x, nonce->len,
3277 additional_data->x,
3278 additional_data->len,
3279 input_data->x, input_data->len,
3280 output_data, output_size,
3281 &output_length );
3282
3283 /* If the operation is not supported, just skip and not fail in case the
3284 * encryption involves a common limitation of cryptography hardwares and
3285 * an alternative implementation. */
3286 if( status == PSA_ERROR_NOT_SUPPORTED )
3287 {
3288 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3289 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3290 }
3291
3292 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003293
3294 if( PSA_SUCCESS == expected_result )
3295 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003296 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003297
Gilles Peskine003a4a92019-05-14 16:09:40 +02003298 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3299 * should be exact. */
3300 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003301 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003302
gabor-mezei-armceface22021-01-21 12:26:17 +01003303 TEST_ASSERT( input_data->len <=
3304 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3305
Ronald Cron5425a212020-08-04 14:58:35 +02003306 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003307 nonce->x, nonce->len,
3308 additional_data->x,
3309 additional_data->len,
3310 output_data, output_length,
3311 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003312 &output_length2 ),
3313 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003314
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003315 ASSERT_COMPARE( input_data->x, input_data->len,
3316 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003318
Gilles Peskinea1cac842018-06-11 19:33:02 +02003319exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003320 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003321 mbedtls_free( output_data );
3322 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003323 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003324}
3325/* END_CASE */
3326
3327/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003328void aead_encrypt( int key_type_arg, data_t *key_data,
3329 int alg_arg,
3330 data_t *nonce,
3331 data_t *additional_data,
3332 data_t *input_data,
3333 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334{
Ronald Cron5425a212020-08-04 14:58:35 +02003335 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336 psa_key_type_t key_type = key_type_arg;
3337 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003338 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339 unsigned char *output_data = NULL;
3340 size_t output_size = 0;
3341 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003343 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344
Gilles Peskine8817f612018-12-18 00:18:46 +01003345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003346
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3348 psa_set_key_algorithm( &attributes, alg );
3349 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003350
Gilles Peskine049c7532019-05-15 20:22:09 +02003351 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003352 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003353 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3354 key_bits = psa_get_key_bits( &attributes );
3355
3356 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3357 alg );
3358 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3359 * should be exact. */
3360 TEST_EQUAL( output_size,
3361 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3362 TEST_ASSERT( output_size <=
3363 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3364 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365
Steven Cooremand588ea12021-01-11 19:36:04 +01003366 status = psa_aead_encrypt( key, alg,
3367 nonce->x, nonce->len,
3368 additional_data->x, additional_data->len,
3369 input_data->x, input_data->len,
3370 output_data, output_size,
3371 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003372
Ronald Cron28a45ed2021-02-09 20:35:42 +01003373 /* If the operation is not supported, just skip and not fail in case the
3374 * encryption involves a common limitation of cryptography hardwares and
3375 * an alternative implementation. */
3376 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003377 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003378 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3379 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003380 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003381
3382 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003383 ASSERT_COMPARE( expected_result->x, expected_result->len,
3384 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003385
Gilles Peskinea1cac842018-06-11 19:33:02 +02003386exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003387 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003388 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003389 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390}
3391/* END_CASE */
3392
3393/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003394void aead_decrypt( int key_type_arg, data_t *key_data,
3395 int alg_arg,
3396 data_t *nonce,
3397 data_t *additional_data,
3398 data_t *input_data,
3399 data_t *expected_data,
3400 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003401{
Ronald Cron5425a212020-08-04 14:58:35 +02003402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403 psa_key_type_t key_type = key_type_arg;
3404 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003405 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406 unsigned char *output_data = NULL;
3407 size_t output_size = 0;
3408 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003410 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003411 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003412
Gilles Peskine8817f612018-12-18 00:18:46 +01003413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003415 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3416 psa_set_key_algorithm( &attributes, alg );
3417 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418
Gilles Peskine049c7532019-05-15 20:22:09 +02003419 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003420 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003421 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3422 key_bits = psa_get_key_bits( &attributes );
3423
3424 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3425 alg );
3426 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3427 expected_result != PSA_ERROR_NOT_SUPPORTED )
3428 {
3429 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3430 * should be exact. */
3431 TEST_EQUAL( output_size,
3432 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3433 TEST_ASSERT( output_size <=
3434 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3435 }
3436 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003437
Steven Cooremand588ea12021-01-11 19:36:04 +01003438 status = psa_aead_decrypt( key, alg,
3439 nonce->x, nonce->len,
3440 additional_data->x,
3441 additional_data->len,
3442 input_data->x, input_data->len,
3443 output_data, output_size,
3444 &output_length );
3445
Ronald Cron28a45ed2021-02-09 20:35:42 +01003446 /* If the operation is not supported, just skip and not fail in case the
3447 * decryption involves a common limitation of cryptography hardwares and
3448 * an alternative implementation. */
3449 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003450 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003451 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3452 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003453 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003454
3455 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456
Gilles Peskine2d277862018-06-18 15:41:12 +02003457 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003458 ASSERT_COMPARE( expected_data->x, expected_data->len,
3459 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003462 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003463 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003464 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465}
3466/* END_CASE */
3467
3468/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003469void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3470 int alg_arg,
3471 data_t *nonce,
3472 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003473 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003474 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003475 int do_test_data_chunked,
3476 int do_set_lengths,
3477 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003478{
Paul Elliottd3f82412021-06-16 16:52:21 +01003479 size_t ad_part_len = 0;
3480 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003481 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003482
Paul Elliotte64deda2021-09-09 14:07:23 +01003483 /* Ensure that either one part of the test or the other is done, i.e this
3484 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003485 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3486
3487 /* Temporary whilst we have algorithms that cannot support chunking */
3488 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003489 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003490 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3491 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003492 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003493 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003494
Paul Elliott33746aa2021-09-15 16:40:40 +01003495 if( do_set_lengths )
3496 {
3497 if( ad_part_len & 0x01 )
3498 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3499 else
3500 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3501 }
3502
Paul Elliott329d5382021-07-22 17:10:45 +01003503 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003504 if( !aead_multipart_internal_func( key_type_arg, key_data,
3505 alg_arg, nonce,
3506 additional_data,
3507 ad_part_len,
3508 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003509 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003510 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003511 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003512 break;
3513
3514 /* length(0) part, length(ad_part_len) part, length(0) part... */
3515 mbedtls_test_set_step( 1000 + ad_part_len );
3516
3517 if( !aead_multipart_internal_func( key_type_arg, key_data,
3518 alg_arg, nonce,
3519 additional_data,
3520 ad_part_len,
3521 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003522 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003523 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003524 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003525 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003526 }
3527 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003528
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003529 /* Temporary whilst we have algorithms that cannot support chunking */
3530 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003531 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003532 for( data_part_len = 1; data_part_len <= input_data->len;
3533 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003534 {
Paul Elliott329d5382021-07-22 17:10:45 +01003535 /* Split data into length(data_part_len) parts. */
3536 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003537
Paul Elliott33746aa2021-09-15 16:40:40 +01003538 if( do_set_lengths )
3539 {
3540 if( data_part_len & 0x01 )
3541 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3542 else
3543 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3544 }
3545
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003546 if( !aead_multipart_internal_func( key_type_arg, key_data,
3547 alg_arg, nonce,
3548 additional_data, -1,
3549 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003550 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003551 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003552 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003553 break;
3554
3555 /* length(0) part, length(data_part_len) part, length(0) part... */
3556 mbedtls_test_set_step( 3000 + data_part_len );
3557
3558 if( !aead_multipart_internal_func( key_type_arg, key_data,
3559 alg_arg, nonce,
3560 additional_data, -1,
3561 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003562 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003563 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003564 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003565 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003566 }
3567 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003568
Paul Elliott0023e0a2021-04-27 10:06:22 +01003569
Paul Elliott8fc45162021-06-23 16:06:01 +01003570 /* Goto is required to silence warnings about unused labels, as we
3571 * don't actually do any test assertions in this function. */
3572 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003573}
3574/* END_CASE */
3575
3576/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003577void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3578 int alg_arg,
3579 data_t *nonce,
3580 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003581 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003583 int do_test_data_chunked,
3584 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003585 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003586{
Paul Elliottd3f82412021-06-16 16:52:21 +01003587 size_t ad_part_len = 0;
3588 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003589 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003590
Paul Elliotte64deda2021-09-09 14:07:23 +01003591 /* Ensure that either one part of the test or the other is done, i.e this
3592 * test does something. */
3593 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3594
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003595 /* Temporary whilst we have algorithms that cannot support chunking */
3596 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003597 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003598 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3599 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003600 {
Paul Elliott329d5382021-07-22 17:10:45 +01003601 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003602 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003603
Paul Elliott33746aa2021-09-15 16:40:40 +01003604 if( do_set_lengths )
3605 {
3606 if( ad_part_len & 0x01 )
3607 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3608 else
3609 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3610 }
3611
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003612 if( !aead_multipart_internal_func( key_type_arg, key_data,
3613 alg_arg, nonce,
3614 additional_data,
3615 ad_part_len,
3616 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003617 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003618 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003619 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003620 break;
3621
3622 /* length(0) part, length(ad_part_len) part, length(0) part... */
3623 mbedtls_test_set_step( 1000 + ad_part_len );
3624
3625 if( !aead_multipart_internal_func( key_type_arg, key_data,
3626 alg_arg, nonce,
3627 additional_data,
3628 ad_part_len,
3629 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003630 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003631 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003632 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003633 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003634 }
3635 }
3636
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003637 /* Temporary whilst we have algorithms that cannot support chunking */
3638 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003639 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003640 for( data_part_len = 1; data_part_len <= input_data->len;
3641 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003642 {
Paul Elliott329d5382021-07-22 17:10:45 +01003643 /* Split data into length(data_part_len) parts. */
3644 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003645
Paul Elliott33746aa2021-09-15 16:40:40 +01003646 if( do_set_lengths )
3647 {
3648 if( data_part_len & 0x01 )
3649 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3650 else
3651 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3652 }
3653
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003654 if( !aead_multipart_internal_func( key_type_arg, key_data,
3655 alg_arg, nonce,
3656 additional_data, -1,
3657 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003658 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003659 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003660 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003661 break;
3662
3663 /* length(0) part, length(data_part_len) part, length(0) part... */
3664 mbedtls_test_set_step( 3000 + data_part_len );
3665
3666 if( !aead_multipart_internal_func( key_type_arg, key_data,
3667 alg_arg, nonce,
3668 additional_data, -1,
3669 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003670 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003671 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003672 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003673 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003674 }
3675 }
3676
Paul Elliott8fc45162021-06-23 16:06:01 +01003677 /* Goto is required to silence warnings about unused labels, as we
3678 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003679 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003680}
3681/* END_CASE */
3682
3683/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003684void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3685 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003686 int nonce_length,
3687 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003688 data_t *additional_data,
3689 data_t *input_data,
3690 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003691{
3692
3693 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3694 psa_key_type_t key_type = key_type_arg;
3695 psa_algorithm_t alg = alg_arg;
3696 psa_aead_operation_t operation;
3697 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3699 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003700 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003701 size_t actual_nonce_length = 0;
3702 size_t expected_nonce_length = expected_nonce_length_arg;
3703 unsigned char *output = NULL;
3704 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003705 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003706 size_t ciphertext_size = 0;
3707 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003708 size_t tag_length = 0;
3709 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003710
3711 PSA_ASSERT( psa_crypto_init( ) );
3712
3713 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3714 psa_set_key_algorithm( & attributes, alg );
3715 psa_set_key_type( & attributes, key_type );
3716
3717 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3718 &key ) );
3719
3720 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3721
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003722 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3723
Paul Elliottf1277632021-08-24 18:11:37 +01003724 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003725
Paul Elliottf1277632021-08-24 18:11:37 +01003726 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003727
Paul Elliottf1277632021-08-24 18:11:37 +01003728 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003729
Paul Elliottf1277632021-08-24 18:11:37 +01003730 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003731
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003732 operation = psa_aead_operation_init( );
3733
3734 status = psa_aead_encrypt_setup( &operation, key, alg );
3735
3736 /* If the operation is not supported, just skip and not fail in case the
3737 * encryption involves a common limitation of cryptography hardwares and
3738 * an alternative implementation. */
3739 if( status == PSA_ERROR_NOT_SUPPORTED )
3740 {
3741 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003742 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003743 }
3744
3745 PSA_ASSERT( status );
3746
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003747 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003748 nonce_length,
3749 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003750
Paul Elliott693bf312021-07-23 17:40:41 +01003751 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003752
Paul Elliottf1277632021-08-24 18:11:37 +01003753 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003754
Paul Elliottf1277632021-08-24 18:11:37 +01003755 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003756
Paul Elliott693bf312021-07-23 17:40:41 +01003757 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003758 {
3759
3760 /* Ensure we can still complete operation. */
3761
3762 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3763 additional_data->len ) );
3764
3765 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003766 output, output_size,
3767 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003768
Paul Elliottf1277632021-08-24 18:11:37 +01003769 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3770 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003771 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3772 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003773
3774exit:
3775 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003776 mbedtls_free( output );
3777 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003778 psa_aead_abort( &operation );
3779 PSA_DONE( );
3780}
3781/* END_CASE */
3782
3783/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003784void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3785 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003786 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003787 data_t *additional_data,
3788 data_t *input_data,
3789 int expected_status_arg )
3790{
3791
3792 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3793 psa_key_type_t key_type = key_type_arg;
3794 psa_algorithm_t alg = alg_arg;
3795 psa_aead_operation_t operation;
3796 uint8_t *nonce_buffer = NULL;
3797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3798 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3799 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003800 unsigned char *output = NULL;
3801 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003802 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003803 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003804 size_t ciphertext_size = 0;
3805 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003806 size_t tag_length = 0;
3807 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003808 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003809
3810 PSA_ASSERT( psa_crypto_init( ) );
3811
3812 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3813 psa_set_key_algorithm( &attributes, alg );
3814 psa_set_key_type( &attributes, key_type );
3815
3816 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3817 &key ) );
3818
3819 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3820
3821 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3822
Paul Elliott6f0e7202021-08-25 12:57:18 +01003823 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003824
Paul Elliott6f0e7202021-08-25 12:57:18 +01003825 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003826
Paul Elliott6f0e7202021-08-25 12:57:18 +01003827 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003828
Paul Elliott6f0e7202021-08-25 12:57:18 +01003829 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003830
3831 operation = psa_aead_operation_init( );
3832
3833 status = psa_aead_encrypt_setup( &operation, key, alg );
3834
3835 /* If the operation is not supported, just skip and not fail in case the
3836 * encryption involves a common limitation of cryptography hardwares and
3837 * an alternative implementation. */
3838 if( status == PSA_ERROR_NOT_SUPPORTED )
3839 {
3840 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003841 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003842 }
3843
3844 PSA_ASSERT( status );
3845
Paul Elliott4023ffd2021-09-10 16:21:22 +01003846 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3847 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003848 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003849 /* Arbitrary size buffer, to test zero length valid buffer. */
3850 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003851 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003852 }
3853 else
3854 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003855 /* If length is zero, then this will return NULL. */
3856 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003857 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003858
Paul Elliott4023ffd2021-09-10 16:21:22 +01003859 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003860 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003861 for( index = 0; index < nonce_length - 1; ++index )
3862 {
3863 nonce_buffer[index] = 'a' + index;
3864 }
Paul Elliott66696b52021-08-16 18:42:41 +01003865 }
Paul Elliott863864a2021-07-23 17:28:31 +01003866 }
3867
Paul Elliott6f0e7202021-08-25 12:57:18 +01003868 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003869
Paul Elliott693bf312021-07-23 17:40:41 +01003870 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003871
3872 if( expected_status == PSA_SUCCESS )
3873 {
3874 /* Ensure we can still complete operation. */
3875
3876 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3877 additional_data->len ) );
3878
3879 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003880 output, output_size,
3881 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003882
Paul Elliott6f0e7202021-08-25 12:57:18 +01003883 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3884 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003885 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3886 }
3887
3888exit:
3889 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003890 mbedtls_free( output );
3891 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003892 mbedtls_free( nonce_buffer );
3893 psa_aead_abort( &operation );
3894 PSA_DONE( );
3895}
3896/* END_CASE */
3897
3898/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003899void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3900 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003901 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003902 data_t *nonce,
3903 data_t *additional_data,
3904 data_t *input_data,
3905 int expected_status_arg )
3906{
3907
3908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3909 psa_key_type_t key_type = key_type_arg;
3910 psa_algorithm_t alg = alg_arg;
3911 psa_aead_operation_t operation;
3912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3913 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3914 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003915 unsigned char *output = NULL;
3916 unsigned char *ciphertext = NULL;
3917 size_t output_size = output_size_arg;
3918 size_t ciphertext_size = 0;
3919 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003920 size_t tag_length = 0;
3921 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3922
3923 PSA_ASSERT( psa_crypto_init( ) );
3924
3925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3926 psa_set_key_algorithm( &attributes, alg );
3927 psa_set_key_type( &attributes, key_type );
3928
3929 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3930 &key ) );
3931
3932 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3933
Paul Elliottc6d11d02021-09-01 12:04:23 +01003934 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003935
Paul Elliottc6d11d02021-09-01 12:04:23 +01003936 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003937
Paul Elliottc6d11d02021-09-01 12:04:23 +01003938 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003939
3940 operation = psa_aead_operation_init( );
3941
3942 status = psa_aead_encrypt_setup( &operation, key, alg );
3943
3944 /* If the operation is not supported, just skip and not fail in case the
3945 * encryption involves a common limitation of cryptography hardwares and
3946 * an alternative implementation. */
3947 if( status == PSA_ERROR_NOT_SUPPORTED )
3948 {
3949 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3950 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3951 }
3952
3953 PSA_ASSERT( status );
3954
3955 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3956
3957 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3958 additional_data->len ) );
3959
3960 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003961 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003962
3963 TEST_EQUAL( status, expected_status );
3964
3965 if( expected_status == PSA_SUCCESS )
3966 {
3967 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003968 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3969 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003970 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3971 }
3972
3973exit:
3974 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003975 mbedtls_free( output );
3976 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003977 psa_aead_abort( &operation );
3978 PSA_DONE( );
3979}
3980/* END_CASE */
3981
Paul Elliott91b021e2021-07-23 18:52:31 +01003982/* BEGIN_CASE */
3983void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3984 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003985 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003986 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003987 data_t *nonce,
3988 data_t *additional_data,
3989 data_t *input_data,
3990 int expected_status_arg )
3991{
3992
3993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3994 psa_key_type_t key_type = key_type_arg;
3995 psa_algorithm_t alg = alg_arg;
3996 psa_aead_operation_t operation;
3997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3998 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3999 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004000 unsigned char *ciphertext = NULL;
4001 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004002 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004003 size_t ciphertext_size = 0;
4004 size_t ciphertext_length = 0;
4005 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004006 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004007 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004008
4009 PSA_ASSERT( psa_crypto_init( ) );
4010
4011 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4012 psa_set_key_algorithm( &attributes, alg );
4013 psa_set_key_type( &attributes, key_type );
4014
4015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4016 &key ) );
4017
4018 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4019
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004020 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004021
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004022 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004023
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004024 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004025
Paul Elliott719c1322021-09-13 18:27:22 +01004026 ASSERT_ALLOC( tag_buffer, tag_size );
4027
Paul Elliott91b021e2021-07-23 18:52:31 +01004028 operation = psa_aead_operation_init( );
4029
4030 status = psa_aead_encrypt_setup( &operation, key, alg );
4031
4032 /* If the operation is not supported, just skip and not fail in case the
4033 * encryption involves a common limitation of cryptography hardwares and
4034 * an alternative implementation. */
4035 if( status == PSA_ERROR_NOT_SUPPORTED )
4036 {
4037 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4038 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4039 }
4040
4041 PSA_ASSERT( status );
4042
4043 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4044
4045 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4046 additional_data->len ) );
4047
4048 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004049 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004050
4051 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004052 status = psa_aead_finish( &operation, finish_ciphertext,
4053 finish_ciphertext_size,
4054 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004055 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004056
4057 TEST_EQUAL( status, expected_status );
4058
4059exit:
4060 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004061 mbedtls_free( ciphertext );
4062 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004063 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004064 psa_aead_abort( &operation );
4065 PSA_DONE( );
4066}
4067/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004068
4069/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004070void aead_multipart_verify( int key_type_arg, data_t *key_data,
4071 int alg_arg,
4072 data_t *nonce,
4073 data_t *additional_data,
4074 data_t *input_data,
4075 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004076 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004077 int expected_status_arg )
4078{
4079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4080 psa_key_type_t key_type = key_type_arg;
4081 psa_algorithm_t alg = alg_arg;
4082 psa_aead_operation_t operation;
4083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4084 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4085 psa_status_t expected_status = expected_status_arg;
4086 unsigned char *plaintext = NULL;
4087 unsigned char *finish_plaintext = NULL;
4088 size_t plaintext_size = 0;
4089 size_t plaintext_length = 0;
4090 size_t verify_plaintext_size = 0;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004091 tagusage_method tag_usage = tag_usage_arg;
4092 unsigned char *tag_buffer = NULL;
4093 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004094
4095 PSA_ASSERT( psa_crypto_init( ) );
4096
4097 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4098 psa_set_key_algorithm( &attributes, alg );
4099 psa_set_key_type( &attributes, key_type );
4100
4101 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4102 &key ) );
4103
4104 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4105
4106 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4107 input_data->len );
4108
4109 ASSERT_ALLOC( plaintext, plaintext_size );
4110
4111 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4112
4113 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4114
4115 operation = psa_aead_operation_init( );
4116
4117 status = psa_aead_decrypt_setup( &operation, key, alg );
4118
4119 /* If the operation is not supported, just skip and not fail in case the
4120 * encryption involves a common limitation of cryptography hardwares and
4121 * an alternative implementation. */
4122 if( status == PSA_ERROR_NOT_SUPPORTED )
4123 {
4124 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4125 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4126 }
4127
4128 PSA_ASSERT( status );
4129
4130 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4131
4132 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4133 additional_data->len ) );
4134
4135 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4136 input_data->len,
4137 plaintext, plaintext_size,
4138 &plaintext_length ) );
4139
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004140 if( tag_usage == USE_GIVEN_TAG )
4141 {
4142 tag_buffer = tag->x;
4143 tag_size = tag->len;
4144 }
4145
Paul Elliott9961a662021-09-17 19:19:02 +01004146 status = psa_aead_verify( &operation, finish_plaintext,
4147 verify_plaintext_size,
4148 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004149 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004150
4151 TEST_EQUAL( status, expected_status );
4152
4153exit:
4154 psa_destroy_key( key );
4155 mbedtls_free( plaintext );
4156 mbedtls_free( finish_plaintext );
4157 psa_aead_abort( &operation );
4158 PSA_DONE( );
4159}
4160/* END_CASE */
4161
Paul Elliott9961a662021-09-17 19:19:02 +01004162/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004163void aead_multipart_setup( int key_type_arg, data_t *key_data,
4164 int alg_arg, int expected_status_arg )
4165{
4166 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4167 psa_key_type_t key_type = key_type_arg;
4168 psa_algorithm_t alg = alg_arg;
4169 psa_aead_operation_t operation;
4170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4171 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4172 psa_status_t expected_status = expected_status_arg;
4173
4174 PSA_ASSERT( psa_crypto_init( ) );
4175
4176 psa_set_key_usage_flags( &attributes,
4177 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4178 psa_set_key_algorithm( &attributes, alg );
4179 psa_set_key_type( &attributes, key_type );
4180
4181 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4182 &key ) );
4183
Paul Elliott64555bd2021-09-20 16:44:44 +01004184 operation = psa_aead_operation_init( );
4185
Paul Elliott5221ef62021-09-19 17:33:03 +01004186 mbedtls_test_set_step( 0 );
4187
4188 status = psa_aead_encrypt_setup( &operation, key, alg );
4189
4190 TEST_EQUAL( status, expected_status );
4191
4192 psa_aead_abort( &operation );
4193
4194 operation = psa_aead_operation_init( );
4195
4196 mbedtls_test_set_step( 1 );
4197
4198 status = psa_aead_decrypt_setup( &operation, key, alg );
4199
4200 TEST_EQUAL(status, expected_status );
4201
4202exit:
4203 psa_destroy_key( key );
4204 psa_aead_abort( &operation );
4205 PSA_DONE( );
4206}
4207/* END_CASE */
4208
4209/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004210void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4211 int alg_arg,
4212 data_t *nonce,
4213 data_t *additional_data,
4214 data_t *input_data )
4215{
4216 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4217 psa_key_type_t key_type = key_type_arg;
4218 psa_algorithm_t alg = alg_arg;
4219 psa_aead_operation_t operation;
4220 unsigned char *output_data = NULL;
4221 unsigned char *final_data = NULL;
4222 size_t output_size = 0;
4223 size_t finish_output_size = 0;
4224 size_t output_length = 0;
4225 size_t key_bits = 0;
4226 size_t tag_length = 0;
4227 size_t tag_size = 0;
4228 size_t nonce_length = 0;
4229 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4230 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4231 size_t output_part_length = 0;
4232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4233
4234 PSA_ASSERT( psa_crypto_init( ) );
4235
4236 psa_set_key_usage_flags( & attributes,
4237 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4238 psa_set_key_algorithm( & attributes, alg );
4239 psa_set_key_type( & attributes, key_type );
4240
4241 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4242 &key ) );
4243
4244 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4245 key_bits = psa_get_key_bits( &attributes );
4246
4247 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4248
4249 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4250
4251 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4252
4253 ASSERT_ALLOC( output_data, output_size );
4254
4255 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4256
4257 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4258
4259 ASSERT_ALLOC( final_data, finish_output_size );
4260
4261 /* Test all operations error without calling setup first. */
4262
4263 operation = psa_aead_operation_init( );
4264
4265 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4266 PSA_ERROR_BAD_STATE );
4267
4268 psa_aead_abort( &operation );
4269
4270 operation = psa_aead_operation_init( );
4271
4272 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4273 PSA_AEAD_NONCE_MAX_SIZE,
4274 &nonce_length ),
4275 PSA_ERROR_BAD_STATE );
4276
4277 psa_aead_abort( &operation );
4278
Paul Elliott481be342021-07-16 17:38:47 +01004279 /* ------------------------------------------------------- */
4280
Paul Elliottc23a9a02021-06-21 18:32:46 +01004281 operation = psa_aead_operation_init( );
4282
4283 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4284 input_data->len ),
4285 PSA_ERROR_BAD_STATE );
4286
4287 psa_aead_abort( &operation );
4288
Paul Elliott481be342021-07-16 17:38:47 +01004289 /* ------------------------------------------------------- */
4290
Paul Elliottc23a9a02021-06-21 18:32:46 +01004291 operation = psa_aead_operation_init( );
4292
4293 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4294 additional_data->len ),
4295 PSA_ERROR_BAD_STATE );
4296
4297 psa_aead_abort( &operation );
4298
Paul Elliott481be342021-07-16 17:38:47 +01004299 /* ------------------------------------------------------- */
4300
Paul Elliottc23a9a02021-06-21 18:32:46 +01004301 operation = psa_aead_operation_init( );
4302
4303 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4304 input_data->len, output_data,
4305 output_size, &output_length ),
4306 PSA_ERROR_BAD_STATE );
4307
4308 psa_aead_abort( &operation );
4309
Paul Elliott481be342021-07-16 17:38:47 +01004310 /* ------------------------------------------------------- */
4311
Paul Elliottc23a9a02021-06-21 18:32:46 +01004312 operation = psa_aead_operation_init( );
4313
4314 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4315 finish_output_size,
4316 &output_part_length,
4317 tag_buffer, tag_length,
4318 &tag_size ),
4319 PSA_ERROR_BAD_STATE );
4320
4321 psa_aead_abort( &operation );
4322
Paul Elliott481be342021-07-16 17:38:47 +01004323 /* ------------------------------------------------------- */
4324
Paul Elliottc23a9a02021-06-21 18:32:46 +01004325 operation = psa_aead_operation_init( );
4326
4327 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4328 finish_output_size,
4329 &output_part_length,
4330 tag_buffer,
4331 tag_length ),
4332 PSA_ERROR_BAD_STATE );
4333
4334 psa_aead_abort( &operation );
4335
4336 /* Test for double setups. */
4337
4338 operation = psa_aead_operation_init( );
4339
4340 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4341
4342 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4343 PSA_ERROR_BAD_STATE );
4344
4345 psa_aead_abort( &operation );
4346
Paul Elliott481be342021-07-16 17:38:47 +01004347 /* ------------------------------------------------------- */
4348
Paul Elliottc23a9a02021-06-21 18:32:46 +01004349 operation = psa_aead_operation_init( );
4350
4351 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4352
4353 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4354 PSA_ERROR_BAD_STATE );
4355
4356 psa_aead_abort( &operation );
4357
Paul Elliott374a2be2021-07-16 17:53:40 +01004358 /* ------------------------------------------------------- */
4359
4360 operation = psa_aead_operation_init( );
4361
4362 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4363
4364 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4365 PSA_ERROR_BAD_STATE );
4366
4367 psa_aead_abort( &operation );
4368
4369 /* ------------------------------------------------------- */
4370
4371 operation = psa_aead_operation_init( );
4372
4373 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4374
4375 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4376 PSA_ERROR_BAD_STATE );
4377
4378 psa_aead_abort( &operation );
4379
Paul Elliottc23a9a02021-06-21 18:32:46 +01004380 /* Test for not setting a nonce. */
4381
4382 operation = psa_aead_operation_init( );
4383
4384 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4385
4386 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4387 additional_data->len ),
4388 PSA_ERROR_BAD_STATE );
4389
4390 psa_aead_abort( &operation );
4391
Paul Elliott7f628422021-09-01 12:08:29 +01004392 /* ------------------------------------------------------- */
4393
4394 operation = psa_aead_operation_init( );
4395
4396 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4397
4398 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4399 input_data->len, output_data,
4400 output_size, &output_length ),
4401 PSA_ERROR_BAD_STATE );
4402
4403 psa_aead_abort( &operation );
4404
Paul Elliottbdc2c682021-09-21 18:37:10 +01004405 /* ------------------------------------------------------- */
4406
4407 operation = psa_aead_operation_init( );
4408
4409 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4410
4411 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4412 finish_output_size,
4413 &output_part_length,
4414 tag_buffer, tag_length,
4415 &tag_size ),
4416 PSA_ERROR_BAD_STATE );
4417
4418 psa_aead_abort( &operation );
4419
4420 /* ------------------------------------------------------- */
4421
4422 operation = psa_aead_operation_init( );
4423
4424 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4425
4426 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4427 finish_output_size,
4428 &output_part_length,
4429 tag_buffer,
4430 tag_length ),
4431 PSA_ERROR_BAD_STATE );
4432
4433 psa_aead_abort( &operation );
4434
Paul Elliottc23a9a02021-06-21 18:32:46 +01004435 /* Test for double setting nonce. */
4436
4437 operation = psa_aead_operation_init( );
4438
4439 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4440
4441 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4442
4443 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4444 PSA_ERROR_BAD_STATE );
4445
4446 psa_aead_abort( &operation );
4447
Paul Elliott374a2be2021-07-16 17:53:40 +01004448 /* Test for double generating nonce. */
4449
4450 operation = psa_aead_operation_init( );
4451
4452 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4453
4454 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4455 PSA_AEAD_NONCE_MAX_SIZE,
4456 &nonce_length ) );
4457
4458 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4459 PSA_AEAD_NONCE_MAX_SIZE,
4460 &nonce_length ),
4461 PSA_ERROR_BAD_STATE );
4462
4463
4464 psa_aead_abort( &operation );
4465
4466 /* Test for generate nonce then set and vice versa */
4467
4468 operation = psa_aead_operation_init( );
4469
4470 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4471
4472 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4473 PSA_AEAD_NONCE_MAX_SIZE,
4474 &nonce_length ) );
4475
4476 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4477 PSA_ERROR_BAD_STATE );
4478
4479 psa_aead_abort( &operation );
4480
4481 /* ------------------------------------------------------- */
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 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4490 PSA_AEAD_NONCE_MAX_SIZE,
4491 &nonce_length ),
4492 PSA_ERROR_BAD_STATE );
4493
4494 psa_aead_abort( &operation );
4495
Paul Elliott7220cae2021-06-22 17:25:57 +01004496 /* Test for generating nonce in decrypt setup. */
4497
4498 operation = psa_aead_operation_init( );
4499
4500 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4501
4502 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4503 PSA_AEAD_NONCE_MAX_SIZE,
4504 &nonce_length ),
4505 PSA_ERROR_BAD_STATE );
4506
4507 psa_aead_abort( &operation );
4508
Paul Elliottc23a9a02021-06-21 18:32:46 +01004509 /* Test for setting lengths twice. */
4510
4511 operation = psa_aead_operation_init( );
4512
4513 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4514
4515 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4516
4517 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4518 input_data->len ) );
4519
4520 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4521 input_data->len ),
4522 PSA_ERROR_BAD_STATE );
4523
4524 psa_aead_abort( &operation );
4525
4526 /* Test for setting lengths after already starting data. */
4527
4528 operation = psa_aead_operation_init( );
4529
4530 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4531
4532 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4533
Paul Elliottf94bd992021-09-19 18:15:59 +01004534 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4535 additional_data->len ) );
4536
4537 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4538 input_data->len ),
4539 PSA_ERROR_BAD_STATE );
4540
4541 psa_aead_abort( &operation );
4542
4543 /* ------------------------------------------------------- */
4544
4545 operation = psa_aead_operation_init( );
4546
4547 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4548
4549 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4550
Paul Elliottc23a9a02021-06-21 18:32:46 +01004551 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4552 input_data->len, output_data,
4553 output_size, &output_length ) );
4554
4555 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4556 input_data->len ),
4557 PSA_ERROR_BAD_STATE );
4558
4559 psa_aead_abort( &operation );
4560
Paul Elliott243080c2021-07-21 19:01:17 +01004561 /* Test for not sending any additional data or data after setting non zero
4562 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004563
4564 operation = psa_aead_operation_init( );
4565
4566 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4567
4568 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4569
4570 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4571 input_data->len ) );
4572
4573 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4574 finish_output_size,
4575 &output_part_length,
4576 tag_buffer, tag_length,
4577 &tag_size ),
4578 PSA_ERROR_INVALID_ARGUMENT );
4579
4580 psa_aead_abort( &operation );
4581
Paul Elliott243080c2021-07-21 19:01:17 +01004582 /* Test for not sending any additional data or data after setting non-zero
4583 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004584
4585 operation = psa_aead_operation_init( );
4586
4587 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4588
4589 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4590
4591 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4592 input_data->len ) );
4593
4594 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4595 finish_output_size,
4596 &output_part_length,
4597 tag_buffer,
4598 tag_length ),
4599 PSA_ERROR_INVALID_ARGUMENT );
4600
4601 psa_aead_abort( &operation );
4602
Paul Elliott243080c2021-07-21 19:01:17 +01004603 /* Test for not sending any additional data after setting a non-zero length
4604 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004605
4606 operation = psa_aead_operation_init( );
4607
4608 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4609
4610 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4611
4612 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4613 input_data->len ) );
4614
4615 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4616 input_data->len, output_data,
4617 output_size, &output_length ),
4618 PSA_ERROR_INVALID_ARGUMENT );
4619
4620 psa_aead_abort( &operation );
4621
Paul Elliottf94bd992021-09-19 18:15:59 +01004622 /* Test for not sending any data after setting a non-zero length for it.*/
4623
4624 operation = psa_aead_operation_init( );
4625
4626 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4627
4628 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4629
4630 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4631 input_data->len ) );
4632
4633 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4634 additional_data->len ) );
4635
4636 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4637 finish_output_size,
4638 &output_part_length,
4639 tag_buffer, tag_length,
4640 &tag_size ),
4641 PSA_ERROR_INVALID_ARGUMENT );
4642
4643 psa_aead_abort( &operation );
4644
Paul Elliottb0450fe2021-09-01 15:06:26 +01004645 /* Test for sending too much additional data after setting lengths. */
4646
4647 operation = psa_aead_operation_init( );
4648
4649 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4650
4651 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4652
4653 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4654
4655
4656 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4657 additional_data->len ),
4658 PSA_ERROR_INVALID_ARGUMENT );
4659
4660 psa_aead_abort( &operation );
4661
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004662 operation = psa_aead_operation_init( );
4663
4664 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4665
4666 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4667
4668 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4669 input_data->len ) );
4670
4671 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4672 additional_data->len ) );
4673
4674 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4675 1 ),
4676 PSA_ERROR_INVALID_ARGUMENT );
4677
4678 psa_aead_abort( &operation );
4679
Paul Elliottb0450fe2021-09-01 15:06:26 +01004680 /* Test for sending too much data after setting lengths. */
4681
4682 operation = psa_aead_operation_init( );
4683
4684 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4685
4686 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4687
4688 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4689
4690 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4691 input_data->len, output_data,
4692 output_size, &output_length ),
4693 PSA_ERROR_INVALID_ARGUMENT );
4694
4695 psa_aead_abort( &operation );
4696
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004697 operation = psa_aead_operation_init( );
4698
4699 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4700
4701 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4702
4703 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4704 input_data->len ) );
4705
4706 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4707 additional_data->len ) );
4708
4709 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4710 input_data->len, output_data,
4711 output_size, &output_length ) );
4712
4713 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4714 1, output_data,
4715 output_size, &output_length ),
4716 PSA_ERROR_INVALID_ARGUMENT );
4717
4718 psa_aead_abort( &operation );
4719
Paul Elliottc23a9a02021-06-21 18:32:46 +01004720 /* Test sending additional data after data. */
4721
4722 operation = psa_aead_operation_init( );
4723
4724 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4725
4726 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4727
4728 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4729 input_data->len, output_data,
4730 output_size, &output_length ) );
4731
4732 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4733 additional_data->len ),
4734 PSA_ERROR_BAD_STATE );
4735
4736 psa_aead_abort( &operation );
4737
Paul Elliott534d0b42021-06-22 19:15:20 +01004738 /* Test calling finish on decryption. */
4739
4740 operation = psa_aead_operation_init( );
4741
4742 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4743
4744 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4745
4746 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4747 finish_output_size,
4748 &output_part_length,
4749 tag_buffer, tag_length,
4750 &tag_size ),
4751 PSA_ERROR_BAD_STATE );
4752
4753 psa_aead_abort( &operation );
4754
4755 /* Test calling verify on encryption. */
4756
4757 operation = psa_aead_operation_init( );
4758
4759 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4760
4761 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4762
4763 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4764 finish_output_size,
4765 &output_part_length,
4766 tag_buffer,
4767 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004768 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004769
4770 psa_aead_abort( &operation );
4771
4772
Paul Elliottc23a9a02021-06-21 18:32:46 +01004773exit:
4774 psa_destroy_key( key );
4775 psa_aead_abort( &operation );
4776 mbedtls_free( output_data );
4777 mbedtls_free( final_data );
4778 PSA_DONE( );
4779}
4780/* END_CASE */
4781
4782/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004783void signature_size( int type_arg,
4784 int bits,
4785 int alg_arg,
4786 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004787{
4788 psa_key_type_t type = type_arg;
4789 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004790 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004791
Gilles Peskinefe11b722018-12-18 00:24:04 +01004792 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004793
Gilles Peskinee59236f2018-01-27 23:32:46 +01004794exit:
4795 ;
4796}
4797/* END_CASE */
4798
4799/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004800void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4801 int alg_arg, data_t *input_data,
4802 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004803{
Ronald Cron5425a212020-08-04 14:58:35 +02004804 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004805 psa_key_type_t key_type = key_type_arg;
4806 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004807 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004808 unsigned char *signature = NULL;
4809 size_t signature_size;
4810 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004812
Gilles Peskine8817f612018-12-18 00:18:46 +01004813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004814
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004815 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004816 psa_set_key_algorithm( &attributes, alg );
4817 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004818
Gilles Peskine049c7532019-05-15 20:22:09 +02004819 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004820 &key ) );
4821 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004822 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004823
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004824 /* Allocate a buffer which has the size advertized by the
4825 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004826 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004827 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004828 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004829 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004830 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004831
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004832 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004833 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004834 input_data->x, input_data->len,
4835 signature, signature_size,
4836 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004837 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004838 ASSERT_COMPARE( output_data->x, output_data->len,
4839 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004840
4841exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004842 /*
4843 * Key attributes may have been returned by psa_get_key_attributes()
4844 * thus reset them as required.
4845 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004846 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004847
Ronald Cron5425a212020-08-04 14:58:35 +02004848 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004849 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004850 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004851}
4852/* END_CASE */
4853
4854/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004855void sign_hash_fail( int key_type_arg, data_t *key_data,
4856 int alg_arg, data_t *input_data,
4857 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004858{
Ronald Cron5425a212020-08-04 14:58:35 +02004859 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004860 psa_key_type_t key_type = key_type_arg;
4861 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004862 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004863 psa_status_t actual_status;
4864 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004865 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004866 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004868
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004869 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004870
Gilles Peskine8817f612018-12-18 00:18:46 +01004871 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004872
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004873 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004874 psa_set_key_algorithm( &attributes, alg );
4875 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004876
Gilles Peskine049c7532019-05-15 20:22:09 +02004877 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004878 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004879
Ronald Cron5425a212020-08-04 14:58:35 +02004880 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004881 input_data->x, input_data->len,
4882 signature, signature_size,
4883 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004884 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004885 /* The value of *signature_length is unspecified on error, but
4886 * whatever it is, it should be less than signature_size, so that
4887 * if the caller tries to read *signature_length bytes without
4888 * checking the error code then they don't overflow a buffer. */
4889 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004890
4891exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004892 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004893 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004894 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004895 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004896}
4897/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004898
4899/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004900void sign_verify_hash( int key_type_arg, data_t *key_data,
4901 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004902{
Ronald Cron5425a212020-08-04 14:58:35 +02004903 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004904 psa_key_type_t key_type = key_type_arg;
4905 psa_algorithm_t alg = alg_arg;
4906 size_t key_bits;
4907 unsigned char *signature = NULL;
4908 size_t signature_size;
4909 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004911
Gilles Peskine8817f612018-12-18 00:18:46 +01004912 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004913
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004914 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004915 psa_set_key_algorithm( &attributes, alg );
4916 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004917
Gilles Peskine049c7532019-05-15 20:22:09 +02004918 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004919 &key ) );
4920 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004921 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004922
4923 /* Allocate a buffer which has the size advertized by the
4924 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004925 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004926 key_bits, alg );
4927 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004928 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004929 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004930
4931 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004932 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004933 input_data->x, input_data->len,
4934 signature, signature_size,
4935 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004936 /* Check that the signature length looks sensible. */
4937 TEST_ASSERT( signature_length <= signature_size );
4938 TEST_ASSERT( signature_length > 0 );
4939
4940 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004941 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004942 input_data->x, input_data->len,
4943 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004944
4945 if( input_data->len != 0 )
4946 {
4947 /* Flip a bit in the input and verify that the signature is now
4948 * detected as invalid. Flip a bit at the beginning, not at the end,
4949 * because ECDSA may ignore the last few bits of the input. */
4950 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004951 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004952 input_data->x, input_data->len,
4953 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004954 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004955 }
4956
4957exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004958 /*
4959 * Key attributes may have been returned by psa_get_key_attributes()
4960 * thus reset them as required.
4961 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004962 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004963
Ronald Cron5425a212020-08-04 14:58:35 +02004964 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004965 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004966 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004967}
4968/* END_CASE */
4969
4970/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004971void verify_hash( int key_type_arg, data_t *key_data,
4972 int alg_arg, data_t *hash_data,
4973 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004974{
Ronald Cron5425a212020-08-04 14:58:35 +02004975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004976 psa_key_type_t key_type = key_type_arg;
4977 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004978 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004979
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004980 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004981
Gilles Peskine8817f612018-12-18 00:18:46 +01004982 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004983
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004984 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004985 psa_set_key_algorithm( &attributes, alg );
4986 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004987
Gilles Peskine049c7532019-05-15 20:22:09 +02004988 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004989 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004990
Ronald Cron5425a212020-08-04 14:58:35 +02004991 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004992 hash_data->x, hash_data->len,
4993 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004994
itayzafrir5c753392018-05-08 11:18:38 +03004995exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004996 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004997 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004998 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004999}
5000/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005001
5002/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005003void verify_hash_fail( int key_type_arg, data_t *key_data,
5004 int alg_arg, data_t *hash_data,
5005 data_t *signature_data,
5006 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005007{
Ronald Cron5425a212020-08-04 14:58:35 +02005008 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005009 psa_key_type_t key_type = key_type_arg;
5010 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005011 psa_status_t actual_status;
5012 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005014
Gilles Peskine8817f612018-12-18 00:18:46 +01005015 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005016
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005017 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005018 psa_set_key_algorithm( &attributes, alg );
5019 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005020
Gilles Peskine049c7532019-05-15 20:22:09 +02005021 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005022 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005023
Ronald Cron5425a212020-08-04 14:58:35 +02005024 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005025 hash_data->x, hash_data->len,
5026 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005027 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005028
5029exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005030 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005031 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005032 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005033}
5034/* END_CASE */
5035
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005036/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005037void sign_message_deterministic( int key_type_arg,
5038 data_t *key_data,
5039 int alg_arg,
5040 data_t *input_data,
5041 data_t *output_data )
5042{
5043 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5044 psa_key_type_t key_type = key_type_arg;
5045 psa_algorithm_t alg = alg_arg;
5046 size_t key_bits;
5047 unsigned char *signature = NULL;
5048 size_t signature_size;
5049 size_t signature_length = 0xdeadbeef;
5050 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5051
5052 PSA_ASSERT( psa_crypto_init( ) );
5053
5054 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5055 psa_set_key_algorithm( &attributes, alg );
5056 psa_set_key_type( &attributes, key_type );
5057
5058 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5059 &key ) );
5060 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5061 key_bits = psa_get_key_bits( &attributes );
5062
5063 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5064 TEST_ASSERT( signature_size != 0 );
5065 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5066 ASSERT_ALLOC( signature, signature_size );
5067
5068 PSA_ASSERT( psa_sign_message( key, alg,
5069 input_data->x, input_data->len,
5070 signature, signature_size,
5071 &signature_length ) );
5072
5073 ASSERT_COMPARE( output_data->x, output_data->len,
5074 signature, signature_length );
5075
5076exit:
5077 psa_reset_key_attributes( &attributes );
5078
5079 psa_destroy_key( key );
5080 mbedtls_free( signature );
5081 PSA_DONE( );
5082
5083}
5084/* END_CASE */
5085
5086/* BEGIN_CASE */
5087void sign_message_fail( int key_type_arg,
5088 data_t *key_data,
5089 int alg_arg,
5090 data_t *input_data,
5091 int signature_size_arg,
5092 int expected_status_arg )
5093{
5094 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5095 psa_key_type_t key_type = key_type_arg;
5096 psa_algorithm_t alg = alg_arg;
5097 size_t signature_size = signature_size_arg;
5098 psa_status_t actual_status;
5099 psa_status_t expected_status = expected_status_arg;
5100 unsigned char *signature = NULL;
5101 size_t signature_length = 0xdeadbeef;
5102 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5103
5104 ASSERT_ALLOC( signature, signature_size );
5105
5106 PSA_ASSERT( psa_crypto_init( ) );
5107
5108 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5109 psa_set_key_algorithm( &attributes, alg );
5110 psa_set_key_type( &attributes, key_type );
5111
5112 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5113 &key ) );
5114
5115 actual_status = psa_sign_message( key, alg,
5116 input_data->x, input_data->len,
5117 signature, signature_size,
5118 &signature_length );
5119 TEST_EQUAL( actual_status, expected_status );
5120 /* The value of *signature_length is unspecified on error, but
5121 * whatever it is, it should be less than signature_size, so that
5122 * if the caller tries to read *signature_length bytes without
5123 * checking the error code then they don't overflow a buffer. */
5124 TEST_ASSERT( signature_length <= signature_size );
5125
5126exit:
5127 psa_reset_key_attributes( &attributes );
5128 psa_destroy_key( key );
5129 mbedtls_free( signature );
5130 PSA_DONE( );
5131}
5132/* END_CASE */
5133
5134/* BEGIN_CASE */
5135void sign_verify_message( int key_type_arg,
5136 data_t *key_data,
5137 int alg_arg,
5138 data_t *input_data )
5139{
5140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5141 psa_key_type_t key_type = key_type_arg;
5142 psa_algorithm_t alg = alg_arg;
5143 size_t key_bits;
5144 unsigned char *signature = NULL;
5145 size_t signature_size;
5146 size_t signature_length = 0xdeadbeef;
5147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5148
5149 PSA_ASSERT( psa_crypto_init( ) );
5150
5151 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5152 PSA_KEY_USAGE_VERIFY_MESSAGE );
5153 psa_set_key_algorithm( &attributes, alg );
5154 psa_set_key_type( &attributes, key_type );
5155
5156 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5157 &key ) );
5158 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5159 key_bits = psa_get_key_bits( &attributes );
5160
5161 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5162 TEST_ASSERT( signature_size != 0 );
5163 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5164 ASSERT_ALLOC( signature, signature_size );
5165
5166 PSA_ASSERT( psa_sign_message( key, alg,
5167 input_data->x, input_data->len,
5168 signature, signature_size,
5169 &signature_length ) );
5170 TEST_ASSERT( signature_length <= signature_size );
5171 TEST_ASSERT( signature_length > 0 );
5172
5173 PSA_ASSERT( psa_verify_message( key, alg,
5174 input_data->x, input_data->len,
5175 signature, signature_length ) );
5176
5177 if( input_data->len != 0 )
5178 {
5179 /* Flip a bit in the input and verify that the signature is now
5180 * detected as invalid. Flip a bit at the beginning, not at the end,
5181 * because ECDSA may ignore the last few bits of the input. */
5182 input_data->x[0] ^= 1;
5183 TEST_EQUAL( psa_verify_message( key, alg,
5184 input_data->x, input_data->len,
5185 signature, signature_length ),
5186 PSA_ERROR_INVALID_SIGNATURE );
5187 }
5188
5189exit:
5190 psa_reset_key_attributes( &attributes );
5191
5192 psa_destroy_key( key );
5193 mbedtls_free( signature );
5194 PSA_DONE( );
5195}
5196/* END_CASE */
5197
5198/* BEGIN_CASE */
5199void verify_message( int key_type_arg,
5200 data_t *key_data,
5201 int alg_arg,
5202 data_t *input_data,
5203 data_t *signature_data )
5204{
5205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5206 psa_key_type_t key_type = key_type_arg;
5207 psa_algorithm_t alg = alg_arg;
5208 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5209
5210 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5211
5212 PSA_ASSERT( psa_crypto_init( ) );
5213
5214 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5215 psa_set_key_algorithm( &attributes, alg );
5216 psa_set_key_type( &attributes, key_type );
5217
5218 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5219 &key ) );
5220
5221 PSA_ASSERT( psa_verify_message( key, alg,
5222 input_data->x, input_data->len,
5223 signature_data->x, signature_data->len ) );
5224
5225exit:
5226 psa_reset_key_attributes( &attributes );
5227 psa_destroy_key( key );
5228 PSA_DONE( );
5229}
5230/* END_CASE */
5231
5232/* BEGIN_CASE */
5233void verify_message_fail( int key_type_arg,
5234 data_t *key_data,
5235 int alg_arg,
5236 data_t *hash_data,
5237 data_t *signature_data,
5238 int expected_status_arg )
5239{
5240 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5241 psa_key_type_t key_type = key_type_arg;
5242 psa_algorithm_t alg = alg_arg;
5243 psa_status_t actual_status;
5244 psa_status_t expected_status = expected_status_arg;
5245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5246
5247 PSA_ASSERT( psa_crypto_init( ) );
5248
5249 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5250 psa_set_key_algorithm( &attributes, alg );
5251 psa_set_key_type( &attributes, key_type );
5252
5253 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5254 &key ) );
5255
5256 actual_status = psa_verify_message( key, alg,
5257 hash_data->x, hash_data->len,
5258 signature_data->x,
5259 signature_data->len );
5260 TEST_EQUAL( actual_status, expected_status );
5261
5262exit:
5263 psa_reset_key_attributes( &attributes );
5264 psa_destroy_key( key );
5265 PSA_DONE( );
5266}
5267/* END_CASE */
5268
5269/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005270void asymmetric_encrypt( int key_type_arg,
5271 data_t *key_data,
5272 int alg_arg,
5273 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005274 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005275 int expected_output_length_arg,
5276 int expected_status_arg )
5277{
Ronald Cron5425a212020-08-04 14:58:35 +02005278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005279 psa_key_type_t key_type = key_type_arg;
5280 psa_algorithm_t alg = alg_arg;
5281 size_t expected_output_length = expected_output_length_arg;
5282 size_t key_bits;
5283 unsigned char *output = NULL;
5284 size_t output_size;
5285 size_t output_length = ~0;
5286 psa_status_t actual_status;
5287 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005289
Gilles Peskine8817f612018-12-18 00:18:46 +01005290 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005291
Gilles Peskine656896e2018-06-29 19:12:28 +02005292 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005293 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5294 psa_set_key_algorithm( &attributes, alg );
5295 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005296 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005297 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005298
5299 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005300 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005301 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005302
Gilles Peskine656896e2018-06-29 19:12:28 +02005303 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005304 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005305 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005306
5307 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005308 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005309 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005310 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005311 output, output_size,
5312 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005313 TEST_EQUAL( actual_status, expected_status );
5314 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005315
Gilles Peskine68428122018-06-30 18:42:41 +02005316 /* If the label is empty, the test framework puts a non-null pointer
5317 * in label->x. Test that a null pointer works as well. */
5318 if( label->len == 0 )
5319 {
5320 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005321 if( output_size != 0 )
5322 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005323 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005324 input_data->x, input_data->len,
5325 NULL, label->len,
5326 output, output_size,
5327 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005328 TEST_EQUAL( actual_status, expected_status );
5329 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005330 }
5331
Gilles Peskine656896e2018-06-29 19:12:28 +02005332exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005333 /*
5334 * Key attributes may have been returned by psa_get_key_attributes()
5335 * thus reset them as required.
5336 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005337 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005338
Ronald Cron5425a212020-08-04 14:58:35 +02005339 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005340 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005341 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005342}
5343/* END_CASE */
5344
5345/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005346void asymmetric_encrypt_decrypt( int key_type_arg,
5347 data_t *key_data,
5348 int alg_arg,
5349 data_t *input_data,
5350 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005351{
Ronald Cron5425a212020-08-04 14:58:35 +02005352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005353 psa_key_type_t key_type = key_type_arg;
5354 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005355 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005356 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005357 size_t output_size;
5358 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005359 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005360 size_t output2_size;
5361 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005363
Gilles Peskine8817f612018-12-18 00:18:46 +01005364 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005365
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005366 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5367 psa_set_key_algorithm( &attributes, alg );
5368 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005369
Gilles Peskine049c7532019-05-15 20:22:09 +02005370 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005371 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005372
5373 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005374 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005375 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005376
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005377 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005378 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005379 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005380
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005381 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005382 TEST_ASSERT( output2_size <=
5383 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5384 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005385 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005386
Gilles Peskineeebd7382018-06-08 18:11:54 +02005387 /* We test encryption by checking that encrypt-then-decrypt gives back
5388 * the original plaintext because of the non-optional random
5389 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005390 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005391 input_data->x, input_data->len,
5392 label->x, label->len,
5393 output, output_size,
5394 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005395 /* We don't know what ciphertext length to expect, but check that
5396 * it looks sensible. */
5397 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005398
Ronald Cron5425a212020-08-04 14:58:35 +02005399 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005400 output, output_length,
5401 label->x, label->len,
5402 output2, output2_size,
5403 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005404 ASSERT_COMPARE( input_data->x, input_data->len,
5405 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005406
5407exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005408 /*
5409 * Key attributes may have been returned by psa_get_key_attributes()
5410 * thus reset them as required.
5411 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005412 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005413
Ronald Cron5425a212020-08-04 14:58:35 +02005414 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005415 mbedtls_free( output );
5416 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005417 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005418}
5419/* END_CASE */
5420
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005421/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005422void asymmetric_decrypt( int key_type_arg,
5423 data_t *key_data,
5424 int alg_arg,
5425 data_t *input_data,
5426 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005427 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005428{
Ronald Cron5425a212020-08-04 14:58:35 +02005429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005430 psa_key_type_t key_type = key_type_arg;
5431 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005432 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005433 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005434 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005435 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005437
Gilles Peskine8817f612018-12-18 00:18:46 +01005438 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005439
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005440 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5441 psa_set_key_algorithm( &attributes, alg );
5442 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005443
Gilles Peskine049c7532019-05-15 20:22:09 +02005444 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005445 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005446
gabor-mezei-armceface22021-01-21 12:26:17 +01005447 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5448 key_bits = psa_get_key_bits( &attributes );
5449
5450 /* Determine the maximum ciphertext length */
5451 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5452 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5453 ASSERT_ALLOC( output, output_size );
5454
Ronald Cron5425a212020-08-04 14:58:35 +02005455 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005456 input_data->x, input_data->len,
5457 label->x, label->len,
5458 output,
5459 output_size,
5460 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005461 ASSERT_COMPARE( expected_data->x, expected_data->len,
5462 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005463
Gilles Peskine68428122018-06-30 18:42:41 +02005464 /* If the label is empty, the test framework puts a non-null pointer
5465 * in label->x. Test that a null pointer works as well. */
5466 if( label->len == 0 )
5467 {
5468 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005469 if( output_size != 0 )
5470 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005471 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005472 input_data->x, input_data->len,
5473 NULL, label->len,
5474 output,
5475 output_size,
5476 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005477 ASSERT_COMPARE( expected_data->x, expected_data->len,
5478 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005479 }
5480
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005481exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005482 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005483 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005484 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005485 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005486}
5487/* END_CASE */
5488
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005489/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005490void asymmetric_decrypt_fail( int key_type_arg,
5491 data_t *key_data,
5492 int alg_arg,
5493 data_t *input_data,
5494 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005495 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005496 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005497{
Ronald Cron5425a212020-08-04 14:58:35 +02005498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005499 psa_key_type_t key_type = key_type_arg;
5500 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005501 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005502 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005503 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005504 psa_status_t actual_status;
5505 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005507
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005508 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005509
Gilles Peskine8817f612018-12-18 00:18:46 +01005510 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005511
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005512 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5513 psa_set_key_algorithm( &attributes, alg );
5514 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005515
Gilles Peskine049c7532019-05-15 20:22:09 +02005516 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005517 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005518
Ronald Cron5425a212020-08-04 14:58:35 +02005519 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005520 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005521 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005522 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005523 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005524 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005525 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005526
Gilles Peskine68428122018-06-30 18:42:41 +02005527 /* If the label is empty, the test framework puts a non-null pointer
5528 * in label->x. Test that a null pointer works as well. */
5529 if( label->len == 0 )
5530 {
5531 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005532 if( output_size != 0 )
5533 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005534 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005535 input_data->x, input_data->len,
5536 NULL, label->len,
5537 output, output_size,
5538 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005539 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005540 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005541 }
5542
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005543exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005544 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005545 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005546 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005547 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005548}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005549/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005550
5551/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005552void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005553{
5554 /* Test each valid way of initializing the object, except for `= {0}`, as
5555 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5556 * though it's OK by the C standard. We could test for this, but we'd need
5557 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005558 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005559 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5560 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5561 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005562
5563 memset( &zero, 0, sizeof( zero ) );
5564
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005566 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005567 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005568 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005569 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005570 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005571 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005572
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005573 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005574 PSA_ASSERT( psa_key_derivation_abort(&func) );
5575 PSA_ASSERT( psa_key_derivation_abort(&init) );
5576 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005577}
5578/* END_CASE */
5579
Janos Follath16de4a42019-06-13 16:32:24 +01005580/* BEGIN_CASE */
5581void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005582{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005583 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005584 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005585 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005586
Gilles Peskine8817f612018-12-18 00:18:46 +01005587 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005588
Janos Follath16de4a42019-06-13 16:32:24 +01005589 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005590 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005591
5592exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005593 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005594 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005595}
5596/* END_CASE */
5597
Janos Follathaf3c2a02019-06-12 12:34:34 +01005598/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005599void derive_set_capacity( int alg_arg, int capacity_arg,
5600 int expected_status_arg )
5601{
5602 psa_algorithm_t alg = alg_arg;
5603 size_t capacity = capacity_arg;
5604 psa_status_t expected_status = expected_status_arg;
5605 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5606
5607 PSA_ASSERT( psa_crypto_init( ) );
5608
5609 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5610
5611 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5612 expected_status );
5613
5614exit:
5615 psa_key_derivation_abort( &operation );
5616 PSA_DONE( );
5617}
5618/* END_CASE */
5619
5620/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005621void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005622 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005623 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005624 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005625 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005626 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005627 int expected_status_arg3,
5628 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005629{
5630 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005631 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5632 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005633 psa_status_t expected_statuses[] = {expected_status_arg1,
5634 expected_status_arg2,
5635 expected_status_arg3};
5636 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005637 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5638 MBEDTLS_SVC_KEY_ID_INIT,
5639 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005640 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5642 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005643 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005644 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005645 psa_status_t expected_output_status = expected_output_status_arg;
5646 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005647
5648 PSA_ASSERT( psa_crypto_init( ) );
5649
5650 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5651 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005652
5653 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5654
5655 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5656 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005657 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005658 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005659 psa_set_key_type( &attributes, key_types[i] );
5660 PSA_ASSERT( psa_import_key( &attributes,
5661 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005662 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005663 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5664 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5665 {
5666 // When taking a private key as secret input, use key agreement
5667 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005668 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5669 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005670 expected_statuses[i] );
5671 }
5672 else
5673 {
5674 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005675 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005676 expected_statuses[i] );
5677 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005678 }
5679 else
5680 {
5681 TEST_EQUAL( psa_key_derivation_input_bytes(
5682 &operation, steps[i],
5683 inputs[i]->x, inputs[i]->len ),
5684 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005685 }
5686 }
5687
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005688 if( output_key_type != PSA_KEY_TYPE_NONE )
5689 {
5690 psa_reset_key_attributes( &attributes );
5691 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5692 psa_set_key_bits( &attributes, 8 );
5693 actual_output_status =
5694 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005695 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005696 }
5697 else
5698 {
5699 uint8_t buffer[1];
5700 actual_output_status =
5701 psa_key_derivation_output_bytes( &operation,
5702 buffer, sizeof( buffer ) );
5703 }
5704 TEST_EQUAL( actual_output_status, expected_output_status );
5705
Janos Follathaf3c2a02019-06-12 12:34:34 +01005706exit:
5707 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005708 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5709 psa_destroy_key( keys[i] );
5710 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005711 PSA_DONE( );
5712}
5713/* END_CASE */
5714
Janos Follathd958bb72019-07-03 15:02:16 +01005715/* BEGIN_CASE */
5716void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005717{
Janos Follathd958bb72019-07-03 15:02:16 +01005718 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005720 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005721 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005722 unsigned char input1[] = "Input 1";
5723 size_t input1_length = sizeof( input1 );
5724 unsigned char input2[] = "Input 2";
5725 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005726 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005727 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005728 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5729 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5730 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005731 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005732
Gilles Peskine8817f612018-12-18 00:18:46 +01005733 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005734
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005735 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5736 psa_set_key_algorithm( &attributes, alg );
5737 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005738
Gilles Peskine73676cb2019-05-15 20:15:10 +02005739 PSA_ASSERT( psa_import_key( &attributes,
5740 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005741 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005742
5743 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005744 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5745 input1, input1_length,
5746 input2, input2_length,
5747 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005748 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005749
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005750 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005751 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005752 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005753
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005754 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005755
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005756 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005757 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005758
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005759exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005760 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005761 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005762 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005763}
5764/* END_CASE */
5765
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005766/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005767void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005768{
5769 uint8_t output_buffer[16];
5770 size_t buffer_size = 16;
5771 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005772 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005773
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005774 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5775 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005776 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005777
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005778 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005779 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005780
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005781 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005782
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005783 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5784 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005785 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005786
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005787 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005788 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005789
5790exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005791 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005792}
5793/* END_CASE */
5794
5795/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005796void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005797 int step1_arg, data_t *input1,
5798 int step2_arg, data_t *input2,
5799 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005800 int requested_capacity_arg,
5801 data_t *expected_output1,
5802 data_t *expected_output2 )
5803{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005804 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005805 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5806 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005807 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5808 MBEDTLS_SVC_KEY_ID_INIT,
5809 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005810 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005811 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005812 uint8_t *expected_outputs[2] =
5813 {expected_output1->x, expected_output2->x};
5814 size_t output_sizes[2] =
5815 {expected_output1->len, expected_output2->len};
5816 size_t output_buffer_size = 0;
5817 uint8_t *output_buffer = NULL;
5818 size_t expected_capacity;
5819 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005821 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005822 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005823
5824 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5825 {
5826 if( output_sizes[i] > output_buffer_size )
5827 output_buffer_size = output_sizes[i];
5828 if( output_sizes[i] == 0 )
5829 expected_outputs[i] = NULL;
5830 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005831 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005832 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005833
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005834 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5835 psa_set_key_algorithm( &attributes, alg );
5836 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005837
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005838 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005839 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5840 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5841 requested_capacity ) );
5842 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005843 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005844 switch( steps[i] )
5845 {
5846 case 0:
5847 break;
5848 case PSA_KEY_DERIVATION_INPUT_SECRET:
5849 PSA_ASSERT( psa_import_key( &attributes,
5850 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005851 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005852
5853 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5854 {
5855 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5856 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5857 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5858 }
5859
Gilles Peskine1468da72019-05-29 17:35:49 +02005860 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005861 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005862 break;
5863 default:
5864 PSA_ASSERT( psa_key_derivation_input_bytes(
5865 &operation, steps[i],
5866 inputs[i]->x, inputs[i]->len ) );
5867 break;
5868 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005869 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005870
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005871 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005872 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005873 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005874 expected_capacity = requested_capacity;
5875
5876 /* Expansion phase. */
5877 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5878 {
5879 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005880 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005881 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005882 if( expected_capacity == 0 && output_sizes[i] == 0 )
5883 {
5884 /* Reading 0 bytes when 0 bytes are available can go either way. */
5885 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005886 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005887 continue;
5888 }
5889 else if( expected_capacity == 0 ||
5890 output_sizes[i] > expected_capacity )
5891 {
5892 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005893 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005894 expected_capacity = 0;
5895 continue;
5896 }
5897 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005898 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005899 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005900 ASSERT_COMPARE( output_buffer, output_sizes[i],
5901 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005902 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005903 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005904 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005905 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005906 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005907 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005908 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005909
5910exit:
5911 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005912 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005913 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5914 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005915 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005916}
5917/* END_CASE */
5918
5919/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005920void derive_full( int alg_arg,
5921 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005922 data_t *input1,
5923 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005924 int requested_capacity_arg )
5925{
Ronald Cron5425a212020-08-04 14:58:35 +02005926 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005927 psa_algorithm_t alg = alg_arg;
5928 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005929 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005930 unsigned char output_buffer[16];
5931 size_t expected_capacity = requested_capacity;
5932 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005934
Gilles Peskine8817f612018-12-18 00:18:46 +01005935 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005936
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005937 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5938 psa_set_key_algorithm( &attributes, alg );
5939 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005940
Gilles Peskine049c7532019-05-15 20:22:09 +02005941 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005942 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005943
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005944 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5945 input1->x, input1->len,
5946 input2->x, input2->len,
5947 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005948 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005949
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005950 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005951 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005952 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005953
5954 /* Expansion phase. */
5955 while( current_capacity > 0 )
5956 {
5957 size_t read_size = sizeof( output_buffer );
5958 if( read_size > current_capacity )
5959 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005960 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005961 output_buffer,
5962 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005963 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005964 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005965 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005966 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005967 }
5968
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005969 /* Check that the operation refuses to go over capacity. */
5970 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005971 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005972
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005973 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005974
5975exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005976 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005977 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005978 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005979}
5980/* END_CASE */
5981
Janos Follathe60c9052019-07-03 13:51:30 +01005982/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005983void derive_key_exercise( int alg_arg,
5984 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005985 data_t *input1,
5986 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005987 int derived_type_arg,
5988 int derived_bits_arg,
5989 int derived_usage_arg,
5990 int derived_alg_arg )
5991{
Ronald Cron5425a212020-08-04 14:58:35 +02005992 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5993 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005994 psa_algorithm_t alg = alg_arg;
5995 psa_key_type_t derived_type = derived_type_arg;
5996 size_t derived_bits = derived_bits_arg;
5997 psa_key_usage_t derived_usage = derived_usage_arg;
5998 psa_algorithm_t derived_alg = derived_alg_arg;
5999 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006000 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006002 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006003
Gilles Peskine8817f612018-12-18 00:18:46 +01006004 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006005
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006006 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6007 psa_set_key_algorithm( &attributes, alg );
6008 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006009 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006010 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006011
6012 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006013 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6014 input1->x, input1->len,
6015 input2->x, input2->len,
6016 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006017 goto exit;
6018
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006019 psa_set_key_usage_flags( &attributes, derived_usage );
6020 psa_set_key_algorithm( &attributes, derived_alg );
6021 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006022 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006023 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006024 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006025
6026 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006027 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006028 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6029 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006030
6031 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006032 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006033 goto exit;
6034
6035exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006036 /*
6037 * Key attributes may have been returned by psa_get_key_attributes()
6038 * thus reset them as required.
6039 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006040 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006041
6042 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006043 psa_destroy_key( base_key );
6044 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006045 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006046}
6047/* END_CASE */
6048
Janos Follath42fd8882019-07-03 14:17:09 +01006049/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006050void derive_key_export( int alg_arg,
6051 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006052 data_t *input1,
6053 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006054 int bytes1_arg,
6055 int bytes2_arg )
6056{
Ronald Cron5425a212020-08-04 14:58:35 +02006057 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6058 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006059 psa_algorithm_t alg = alg_arg;
6060 size_t bytes1 = bytes1_arg;
6061 size_t bytes2 = bytes2_arg;
6062 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006063 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006064 uint8_t *output_buffer = NULL;
6065 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006066 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6067 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006068 size_t length;
6069
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006070 ASSERT_ALLOC( output_buffer, capacity );
6071 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006072 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006073
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006074 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6075 psa_set_key_algorithm( &base_attributes, alg );
6076 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006077 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006078 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006079
6080 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006081 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6082 input1->x, input1->len,
6083 input2->x, input2->len,
6084 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006085 goto exit;
6086
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006087 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006088 output_buffer,
6089 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006090 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006091
6092 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006093 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6094 input1->x, input1->len,
6095 input2->x, input2->len,
6096 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006097 goto exit;
6098
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006099 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6100 psa_set_key_algorithm( &derived_attributes, 0 );
6101 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006102 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006103 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006104 &derived_key ) );
6105 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006106 export_buffer, bytes1,
6107 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006108 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006109 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006110 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006111 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006112 &derived_key ) );
6113 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006114 export_buffer + bytes1, bytes2,
6115 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006116 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006117
6118 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006119 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6120 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006121
6122exit:
6123 mbedtls_free( output_buffer );
6124 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006125 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006126 psa_destroy_key( base_key );
6127 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006128 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006129}
6130/* END_CASE */
6131
6132/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006133void derive_key( int alg_arg,
6134 data_t *key_data, data_t *input1, data_t *input2,
6135 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006136 int expected_status_arg,
6137 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006138{
Ronald Cron5425a212020-08-04 14:58:35 +02006139 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6140 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006141 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006142 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006143 size_t bits = bits_arg;
6144 psa_status_t expected_status = expected_status_arg;
6145 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6146 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6147 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6148
6149 PSA_ASSERT( psa_crypto_init( ) );
6150
6151 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6152 psa_set_key_algorithm( &base_attributes, alg );
6153 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6154 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006155 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006156
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006157 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6158 input1->x, input1->len,
6159 input2->x, input2->len,
6160 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006161 goto exit;
6162
6163 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6164 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006165 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006166 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006167
6168 psa_status_t status =
6169 psa_key_derivation_output_key( &derived_attributes,
6170 &operation,
6171 &derived_key );
6172 if( is_large_output > 0 )
6173 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6174 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006175
6176exit:
6177 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006178 psa_destroy_key( base_key );
6179 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006180 PSA_DONE( );
6181}
6182/* END_CASE */
6183
6184/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006185void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006186 int our_key_type_arg, int our_key_alg_arg,
6187 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006188 int expected_status_arg )
6189{
Ronald Cron5425a212020-08-04 14:58:35 +02006190 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006191 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006192 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006193 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006194 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006195 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006196 psa_status_t expected_status = expected_status_arg;
6197 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006198
Gilles Peskine8817f612018-12-18 00:18:46 +01006199 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006200
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006201 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006202 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006203 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006204 PSA_ASSERT( psa_import_key( &attributes,
6205 our_key_data->x, our_key_data->len,
6206 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006207
Gilles Peskine77f40d82019-04-11 21:27:06 +02006208 /* The tests currently include inputs that should fail at either step.
6209 * Test cases that fail at the setup step should be changed to call
6210 * key_derivation_setup instead, and this function should be renamed
6211 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006212 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006213 if( status == PSA_SUCCESS )
6214 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006215 TEST_EQUAL( psa_key_derivation_key_agreement(
6216 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6217 our_key,
6218 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006219 expected_status );
6220 }
6221 else
6222 {
6223 TEST_ASSERT( status == expected_status );
6224 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006225
6226exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006227 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006228 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006229 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006230}
6231/* END_CASE */
6232
6233/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006234void raw_key_agreement( int alg_arg,
6235 int our_key_type_arg, data_t *our_key_data,
6236 data_t *peer_key_data,
6237 data_t *expected_output )
6238{
Ronald Cron5425a212020-08-04 14:58:35 +02006239 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006240 psa_algorithm_t alg = alg_arg;
6241 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006243 unsigned char *output = NULL;
6244 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006245 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006246
6247 ASSERT_ALLOC( output, expected_output->len );
6248 PSA_ASSERT( psa_crypto_init( ) );
6249
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6251 psa_set_key_algorithm( &attributes, alg );
6252 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006253 PSA_ASSERT( psa_import_key( &attributes,
6254 our_key_data->x, our_key_data->len,
6255 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006256
gabor-mezei-armceface22021-01-21 12:26:17 +01006257 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6258 key_bits = psa_get_key_bits( &attributes );
6259
Gilles Peskinebe697d82019-05-16 18:00:41 +02006260 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6261 peer_key_data->x, peer_key_data->len,
6262 output, expected_output->len,
6263 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006264 ASSERT_COMPARE( output, output_length,
6265 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006266 TEST_ASSERT( output_length <=
6267 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6268 TEST_ASSERT( output_length <=
6269 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006270
6271exit:
6272 mbedtls_free( output );
6273 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006274 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006275}
6276/* END_CASE */
6277
6278/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006279void key_agreement_capacity( int alg_arg,
6280 int our_key_type_arg, data_t *our_key_data,
6281 data_t *peer_key_data,
6282 int expected_capacity_arg )
6283{
Ronald Cron5425a212020-08-04 14:58:35 +02006284 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006285 psa_algorithm_t alg = alg_arg;
6286 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006287 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006289 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006290 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006291
Gilles Peskine8817f612018-12-18 00:18:46 +01006292 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006293
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006294 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6295 psa_set_key_algorithm( &attributes, alg );
6296 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006297 PSA_ASSERT( psa_import_key( &attributes,
6298 our_key_data->x, our_key_data->len,
6299 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006300
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006301 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006302 PSA_ASSERT( psa_key_derivation_key_agreement(
6303 &operation,
6304 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6305 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006306 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6307 {
6308 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006309 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006310 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006311 NULL, 0 ) );
6312 }
Gilles Peskine59685592018-09-18 12:11:34 +02006313
Gilles Peskinebf491972018-10-25 22:36:12 +02006314 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006315 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006316 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006317 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006318
Gilles Peskinebf491972018-10-25 22:36:12 +02006319 /* Test the actual capacity by reading the output. */
6320 while( actual_capacity > sizeof( output ) )
6321 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006322 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006323 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006324 actual_capacity -= sizeof( output );
6325 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006326 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006327 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006328 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006329 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006330
Gilles Peskine59685592018-09-18 12:11:34 +02006331exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006332 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006333 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006334 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006335}
6336/* END_CASE */
6337
6338/* BEGIN_CASE */
6339void key_agreement_output( int alg_arg,
6340 int our_key_type_arg, data_t *our_key_data,
6341 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006342 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006343{
Ronald Cron5425a212020-08-04 14:58:35 +02006344 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006345 psa_algorithm_t alg = alg_arg;
6346 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006347 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006349 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006350
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006351 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6352 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006353
Gilles Peskine8817f612018-12-18 00:18:46 +01006354 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006355
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006356 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6357 psa_set_key_algorithm( &attributes, alg );
6358 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006359 PSA_ASSERT( psa_import_key( &attributes,
6360 our_key_data->x, our_key_data->len,
6361 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006362
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006363 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006364 PSA_ASSERT( psa_key_derivation_key_agreement(
6365 &operation,
6366 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6367 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006368 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6369 {
6370 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006371 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006372 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006373 NULL, 0 ) );
6374 }
Gilles Peskine59685592018-09-18 12:11:34 +02006375
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006376 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006377 actual_output,
6378 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006379 ASSERT_COMPARE( actual_output, expected_output1->len,
6380 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006381 if( expected_output2->len != 0 )
6382 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006383 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006384 actual_output,
6385 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006386 ASSERT_COMPARE( actual_output, expected_output2->len,
6387 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006388 }
Gilles Peskine59685592018-09-18 12:11:34 +02006389
6390exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006391 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006392 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006393 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006394 mbedtls_free( actual_output );
6395}
6396/* END_CASE */
6397
6398/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006399void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006400{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006401 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006402 unsigned char *output = NULL;
6403 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006404 size_t i;
6405 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006406
Simon Butcher49f8e312020-03-03 15:51:50 +00006407 TEST_ASSERT( bytes_arg >= 0 );
6408
Gilles Peskine91892022021-02-08 19:50:26 +01006409 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006410 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006411
Gilles Peskine8817f612018-12-18 00:18:46 +01006412 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006413
Gilles Peskinea50d7392018-06-21 10:22:13 +02006414 /* Run several times, to ensure that every output byte will be
6415 * nonzero at least once with overwhelming probability
6416 * (2^(-8*number_of_runs)). */
6417 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006418 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006419 if( bytes != 0 )
6420 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006421 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006422
Gilles Peskinea50d7392018-06-21 10:22:13 +02006423 for( i = 0; i < bytes; i++ )
6424 {
6425 if( output[i] != 0 )
6426 ++changed[i];
6427 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006428 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006429
6430 /* Check that every byte was changed to nonzero at least once. This
6431 * validates that psa_generate_random is overwriting every byte of
6432 * the output buffer. */
6433 for( i = 0; i < bytes; i++ )
6434 {
6435 TEST_ASSERT( changed[i] != 0 );
6436 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006437
6438exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006439 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006440 mbedtls_free( output );
6441 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006442}
6443/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006444
6445/* BEGIN_CASE */
6446void generate_key( int type_arg,
6447 int bits_arg,
6448 int usage_arg,
6449 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006450 int expected_status_arg,
6451 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006452{
Ronald Cron5425a212020-08-04 14:58:35 +02006453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006454 psa_key_type_t type = type_arg;
6455 psa_key_usage_t usage = usage_arg;
6456 size_t bits = bits_arg;
6457 psa_algorithm_t alg = alg_arg;
6458 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006459 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006460 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006461
Gilles Peskine8817f612018-12-18 00:18:46 +01006462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006463
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006464 psa_set_key_usage_flags( &attributes, usage );
6465 psa_set_key_algorithm( &attributes, alg );
6466 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006467 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006468
6469 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006470 psa_status_t status = psa_generate_key( &attributes, &key );
6471
6472 if( is_large_key > 0 )
6473 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6474 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006475 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006476 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006477
6478 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006479 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006480 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6481 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006482
Gilles Peskine818ca122018-06-20 18:16:48 +02006483 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006484 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006485 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006486
6487exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006488 /*
6489 * Key attributes may have been returned by psa_get_key_attributes()
6490 * thus reset them as required.
6491 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006492 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006493
Ronald Cron5425a212020-08-04 14:58:35 +02006494 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006495 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006496}
6497/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006498
Ronald Cronee414c72021-03-18 18:50:08 +01006499/* 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 +02006500void generate_key_rsa( int bits_arg,
6501 data_t *e_arg,
6502 int expected_status_arg )
6503{
Ronald Cron5425a212020-08-04 14:58:35 +02006504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006505 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006506 size_t bits = bits_arg;
6507 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6508 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6509 psa_status_t expected_status = expected_status_arg;
6510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6511 uint8_t *exported = NULL;
6512 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006513 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006514 size_t exported_length = SIZE_MAX;
6515 uint8_t *e_read_buffer = NULL;
6516 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006517 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006518 size_t e_read_length = SIZE_MAX;
6519
6520 if( e_arg->len == 0 ||
6521 ( e_arg->len == 3 &&
6522 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6523 {
6524 is_default_public_exponent = 1;
6525 e_read_size = 0;
6526 }
6527 ASSERT_ALLOC( e_read_buffer, e_read_size );
6528 ASSERT_ALLOC( exported, exported_size );
6529
6530 PSA_ASSERT( psa_crypto_init( ) );
6531
6532 psa_set_key_usage_flags( &attributes, usage );
6533 psa_set_key_algorithm( &attributes, alg );
6534 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6535 e_arg->x, e_arg->len ) );
6536 psa_set_key_bits( &attributes, bits );
6537
6538 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006539 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006540 if( expected_status != PSA_SUCCESS )
6541 goto exit;
6542
6543 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006544 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006545 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6546 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6547 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6548 e_read_buffer, e_read_size,
6549 &e_read_length ) );
6550 if( is_default_public_exponent )
6551 TEST_EQUAL( e_read_length, 0 );
6552 else
6553 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6554
6555 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006556 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006557 goto exit;
6558
6559 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006560 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006561 exported, exported_size,
6562 &exported_length ) );
6563 {
6564 uint8_t *p = exported;
6565 uint8_t *end = exported + exported_length;
6566 size_t len;
6567 /* RSAPublicKey ::= SEQUENCE {
6568 * modulus INTEGER, -- n
6569 * publicExponent INTEGER } -- e
6570 */
6571 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006572 MBEDTLS_ASN1_SEQUENCE |
6573 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006574 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006575 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6576 MBEDTLS_ASN1_INTEGER ) );
6577 if( len >= 1 && p[0] == 0 )
6578 {
6579 ++p;
6580 --len;
6581 }
6582 if( e_arg->len == 0 )
6583 {
6584 TEST_EQUAL( len, 3 );
6585 TEST_EQUAL( p[0], 1 );
6586 TEST_EQUAL( p[1], 0 );
6587 TEST_EQUAL( p[2], 1 );
6588 }
6589 else
6590 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6591 }
6592
6593exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006594 /*
6595 * Key attributes may have been returned by psa_get_key_attributes() or
6596 * set by psa_set_key_domain_parameters() thus reset them as required.
6597 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006598 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006599
Ronald Cron5425a212020-08-04 14:58:35 +02006600 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006601 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006602 mbedtls_free( e_read_buffer );
6603 mbedtls_free( exported );
6604}
6605/* END_CASE */
6606
Darryl Greend49a4992018-06-18 17:27:26 +01006607/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006608void persistent_key_load_key_from_storage( data_t *data,
6609 int type_arg, int bits_arg,
6610 int usage_flags_arg, int alg_arg,
6611 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006612{
Ronald Cron71016a92020-08-28 19:01:50 +02006613 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006614 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6616 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006617 psa_key_type_t type = type_arg;
6618 size_t bits = bits_arg;
6619 psa_key_usage_t usage_flags = usage_flags_arg;
6620 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006621 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006622 unsigned char *first_export = NULL;
6623 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006624 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006625 size_t first_exported_length;
6626 size_t second_exported_length;
6627
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006628 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6629 {
6630 ASSERT_ALLOC( first_export, export_size );
6631 ASSERT_ALLOC( second_export, export_size );
6632 }
Darryl Greend49a4992018-06-18 17:27:26 +01006633
Gilles Peskine8817f612018-12-18 00:18:46 +01006634 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006635
Gilles Peskinec87af662019-05-15 16:12:22 +02006636 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006637 psa_set_key_usage_flags( &attributes, usage_flags );
6638 psa_set_key_algorithm( &attributes, alg );
6639 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006640 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006641
Darryl Green0c6575a2018-11-07 16:05:30 +00006642 switch( generation_method )
6643 {
6644 case IMPORT_KEY:
6645 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006646 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006647 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006648 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006649
Darryl Green0c6575a2018-11-07 16:05:30 +00006650 case GENERATE_KEY:
6651 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006652 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006653 break;
6654
6655 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006656#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006657 {
6658 /* Create base key */
6659 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6660 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6661 psa_set_key_usage_flags( &base_attributes,
6662 PSA_KEY_USAGE_DERIVE );
6663 psa_set_key_algorithm( &base_attributes, derive_alg );
6664 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006665 PSA_ASSERT( psa_import_key( &base_attributes,
6666 data->x, data->len,
6667 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006668 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006669 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006670 PSA_ASSERT( psa_key_derivation_input_key(
6671 &operation,
6672 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006673 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006674 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006675 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006676 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6677 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006678 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006679 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006680 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006681 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006682 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006683#else
6684 TEST_ASSUME( ! "KDF not supported in this configuration" );
6685#endif
6686 break;
6687
6688 default:
6689 TEST_ASSERT( ! "generation_method not implemented in test" );
6690 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006691 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006692 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006693
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006694 /* Export the key if permitted by the key policy. */
6695 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6696 {
Ronald Cron5425a212020-08-04 14:58:35 +02006697 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006698 first_export, export_size,
6699 &first_exported_length ) );
6700 if( generation_method == IMPORT_KEY )
6701 ASSERT_COMPARE( data->x, data->len,
6702 first_export, first_exported_length );
6703 }
Darryl Greend49a4992018-06-18 17:27:26 +01006704
6705 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006706 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006707 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006708 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006709
Darryl Greend49a4992018-06-18 17:27:26 +01006710 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006711 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006712 TEST_ASSERT( mbedtls_svc_key_id_equal(
6713 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006714 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6715 PSA_KEY_LIFETIME_PERSISTENT );
6716 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6717 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6718 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6719 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006720
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006721 /* Export the key again if permitted by the key policy. */
6722 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006723 {
Ronald Cron5425a212020-08-04 14:58:35 +02006724 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006725 second_export, export_size,
6726 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006727 ASSERT_COMPARE( first_export, first_exported_length,
6728 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006729 }
6730
6731 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006732 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006733 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006734
6735exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006736 /*
6737 * Key attributes may have been returned by psa_get_key_attributes()
6738 * thus reset them as required.
6739 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006740 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006741
Darryl Greend49a4992018-06-18 17:27:26 +01006742 mbedtls_free( first_export );
6743 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006744 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006745 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006746 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006747 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006748}
6749/* END_CASE */