blob: f5865bb741ad315b6a6ae3d8a22f9eefafe81bbb [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 Elliott97fd1ba2021-07-21 18:46:06 +0100274/*!
275 * \brief Internal Function for AEAD multipart tests.
276 *
277 * \param key_type_arg Type of key passed in
278 * \param key_data The encryption / decryption key data
279 * \param alg_arg The type of algorithm used
280 * \param nonce Nonce data
281 * \param additional_data Additional data
282 * \param ad_part_len If not -1, the length of chunks to
283 * feed additional data in to be encrypted /
284 * decrypted. If -1, no chunking.
285 * \param input_data Data to encrypt / decrypt
286 * \param data_part_len If not -1, the length of chunks to feed the
287 * data in to be encrypted / decrypted. If -1,
288 * no chunking
289 * \param do_set_lengths If non-zero, then set lengths prior to
290 * calling encryption / decryption.
291 * \param expected_output Expected output
Paul Elliott41ffae12021-07-22 21:52:01 +0100292 * \param expect_valid_signature If non zero, we expect the signature to be
293 * valid
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100294 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100295 * \param do_zero_parts If non-zero, interleave zero length chunks
296 * with normal length chunks
297 * \param swap_set_functions If non-zero, swap the order of set lengths
298 * and set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100299 *
300 * \return int Zero on failure, non-zero on success.
301 *
302 */
303static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
304 int alg_arg,
305 data_t *nonce,
306 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100307 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100308 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100309 int data_part_len_arg,
Paul Elliott33746aa2021-09-15 16:40:40 +0100310 setlengths_method set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100311 data_t *expected_output,
312 int expect_valid_signature,
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 }
442 else
443 {
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100444 if( additional_data->len - part_offset < ad_part_len )
Paul Elliott329d5382021-07-22 17:10:45 +0100445 {
446 part_length = additional_data->len - part_offset;
447 }
448 else
449 {
450 part_length = ad_part_len;
451 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100452 }
453
454 PSA_ASSERT( psa_aead_update_ad( &operation,
455 additional_data->x + part_offset,
456 part_length ) );
457
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 }
459 }
460 else
461 {
462 /* Pass additional data in one go. */
463 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
464 additional_data->len ) );
465 }
466
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100467 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100468 {
469 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100470 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100471 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100472 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100473
474 ASSERT_ALLOC( part_data, part_data_size );
475
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100476 for( part_offset = 0, part_count = 0;
477 part_offset < data_true_size;
478 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100479 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100480 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 {
Paul Elliott329d5382021-07-22 17:10:45 +0100482 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 }
484 else
485 {
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100486 if( ( data_true_size - part_offset ) < data_part_len )
Paul Elliott329d5382021-07-22 17:10:45 +0100487 {
488 part_length = ( data_true_size - part_offset );
489 }
490 else
491 {
492 part_length = data_part_len;
493 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100494 }
495
496 PSA_ASSERT( psa_aead_update( &operation,
497 ( input_data->x + part_offset ),
498 part_length, part_data,
499 part_data_size,
500 &output_part_length ) );
501
502 if( output_data && output_part_length )
503 {
504 memcpy( ( output_data + part_offset ), part_data,
505 output_part_length );
506 }
507
Paul Elliottd3f82412021-06-16 16:52:21 +0100508 output_length += output_part_length;
509 }
510 }
511 else
512 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100513 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100515 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100516 output_size, &output_length ) );
517 }
518
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 if( is_encrypt )
520 PSA_ASSERT( psa_aead_finish( &operation, final_data,
521 final_output_size,
522 &output_part_length,
523 tag_buffer, tag_length,
524 &tag_size ) );
525 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100526 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100527 status = psa_aead_verify( &operation, final_data,
528 final_output_size,
529 &output_part_length,
530 ( input_data->x + data_true_size ),
531 tag_length );
532
533 if( status != PSA_SUCCESS )
534 {
535 if( !expect_valid_signature )
536 {
537 /* Expected failure. */
538 test_ok = 1;
539 goto exit;
540 }
541 else
542 PSA_ASSERT( status );
543 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100544 }
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546 if( output_data && output_part_length )
547 memcpy( ( output_data + output_length ), final_data,
548 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100549
550 output_length += output_part_length;
551
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100552
553 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
554 * should be exact.*/
555 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100556 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100557 TEST_EQUAL( tag_length, tag_size );
558
559 if( output_data && tag_length )
560 memcpy( ( output_data + output_length ), tag_buffer,
561 tag_length );
562
563 output_length += tag_length;
564
565 TEST_EQUAL( output_length,
566 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
567 input_data->len ) );
568 TEST_ASSERT( output_length <=
569 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
570 }
571 else
572 {
573 TEST_EQUAL( output_length,
574 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
575 input_data->len ) );
576 TEST_ASSERT( output_length <=
577 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100578 }
579
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100582 output_data, output_length );
583
Paul Elliottd3f82412021-06-16 16:52:21 +0100584
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100585 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100586
587exit:
588 psa_destroy_key( key );
589 psa_aead_abort( &operation );
590 mbedtls_free( output_data );
591 mbedtls_free( part_data );
592 mbedtls_free( final_data );
593 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100594
595 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100596}
597
Gilles Peskinee59236f2018-01-27 23:32:46 +0100598/* END_HEADER */
599
600/* BEGIN_DEPENDENCIES
601 * depends_on:MBEDTLS_PSA_CRYPTO_C
602 * END_DEPENDENCIES
603 */
604
605/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200606void static_checks( )
607{
608 size_t max_truncated_mac_size =
609 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
610
611 /* Check that the length for a truncated MAC always fits in the algorithm
612 * encoding. The shifted mask is the maximum truncated value. The
613 * untruncated algorithm may be one byte larger. */
614 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
615}
616/* END_CASE */
617
618/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200619void import_with_policy( int type_arg,
620 int usage_arg, int alg_arg,
621 int expected_status_arg )
622{
623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
624 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200626 psa_key_type_t type = type_arg;
627 psa_key_usage_t usage = usage_arg;
628 psa_algorithm_t alg = alg_arg;
629 psa_status_t expected_status = expected_status_arg;
630 const uint8_t key_material[16] = {0};
631 psa_status_t status;
632
633 PSA_ASSERT( psa_crypto_init( ) );
634
635 psa_set_key_type( &attributes, type );
636 psa_set_key_usage_flags( &attributes, usage );
637 psa_set_key_algorithm( &attributes, alg );
638
639 status = psa_import_key( &attributes,
640 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200641 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200642 TEST_EQUAL( status, expected_status );
643 if( status != PSA_SUCCESS )
644 goto exit;
645
Ronald Cron5425a212020-08-04 14:58:35 +0200646 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200647 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
648 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
649 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200650 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200651
Ronald Cron5425a212020-08-04 14:58:35 +0200652 PSA_ASSERT( psa_destroy_key( key ) );
653 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200654
655exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100656 /*
657 * Key attributes may have been returned by psa_get_key_attributes()
658 * thus reset them as required.
659 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200660 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100661
662 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200663 PSA_DONE( );
664}
665/* END_CASE */
666
667/* BEGIN_CASE */
668void import_with_data( data_t *data, int type_arg,
669 int attr_bits_arg,
670 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200671{
672 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
673 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200675 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200676 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200677 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679
Gilles Peskine8817f612018-12-18 00:18:46 +0100680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100681
Gilles Peskine4747d192019-04-17 15:05:45 +0200682 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200683 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200684
Ronald Cron5425a212020-08-04 14:58:35 +0200685 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100686 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200687 if( status != PSA_SUCCESS )
688 goto exit;
689
Ronald Cron5425a212020-08-04 14:58:35 +0200690 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200691 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200692 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200693 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200694 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200695
Ronald Cron5425a212020-08-04 14:58:35 +0200696 PSA_ASSERT( psa_destroy_key( key ) );
697 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698
699exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100700 /*
701 * Key attributes may have been returned by psa_get_key_attributes()
702 * thus reset them as required.
703 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200704 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100705
706 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200707 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708}
709/* END_CASE */
710
711/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200712void import_large_key( int type_arg, int byte_size_arg,
713 int expected_status_arg )
714{
715 psa_key_type_t type = type_arg;
716 size_t byte_size = byte_size_arg;
717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
718 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200720 psa_status_t status;
721 uint8_t *buffer = NULL;
722 size_t buffer_size = byte_size + 1;
723 size_t n;
724
Steven Cooreman69967ce2021-01-18 18:01:08 +0100725 /* Skip the test case if the target running the test cannot
726 * accomodate large keys due to heap size constraints */
727 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200728 memset( buffer, 'K', byte_size );
729
730 PSA_ASSERT( psa_crypto_init( ) );
731
732 /* Try importing the key */
733 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
734 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200735 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100736 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200737 TEST_EQUAL( status, expected_status );
738
739 if( status == PSA_SUCCESS )
740 {
Ronald Cron5425a212020-08-04 14:58:35 +0200741 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200742 TEST_EQUAL( psa_get_key_type( &attributes ), type );
743 TEST_EQUAL( psa_get_key_bits( &attributes ),
744 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200745 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200746 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200747 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 for( n = 0; n < byte_size; n++ )
749 TEST_EQUAL( buffer[n], 'K' );
750 for( n = byte_size; n < buffer_size; n++ )
751 TEST_EQUAL( buffer[n], 0 );
752 }
753
754exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100755 /*
756 * Key attributes may have been returned by psa_get_key_attributes()
757 * thus reset them as required.
758 */
759 psa_reset_key_attributes( &attributes );
760
Ronald Cron5425a212020-08-04 14:58:35 +0200761 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200762 PSA_DONE( );
763 mbedtls_free( buffer );
764}
765/* END_CASE */
766
767/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200768void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
769{
Ronald Cron5425a212020-08-04 14:58:35 +0200770 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200771 size_t bits = bits_arg;
772 psa_status_t expected_status = expected_status_arg;
773 psa_status_t status;
774 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200775 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200776 size_t buffer_size = /* Slight overapproximations */
777 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200778 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200779 unsigned char *p;
780 int ret;
781 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200783
Gilles Peskine8817f612018-12-18 00:18:46 +0100784 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200785 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200786
787 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
788 bits, keypair ) ) >= 0 );
789 length = ret;
790
791 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200792 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200793 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100794 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200795
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200796 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200797 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200798
799exit:
800 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200801 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200802}
803/* END_CASE */
804
805/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300806void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300807 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200808 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100809 int expected_bits,
810 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200811 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100812 int canonical_input )
813{
Ronald Cron5425a212020-08-04 14:58:35 +0200814 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200816 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200817 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 unsigned char *exported = NULL;
820 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100822 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200825 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826
Moran Pekercb088e72018-07-17 17:36:59 +0300827 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200828 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200830 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100831 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100832
Gilles Peskine4747d192019-04-17 15:05:45 +0200833 psa_set_key_usage_flags( &attributes, usage_arg );
834 psa_set_key_algorithm( &attributes, alg );
835 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700836
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200838 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100839
840 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200841 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200842 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
843 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200844 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845
846 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200847 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100848 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100849
850 /* The exported length must be set by psa_export_key() to a value between 0
851 * and export_size. On errors, the exported length must be 0. */
852 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
853 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
854 TEST_ASSERT( exported_length <= export_size );
855
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200856 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200857 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200859 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100860 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200862 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100863
Gilles Peskineea38a922021-02-13 00:05:16 +0100864 /* Run sanity checks on the exported key. For non-canonical inputs,
865 * this validates the canonical representations. For canonical inputs,
866 * this doesn't directly validate the implementation, but it still helps
867 * by cross-validating the test data with the sanity check code. */
868 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200869 goto exit;
870
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200872 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873 else
874 {
Ronald Cron5425a212020-08-04 14:58:35 +0200875 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200876 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200877 &key2 ) );
878 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 reexported,
880 export_size,
881 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200882 ASSERT_COMPARE( exported, exported_length,
883 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200884 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100886 TEST_ASSERT( exported_length <=
887 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
888 psa_get_key_bits( &got_attributes ) ) );
889 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890
891destroy:
892 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200893 PSA_ASSERT( psa_destroy_key( key ) );
894 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100895
896exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100897 /*
898 * Key attributes may have been returned by psa_get_key_attributes()
899 * thus reset them as required.
900 */
901 psa_reset_key_attributes( &got_attributes );
902
itayzafrir3e02b3b2018-06-12 17:06:52 +0300903 mbedtls_free( exported );
904 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200905 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100906}
907/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100908
Moran Pekerf709f4a2018-06-06 17:26:04 +0300909/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300910void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200911 int type_arg,
912 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100913 int export_size_delta,
914 int expected_export_status_arg,
915 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300916{
Ronald Cron5425a212020-08-04 14:58:35 +0200917 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200919 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200920 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300921 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300922 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100923 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100924 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200925 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300926
Gilles Peskine8817f612018-12-18 00:18:46 +0100927 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300928
Gilles Peskine4747d192019-04-17 15:05:45 +0200929 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
930 psa_set_key_algorithm( &attributes, alg );
931 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300932
933 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200934 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300935
Gilles Peskine49c25912018-10-29 15:15:31 +0100936 /* Export the public key */
937 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200938 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200939 exported, export_size,
940 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100941 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100942 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100943 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200944 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100945 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200946 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200947 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100948 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100949 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100950 TEST_ASSERT( expected_public_key->len <=
951 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
952 TEST_ASSERT( expected_public_key->len <=
953 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100954 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
955 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100956 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300957
958exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100959 /*
960 * Key attributes may have been returned by psa_get_key_attributes()
961 * thus reset them as required.
962 */
963 psa_reset_key_attributes( &attributes );
964
itayzafrir3e02b3b2018-06-12 17:06:52 +0300965 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200966 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200967 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300968}
969/* END_CASE */
970
Gilles Peskine20035e32018-02-03 22:44:14 +0100971/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200972void import_and_exercise_key( data_t *data,
973 int type_arg,
974 int bits_arg,
975 int alg_arg )
976{
Ronald Cron5425a212020-08-04 14:58:35 +0200977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200978 psa_key_type_t type = type_arg;
979 size_t bits = bits_arg;
980 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100981 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200983 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200984
Gilles Peskine8817f612018-12-18 00:18:46 +0100985 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200986
Gilles Peskine4747d192019-04-17 15:05:45 +0200987 psa_set_key_usage_flags( &attributes, usage );
988 psa_set_key_algorithm( &attributes, alg );
989 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200990
991 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200992 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993
994 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200995 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200996 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
997 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200998
999 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001000 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001001 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001002
Ronald Cron5425a212020-08-04 14:58:35 +02001003 PSA_ASSERT( psa_destroy_key( key ) );
1004 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001005
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001006exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001007 /*
1008 * Key attributes may have been returned by psa_get_key_attributes()
1009 * thus reset them as required.
1010 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001011 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001012
1013 psa_reset_key_attributes( &attributes );
1014 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001015 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001016}
1017/* END_CASE */
1018
1019/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001020void effective_key_attributes( int type_arg, int expected_type_arg,
1021 int bits_arg, int expected_bits_arg,
1022 int usage_arg, int expected_usage_arg,
1023 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001024{
Ronald Cron5425a212020-08-04 14:58:35 +02001025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001026 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001027 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001028 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001029 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001030 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001031 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001032 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001033 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001034 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001035
Gilles Peskine8817f612018-12-18 00:18:46 +01001036 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001037
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001038 psa_set_key_usage_flags( &attributes, usage );
1039 psa_set_key_algorithm( &attributes, alg );
1040 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001041 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001042
Ronald Cron5425a212020-08-04 14:58:35 +02001043 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001044 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001047 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1048 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1049 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1050 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001051
1052exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001053 /*
1054 * Key attributes may have been returned by psa_get_key_attributes()
1055 * thus reset them as required.
1056 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001057 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001058
1059 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001060 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001061}
1062/* END_CASE */
1063
1064/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001065void check_key_policy( int type_arg, int bits_arg,
1066 int usage_arg, int alg_arg )
1067{
1068 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1069 usage_arg, usage_arg, alg_arg, alg_arg );
1070 goto exit;
1071}
1072/* END_CASE */
1073
1074/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001075void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001076{
1077 /* Test each valid way of initializing the object, except for `= {0}`, as
1078 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1079 * though it's OK by the C standard. We could test for this, but we'd need
1080 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001081 psa_key_attributes_t func = psa_key_attributes_init( );
1082 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1083 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001084
1085 memset( &zero, 0, sizeof( zero ) );
1086
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001087 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1088 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1089 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001090
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001091 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1092 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1093 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1094
1095 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1096 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1097 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1098
1099 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1100 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1101 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1102
1103 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1104 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1105 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001106}
1107/* END_CASE */
1108
1109/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001110void mac_key_policy( int policy_usage,
1111 int policy_alg,
1112 int key_type,
1113 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001114 int exercise_alg,
1115 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001116{
Ronald Cron5425a212020-08-04 14:58:35 +02001117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001119 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001120 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001121 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001122 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001123
Gilles Peskine8817f612018-12-18 00:18:46 +01001124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001126 psa_set_key_usage_flags( &attributes, policy_usage );
1127 psa_set_key_algorithm( &attributes, policy_alg );
1128 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001129
Gilles Peskine049c7532019-05-15 20:22:09 +02001130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001131 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001132
Ronald Cron5425a212020-08-04 14:58:35 +02001133 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001134 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001135 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001136 else
1137 TEST_EQUAL( status, expected_status );
1138
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001140
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001142 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001143 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001144 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001145 else
1146 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001147
1148exit:
1149 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001150 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001151 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001152}
1153/* END_CASE */
1154
1155/* BEGIN_CASE */
1156void cipher_key_policy( int policy_usage,
1157 int policy_alg,
1158 int key_type,
1159 data_t *key_data,
1160 int exercise_alg )
1161{
Ronald Cron5425a212020-08-04 14:58:35 +02001162 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001164 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001165 psa_status_t status;
1166
Gilles Peskine8817f612018-12-18 00:18:46 +01001167 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001168
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001169 psa_set_key_usage_flags( &attributes, policy_usage );
1170 psa_set_key_algorithm( &attributes, policy_alg );
1171 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
Gilles Peskine049c7532019-05-15 20:22:09 +02001173 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001174 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001175
Ronald Cron5425a212020-08-04 14:58:35 +02001176 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177 if( policy_alg == exercise_alg &&
1178 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001179 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001180 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001181 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182 psa_cipher_abort( &operation );
1183
Ronald Cron5425a212020-08-04 14:58:35 +02001184 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001185 if( policy_alg == exercise_alg &&
1186 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001188 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001189 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001190
1191exit:
1192 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001193 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001194 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001195}
1196/* END_CASE */
1197
1198/* BEGIN_CASE */
1199void aead_key_policy( int policy_usage,
1200 int policy_alg,
1201 int key_type,
1202 data_t *key_data,
1203 int nonce_length_arg,
1204 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001205 int exercise_alg,
1206 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207{
Ronald Cron5425a212020-08-04 14:58:35 +02001208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001211 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001212 unsigned char nonce[16] = {0};
1213 size_t nonce_length = nonce_length_arg;
1214 unsigned char tag[16];
1215 size_t tag_length = tag_length_arg;
1216 size_t output_length;
1217
1218 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1219 TEST_ASSERT( tag_length <= sizeof( tag ) );
1220
Gilles Peskine8817f612018-12-18 00:18:46 +01001221 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001223 psa_set_key_usage_flags( &attributes, policy_usage );
1224 psa_set_key_algorithm( &attributes, policy_alg );
1225 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001226
Gilles Peskine049c7532019-05-15 20:22:09 +02001227 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001228 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001229
Ronald Cron5425a212020-08-04 14:58:35 +02001230 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001231 nonce, nonce_length,
1232 NULL, 0,
1233 NULL, 0,
1234 tag, tag_length,
1235 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001236 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1237 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001238 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001239 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001240
1241 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001242 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001243 nonce, nonce_length,
1244 NULL, 0,
1245 tag, tag_length,
1246 NULL, 0,
1247 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001248 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1249 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1250 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001251 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001253 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001254
1255exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001256 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001257 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001258}
1259/* END_CASE */
1260
1261/* BEGIN_CASE */
1262void asymmetric_encryption_key_policy( int policy_usage,
1263 int policy_alg,
1264 int key_type,
1265 data_t *key_data,
1266 int exercise_alg )
1267{
Ronald Cron5425a212020-08-04 14:58:35 +02001268 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001270 psa_status_t status;
1271 size_t key_bits;
1272 size_t buffer_length;
1273 unsigned char *buffer = NULL;
1274 size_t output_length;
1275
Gilles Peskine8817f612018-12-18 00:18:46 +01001276 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001278 psa_set_key_usage_flags( &attributes, policy_usage );
1279 psa_set_key_algorithm( &attributes, policy_alg );
1280 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001281
Gilles Peskine049c7532019-05-15 20:22:09 +02001282 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001283 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001284
Ronald Cron5425a212020-08-04 14:58:35 +02001285 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001286 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1288 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001289 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001290
Ronald Cron5425a212020-08-04 14:58:35 +02001291 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001292 NULL, 0,
1293 NULL, 0,
1294 buffer, buffer_length,
1295 &output_length );
1296 if( policy_alg == exercise_alg &&
1297 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001298 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001299 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001300 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001301
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001302 if( buffer_length != 0 )
1303 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001304 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001305 buffer, buffer_length,
1306 NULL, 0,
1307 buffer, buffer_length,
1308 &output_length );
1309 if( policy_alg == exercise_alg &&
1310 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001311 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001312 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001313 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001314
1315exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001316 /*
1317 * Key attributes may have been returned by psa_get_key_attributes()
1318 * thus reset them as required.
1319 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001320 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001321
1322 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001323 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001324 mbedtls_free( buffer );
1325}
1326/* END_CASE */
1327
1328/* BEGIN_CASE */
1329void asymmetric_signature_key_policy( int policy_usage,
1330 int policy_alg,
1331 int key_type,
1332 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001333 int exercise_alg,
1334 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001335{
Ronald Cron5425a212020-08-04 14:58:35 +02001336 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001338 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001339 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1340 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1341 * compatible with the policy and `payload_length_arg` is supposed to be
1342 * a valid input length to sign. If `payload_length_arg <= 0`,
1343 * `exercise_alg` is supposed to be forbidden by the policy. */
1344 int compatible_alg = payload_length_arg > 0;
1345 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001346 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001347 size_t signature_length;
1348
Gilles Peskine8817f612018-12-18 00:18:46 +01001349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001350
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001351 psa_set_key_usage_flags( &attributes, policy_usage );
1352 psa_set_key_algorithm( &attributes, policy_alg );
1353 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354
Gilles Peskine049c7532019-05-15 20:22:09 +02001355 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001356 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001357
Ronald Cron5425a212020-08-04 14:58:35 +02001358 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001359 payload, payload_length,
1360 signature, sizeof( signature ),
1361 &signature_length );
1362 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001363 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001365 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001366
1367 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001368 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001369 payload, payload_length,
1370 signature, sizeof( signature ) );
1371 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001372 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001373 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001374 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001375
1376exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001377 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001378 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001379}
1380/* END_CASE */
1381
Janos Follathba3fab92019-06-11 14:50:16 +01001382/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001383void derive_key_policy( int policy_usage,
1384 int policy_alg,
1385 int key_type,
1386 data_t *key_data,
1387 int exercise_alg )
1388{
Ronald Cron5425a212020-08-04 14:58:35 +02001389 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001390 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001391 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001392 psa_status_t status;
1393
Gilles Peskine8817f612018-12-18 00:18:46 +01001394 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001395
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001396 psa_set_key_usage_flags( &attributes, policy_usage );
1397 psa_set_key_algorithm( &attributes, policy_alg );
1398 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001399
Gilles Peskine049c7532019-05-15 20:22:09 +02001400 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001401 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001402
Janos Follathba3fab92019-06-11 14:50:16 +01001403 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1404
1405 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1406 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001407 {
Janos Follathba3fab92019-06-11 14:50:16 +01001408 PSA_ASSERT( psa_key_derivation_input_bytes(
1409 &operation,
1410 PSA_KEY_DERIVATION_INPUT_SEED,
1411 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001412 }
Janos Follathba3fab92019-06-11 14:50:16 +01001413
1414 status = psa_key_derivation_input_key( &operation,
1415 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001416 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001417
Gilles Peskineea0fb492018-07-12 17:17:20 +02001418 if( policy_alg == exercise_alg &&
1419 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001420 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001421 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001422 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001423
1424exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001425 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001426 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001427 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001428}
1429/* END_CASE */
1430
1431/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001432void agreement_key_policy( int policy_usage,
1433 int policy_alg,
1434 int key_type_arg,
1435 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001436 int exercise_alg,
1437 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001438{
Ronald Cron5425a212020-08-04 14:58:35 +02001439 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001440 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001442 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001443 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001444 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001445
Gilles Peskine8817f612018-12-18 00:18:46 +01001446 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001447
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001448 psa_set_key_usage_flags( &attributes, policy_usage );
1449 psa_set_key_algorithm( &attributes, policy_alg );
1450 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001451
Gilles Peskine049c7532019-05-15 20:22:09 +02001452 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001453 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001454
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001455 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001456 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001457
Steven Cooremance48e852020-10-05 16:02:45 +02001458 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001459
1460exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001461 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001462 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001463 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001464}
1465/* END_CASE */
1466
1467/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001468void key_policy_alg2( int key_type_arg, data_t *key_data,
1469 int usage_arg, int alg_arg, int alg2_arg )
1470{
Ronald Cron5425a212020-08-04 14:58:35 +02001471 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001472 psa_key_type_t key_type = key_type_arg;
1473 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1474 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1475 psa_key_usage_t usage = usage_arg;
1476 psa_algorithm_t alg = alg_arg;
1477 psa_algorithm_t alg2 = alg2_arg;
1478
1479 PSA_ASSERT( psa_crypto_init( ) );
1480
1481 psa_set_key_usage_flags( &attributes, usage );
1482 psa_set_key_algorithm( &attributes, alg );
1483 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1484 psa_set_key_type( &attributes, key_type );
1485 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001486 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001487
Ronald Cron5425a212020-08-04 14:58:35 +02001488 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001489 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1490 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1491 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1492
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001493 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001494 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001495 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001496 goto exit;
1497
1498exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001499 /*
1500 * Key attributes may have been returned by psa_get_key_attributes()
1501 * thus reset them as required.
1502 */
1503 psa_reset_key_attributes( &got_attributes );
1504
Ronald Cron5425a212020-08-04 14:58:35 +02001505 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001506 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001507}
1508/* END_CASE */
1509
1510/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001511void raw_agreement_key_policy( int policy_usage,
1512 int policy_alg,
1513 int key_type_arg,
1514 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001515 int exercise_alg,
1516 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517{
Ronald Cron5425a212020-08-04 14:58:35 +02001518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001520 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001521 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001522 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001523 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001524
1525 PSA_ASSERT( psa_crypto_init( ) );
1526
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001527 psa_set_key_usage_flags( &attributes, policy_usage );
1528 psa_set_key_algorithm( &attributes, policy_alg );
1529 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001530
Gilles Peskine049c7532019-05-15 20:22:09 +02001531 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001532 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001533
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001534 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001535
Steven Cooremance48e852020-10-05 16:02:45 +02001536 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001537
1538exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001539 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001540 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001541 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001542}
1543/* END_CASE */
1544
1545/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001546void copy_success( int source_usage_arg,
1547 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001548 int type_arg, data_t *material,
1549 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001550 int target_usage_arg,
1551 int target_alg_arg, int target_alg2_arg,
1552 int expected_usage_arg,
1553 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001554{
Gilles Peskineca25db92019-04-19 11:43:08 +02001555 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1556 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001557 psa_key_usage_t expected_usage = expected_usage_arg;
1558 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001559 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001560 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1561 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001562 uint8_t *export_buffer = NULL;
1563
Gilles Peskine57ab7212019-01-28 13:03:09 +01001564 PSA_ASSERT( psa_crypto_init( ) );
1565
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 /* Prepare the source key. */
1567 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1568 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001569 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001570 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001571 PSA_ASSERT( psa_import_key( &source_attributes,
1572 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001573 &source_key ) );
1574 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001575
Gilles Peskineca25db92019-04-19 11:43:08 +02001576 /* Prepare the target attributes. */
1577 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001578 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001579 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001580 /* Set volatile lifetime to reset the key identifier to 0. */
1581 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1582 }
1583
Gilles Peskineca25db92019-04-19 11:43:08 +02001584 if( target_usage_arg != -1 )
1585 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1586 if( target_alg_arg != -1 )
1587 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001588 if( target_alg2_arg != -1 )
1589 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001590
1591 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001592 PSA_ASSERT( psa_copy_key( source_key,
1593 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001594
1595 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001596 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001597
1598 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001599 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001600 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1601 psa_get_key_type( &target_attributes ) );
1602 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1603 psa_get_key_bits( &target_attributes ) );
1604 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1605 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001606 TEST_EQUAL( expected_alg2,
1607 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001608 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1609 {
1610 size_t length;
1611 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001612 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001613 material->len, &length ) );
1614 ASSERT_COMPARE( material->x, material->len,
1615 export_buffer, length );
1616 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001617
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001618 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001619 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001620 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001621 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001622
Ronald Cron5425a212020-08-04 14:58:35 +02001623 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001624
1625exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001626 /*
1627 * Source and target key attributes may have been returned by
1628 * psa_get_key_attributes() thus reset them as required.
1629 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001630 psa_reset_key_attributes( &source_attributes );
1631 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001632
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001633 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001634 mbedtls_free( export_buffer );
1635}
1636/* END_CASE */
1637
1638/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001639void copy_fail( int source_usage_arg,
1640 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001641 int type_arg, data_t *material,
1642 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001643 int target_usage_arg,
1644 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001645 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001646 int expected_status_arg )
1647{
1648 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1649 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001650 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1651 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001652 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001653
1654 PSA_ASSERT( psa_crypto_init( ) );
1655
1656 /* Prepare the source key. */
1657 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1658 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001659 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001660 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001661 PSA_ASSERT( psa_import_key( &source_attributes,
1662 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001663 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001664
1665 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001666 psa_set_key_id( &target_attributes, key_id );
1667 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001668 psa_set_key_type( &target_attributes, target_type_arg );
1669 psa_set_key_bits( &target_attributes, target_bits_arg );
1670 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1671 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001672 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001673
1674 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001675 TEST_EQUAL( psa_copy_key( source_key,
1676 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001677 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001678
Ronald Cron5425a212020-08-04 14:58:35 +02001679 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001680
Gilles Peskine4a644642019-05-03 17:14:08 +02001681exit:
1682 psa_reset_key_attributes( &source_attributes );
1683 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001684 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001685}
1686/* END_CASE */
1687
1688/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001689void hash_operation_init( )
1690{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001691 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001692 /* Test each valid way of initializing the object, except for `= {0}`, as
1693 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1694 * though it's OK by the C standard. We could test for this, but we'd need
1695 * to supress the Clang warning for the test. */
1696 psa_hash_operation_t func = psa_hash_operation_init( );
1697 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1698 psa_hash_operation_t zero;
1699
1700 memset( &zero, 0, sizeof( zero ) );
1701
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001702 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001703 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1704 PSA_ERROR_BAD_STATE );
1705 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1706 PSA_ERROR_BAD_STATE );
1707 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1708 PSA_ERROR_BAD_STATE );
1709
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001710 /* A default hash operation should be abortable without error. */
1711 PSA_ASSERT( psa_hash_abort( &func ) );
1712 PSA_ASSERT( psa_hash_abort( &init ) );
1713 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001714}
1715/* END_CASE */
1716
1717/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001718void hash_setup( int alg_arg,
1719 int expected_status_arg )
1720{
1721 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001722 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001723 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001724 psa_status_t status;
1725
Gilles Peskine8817f612018-12-18 00:18:46 +01001726 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001727
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001728 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001729 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001730
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001731 /* Whether setup succeeded or failed, abort must succeed. */
1732 PSA_ASSERT( psa_hash_abort( &operation ) );
1733
1734 /* If setup failed, reproduce the failure, so as to
1735 * test the resulting state of the operation object. */
1736 if( status != PSA_SUCCESS )
1737 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1738
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001739 /* Now the operation object should be reusable. */
1740#if defined(KNOWN_SUPPORTED_HASH_ALG)
1741 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1742 PSA_ASSERT( psa_hash_abort( &operation ) );
1743#endif
1744
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001745exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001746 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001747}
1748/* END_CASE */
1749
1750/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001751void hash_compute_fail( int alg_arg, data_t *input,
1752 int output_size_arg, int expected_status_arg )
1753{
1754 psa_algorithm_t alg = alg_arg;
1755 uint8_t *output = NULL;
1756 size_t output_size = output_size_arg;
1757 size_t output_length = INVALID_EXPORT_LENGTH;
1758 psa_status_t expected_status = expected_status_arg;
1759 psa_status_t status;
1760
1761 ASSERT_ALLOC( output, output_size );
1762
1763 PSA_ASSERT( psa_crypto_init( ) );
1764
1765 status = psa_hash_compute( alg, input->x, input->len,
1766 output, output_size, &output_length );
1767 TEST_EQUAL( status, expected_status );
1768 TEST_ASSERT( output_length <= output_size );
1769
1770exit:
1771 mbedtls_free( output );
1772 PSA_DONE( );
1773}
1774/* END_CASE */
1775
1776/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001777void hash_compare_fail( int alg_arg, data_t *input,
1778 data_t *reference_hash,
1779 int expected_status_arg )
1780{
1781 psa_algorithm_t alg = alg_arg;
1782 psa_status_t expected_status = expected_status_arg;
1783 psa_status_t status;
1784
1785 PSA_ASSERT( psa_crypto_init( ) );
1786
1787 status = psa_hash_compare( alg, input->x, input->len,
1788 reference_hash->x, reference_hash->len );
1789 TEST_EQUAL( status, expected_status );
1790
1791exit:
1792 PSA_DONE( );
1793}
1794/* END_CASE */
1795
1796/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001797void hash_compute_compare( int alg_arg, data_t *input,
1798 data_t *expected_output )
1799{
1800 psa_algorithm_t alg = alg_arg;
1801 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1802 size_t output_length = INVALID_EXPORT_LENGTH;
1803 size_t i;
1804
1805 PSA_ASSERT( psa_crypto_init( ) );
1806
1807 /* Compute with tight buffer */
1808 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001809 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001810 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001811 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001812 ASSERT_COMPARE( output, output_length,
1813 expected_output->x, expected_output->len );
1814
1815 /* Compute with larger buffer */
1816 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1817 output, sizeof( output ),
1818 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001819 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001820 ASSERT_COMPARE( output, output_length,
1821 expected_output->x, expected_output->len );
1822
1823 /* Compare with correct hash */
1824 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1825 output, output_length ) );
1826
1827 /* Compare with trailing garbage */
1828 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1829 output, output_length + 1 ),
1830 PSA_ERROR_INVALID_SIGNATURE );
1831
1832 /* Compare with truncated hash */
1833 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1834 output, output_length - 1 ),
1835 PSA_ERROR_INVALID_SIGNATURE );
1836
1837 /* Compare with corrupted value */
1838 for( i = 0; i < output_length; i++ )
1839 {
Chris Jones9634bb12021-01-20 15:56:42 +00001840 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001841 output[i] ^= 1;
1842 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1843 output, output_length ),
1844 PSA_ERROR_INVALID_SIGNATURE );
1845 output[i] ^= 1;
1846 }
1847
1848exit:
1849 PSA_DONE( );
1850}
1851/* END_CASE */
1852
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001853/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001854void hash_bad_order( )
1855{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001856 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001857 unsigned char input[] = "";
1858 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001859 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001860 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1861 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1862 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001863 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001864 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001865 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001866
Gilles Peskine8817f612018-12-18 00:18:46 +01001867 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001868
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001869 /* Call setup twice in a row. */
1870 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1871 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1872 PSA_ERROR_BAD_STATE );
1873 PSA_ASSERT( psa_hash_abort( &operation ) );
1874
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001875 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001876 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001877 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001878 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001879
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001880 /* Call update after finish. */
1881 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1882 PSA_ASSERT( psa_hash_finish( &operation,
1883 hash, sizeof( hash ), &hash_len ) );
1884 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001885 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001886 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001887
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001888 /* Call verify without calling setup beforehand. */
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 verify after finish. */
1895 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1896 PSA_ASSERT( psa_hash_finish( &operation,
1897 hash, sizeof( hash ), &hash_len ) );
1898 TEST_EQUAL( psa_hash_verify( &operation,
1899 valid_hash, sizeof( valid_hash ) ),
1900 PSA_ERROR_BAD_STATE );
1901 PSA_ASSERT( psa_hash_abort( &operation ) );
1902
1903 /* Call verify twice in a row. */
1904 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1905 PSA_ASSERT( psa_hash_verify( &operation,
1906 valid_hash, sizeof( valid_hash ) ) );
1907 TEST_EQUAL( psa_hash_verify( &operation,
1908 valid_hash, sizeof( valid_hash ) ),
1909 PSA_ERROR_BAD_STATE );
1910 PSA_ASSERT( psa_hash_abort( &operation ) );
1911
1912 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001913 TEST_EQUAL( psa_hash_finish( &operation,
1914 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001915 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001916 PSA_ASSERT( psa_hash_abort( &operation ) );
1917
1918 /* Call finish twice in a row. */
1919 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1920 PSA_ASSERT( psa_hash_finish( &operation,
1921 hash, sizeof( hash ), &hash_len ) );
1922 TEST_EQUAL( psa_hash_finish( &operation,
1923 hash, sizeof( hash ), &hash_len ),
1924 PSA_ERROR_BAD_STATE );
1925 PSA_ASSERT( psa_hash_abort( &operation ) );
1926
1927 /* Call finish after calling verify. */
1928 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1929 PSA_ASSERT( psa_hash_verify( &operation,
1930 valid_hash, sizeof( valid_hash ) ) );
1931 TEST_EQUAL( psa_hash_finish( &operation,
1932 hash, sizeof( hash ), &hash_len ),
1933 PSA_ERROR_BAD_STATE );
1934 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001935
1936exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001937 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001938}
1939/* END_CASE */
1940
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001941/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001942void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001943{
1944 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001945 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1946 * appended to it */
1947 unsigned char hash[] = {
1948 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1949 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1950 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001951 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001952 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001953
Gilles Peskine8817f612018-12-18 00:18:46 +01001954 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001955
itayzafrir27e69452018-11-01 14:26:34 +02001956 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001957 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001958 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001959 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001960
itayzafrir27e69452018-11-01 14:26:34 +02001961 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001962 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001963 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001964 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001965
itayzafrir27e69452018-11-01 14:26:34 +02001966 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001968 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001969 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001970
itayzafrirec93d302018-10-18 18:01:10 +03001971exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001972 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001973}
1974/* END_CASE */
1975
Ronald Cronee414c72021-03-18 18:50:08 +01001976/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001977void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001978{
1979 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001980 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001981 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001982 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001983 size_t hash_len;
1984
Gilles Peskine8817f612018-12-18 00:18:46 +01001985 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001986
itayzafrir58028322018-10-25 10:22:01 +03001987 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001988 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001989 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001990 hash, expected_size - 1, &hash_len ),
1991 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001992
1993exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001994 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001995}
1996/* END_CASE */
1997
Ronald Cronee414c72021-03-18 18:50:08 +01001998/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001999void hash_clone_source_state( )
2000{
2001 psa_algorithm_t alg = PSA_ALG_SHA_256;
2002 unsigned char hash[PSA_HASH_MAX_SIZE];
2003 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2004 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2005 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2006 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2007 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2008 size_t hash_len;
2009
2010 PSA_ASSERT( psa_crypto_init( ) );
2011 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2012
2013 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2014 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2015 PSA_ASSERT( psa_hash_finish( &op_finished,
2016 hash, sizeof( hash ), &hash_len ) );
2017 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2018 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2019
2020 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2021 PSA_ERROR_BAD_STATE );
2022
2023 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2024 PSA_ASSERT( psa_hash_finish( &op_init,
2025 hash, sizeof( hash ), &hash_len ) );
2026 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2027 PSA_ASSERT( psa_hash_finish( &op_finished,
2028 hash, sizeof( hash ), &hash_len ) );
2029 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2030 PSA_ASSERT( psa_hash_finish( &op_aborted,
2031 hash, sizeof( hash ), &hash_len ) );
2032
2033exit:
2034 psa_hash_abort( &op_source );
2035 psa_hash_abort( &op_init );
2036 psa_hash_abort( &op_setup );
2037 psa_hash_abort( &op_finished );
2038 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002039 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002040}
2041/* END_CASE */
2042
Ronald Cronee414c72021-03-18 18:50:08 +01002043/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002044void hash_clone_target_state( )
2045{
2046 psa_algorithm_t alg = PSA_ALG_SHA_256;
2047 unsigned char hash[PSA_HASH_MAX_SIZE];
2048 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2049 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2050 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2051 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2052 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2053 size_t hash_len;
2054
2055 PSA_ASSERT( psa_crypto_init( ) );
2056
2057 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2058 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2059 PSA_ASSERT( psa_hash_finish( &op_finished,
2060 hash, sizeof( hash ), &hash_len ) );
2061 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2062 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2063
2064 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2065 PSA_ASSERT( psa_hash_finish( &op_target,
2066 hash, sizeof( hash ), &hash_len ) );
2067
2068 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2069 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2070 PSA_ERROR_BAD_STATE );
2071 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2072 PSA_ERROR_BAD_STATE );
2073
2074exit:
2075 psa_hash_abort( &op_target );
2076 psa_hash_abort( &op_init );
2077 psa_hash_abort( &op_setup );
2078 psa_hash_abort( &op_finished );
2079 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002080 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002081}
2082/* END_CASE */
2083
itayzafrir58028322018-10-25 10:22:01 +03002084/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002085void mac_operation_init( )
2086{
Jaeden Amero252ef282019-02-15 14:05:35 +00002087 const uint8_t input[1] = { 0 };
2088
Jaeden Amero769ce272019-01-04 11:48:03 +00002089 /* Test each valid way of initializing the object, except for `= {0}`, as
2090 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2091 * though it's OK by the C standard. We could test for this, but we'd need
2092 * to supress the Clang warning for the test. */
2093 psa_mac_operation_t func = psa_mac_operation_init( );
2094 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2095 psa_mac_operation_t zero;
2096
2097 memset( &zero, 0, sizeof( zero ) );
2098
Jaeden Amero252ef282019-02-15 14:05:35 +00002099 /* A freshly-initialized MAC operation should not be usable. */
2100 TEST_EQUAL( psa_mac_update( &func,
2101 input, sizeof( input ) ),
2102 PSA_ERROR_BAD_STATE );
2103 TEST_EQUAL( psa_mac_update( &init,
2104 input, sizeof( input ) ),
2105 PSA_ERROR_BAD_STATE );
2106 TEST_EQUAL( psa_mac_update( &zero,
2107 input, sizeof( input ) ),
2108 PSA_ERROR_BAD_STATE );
2109
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002110 /* A default MAC operation should be abortable without error. */
2111 PSA_ASSERT( psa_mac_abort( &func ) );
2112 PSA_ASSERT( psa_mac_abort( &init ) );
2113 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002114}
2115/* END_CASE */
2116
2117/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002118void mac_setup( int key_type_arg,
2119 data_t *key,
2120 int alg_arg,
2121 int expected_status_arg )
2122{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002123 psa_key_type_t key_type = key_type_arg;
2124 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002125 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002126 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002127 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2128#if defined(KNOWN_SUPPORTED_MAC_ALG)
2129 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2130#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002131
Gilles Peskine8817f612018-12-18 00:18:46 +01002132 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002133
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002134 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2135 &operation, &status ) )
2136 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002137 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002138
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002139 /* The operation object should be reusable. */
2140#if defined(KNOWN_SUPPORTED_MAC_ALG)
2141 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2142 smoke_test_key_data,
2143 sizeof( smoke_test_key_data ),
2144 KNOWN_SUPPORTED_MAC_ALG,
2145 &operation, &status ) )
2146 goto exit;
2147 TEST_EQUAL( status, PSA_SUCCESS );
2148#endif
2149
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002150exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002151 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002152}
2153/* END_CASE */
2154
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002155/* 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 +00002156void mac_bad_order( )
2157{
Ronald Cron5425a212020-08-04 14:58:35 +02002158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002159 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2160 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002161 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002162 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2163 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2164 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002166 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2167 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2168 size_t sign_mac_length = 0;
2169 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2170 const uint8_t verify_mac[] = {
2171 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2172 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2173 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2174
2175 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002176 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002177 psa_set_key_algorithm( &attributes, alg );
2178 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002179
Ronald Cron5425a212020-08-04 14:58:35 +02002180 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2181 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002182
Jaeden Amero252ef282019-02-15 14:05:35 +00002183 /* Call update without calling setup beforehand. */
2184 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2185 PSA_ERROR_BAD_STATE );
2186 PSA_ASSERT( psa_mac_abort( &operation ) );
2187
2188 /* Call sign finish without calling setup beforehand. */
2189 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2190 &sign_mac_length),
2191 PSA_ERROR_BAD_STATE );
2192 PSA_ASSERT( psa_mac_abort( &operation ) );
2193
2194 /* Call verify finish without calling setup beforehand. */
2195 TEST_EQUAL( psa_mac_verify_finish( &operation,
2196 verify_mac, sizeof( verify_mac ) ),
2197 PSA_ERROR_BAD_STATE );
2198 PSA_ASSERT( psa_mac_abort( &operation ) );
2199
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002200 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002201 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2202 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002203 PSA_ERROR_BAD_STATE );
2204 PSA_ASSERT( psa_mac_abort( &operation ) );
2205
Jaeden Amero252ef282019-02-15 14:05:35 +00002206 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002207 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002208 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2209 PSA_ASSERT( psa_mac_sign_finish( &operation,
2210 sign_mac, sizeof( sign_mac ),
2211 &sign_mac_length ) );
2212 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2213 PSA_ERROR_BAD_STATE );
2214 PSA_ASSERT( psa_mac_abort( &operation ) );
2215
2216 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002217 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002218 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2219 PSA_ASSERT( psa_mac_verify_finish( &operation,
2220 verify_mac, sizeof( verify_mac ) ) );
2221 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2222 PSA_ERROR_BAD_STATE );
2223 PSA_ASSERT( psa_mac_abort( &operation ) );
2224
2225 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002226 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002227 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2228 PSA_ASSERT( psa_mac_sign_finish( &operation,
2229 sign_mac, sizeof( sign_mac ),
2230 &sign_mac_length ) );
2231 TEST_EQUAL( psa_mac_sign_finish( &operation,
2232 sign_mac, sizeof( sign_mac ),
2233 &sign_mac_length ),
2234 PSA_ERROR_BAD_STATE );
2235 PSA_ASSERT( psa_mac_abort( &operation ) );
2236
2237 /* Call verify finish twice in a row. */
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 PSA_ASSERT( psa_mac_verify_finish( &operation,
2241 verify_mac, sizeof( verify_mac ) ) );
2242 TEST_EQUAL( psa_mac_verify_finish( &operation,
2243 verify_mac, sizeof( verify_mac ) ),
2244 PSA_ERROR_BAD_STATE );
2245 PSA_ASSERT( psa_mac_abort( &operation ) );
2246
2247 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002248 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002249 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2250 TEST_EQUAL( psa_mac_verify_finish( &operation,
2251 verify_mac, sizeof( verify_mac ) ),
2252 PSA_ERROR_BAD_STATE );
2253 PSA_ASSERT( psa_mac_abort( &operation ) );
2254
2255 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002256 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002257 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2258 TEST_EQUAL( psa_mac_sign_finish( &operation,
2259 sign_mac, sizeof( sign_mac ),
2260 &sign_mac_length ),
2261 PSA_ERROR_BAD_STATE );
2262 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002263
Ronald Cron5425a212020-08-04 14:58:35 +02002264 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002265
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002266exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002267 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002268}
2269/* END_CASE */
2270
2271/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002272void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002273 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002274 int alg_arg,
2275 data_t *input,
2276 data_t *expected_mac )
2277{
Ronald Cron5425a212020-08-04 14:58:35 +02002278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002279 psa_key_type_t key_type = key_type_arg;
2280 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002281 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002283 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002284 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002285 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002286 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002287 const size_t output_sizes_to_test[] = {
2288 0,
2289 1,
2290 expected_mac->len - 1,
2291 expected_mac->len,
2292 expected_mac->len + 1,
2293 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002294
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002295 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002296 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002297 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002298
Gilles Peskine8817f612018-12-18 00:18:46 +01002299 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002300
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002301 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002302 psa_set_key_algorithm( &attributes, alg );
2303 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002304
Ronald Cron5425a212020-08-04 14:58:35 +02002305 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2306 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002307
Gilles Peskine8b356b52020-08-25 23:44:59 +02002308 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2309 {
2310 const size_t output_size = output_sizes_to_test[i];
2311 psa_status_t expected_status =
2312 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2313 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002314
Chris Jones9634bb12021-01-20 15:56:42 +00002315 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002316 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002317
Gilles Peskine8b356b52020-08-25 23:44:59 +02002318 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002319 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002320 PSA_ASSERT( psa_mac_update( &operation,
2321 input->x, input->len ) );
2322 TEST_EQUAL( psa_mac_sign_finish( &operation,
2323 actual_mac, output_size,
2324 &mac_length ),
2325 expected_status );
2326 PSA_ASSERT( psa_mac_abort( &operation ) );
2327
2328 if( expected_status == PSA_SUCCESS )
2329 {
2330 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2331 actual_mac, mac_length );
2332 }
2333 mbedtls_free( actual_mac );
2334 actual_mac = NULL;
2335 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002336
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002337exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002338 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002339 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002340 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002341 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002342}
2343/* END_CASE */
2344
2345/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002346void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002347 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002348 int alg_arg,
2349 data_t *input,
2350 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002351{
Ronald Cron5425a212020-08-04 14:58:35 +02002352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002353 psa_key_type_t key_type = key_type_arg;
2354 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002355 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002357 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002358
Gilles Peskine69c12672018-06-28 00:07:19 +02002359 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2360
Gilles Peskine8817f612018-12-18 00:18:46 +01002361 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002362
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002364 psa_set_key_algorithm( &attributes, alg );
2365 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002366
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2368 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002369
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002370 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002371 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002372 PSA_ASSERT( psa_mac_update( &operation,
2373 input->x, input->len ) );
2374 PSA_ASSERT( psa_mac_verify_finish( &operation,
2375 expected_mac->x,
2376 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002377
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002378 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002379 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002380 PSA_ASSERT( psa_mac_update( &operation,
2381 input->x, input->len ) );
2382 TEST_EQUAL( psa_mac_verify_finish( &operation,
2383 expected_mac->x,
2384 expected_mac->len - 1 ),
2385 PSA_ERROR_INVALID_SIGNATURE );
2386
2387 /* Test a MAC that's too long. */
2388 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2389 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002390 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002391 PSA_ASSERT( psa_mac_update( &operation,
2392 input->x, input->len ) );
2393 TEST_EQUAL( psa_mac_verify_finish( &operation,
2394 perturbed_mac,
2395 expected_mac->len + 1 ),
2396 PSA_ERROR_INVALID_SIGNATURE );
2397
2398 /* Test changing one byte. */
2399 for( size_t i = 0; i < expected_mac->len; i++ )
2400 {
Chris Jones9634bb12021-01-20 15:56:42 +00002401 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002402 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002403 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002404 PSA_ASSERT( psa_mac_update( &operation,
2405 input->x, input->len ) );
2406 TEST_EQUAL( psa_mac_verify_finish( &operation,
2407 perturbed_mac,
2408 expected_mac->len ),
2409 PSA_ERROR_INVALID_SIGNATURE );
2410 perturbed_mac[i] ^= 1;
2411 }
2412
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002414 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002415 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002416 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002417 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418}
2419/* END_CASE */
2420
2421/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002422void cipher_operation_init( )
2423{
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 const uint8_t input[1] = { 0 };
2425 unsigned char output[1] = { 0 };
2426 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002427 /* Test each valid way of initializing the object, except for `= {0}`, as
2428 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2429 * though it's OK by the C standard. We could test for this, but we'd need
2430 * to supress the Clang warning for the test. */
2431 psa_cipher_operation_t func = psa_cipher_operation_init( );
2432 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2433 psa_cipher_operation_t zero;
2434
2435 memset( &zero, 0, sizeof( zero ) );
2436
Jaeden Ameroab439972019-02-15 14:12:05 +00002437 /* A freshly-initialized cipher operation should not be usable. */
2438 TEST_EQUAL( psa_cipher_update( &func,
2439 input, sizeof( input ),
2440 output, sizeof( output ),
2441 &output_length ),
2442 PSA_ERROR_BAD_STATE );
2443 TEST_EQUAL( psa_cipher_update( &init,
2444 input, sizeof( input ),
2445 output, sizeof( output ),
2446 &output_length ),
2447 PSA_ERROR_BAD_STATE );
2448 TEST_EQUAL( psa_cipher_update( &zero,
2449 input, sizeof( input ),
2450 output, sizeof( output ),
2451 &output_length ),
2452 PSA_ERROR_BAD_STATE );
2453
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002454 /* A default cipher operation should be abortable without error. */
2455 PSA_ASSERT( psa_cipher_abort( &func ) );
2456 PSA_ASSERT( psa_cipher_abort( &init ) );
2457 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002458}
2459/* END_CASE */
2460
2461/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002462void cipher_setup( int key_type_arg,
2463 data_t *key,
2464 int alg_arg,
2465 int expected_status_arg )
2466{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002467 psa_key_type_t key_type = key_type_arg;
2468 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002469 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002470 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002472#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002473 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2474#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002475
Gilles Peskine8817f612018-12-18 00:18:46 +01002476 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002477
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002478 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2479 &operation, &status ) )
2480 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002481 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002482
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002483 /* The operation object should be reusable. */
2484#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2485 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2486 smoke_test_key_data,
2487 sizeof( smoke_test_key_data ),
2488 KNOWN_SUPPORTED_CIPHER_ALG,
2489 &operation, &status ) )
2490 goto exit;
2491 TEST_EQUAL( status, PSA_SUCCESS );
2492#endif
2493
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002494exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002495 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002496 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002497}
2498/* END_CASE */
2499
Ronald Cronee414c72021-03-18 18:50:08 +01002500/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002501void cipher_bad_order( )
2502{
Ronald Cron5425a212020-08-04 14:58:35 +02002503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002504 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2505 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002507 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002508 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002509 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002510 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2511 0xaa, 0xaa, 0xaa, 0xaa };
2512 const uint8_t text[] = {
2513 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2514 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002515 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002516 size_t length = 0;
2517
2518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002519 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2520 psa_set_key_algorithm( &attributes, alg );
2521 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002522 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2523 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002524
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002525 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002526 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2527 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002528 PSA_ERROR_BAD_STATE );
2529 PSA_ASSERT( psa_cipher_abort( &operation ) );
2530
2531 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002532 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2533 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002534 PSA_ERROR_BAD_STATE );
2535 PSA_ASSERT( psa_cipher_abort( &operation ) );
2536
Jaeden Ameroab439972019-02-15 14:12:05 +00002537 /* Generate an IV without calling setup beforehand. */
2538 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2539 buffer, sizeof( buffer ),
2540 &length ),
2541 PSA_ERROR_BAD_STATE );
2542 PSA_ASSERT( psa_cipher_abort( &operation ) );
2543
2544 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002545 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002546 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2547 buffer, sizeof( buffer ),
2548 &length ) );
2549 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2550 buffer, sizeof( buffer ),
2551 &length ),
2552 PSA_ERROR_BAD_STATE );
2553 PSA_ASSERT( psa_cipher_abort( &operation ) );
2554
2555 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002556 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002557 PSA_ASSERT( psa_cipher_set_iv( &operation,
2558 iv, sizeof( iv ) ) );
2559 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2560 buffer, sizeof( buffer ),
2561 &length ),
2562 PSA_ERROR_BAD_STATE );
2563 PSA_ASSERT( psa_cipher_abort( &operation ) );
2564
2565 /* Set an IV without calling setup beforehand. */
2566 TEST_EQUAL( psa_cipher_set_iv( &operation,
2567 iv, sizeof( iv ) ),
2568 PSA_ERROR_BAD_STATE );
2569 PSA_ASSERT( psa_cipher_abort( &operation ) );
2570
2571 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002572 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002573 PSA_ASSERT( psa_cipher_set_iv( &operation,
2574 iv, sizeof( iv ) ) );
2575 TEST_EQUAL( psa_cipher_set_iv( &operation,
2576 iv, sizeof( iv ) ),
2577 PSA_ERROR_BAD_STATE );
2578 PSA_ASSERT( psa_cipher_abort( &operation ) );
2579
2580 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002581 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002582 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2583 buffer, sizeof( buffer ),
2584 &length ) );
2585 TEST_EQUAL( psa_cipher_set_iv( &operation,
2586 iv, sizeof( iv ) ),
2587 PSA_ERROR_BAD_STATE );
2588 PSA_ASSERT( psa_cipher_abort( &operation ) );
2589
2590 /* Call update without calling setup beforehand. */
2591 TEST_EQUAL( psa_cipher_update( &operation,
2592 text, sizeof( text ),
2593 buffer, sizeof( buffer ),
2594 &length ),
2595 PSA_ERROR_BAD_STATE );
2596 PSA_ASSERT( psa_cipher_abort( &operation ) );
2597
2598 /* Call update without an IV where an IV is required. */
2599 TEST_EQUAL( psa_cipher_update( &operation,
2600 text, sizeof( text ),
2601 buffer, sizeof( buffer ),
2602 &length ),
2603 PSA_ERROR_BAD_STATE );
2604 PSA_ASSERT( psa_cipher_abort( &operation ) );
2605
2606 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002607 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002608 PSA_ASSERT( psa_cipher_set_iv( &operation,
2609 iv, sizeof( iv ) ) );
2610 PSA_ASSERT( psa_cipher_finish( &operation,
2611 buffer, sizeof( buffer ), &length ) );
2612 TEST_EQUAL( psa_cipher_update( &operation,
2613 text, sizeof( text ),
2614 buffer, sizeof( buffer ),
2615 &length ),
2616 PSA_ERROR_BAD_STATE );
2617 PSA_ASSERT( psa_cipher_abort( &operation ) );
2618
2619 /* Call finish without calling setup beforehand. */
2620 TEST_EQUAL( psa_cipher_finish( &operation,
2621 buffer, sizeof( buffer ), &length ),
2622 PSA_ERROR_BAD_STATE );
2623 PSA_ASSERT( psa_cipher_abort( &operation ) );
2624
2625 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002626 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002627 /* Not calling update means we are encrypting an empty buffer, which is OK
2628 * for cipher modes with padding. */
2629 TEST_EQUAL( psa_cipher_finish( &operation,
2630 buffer, sizeof( buffer ), &length ),
2631 PSA_ERROR_BAD_STATE );
2632 PSA_ASSERT( psa_cipher_abort( &operation ) );
2633
2634 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002635 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002636 PSA_ASSERT( psa_cipher_set_iv( &operation,
2637 iv, sizeof( iv ) ) );
2638 PSA_ASSERT( psa_cipher_finish( &operation,
2639 buffer, sizeof( buffer ), &length ) );
2640 TEST_EQUAL( psa_cipher_finish( &operation,
2641 buffer, sizeof( buffer ), &length ),
2642 PSA_ERROR_BAD_STATE );
2643 PSA_ASSERT( psa_cipher_abort( &operation ) );
2644
Ronald Cron5425a212020-08-04 14:58:35 +02002645 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002646
Jaeden Ameroab439972019-02-15 14:12:05 +00002647exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002648 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002649 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002650}
2651/* END_CASE */
2652
2653/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002655 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002656 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002657 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002658{
Ronald Cron5425a212020-08-04 14:58:35 +02002659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002660 psa_status_t status;
2661 psa_key_type_t key_type = key_type_arg;
2662 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002663 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002664 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002665 size_t output_buffer_size = 0;
2666 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002667 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002668 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002670
Gilles Peskine8817f612018-12-18 00:18:46 +01002671 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002672
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002673 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2674 psa_set_key_algorithm( &attributes, alg );
2675 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002676
Ronald Cron5425a212020-08-04 14:58:35 +02002677 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2678 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002679
Ronald Cron5425a212020-08-04 14:58:35 +02002680 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002681
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002682 if( iv->len > 0 )
2683 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002684 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002685 }
2686
gabor-mezei-armceface22021-01-21 12:26:17 +01002687 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2688 TEST_ASSERT( output_buffer_size <=
2689 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002690 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002691
Gilles Peskine8817f612018-12-18 00:18:46 +01002692 PSA_ASSERT( psa_cipher_update( &operation,
2693 input->x, input->len,
2694 output, output_buffer_size,
2695 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002696 TEST_ASSERT( function_output_length <=
2697 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2698 TEST_ASSERT( function_output_length <=
2699 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002700 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002701
Gilles Peskine50e586b2018-06-08 14:28:46 +02002702 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002703 ( output_buffer_size == 0 ? NULL :
2704 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002705 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002707 TEST_ASSERT( function_output_length <=
2708 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2709 TEST_ASSERT( function_output_length <=
2710 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002711 total_output_length += function_output_length;
2712
Gilles Peskinefe11b722018-12-18 00:24:04 +01002713 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002714 if( expected_status == PSA_SUCCESS )
2715 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002716 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002717 ASSERT_COMPARE( expected_output->x, expected_output->len,
2718 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002719 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002720
Gilles Peskine50e586b2018-06-08 14:28:46 +02002721exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002722 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002723 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002724 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002725 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002726}
2727/* END_CASE */
2728
2729/* BEGIN_CASE */
2730void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002731 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002732 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002733 int first_part_size_arg,
2734 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002735 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736{
Ronald Cron5425a212020-08-04 14:58:35 +02002737 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738 psa_key_type_t key_type = key_type_arg;
2739 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002740 size_t first_part_size = first_part_size_arg;
2741 size_t output1_length = output1_length_arg;
2742 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002743 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002744 size_t output_buffer_size = 0;
2745 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002746 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002747 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002749
Gilles Peskine8817f612018-12-18 00:18:46 +01002750 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002751
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002752 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2753 psa_set_key_algorithm( &attributes, alg );
2754 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002755
Ronald Cron5425a212020-08-04 14:58:35 +02002756 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2757 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002758
Ronald Cron5425a212020-08-04 14:58:35 +02002759 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002760
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002761 if( iv->len > 0 )
2762 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002763 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002764 }
2765
gabor-mezei-armceface22021-01-21 12:26:17 +01002766 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2767 TEST_ASSERT( output_buffer_size <=
2768 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002769 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002770
Gilles Peskinee0866522019-02-19 19:44:00 +01002771 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002772 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2773 output, output_buffer_size,
2774 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002775 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002776 TEST_ASSERT( function_output_length <=
2777 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2778 TEST_ASSERT( function_output_length <=
2779 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002780 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002781
Gilles Peskine8817f612018-12-18 00:18:46 +01002782 PSA_ASSERT( psa_cipher_update( &operation,
2783 input->x + first_part_size,
2784 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002785 ( output_buffer_size == 0 ? NULL :
2786 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002787 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002788 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002789 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002790 TEST_ASSERT( function_output_length <=
2791 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2792 alg,
2793 input->len - first_part_size ) );
2794 TEST_ASSERT( function_output_length <=
2795 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002796 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002797
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002799 ( output_buffer_size == 0 ? NULL :
2800 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002801 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002803 TEST_ASSERT( function_output_length <=
2804 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2805 TEST_ASSERT( function_output_length <=
2806 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002807 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002810 ASSERT_COMPARE( expected_output->x, expected_output->len,
2811 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812
2813exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002814 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002815 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002816 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002817 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818}
2819/* END_CASE */
2820
2821/* BEGIN_CASE */
2822void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002823 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002824 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002825 int first_part_size_arg,
2826 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002827 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828{
Ronald Cron5425a212020-08-04 14:58:35 +02002829 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830 psa_key_type_t key_type = key_type_arg;
2831 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002832 size_t first_part_size = first_part_size_arg;
2833 size_t output1_length = output1_length_arg;
2834 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002835 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002836 size_t output_buffer_size = 0;
2837 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002838 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002839 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002841
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002843
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002844 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2845 psa_set_key_algorithm( &attributes, alg );
2846 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002847
Ronald Cron5425a212020-08-04 14:58:35 +02002848 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2849 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002850
Ronald Cron5425a212020-08-04 14:58:35 +02002851 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002852
Steven Cooreman177deba2020-09-07 17:14:14 +02002853 if( iv->len > 0 )
2854 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002855 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002856 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002857
gabor-mezei-armceface22021-01-21 12:26:17 +01002858 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2859 TEST_ASSERT( output_buffer_size <=
2860 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002861 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002862
Gilles Peskinee0866522019-02-19 19:44:00 +01002863 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002864 PSA_ASSERT( psa_cipher_update( &operation,
2865 input->x, first_part_size,
2866 output, output_buffer_size,
2867 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002868 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002869 TEST_ASSERT( function_output_length <=
2870 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2871 TEST_ASSERT( function_output_length <=
2872 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002873 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002874
Gilles Peskine8817f612018-12-18 00:18:46 +01002875 PSA_ASSERT( psa_cipher_update( &operation,
2876 input->x + first_part_size,
2877 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002878 ( output_buffer_size == 0 ? NULL :
2879 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002880 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002881 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002882 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002883 TEST_ASSERT( function_output_length <=
2884 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2885 alg,
2886 input->len - first_part_size ) );
2887 TEST_ASSERT( function_output_length <=
2888 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002889 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002890
Gilles Peskine8817f612018-12-18 00:18:46 +01002891 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002892 ( output_buffer_size == 0 ? NULL :
2893 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002894 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002895 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002896 TEST_ASSERT( function_output_length <=
2897 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2898 TEST_ASSERT( function_output_length <=
2899 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002900 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002901 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002902
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002903 ASSERT_COMPARE( expected_output->x, expected_output->len,
2904 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002905
2906exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002907 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002908 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002909 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002910 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002911}
2912/* END_CASE */
2913
Gilles Peskine50e586b2018-06-08 14:28:46 +02002914/* BEGIN_CASE */
2915void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002916 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002917 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002918 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002919{
Ronald Cron5425a212020-08-04 14:58:35 +02002920 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921 psa_status_t status;
2922 psa_key_type_t key_type = key_type_arg;
2923 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002924 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002925 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926 size_t output_buffer_size = 0;
2927 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002928 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002929 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002931
Gilles Peskine8817f612018-12-18 00:18:46 +01002932 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002933
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2935 psa_set_key_algorithm( &attributes, alg );
2936 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002937
Ronald Cron5425a212020-08-04 14:58:35 +02002938 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2939 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002940
Ronald Cron5425a212020-08-04 14:58:35 +02002941 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002942
Steven Cooreman177deba2020-09-07 17:14:14 +02002943 if( iv->len > 0 )
2944 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002945 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002946 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002947
gabor-mezei-armceface22021-01-21 12:26:17 +01002948 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2949 TEST_ASSERT( output_buffer_size <=
2950 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002951 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002952
Gilles Peskine8817f612018-12-18 00:18:46 +01002953 PSA_ASSERT( psa_cipher_update( &operation,
2954 input->x, input->len,
2955 output, output_buffer_size,
2956 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002957 TEST_ASSERT( function_output_length <=
2958 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2959 TEST_ASSERT( function_output_length <=
2960 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002961 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002962
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002964 ( output_buffer_size == 0 ? NULL :
2965 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002966 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002968 TEST_ASSERT( function_output_length <=
2969 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2970 TEST_ASSERT( function_output_length <=
2971 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002972 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002973 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974
2975 if( expected_status == PSA_SUCCESS )
2976 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002977 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002978 ASSERT_COMPARE( expected_output->x, expected_output->len,
2979 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002980 }
2981
Gilles Peskine50e586b2018-06-08 14:28:46 +02002982exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002983 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002984 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002985 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002986 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002987}
2988/* END_CASE */
2989
Gilles Peskine50e586b2018-06-08 14:28:46 +02002990/* BEGIN_CASE */
2991void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002992 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002993 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002994{
Ronald Cron5425a212020-08-04 14:58:35 +02002995 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002996 psa_key_type_t key_type = key_type_arg;
2997 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002998 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002999 size_t iv_size = 16;
3000 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003001 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003002 size_t output1_size = 0;
3003 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003004 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003005 size_t output2_size = 0;
3006 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003007 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003008 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3009 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003011
Gilles Peskine8817f612018-12-18 00:18:46 +01003012 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003013
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003014 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3015 psa_set_key_algorithm( &attributes, alg );
3016 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003017
Ronald Cron5425a212020-08-04 14:58:35 +02003018 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3019 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003020
Ronald Cron5425a212020-08-04 14:58:35 +02003021 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3022 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003023
Steven Cooreman177deba2020-09-07 17:14:14 +02003024 if( alg != PSA_ALG_ECB_NO_PADDING )
3025 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003026 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3027 iv, iv_size,
3028 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003029 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003030 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3031 TEST_ASSERT( output1_size <=
3032 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003033 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003034
Gilles Peskine8817f612018-12-18 00:18:46 +01003035 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3036 output1, output1_size,
3037 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003038 TEST_ASSERT( output1_length <=
3039 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3040 TEST_ASSERT( output1_length <=
3041 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3042
Gilles Peskine8817f612018-12-18 00:18:46 +01003043 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003044 output1 + output1_length,
3045 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003046 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003047 TEST_ASSERT( function_output_length <=
3048 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3049 TEST_ASSERT( function_output_length <=
3050 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003051
Gilles Peskine048b7f02018-06-08 14:20:49 +02003052 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003053
Gilles Peskine8817f612018-12-18 00:18:46 +01003054 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003055
3056 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003057 TEST_ASSERT( output2_size <=
3058 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3059 TEST_ASSERT( output2_size <=
3060 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003061 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003062
Steven Cooreman177deba2020-09-07 17:14:14 +02003063 if( iv_length > 0 )
3064 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003065 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3066 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003067 }
3068
Gilles Peskine8817f612018-12-18 00:18:46 +01003069 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3070 output2, output2_size,
3071 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003072 TEST_ASSERT( output2_length <=
3073 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3074 TEST_ASSERT( output2_length <=
3075 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3076
Gilles Peskine048b7f02018-06-08 14:20:49 +02003077 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003078 PSA_ASSERT( psa_cipher_finish( &operation2,
3079 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003080 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003081 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003082 TEST_ASSERT( function_output_length <=
3083 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3084 TEST_ASSERT( function_output_length <=
3085 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003086
Gilles Peskine048b7f02018-06-08 14:20:49 +02003087 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003090
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003091 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003092
3093exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003094 psa_cipher_abort( &operation1 );
3095 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003096 mbedtls_free( output1 );
3097 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003098 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003099 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003100}
3101/* END_CASE */
3102
3103/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003104void cipher_verify_output_multipart( int alg_arg,
3105 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003106 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003107 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003108 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003109{
Ronald Cron5425a212020-08-04 14:58:35 +02003110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003111 psa_key_type_t key_type = key_type_arg;
3112 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003113 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003114 unsigned char iv[16] = {0};
3115 size_t iv_size = 16;
3116 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003117 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003118 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003119 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003120 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003121 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003122 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003123 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003124 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3125 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003127
Gilles Peskine8817f612018-12-18 00:18:46 +01003128 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003130 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3131 psa_set_key_algorithm( &attributes, alg );
3132 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003133
Ronald Cron5425a212020-08-04 14:58:35 +02003134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3135 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003136
Ronald Cron5425a212020-08-04 14:58:35 +02003137 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3138 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003139
Steven Cooreman177deba2020-09-07 17:14:14 +02003140 if( alg != PSA_ALG_ECB_NO_PADDING )
3141 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003142 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3143 iv, iv_size,
3144 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003145 }
3146
gabor-mezei-armceface22021-01-21 12:26:17 +01003147 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3148 TEST_ASSERT( output1_buffer_size <=
3149 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003150 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003151
Gilles Peskinee0866522019-02-19 19:44:00 +01003152 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003153
Gilles Peskine8817f612018-12-18 00:18:46 +01003154 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3155 output1, output1_buffer_size,
3156 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003157 TEST_ASSERT( function_output_length <=
3158 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3159 TEST_ASSERT( function_output_length <=
3160 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003161 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003162
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 PSA_ASSERT( psa_cipher_update( &operation1,
3164 input->x + first_part_size,
3165 input->len - first_part_size,
3166 output1, output1_buffer_size,
3167 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003168 TEST_ASSERT( function_output_length <=
3169 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3170 alg,
3171 input->len - first_part_size ) );
3172 TEST_ASSERT( function_output_length <=
3173 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003174 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003175
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_cipher_finish( &operation1,
3177 output1 + output1_length,
3178 output1_buffer_size - output1_length,
3179 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003180 TEST_ASSERT( function_output_length <=
3181 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3182 TEST_ASSERT( function_output_length <=
3183 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003184 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003185
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003187
Gilles Peskine048b7f02018-06-08 14:20:49 +02003188 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003189 TEST_ASSERT( output2_buffer_size <=
3190 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3191 TEST_ASSERT( output2_buffer_size <=
3192 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003193 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003194
Steven Cooreman177deba2020-09-07 17:14:14 +02003195 if( iv_length > 0 )
3196 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003197 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3198 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003199 }
Moran Pekerded84402018-06-06 16:36:50 +03003200
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3202 output2, output2_buffer_size,
3203 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003204 TEST_ASSERT( function_output_length <=
3205 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3206 TEST_ASSERT( function_output_length <=
3207 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003208 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003209
Gilles Peskine8817f612018-12-18 00:18:46 +01003210 PSA_ASSERT( psa_cipher_update( &operation2,
3211 output1 + first_part_size,
3212 output1_length - first_part_size,
3213 output2, output2_buffer_size,
3214 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003215 TEST_ASSERT( function_output_length <=
3216 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3217 alg,
3218 output1_length - first_part_size ) );
3219 TEST_ASSERT( function_output_length <=
3220 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003221 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003222
Gilles Peskine8817f612018-12-18 00:18:46 +01003223 PSA_ASSERT( psa_cipher_finish( &operation2,
3224 output2 + output2_length,
3225 output2_buffer_size - output2_length,
3226 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003227 TEST_ASSERT( function_output_length <=
3228 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3229 TEST_ASSERT( function_output_length <=
3230 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003231 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003232
Gilles Peskine8817f612018-12-18 00:18:46 +01003233 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003234
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003235 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003236
3237exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003238 psa_cipher_abort( &operation1 );
3239 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003240 mbedtls_free( output1 );
3241 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003242 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003243 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003244}
3245/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003246
Gilles Peskine20035e32018-02-03 22:44:14 +01003247/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003248void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003249 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003250 data_t *nonce,
3251 data_t *additional_data,
3252 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003253 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254{
Ronald Cron5425a212020-08-04 14:58:35 +02003255 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003256 psa_key_type_t key_type = key_type_arg;
3257 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003258 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003259 unsigned char *output_data = NULL;
3260 size_t output_size = 0;
3261 size_t output_length = 0;
3262 unsigned char *output_data2 = NULL;
3263 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003264 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003265 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003266 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003267
Gilles Peskine8817f612018-12-18 00:18:46 +01003268 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003269
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003270 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3271 psa_set_key_algorithm( &attributes, alg );
3272 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003273
Gilles Peskine049c7532019-05-15 20:22:09 +02003274 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003275 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003276 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3277 key_bits = psa_get_key_bits( &attributes );
3278
3279 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3280 alg );
3281 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3282 * should be exact. */
3283 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3284 expected_result != PSA_ERROR_NOT_SUPPORTED )
3285 {
3286 TEST_EQUAL( output_size,
3287 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3288 TEST_ASSERT( output_size <=
3289 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3290 }
3291 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003292
Steven Cooremanf49478b2021-02-15 15:19:25 +01003293 status = psa_aead_encrypt( key, alg,
3294 nonce->x, nonce->len,
3295 additional_data->x,
3296 additional_data->len,
3297 input_data->x, input_data->len,
3298 output_data, output_size,
3299 &output_length );
3300
3301 /* If the operation is not supported, just skip and not fail in case the
3302 * encryption involves a common limitation of cryptography hardwares and
3303 * an alternative implementation. */
3304 if( status == PSA_ERROR_NOT_SUPPORTED )
3305 {
3306 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3307 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3308 }
3309
3310 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311
3312 if( PSA_SUCCESS == expected_result )
3313 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003314 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003315
Gilles Peskine003a4a92019-05-14 16:09:40 +02003316 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3317 * should be exact. */
3318 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003319 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003320
gabor-mezei-armceface22021-01-21 12:26:17 +01003321 TEST_ASSERT( input_data->len <=
3322 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3323
Ronald Cron5425a212020-08-04 14:58:35 +02003324 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003325 nonce->x, nonce->len,
3326 additional_data->x,
3327 additional_data->len,
3328 output_data, output_length,
3329 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003330 &output_length2 ),
3331 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003332
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003333 ASSERT_COMPARE( input_data->x, input_data->len,
3334 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003335 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003336
Gilles Peskinea1cac842018-06-11 19:33:02 +02003337exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003338 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339 mbedtls_free( output_data );
3340 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003341 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342}
3343/* END_CASE */
3344
3345/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003346void aead_encrypt( int key_type_arg, data_t *key_data,
3347 int alg_arg,
3348 data_t *nonce,
3349 data_t *additional_data,
3350 data_t *input_data,
3351 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003352{
Ronald Cron5425a212020-08-04 14:58:35 +02003353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003354 psa_key_type_t key_type = key_type_arg;
3355 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003356 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003357 unsigned char *output_data = NULL;
3358 size_t output_size = 0;
3359 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003361 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003362
Gilles Peskine8817f612018-12-18 00:18:46 +01003363 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003365 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3366 psa_set_key_algorithm( &attributes, alg );
3367 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003368
Gilles Peskine049c7532019-05-15 20:22:09 +02003369 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003370 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3372 key_bits = psa_get_key_bits( &attributes );
3373
3374 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3375 alg );
3376 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3377 * should be exact. */
3378 TEST_EQUAL( output_size,
3379 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3380 TEST_ASSERT( output_size <=
3381 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3382 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383
Steven Cooremand588ea12021-01-11 19:36:04 +01003384 status = psa_aead_encrypt( key, alg,
3385 nonce->x, nonce->len,
3386 additional_data->x, additional_data->len,
3387 input_data->x, input_data->len,
3388 output_data, output_size,
3389 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390
Ronald Cron28a45ed2021-02-09 20:35:42 +01003391 /* If the operation is not supported, just skip and not fail in case the
3392 * encryption involves a common limitation of cryptography hardwares and
3393 * an alternative implementation. */
3394 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003395 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003396 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3397 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003398 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003399
3400 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003401 ASSERT_COMPARE( expected_result->x, expected_result->len,
3402 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003403
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003405 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003407 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003408}
3409/* END_CASE */
3410
3411/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003412void aead_decrypt( int key_type_arg, data_t *key_data,
3413 int alg_arg,
3414 data_t *nonce,
3415 data_t *additional_data,
3416 data_t *input_data,
3417 data_t *expected_data,
3418 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003419{
Ronald Cron5425a212020-08-04 14:58:35 +02003420 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003421 psa_key_type_t key_type = key_type_arg;
3422 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003423 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003424 unsigned char *output_data = NULL;
3425 size_t output_size = 0;
3426 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003428 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003429 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003430
Gilles Peskine8817f612018-12-18 00:18:46 +01003431 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003432
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003433 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3434 psa_set_key_algorithm( &attributes, alg );
3435 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003436
Gilles Peskine049c7532019-05-15 20:22:09 +02003437 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003438 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003439 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3440 key_bits = psa_get_key_bits( &attributes );
3441
3442 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3443 alg );
3444 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3445 expected_result != PSA_ERROR_NOT_SUPPORTED )
3446 {
3447 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3448 * should be exact. */
3449 TEST_EQUAL( output_size,
3450 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3451 TEST_ASSERT( output_size <=
3452 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3453 }
3454 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003455
Steven Cooremand588ea12021-01-11 19:36:04 +01003456 status = psa_aead_decrypt( key, alg,
3457 nonce->x, nonce->len,
3458 additional_data->x,
3459 additional_data->len,
3460 input_data->x, input_data->len,
3461 output_data, output_size,
3462 &output_length );
3463
Ronald Cron28a45ed2021-02-09 20:35:42 +01003464 /* If the operation is not supported, just skip and not fail in case the
3465 * decryption involves a common limitation of cryptography hardwares and
3466 * an alternative implementation. */
3467 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003468 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003469 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3470 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003471 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003472
3473 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474
Gilles Peskine2d277862018-06-18 15:41:12 +02003475 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003476 ASSERT_COMPARE( expected_data->x, expected_data->len,
3477 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003480 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003481 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003482 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003483}
3484/* END_CASE */
3485
3486/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003487void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3488 int alg_arg,
3489 data_t *nonce,
3490 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003491 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003492 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003493 int do_test_data_chunked,
3494 int do_set_lengths,
3495 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003496{
Paul Elliottd3f82412021-06-16 16:52:21 +01003497 size_t ad_part_len = 0;
3498 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003499 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003500
Paul Elliotte64deda2021-09-09 14:07:23 +01003501 /* Ensure that either one part of the test or the other is done, i.e this
3502 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003503 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3504
3505 /* Temporary whilst we have algorithms that cannot support chunking */
3506 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003507 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003508 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3509 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003510 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003511 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003512
Paul Elliott33746aa2021-09-15 16:40:40 +01003513 if( do_set_lengths )
3514 {
3515 if( ad_part_len & 0x01 )
3516 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3517 else
3518 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3519 }
3520
Paul Elliott329d5382021-07-22 17:10:45 +01003521 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003522 if( !aead_multipart_internal_func( key_type_arg, key_data,
3523 alg_arg, nonce,
3524 additional_data,
3525 ad_part_len,
3526 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003527 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003528 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003529 1, 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003530 break;
3531
3532 /* length(0) part, length(ad_part_len) part, length(0) part... */
3533 mbedtls_test_set_step( 1000 + ad_part_len );
3534
3535 if( !aead_multipart_internal_func( key_type_arg, key_data,
3536 alg_arg, nonce,
3537 additional_data,
3538 ad_part_len,
3539 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003540 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003541 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003542 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003543 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003544 }
3545 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003546
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003547 /* Temporary whilst we have algorithms that cannot support chunking */
3548 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003549 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003550 for( data_part_len = 1; data_part_len <= input_data->len;
3551 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003552 {
Paul Elliott329d5382021-07-22 17:10:45 +01003553 /* Split data into length(data_part_len) parts. */
3554 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003555
Paul Elliott33746aa2021-09-15 16:40:40 +01003556 if( do_set_lengths )
3557 {
3558 if( data_part_len & 0x01 )
3559 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3560 else
3561 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3562 }
3563
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003564 if( !aead_multipart_internal_func( key_type_arg, key_data,
3565 alg_arg, nonce,
3566 additional_data, -1,
3567 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003568 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003569 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003570 1, 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003571 break;
3572
3573 /* length(0) part, length(data_part_len) part, length(0) part... */
3574 mbedtls_test_set_step( 3000 + data_part_len );
3575
3576 if( !aead_multipart_internal_func( key_type_arg, key_data,
3577 alg_arg, nonce,
3578 additional_data, -1,
3579 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003580 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003581 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003582 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003583 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003584 }
3585 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003586
Paul Elliott0023e0a2021-04-27 10:06:22 +01003587
Paul Elliott8fc45162021-06-23 16:06:01 +01003588 /* Goto is required to silence warnings about unused labels, as we
3589 * don't actually do any test assertions in this function. */
3590 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003591}
3592/* END_CASE */
3593
3594/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003595void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3596 int alg_arg,
3597 data_t *nonce,
3598 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003599 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003600 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003601 int do_test_data_chunked,
3602 int do_set_lengths,
3603 data_t *expected_output,
3604 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003605{
Paul Elliottd3f82412021-06-16 16:52:21 +01003606 size_t ad_part_len = 0;
3607 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003608 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003609
Paul Elliotte64deda2021-09-09 14:07:23 +01003610 /* Ensure that either one part of the test or the other is done, i.e this
3611 * test does something. */
3612 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3613
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003614 /* Temporary whilst we have algorithms that cannot support chunking */
3615 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003616 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003617 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3618 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003619 {
Paul Elliott329d5382021-07-22 17:10:45 +01003620 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003621 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003622
Paul Elliott33746aa2021-09-15 16:40:40 +01003623 if( do_set_lengths )
3624 {
3625 if( ad_part_len & 0x01 )
3626 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3627 else
3628 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3629 }
3630
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003631 if( !aead_multipart_internal_func( key_type_arg, key_data,
3632 alg_arg, nonce,
3633 additional_data,
3634 ad_part_len,
3635 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003636 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003637 expected_output,
3638 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003639 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003640 break;
3641
3642 /* length(0) part, length(ad_part_len) part, length(0) part... */
3643 mbedtls_test_set_step( 1000 + ad_part_len );
3644
3645 if( !aead_multipart_internal_func( key_type_arg, key_data,
3646 alg_arg, nonce,
3647 additional_data,
3648 ad_part_len,
3649 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003650 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003651 expected_output,
3652 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003653 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003654 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003655 }
3656 }
3657
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003658 /* Temporary whilst we have algorithms that cannot support chunking */
3659 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003660 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003661 for( data_part_len = 1; data_part_len <= input_data->len;
3662 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003663 {
Paul Elliott329d5382021-07-22 17:10:45 +01003664 /* Split data into length(data_part_len) parts. */
3665 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003666
Paul Elliott33746aa2021-09-15 16:40:40 +01003667 if( do_set_lengths )
3668 {
3669 if( data_part_len & 0x01 )
3670 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3671 else
3672 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3673 }
3674
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003675 if( !aead_multipart_internal_func( key_type_arg, key_data,
3676 alg_arg, nonce,
3677 additional_data, -1,
3678 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003679 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003680 expected_output,
3681 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003682 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003683 break;
3684
3685 /* length(0) part, length(data_part_len) part, length(0) part... */
3686 mbedtls_test_set_step( 3000 + data_part_len );
3687
3688 if( !aead_multipart_internal_func( key_type_arg, key_data,
3689 alg_arg, nonce,
3690 additional_data, -1,
3691 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003692 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003693 expected_output,
3694 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003695 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003696 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003697 }
3698 }
3699
Paul Elliott8fc45162021-06-23 16:06:01 +01003700 /* Goto is required to silence warnings about unused labels, as we
3701 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003702 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003703}
3704/* END_CASE */
3705
3706/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003707void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3708 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003709 int nonce_length,
3710 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003711 data_t *additional_data,
3712 data_t *input_data,
3713 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003714{
3715
3716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3717 psa_key_type_t key_type = key_type_arg;
3718 psa_algorithm_t alg = alg_arg;
3719 psa_aead_operation_t operation;
3720 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3722 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003723 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003724 size_t actual_nonce_length = 0;
3725 size_t expected_nonce_length = expected_nonce_length_arg;
3726 unsigned char *output = NULL;
3727 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003728 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003729 size_t ciphertext_size = 0;
3730 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003731 size_t tag_length = 0;
3732 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003733
3734 PSA_ASSERT( psa_crypto_init( ) );
3735
3736 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3737 psa_set_key_algorithm( & attributes, alg );
3738 psa_set_key_type( & attributes, key_type );
3739
3740 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3741 &key ) );
3742
3743 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3744
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003745 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3746
Paul Elliottf1277632021-08-24 18:11:37 +01003747 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003748
Paul Elliottf1277632021-08-24 18:11:37 +01003749 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003750
Paul Elliottf1277632021-08-24 18:11:37 +01003751 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003752
Paul Elliottf1277632021-08-24 18:11:37 +01003753 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003754
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003755 operation = psa_aead_operation_init( );
3756
3757 status = psa_aead_encrypt_setup( &operation, key, alg );
3758
3759 /* If the operation is not supported, just skip and not fail in case the
3760 * encryption involves a common limitation of cryptography hardwares and
3761 * an alternative implementation. */
3762 if( status == PSA_ERROR_NOT_SUPPORTED )
3763 {
3764 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003765 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003766 }
3767
3768 PSA_ASSERT( status );
3769
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003770 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003771 nonce_length,
3772 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003773
Paul Elliott693bf312021-07-23 17:40:41 +01003774 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003775
Paul Elliottf1277632021-08-24 18:11:37 +01003776 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003777
Paul Elliottf1277632021-08-24 18:11:37 +01003778 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003779
Paul Elliott693bf312021-07-23 17:40:41 +01003780 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003781 {
3782
3783 /* Ensure we can still complete operation. */
3784
3785 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3786 additional_data->len ) );
3787
3788 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003789 output, output_size,
3790 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003791
Paul Elliottf1277632021-08-24 18:11:37 +01003792 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3793 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003794 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3795 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003796
3797exit:
3798 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003799 mbedtls_free( output );
3800 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003801 psa_aead_abort( &operation );
3802 PSA_DONE( );
3803}
3804/* END_CASE */
3805
3806/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003807void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3808 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003809 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003810 data_t *additional_data,
3811 data_t *input_data,
3812 int expected_status_arg )
3813{
3814
3815 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3816 psa_key_type_t key_type = key_type_arg;
3817 psa_algorithm_t alg = alg_arg;
3818 psa_aead_operation_t operation;
3819 uint8_t *nonce_buffer = NULL;
3820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3821 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3822 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003823 unsigned char *output = NULL;
3824 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003825 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003826 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003827 size_t ciphertext_size = 0;
3828 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003829 size_t tag_length = 0;
3830 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003831 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003832
3833 PSA_ASSERT( psa_crypto_init( ) );
3834
3835 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3836 psa_set_key_algorithm( &attributes, alg );
3837 psa_set_key_type( &attributes, key_type );
3838
3839 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3840 &key ) );
3841
3842 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3843
3844 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3845
Paul Elliott6f0e7202021-08-25 12:57:18 +01003846 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003847
Paul Elliott6f0e7202021-08-25 12:57:18 +01003848 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003849
Paul Elliott6f0e7202021-08-25 12:57:18 +01003850 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003851
Paul Elliott6f0e7202021-08-25 12:57:18 +01003852 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003853
3854 operation = psa_aead_operation_init( );
3855
3856 status = psa_aead_encrypt_setup( &operation, key, alg );
3857
3858 /* If the operation is not supported, just skip and not fail in case the
3859 * encryption involves a common limitation of cryptography hardwares and
3860 * an alternative implementation. */
3861 if( status == PSA_ERROR_NOT_SUPPORTED )
3862 {
3863 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003864 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003865 }
3866
3867 PSA_ASSERT( status );
3868
Paul Elliott4023ffd2021-09-10 16:21:22 +01003869 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3870 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003871 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003872 /* Arbitrary size buffer, to test zero length valid buffer. */
3873 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003874 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003875 }
3876 else
3877 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003878 /* If length is zero, then this will return NULL. */
3879 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003880 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003881
Paul Elliott4023ffd2021-09-10 16:21:22 +01003882 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003883 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003884 for( index = 0; index < nonce_length - 1; ++index )
3885 {
3886 nonce_buffer[index] = 'a' + index;
3887 }
Paul Elliott66696b52021-08-16 18:42:41 +01003888 }
Paul Elliott863864a2021-07-23 17:28:31 +01003889 }
3890
Paul Elliott6f0e7202021-08-25 12:57:18 +01003891 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003892
Paul Elliott693bf312021-07-23 17:40:41 +01003893 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003894
3895 if( expected_status == PSA_SUCCESS )
3896 {
3897 /* Ensure we can still complete operation. */
3898
3899 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3900 additional_data->len ) );
3901
3902 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003903 output, output_size,
3904 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003905
Paul Elliott6f0e7202021-08-25 12:57:18 +01003906 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3907 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003908 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3909 }
3910
3911exit:
3912 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003913 mbedtls_free( output );
3914 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003915 mbedtls_free( nonce_buffer );
3916 psa_aead_abort( &operation );
3917 PSA_DONE( );
3918}
3919/* END_CASE */
3920
3921/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003922void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3923 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003924 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003925 data_t *nonce,
3926 data_t *additional_data,
3927 data_t *input_data,
3928 int expected_status_arg )
3929{
3930
3931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3932 psa_key_type_t key_type = key_type_arg;
3933 psa_algorithm_t alg = alg_arg;
3934 psa_aead_operation_t operation;
3935 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3936 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3937 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003938 unsigned char *output = NULL;
3939 unsigned char *ciphertext = NULL;
3940 size_t output_size = output_size_arg;
3941 size_t ciphertext_size = 0;
3942 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003943 size_t tag_length = 0;
3944 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3945
3946 PSA_ASSERT( psa_crypto_init( ) );
3947
3948 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3949 psa_set_key_algorithm( &attributes, alg );
3950 psa_set_key_type( &attributes, key_type );
3951
3952 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3953 &key ) );
3954
3955 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3956
Paul Elliottc6d11d02021-09-01 12:04:23 +01003957 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003958
Paul Elliottc6d11d02021-09-01 12:04:23 +01003959 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003960
Paul Elliottc6d11d02021-09-01 12:04:23 +01003961 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003962
3963 operation = psa_aead_operation_init( );
3964
3965 status = psa_aead_encrypt_setup( &operation, key, alg );
3966
3967 /* If the operation is not supported, just skip and not fail in case the
3968 * encryption involves a common limitation of cryptography hardwares and
3969 * an alternative implementation. */
3970 if( status == PSA_ERROR_NOT_SUPPORTED )
3971 {
3972 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3973 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3974 }
3975
3976 PSA_ASSERT( status );
3977
3978 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3979
3980 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3981 additional_data->len ) );
3982
3983 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003984 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003985
3986 TEST_EQUAL( status, expected_status );
3987
3988 if( expected_status == PSA_SUCCESS )
3989 {
3990 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003991 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3992 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003993 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3994 }
3995
3996exit:
3997 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003998 mbedtls_free( output );
3999 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004000 psa_aead_abort( &operation );
4001 PSA_DONE( );
4002}
4003/* END_CASE */
4004
Paul Elliott91b021e2021-07-23 18:52:31 +01004005/* BEGIN_CASE */
4006void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4007 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004008 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004009 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004010 data_t *nonce,
4011 data_t *additional_data,
4012 data_t *input_data,
4013 int expected_status_arg )
4014{
4015
4016 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4017 psa_key_type_t key_type = key_type_arg;
4018 psa_algorithm_t alg = alg_arg;
4019 psa_aead_operation_t operation;
4020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4021 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4022 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004023 unsigned char *ciphertext = NULL;
4024 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004025 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004026 size_t ciphertext_size = 0;
4027 size_t ciphertext_length = 0;
4028 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004029 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004030 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004031
4032 PSA_ASSERT( psa_crypto_init( ) );
4033
4034 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4035 psa_set_key_algorithm( &attributes, alg );
4036 psa_set_key_type( &attributes, key_type );
4037
4038 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4039 &key ) );
4040
4041 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4042
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004043 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004044
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004045 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004046
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004047 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004048
Paul Elliott719c1322021-09-13 18:27:22 +01004049 ASSERT_ALLOC( tag_buffer, tag_size );
4050
Paul Elliott91b021e2021-07-23 18:52:31 +01004051 operation = psa_aead_operation_init( );
4052
4053 status = psa_aead_encrypt_setup( &operation, key, alg );
4054
4055 /* If the operation is not supported, just skip and not fail in case the
4056 * encryption involves a common limitation of cryptography hardwares and
4057 * an alternative implementation. */
4058 if( status == PSA_ERROR_NOT_SUPPORTED )
4059 {
4060 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4061 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4062 }
4063
4064 PSA_ASSERT( status );
4065
4066 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4067
4068 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4069 additional_data->len ) );
4070
4071 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004072 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004073
4074 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004075 status = psa_aead_finish( &operation, finish_ciphertext,
4076 finish_ciphertext_size,
4077 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004078 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004079
4080 TEST_EQUAL( status, expected_status );
4081
4082exit:
4083 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004084 mbedtls_free( ciphertext );
4085 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004086 psa_aead_abort( &operation );
4087 PSA_DONE( );
4088}
4089/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004090
4091/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004092void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4093 int alg_arg,
4094 data_t *nonce,
4095 data_t *additional_data,
4096 data_t *input_data )
4097{
4098 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4099 psa_key_type_t key_type = key_type_arg;
4100 psa_algorithm_t alg = alg_arg;
4101 psa_aead_operation_t operation;
4102 unsigned char *output_data = NULL;
4103 unsigned char *final_data = NULL;
4104 size_t output_size = 0;
4105 size_t finish_output_size = 0;
4106 size_t output_length = 0;
4107 size_t key_bits = 0;
4108 size_t tag_length = 0;
4109 size_t tag_size = 0;
4110 size_t nonce_length = 0;
4111 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4112 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4113 size_t output_part_length = 0;
4114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4115
4116 PSA_ASSERT( psa_crypto_init( ) );
4117
4118 psa_set_key_usage_flags( & attributes,
4119 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4120 psa_set_key_algorithm( & attributes, alg );
4121 psa_set_key_type( & attributes, key_type );
4122
4123 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4124 &key ) );
4125
4126 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4127 key_bits = psa_get_key_bits( &attributes );
4128
4129 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4130
4131 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4132
4133 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4134
4135 ASSERT_ALLOC( output_data, output_size );
4136
4137 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4138
4139 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4140
4141 ASSERT_ALLOC( final_data, finish_output_size );
4142
4143 /* Test all operations error without calling setup first. */
4144
4145 operation = psa_aead_operation_init( );
4146
4147 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4148 PSA_ERROR_BAD_STATE );
4149
4150 psa_aead_abort( &operation );
4151
4152 operation = psa_aead_operation_init( );
4153
4154 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4155 PSA_AEAD_NONCE_MAX_SIZE,
4156 &nonce_length ),
4157 PSA_ERROR_BAD_STATE );
4158
4159 psa_aead_abort( &operation );
4160
Paul Elliott481be342021-07-16 17:38:47 +01004161 /* ------------------------------------------------------- */
4162
Paul Elliottc23a9a02021-06-21 18:32:46 +01004163 operation = psa_aead_operation_init( );
4164
4165 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4166 input_data->len ),
4167 PSA_ERROR_BAD_STATE );
4168
4169 psa_aead_abort( &operation );
4170
Paul Elliott481be342021-07-16 17:38:47 +01004171 /* ------------------------------------------------------- */
4172
Paul Elliottc23a9a02021-06-21 18:32:46 +01004173 operation = psa_aead_operation_init( );
4174
4175 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4176 additional_data->len ),
4177 PSA_ERROR_BAD_STATE );
4178
4179 psa_aead_abort( &operation );
4180
Paul Elliott481be342021-07-16 17:38:47 +01004181 /* ------------------------------------------------------- */
4182
Paul Elliottc23a9a02021-06-21 18:32:46 +01004183 operation = psa_aead_operation_init( );
4184
4185 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4186 input_data->len, output_data,
4187 output_size, &output_length ),
4188 PSA_ERROR_BAD_STATE );
4189
4190 psa_aead_abort( &operation );
4191
Paul Elliott481be342021-07-16 17:38:47 +01004192 /* ------------------------------------------------------- */
4193
Paul Elliottc23a9a02021-06-21 18:32:46 +01004194 operation = psa_aead_operation_init( );
4195
4196 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4197 finish_output_size,
4198 &output_part_length,
4199 tag_buffer, tag_length,
4200 &tag_size ),
4201 PSA_ERROR_BAD_STATE );
4202
4203 psa_aead_abort( &operation );
4204
Paul Elliott481be342021-07-16 17:38:47 +01004205 /* ------------------------------------------------------- */
4206
Paul Elliottc23a9a02021-06-21 18:32:46 +01004207 operation = psa_aead_operation_init( );
4208
4209 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4210 finish_output_size,
4211 &output_part_length,
4212 tag_buffer,
4213 tag_length ),
4214 PSA_ERROR_BAD_STATE );
4215
4216 psa_aead_abort( &operation );
4217
4218 /* Test for double setups. */
4219
4220 operation = psa_aead_operation_init( );
4221
4222 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4223
4224 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4225 PSA_ERROR_BAD_STATE );
4226
4227 psa_aead_abort( &operation );
4228
Paul Elliott481be342021-07-16 17:38:47 +01004229 /* ------------------------------------------------------- */
4230
Paul Elliottc23a9a02021-06-21 18:32:46 +01004231 operation = psa_aead_operation_init( );
4232
4233 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4234
4235 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4236 PSA_ERROR_BAD_STATE );
4237
4238 psa_aead_abort( &operation );
4239
Paul Elliott374a2be2021-07-16 17:53:40 +01004240 /* ------------------------------------------------------- */
4241
4242 operation = psa_aead_operation_init( );
4243
4244 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4245
4246 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4247 PSA_ERROR_BAD_STATE );
4248
4249 psa_aead_abort( &operation );
4250
4251 /* ------------------------------------------------------- */
4252
4253 operation = psa_aead_operation_init( );
4254
4255 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4256
4257 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4258 PSA_ERROR_BAD_STATE );
4259
4260 psa_aead_abort( &operation );
4261
Paul Elliottc23a9a02021-06-21 18:32:46 +01004262 /* Test for not setting a nonce. */
4263
4264 operation = psa_aead_operation_init( );
4265
4266 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4267
4268 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4269 additional_data->len ),
4270 PSA_ERROR_BAD_STATE );
4271
4272 psa_aead_abort( &operation );
4273
Paul Elliott7f628422021-09-01 12:08:29 +01004274 /* ------------------------------------------------------- */
4275
4276 operation = psa_aead_operation_init( );
4277
4278 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4279
4280 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4281 input_data->len, output_data,
4282 output_size, &output_length ),
4283 PSA_ERROR_BAD_STATE );
4284
4285 psa_aead_abort( &operation );
4286
Paul Elliottc23a9a02021-06-21 18:32:46 +01004287 /* Test for double setting nonce. */
4288
4289 operation = psa_aead_operation_init( );
4290
4291 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4292
4293 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4294
4295 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4296 PSA_ERROR_BAD_STATE );
4297
4298 psa_aead_abort( &operation );
4299
Paul Elliott374a2be2021-07-16 17:53:40 +01004300 /* Test for double generating nonce. */
4301
4302 operation = psa_aead_operation_init( );
4303
4304 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4305
4306 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4307 PSA_AEAD_NONCE_MAX_SIZE,
4308 &nonce_length ) );
4309
4310 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4311 PSA_AEAD_NONCE_MAX_SIZE,
4312 &nonce_length ),
4313 PSA_ERROR_BAD_STATE );
4314
4315
4316 psa_aead_abort( &operation );
4317
4318 /* Test for generate nonce then set and vice versa */
4319
4320 operation = psa_aead_operation_init( );
4321
4322 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4323
4324 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4325 PSA_AEAD_NONCE_MAX_SIZE,
4326 &nonce_length ) );
4327
4328 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4329 PSA_ERROR_BAD_STATE );
4330
4331 psa_aead_abort( &operation );
4332
4333 /* ------------------------------------------------------- */
4334
4335 operation = psa_aead_operation_init( );
4336
4337 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4338
4339 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4340
4341 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4342 PSA_AEAD_NONCE_MAX_SIZE,
4343 &nonce_length ),
4344 PSA_ERROR_BAD_STATE );
4345
4346 psa_aead_abort( &operation );
4347
Paul Elliott7220cae2021-06-22 17:25:57 +01004348 /* Test for generating nonce in decrypt setup. */
4349
4350 operation = psa_aead_operation_init( );
4351
4352 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4353
4354 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4355 PSA_AEAD_NONCE_MAX_SIZE,
4356 &nonce_length ),
4357 PSA_ERROR_BAD_STATE );
4358
4359 psa_aead_abort( &operation );
4360
Paul Elliottc23a9a02021-06-21 18:32:46 +01004361 /* Test for setting lengths twice. */
4362
4363 operation = psa_aead_operation_init( );
4364
4365 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4366
4367 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4368
4369 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4370 input_data->len ) );
4371
4372 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4373 input_data->len ),
4374 PSA_ERROR_BAD_STATE );
4375
4376 psa_aead_abort( &operation );
4377
4378 /* Test for setting lengths after already starting data. */
4379
4380 operation = psa_aead_operation_init( );
4381
4382 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4383
4384 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4385
4386 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4387 input_data->len, output_data,
4388 output_size, &output_length ) );
4389
4390 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4391 input_data->len ),
4392 PSA_ERROR_BAD_STATE );
4393
4394 psa_aead_abort( &operation );
4395
Paul Elliott243080c2021-07-21 19:01:17 +01004396 /* Test for not sending any additional data or data after setting non zero
4397 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004398
4399 operation = psa_aead_operation_init( );
4400
4401 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4402
4403 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4404
4405 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4406 input_data->len ) );
4407
4408 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4409 finish_output_size,
4410 &output_part_length,
4411 tag_buffer, tag_length,
4412 &tag_size ),
4413 PSA_ERROR_INVALID_ARGUMENT );
4414
4415 psa_aead_abort( &operation );
4416
Paul Elliott243080c2021-07-21 19:01:17 +01004417 /* Test for not sending any additional data or data after setting non-zero
4418 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004419
4420 operation = psa_aead_operation_init( );
4421
4422 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4423
4424 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4425
4426 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4427 input_data->len ) );
4428
4429 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4430 finish_output_size,
4431 &output_part_length,
4432 tag_buffer,
4433 tag_length ),
4434 PSA_ERROR_INVALID_ARGUMENT );
4435
4436 psa_aead_abort( &operation );
4437
Paul Elliott243080c2021-07-21 19:01:17 +01004438 /* Test for not sending any additional data after setting a non-zero length
4439 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004440
4441 operation = psa_aead_operation_init( );
4442
4443 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4444
4445 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4446
4447 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4448 input_data->len ) );
4449
4450 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4451 input_data->len, output_data,
4452 output_size, &output_length ),
4453 PSA_ERROR_INVALID_ARGUMENT );
4454
4455 psa_aead_abort( &operation );
4456
Paul Elliottb0450fe2021-09-01 15:06:26 +01004457 /* Test for sending too much additional data after setting lengths. */
4458
4459 operation = psa_aead_operation_init( );
4460
4461 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4462
4463 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4464
4465 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4466
4467
4468 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4469 additional_data->len ),
4470 PSA_ERROR_INVALID_ARGUMENT );
4471
4472 psa_aead_abort( &operation );
4473
4474 /* Test for sending too much data after setting lengths. */
4475
4476 operation = psa_aead_operation_init( );
4477
4478 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4479
4480 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4481
4482 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4483
4484 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4485 input_data->len, output_data,
4486 output_size, &output_length ),
4487 PSA_ERROR_INVALID_ARGUMENT );
4488
4489 psa_aead_abort( &operation );
4490
Paul Elliottc23a9a02021-06-21 18:32:46 +01004491 /* Test sending additional data after data. */
4492
4493 operation = psa_aead_operation_init( );
4494
4495 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4496
4497 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4498
4499 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4500 input_data->len, output_data,
4501 output_size, &output_length ) );
4502
4503 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4504 additional_data->len ),
4505 PSA_ERROR_BAD_STATE );
4506
4507 psa_aead_abort( &operation );
4508
Paul Elliott534d0b42021-06-22 19:15:20 +01004509 /* Test calling finish on decryption. */
4510
4511 operation = psa_aead_operation_init( );
4512
4513 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4514
4515 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4516
4517 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4518 finish_output_size,
4519 &output_part_length,
4520 tag_buffer, tag_length,
4521 &tag_size ),
4522 PSA_ERROR_BAD_STATE );
4523
4524 psa_aead_abort( &operation );
4525
4526 /* Test calling verify on encryption. */
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
4534 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4535 finish_output_size,
4536 &output_part_length,
4537 tag_buffer,
4538 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004539 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004540
4541 psa_aead_abort( &operation );
4542
4543
Paul Elliottc23a9a02021-06-21 18:32:46 +01004544exit:
4545 psa_destroy_key( key );
4546 psa_aead_abort( &operation );
4547 mbedtls_free( output_data );
4548 mbedtls_free( final_data );
4549 PSA_DONE( );
4550}
4551/* END_CASE */
4552
4553/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004554void signature_size( int type_arg,
4555 int bits,
4556 int alg_arg,
4557 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004558{
4559 psa_key_type_t type = type_arg;
4560 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004561 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004562
Gilles Peskinefe11b722018-12-18 00:24:04 +01004563 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004564
Gilles Peskinee59236f2018-01-27 23:32:46 +01004565exit:
4566 ;
4567}
4568/* END_CASE */
4569
4570/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004571void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4572 int alg_arg, data_t *input_data,
4573 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004574{
Ronald Cron5425a212020-08-04 14:58:35 +02004575 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004576 psa_key_type_t key_type = key_type_arg;
4577 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004578 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004579 unsigned char *signature = NULL;
4580 size_t signature_size;
4581 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004582 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004583
Gilles Peskine8817f612018-12-18 00:18:46 +01004584 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004585
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004586 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004587 psa_set_key_algorithm( &attributes, alg );
4588 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004589
Gilles Peskine049c7532019-05-15 20:22:09 +02004590 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004591 &key ) );
4592 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004593 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004594
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004595 /* Allocate a buffer which has the size advertized by the
4596 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004597 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004598 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004599 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004600 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004601 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004602
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004603 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004604 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004605 input_data->x, input_data->len,
4606 signature, signature_size,
4607 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004608 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004609 ASSERT_COMPARE( output_data->x, output_data->len,
4610 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004611
4612exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004613 /*
4614 * Key attributes may have been returned by psa_get_key_attributes()
4615 * thus reset them as required.
4616 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004617 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004618
Ronald Cron5425a212020-08-04 14:58:35 +02004619 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004620 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004621 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004622}
4623/* END_CASE */
4624
4625/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004626void sign_hash_fail( int key_type_arg, data_t *key_data,
4627 int alg_arg, data_t *input_data,
4628 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004629{
Ronald Cron5425a212020-08-04 14:58:35 +02004630 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004631 psa_key_type_t key_type = key_type_arg;
4632 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004633 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004634 psa_status_t actual_status;
4635 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004636 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004637 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004638 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004639
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004640 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004641
Gilles Peskine8817f612018-12-18 00:18:46 +01004642 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004643
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004644 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004645 psa_set_key_algorithm( &attributes, alg );
4646 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004647
Gilles Peskine049c7532019-05-15 20:22:09 +02004648 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004649 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004650
Ronald Cron5425a212020-08-04 14:58:35 +02004651 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004652 input_data->x, input_data->len,
4653 signature, signature_size,
4654 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004655 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004656 /* The value of *signature_length is unspecified on error, but
4657 * whatever it is, it should be less than signature_size, so that
4658 * if the caller tries to read *signature_length bytes without
4659 * checking the error code then they don't overflow a buffer. */
4660 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004661
4662exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004663 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004664 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004665 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004666 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004667}
4668/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004669
4670/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004671void sign_verify_hash( int key_type_arg, data_t *key_data,
4672 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004673{
Ronald Cron5425a212020-08-04 14:58:35 +02004674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004675 psa_key_type_t key_type = key_type_arg;
4676 psa_algorithm_t alg = alg_arg;
4677 size_t key_bits;
4678 unsigned char *signature = NULL;
4679 size_t signature_size;
4680 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004682
Gilles Peskine8817f612018-12-18 00:18:46 +01004683 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004684
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004685 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004686 psa_set_key_algorithm( &attributes, alg );
4687 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004688
Gilles Peskine049c7532019-05-15 20:22:09 +02004689 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004690 &key ) );
4691 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004692 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004693
4694 /* Allocate a buffer which has the size advertized by the
4695 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004696 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004697 key_bits, alg );
4698 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004699 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004700 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004701
4702 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004703 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004704 input_data->x, input_data->len,
4705 signature, signature_size,
4706 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004707 /* Check that the signature length looks sensible. */
4708 TEST_ASSERT( signature_length <= signature_size );
4709 TEST_ASSERT( signature_length > 0 );
4710
4711 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004712 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004713 input_data->x, input_data->len,
4714 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004715
4716 if( input_data->len != 0 )
4717 {
4718 /* Flip a bit in the input and verify that the signature is now
4719 * detected as invalid. Flip a bit at the beginning, not at the end,
4720 * because ECDSA may ignore the last few bits of the input. */
4721 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004722 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004723 input_data->x, input_data->len,
4724 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004725 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004726 }
4727
4728exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004729 /*
4730 * Key attributes may have been returned by psa_get_key_attributes()
4731 * thus reset them as required.
4732 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004733 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004734
Ronald Cron5425a212020-08-04 14:58:35 +02004735 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004736 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004737 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004738}
4739/* END_CASE */
4740
4741/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004742void verify_hash( int key_type_arg, data_t *key_data,
4743 int alg_arg, data_t *hash_data,
4744 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004745{
Ronald Cron5425a212020-08-04 14:58:35 +02004746 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004747 psa_key_type_t key_type = key_type_arg;
4748 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004749 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004750
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004751 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004752
Gilles Peskine8817f612018-12-18 00:18:46 +01004753 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004754
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004755 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004756 psa_set_key_algorithm( &attributes, alg );
4757 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004758
Gilles Peskine049c7532019-05-15 20:22:09 +02004759 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004760 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004761
Ronald Cron5425a212020-08-04 14:58:35 +02004762 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004763 hash_data->x, hash_data->len,
4764 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004765
itayzafrir5c753392018-05-08 11:18:38 +03004766exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004767 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004768 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004769 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004770}
4771/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004772
4773/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004774void verify_hash_fail( int key_type_arg, data_t *key_data,
4775 int alg_arg, data_t *hash_data,
4776 data_t *signature_data,
4777 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004778{
Ronald Cron5425a212020-08-04 14:58:35 +02004779 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004780 psa_key_type_t key_type = key_type_arg;
4781 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004782 psa_status_t actual_status;
4783 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004784 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004785
Gilles Peskine8817f612018-12-18 00:18:46 +01004786 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004787
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004788 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004789 psa_set_key_algorithm( &attributes, alg );
4790 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004791
Gilles Peskine049c7532019-05-15 20:22:09 +02004792 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004793 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004794
Ronald Cron5425a212020-08-04 14:58:35 +02004795 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004796 hash_data->x, hash_data->len,
4797 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004798 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004799
4800exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004801 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004802 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004803 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004804}
4805/* END_CASE */
4806
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004807/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004808void sign_message_deterministic( int key_type_arg,
4809 data_t *key_data,
4810 int alg_arg,
4811 data_t *input_data,
4812 data_t *output_data )
4813{
4814 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4815 psa_key_type_t key_type = key_type_arg;
4816 psa_algorithm_t alg = alg_arg;
4817 size_t key_bits;
4818 unsigned char *signature = NULL;
4819 size_t signature_size;
4820 size_t signature_length = 0xdeadbeef;
4821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4822
4823 PSA_ASSERT( psa_crypto_init( ) );
4824
4825 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4826 psa_set_key_algorithm( &attributes, alg );
4827 psa_set_key_type( &attributes, key_type );
4828
4829 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4830 &key ) );
4831 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4832 key_bits = psa_get_key_bits( &attributes );
4833
4834 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4835 TEST_ASSERT( signature_size != 0 );
4836 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4837 ASSERT_ALLOC( signature, signature_size );
4838
4839 PSA_ASSERT( psa_sign_message( key, alg,
4840 input_data->x, input_data->len,
4841 signature, signature_size,
4842 &signature_length ) );
4843
4844 ASSERT_COMPARE( output_data->x, output_data->len,
4845 signature, signature_length );
4846
4847exit:
4848 psa_reset_key_attributes( &attributes );
4849
4850 psa_destroy_key( key );
4851 mbedtls_free( signature );
4852 PSA_DONE( );
4853
4854}
4855/* END_CASE */
4856
4857/* BEGIN_CASE */
4858void sign_message_fail( int key_type_arg,
4859 data_t *key_data,
4860 int alg_arg,
4861 data_t *input_data,
4862 int signature_size_arg,
4863 int expected_status_arg )
4864{
4865 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4866 psa_key_type_t key_type = key_type_arg;
4867 psa_algorithm_t alg = alg_arg;
4868 size_t signature_size = signature_size_arg;
4869 psa_status_t actual_status;
4870 psa_status_t expected_status = expected_status_arg;
4871 unsigned char *signature = NULL;
4872 size_t signature_length = 0xdeadbeef;
4873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4874
4875 ASSERT_ALLOC( signature, signature_size );
4876
4877 PSA_ASSERT( psa_crypto_init( ) );
4878
4879 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4880 psa_set_key_algorithm( &attributes, alg );
4881 psa_set_key_type( &attributes, key_type );
4882
4883 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4884 &key ) );
4885
4886 actual_status = psa_sign_message( key, alg,
4887 input_data->x, input_data->len,
4888 signature, signature_size,
4889 &signature_length );
4890 TEST_EQUAL( actual_status, expected_status );
4891 /* The value of *signature_length is unspecified on error, but
4892 * whatever it is, it should be less than signature_size, so that
4893 * if the caller tries to read *signature_length bytes without
4894 * checking the error code then they don't overflow a buffer. */
4895 TEST_ASSERT( signature_length <= signature_size );
4896
4897exit:
4898 psa_reset_key_attributes( &attributes );
4899 psa_destroy_key( key );
4900 mbedtls_free( signature );
4901 PSA_DONE( );
4902}
4903/* END_CASE */
4904
4905/* BEGIN_CASE */
4906void sign_verify_message( int key_type_arg,
4907 data_t *key_data,
4908 int alg_arg,
4909 data_t *input_data )
4910{
4911 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4912 psa_key_type_t key_type = key_type_arg;
4913 psa_algorithm_t alg = alg_arg;
4914 size_t key_bits;
4915 unsigned char *signature = NULL;
4916 size_t signature_size;
4917 size_t signature_length = 0xdeadbeef;
4918 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4919
4920 PSA_ASSERT( psa_crypto_init( ) );
4921
4922 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4923 PSA_KEY_USAGE_VERIFY_MESSAGE );
4924 psa_set_key_algorithm( &attributes, alg );
4925 psa_set_key_type( &attributes, key_type );
4926
4927 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4928 &key ) );
4929 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4930 key_bits = psa_get_key_bits( &attributes );
4931
4932 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4933 TEST_ASSERT( signature_size != 0 );
4934 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4935 ASSERT_ALLOC( signature, signature_size );
4936
4937 PSA_ASSERT( psa_sign_message( key, alg,
4938 input_data->x, input_data->len,
4939 signature, signature_size,
4940 &signature_length ) );
4941 TEST_ASSERT( signature_length <= signature_size );
4942 TEST_ASSERT( signature_length > 0 );
4943
4944 PSA_ASSERT( psa_verify_message( key, alg,
4945 input_data->x, input_data->len,
4946 signature, signature_length ) );
4947
4948 if( input_data->len != 0 )
4949 {
4950 /* Flip a bit in the input and verify that the signature is now
4951 * detected as invalid. Flip a bit at the beginning, not at the end,
4952 * because ECDSA may ignore the last few bits of the input. */
4953 input_data->x[0] ^= 1;
4954 TEST_EQUAL( psa_verify_message( key, alg,
4955 input_data->x, input_data->len,
4956 signature, signature_length ),
4957 PSA_ERROR_INVALID_SIGNATURE );
4958 }
4959
4960exit:
4961 psa_reset_key_attributes( &attributes );
4962
4963 psa_destroy_key( key );
4964 mbedtls_free( signature );
4965 PSA_DONE( );
4966}
4967/* END_CASE */
4968
4969/* BEGIN_CASE */
4970void verify_message( int key_type_arg,
4971 data_t *key_data,
4972 int alg_arg,
4973 data_t *input_data,
4974 data_t *signature_data )
4975{
4976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4977 psa_key_type_t key_type = key_type_arg;
4978 psa_algorithm_t alg = alg_arg;
4979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4980
4981 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4982
4983 PSA_ASSERT( psa_crypto_init( ) );
4984
4985 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4986 psa_set_key_algorithm( &attributes, alg );
4987 psa_set_key_type( &attributes, key_type );
4988
4989 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4990 &key ) );
4991
4992 PSA_ASSERT( psa_verify_message( key, alg,
4993 input_data->x, input_data->len,
4994 signature_data->x, signature_data->len ) );
4995
4996exit:
4997 psa_reset_key_attributes( &attributes );
4998 psa_destroy_key( key );
4999 PSA_DONE( );
5000}
5001/* END_CASE */
5002
5003/* BEGIN_CASE */
5004void verify_message_fail( int key_type_arg,
5005 data_t *key_data,
5006 int alg_arg,
5007 data_t *hash_data,
5008 data_t *signature_data,
5009 int expected_status_arg )
5010{
5011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5012 psa_key_type_t key_type = key_type_arg;
5013 psa_algorithm_t alg = alg_arg;
5014 psa_status_t actual_status;
5015 psa_status_t expected_status = expected_status_arg;
5016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5017
5018 PSA_ASSERT( psa_crypto_init( ) );
5019
5020 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5021 psa_set_key_algorithm( &attributes, alg );
5022 psa_set_key_type( &attributes, key_type );
5023
5024 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5025 &key ) );
5026
5027 actual_status = psa_verify_message( key, alg,
5028 hash_data->x, hash_data->len,
5029 signature_data->x,
5030 signature_data->len );
5031 TEST_EQUAL( actual_status, expected_status );
5032
5033exit:
5034 psa_reset_key_attributes( &attributes );
5035 psa_destroy_key( key );
5036 PSA_DONE( );
5037}
5038/* END_CASE */
5039
5040/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005041void asymmetric_encrypt( int key_type_arg,
5042 data_t *key_data,
5043 int alg_arg,
5044 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005045 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005046 int expected_output_length_arg,
5047 int expected_status_arg )
5048{
Ronald Cron5425a212020-08-04 14:58:35 +02005049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005050 psa_key_type_t key_type = key_type_arg;
5051 psa_algorithm_t alg = alg_arg;
5052 size_t expected_output_length = expected_output_length_arg;
5053 size_t key_bits;
5054 unsigned char *output = NULL;
5055 size_t output_size;
5056 size_t output_length = ~0;
5057 psa_status_t actual_status;
5058 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005060
Gilles Peskine8817f612018-12-18 00:18:46 +01005061 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005062
Gilles Peskine656896e2018-06-29 19:12:28 +02005063 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005064 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5065 psa_set_key_algorithm( &attributes, alg );
5066 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005067 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005068 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005069
5070 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005071 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005072 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005073
Gilles Peskine656896e2018-06-29 19:12:28 +02005074 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005075 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005076 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005077
5078 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005079 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005080 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005081 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005082 output, output_size,
5083 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005084 TEST_EQUAL( actual_status, expected_status );
5085 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005086
Gilles Peskine68428122018-06-30 18:42:41 +02005087 /* If the label is empty, the test framework puts a non-null pointer
5088 * in label->x. Test that a null pointer works as well. */
5089 if( label->len == 0 )
5090 {
5091 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005092 if( output_size != 0 )
5093 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005094 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005095 input_data->x, input_data->len,
5096 NULL, label->len,
5097 output, output_size,
5098 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005099 TEST_EQUAL( actual_status, expected_status );
5100 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005101 }
5102
Gilles Peskine656896e2018-06-29 19:12:28 +02005103exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005104 /*
5105 * Key attributes may have been returned by psa_get_key_attributes()
5106 * thus reset them as required.
5107 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005108 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005109
Ronald Cron5425a212020-08-04 14:58:35 +02005110 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005111 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005112 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005113}
5114/* END_CASE */
5115
5116/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005117void asymmetric_encrypt_decrypt( int key_type_arg,
5118 data_t *key_data,
5119 int alg_arg,
5120 data_t *input_data,
5121 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005122{
Ronald Cron5425a212020-08-04 14:58:35 +02005123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005124 psa_key_type_t key_type = key_type_arg;
5125 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005126 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005127 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005128 size_t output_size;
5129 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005130 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005131 size_t output2_size;
5132 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005134
Gilles Peskine8817f612018-12-18 00:18:46 +01005135 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5138 psa_set_key_algorithm( &attributes, alg );
5139 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005140
Gilles Peskine049c7532019-05-15 20:22:09 +02005141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005142 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005143
5144 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005145 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005146 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005147
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005148 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005149 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005150 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005151
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005152 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005153 TEST_ASSERT( output2_size <=
5154 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5155 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005156 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005157
Gilles Peskineeebd7382018-06-08 18:11:54 +02005158 /* We test encryption by checking that encrypt-then-decrypt gives back
5159 * the original plaintext because of the non-optional random
5160 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005161 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005162 input_data->x, input_data->len,
5163 label->x, label->len,
5164 output, output_size,
5165 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005166 /* We don't know what ciphertext length to expect, but check that
5167 * it looks sensible. */
5168 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005169
Ronald Cron5425a212020-08-04 14:58:35 +02005170 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005171 output, output_length,
5172 label->x, label->len,
5173 output2, output2_size,
5174 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005175 ASSERT_COMPARE( input_data->x, input_data->len,
5176 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005177
5178exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005179 /*
5180 * Key attributes may have been returned by psa_get_key_attributes()
5181 * thus reset them as required.
5182 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005183 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005184
Ronald Cron5425a212020-08-04 14:58:35 +02005185 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005186 mbedtls_free( output );
5187 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005188 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005189}
5190/* END_CASE */
5191
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005192/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005193void asymmetric_decrypt( int key_type_arg,
5194 data_t *key_data,
5195 int alg_arg,
5196 data_t *input_data,
5197 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005198 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005199{
Ronald Cron5425a212020-08-04 14:58:35 +02005200 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005201 psa_key_type_t key_type = key_type_arg;
5202 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005203 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005204 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005205 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005206 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005208
Gilles Peskine8817f612018-12-18 00:18:46 +01005209 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005210
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005211 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5212 psa_set_key_algorithm( &attributes, alg );
5213 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005214
Gilles Peskine049c7532019-05-15 20:22:09 +02005215 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005216 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005217
gabor-mezei-armceface22021-01-21 12:26:17 +01005218 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5219 key_bits = psa_get_key_bits( &attributes );
5220
5221 /* Determine the maximum ciphertext length */
5222 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5223 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5224 ASSERT_ALLOC( output, output_size );
5225
Ronald Cron5425a212020-08-04 14:58:35 +02005226 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005227 input_data->x, input_data->len,
5228 label->x, label->len,
5229 output,
5230 output_size,
5231 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005232 ASSERT_COMPARE( expected_data->x, expected_data->len,
5233 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005234
Gilles Peskine68428122018-06-30 18:42:41 +02005235 /* If the label is empty, the test framework puts a non-null pointer
5236 * in label->x. Test that a null pointer works as well. */
5237 if( label->len == 0 )
5238 {
5239 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005240 if( output_size != 0 )
5241 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005242 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005243 input_data->x, input_data->len,
5244 NULL, label->len,
5245 output,
5246 output_size,
5247 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005248 ASSERT_COMPARE( expected_data->x, expected_data->len,
5249 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005250 }
5251
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005252exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005253 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005254 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005255 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005256 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005257}
5258/* END_CASE */
5259
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005260/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005261void asymmetric_decrypt_fail( int key_type_arg,
5262 data_t *key_data,
5263 int alg_arg,
5264 data_t *input_data,
5265 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005266 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005267 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005268{
Ronald Cron5425a212020-08-04 14:58:35 +02005269 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005270 psa_key_type_t key_type = key_type_arg;
5271 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005272 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005273 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005274 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005275 psa_status_t actual_status;
5276 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005278
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005279 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005280
Gilles Peskine8817f612018-12-18 00:18:46 +01005281 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005282
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005283 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5284 psa_set_key_algorithm( &attributes, alg );
5285 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005286
Gilles Peskine049c7532019-05-15 20:22:09 +02005287 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005288 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005289
Ronald Cron5425a212020-08-04 14:58:35 +02005290 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005291 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005292 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005293 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005294 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005295 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005296 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005297
Gilles Peskine68428122018-06-30 18:42:41 +02005298 /* If the label is empty, the test framework puts a non-null pointer
5299 * in label->x. Test that a null pointer works as well. */
5300 if( label->len == 0 )
5301 {
5302 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005303 if( output_size != 0 )
5304 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005305 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005306 input_data->x, input_data->len,
5307 NULL, label->len,
5308 output, output_size,
5309 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005310 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005311 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005312 }
5313
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005314exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005315 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005316 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005317 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005318 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005319}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005320/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005321
5322/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005323void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005324{
5325 /* Test each valid way of initializing the object, except for `= {0}`, as
5326 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5327 * though it's OK by the C standard. We could test for this, but we'd need
5328 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005329 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005330 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5331 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5332 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005333
5334 memset( &zero, 0, sizeof( zero ) );
5335
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005337 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005338 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005339 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005340 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005341 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005342 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005343
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005345 PSA_ASSERT( psa_key_derivation_abort(&func) );
5346 PSA_ASSERT( psa_key_derivation_abort(&init) );
5347 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005348}
5349/* END_CASE */
5350
Janos Follath16de4a42019-06-13 16:32:24 +01005351/* BEGIN_CASE */
5352void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005353{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005354 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005355 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005356 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005357
Gilles Peskine8817f612018-12-18 00:18:46 +01005358 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005359
Janos Follath16de4a42019-06-13 16:32:24 +01005360 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005361 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005362
5363exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005364 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005365 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005366}
5367/* END_CASE */
5368
Janos Follathaf3c2a02019-06-12 12:34:34 +01005369/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005370void derive_set_capacity( int alg_arg, int capacity_arg,
5371 int expected_status_arg )
5372{
5373 psa_algorithm_t alg = alg_arg;
5374 size_t capacity = capacity_arg;
5375 psa_status_t expected_status = expected_status_arg;
5376 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5377
5378 PSA_ASSERT( psa_crypto_init( ) );
5379
5380 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5381
5382 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5383 expected_status );
5384
5385exit:
5386 psa_key_derivation_abort( &operation );
5387 PSA_DONE( );
5388}
5389/* END_CASE */
5390
5391/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005392void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005393 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005394 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005395 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005396 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005397 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005398 int expected_status_arg3,
5399 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005400{
5401 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005402 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5403 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005404 psa_status_t expected_statuses[] = {expected_status_arg1,
5405 expected_status_arg2,
5406 expected_status_arg3};
5407 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005408 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5409 MBEDTLS_SVC_KEY_ID_INIT,
5410 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005411 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5413 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005414 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005415 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005416 psa_status_t expected_output_status = expected_output_status_arg;
5417 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005418
5419 PSA_ASSERT( psa_crypto_init( ) );
5420
5421 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5422 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005423
5424 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5425
5426 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5427 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005428 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005429 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005430 psa_set_key_type( &attributes, key_types[i] );
5431 PSA_ASSERT( psa_import_key( &attributes,
5432 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005433 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005434 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5435 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5436 {
5437 // When taking a private key as secret input, use key agreement
5438 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005439 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5440 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005441 expected_statuses[i] );
5442 }
5443 else
5444 {
5445 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005446 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005447 expected_statuses[i] );
5448 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005449 }
5450 else
5451 {
5452 TEST_EQUAL( psa_key_derivation_input_bytes(
5453 &operation, steps[i],
5454 inputs[i]->x, inputs[i]->len ),
5455 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005456 }
5457 }
5458
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005459 if( output_key_type != PSA_KEY_TYPE_NONE )
5460 {
5461 psa_reset_key_attributes( &attributes );
5462 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5463 psa_set_key_bits( &attributes, 8 );
5464 actual_output_status =
5465 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005466 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005467 }
5468 else
5469 {
5470 uint8_t buffer[1];
5471 actual_output_status =
5472 psa_key_derivation_output_bytes( &operation,
5473 buffer, sizeof( buffer ) );
5474 }
5475 TEST_EQUAL( actual_output_status, expected_output_status );
5476
Janos Follathaf3c2a02019-06-12 12:34:34 +01005477exit:
5478 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005479 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5480 psa_destroy_key( keys[i] );
5481 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005482 PSA_DONE( );
5483}
5484/* END_CASE */
5485
Janos Follathd958bb72019-07-03 15:02:16 +01005486/* BEGIN_CASE */
5487void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005488{
Janos Follathd958bb72019-07-03 15:02:16 +01005489 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005490 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005491 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005492 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005493 unsigned char input1[] = "Input 1";
5494 size_t input1_length = sizeof( input1 );
5495 unsigned char input2[] = "Input 2";
5496 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005497 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005498 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005499 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5500 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5501 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005503
Gilles Peskine8817f612018-12-18 00:18:46 +01005504 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005505
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005506 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5507 psa_set_key_algorithm( &attributes, alg );
5508 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005509
Gilles Peskine73676cb2019-05-15 20:15:10 +02005510 PSA_ASSERT( psa_import_key( &attributes,
5511 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005512 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005513
5514 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005515 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5516 input1, input1_length,
5517 input2, input2_length,
5518 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005519 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005520
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005521 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005522 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005523 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005524
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005526
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005527 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005528 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005529
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005530exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005531 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005532 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005533 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005534}
5535/* END_CASE */
5536
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005537/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005538void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005539{
5540 uint8_t output_buffer[16];
5541 size_t buffer_size = 16;
5542 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005543 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005544
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005545 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5546 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005547 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005548
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005549 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005550 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005551
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005552 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005553
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005554 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5555 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005556 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005557
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005558 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005559 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005560
5561exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005562 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005563}
5564/* END_CASE */
5565
5566/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005567void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005568 int step1_arg, data_t *input1,
5569 int step2_arg, data_t *input2,
5570 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005571 int requested_capacity_arg,
5572 data_t *expected_output1,
5573 data_t *expected_output2 )
5574{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005575 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005576 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5577 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005578 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5579 MBEDTLS_SVC_KEY_ID_INIT,
5580 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005581 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005582 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005583 uint8_t *expected_outputs[2] =
5584 {expected_output1->x, expected_output2->x};
5585 size_t output_sizes[2] =
5586 {expected_output1->len, expected_output2->len};
5587 size_t output_buffer_size = 0;
5588 uint8_t *output_buffer = NULL;
5589 size_t expected_capacity;
5590 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005592 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005593 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005594
5595 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5596 {
5597 if( output_sizes[i] > output_buffer_size )
5598 output_buffer_size = output_sizes[i];
5599 if( output_sizes[i] == 0 )
5600 expected_outputs[i] = NULL;
5601 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005602 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005603 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005604
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005605 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5606 psa_set_key_algorithm( &attributes, alg );
5607 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005608
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005609 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005610 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5611 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5612 requested_capacity ) );
5613 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005614 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005615 switch( steps[i] )
5616 {
5617 case 0:
5618 break;
5619 case PSA_KEY_DERIVATION_INPUT_SECRET:
5620 PSA_ASSERT( psa_import_key( &attributes,
5621 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005622 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005623
5624 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5625 {
5626 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5627 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5628 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5629 }
5630
Gilles Peskine1468da72019-05-29 17:35:49 +02005631 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005632 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005633 break;
5634 default:
5635 PSA_ASSERT( psa_key_derivation_input_bytes(
5636 &operation, steps[i],
5637 inputs[i]->x, inputs[i]->len ) );
5638 break;
5639 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005640 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005641
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005642 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005643 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005644 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005645 expected_capacity = requested_capacity;
5646
5647 /* Expansion phase. */
5648 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5649 {
5650 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005651 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005652 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005653 if( expected_capacity == 0 && output_sizes[i] == 0 )
5654 {
5655 /* Reading 0 bytes when 0 bytes are available can go either way. */
5656 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005657 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005658 continue;
5659 }
5660 else if( expected_capacity == 0 ||
5661 output_sizes[i] > expected_capacity )
5662 {
5663 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005664 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005665 expected_capacity = 0;
5666 continue;
5667 }
5668 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005669 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005670 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005671 ASSERT_COMPARE( output_buffer, output_sizes[i],
5672 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005673 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005674 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005675 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005676 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005677 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005678 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005679 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005680
5681exit:
5682 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005683 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005684 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5685 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005686 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005687}
5688/* END_CASE */
5689
5690/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005691void derive_full( int alg_arg,
5692 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005693 data_t *input1,
5694 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005695 int requested_capacity_arg )
5696{
Ronald Cron5425a212020-08-04 14:58:35 +02005697 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005698 psa_algorithm_t alg = alg_arg;
5699 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005700 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005701 unsigned char output_buffer[16];
5702 size_t expected_capacity = requested_capacity;
5703 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005705
Gilles Peskine8817f612018-12-18 00:18:46 +01005706 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005707
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005708 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5709 psa_set_key_algorithm( &attributes, alg );
5710 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005711
Gilles Peskine049c7532019-05-15 20:22:09 +02005712 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005713 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005714
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005715 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5716 input1->x, input1->len,
5717 input2->x, input2->len,
5718 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005719 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005720
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005721 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005722 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005723 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005724
5725 /* Expansion phase. */
5726 while( current_capacity > 0 )
5727 {
5728 size_t read_size = sizeof( output_buffer );
5729 if( read_size > current_capacity )
5730 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005731 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005732 output_buffer,
5733 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005734 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005735 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005736 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005737 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005738 }
5739
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740 /* Check that the operation refuses to go over capacity. */
5741 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005742 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005743
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005745
5746exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005747 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005748 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005749 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005750}
5751/* END_CASE */
5752
Janos Follathe60c9052019-07-03 13:51:30 +01005753/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005754void derive_key_exercise( int alg_arg,
5755 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005756 data_t *input1,
5757 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005758 int derived_type_arg,
5759 int derived_bits_arg,
5760 int derived_usage_arg,
5761 int derived_alg_arg )
5762{
Ronald Cron5425a212020-08-04 14:58:35 +02005763 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5764 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005765 psa_algorithm_t alg = alg_arg;
5766 psa_key_type_t derived_type = derived_type_arg;
5767 size_t derived_bits = derived_bits_arg;
5768 psa_key_usage_t derived_usage = derived_usage_arg;
5769 psa_algorithm_t derived_alg = derived_alg_arg;
5770 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005771 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005773 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005774
Gilles Peskine8817f612018-12-18 00:18:46 +01005775 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005776
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005777 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5778 psa_set_key_algorithm( &attributes, alg );
5779 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005780 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005781 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005782
5783 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005784 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5785 input1->x, input1->len,
5786 input2->x, input2->len,
5787 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005788 goto exit;
5789
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005790 psa_set_key_usage_flags( &attributes, derived_usage );
5791 psa_set_key_algorithm( &attributes, derived_alg );
5792 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005793 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005794 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005795 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005796
5797 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005798 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005799 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5800 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005801
5802 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005803 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005804 goto exit;
5805
5806exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005807 /*
5808 * Key attributes may have been returned by psa_get_key_attributes()
5809 * thus reset them as required.
5810 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005811 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005812
5813 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005814 psa_destroy_key( base_key );
5815 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005816 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005817}
5818/* END_CASE */
5819
Janos Follath42fd8882019-07-03 14:17:09 +01005820/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005821void derive_key_export( int alg_arg,
5822 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005823 data_t *input1,
5824 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005825 int bytes1_arg,
5826 int bytes2_arg )
5827{
Ronald Cron5425a212020-08-04 14:58:35 +02005828 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5829 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005830 psa_algorithm_t alg = alg_arg;
5831 size_t bytes1 = bytes1_arg;
5832 size_t bytes2 = bytes2_arg;
5833 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005834 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005835 uint8_t *output_buffer = NULL;
5836 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005837 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5838 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005839 size_t length;
5840
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005841 ASSERT_ALLOC( output_buffer, capacity );
5842 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005843 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005844
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005845 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5846 psa_set_key_algorithm( &base_attributes, alg );
5847 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005848 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005849 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005850
5851 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005852 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5853 input1->x, input1->len,
5854 input2->x, input2->len,
5855 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005856 goto exit;
5857
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005858 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005859 output_buffer,
5860 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005861 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005862
5863 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005864 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5865 input1->x, input1->len,
5866 input2->x, input2->len,
5867 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005868 goto exit;
5869
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005870 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5871 psa_set_key_algorithm( &derived_attributes, 0 );
5872 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005873 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005874 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005875 &derived_key ) );
5876 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005877 export_buffer, bytes1,
5878 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005879 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005880 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005881 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005882 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005883 &derived_key ) );
5884 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005885 export_buffer + bytes1, bytes2,
5886 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005887 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005888
5889 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005890 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5891 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005892
5893exit:
5894 mbedtls_free( output_buffer );
5895 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005896 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005897 psa_destroy_key( base_key );
5898 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005899 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005900}
5901/* END_CASE */
5902
5903/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005904void derive_key( int alg_arg,
5905 data_t *key_data, data_t *input1, data_t *input2,
5906 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005907 int expected_status_arg,
5908 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005909{
Ronald Cron5425a212020-08-04 14:58:35 +02005910 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5911 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005912 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005913 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005914 size_t bits = bits_arg;
5915 psa_status_t expected_status = expected_status_arg;
5916 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5917 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5918 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5919
5920 PSA_ASSERT( psa_crypto_init( ) );
5921
5922 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5923 psa_set_key_algorithm( &base_attributes, alg );
5924 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5925 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005926 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005927
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005928 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5929 input1->x, input1->len,
5930 input2->x, input2->len,
5931 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005932 goto exit;
5933
5934 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5935 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005936 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005937 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005938
5939 psa_status_t status =
5940 psa_key_derivation_output_key( &derived_attributes,
5941 &operation,
5942 &derived_key );
5943 if( is_large_output > 0 )
5944 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5945 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005946
5947exit:
5948 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005949 psa_destroy_key( base_key );
5950 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005951 PSA_DONE( );
5952}
5953/* END_CASE */
5954
5955/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005956void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005957 int our_key_type_arg, int our_key_alg_arg,
5958 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005959 int expected_status_arg )
5960{
Ronald Cron5425a212020-08-04 14:58:35 +02005961 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005962 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005963 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005964 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005965 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005967 psa_status_t expected_status = expected_status_arg;
5968 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005969
Gilles Peskine8817f612018-12-18 00:18:46 +01005970 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005971
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005972 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005973 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005974 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005975 PSA_ASSERT( psa_import_key( &attributes,
5976 our_key_data->x, our_key_data->len,
5977 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005978
Gilles Peskine77f40d82019-04-11 21:27:06 +02005979 /* The tests currently include inputs that should fail at either step.
5980 * Test cases that fail at the setup step should be changed to call
5981 * key_derivation_setup instead, and this function should be renamed
5982 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005983 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005984 if( status == PSA_SUCCESS )
5985 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005986 TEST_EQUAL( psa_key_derivation_key_agreement(
5987 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5988 our_key,
5989 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005990 expected_status );
5991 }
5992 else
5993 {
5994 TEST_ASSERT( status == expected_status );
5995 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005996
5997exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005998 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005999 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006000 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006001}
6002/* END_CASE */
6003
6004/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006005void raw_key_agreement( int alg_arg,
6006 int our_key_type_arg, data_t *our_key_data,
6007 data_t *peer_key_data,
6008 data_t *expected_output )
6009{
Ronald Cron5425a212020-08-04 14:58:35 +02006010 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006011 psa_algorithm_t alg = alg_arg;
6012 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006014 unsigned char *output = NULL;
6015 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006016 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006017
6018 ASSERT_ALLOC( output, expected_output->len );
6019 PSA_ASSERT( psa_crypto_init( ) );
6020
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006021 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6022 psa_set_key_algorithm( &attributes, alg );
6023 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006024 PSA_ASSERT( psa_import_key( &attributes,
6025 our_key_data->x, our_key_data->len,
6026 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006027
gabor-mezei-armceface22021-01-21 12:26:17 +01006028 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6029 key_bits = psa_get_key_bits( &attributes );
6030
Gilles Peskinebe697d82019-05-16 18:00:41 +02006031 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6032 peer_key_data->x, peer_key_data->len,
6033 output, expected_output->len,
6034 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006035 ASSERT_COMPARE( output, output_length,
6036 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006037 TEST_ASSERT( output_length <=
6038 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6039 TEST_ASSERT( output_length <=
6040 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006041
6042exit:
6043 mbedtls_free( output );
6044 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006045 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006046}
6047/* END_CASE */
6048
6049/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006050void key_agreement_capacity( int alg_arg,
6051 int our_key_type_arg, data_t *our_key_data,
6052 data_t *peer_key_data,
6053 int expected_capacity_arg )
6054{
Ronald Cron5425a212020-08-04 14:58:35 +02006055 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006056 psa_algorithm_t alg = alg_arg;
6057 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006058 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006060 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006061 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006062
Gilles Peskine8817f612018-12-18 00:18:46 +01006063 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006064
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006065 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6066 psa_set_key_algorithm( &attributes, alg );
6067 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006068 PSA_ASSERT( psa_import_key( &attributes,
6069 our_key_data->x, our_key_data->len,
6070 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006071
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006072 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006073 PSA_ASSERT( psa_key_derivation_key_agreement(
6074 &operation,
6075 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6076 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006077 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6078 {
6079 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006080 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006081 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006082 NULL, 0 ) );
6083 }
Gilles Peskine59685592018-09-18 12:11:34 +02006084
Gilles Peskinebf491972018-10-25 22:36:12 +02006085 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006086 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006087 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006088 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006089
Gilles Peskinebf491972018-10-25 22:36:12 +02006090 /* Test the actual capacity by reading the output. */
6091 while( actual_capacity > sizeof( output ) )
6092 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006093 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006094 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006095 actual_capacity -= sizeof( output );
6096 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006097 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006098 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006099 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006100 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006101
Gilles Peskine59685592018-09-18 12:11:34 +02006102exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006103 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006104 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006105 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006106}
6107/* END_CASE */
6108
6109/* BEGIN_CASE */
6110void key_agreement_output( int alg_arg,
6111 int our_key_type_arg, data_t *our_key_data,
6112 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006113 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006114{
Ronald Cron5425a212020-08-04 14:58:35 +02006115 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006116 psa_algorithm_t alg = alg_arg;
6117 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006118 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006119 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006120 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006121
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006122 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6123 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006124
Gilles Peskine8817f612018-12-18 00:18:46 +01006125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006126
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006127 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6128 psa_set_key_algorithm( &attributes, alg );
6129 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006130 PSA_ASSERT( psa_import_key( &attributes,
6131 our_key_data->x, our_key_data->len,
6132 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006133
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006134 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006135 PSA_ASSERT( psa_key_derivation_key_agreement(
6136 &operation,
6137 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6138 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006139 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6140 {
6141 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006142 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006143 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006144 NULL, 0 ) );
6145 }
Gilles Peskine59685592018-09-18 12:11:34 +02006146
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006147 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006148 actual_output,
6149 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006150 ASSERT_COMPARE( actual_output, expected_output1->len,
6151 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006152 if( expected_output2->len != 0 )
6153 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006154 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006155 actual_output,
6156 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006157 ASSERT_COMPARE( actual_output, expected_output2->len,
6158 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006159 }
Gilles Peskine59685592018-09-18 12:11:34 +02006160
6161exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006162 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006163 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006164 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006165 mbedtls_free( actual_output );
6166}
6167/* END_CASE */
6168
6169/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006170void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006171{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006172 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006173 unsigned char *output = NULL;
6174 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006175 size_t i;
6176 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006177
Simon Butcher49f8e312020-03-03 15:51:50 +00006178 TEST_ASSERT( bytes_arg >= 0 );
6179
Gilles Peskine91892022021-02-08 19:50:26 +01006180 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006181 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006182
Gilles Peskine8817f612018-12-18 00:18:46 +01006183 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006184
Gilles Peskinea50d7392018-06-21 10:22:13 +02006185 /* Run several times, to ensure that every output byte will be
6186 * nonzero at least once with overwhelming probability
6187 * (2^(-8*number_of_runs)). */
6188 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006189 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006190 if( bytes != 0 )
6191 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006192 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006193
Gilles Peskinea50d7392018-06-21 10:22:13 +02006194 for( i = 0; i < bytes; i++ )
6195 {
6196 if( output[i] != 0 )
6197 ++changed[i];
6198 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006199 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006200
6201 /* Check that every byte was changed to nonzero at least once. This
6202 * validates that psa_generate_random is overwriting every byte of
6203 * the output buffer. */
6204 for( i = 0; i < bytes; i++ )
6205 {
6206 TEST_ASSERT( changed[i] != 0 );
6207 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006208
6209exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006210 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006211 mbedtls_free( output );
6212 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006213}
6214/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006215
6216/* BEGIN_CASE */
6217void generate_key( int type_arg,
6218 int bits_arg,
6219 int usage_arg,
6220 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006221 int expected_status_arg,
6222 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006223{
Ronald Cron5425a212020-08-04 14:58:35 +02006224 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006225 psa_key_type_t type = type_arg;
6226 psa_key_usage_t usage = usage_arg;
6227 size_t bits = bits_arg;
6228 psa_algorithm_t alg = alg_arg;
6229 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006231 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006232
Gilles Peskine8817f612018-12-18 00:18:46 +01006233 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006234
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006235 psa_set_key_usage_flags( &attributes, usage );
6236 psa_set_key_algorithm( &attributes, alg );
6237 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006238 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006239
6240 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006241 psa_status_t status = psa_generate_key( &attributes, &key );
6242
6243 if( is_large_key > 0 )
6244 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6245 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006246 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006247 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006248
6249 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006250 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006251 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6252 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006253
Gilles Peskine818ca122018-06-20 18:16:48 +02006254 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006255 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006256 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006257
6258exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006259 /*
6260 * Key attributes may have been returned by psa_get_key_attributes()
6261 * thus reset them as required.
6262 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006263 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006264
Ronald Cron5425a212020-08-04 14:58:35 +02006265 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006266 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006267}
6268/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006269
Ronald Cronee414c72021-03-18 18:50:08 +01006270/* 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 +02006271void generate_key_rsa( int bits_arg,
6272 data_t *e_arg,
6273 int expected_status_arg )
6274{
Ronald Cron5425a212020-08-04 14:58:35 +02006275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006276 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006277 size_t bits = bits_arg;
6278 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6279 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6280 psa_status_t expected_status = expected_status_arg;
6281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6282 uint8_t *exported = NULL;
6283 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006284 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006285 size_t exported_length = SIZE_MAX;
6286 uint8_t *e_read_buffer = NULL;
6287 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006288 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006289 size_t e_read_length = SIZE_MAX;
6290
6291 if( e_arg->len == 0 ||
6292 ( e_arg->len == 3 &&
6293 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6294 {
6295 is_default_public_exponent = 1;
6296 e_read_size = 0;
6297 }
6298 ASSERT_ALLOC( e_read_buffer, e_read_size );
6299 ASSERT_ALLOC( exported, exported_size );
6300
6301 PSA_ASSERT( psa_crypto_init( ) );
6302
6303 psa_set_key_usage_flags( &attributes, usage );
6304 psa_set_key_algorithm( &attributes, alg );
6305 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6306 e_arg->x, e_arg->len ) );
6307 psa_set_key_bits( &attributes, bits );
6308
6309 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006310 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006311 if( expected_status != PSA_SUCCESS )
6312 goto exit;
6313
6314 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006315 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006316 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6317 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6318 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6319 e_read_buffer, e_read_size,
6320 &e_read_length ) );
6321 if( is_default_public_exponent )
6322 TEST_EQUAL( e_read_length, 0 );
6323 else
6324 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6325
6326 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006327 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006328 goto exit;
6329
6330 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006331 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006332 exported, exported_size,
6333 &exported_length ) );
6334 {
6335 uint8_t *p = exported;
6336 uint8_t *end = exported + exported_length;
6337 size_t len;
6338 /* RSAPublicKey ::= SEQUENCE {
6339 * modulus INTEGER, -- n
6340 * publicExponent INTEGER } -- e
6341 */
6342 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006343 MBEDTLS_ASN1_SEQUENCE |
6344 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006345 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006346 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6347 MBEDTLS_ASN1_INTEGER ) );
6348 if( len >= 1 && p[0] == 0 )
6349 {
6350 ++p;
6351 --len;
6352 }
6353 if( e_arg->len == 0 )
6354 {
6355 TEST_EQUAL( len, 3 );
6356 TEST_EQUAL( p[0], 1 );
6357 TEST_EQUAL( p[1], 0 );
6358 TEST_EQUAL( p[2], 1 );
6359 }
6360 else
6361 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6362 }
6363
6364exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006365 /*
6366 * Key attributes may have been returned by psa_get_key_attributes() or
6367 * set by psa_set_key_domain_parameters() thus reset them as required.
6368 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006369 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006370
Ronald Cron5425a212020-08-04 14:58:35 +02006371 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006372 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006373 mbedtls_free( e_read_buffer );
6374 mbedtls_free( exported );
6375}
6376/* END_CASE */
6377
Darryl Greend49a4992018-06-18 17:27:26 +01006378/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006379void persistent_key_load_key_from_storage( data_t *data,
6380 int type_arg, int bits_arg,
6381 int usage_flags_arg, int alg_arg,
6382 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006383{
Ronald Cron71016a92020-08-28 19:01:50 +02006384 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006385 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6387 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006388 psa_key_type_t type = type_arg;
6389 size_t bits = bits_arg;
6390 psa_key_usage_t usage_flags = usage_flags_arg;
6391 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006392 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006393 unsigned char *first_export = NULL;
6394 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006395 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006396 size_t first_exported_length;
6397 size_t second_exported_length;
6398
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006399 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6400 {
6401 ASSERT_ALLOC( first_export, export_size );
6402 ASSERT_ALLOC( second_export, export_size );
6403 }
Darryl Greend49a4992018-06-18 17:27:26 +01006404
Gilles Peskine8817f612018-12-18 00:18:46 +01006405 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006406
Gilles Peskinec87af662019-05-15 16:12:22 +02006407 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006408 psa_set_key_usage_flags( &attributes, usage_flags );
6409 psa_set_key_algorithm( &attributes, alg );
6410 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006411 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006412
Darryl Green0c6575a2018-11-07 16:05:30 +00006413 switch( generation_method )
6414 {
6415 case IMPORT_KEY:
6416 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006417 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006418 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006419 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006420
Darryl Green0c6575a2018-11-07 16:05:30 +00006421 case GENERATE_KEY:
6422 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006423 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006424 break;
6425
6426 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006427#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006428 {
6429 /* Create base key */
6430 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6431 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6432 psa_set_key_usage_flags( &base_attributes,
6433 PSA_KEY_USAGE_DERIVE );
6434 psa_set_key_algorithm( &base_attributes, derive_alg );
6435 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006436 PSA_ASSERT( psa_import_key( &base_attributes,
6437 data->x, data->len,
6438 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006439 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006440 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006441 PSA_ASSERT( psa_key_derivation_input_key(
6442 &operation,
6443 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006444 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006445 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006446 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006447 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6448 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006449 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006450 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006451 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006452 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006453 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006454#else
6455 TEST_ASSUME( ! "KDF not supported in this configuration" );
6456#endif
6457 break;
6458
6459 default:
6460 TEST_ASSERT( ! "generation_method not implemented in test" );
6461 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006462 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006463 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006464
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006465 /* Export the key if permitted by the key policy. */
6466 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6467 {
Ronald Cron5425a212020-08-04 14:58:35 +02006468 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006469 first_export, export_size,
6470 &first_exported_length ) );
6471 if( generation_method == IMPORT_KEY )
6472 ASSERT_COMPARE( data->x, data->len,
6473 first_export, first_exported_length );
6474 }
Darryl Greend49a4992018-06-18 17:27:26 +01006475
6476 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006477 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006478 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006479 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006480
Darryl Greend49a4992018-06-18 17:27:26 +01006481 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006482 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006483 TEST_ASSERT( mbedtls_svc_key_id_equal(
6484 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006485 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6486 PSA_KEY_LIFETIME_PERSISTENT );
6487 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6488 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6489 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6490 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006491
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006492 /* Export the key again if permitted by the key policy. */
6493 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006494 {
Ronald Cron5425a212020-08-04 14:58:35 +02006495 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006496 second_export, export_size,
6497 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006498 ASSERT_COMPARE( first_export, first_exported_length,
6499 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006500 }
6501
6502 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006503 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006504 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006505
6506exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006507 /*
6508 * Key attributes may have been returned by psa_get_key_attributes()
6509 * thus reset them as required.
6510 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006511 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006512
Darryl Greend49a4992018-06-18 17:27:26 +01006513 mbedtls_free( first_export );
6514 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006515 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006516 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006517 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006518 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006519}
6520/* END_CASE */