blob: 46f7a1d529d90e4887874b548db1ab8ed8d17f1e [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 Elliott97fd1ba2021-07-21 18:46:06 +0100267/*!
268 * \brief Internal Function for AEAD multipart tests.
269 *
270 * \param key_type_arg Type of key passed in
271 * \param key_data The encryption / decryption key data
272 * \param alg_arg The type of algorithm used
273 * \param nonce Nonce data
274 * \param additional_data Additional data
275 * \param ad_part_len If not -1, the length of chunks to
276 * feed additional data in to be encrypted /
277 * decrypted. If -1, no chunking.
278 * \param input_data Data to encrypt / decrypt
279 * \param data_part_len If not -1, the length of chunks to feed the
280 * data in to be encrypted / decrypted. If -1,
281 * no chunking
282 * \param do_set_lengths If non-zero, then set lengths prior to
283 * calling encryption / decryption.
284 * \param expected_output Expected output
285 * \param expected_status_arg Expected status
286 * \param is_encrypt If non-zero this is an encryption operation.
287 *
288 * \return int Zero on failure, non-zero on success.
289 *
290 */
291static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
292 int alg_arg,
293 data_t *nonce,
294 data_t *additional_data,
295 int ad_part_len,
296 data_t *input_data,
297 int data_part_len,
298 int do_set_lengths,
299 data_t *expected_output,
300 int expect_valid_signature,
Paul Elliott329d5382021-07-22 17:10:45 +0100301 int is_encrypt,
302 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100303{
304 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
305 psa_key_type_t key_type = key_type_arg;
306 psa_algorithm_t alg = alg_arg;
307 psa_aead_operation_t operation;
308 unsigned char *output_data = NULL;
309 unsigned char *part_data = NULL;
310 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100311 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100312 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 size_t output_size = 0;
314 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100315 size_t output_length = 0;
316 size_t key_bits = 0;
317 size_t tag_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100318 uint32_t part_offset = 0;
319 size_t part_length = 0;
320 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100321 size_t tag_size = 0;
322 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100323 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
324 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
325
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100326 int test_ok = 0;
Paul Elliott329d5382021-07-22 17:10:45 +0100327 uint32_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100328
Paul Elliottd3f82412021-06-16 16:52:21 +0100329 PSA_ASSERT( psa_crypto_init( ) );
330
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100331 if( is_encrypt )
332 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
333 else
334 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
335
Paul Elliottd3f82412021-06-16 16:52:21 +0100336 psa_set_key_algorithm( &attributes, alg );
337 psa_set_key_type( &attributes, key_type );
338
339 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
340 &key ) );
341
342 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
343 key_bits = psa_get_key_bits( &attributes );
344
345 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
346
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100347 if( is_encrypt )
348 {
349 /* Tag gets written at end of buffer. */
350 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
351 ( input_data->len +
352 tag_length ) );
353 data_true_size = input_data->len;
354 }
355 else
356 {
357 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
358 ( input_data->len -
359 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 /* Do not want to attempt to decrypt tag. */
362 data_true_size = input_data->len - tag_length;
363 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100364
365 ASSERT_ALLOC( output_data, output_size );
366
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100367 if( is_encrypt )
368 {
369 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
370 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
371 }
372 else
373 {
374 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
375 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
376 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100377
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100378 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100379
380 operation = psa_aead_operation_init( );
381
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100382
383 if( is_encrypt )
384 status = psa_aead_encrypt_setup( &operation, key, alg );
385 else
386 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100387
388 /* If the operation is not supported, just skip and not fail in case the
389 * encryption involves a common limitation of cryptography hardwares and
390 * an alternative implementation. */
391 if( status == PSA_ERROR_NOT_SUPPORTED )
392 {
393 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
394 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
395 }
396
397 PSA_ASSERT( status );
398
399 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
400
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100401 if( do_set_lengths )
Paul Elliottd3f82412021-06-16 16:52:21 +0100402 {
403 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100404 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100405 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
407 if( ad_part_len != -1 )
408 {
409 /* Pass additional data in parts */
410 part_offset = 0;
411
412 while( part_offset < additional_data->len )
413 {
Paul Elliott329d5382021-07-22 17:10:45 +0100414 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100415 {
Paul Elliott329d5382021-07-22 17:10:45 +0100416 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100417 }
418 else
419 {
Paul Elliott329d5382021-07-22 17:10:45 +0100420 if( additional_data->len - part_offset <
421 ( uint32_t ) ad_part_len )
422 {
423 part_length = additional_data->len - part_offset;
424 }
425 else
426 {
427 part_length = ad_part_len;
428 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100429 }
430
431 PSA_ASSERT( psa_aead_update_ad( &operation,
432 additional_data->x + part_offset,
433 part_length ) );
434
435 part_offset += part_length;
436 }
437 }
438 else
439 {
440 /* Pass additional data in one go. */
441 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
442 additional_data->len ) );
443 }
444
445 if( data_part_len != -1 )
446 {
447 /* Pass data in parts */
448 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100449 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100450
451 ASSERT_ALLOC( part_data, part_data_size );
452
453 part_offset = 0;
454
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100455 while( part_offset < data_true_size )
Paul Elliottd3f82412021-06-16 16:52:21 +0100456 {
Paul Elliott329d5382021-07-22 17:10:45 +0100457 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 {
Paul Elliott329d5382021-07-22 17:10:45 +0100459 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100460 }
461 else
462 {
Paul Elliott329d5382021-07-22 17:10:45 +0100463 if( ( data_true_size - part_offset ) < ( uint32_t ) data_part_len )
464 {
465 part_length = ( data_true_size - part_offset );
466 }
467 else
468 {
469 part_length = data_part_len;
470 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100471 }
472
473 PSA_ASSERT( psa_aead_update( &operation,
474 ( input_data->x + part_offset ),
475 part_length, part_data,
476 part_data_size,
477 &output_part_length ) );
478
479 if( output_data && output_part_length )
480 {
481 memcpy( ( output_data + part_offset ), part_data,
482 output_part_length );
483 }
484
485 part_offset += part_length;
486 output_length += output_part_length;
487 }
488 }
489 else
490 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100491 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100493 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100494 output_size, &output_length ) );
495 }
496
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100497 if( is_encrypt )
498 PSA_ASSERT( psa_aead_finish( &operation, final_data,
499 final_output_size,
500 &output_part_length,
501 tag_buffer, tag_length,
502 &tag_size ) );
503 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100504 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100505 status = psa_aead_verify( &operation, final_data,
506 final_output_size,
507 &output_part_length,
508 ( input_data->x + data_true_size ),
509 tag_length );
510
511 if( status != PSA_SUCCESS )
512 {
513 if( !expect_valid_signature )
514 {
515 /* Expected failure. */
516 test_ok = 1;
517 goto exit;
518 }
519 else
520 PSA_ASSERT( status );
521 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 }
523
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100524 if( output_data && output_part_length )
525 memcpy( ( output_data + output_length ), final_data,
526 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100527
528 output_length += output_part_length;
529
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100530
531 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
532 * should be exact.*/
533 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100534 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100535 TEST_EQUAL( tag_length, tag_size );
536
537 if( output_data && tag_length )
538 memcpy( ( output_data + output_length ), tag_buffer,
539 tag_length );
540
541 output_length += tag_length;
542
543 TEST_EQUAL( output_length,
544 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
545 input_data->len ) );
546 TEST_ASSERT( output_length <=
547 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
548 }
549 else
550 {
551 TEST_EQUAL( output_length,
552 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
553 input_data->len ) );
554 TEST_ASSERT( output_length <=
555 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100556 }
557
Paul Elliottd3f82412021-06-16 16:52:21 +0100558
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100559 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100560 output_data, output_length );
561
Paul Elliottd3f82412021-06-16 16:52:21 +0100562
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100563 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100564
565exit:
566 psa_destroy_key( key );
567 psa_aead_abort( &operation );
568 mbedtls_free( output_data );
569 mbedtls_free( part_data );
570 mbedtls_free( final_data );
571 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100572
573 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100574}
575
Gilles Peskinee59236f2018-01-27 23:32:46 +0100576/* END_HEADER */
577
578/* BEGIN_DEPENDENCIES
579 * depends_on:MBEDTLS_PSA_CRYPTO_C
580 * END_DEPENDENCIES
581 */
582
583/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200584void static_checks( )
585{
586 size_t max_truncated_mac_size =
587 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
588
589 /* Check that the length for a truncated MAC always fits in the algorithm
590 * encoding. The shifted mask is the maximum truncated value. The
591 * untruncated algorithm may be one byte larger. */
592 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
593}
594/* END_CASE */
595
596/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200597void import_with_policy( int type_arg,
598 int usage_arg, int alg_arg,
599 int expected_status_arg )
600{
601 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
602 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200603 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200604 psa_key_type_t type = type_arg;
605 psa_key_usage_t usage = usage_arg;
606 psa_algorithm_t alg = alg_arg;
607 psa_status_t expected_status = expected_status_arg;
608 const uint8_t key_material[16] = {0};
609 psa_status_t status;
610
611 PSA_ASSERT( psa_crypto_init( ) );
612
613 psa_set_key_type( &attributes, type );
614 psa_set_key_usage_flags( &attributes, usage );
615 psa_set_key_algorithm( &attributes, alg );
616
617 status = psa_import_key( &attributes,
618 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200619 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200620 TEST_EQUAL( status, expected_status );
621 if( status != PSA_SUCCESS )
622 goto exit;
623
Ronald Cron5425a212020-08-04 14:58:35 +0200624 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200625 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
626 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
627 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200628 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200629
Ronald Cron5425a212020-08-04 14:58:35 +0200630 PSA_ASSERT( psa_destroy_key( key ) );
631 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200632
633exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100634 /*
635 * Key attributes may have been returned by psa_get_key_attributes()
636 * thus reset them as required.
637 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200638 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100639
640 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641 PSA_DONE( );
642}
643/* END_CASE */
644
645/* BEGIN_CASE */
646void import_with_data( data_t *data, int type_arg,
647 int attr_bits_arg,
648 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200649{
650 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
651 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200652 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200653 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200654 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200655 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100656 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657
Gilles Peskine8817f612018-12-18 00:18:46 +0100658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659
Gilles Peskine4747d192019-04-17 15:05:45 +0200660 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200661 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200662
Ronald Cron5425a212020-08-04 14:58:35 +0200663 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100664 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200665 if( status != PSA_SUCCESS )
666 goto exit;
667
Ronald Cron5425a212020-08-04 14:58:35 +0200668 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200669 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200670 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200671 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200672 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200673
Ronald Cron5425a212020-08-04 14:58:35 +0200674 PSA_ASSERT( psa_destroy_key( key ) );
675 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
677exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100678 /*
679 * Key attributes may have been returned by psa_get_key_attributes()
680 * thus reset them as required.
681 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200682 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100683
684 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200685 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686}
687/* END_CASE */
688
689/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200690void import_large_key( int type_arg, int byte_size_arg,
691 int expected_status_arg )
692{
693 psa_key_type_t type = type_arg;
694 size_t byte_size = byte_size_arg;
695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
696 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200697 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200698 psa_status_t status;
699 uint8_t *buffer = NULL;
700 size_t buffer_size = byte_size + 1;
701 size_t n;
702
Steven Cooreman69967ce2021-01-18 18:01:08 +0100703 /* Skip the test case if the target running the test cannot
704 * accomodate large keys due to heap size constraints */
705 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200706 memset( buffer, 'K', byte_size );
707
708 PSA_ASSERT( psa_crypto_init( ) );
709
710 /* Try importing the key */
711 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
712 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200713 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100714 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200715 TEST_EQUAL( status, expected_status );
716
717 if( status == PSA_SUCCESS )
718 {
Ronald Cron5425a212020-08-04 14:58:35 +0200719 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200720 TEST_EQUAL( psa_get_key_type( &attributes ), type );
721 TEST_EQUAL( psa_get_key_bits( &attributes ),
722 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200723 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200724 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200725 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200726 for( n = 0; n < byte_size; n++ )
727 TEST_EQUAL( buffer[n], 'K' );
728 for( n = byte_size; n < buffer_size; n++ )
729 TEST_EQUAL( buffer[n], 0 );
730 }
731
732exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100733 /*
734 * Key attributes may have been returned by psa_get_key_attributes()
735 * thus reset them as required.
736 */
737 psa_reset_key_attributes( &attributes );
738
Ronald Cron5425a212020-08-04 14:58:35 +0200739 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200740 PSA_DONE( );
741 mbedtls_free( buffer );
742}
743/* END_CASE */
744
745/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200746void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
747{
Ronald Cron5425a212020-08-04 14:58:35 +0200748 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200749 size_t bits = bits_arg;
750 psa_status_t expected_status = expected_status_arg;
751 psa_status_t status;
752 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200753 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200754 size_t buffer_size = /* Slight overapproximations */
755 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200756 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200757 unsigned char *p;
758 int ret;
759 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200760 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200761
Gilles Peskine8817f612018-12-18 00:18:46 +0100762 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200763 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200764
765 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
766 bits, keypair ) ) >= 0 );
767 length = ret;
768
769 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200770 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200771 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100772 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200773
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200774 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200775 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200776
777exit:
778 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200779 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200780}
781/* END_CASE */
782
783/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300784void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300785 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200786 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100787 int expected_bits,
788 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200789 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 int canonical_input )
791{
Ronald Cron5425a212020-08-04 14:58:35 +0200792 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100793 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200794 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200795 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100796 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797 unsigned char *exported = NULL;
798 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100799 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100800 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200803 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100804
Moran Pekercb088e72018-07-17 17:36:59 +0300805 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200806 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100807 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200808 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100809 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100810
Gilles Peskine4747d192019-04-17 15:05:45 +0200811 psa_set_key_usage_flags( &attributes, usage_arg );
812 psa_set_key_algorithm( &attributes, alg );
813 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700814
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200816 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100817
818 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200819 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200820 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
821 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200822 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823
824 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200825 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100826 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100827
828 /* The exported length must be set by psa_export_key() to a value between 0
829 * and export_size. On errors, the exported length must be 0. */
830 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
831 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
832 TEST_ASSERT( exported_length <= export_size );
833
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200834 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200835 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100836 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200837 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100838 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100839 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200840 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100841
Gilles Peskineea38a922021-02-13 00:05:16 +0100842 /* Run sanity checks on the exported key. For non-canonical inputs,
843 * this validates the canonical representations. For canonical inputs,
844 * this doesn't directly validate the implementation, but it still helps
845 * by cross-validating the test data with the sanity check code. */
846 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200847 goto exit;
848
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100849 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200850 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100851 else
852 {
Ronald Cron5425a212020-08-04 14:58:35 +0200853 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200854 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200855 &key2 ) );
856 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100857 reexported,
858 export_size,
859 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200860 ASSERT_COMPARE( exported, exported_length,
861 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200862 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100863 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100864 TEST_ASSERT( exported_length <=
865 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
866 psa_get_key_bits( &got_attributes ) ) );
867 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100868
869destroy:
870 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200871 PSA_ASSERT( psa_destroy_key( key ) );
872 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873
874exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100875 /*
876 * Key attributes may have been returned by psa_get_key_attributes()
877 * thus reset them as required.
878 */
879 psa_reset_key_attributes( &got_attributes );
880
itayzafrir3e02b3b2018-06-12 17:06:52 +0300881 mbedtls_free( exported );
882 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200883 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884}
885/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100886
Moran Pekerf709f4a2018-06-06 17:26:04 +0300887/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300888void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200889 int type_arg,
890 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100891 int export_size_delta,
892 int expected_export_status_arg,
893 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300894{
Ronald Cron5425a212020-08-04 14:58:35 +0200895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300896 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200897 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200898 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300899 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300900 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100901 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100902 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300904
Gilles Peskine8817f612018-12-18 00:18:46 +0100905 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300906
Gilles Peskine4747d192019-04-17 15:05:45 +0200907 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
908 psa_set_key_algorithm( &attributes, alg );
909 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910
911 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200912 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300913
Gilles Peskine49c25912018-10-29 15:15:31 +0100914 /* Export the public key */
915 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200916 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200917 exported, export_size,
918 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100919 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100920 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100921 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200922 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100923 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200924 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200925 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100926 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100927 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100928 TEST_ASSERT( expected_public_key->len <=
929 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
930 TEST_ASSERT( expected_public_key->len <=
931 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100932 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
933 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100934 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300935
936exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100937 /*
938 * Key attributes may have been returned by psa_get_key_attributes()
939 * thus reset them as required.
940 */
941 psa_reset_key_attributes( &attributes );
942
itayzafrir3e02b3b2018-06-12 17:06:52 +0300943 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200944 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200945 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300946}
947/* END_CASE */
948
Gilles Peskine20035e32018-02-03 22:44:14 +0100949/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200950void import_and_exercise_key( data_t *data,
951 int type_arg,
952 int bits_arg,
953 int alg_arg )
954{
Ronald Cron5425a212020-08-04 14:58:35 +0200955 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200956 psa_key_type_t type = type_arg;
957 size_t bits = bits_arg;
958 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100959 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200961 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200962
Gilles Peskine8817f612018-12-18 00:18:46 +0100963 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200964
Gilles Peskine4747d192019-04-17 15:05:45 +0200965 psa_set_key_usage_flags( &attributes, usage );
966 psa_set_key_algorithm( &attributes, alg );
967 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200968
969 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200970 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200971
972 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200973 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200974 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
975 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200976
977 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100978 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200979 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980
Ronald Cron5425a212020-08-04 14:58:35 +0200981 PSA_ASSERT( psa_destroy_key( key ) );
982 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200983
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200984exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100985 /*
986 * Key attributes may have been returned by psa_get_key_attributes()
987 * thus reset them as required.
988 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200989 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100990
991 psa_reset_key_attributes( &attributes );
992 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200993 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200994}
995/* END_CASE */
996
997/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100998void effective_key_attributes( int type_arg, int expected_type_arg,
999 int bits_arg, int expected_bits_arg,
1000 int usage_arg, int expected_usage_arg,
1001 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001002{
Ronald Cron5425a212020-08-04 14:58:35 +02001003 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001004 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001005 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001006 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001007 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001008 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001009 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001010 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001011 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001012 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001013
Gilles Peskine8817f612018-12-18 00:18:46 +01001014 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001015
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 psa_set_key_usage_flags( &attributes, usage );
1017 psa_set_key_algorithm( &attributes, alg );
1018 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001019 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001020
Ronald Cron5425a212020-08-04 14:58:35 +02001021 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001022 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001023
Ronald Cron5425a212020-08-04 14:58:35 +02001024 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001025 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1026 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1027 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1028 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001029
1030exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001031 /*
1032 * Key attributes may have been returned by psa_get_key_attributes()
1033 * thus reset them as required.
1034 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001035 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001036
1037 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001038 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001039}
1040/* END_CASE */
1041
1042/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001043void check_key_policy( int type_arg, int bits_arg,
1044 int usage_arg, int alg_arg )
1045{
1046 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1047 usage_arg, usage_arg, alg_arg, alg_arg );
1048 goto exit;
1049}
1050/* END_CASE */
1051
1052/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001053void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001054{
1055 /* Test each valid way of initializing the object, except for `= {0}`, as
1056 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1057 * though it's OK by the C standard. We could test for this, but we'd need
1058 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001059 psa_key_attributes_t func = psa_key_attributes_init( );
1060 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1061 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001062
1063 memset( &zero, 0, sizeof( zero ) );
1064
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001065 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1066 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1067 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001068
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001069 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1070 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1071 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1072
1073 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1074 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1075 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1076
1077 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1078 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1079 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1080
1081 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1082 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1083 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001084}
1085/* END_CASE */
1086
1087/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001088void mac_key_policy( int policy_usage,
1089 int policy_alg,
1090 int key_type,
1091 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001092 int exercise_alg,
1093 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001094{
Ronald Cron5425a212020-08-04 14:58:35 +02001095 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001097 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001098 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001099 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001100 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001101
Gilles Peskine8817f612018-12-18 00:18:46 +01001102 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001103
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001104 psa_set_key_usage_flags( &attributes, policy_usage );
1105 psa_set_key_algorithm( &attributes, policy_alg );
1106 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001107
Gilles Peskine049c7532019-05-15 20:22:09 +02001108 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001109 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001110
Ronald Cron5425a212020-08-04 14:58:35 +02001111 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001112 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001113 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001114 else
1115 TEST_EQUAL( status, expected_status );
1116
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001117 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001118
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001119 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001120 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001121 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001122 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001123 else
1124 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001125
1126exit:
1127 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001128 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001129 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001130}
1131/* END_CASE */
1132
1133/* BEGIN_CASE */
1134void cipher_key_policy( int policy_usage,
1135 int policy_alg,
1136 int key_type,
1137 data_t *key_data,
1138 int exercise_alg )
1139{
Ronald Cron5425a212020-08-04 14:58:35 +02001140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001141 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001142 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001143 psa_status_t status;
1144
Gilles Peskine8817f612018-12-18 00:18:46 +01001145 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001146
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001147 psa_set_key_usage_flags( &attributes, policy_usage );
1148 psa_set_key_algorithm( &attributes, policy_alg );
1149 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001150
Gilles Peskine049c7532019-05-15 20:22:09 +02001151 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001152 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001153
Ronald Cron5425a212020-08-04 14:58:35 +02001154 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001155 if( policy_alg == exercise_alg &&
1156 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001157 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001158 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001159 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001160 psa_cipher_abort( &operation );
1161
Ronald Cron5425a212020-08-04 14:58:35 +02001162 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001163 if( policy_alg == exercise_alg &&
1164 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001165 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001166 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001167 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001168
1169exit:
1170 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001171 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001172 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001173}
1174/* END_CASE */
1175
1176/* BEGIN_CASE */
1177void aead_key_policy( int policy_usage,
1178 int policy_alg,
1179 int key_type,
1180 data_t *key_data,
1181 int nonce_length_arg,
1182 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001183 int exercise_alg,
1184 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001185{
Ronald Cron5425a212020-08-04 14:58:35 +02001186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001188 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001189 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001190 unsigned char nonce[16] = {0};
1191 size_t nonce_length = nonce_length_arg;
1192 unsigned char tag[16];
1193 size_t tag_length = tag_length_arg;
1194 size_t output_length;
1195
1196 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1197 TEST_ASSERT( tag_length <= sizeof( tag ) );
1198
Gilles Peskine8817f612018-12-18 00:18:46 +01001199 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001200
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001201 psa_set_key_usage_flags( &attributes, policy_usage );
1202 psa_set_key_algorithm( &attributes, policy_alg );
1203 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204
Gilles Peskine049c7532019-05-15 20:22:09 +02001205 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001206 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207
Ronald Cron5425a212020-08-04 14:58:35 +02001208 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209 nonce, nonce_length,
1210 NULL, 0,
1211 NULL, 0,
1212 tag, tag_length,
1213 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001214 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1215 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001216 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001217 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218
1219 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001220 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001221 nonce, nonce_length,
1222 NULL, 0,
1223 tag, tag_length,
1224 NULL, 0,
1225 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001226 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1227 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1228 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001229 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001231 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001232
1233exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001234 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001235 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001236}
1237/* END_CASE */
1238
1239/* BEGIN_CASE */
1240void asymmetric_encryption_key_policy( int policy_usage,
1241 int policy_alg,
1242 int key_type,
1243 data_t *key_data,
1244 int exercise_alg )
1245{
Ronald Cron5425a212020-08-04 14:58:35 +02001246 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001248 psa_status_t status;
1249 size_t key_bits;
1250 size_t buffer_length;
1251 unsigned char *buffer = NULL;
1252 size_t output_length;
1253
Gilles Peskine8817f612018-12-18 00:18:46 +01001254 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001255
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001256 psa_set_key_usage_flags( &attributes, policy_usage );
1257 psa_set_key_algorithm( &attributes, policy_alg );
1258 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001259
Gilles Peskine049c7532019-05-15 20:22:09 +02001260 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001261 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001262
Ronald Cron5425a212020-08-04 14:58:35 +02001263 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001264 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001265 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1266 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001267 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001268
Ronald Cron5425a212020-08-04 14:58:35 +02001269 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001270 NULL, 0,
1271 NULL, 0,
1272 buffer, buffer_length,
1273 &output_length );
1274 if( policy_alg == exercise_alg &&
1275 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001276 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001278 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001279
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001280 if( buffer_length != 0 )
1281 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001282 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001283 buffer, buffer_length,
1284 NULL, 0,
1285 buffer, buffer_length,
1286 &output_length );
1287 if( policy_alg == exercise_alg &&
1288 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001289 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001290 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001291 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001292
1293exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001294 /*
1295 * Key attributes may have been returned by psa_get_key_attributes()
1296 * thus reset them as required.
1297 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001298 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001299
1300 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001301 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001302 mbedtls_free( buffer );
1303}
1304/* END_CASE */
1305
1306/* BEGIN_CASE */
1307void asymmetric_signature_key_policy( int policy_usage,
1308 int policy_alg,
1309 int key_type,
1310 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001311 int exercise_alg,
1312 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001313{
Ronald Cron5425a212020-08-04 14:58:35 +02001314 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001316 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001317 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1318 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1319 * compatible with the policy and `payload_length_arg` is supposed to be
1320 * a valid input length to sign. If `payload_length_arg <= 0`,
1321 * `exercise_alg` is supposed to be forbidden by the policy. */
1322 int compatible_alg = payload_length_arg > 0;
1323 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001324 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001325 size_t signature_length;
1326
Gilles Peskine8817f612018-12-18 00:18:46 +01001327 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001328
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001329 psa_set_key_usage_flags( &attributes, policy_usage );
1330 psa_set_key_algorithm( &attributes, policy_alg );
1331 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332
Gilles Peskine049c7532019-05-15 20:22:09 +02001333 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001334 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001335
Ronald Cron5425a212020-08-04 14:58:35 +02001336 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001337 payload, payload_length,
1338 signature, sizeof( signature ),
1339 &signature_length );
1340 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001341 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001342 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001343 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001344
1345 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001346 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001347 payload, payload_length,
1348 signature, sizeof( signature ) );
1349 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001350 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001351 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001352 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001353
1354exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001355 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001356 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001357}
1358/* END_CASE */
1359
Janos Follathba3fab92019-06-11 14:50:16 +01001360/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001361void derive_key_policy( int policy_usage,
1362 int policy_alg,
1363 int key_type,
1364 data_t *key_data,
1365 int exercise_alg )
1366{
Ronald Cron5425a212020-08-04 14:58:35 +02001367 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001369 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001370 psa_status_t status;
1371
Gilles Peskine8817f612018-12-18 00:18:46 +01001372 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001373
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001374 psa_set_key_usage_flags( &attributes, policy_usage );
1375 psa_set_key_algorithm( &attributes, policy_alg );
1376 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001377
Gilles Peskine049c7532019-05-15 20:22:09 +02001378 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001379 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001380
Janos Follathba3fab92019-06-11 14:50:16 +01001381 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1382
1383 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1384 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001385 {
Janos Follathba3fab92019-06-11 14:50:16 +01001386 PSA_ASSERT( psa_key_derivation_input_bytes(
1387 &operation,
1388 PSA_KEY_DERIVATION_INPUT_SEED,
1389 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001390 }
Janos Follathba3fab92019-06-11 14:50:16 +01001391
1392 status = psa_key_derivation_input_key( &operation,
1393 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001394 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001395
Gilles Peskineea0fb492018-07-12 17:17:20 +02001396 if( policy_alg == exercise_alg &&
1397 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001398 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001399 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001400 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001401
1402exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001403 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001404 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001405 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001406}
1407/* END_CASE */
1408
1409/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001410void agreement_key_policy( int policy_usage,
1411 int policy_alg,
1412 int key_type_arg,
1413 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001414 int exercise_alg,
1415 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001416{
Ronald Cron5425a212020-08-04 14:58:35 +02001417 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001418 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001419 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001420 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001421 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001422 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001423
Gilles Peskine8817f612018-12-18 00:18:46 +01001424 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001425
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001426 psa_set_key_usage_flags( &attributes, policy_usage );
1427 psa_set_key_algorithm( &attributes, policy_alg );
1428 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001429
Gilles Peskine049c7532019-05-15 20:22:09 +02001430 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001431 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001432
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001433 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001434 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001435
Steven Cooremance48e852020-10-05 16:02:45 +02001436 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001437
1438exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001439 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001440 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001441 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001442}
1443/* END_CASE */
1444
1445/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001446void key_policy_alg2( int key_type_arg, data_t *key_data,
1447 int usage_arg, int alg_arg, int alg2_arg )
1448{
Ronald Cron5425a212020-08-04 14:58:35 +02001449 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001450 psa_key_type_t key_type = key_type_arg;
1451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1452 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1453 psa_key_usage_t usage = usage_arg;
1454 psa_algorithm_t alg = alg_arg;
1455 psa_algorithm_t alg2 = alg2_arg;
1456
1457 PSA_ASSERT( psa_crypto_init( ) );
1458
1459 psa_set_key_usage_flags( &attributes, usage );
1460 psa_set_key_algorithm( &attributes, alg );
1461 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1462 psa_set_key_type( &attributes, key_type );
1463 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001464 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001465
Ronald Cron5425a212020-08-04 14:58:35 +02001466 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001467 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1468 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1469 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1470
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001471 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001472 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001473 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001474 goto exit;
1475
1476exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001477 /*
1478 * Key attributes may have been returned by psa_get_key_attributes()
1479 * thus reset them as required.
1480 */
1481 psa_reset_key_attributes( &got_attributes );
1482
Ronald Cron5425a212020-08-04 14:58:35 +02001483 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001484 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001485}
1486/* END_CASE */
1487
1488/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001489void raw_agreement_key_policy( int policy_usage,
1490 int policy_alg,
1491 int key_type_arg,
1492 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001493 int exercise_alg,
1494 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001495{
Ronald Cron5425a212020-08-04 14:58:35 +02001496 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001497 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001498 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001499 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001500 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001501 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001502
1503 PSA_ASSERT( psa_crypto_init( ) );
1504
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001505 psa_set_key_usage_flags( &attributes, policy_usage );
1506 psa_set_key_algorithm( &attributes, policy_alg );
1507 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001508
Gilles Peskine049c7532019-05-15 20:22:09 +02001509 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001510 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001511
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001512 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001513
Steven Cooremance48e852020-10-05 16:02:45 +02001514 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001515
1516exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001517 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001518 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001519 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001520}
1521/* END_CASE */
1522
1523/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001524void copy_success( int source_usage_arg,
1525 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001526 int type_arg, data_t *material,
1527 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001528 int target_usage_arg,
1529 int target_alg_arg, int target_alg2_arg,
1530 int expected_usage_arg,
1531 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001532{
Gilles Peskineca25db92019-04-19 11:43:08 +02001533 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1534 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001535 psa_key_usage_t expected_usage = expected_usage_arg;
1536 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001537 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001538 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1539 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001540 uint8_t *export_buffer = NULL;
1541
Gilles Peskine57ab7212019-01-28 13:03:09 +01001542 PSA_ASSERT( psa_crypto_init( ) );
1543
Gilles Peskineca25db92019-04-19 11:43:08 +02001544 /* Prepare the source key. */
1545 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1546 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001547 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001548 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001549 PSA_ASSERT( psa_import_key( &source_attributes,
1550 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001551 &source_key ) );
1552 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001553
Gilles Peskineca25db92019-04-19 11:43:08 +02001554 /* Prepare the target attributes. */
1555 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001556 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001557 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001558 /* Set volatile lifetime to reset the key identifier to 0. */
1559 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1560 }
1561
Gilles Peskineca25db92019-04-19 11:43:08 +02001562 if( target_usage_arg != -1 )
1563 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1564 if( target_alg_arg != -1 )
1565 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001566 if( target_alg2_arg != -1 )
1567 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001568
1569 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001570 PSA_ASSERT( psa_copy_key( source_key,
1571 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001572
1573 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001574 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001575
1576 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001577 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001578 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1579 psa_get_key_type( &target_attributes ) );
1580 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1581 psa_get_key_bits( &target_attributes ) );
1582 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1583 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001584 TEST_EQUAL( expected_alg2,
1585 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001586 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1587 {
1588 size_t length;
1589 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001590 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001591 material->len, &length ) );
1592 ASSERT_COMPARE( material->x, material->len,
1593 export_buffer, length );
1594 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001595
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001596 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001597 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001598 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001599 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001600
Ronald Cron5425a212020-08-04 14:58:35 +02001601 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001602
1603exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001604 /*
1605 * Source and target key attributes may have been returned by
1606 * psa_get_key_attributes() thus reset them as required.
1607 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001608 psa_reset_key_attributes( &source_attributes );
1609 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001610
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001611 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001612 mbedtls_free( export_buffer );
1613}
1614/* END_CASE */
1615
1616/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001617void copy_fail( int source_usage_arg,
1618 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001619 int type_arg, data_t *material,
1620 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001621 int target_usage_arg,
1622 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001623 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001624 int expected_status_arg )
1625{
1626 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1627 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001628 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1629 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001630 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001631
1632 PSA_ASSERT( psa_crypto_init( ) );
1633
1634 /* Prepare the source key. */
1635 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1636 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001637 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001638 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001639 PSA_ASSERT( psa_import_key( &source_attributes,
1640 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001641 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001642
1643 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001644 psa_set_key_id( &target_attributes, key_id );
1645 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001646 psa_set_key_type( &target_attributes, target_type_arg );
1647 psa_set_key_bits( &target_attributes, target_bits_arg );
1648 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1649 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001650 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001651
1652 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001653 TEST_EQUAL( psa_copy_key( source_key,
1654 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001655 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001656
Ronald Cron5425a212020-08-04 14:58:35 +02001657 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001658
Gilles Peskine4a644642019-05-03 17:14:08 +02001659exit:
1660 psa_reset_key_attributes( &source_attributes );
1661 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001662 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001663}
1664/* END_CASE */
1665
1666/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001667void hash_operation_init( )
1668{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001669 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001670 /* Test each valid way of initializing the object, except for `= {0}`, as
1671 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1672 * though it's OK by the C standard. We could test for this, but we'd need
1673 * to supress the Clang warning for the test. */
1674 psa_hash_operation_t func = psa_hash_operation_init( );
1675 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1676 psa_hash_operation_t zero;
1677
1678 memset( &zero, 0, sizeof( zero ) );
1679
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001680 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001681 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1682 PSA_ERROR_BAD_STATE );
1683 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1684 PSA_ERROR_BAD_STATE );
1685 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1686 PSA_ERROR_BAD_STATE );
1687
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001688 /* A default hash operation should be abortable without error. */
1689 PSA_ASSERT( psa_hash_abort( &func ) );
1690 PSA_ASSERT( psa_hash_abort( &init ) );
1691 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001692}
1693/* END_CASE */
1694
1695/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001696void hash_setup( int alg_arg,
1697 int expected_status_arg )
1698{
1699 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001700 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001701 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001702 psa_status_t status;
1703
Gilles Peskine8817f612018-12-18 00:18:46 +01001704 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001705
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001706 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001707 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001708
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001709 /* Whether setup succeeded or failed, abort must succeed. */
1710 PSA_ASSERT( psa_hash_abort( &operation ) );
1711
1712 /* If setup failed, reproduce the failure, so as to
1713 * test the resulting state of the operation object. */
1714 if( status != PSA_SUCCESS )
1715 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1716
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001717 /* Now the operation object should be reusable. */
1718#if defined(KNOWN_SUPPORTED_HASH_ALG)
1719 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1720 PSA_ASSERT( psa_hash_abort( &operation ) );
1721#endif
1722
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001723exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001724 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001725}
1726/* END_CASE */
1727
1728/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001729void hash_compute_fail( int alg_arg, data_t *input,
1730 int output_size_arg, int expected_status_arg )
1731{
1732 psa_algorithm_t alg = alg_arg;
1733 uint8_t *output = NULL;
1734 size_t output_size = output_size_arg;
1735 size_t output_length = INVALID_EXPORT_LENGTH;
1736 psa_status_t expected_status = expected_status_arg;
1737 psa_status_t status;
1738
1739 ASSERT_ALLOC( output, output_size );
1740
1741 PSA_ASSERT( psa_crypto_init( ) );
1742
1743 status = psa_hash_compute( alg, input->x, input->len,
1744 output, output_size, &output_length );
1745 TEST_EQUAL( status, expected_status );
1746 TEST_ASSERT( output_length <= output_size );
1747
1748exit:
1749 mbedtls_free( output );
1750 PSA_DONE( );
1751}
1752/* END_CASE */
1753
1754/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001755void hash_compare_fail( int alg_arg, data_t *input,
1756 data_t *reference_hash,
1757 int expected_status_arg )
1758{
1759 psa_algorithm_t alg = alg_arg;
1760 psa_status_t expected_status = expected_status_arg;
1761 psa_status_t status;
1762
1763 PSA_ASSERT( psa_crypto_init( ) );
1764
1765 status = psa_hash_compare( alg, input->x, input->len,
1766 reference_hash->x, reference_hash->len );
1767 TEST_EQUAL( status, expected_status );
1768
1769exit:
1770 PSA_DONE( );
1771}
1772/* END_CASE */
1773
1774/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001775void hash_compute_compare( int alg_arg, data_t *input,
1776 data_t *expected_output )
1777{
1778 psa_algorithm_t alg = alg_arg;
1779 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1780 size_t output_length = INVALID_EXPORT_LENGTH;
1781 size_t i;
1782
1783 PSA_ASSERT( psa_crypto_init( ) );
1784
1785 /* Compute with tight buffer */
1786 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001787 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001788 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001789 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001790 ASSERT_COMPARE( output, output_length,
1791 expected_output->x, expected_output->len );
1792
1793 /* Compute with larger buffer */
1794 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1795 output, sizeof( output ),
1796 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001797 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001798 ASSERT_COMPARE( output, output_length,
1799 expected_output->x, expected_output->len );
1800
1801 /* Compare with correct hash */
1802 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1803 output, output_length ) );
1804
1805 /* Compare with trailing garbage */
1806 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1807 output, output_length + 1 ),
1808 PSA_ERROR_INVALID_SIGNATURE );
1809
1810 /* Compare with truncated hash */
1811 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1812 output, output_length - 1 ),
1813 PSA_ERROR_INVALID_SIGNATURE );
1814
1815 /* Compare with corrupted value */
1816 for( i = 0; i < output_length; i++ )
1817 {
Chris Jones9634bb12021-01-20 15:56:42 +00001818 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001819 output[i] ^= 1;
1820 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1821 output, output_length ),
1822 PSA_ERROR_INVALID_SIGNATURE );
1823 output[i] ^= 1;
1824 }
1825
1826exit:
1827 PSA_DONE( );
1828}
1829/* END_CASE */
1830
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001831/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001832void hash_bad_order( )
1833{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001834 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001835 unsigned char input[] = "";
1836 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001837 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001838 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1839 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1840 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001841 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001842 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001843 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001844
Gilles Peskine8817f612018-12-18 00:18:46 +01001845 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001846
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001847 /* Call setup twice in a row. */
1848 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1849 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1850 PSA_ERROR_BAD_STATE );
1851 PSA_ASSERT( psa_hash_abort( &operation ) );
1852
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001853 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001854 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001855 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001856 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001857
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001858 /* Call update after finish. */
1859 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1860 PSA_ASSERT( psa_hash_finish( &operation,
1861 hash, sizeof( hash ), &hash_len ) );
1862 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001863 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001864 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001865
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001866 /* Call verify without calling setup beforehand. */
1867 TEST_EQUAL( psa_hash_verify( &operation,
1868 valid_hash, sizeof( valid_hash ) ),
1869 PSA_ERROR_BAD_STATE );
1870 PSA_ASSERT( psa_hash_abort( &operation ) );
1871
1872 /* Call verify after finish. */
1873 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1874 PSA_ASSERT( psa_hash_finish( &operation,
1875 hash, sizeof( hash ), &hash_len ) );
1876 TEST_EQUAL( psa_hash_verify( &operation,
1877 valid_hash, sizeof( valid_hash ) ),
1878 PSA_ERROR_BAD_STATE );
1879 PSA_ASSERT( psa_hash_abort( &operation ) );
1880
1881 /* Call verify twice in a row. */
1882 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1883 PSA_ASSERT( psa_hash_verify( &operation,
1884 valid_hash, sizeof( valid_hash ) ) );
1885 TEST_EQUAL( psa_hash_verify( &operation,
1886 valid_hash, sizeof( valid_hash ) ),
1887 PSA_ERROR_BAD_STATE );
1888 PSA_ASSERT( psa_hash_abort( &operation ) );
1889
1890 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001891 TEST_EQUAL( psa_hash_finish( &operation,
1892 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001893 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001894 PSA_ASSERT( psa_hash_abort( &operation ) );
1895
1896 /* Call finish twice in a row. */
1897 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1898 PSA_ASSERT( psa_hash_finish( &operation,
1899 hash, sizeof( hash ), &hash_len ) );
1900 TEST_EQUAL( psa_hash_finish( &operation,
1901 hash, sizeof( hash ), &hash_len ),
1902 PSA_ERROR_BAD_STATE );
1903 PSA_ASSERT( psa_hash_abort( &operation ) );
1904
1905 /* Call finish after calling verify. */
1906 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1907 PSA_ASSERT( psa_hash_verify( &operation,
1908 valid_hash, sizeof( valid_hash ) ) );
1909 TEST_EQUAL( psa_hash_finish( &operation,
1910 hash, sizeof( hash ), &hash_len ),
1911 PSA_ERROR_BAD_STATE );
1912 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001913
1914exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001915 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001916}
1917/* END_CASE */
1918
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001919/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001920void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001921{
1922 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001923 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1924 * appended to it */
1925 unsigned char hash[] = {
1926 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1927 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1928 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001929 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001930 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001931
Gilles Peskine8817f612018-12-18 00:18:46 +01001932 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001933
itayzafrir27e69452018-11-01 14:26:34 +02001934 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001935 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001936 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001937 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001938
itayzafrir27e69452018-11-01 14:26:34 +02001939 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001940 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001941 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001942 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001943
itayzafrir27e69452018-11-01 14:26:34 +02001944 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001945 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001946 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001947 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001948
itayzafrirec93d302018-10-18 18:01:10 +03001949exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001950 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001951}
1952/* END_CASE */
1953
Ronald Cronee414c72021-03-18 18:50:08 +01001954/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001955void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001956{
1957 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001958 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001959 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001960 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001961 size_t hash_len;
1962
Gilles Peskine8817f612018-12-18 00:18:46 +01001963 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001964
itayzafrir58028322018-10-25 10:22:01 +03001965 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001966 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001967 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001968 hash, expected_size - 1, &hash_len ),
1969 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001970
1971exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001972 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001973}
1974/* END_CASE */
1975
Ronald Cronee414c72021-03-18 18:50:08 +01001976/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001977void hash_clone_source_state( )
1978{
1979 psa_algorithm_t alg = PSA_ALG_SHA_256;
1980 unsigned char hash[PSA_HASH_MAX_SIZE];
1981 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1982 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1983 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1984 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1985 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1986 size_t hash_len;
1987
1988 PSA_ASSERT( psa_crypto_init( ) );
1989 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1990
1991 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1992 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1993 PSA_ASSERT( psa_hash_finish( &op_finished,
1994 hash, sizeof( hash ), &hash_len ) );
1995 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1996 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1997
1998 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1999 PSA_ERROR_BAD_STATE );
2000
2001 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2002 PSA_ASSERT( psa_hash_finish( &op_init,
2003 hash, sizeof( hash ), &hash_len ) );
2004 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2005 PSA_ASSERT( psa_hash_finish( &op_finished,
2006 hash, sizeof( hash ), &hash_len ) );
2007 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2008 PSA_ASSERT( psa_hash_finish( &op_aborted,
2009 hash, sizeof( hash ), &hash_len ) );
2010
2011exit:
2012 psa_hash_abort( &op_source );
2013 psa_hash_abort( &op_init );
2014 psa_hash_abort( &op_setup );
2015 psa_hash_abort( &op_finished );
2016 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002017 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002018}
2019/* END_CASE */
2020
Ronald Cronee414c72021-03-18 18:50:08 +01002021/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002022void hash_clone_target_state( )
2023{
2024 psa_algorithm_t alg = PSA_ALG_SHA_256;
2025 unsigned char hash[PSA_HASH_MAX_SIZE];
2026 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2027 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2028 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2029 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2030 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2031 size_t hash_len;
2032
2033 PSA_ASSERT( psa_crypto_init( ) );
2034
2035 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2036 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2037 PSA_ASSERT( psa_hash_finish( &op_finished,
2038 hash, sizeof( hash ), &hash_len ) );
2039 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2040 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2041
2042 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2043 PSA_ASSERT( psa_hash_finish( &op_target,
2044 hash, sizeof( hash ), &hash_len ) );
2045
2046 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2047 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2048 PSA_ERROR_BAD_STATE );
2049 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2050 PSA_ERROR_BAD_STATE );
2051
2052exit:
2053 psa_hash_abort( &op_target );
2054 psa_hash_abort( &op_init );
2055 psa_hash_abort( &op_setup );
2056 psa_hash_abort( &op_finished );
2057 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002058 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002059}
2060/* END_CASE */
2061
itayzafrir58028322018-10-25 10:22:01 +03002062/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002063void mac_operation_init( )
2064{
Jaeden Amero252ef282019-02-15 14:05:35 +00002065 const uint8_t input[1] = { 0 };
2066
Jaeden Amero769ce272019-01-04 11:48:03 +00002067 /* Test each valid way of initializing the object, except for `= {0}`, as
2068 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2069 * though it's OK by the C standard. We could test for this, but we'd need
2070 * to supress the Clang warning for the test. */
2071 psa_mac_operation_t func = psa_mac_operation_init( );
2072 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2073 psa_mac_operation_t zero;
2074
2075 memset( &zero, 0, sizeof( zero ) );
2076
Jaeden Amero252ef282019-02-15 14:05:35 +00002077 /* A freshly-initialized MAC operation should not be usable. */
2078 TEST_EQUAL( psa_mac_update( &func,
2079 input, sizeof( input ) ),
2080 PSA_ERROR_BAD_STATE );
2081 TEST_EQUAL( psa_mac_update( &init,
2082 input, sizeof( input ) ),
2083 PSA_ERROR_BAD_STATE );
2084 TEST_EQUAL( psa_mac_update( &zero,
2085 input, sizeof( input ) ),
2086 PSA_ERROR_BAD_STATE );
2087
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002088 /* A default MAC operation should be abortable without error. */
2089 PSA_ASSERT( psa_mac_abort( &func ) );
2090 PSA_ASSERT( psa_mac_abort( &init ) );
2091 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002092}
2093/* END_CASE */
2094
2095/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002096void mac_setup( int key_type_arg,
2097 data_t *key,
2098 int alg_arg,
2099 int expected_status_arg )
2100{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002101 psa_key_type_t key_type = key_type_arg;
2102 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002103 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002104 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002105 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2106#if defined(KNOWN_SUPPORTED_MAC_ALG)
2107 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2108#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002109
Gilles Peskine8817f612018-12-18 00:18:46 +01002110 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002111
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002112 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2113 &operation, &status ) )
2114 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002115 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002116
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002117 /* The operation object should be reusable. */
2118#if defined(KNOWN_SUPPORTED_MAC_ALG)
2119 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2120 smoke_test_key_data,
2121 sizeof( smoke_test_key_data ),
2122 KNOWN_SUPPORTED_MAC_ALG,
2123 &operation, &status ) )
2124 goto exit;
2125 TEST_EQUAL( status, PSA_SUCCESS );
2126#endif
2127
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002128exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002129 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002130}
2131/* END_CASE */
2132
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002133/* 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 +00002134void mac_bad_order( )
2135{
Ronald Cron5425a212020-08-04 14:58:35 +02002136 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002137 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2138 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002139 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002140 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2141 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2142 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002144 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2145 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2146 size_t sign_mac_length = 0;
2147 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2148 const uint8_t verify_mac[] = {
2149 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2150 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2151 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2152
2153 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002154 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002155 psa_set_key_algorithm( &attributes, alg );
2156 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002157
Ronald Cron5425a212020-08-04 14:58:35 +02002158 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2159 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002160
Jaeden Amero252ef282019-02-15 14:05:35 +00002161 /* Call update without calling setup beforehand. */
2162 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2163 PSA_ERROR_BAD_STATE );
2164 PSA_ASSERT( psa_mac_abort( &operation ) );
2165
2166 /* Call sign finish without calling setup beforehand. */
2167 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2168 &sign_mac_length),
2169 PSA_ERROR_BAD_STATE );
2170 PSA_ASSERT( psa_mac_abort( &operation ) );
2171
2172 /* Call verify finish without calling setup beforehand. */
2173 TEST_EQUAL( psa_mac_verify_finish( &operation,
2174 verify_mac, sizeof( verify_mac ) ),
2175 PSA_ERROR_BAD_STATE );
2176 PSA_ASSERT( psa_mac_abort( &operation ) );
2177
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002178 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002179 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2180 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002181 PSA_ERROR_BAD_STATE );
2182 PSA_ASSERT( psa_mac_abort( &operation ) );
2183
Jaeden Amero252ef282019-02-15 14:05:35 +00002184 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002185 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002186 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2187 PSA_ASSERT( psa_mac_sign_finish( &operation,
2188 sign_mac, sizeof( sign_mac ),
2189 &sign_mac_length ) );
2190 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2191 PSA_ERROR_BAD_STATE );
2192 PSA_ASSERT( psa_mac_abort( &operation ) );
2193
2194 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002195 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002196 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2197 PSA_ASSERT( psa_mac_verify_finish( &operation,
2198 verify_mac, sizeof( verify_mac ) ) );
2199 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2200 PSA_ERROR_BAD_STATE );
2201 PSA_ASSERT( psa_mac_abort( &operation ) );
2202
2203 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002204 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002205 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2206 PSA_ASSERT( psa_mac_sign_finish( &operation,
2207 sign_mac, sizeof( sign_mac ),
2208 &sign_mac_length ) );
2209 TEST_EQUAL( psa_mac_sign_finish( &operation,
2210 sign_mac, sizeof( sign_mac ),
2211 &sign_mac_length ),
2212 PSA_ERROR_BAD_STATE );
2213 PSA_ASSERT( psa_mac_abort( &operation ) );
2214
2215 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002216 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002217 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2218 PSA_ASSERT( psa_mac_verify_finish( &operation,
2219 verify_mac, sizeof( verify_mac ) ) );
2220 TEST_EQUAL( psa_mac_verify_finish( &operation,
2221 verify_mac, sizeof( verify_mac ) ),
2222 PSA_ERROR_BAD_STATE );
2223 PSA_ASSERT( psa_mac_abort( &operation ) );
2224
2225 /* Setup sign but try verify. */
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 TEST_EQUAL( psa_mac_verify_finish( &operation,
2229 verify_mac, sizeof( verify_mac ) ),
2230 PSA_ERROR_BAD_STATE );
2231 PSA_ASSERT( psa_mac_abort( &operation ) );
2232
2233 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002234 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002235 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2236 TEST_EQUAL( psa_mac_sign_finish( &operation,
2237 sign_mac, sizeof( sign_mac ),
2238 &sign_mac_length ),
2239 PSA_ERROR_BAD_STATE );
2240 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002241
Ronald Cron5425a212020-08-04 14:58:35 +02002242 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002243
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002244exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002245 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002246}
2247/* END_CASE */
2248
2249/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002250void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002251 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002252 int alg_arg,
2253 data_t *input,
2254 data_t *expected_mac )
2255{
Ronald Cron5425a212020-08-04 14:58:35 +02002256 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002257 psa_key_type_t key_type = key_type_arg;
2258 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002259 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002261 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002262 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002263 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002264 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002265 const size_t output_sizes_to_test[] = {
2266 0,
2267 1,
2268 expected_mac->len - 1,
2269 expected_mac->len,
2270 expected_mac->len + 1,
2271 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002272
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002273 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002274 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002275 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002276
Gilles Peskine8817f612018-12-18 00:18:46 +01002277 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002278
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002279 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002280 psa_set_key_algorithm( &attributes, alg );
2281 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002282
Ronald Cron5425a212020-08-04 14:58:35 +02002283 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2284 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002285
Gilles Peskine8b356b52020-08-25 23:44:59 +02002286 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2287 {
2288 const size_t output_size = output_sizes_to_test[i];
2289 psa_status_t expected_status =
2290 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2291 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002292
Chris Jones9634bb12021-01-20 15:56:42 +00002293 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002294 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002295
Gilles Peskine8b356b52020-08-25 23:44:59 +02002296 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002297 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002298 PSA_ASSERT( psa_mac_update( &operation,
2299 input->x, input->len ) );
2300 TEST_EQUAL( psa_mac_sign_finish( &operation,
2301 actual_mac, output_size,
2302 &mac_length ),
2303 expected_status );
2304 PSA_ASSERT( psa_mac_abort( &operation ) );
2305
2306 if( expected_status == PSA_SUCCESS )
2307 {
2308 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2309 actual_mac, mac_length );
2310 }
2311 mbedtls_free( actual_mac );
2312 actual_mac = NULL;
2313 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002314
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002315exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002316 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002317 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002318 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002319 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002320}
2321/* END_CASE */
2322
2323/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002324void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002325 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002326 int alg_arg,
2327 data_t *input,
2328 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002329{
Ronald Cron5425a212020-08-04 14:58:35 +02002330 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002331 psa_key_type_t key_type = key_type_arg;
2332 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002333 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002335 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002336
Gilles Peskine69c12672018-06-28 00:07:19 +02002337 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2338
Gilles Peskine8817f612018-12-18 00:18:46 +01002339 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002340
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002341 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002342 psa_set_key_algorithm( &attributes, alg );
2343 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002344
Ronald Cron5425a212020-08-04 14:58:35 +02002345 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2346 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002347
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002348 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002349 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002350 PSA_ASSERT( psa_mac_update( &operation,
2351 input->x, input->len ) );
2352 PSA_ASSERT( psa_mac_verify_finish( &operation,
2353 expected_mac->x,
2354 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002355
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002356 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002357 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002358 PSA_ASSERT( psa_mac_update( &operation,
2359 input->x, input->len ) );
2360 TEST_EQUAL( psa_mac_verify_finish( &operation,
2361 expected_mac->x,
2362 expected_mac->len - 1 ),
2363 PSA_ERROR_INVALID_SIGNATURE );
2364
2365 /* Test a MAC that's too long. */
2366 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2367 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002368 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002369 PSA_ASSERT( psa_mac_update( &operation,
2370 input->x, input->len ) );
2371 TEST_EQUAL( psa_mac_verify_finish( &operation,
2372 perturbed_mac,
2373 expected_mac->len + 1 ),
2374 PSA_ERROR_INVALID_SIGNATURE );
2375
2376 /* Test changing one byte. */
2377 for( size_t i = 0; i < expected_mac->len; i++ )
2378 {
Chris Jones9634bb12021-01-20 15:56:42 +00002379 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002380 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002381 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002382 PSA_ASSERT( psa_mac_update( &operation,
2383 input->x, input->len ) );
2384 TEST_EQUAL( psa_mac_verify_finish( &operation,
2385 perturbed_mac,
2386 expected_mac->len ),
2387 PSA_ERROR_INVALID_SIGNATURE );
2388 perturbed_mac[i] ^= 1;
2389 }
2390
Gilles Peskine8c9def32018-02-08 10:02:12 +01002391exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002392 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002393 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002394 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002395 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002396}
2397/* END_CASE */
2398
2399/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002400void cipher_operation_init( )
2401{
Jaeden Ameroab439972019-02-15 14:12:05 +00002402 const uint8_t input[1] = { 0 };
2403 unsigned char output[1] = { 0 };
2404 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002405 /* Test each valid way of initializing the object, except for `= {0}`, as
2406 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2407 * though it's OK by the C standard. We could test for this, but we'd need
2408 * to supress the Clang warning for the test. */
2409 psa_cipher_operation_t func = psa_cipher_operation_init( );
2410 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2411 psa_cipher_operation_t zero;
2412
2413 memset( &zero, 0, sizeof( zero ) );
2414
Jaeden Ameroab439972019-02-15 14:12:05 +00002415 /* A freshly-initialized cipher operation should not be usable. */
2416 TEST_EQUAL( psa_cipher_update( &func,
2417 input, sizeof( input ),
2418 output, sizeof( output ),
2419 &output_length ),
2420 PSA_ERROR_BAD_STATE );
2421 TEST_EQUAL( psa_cipher_update( &init,
2422 input, sizeof( input ),
2423 output, sizeof( output ),
2424 &output_length ),
2425 PSA_ERROR_BAD_STATE );
2426 TEST_EQUAL( psa_cipher_update( &zero,
2427 input, sizeof( input ),
2428 output, sizeof( output ),
2429 &output_length ),
2430 PSA_ERROR_BAD_STATE );
2431
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002432 /* A default cipher operation should be abortable without error. */
2433 PSA_ASSERT( psa_cipher_abort( &func ) );
2434 PSA_ASSERT( psa_cipher_abort( &init ) );
2435 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002436}
2437/* END_CASE */
2438
2439/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002440void cipher_setup( int key_type_arg,
2441 data_t *key,
2442 int alg_arg,
2443 int expected_status_arg )
2444{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002445 psa_key_type_t key_type = key_type_arg;
2446 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002447 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002448 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002450#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002451 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2452#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002453
Gilles Peskine8817f612018-12-18 00:18:46 +01002454 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002455
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002456 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2457 &operation, &status ) )
2458 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002459 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002460
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002461 /* The operation object should be reusable. */
2462#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2463 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2464 smoke_test_key_data,
2465 sizeof( smoke_test_key_data ),
2466 KNOWN_SUPPORTED_CIPHER_ALG,
2467 &operation, &status ) )
2468 goto exit;
2469 TEST_EQUAL( status, PSA_SUCCESS );
2470#endif
2471
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002472exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002473 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002474 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002475}
2476/* END_CASE */
2477
Ronald Cronee414c72021-03-18 18:50:08 +01002478/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002479void cipher_bad_order( )
2480{
Ronald Cron5425a212020-08-04 14:58:35 +02002481 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002482 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2483 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002484 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002485 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002486 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002487 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002488 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2489 0xaa, 0xaa, 0xaa, 0xaa };
2490 const uint8_t text[] = {
2491 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2492 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002493 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002494 size_t length = 0;
2495
2496 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002497 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2498 psa_set_key_algorithm( &attributes, alg );
2499 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002500 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2501 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002502
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002503 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002504 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2505 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002506 PSA_ERROR_BAD_STATE );
2507 PSA_ASSERT( psa_cipher_abort( &operation ) );
2508
2509 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002510 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2511 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002512 PSA_ERROR_BAD_STATE );
2513 PSA_ASSERT( psa_cipher_abort( &operation ) );
2514
Jaeden Ameroab439972019-02-15 14:12:05 +00002515 /* Generate an IV without calling setup beforehand. */
2516 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2517 buffer, sizeof( buffer ),
2518 &length ),
2519 PSA_ERROR_BAD_STATE );
2520 PSA_ASSERT( psa_cipher_abort( &operation ) );
2521
2522 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002523 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002524 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2525 buffer, sizeof( buffer ),
2526 &length ) );
2527 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2528 buffer, sizeof( buffer ),
2529 &length ),
2530 PSA_ERROR_BAD_STATE );
2531 PSA_ASSERT( psa_cipher_abort( &operation ) );
2532
2533 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002534 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002535 PSA_ASSERT( psa_cipher_set_iv( &operation,
2536 iv, sizeof( iv ) ) );
2537 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2538 buffer, sizeof( buffer ),
2539 &length ),
2540 PSA_ERROR_BAD_STATE );
2541 PSA_ASSERT( psa_cipher_abort( &operation ) );
2542
2543 /* Set an IV without calling setup beforehand. */
2544 TEST_EQUAL( psa_cipher_set_iv( &operation,
2545 iv, sizeof( iv ) ),
2546 PSA_ERROR_BAD_STATE );
2547 PSA_ASSERT( psa_cipher_abort( &operation ) );
2548
2549 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002550 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002551 PSA_ASSERT( psa_cipher_set_iv( &operation,
2552 iv, sizeof( iv ) ) );
2553 TEST_EQUAL( psa_cipher_set_iv( &operation,
2554 iv, sizeof( iv ) ),
2555 PSA_ERROR_BAD_STATE );
2556 PSA_ASSERT( psa_cipher_abort( &operation ) );
2557
2558 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002559 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002560 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2561 buffer, sizeof( buffer ),
2562 &length ) );
2563 TEST_EQUAL( psa_cipher_set_iv( &operation,
2564 iv, sizeof( iv ) ),
2565 PSA_ERROR_BAD_STATE );
2566 PSA_ASSERT( psa_cipher_abort( &operation ) );
2567
2568 /* Call update without calling setup beforehand. */
2569 TEST_EQUAL( psa_cipher_update( &operation,
2570 text, sizeof( text ),
2571 buffer, sizeof( buffer ),
2572 &length ),
2573 PSA_ERROR_BAD_STATE );
2574 PSA_ASSERT( psa_cipher_abort( &operation ) );
2575
2576 /* Call update without an IV where an IV is required. */
2577 TEST_EQUAL( psa_cipher_update( &operation,
2578 text, sizeof( text ),
2579 buffer, sizeof( buffer ),
2580 &length ),
2581 PSA_ERROR_BAD_STATE );
2582 PSA_ASSERT( psa_cipher_abort( &operation ) );
2583
2584 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002585 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002586 PSA_ASSERT( psa_cipher_set_iv( &operation,
2587 iv, sizeof( iv ) ) );
2588 PSA_ASSERT( psa_cipher_finish( &operation,
2589 buffer, sizeof( buffer ), &length ) );
2590 TEST_EQUAL( psa_cipher_update( &operation,
2591 text, sizeof( text ),
2592 buffer, sizeof( buffer ),
2593 &length ),
2594 PSA_ERROR_BAD_STATE );
2595 PSA_ASSERT( psa_cipher_abort( &operation ) );
2596
2597 /* Call finish without calling setup beforehand. */
2598 TEST_EQUAL( psa_cipher_finish( &operation,
2599 buffer, sizeof( buffer ), &length ),
2600 PSA_ERROR_BAD_STATE );
2601 PSA_ASSERT( psa_cipher_abort( &operation ) );
2602
2603 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002604 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002605 /* Not calling update means we are encrypting an empty buffer, which is OK
2606 * for cipher modes with padding. */
2607 TEST_EQUAL( psa_cipher_finish( &operation,
2608 buffer, sizeof( buffer ), &length ),
2609 PSA_ERROR_BAD_STATE );
2610 PSA_ASSERT( psa_cipher_abort( &operation ) );
2611
2612 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002613 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002614 PSA_ASSERT( psa_cipher_set_iv( &operation,
2615 iv, sizeof( iv ) ) );
2616 PSA_ASSERT( psa_cipher_finish( &operation,
2617 buffer, sizeof( buffer ), &length ) );
2618 TEST_EQUAL( psa_cipher_finish( &operation,
2619 buffer, sizeof( buffer ), &length ),
2620 PSA_ERROR_BAD_STATE );
2621 PSA_ASSERT( psa_cipher_abort( &operation ) );
2622
Ronald Cron5425a212020-08-04 14:58:35 +02002623 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002624
Jaeden Ameroab439972019-02-15 14:12:05 +00002625exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002626 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002627 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002628}
2629/* END_CASE */
2630
2631/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002632void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002633 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002634 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002635 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002636{
Ronald Cron5425a212020-08-04 14:58:35 +02002637 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002638 psa_status_t status;
2639 psa_key_type_t key_type = key_type_arg;
2640 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002641 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002642 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002643 size_t output_buffer_size = 0;
2644 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002645 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002646 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002647 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002648
Gilles Peskine8817f612018-12-18 00:18:46 +01002649 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002650
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002651 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2652 psa_set_key_algorithm( &attributes, alg );
2653 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002654
Ronald Cron5425a212020-08-04 14:58:35 +02002655 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2656 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657
Ronald Cron5425a212020-08-04 14:58:35 +02002658 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002660 if( iv->len > 0 )
2661 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002662 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002663 }
2664
gabor-mezei-armceface22021-01-21 12:26:17 +01002665 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2666 TEST_ASSERT( output_buffer_size <=
2667 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002668 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002669
Gilles Peskine8817f612018-12-18 00:18:46 +01002670 PSA_ASSERT( psa_cipher_update( &operation,
2671 input->x, input->len,
2672 output, output_buffer_size,
2673 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002674 TEST_ASSERT( function_output_length <=
2675 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2676 TEST_ASSERT( function_output_length <=
2677 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002678 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002679
Gilles Peskine50e586b2018-06-08 14:28:46 +02002680 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002681 ( output_buffer_size == 0 ? NULL :
2682 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002683 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002685 TEST_ASSERT( function_output_length <=
2686 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2687 TEST_ASSERT( function_output_length <=
2688 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002689 total_output_length += function_output_length;
2690
Gilles Peskinefe11b722018-12-18 00:24:04 +01002691 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002692 if( expected_status == PSA_SUCCESS )
2693 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002694 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002695 ASSERT_COMPARE( expected_output->x, expected_output->len,
2696 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002697 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002698
Gilles Peskine50e586b2018-06-08 14:28:46 +02002699exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002700 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002701 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002702 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002703 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002704}
2705/* END_CASE */
2706
2707/* BEGIN_CASE */
2708void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002709 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002710 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002711 int first_part_size_arg,
2712 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002713 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002714{
Ronald Cron5425a212020-08-04 14:58:35 +02002715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002716 psa_key_type_t key_type = key_type_arg;
2717 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002718 size_t first_part_size = first_part_size_arg;
2719 size_t output1_length = output1_length_arg;
2720 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002721 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002722 size_t output_buffer_size = 0;
2723 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002724 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002725 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002727
Gilles Peskine8817f612018-12-18 00:18:46 +01002728 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002729
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002730 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2731 psa_set_key_algorithm( &attributes, alg );
2732 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002733
Ronald Cron5425a212020-08-04 14:58:35 +02002734 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2735 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736
Ronald Cron5425a212020-08-04 14:58:35 +02002737 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002739 if( iv->len > 0 )
2740 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002741 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002742 }
2743
gabor-mezei-armceface22021-01-21 12:26:17 +01002744 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2745 TEST_ASSERT( output_buffer_size <=
2746 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002747 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002748
Gilles Peskinee0866522019-02-19 19:44:00 +01002749 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002750 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2751 output, output_buffer_size,
2752 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002753 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002754 TEST_ASSERT( function_output_length <=
2755 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2756 TEST_ASSERT( function_output_length <=
2757 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002758 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002759
Gilles Peskine8817f612018-12-18 00:18:46 +01002760 PSA_ASSERT( psa_cipher_update( &operation,
2761 input->x + first_part_size,
2762 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002763 ( output_buffer_size == 0 ? NULL :
2764 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002765 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002766 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002767 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002768 TEST_ASSERT( function_output_length <=
2769 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2770 alg,
2771 input->len - first_part_size ) );
2772 TEST_ASSERT( function_output_length <=
2773 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002774 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002775
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002777 ( output_buffer_size == 0 ? NULL :
2778 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002779 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002781 TEST_ASSERT( function_output_length <=
2782 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2783 TEST_ASSERT( function_output_length <=
2784 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002785 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002786 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002787
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002788 ASSERT_COMPARE( expected_output->x, expected_output->len,
2789 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002790
2791exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002792 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002793 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002794 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002795 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002796}
2797/* END_CASE */
2798
2799/* BEGIN_CASE */
2800void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002801 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002802 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002803 int first_part_size_arg,
2804 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002805 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002806{
Ronald Cron5425a212020-08-04 14:58:35 +02002807 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002808 psa_key_type_t key_type = key_type_arg;
2809 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002810 size_t first_part_size = first_part_size_arg;
2811 size_t output1_length = output1_length_arg;
2812 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002813 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002814 size_t output_buffer_size = 0;
2815 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002816 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002817 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002819
Gilles Peskine8817f612018-12-18 00:18:46 +01002820 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002821
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002822 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2823 psa_set_key_algorithm( &attributes, alg );
2824 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002825
Ronald Cron5425a212020-08-04 14:58:35 +02002826 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2827 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828
Ronald Cron5425a212020-08-04 14:58:35 +02002829 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830
Steven Cooreman177deba2020-09-07 17:14:14 +02002831 if( iv->len > 0 )
2832 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002833 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002834 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002835
gabor-mezei-armceface22021-01-21 12:26:17 +01002836 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2837 TEST_ASSERT( output_buffer_size <=
2838 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002839 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002840
Gilles Peskinee0866522019-02-19 19:44:00 +01002841 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 PSA_ASSERT( psa_cipher_update( &operation,
2843 input->x, first_part_size,
2844 output, output_buffer_size,
2845 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002846 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002847 TEST_ASSERT( function_output_length <=
2848 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2849 TEST_ASSERT( function_output_length <=
2850 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002851 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002852
Gilles Peskine8817f612018-12-18 00:18:46 +01002853 PSA_ASSERT( psa_cipher_update( &operation,
2854 input->x + first_part_size,
2855 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002856 ( output_buffer_size == 0 ? NULL :
2857 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002858 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002859 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002860 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002861 TEST_ASSERT( function_output_length <=
2862 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2863 alg,
2864 input->len - first_part_size ) );
2865 TEST_ASSERT( function_output_length <=
2866 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002867 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002868
Gilles Peskine8817f612018-12-18 00:18:46 +01002869 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002870 ( output_buffer_size == 0 ? NULL :
2871 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002872 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002874 TEST_ASSERT( function_output_length <=
2875 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2876 TEST_ASSERT( function_output_length <=
2877 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002878 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002879 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002880
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002881 ASSERT_COMPARE( expected_output->x, expected_output->len,
2882 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002883
2884exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002885 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002886 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002887 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002888 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889}
2890/* END_CASE */
2891
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892/* BEGIN_CASE */
2893void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002894 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002895 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002896 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002897{
Ronald Cron5425a212020-08-04 14:58:35 +02002898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002899 psa_status_t status;
2900 psa_key_type_t key_type = key_type_arg;
2901 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002902 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002903 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002904 size_t output_buffer_size = 0;
2905 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002906 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002907 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002908 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002909
Gilles Peskine8817f612018-12-18 00:18:46 +01002910 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002911
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002912 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2913 psa_set_key_algorithm( &attributes, alg );
2914 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002915
Ronald Cron5425a212020-08-04 14:58:35 +02002916 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2917 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002918
Ronald Cron5425a212020-08-04 14:58:35 +02002919 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920
Steven Cooreman177deba2020-09-07 17:14:14 +02002921 if( iv->len > 0 )
2922 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002923 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002924 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002925
gabor-mezei-armceface22021-01-21 12:26:17 +01002926 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2927 TEST_ASSERT( output_buffer_size <=
2928 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002929 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002930
Gilles Peskine8817f612018-12-18 00:18:46 +01002931 PSA_ASSERT( psa_cipher_update( &operation,
2932 input->x, input->len,
2933 output, output_buffer_size,
2934 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002935 TEST_ASSERT( function_output_length <=
2936 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2937 TEST_ASSERT( function_output_length <=
2938 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002939 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002940
Gilles Peskine50e586b2018-06-08 14:28:46 +02002941 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002942 ( output_buffer_size == 0 ? NULL :
2943 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002944 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002945 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002946 TEST_ASSERT( function_output_length <=
2947 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2948 TEST_ASSERT( function_output_length <=
2949 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002950 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002951 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002952
2953 if( expected_status == PSA_SUCCESS )
2954 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002955 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002956 ASSERT_COMPARE( expected_output->x, expected_output->len,
2957 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002958 }
2959
Gilles Peskine50e586b2018-06-08 14:28:46 +02002960exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002961 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002962 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002963 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002964 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002965}
2966/* END_CASE */
2967
Gilles Peskine50e586b2018-06-08 14:28:46 +02002968/* BEGIN_CASE */
2969void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002970 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002971 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002972{
Ronald Cron5425a212020-08-04 14:58:35 +02002973 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002974 psa_key_type_t key_type = key_type_arg;
2975 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002976 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002977 size_t iv_size = 16;
2978 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002979 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002980 size_t output1_size = 0;
2981 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002982 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002983 size_t output2_size = 0;
2984 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002985 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002986 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2987 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002989
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002991
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002992 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2993 psa_set_key_algorithm( &attributes, alg );
2994 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002995
Ronald Cron5425a212020-08-04 14:58:35 +02002996 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2997 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002998
Ronald Cron5425a212020-08-04 14:58:35 +02002999 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3000 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003001
Steven Cooreman177deba2020-09-07 17:14:14 +02003002 if( alg != PSA_ALG_ECB_NO_PADDING )
3003 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003004 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3005 iv, iv_size,
3006 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003007 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003008 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3009 TEST_ASSERT( output1_size <=
3010 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003011 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003012
Gilles Peskine8817f612018-12-18 00:18:46 +01003013 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3014 output1, output1_size,
3015 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003016 TEST_ASSERT( output1_length <=
3017 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3018 TEST_ASSERT( output1_length <=
3019 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3020
Gilles Peskine8817f612018-12-18 00:18:46 +01003021 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003022 output1 + output1_length,
3023 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003024 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003025 TEST_ASSERT( function_output_length <=
3026 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3027 TEST_ASSERT( function_output_length <=
3028 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003029
Gilles Peskine048b7f02018-06-08 14:20:49 +02003030 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003031
Gilles Peskine8817f612018-12-18 00:18:46 +01003032 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003033
3034 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003035 TEST_ASSERT( output2_size <=
3036 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3037 TEST_ASSERT( output2_size <=
3038 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003039 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003040
Steven Cooreman177deba2020-09-07 17:14:14 +02003041 if( iv_length > 0 )
3042 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003043 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3044 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003045 }
3046
Gilles Peskine8817f612018-12-18 00:18:46 +01003047 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3048 output2, output2_size,
3049 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003050 TEST_ASSERT( output2_length <=
3051 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3052 TEST_ASSERT( output2_length <=
3053 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3054
Gilles Peskine048b7f02018-06-08 14:20:49 +02003055 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003056 PSA_ASSERT( psa_cipher_finish( &operation2,
3057 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003058 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003059 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003060 TEST_ASSERT( function_output_length <=
3061 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3062 TEST_ASSERT( function_output_length <=
3063 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003064
Gilles Peskine048b7f02018-06-08 14:20:49 +02003065 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003066
Gilles Peskine8817f612018-12-18 00:18:46 +01003067 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003068
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003069 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003070
3071exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003072 psa_cipher_abort( &operation1 );
3073 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003074 mbedtls_free( output1 );
3075 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003076 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003077 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003078}
3079/* END_CASE */
3080
3081/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003082void cipher_verify_output_multipart( int alg_arg,
3083 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003084 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003085 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003086 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003087{
Ronald Cron5425a212020-08-04 14:58:35 +02003088 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003089 psa_key_type_t key_type = key_type_arg;
3090 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003091 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003092 unsigned char iv[16] = {0};
3093 size_t iv_size = 16;
3094 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003095 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003096 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003097 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003098 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003099 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003100 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003101 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003102 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3103 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003104 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003105
Gilles Peskine8817f612018-12-18 00:18:46 +01003106 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003107
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003108 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3109 psa_set_key_algorithm( &attributes, alg );
3110 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003111
Ronald Cron5425a212020-08-04 14:58:35 +02003112 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3113 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003114
Ronald Cron5425a212020-08-04 14:58:35 +02003115 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3116 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003117
Steven Cooreman177deba2020-09-07 17:14:14 +02003118 if( alg != PSA_ALG_ECB_NO_PADDING )
3119 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003120 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3121 iv, iv_size,
3122 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003123 }
3124
gabor-mezei-armceface22021-01-21 12:26:17 +01003125 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3126 TEST_ASSERT( output1_buffer_size <=
3127 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003128 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003129
Gilles Peskinee0866522019-02-19 19:44:00 +01003130 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003131
Gilles Peskine8817f612018-12-18 00:18:46 +01003132 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3133 output1, output1_buffer_size,
3134 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003135 TEST_ASSERT( function_output_length <=
3136 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3137 TEST_ASSERT( function_output_length <=
3138 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003139 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003140
Gilles Peskine8817f612018-12-18 00:18:46 +01003141 PSA_ASSERT( psa_cipher_update( &operation1,
3142 input->x + first_part_size,
3143 input->len - first_part_size,
3144 output1, output1_buffer_size,
3145 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003146 TEST_ASSERT( function_output_length <=
3147 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3148 alg,
3149 input->len - first_part_size ) );
3150 TEST_ASSERT( function_output_length <=
3151 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003152 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003153
Gilles Peskine8817f612018-12-18 00:18:46 +01003154 PSA_ASSERT( psa_cipher_finish( &operation1,
3155 output1 + output1_length,
3156 output1_buffer_size - output1_length,
3157 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003158 TEST_ASSERT( function_output_length <=
3159 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3160 TEST_ASSERT( function_output_length <=
3161 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003162 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003163
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003165
Gilles Peskine048b7f02018-06-08 14:20:49 +02003166 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003167 TEST_ASSERT( output2_buffer_size <=
3168 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3169 TEST_ASSERT( output2_buffer_size <=
3170 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003171 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003172
Steven Cooreman177deba2020-09-07 17:14:14 +02003173 if( iv_length > 0 )
3174 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003175 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3176 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003177 }
Moran Pekerded84402018-06-06 16:36:50 +03003178
Gilles Peskine8817f612018-12-18 00:18:46 +01003179 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3180 output2, output2_buffer_size,
3181 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003182 TEST_ASSERT( function_output_length <=
3183 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3184 TEST_ASSERT( function_output_length <=
3185 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003186 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003187
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 PSA_ASSERT( psa_cipher_update( &operation2,
3189 output1 + first_part_size,
3190 output1_length - first_part_size,
3191 output2, output2_buffer_size,
3192 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003193 TEST_ASSERT( function_output_length <=
3194 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3195 alg,
3196 output1_length - first_part_size ) );
3197 TEST_ASSERT( function_output_length <=
3198 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003199 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003200
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_cipher_finish( &operation2,
3202 output2 + output2_length,
3203 output2_buffer_size - output2_length,
3204 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003205 TEST_ASSERT( function_output_length <=
3206 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3207 TEST_ASSERT( function_output_length <=
3208 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003209 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003210
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003212
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003213 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003214
3215exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003216 psa_cipher_abort( &operation1 );
3217 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003218 mbedtls_free( output1 );
3219 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003220 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003221 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003222}
3223/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003224
Gilles Peskine20035e32018-02-03 22:44:14 +01003225/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003226void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003227 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003228 data_t *nonce,
3229 data_t *additional_data,
3230 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003231 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003232{
Ronald Cron5425a212020-08-04 14:58:35 +02003233 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003234 psa_key_type_t key_type = key_type_arg;
3235 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003236 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003237 unsigned char *output_data = NULL;
3238 size_t output_size = 0;
3239 size_t output_length = 0;
3240 unsigned char *output_data2 = NULL;
3241 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003242 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003243 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003244 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003245
Gilles Peskine8817f612018-12-18 00:18:46 +01003246 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003247
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003248 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3249 psa_set_key_algorithm( &attributes, alg );
3250 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251
Gilles Peskine049c7532019-05-15 20:22:09 +02003252 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003253 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003254 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3255 key_bits = psa_get_key_bits( &attributes );
3256
3257 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3258 alg );
3259 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3260 * should be exact. */
3261 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3262 expected_result != PSA_ERROR_NOT_SUPPORTED )
3263 {
3264 TEST_EQUAL( output_size,
3265 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3266 TEST_ASSERT( output_size <=
3267 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3268 }
3269 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003270
Steven Cooremanf49478b2021-02-15 15:19:25 +01003271 status = psa_aead_encrypt( key, alg,
3272 nonce->x, nonce->len,
3273 additional_data->x,
3274 additional_data->len,
3275 input_data->x, input_data->len,
3276 output_data, output_size,
3277 &output_length );
3278
3279 /* If the operation is not supported, just skip and not fail in case the
3280 * encryption involves a common limitation of cryptography hardwares and
3281 * an alternative implementation. */
3282 if( status == PSA_ERROR_NOT_SUPPORTED )
3283 {
3284 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3285 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3286 }
3287
3288 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003289
3290 if( PSA_SUCCESS == expected_result )
3291 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003292 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003293
Gilles Peskine003a4a92019-05-14 16:09:40 +02003294 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3295 * should be exact. */
3296 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003297 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003298
gabor-mezei-armceface22021-01-21 12:26:17 +01003299 TEST_ASSERT( input_data->len <=
3300 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3301
Ronald Cron5425a212020-08-04 14:58:35 +02003302 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003303 nonce->x, nonce->len,
3304 additional_data->x,
3305 additional_data->len,
3306 output_data, output_length,
3307 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003308 &output_length2 ),
3309 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003310
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003311 ASSERT_COMPARE( input_data->x, input_data->len,
3312 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003313 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003314
Gilles Peskinea1cac842018-06-11 19:33:02 +02003315exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003316 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317 mbedtls_free( output_data );
3318 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003319 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003320}
3321/* END_CASE */
3322
3323/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003324void aead_encrypt( int key_type_arg, data_t *key_data,
3325 int alg_arg,
3326 data_t *nonce,
3327 data_t *additional_data,
3328 data_t *input_data,
3329 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003330{
Ronald Cron5425a212020-08-04 14:58:35 +02003331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003332 psa_key_type_t key_type = key_type_arg;
3333 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003334 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003335 unsigned char *output_data = NULL;
3336 size_t output_size = 0;
3337 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003339 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003340
Gilles Peskine8817f612018-12-18 00:18:46 +01003341 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003343 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3344 psa_set_key_algorithm( &attributes, alg );
3345 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003346
Gilles Peskine049c7532019-05-15 20:22:09 +02003347 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003348 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003349 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3350 key_bits = psa_get_key_bits( &attributes );
3351
3352 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3353 alg );
3354 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3355 * should be exact. */
3356 TEST_EQUAL( output_size,
3357 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3358 TEST_ASSERT( output_size <=
3359 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3360 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003361
Steven Cooremand588ea12021-01-11 19:36:04 +01003362 status = psa_aead_encrypt( key, alg,
3363 nonce->x, nonce->len,
3364 additional_data->x, additional_data->len,
3365 input_data->x, input_data->len,
3366 output_data, output_size,
3367 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003368
Ronald Cron28a45ed2021-02-09 20:35:42 +01003369 /* If the operation is not supported, just skip and not fail in case the
3370 * encryption involves a common limitation of cryptography hardwares and
3371 * an alternative implementation. */
3372 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003373 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003374 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3375 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003376 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003377
3378 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003379 ASSERT_COMPARE( expected_result->x, expected_result->len,
3380 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003381
Gilles Peskinea1cac842018-06-11 19:33:02 +02003382exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003383 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003384 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003385 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003386}
3387/* END_CASE */
3388
3389/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003390void aead_decrypt( int key_type_arg, data_t *key_data,
3391 int alg_arg,
3392 data_t *nonce,
3393 data_t *additional_data,
3394 data_t *input_data,
3395 data_t *expected_data,
3396 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003397{
Ronald Cron5425a212020-08-04 14:58:35 +02003398 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003399 psa_key_type_t key_type = key_type_arg;
3400 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003401 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402 unsigned char *output_data = NULL;
3403 size_t output_size = 0;
3404 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003406 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003407 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003408
Gilles Peskine8817f612018-12-18 00:18:46 +01003409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003410
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3412 psa_set_key_algorithm( &attributes, alg );
3413 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414
Gilles Peskine049c7532019-05-15 20:22:09 +02003415 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003416 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003417 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3418 key_bits = psa_get_key_bits( &attributes );
3419
3420 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3421 alg );
3422 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3423 expected_result != PSA_ERROR_NOT_SUPPORTED )
3424 {
3425 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3426 * should be exact. */
3427 TEST_EQUAL( output_size,
3428 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3429 TEST_ASSERT( output_size <=
3430 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3431 }
3432 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003433
Steven Cooremand588ea12021-01-11 19:36:04 +01003434 status = psa_aead_decrypt( key, alg,
3435 nonce->x, nonce->len,
3436 additional_data->x,
3437 additional_data->len,
3438 input_data->x, input_data->len,
3439 output_data, output_size,
3440 &output_length );
3441
Ronald Cron28a45ed2021-02-09 20:35:42 +01003442 /* If the operation is not supported, just skip and not fail in case the
3443 * decryption involves a common limitation of cryptography hardwares and
3444 * an alternative implementation. */
3445 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003446 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003447 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3448 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003449 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003450
3451 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003452
Gilles Peskine2d277862018-06-18 15:41:12 +02003453 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003454 ASSERT_COMPARE( expected_data->x, expected_data->len,
3455 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456
Gilles Peskinea1cac842018-06-11 19:33:02 +02003457exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003458 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003460 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461}
3462/* END_CASE */
3463
3464/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003465void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3466 int alg_arg,
3467 data_t *nonce,
3468 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003469 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003470 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003471 int do_test_data_chunked,
3472 int do_set_lengths,
3473 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003474{
Paul Elliottd3f82412021-06-16 16:52:21 +01003475 size_t ad_part_len = 0;
3476 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003477
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003478 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3479
3480 /* Temporary whilst we have algorithms that cannot support chunking */
3481 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003482 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003483 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3484 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003485 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003486 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003487
Paul Elliott329d5382021-07-22 17:10:45 +01003488 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003489 if( !aead_multipart_internal_func( key_type_arg, key_data,
3490 alg_arg, nonce,
3491 additional_data,
3492 ad_part_len,
3493 input_data, -1,
3494 do_set_lengths,
3495 expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +01003496 1, 1, 0 ) )
3497 break;
3498
3499 /* length(0) part, length(ad_part_len) part, length(0) part... */
3500 mbedtls_test_set_step( 1000 + ad_part_len );
3501
3502 if( !aead_multipart_internal_func( key_type_arg, key_data,
3503 alg_arg, nonce,
3504 additional_data,
3505 ad_part_len,
3506 input_data, -1,
3507 do_set_lengths,
3508 expected_output,
3509 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003510 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003511 }
3512 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003513
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003514 /* Temporary whilst we have algorithms that cannot support chunking */
3515 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003516 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003517 for( data_part_len = 1; data_part_len <= input_data->len;
3518 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003519 {
Paul Elliott329d5382021-07-22 17:10:45 +01003520 /* Split data into length(data_part_len) parts. */
3521 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003522
3523 if( !aead_multipart_internal_func( key_type_arg, key_data,
3524 alg_arg, nonce,
3525 additional_data, -1,
3526 input_data, data_part_len,
3527 do_set_lengths,
3528 expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +01003529 1, 1, 0 ) )
3530 break;
3531
3532 /* length(0) part, length(data_part_len) part, length(0) part... */
3533 mbedtls_test_set_step( 3000 + data_part_len );
3534
3535 if( !aead_multipart_internal_func( key_type_arg, key_data,
3536 alg_arg, nonce,
3537 additional_data, -1,
3538 input_data, data_part_len,
3539 do_set_lengths,
3540 expected_output,
3541 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003542 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003543 }
3544 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003545
Paul Elliott0023e0a2021-04-27 10:06:22 +01003546
Paul Elliott8fc45162021-06-23 16:06:01 +01003547 /* Goto is required to silence warnings about unused labels, as we
3548 * don't actually do any test assertions in this function. */
3549 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003550}
3551/* END_CASE */
3552
3553/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003554void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3555 int alg_arg,
3556 data_t *nonce,
3557 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003558 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003559 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003560 int do_test_data_chunked,
3561 int do_set_lengths,
3562 data_t *expected_output,
3563 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003564{
Paul Elliottd3f82412021-06-16 16:52:21 +01003565 size_t ad_part_len = 0;
3566 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003567
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003568 /* Temporary whilst we have algorithms that cannot support chunking */
3569 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003570 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003571 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3572 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003573 {
Paul Elliott329d5382021-07-22 17:10:45 +01003574 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003575 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003576
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003577 if( !aead_multipart_internal_func( key_type_arg, key_data,
3578 alg_arg, nonce,
3579 additional_data,
3580 ad_part_len,
3581 input_data, -1,
3582 do_set_lengths,
3583 expected_output,
3584 expect_valid_signature,
Paul Elliott329d5382021-07-22 17:10:45 +01003585 0, 0 ) )
3586 break;
3587
3588 /* length(0) part, length(ad_part_len) part, length(0) part... */
3589 mbedtls_test_set_step( 1000 + ad_part_len );
3590
3591 if( !aead_multipart_internal_func( key_type_arg, key_data,
3592 alg_arg, nonce,
3593 additional_data,
3594 ad_part_len,
3595 input_data, -1,
3596 do_set_lengths,
3597 expected_output,
3598 expect_valid_signature,
3599 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003600 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003601 }
3602 }
3603
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003604 /* Temporary whilst we have algorithms that cannot support chunking */
3605 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003606 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003607 for( data_part_len = 1; data_part_len <= input_data->len;
3608 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003609 {
Paul Elliott329d5382021-07-22 17:10:45 +01003610 /* Split data into length(data_part_len) parts. */
3611 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003612
3613 if( !aead_multipart_internal_func( key_type_arg, key_data,
3614 alg_arg, nonce,
3615 additional_data, -1,
3616 input_data, data_part_len,
3617 do_set_lengths,
3618 expected_output,
3619 expect_valid_signature,
Paul Elliott329d5382021-07-22 17:10:45 +01003620 0, 0 ) )
3621 break;
3622
3623 /* length(0) part, length(data_part_len) part, length(0) part... */
3624 mbedtls_test_set_step( 3000 + data_part_len );
3625
3626 if( !aead_multipart_internal_func( key_type_arg, key_data,
3627 alg_arg, nonce,
3628 additional_data, -1,
3629 input_data, data_part_len,
3630 do_set_lengths,
3631 expected_output,
3632 expect_valid_signature,
3633 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003634 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003635 }
3636 }
3637
Paul Elliott8fc45162021-06-23 16:06:01 +01003638 /* Goto is required to silence warnings about unused labels, as we
3639 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003640 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003641}
3642/* END_CASE */
3643
3644/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003645void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3646 int alg_arg,
3647 int nonce_len,
Paul Elliottd85f5472021-07-16 18:20:16 +01003648 int expected_generated_len_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003649 data_t *additional_data,
3650 data_t *input_data,
3651 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003652{
3653
3654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3655 psa_key_type_t key_type = key_type_arg;
3656 psa_algorithm_t alg = alg_arg;
3657 psa_aead_operation_t operation;
3658 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3660 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3661 size_t nonce_generated_len = 0;
Paul Elliottd85f5472021-07-16 18:20:16 +01003662 size_t expected_generated_len = expected_generated_len_arg;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003663 unsigned char *output_data = NULL;
3664 unsigned char *final_data = NULL;
3665 size_t output_size = 0;
3666 size_t finish_output_size = 0;
3667 size_t output_length = 0;
3668 size_t tag_length = 0;
3669 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003670
3671 PSA_ASSERT( psa_crypto_init( ) );
3672
3673 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3674 psa_set_key_algorithm( & attributes, alg );
3675 psa_set_key_type( & attributes, key_type );
3676
3677 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3678 &key ) );
3679
3680 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3681
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003682 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3683
3684 ASSERT_ALLOC( output_data, output_size );
3685
3686 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3687
3688 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3689
3690 ASSERT_ALLOC( final_data, finish_output_size );
3691
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003692 operation = psa_aead_operation_init( );
3693
3694 status = psa_aead_encrypt_setup( &operation, key, alg );
3695
3696 /* If the operation is not supported, just skip and not fail in case the
3697 * encryption involves a common limitation of cryptography hardwares and
3698 * an alternative implementation. */
3699 if( status == PSA_ERROR_NOT_SUPPORTED )
3700 {
3701 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3702 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len );
3703 }
3704
3705 PSA_ASSERT( status );
3706
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003707 status = psa_aead_generate_nonce( &operation, nonce_buffer,
3708 nonce_len,
3709 &nonce_generated_len );
3710
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003711 TEST_ASSERT( status == expected_status_arg );
3712
Paul Elliottd85f5472021-07-16 18:20:16 +01003713 TEST_EQUAL( nonce_generated_len, expected_generated_len );
3714
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003715 TEST_ASSERT( nonce_generated_len < PSA_AEAD_NONCE_MAX_SIZE );
3716
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003717 if( expected_status_arg == PSA_SUCCESS )
3718 {
3719
3720 /* Ensure we can still complete operation. */
3721
3722 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3723 additional_data->len ) );
3724
3725 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
3726 output_data, output_size, &output_length ) );
3727
3728 PSA_ASSERT( psa_aead_finish( &operation, final_data, finish_output_size,
3729 &output_length, tag_buffer,
3730 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3731 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003732
3733exit:
3734 psa_destroy_key( key );
Paul Elliott16906f92021-06-24 09:57:01 +01003735 mbedtls_free( output_data );
3736 mbedtls_free( final_data );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003737 psa_aead_abort( &operation );
3738 PSA_DONE( );
3739}
3740/* END_CASE */
3741
3742/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01003743void aead_multipart_state_test( int key_type_arg, data_t *key_data,
3744 int alg_arg,
3745 data_t *nonce,
3746 data_t *additional_data,
3747 data_t *input_data )
3748{
3749 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3750 psa_key_type_t key_type = key_type_arg;
3751 psa_algorithm_t alg = alg_arg;
3752 psa_aead_operation_t operation;
3753 unsigned char *output_data = NULL;
3754 unsigned char *final_data = NULL;
3755 size_t output_size = 0;
3756 size_t finish_output_size = 0;
3757 size_t output_length = 0;
3758 size_t key_bits = 0;
3759 size_t tag_length = 0;
3760 size_t tag_size = 0;
3761 size_t nonce_length = 0;
3762 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3763 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3764 size_t output_part_length = 0;
3765 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3766
3767 PSA_ASSERT( psa_crypto_init( ) );
3768
3769 psa_set_key_usage_flags( & attributes,
3770 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3771 psa_set_key_algorithm( & attributes, alg );
3772 psa_set_key_type( & attributes, key_type );
3773
3774 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3775 &key ) );
3776
3777 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3778 key_bits = psa_get_key_bits( &attributes );
3779
3780 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
3781
3782 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
3783
3784 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3785
3786 ASSERT_ALLOC( output_data, output_size );
3787
3788 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3789
3790 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3791
3792 ASSERT_ALLOC( final_data, finish_output_size );
3793
3794 /* Test all operations error without calling setup first. */
3795
3796 operation = psa_aead_operation_init( );
3797
3798 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3799 PSA_ERROR_BAD_STATE );
3800
3801 psa_aead_abort( &operation );
3802
3803 operation = psa_aead_operation_init( );
3804
3805 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3806 PSA_AEAD_NONCE_MAX_SIZE,
3807 &nonce_length ),
3808 PSA_ERROR_BAD_STATE );
3809
3810 psa_aead_abort( &operation );
3811
Paul Elliott481be342021-07-16 17:38:47 +01003812 /* ------------------------------------------------------- */
3813
Paul Elliottc23a9a02021-06-21 18:32:46 +01003814 operation = psa_aead_operation_init( );
3815
3816 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
3817 input_data->len ),
3818 PSA_ERROR_BAD_STATE );
3819
3820 psa_aead_abort( &operation );
3821
Paul Elliott481be342021-07-16 17:38:47 +01003822 /* ------------------------------------------------------- */
3823
Paul Elliottc23a9a02021-06-21 18:32:46 +01003824 operation = psa_aead_operation_init( );
3825
3826 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3827 additional_data->len ),
3828 PSA_ERROR_BAD_STATE );
3829
3830 psa_aead_abort( &operation );
3831
Paul Elliott481be342021-07-16 17:38:47 +01003832 /* ------------------------------------------------------- */
3833
Paul Elliottc23a9a02021-06-21 18:32:46 +01003834 operation = psa_aead_operation_init( );
3835
3836 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
3837 input_data->len, output_data,
3838 output_size, &output_length ),
3839 PSA_ERROR_BAD_STATE );
3840
3841 psa_aead_abort( &operation );
3842
Paul Elliott481be342021-07-16 17:38:47 +01003843 /* ------------------------------------------------------- */
3844
Paul Elliottc23a9a02021-06-21 18:32:46 +01003845 operation = psa_aead_operation_init( );
3846
3847 TEST_EQUAL( psa_aead_finish( &operation, final_data,
3848 finish_output_size,
3849 &output_part_length,
3850 tag_buffer, tag_length,
3851 &tag_size ),
3852 PSA_ERROR_BAD_STATE );
3853
3854 psa_aead_abort( &operation );
3855
Paul Elliott481be342021-07-16 17:38:47 +01003856 /* ------------------------------------------------------- */
3857
Paul Elliottc23a9a02021-06-21 18:32:46 +01003858 operation = psa_aead_operation_init( );
3859
3860 TEST_EQUAL( psa_aead_verify( &operation, final_data,
3861 finish_output_size,
3862 &output_part_length,
3863 tag_buffer,
3864 tag_length ),
3865 PSA_ERROR_BAD_STATE );
3866
3867 psa_aead_abort( &operation );
3868
3869 /* Test for double setups. */
3870
3871 operation = psa_aead_operation_init( );
3872
3873 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3874
3875 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
3876 PSA_ERROR_BAD_STATE );
3877
3878 psa_aead_abort( &operation );
3879
Paul Elliott481be342021-07-16 17:38:47 +01003880 /* ------------------------------------------------------- */
3881
Paul Elliottc23a9a02021-06-21 18:32:46 +01003882 operation = psa_aead_operation_init( );
3883
3884 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3885
3886 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
3887 PSA_ERROR_BAD_STATE );
3888
3889 psa_aead_abort( &operation );
3890
Paul Elliott374a2be2021-07-16 17:53:40 +01003891 /* ------------------------------------------------------- */
3892
3893 operation = psa_aead_operation_init( );
3894
3895 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3896
3897 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
3898 PSA_ERROR_BAD_STATE );
3899
3900 psa_aead_abort( &operation );
3901
3902 /* ------------------------------------------------------- */
3903
3904 operation = psa_aead_operation_init( );
3905
3906 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3907
3908 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
3909 PSA_ERROR_BAD_STATE );
3910
3911 psa_aead_abort( &operation );
3912
Paul Elliottc23a9a02021-06-21 18:32:46 +01003913 /* Test for not setting a nonce. */
3914
3915 operation = psa_aead_operation_init( );
3916
3917 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3918
3919 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
3920 additional_data->len ),
3921 PSA_ERROR_BAD_STATE );
3922
3923 psa_aead_abort( &operation );
3924
3925 /* Test for double setting nonce. */
3926
3927 operation = psa_aead_operation_init( );
3928
3929 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3930
3931 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3932
3933 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3934 PSA_ERROR_BAD_STATE );
3935
3936 psa_aead_abort( &operation );
3937
Paul Elliott374a2be2021-07-16 17:53:40 +01003938 /* Test for double generating nonce. */
3939
3940 operation = psa_aead_operation_init( );
3941
3942 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3943
3944 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3945 PSA_AEAD_NONCE_MAX_SIZE,
3946 &nonce_length ) );
3947
3948 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3949 PSA_AEAD_NONCE_MAX_SIZE,
3950 &nonce_length ),
3951 PSA_ERROR_BAD_STATE );
3952
3953
3954 psa_aead_abort( &operation );
3955
3956 /* Test for generate nonce then set and vice versa */
3957
3958 operation = psa_aead_operation_init( );
3959
3960 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3961
3962 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3963 PSA_AEAD_NONCE_MAX_SIZE,
3964 &nonce_length ) );
3965
3966 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
3967 PSA_ERROR_BAD_STATE );
3968
3969 psa_aead_abort( &operation );
3970
3971 /* ------------------------------------------------------- */
3972
3973 operation = psa_aead_operation_init( );
3974
3975 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
3976
3977 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3978
3979 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3980 PSA_AEAD_NONCE_MAX_SIZE,
3981 &nonce_length ),
3982 PSA_ERROR_BAD_STATE );
3983
3984 psa_aead_abort( &operation );
3985
Paul Elliott7220cae2021-06-22 17:25:57 +01003986 /* Test for generating nonce in decrypt setup. */
3987
3988 operation = psa_aead_operation_init( );
3989
3990 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
3991
3992 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
3993 PSA_AEAD_NONCE_MAX_SIZE,
3994 &nonce_length ),
3995 PSA_ERROR_BAD_STATE );
3996
3997 psa_aead_abort( &operation );
3998
Paul Elliottc23a9a02021-06-21 18:32:46 +01003999 /* Test for setting lengths twice. */
4000
4001 operation = psa_aead_operation_init( );
4002
4003 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4004
4005 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4006
4007 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4008 input_data->len ) );
4009
4010 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4011 input_data->len ),
4012 PSA_ERROR_BAD_STATE );
4013
4014 psa_aead_abort( &operation );
4015
Paul Elliott01876512021-06-23 18:13:04 +01004016 /* Test that generate/set nonce and set lengths are interchangeable (we
4017 * already tested set nonce followed by set lengths above). */
4018
4019 operation = psa_aead_operation_init( );
4020
4021 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4022
4023 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4024 input_data->len ) );
4025
4026 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4027
4028 psa_aead_abort( &operation );
4029
Paul Elliott481be342021-07-16 17:38:47 +01004030 /* ------------------------------------------------------- */
4031
Paul Elliott01876512021-06-23 18:13:04 +01004032 operation = psa_aead_operation_init( );
4033
4034 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4035
4036 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4037 input_data->len ) );
4038
4039 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4040 PSA_AEAD_NONCE_MAX_SIZE,
4041 &nonce_length ) );
4042
4043 psa_aead_abort( &operation );
4044
Paul Elliott481be342021-07-16 17:38:47 +01004045 /* ------------------------------------------------------- */
4046
Paul Elliott01876512021-06-23 18:13:04 +01004047 operation = psa_aead_operation_init( );
4048
4049 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4050
4051 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4052 PSA_AEAD_NONCE_MAX_SIZE,
4053 &nonce_length ) );
4054
4055 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4056 input_data->len ) );
4057
4058 psa_aead_abort( &operation );
4059
Paul Elliottc23a9a02021-06-21 18:32:46 +01004060 /* Test for setting lengths after already starting data. */
4061
4062 operation = psa_aead_operation_init( );
4063
4064 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4065
4066 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4067
4068 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4069 input_data->len, output_data,
4070 output_size, &output_length ) );
4071
4072 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4073 input_data->len ),
4074 PSA_ERROR_BAD_STATE );
4075
4076 psa_aead_abort( &operation );
4077
Paul Elliott243080c2021-07-21 19:01:17 +01004078 /* Test for not sending any additional data or data after setting non zero
4079 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004080
4081 operation = psa_aead_operation_init( );
4082
4083 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4084
4085 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4086
4087 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4088 input_data->len ) );
4089
4090 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4091 finish_output_size,
4092 &output_part_length,
4093 tag_buffer, tag_length,
4094 &tag_size ),
4095 PSA_ERROR_INVALID_ARGUMENT );
4096
4097 psa_aead_abort( &operation );
4098
Paul Elliott243080c2021-07-21 19:01:17 +01004099 /* Test for not sending any additional data or data after setting non-zero
4100 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004101
4102 operation = psa_aead_operation_init( );
4103
4104 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4105
4106 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4107
4108 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4109 input_data->len ) );
4110
4111 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4112 finish_output_size,
4113 &output_part_length,
4114 tag_buffer,
4115 tag_length ),
4116 PSA_ERROR_INVALID_ARGUMENT );
4117
4118 psa_aead_abort( &operation );
4119
Paul Elliott243080c2021-07-21 19:01:17 +01004120 /* Test for not sending any additional data after setting a non-zero length
4121 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004122
4123 operation = psa_aead_operation_init( );
4124
4125 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4126
4127 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4128
4129 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4130 input_data->len ) );
4131
4132 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4133 input_data->len, output_data,
4134 output_size, &output_length ),
4135 PSA_ERROR_INVALID_ARGUMENT );
4136
4137 psa_aead_abort( &operation );
4138
4139 /* Test sending additional data after data. */
4140
4141 operation = psa_aead_operation_init( );
4142
4143 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4144
4145 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4146
4147 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4148 input_data->len, output_data,
4149 output_size, &output_length ) );
4150
4151 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4152 additional_data->len ),
4153 PSA_ERROR_BAD_STATE );
4154
4155 psa_aead_abort( &operation );
4156
Paul Elliott534d0b42021-06-22 19:15:20 +01004157 /* Test calling finish on decryption. */
4158
4159 operation = psa_aead_operation_init( );
4160
4161 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4162
4163 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4164
4165 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4166 finish_output_size,
4167 &output_part_length,
4168 tag_buffer, tag_length,
4169 &tag_size ),
4170 PSA_ERROR_BAD_STATE );
4171
4172 psa_aead_abort( &operation );
4173
4174 /* Test calling verify on encryption. */
4175
4176 operation = psa_aead_operation_init( );
4177
4178 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4179
4180 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4181
4182 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4183 finish_output_size,
4184 &output_part_length,
4185 tag_buffer,
4186 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004187 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004188
4189 psa_aead_abort( &operation );
4190
4191
Paul Elliottc23a9a02021-06-21 18:32:46 +01004192exit:
4193 psa_destroy_key( key );
4194 psa_aead_abort( &operation );
4195 mbedtls_free( output_data );
4196 mbedtls_free( final_data );
4197 PSA_DONE( );
4198}
4199/* END_CASE */
4200
4201/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004202void signature_size( int type_arg,
4203 int bits,
4204 int alg_arg,
4205 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004206{
4207 psa_key_type_t type = type_arg;
4208 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004209 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004210
Gilles Peskinefe11b722018-12-18 00:24:04 +01004211 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004212
Gilles Peskinee59236f2018-01-27 23:32:46 +01004213exit:
4214 ;
4215}
4216/* END_CASE */
4217
4218/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004219void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4220 int alg_arg, data_t *input_data,
4221 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004222{
Ronald Cron5425a212020-08-04 14:58:35 +02004223 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004224 psa_key_type_t key_type = key_type_arg;
4225 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004226 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004227 unsigned char *signature = NULL;
4228 size_t signature_size;
4229 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004231
Gilles Peskine8817f612018-12-18 00:18:46 +01004232 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004233
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004234 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004235 psa_set_key_algorithm( &attributes, alg );
4236 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004237
Gilles Peskine049c7532019-05-15 20:22:09 +02004238 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004239 &key ) );
4240 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004241 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004242
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004243 /* Allocate a buffer which has the size advertized by the
4244 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004245 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004246 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004247 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004248 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004249 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004250
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004251 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004252 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004253 input_data->x, input_data->len,
4254 signature, signature_size,
4255 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004256 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004257 ASSERT_COMPARE( output_data->x, output_data->len,
4258 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004259
4260exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004261 /*
4262 * Key attributes may have been returned by psa_get_key_attributes()
4263 * thus reset them as required.
4264 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004265 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004266
Ronald Cron5425a212020-08-04 14:58:35 +02004267 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004268 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004269 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004270}
4271/* END_CASE */
4272
4273/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004274void sign_hash_fail( int key_type_arg, data_t *key_data,
4275 int alg_arg, data_t *input_data,
4276 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004277{
Ronald Cron5425a212020-08-04 14:58:35 +02004278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004279 psa_key_type_t key_type = key_type_arg;
4280 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004281 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004282 psa_status_t actual_status;
4283 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004284 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004285 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004287
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004288 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004289
Gilles Peskine8817f612018-12-18 00:18:46 +01004290 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004291
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004292 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004293 psa_set_key_algorithm( &attributes, alg );
4294 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004295
Gilles Peskine049c7532019-05-15 20:22:09 +02004296 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004297 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004298
Ronald Cron5425a212020-08-04 14:58:35 +02004299 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004300 input_data->x, input_data->len,
4301 signature, signature_size,
4302 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004303 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004304 /* The value of *signature_length is unspecified on error, but
4305 * whatever it is, it should be less than signature_size, so that
4306 * if the caller tries to read *signature_length bytes without
4307 * checking the error code then they don't overflow a buffer. */
4308 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004309
4310exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004311 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004312 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004313 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004314 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004315}
4316/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004317
4318/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004319void sign_verify_hash( int key_type_arg, data_t *key_data,
4320 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004321{
Ronald Cron5425a212020-08-04 14:58:35 +02004322 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004323 psa_key_type_t key_type = key_type_arg;
4324 psa_algorithm_t alg = alg_arg;
4325 size_t key_bits;
4326 unsigned char *signature = NULL;
4327 size_t signature_size;
4328 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004330
Gilles Peskine8817f612018-12-18 00:18:46 +01004331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004332
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004333 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004334 psa_set_key_algorithm( &attributes, alg );
4335 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004336
Gilles Peskine049c7532019-05-15 20:22:09 +02004337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004338 &key ) );
4339 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004340 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004341
4342 /* Allocate a buffer which has the size advertized by the
4343 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004344 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004345 key_bits, alg );
4346 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004347 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004348 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004349
4350 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004351 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004352 input_data->x, input_data->len,
4353 signature, signature_size,
4354 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004355 /* Check that the signature length looks sensible. */
4356 TEST_ASSERT( signature_length <= signature_size );
4357 TEST_ASSERT( signature_length > 0 );
4358
4359 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004360 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004361 input_data->x, input_data->len,
4362 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004363
4364 if( input_data->len != 0 )
4365 {
4366 /* Flip a bit in the input and verify that the signature is now
4367 * detected as invalid. Flip a bit at the beginning, not at the end,
4368 * because ECDSA may ignore the last few bits of the input. */
4369 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004370 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004371 input_data->x, input_data->len,
4372 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004373 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004374 }
4375
4376exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004377 /*
4378 * Key attributes may have been returned by psa_get_key_attributes()
4379 * thus reset them as required.
4380 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004381 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004382
Ronald Cron5425a212020-08-04 14:58:35 +02004383 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004384 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004385 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004386}
4387/* END_CASE */
4388
4389/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004390void verify_hash( int key_type_arg, data_t *key_data,
4391 int alg_arg, data_t *hash_data,
4392 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004393{
Ronald Cron5425a212020-08-04 14:58:35 +02004394 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004395 psa_key_type_t key_type = key_type_arg;
4396 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004398
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004399 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004400
Gilles Peskine8817f612018-12-18 00:18:46 +01004401 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004402
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004403 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004404 psa_set_key_algorithm( &attributes, alg );
4405 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004406
Gilles Peskine049c7532019-05-15 20:22:09 +02004407 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004408 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004409
Ronald Cron5425a212020-08-04 14:58:35 +02004410 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004411 hash_data->x, hash_data->len,
4412 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004413
itayzafrir5c753392018-05-08 11:18:38 +03004414exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004415 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004416 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004417 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004418}
4419/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004420
4421/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004422void verify_hash_fail( int key_type_arg, data_t *key_data,
4423 int alg_arg, data_t *hash_data,
4424 data_t *signature_data,
4425 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004426{
Ronald Cron5425a212020-08-04 14:58:35 +02004427 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004428 psa_key_type_t key_type = key_type_arg;
4429 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004430 psa_status_t actual_status;
4431 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004432 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004433
Gilles Peskine8817f612018-12-18 00:18:46 +01004434 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004435
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004436 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004437 psa_set_key_algorithm( &attributes, alg );
4438 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004439
Gilles Peskine049c7532019-05-15 20:22:09 +02004440 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004441 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004442
Ronald Cron5425a212020-08-04 14:58:35 +02004443 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004444 hash_data->x, hash_data->len,
4445 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004446 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004447
4448exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004449 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004450 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004451 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004452}
4453/* END_CASE */
4454
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004455/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004456void sign_message_deterministic( int key_type_arg,
4457 data_t *key_data,
4458 int alg_arg,
4459 data_t *input_data,
4460 data_t *output_data )
4461{
4462 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4463 psa_key_type_t key_type = key_type_arg;
4464 psa_algorithm_t alg = alg_arg;
4465 size_t key_bits;
4466 unsigned char *signature = NULL;
4467 size_t signature_size;
4468 size_t signature_length = 0xdeadbeef;
4469 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4470
4471 PSA_ASSERT( psa_crypto_init( ) );
4472
4473 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4474 psa_set_key_algorithm( &attributes, alg );
4475 psa_set_key_type( &attributes, key_type );
4476
4477 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4478 &key ) );
4479 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4480 key_bits = psa_get_key_bits( &attributes );
4481
4482 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4483 TEST_ASSERT( signature_size != 0 );
4484 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4485 ASSERT_ALLOC( signature, signature_size );
4486
4487 PSA_ASSERT( psa_sign_message( key, alg,
4488 input_data->x, input_data->len,
4489 signature, signature_size,
4490 &signature_length ) );
4491
4492 ASSERT_COMPARE( output_data->x, output_data->len,
4493 signature, signature_length );
4494
4495exit:
4496 psa_reset_key_attributes( &attributes );
4497
4498 psa_destroy_key( key );
4499 mbedtls_free( signature );
4500 PSA_DONE( );
4501
4502}
4503/* END_CASE */
4504
4505/* BEGIN_CASE */
4506void sign_message_fail( int key_type_arg,
4507 data_t *key_data,
4508 int alg_arg,
4509 data_t *input_data,
4510 int signature_size_arg,
4511 int expected_status_arg )
4512{
4513 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4514 psa_key_type_t key_type = key_type_arg;
4515 psa_algorithm_t alg = alg_arg;
4516 size_t signature_size = signature_size_arg;
4517 psa_status_t actual_status;
4518 psa_status_t expected_status = expected_status_arg;
4519 unsigned char *signature = NULL;
4520 size_t signature_length = 0xdeadbeef;
4521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4522
4523 ASSERT_ALLOC( signature, signature_size );
4524
4525 PSA_ASSERT( psa_crypto_init( ) );
4526
4527 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4528 psa_set_key_algorithm( &attributes, alg );
4529 psa_set_key_type( &attributes, key_type );
4530
4531 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4532 &key ) );
4533
4534 actual_status = psa_sign_message( key, alg,
4535 input_data->x, input_data->len,
4536 signature, signature_size,
4537 &signature_length );
4538 TEST_EQUAL( actual_status, expected_status );
4539 /* The value of *signature_length is unspecified on error, but
4540 * whatever it is, it should be less than signature_size, so that
4541 * if the caller tries to read *signature_length bytes without
4542 * checking the error code then they don't overflow a buffer. */
4543 TEST_ASSERT( signature_length <= signature_size );
4544
4545exit:
4546 psa_reset_key_attributes( &attributes );
4547 psa_destroy_key( key );
4548 mbedtls_free( signature );
4549 PSA_DONE( );
4550}
4551/* END_CASE */
4552
4553/* BEGIN_CASE */
4554void sign_verify_message( int key_type_arg,
4555 data_t *key_data,
4556 int alg_arg,
4557 data_t *input_data )
4558{
4559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4560 psa_key_type_t key_type = key_type_arg;
4561 psa_algorithm_t alg = alg_arg;
4562 size_t key_bits;
4563 unsigned char *signature = NULL;
4564 size_t signature_size;
4565 size_t signature_length = 0xdeadbeef;
4566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4567
4568 PSA_ASSERT( psa_crypto_init( ) );
4569
4570 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4571 PSA_KEY_USAGE_VERIFY_MESSAGE );
4572 psa_set_key_algorithm( &attributes, alg );
4573 psa_set_key_type( &attributes, key_type );
4574
4575 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4576 &key ) );
4577 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4578 key_bits = psa_get_key_bits( &attributes );
4579
4580 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4581 TEST_ASSERT( signature_size != 0 );
4582 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4583 ASSERT_ALLOC( signature, signature_size );
4584
4585 PSA_ASSERT( psa_sign_message( key, alg,
4586 input_data->x, input_data->len,
4587 signature, signature_size,
4588 &signature_length ) );
4589 TEST_ASSERT( signature_length <= signature_size );
4590 TEST_ASSERT( signature_length > 0 );
4591
4592 PSA_ASSERT( psa_verify_message( key, alg,
4593 input_data->x, input_data->len,
4594 signature, signature_length ) );
4595
4596 if( input_data->len != 0 )
4597 {
4598 /* Flip a bit in the input and verify that the signature is now
4599 * detected as invalid. Flip a bit at the beginning, not at the end,
4600 * because ECDSA may ignore the last few bits of the input. */
4601 input_data->x[0] ^= 1;
4602 TEST_EQUAL( psa_verify_message( key, alg,
4603 input_data->x, input_data->len,
4604 signature, signature_length ),
4605 PSA_ERROR_INVALID_SIGNATURE );
4606 }
4607
4608exit:
4609 psa_reset_key_attributes( &attributes );
4610
4611 psa_destroy_key( key );
4612 mbedtls_free( signature );
4613 PSA_DONE( );
4614}
4615/* END_CASE */
4616
4617/* BEGIN_CASE */
4618void verify_message( int key_type_arg,
4619 data_t *key_data,
4620 int alg_arg,
4621 data_t *input_data,
4622 data_t *signature_data )
4623{
4624 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4625 psa_key_type_t key_type = key_type_arg;
4626 psa_algorithm_t alg = alg_arg;
4627 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4628
4629 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4630
4631 PSA_ASSERT( psa_crypto_init( ) );
4632
4633 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4634 psa_set_key_algorithm( &attributes, alg );
4635 psa_set_key_type( &attributes, key_type );
4636
4637 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4638 &key ) );
4639
4640 PSA_ASSERT( psa_verify_message( key, alg,
4641 input_data->x, input_data->len,
4642 signature_data->x, signature_data->len ) );
4643
4644exit:
4645 psa_reset_key_attributes( &attributes );
4646 psa_destroy_key( key );
4647 PSA_DONE( );
4648}
4649/* END_CASE */
4650
4651/* BEGIN_CASE */
4652void verify_message_fail( int key_type_arg,
4653 data_t *key_data,
4654 int alg_arg,
4655 data_t *hash_data,
4656 data_t *signature_data,
4657 int expected_status_arg )
4658{
4659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4660 psa_key_type_t key_type = key_type_arg;
4661 psa_algorithm_t alg = alg_arg;
4662 psa_status_t actual_status;
4663 psa_status_t expected_status = expected_status_arg;
4664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4665
4666 PSA_ASSERT( psa_crypto_init( ) );
4667
4668 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4669 psa_set_key_algorithm( &attributes, alg );
4670 psa_set_key_type( &attributes, key_type );
4671
4672 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4673 &key ) );
4674
4675 actual_status = psa_verify_message( key, alg,
4676 hash_data->x, hash_data->len,
4677 signature_data->x,
4678 signature_data->len );
4679 TEST_EQUAL( actual_status, expected_status );
4680
4681exit:
4682 psa_reset_key_attributes( &attributes );
4683 psa_destroy_key( key );
4684 PSA_DONE( );
4685}
4686/* END_CASE */
4687
4688/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004689void asymmetric_encrypt( int key_type_arg,
4690 data_t *key_data,
4691 int alg_arg,
4692 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004693 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004694 int expected_output_length_arg,
4695 int expected_status_arg )
4696{
Ronald Cron5425a212020-08-04 14:58:35 +02004697 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004698 psa_key_type_t key_type = key_type_arg;
4699 psa_algorithm_t alg = alg_arg;
4700 size_t expected_output_length = expected_output_length_arg;
4701 size_t key_bits;
4702 unsigned char *output = NULL;
4703 size_t output_size;
4704 size_t output_length = ~0;
4705 psa_status_t actual_status;
4706 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004708
Gilles Peskine8817f612018-12-18 00:18:46 +01004709 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004710
Gilles Peskine656896e2018-06-29 19:12:28 +02004711 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004712 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4713 psa_set_key_algorithm( &attributes, alg );
4714 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004715 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004716 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004717
4718 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004719 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004720 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004721
Gilles Peskine656896e2018-06-29 19:12:28 +02004722 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004723 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004724 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004725
4726 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004727 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004728 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004729 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004730 output, output_size,
4731 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004732 TEST_EQUAL( actual_status, expected_status );
4733 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004734
Gilles Peskine68428122018-06-30 18:42:41 +02004735 /* If the label is empty, the test framework puts a non-null pointer
4736 * in label->x. Test that a null pointer works as well. */
4737 if( label->len == 0 )
4738 {
4739 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004740 if( output_size != 0 )
4741 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004742 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004743 input_data->x, input_data->len,
4744 NULL, label->len,
4745 output, output_size,
4746 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004747 TEST_EQUAL( actual_status, expected_status );
4748 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004749 }
4750
Gilles Peskine656896e2018-06-29 19:12:28 +02004751exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004752 /*
4753 * Key attributes may have been returned by psa_get_key_attributes()
4754 * thus reset them as required.
4755 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004756 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004757
Ronald Cron5425a212020-08-04 14:58:35 +02004758 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004759 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004760 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004761}
4762/* END_CASE */
4763
4764/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004765void asymmetric_encrypt_decrypt( int key_type_arg,
4766 data_t *key_data,
4767 int alg_arg,
4768 data_t *input_data,
4769 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004770{
Ronald Cron5425a212020-08-04 14:58:35 +02004771 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004772 psa_key_type_t key_type = key_type_arg;
4773 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004774 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004775 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004776 size_t output_size;
4777 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004778 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004779 size_t output2_size;
4780 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004781 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004782
Gilles Peskine8817f612018-12-18 00:18:46 +01004783 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004784
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004785 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4786 psa_set_key_algorithm( &attributes, alg );
4787 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004788
Gilles Peskine049c7532019-05-15 20:22:09 +02004789 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004790 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004791
4792 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004793 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004794 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004795
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004796 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004797 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004798 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004799
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004800 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004801 TEST_ASSERT( output2_size <=
4802 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4803 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004804 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004805
Gilles Peskineeebd7382018-06-08 18:11:54 +02004806 /* We test encryption by checking that encrypt-then-decrypt gives back
4807 * the original plaintext because of the non-optional random
4808 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004809 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004810 input_data->x, input_data->len,
4811 label->x, label->len,
4812 output, output_size,
4813 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004814 /* We don't know what ciphertext length to expect, but check that
4815 * it looks sensible. */
4816 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004817
Ronald Cron5425a212020-08-04 14:58:35 +02004818 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004819 output, output_length,
4820 label->x, label->len,
4821 output2, output2_size,
4822 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004823 ASSERT_COMPARE( input_data->x, input_data->len,
4824 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004825
4826exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004827 /*
4828 * Key attributes may have been returned by psa_get_key_attributes()
4829 * thus reset them as required.
4830 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004831 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004832
Ronald Cron5425a212020-08-04 14:58:35 +02004833 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004834 mbedtls_free( output );
4835 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004836 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004837}
4838/* END_CASE */
4839
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004840/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004841void asymmetric_decrypt( int key_type_arg,
4842 data_t *key_data,
4843 int alg_arg,
4844 data_t *input_data,
4845 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004846 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004847{
Ronald Cron5425a212020-08-04 14:58:35 +02004848 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004849 psa_key_type_t key_type = key_type_arg;
4850 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004851 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004852 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004853 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004854 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004855 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004856
Gilles Peskine8817f612018-12-18 00:18:46 +01004857 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004858
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004859 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4860 psa_set_key_algorithm( &attributes, alg );
4861 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004862
Gilles Peskine049c7532019-05-15 20:22:09 +02004863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004864 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004865
gabor-mezei-armceface22021-01-21 12:26:17 +01004866 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4867 key_bits = psa_get_key_bits( &attributes );
4868
4869 /* Determine the maximum ciphertext length */
4870 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4871 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4872 ASSERT_ALLOC( output, output_size );
4873
Ronald Cron5425a212020-08-04 14:58:35 +02004874 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004875 input_data->x, input_data->len,
4876 label->x, label->len,
4877 output,
4878 output_size,
4879 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004880 ASSERT_COMPARE( expected_data->x, expected_data->len,
4881 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004882
Gilles Peskine68428122018-06-30 18:42:41 +02004883 /* If the label is empty, the test framework puts a non-null pointer
4884 * in label->x. Test that a null pointer works as well. */
4885 if( label->len == 0 )
4886 {
4887 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004888 if( output_size != 0 )
4889 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004890 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004891 input_data->x, input_data->len,
4892 NULL, label->len,
4893 output,
4894 output_size,
4895 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004896 ASSERT_COMPARE( expected_data->x, expected_data->len,
4897 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004898 }
4899
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004900exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004901 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004902 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004903 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004904 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004905}
4906/* END_CASE */
4907
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004908/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004909void asymmetric_decrypt_fail( int key_type_arg,
4910 data_t *key_data,
4911 int alg_arg,
4912 data_t *input_data,
4913 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004914 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004915 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004916{
Ronald Cron5425a212020-08-04 14:58:35 +02004917 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004918 psa_key_type_t key_type = key_type_arg;
4919 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004920 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004921 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004922 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004923 psa_status_t actual_status;
4924 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004925 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004926
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004927 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004928
Gilles Peskine8817f612018-12-18 00:18:46 +01004929 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004930
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004931 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4932 psa_set_key_algorithm( &attributes, alg );
4933 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004934
Gilles Peskine049c7532019-05-15 20:22:09 +02004935 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004936 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004937
Ronald Cron5425a212020-08-04 14:58:35 +02004938 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004939 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004940 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004941 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004942 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004943 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004944 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004945
Gilles Peskine68428122018-06-30 18:42:41 +02004946 /* If the label is empty, the test framework puts a non-null pointer
4947 * in label->x. Test that a null pointer works as well. */
4948 if( label->len == 0 )
4949 {
4950 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004951 if( output_size != 0 )
4952 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004953 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004954 input_data->x, input_data->len,
4955 NULL, label->len,
4956 output, output_size,
4957 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004958 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004959 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004960 }
4961
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004962exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004963 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004964 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004965 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004966 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004967}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004968/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004969
4970/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004971void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004972{
4973 /* Test each valid way of initializing the object, except for `= {0}`, as
4974 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4975 * though it's OK by the C standard. We could test for this, but we'd need
4976 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004977 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004978 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4979 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4980 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004981
4982 memset( &zero, 0, sizeof( zero ) );
4983
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004985 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004986 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004987 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004988 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004989 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004990 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004991
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004992 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004993 PSA_ASSERT( psa_key_derivation_abort(&func) );
4994 PSA_ASSERT( psa_key_derivation_abort(&init) );
4995 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004996}
4997/* END_CASE */
4998
Janos Follath16de4a42019-06-13 16:32:24 +01004999/* BEGIN_CASE */
5000void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005001{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005002 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005003 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005004 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005005
Gilles Peskine8817f612018-12-18 00:18:46 +01005006 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005007
Janos Follath16de4a42019-06-13 16:32:24 +01005008 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005009 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005010
5011exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005012 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005013 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005014}
5015/* END_CASE */
5016
Janos Follathaf3c2a02019-06-12 12:34:34 +01005017/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005018void derive_set_capacity( int alg_arg, int capacity_arg,
5019 int expected_status_arg )
5020{
5021 psa_algorithm_t alg = alg_arg;
5022 size_t capacity = capacity_arg;
5023 psa_status_t expected_status = expected_status_arg;
5024 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5025
5026 PSA_ASSERT( psa_crypto_init( ) );
5027
5028 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5029
5030 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5031 expected_status );
5032
5033exit:
5034 psa_key_derivation_abort( &operation );
5035 PSA_DONE( );
5036}
5037/* END_CASE */
5038
5039/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005040void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005041 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005042 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005043 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005044 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005045 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005046 int expected_status_arg3,
5047 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005048{
5049 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005050 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5051 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005052 psa_status_t expected_statuses[] = {expected_status_arg1,
5053 expected_status_arg2,
5054 expected_status_arg3};
5055 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005056 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5057 MBEDTLS_SVC_KEY_ID_INIT,
5058 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005059 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5061 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005062 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005063 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005064 psa_status_t expected_output_status = expected_output_status_arg;
5065 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005066
5067 PSA_ASSERT( psa_crypto_init( ) );
5068
5069 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5070 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005071
5072 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5073
5074 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5075 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005076 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005077 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005078 psa_set_key_type( &attributes, key_types[i] );
5079 PSA_ASSERT( psa_import_key( &attributes,
5080 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005081 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005082 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5083 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5084 {
5085 // When taking a private key as secret input, use key agreement
5086 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005087 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5088 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005089 expected_statuses[i] );
5090 }
5091 else
5092 {
5093 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005094 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005095 expected_statuses[i] );
5096 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005097 }
5098 else
5099 {
5100 TEST_EQUAL( psa_key_derivation_input_bytes(
5101 &operation, steps[i],
5102 inputs[i]->x, inputs[i]->len ),
5103 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005104 }
5105 }
5106
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005107 if( output_key_type != PSA_KEY_TYPE_NONE )
5108 {
5109 psa_reset_key_attributes( &attributes );
5110 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5111 psa_set_key_bits( &attributes, 8 );
5112 actual_output_status =
5113 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005114 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005115 }
5116 else
5117 {
5118 uint8_t buffer[1];
5119 actual_output_status =
5120 psa_key_derivation_output_bytes( &operation,
5121 buffer, sizeof( buffer ) );
5122 }
5123 TEST_EQUAL( actual_output_status, expected_output_status );
5124
Janos Follathaf3c2a02019-06-12 12:34:34 +01005125exit:
5126 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005127 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5128 psa_destroy_key( keys[i] );
5129 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005130 PSA_DONE( );
5131}
5132/* END_CASE */
5133
Janos Follathd958bb72019-07-03 15:02:16 +01005134/* BEGIN_CASE */
5135void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005136{
Janos Follathd958bb72019-07-03 15:02:16 +01005137 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005139 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005141 unsigned char input1[] = "Input 1";
5142 size_t input1_length = sizeof( input1 );
5143 unsigned char input2[] = "Input 2";
5144 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005145 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005146 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005147 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5148 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5149 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005151
Gilles Peskine8817f612018-12-18 00:18:46 +01005152 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005153
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005154 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5155 psa_set_key_algorithm( &attributes, alg );
5156 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005157
Gilles Peskine73676cb2019-05-15 20:15:10 +02005158 PSA_ASSERT( psa_import_key( &attributes,
5159 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005160 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005161
5162 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005163 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5164 input1, input1_length,
5165 input2, input2_length,
5166 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005167 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005168
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005169 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005170 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005171 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005172
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005173 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005174
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005175 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005176 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005177
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005178exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005179 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005180 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005181 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005182}
5183/* END_CASE */
5184
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005185/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005186void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005187{
5188 uint8_t output_buffer[16];
5189 size_t buffer_size = 16;
5190 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005191 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005192
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005193 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5194 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005195 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005196
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005198 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005199
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005200 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005201
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005202 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5203 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005204 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005205
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005207 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005208
5209exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005211}
5212/* END_CASE */
5213
5214/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005215void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005216 int step1_arg, data_t *input1,
5217 int step2_arg, data_t *input2,
5218 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005219 int requested_capacity_arg,
5220 data_t *expected_output1,
5221 data_t *expected_output2 )
5222{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005223 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005224 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5225 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005226 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5227 MBEDTLS_SVC_KEY_ID_INIT,
5228 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005229 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005231 uint8_t *expected_outputs[2] =
5232 {expected_output1->x, expected_output2->x};
5233 size_t output_sizes[2] =
5234 {expected_output1->len, expected_output2->len};
5235 size_t output_buffer_size = 0;
5236 uint8_t *output_buffer = NULL;
5237 size_t expected_capacity;
5238 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005240 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005241 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005242
5243 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5244 {
5245 if( output_sizes[i] > output_buffer_size )
5246 output_buffer_size = output_sizes[i];
5247 if( output_sizes[i] == 0 )
5248 expected_outputs[i] = NULL;
5249 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005250 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005251 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005252
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005253 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5254 psa_set_key_algorithm( &attributes, alg );
5255 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005256
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005257 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005258 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5259 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5260 requested_capacity ) );
5261 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005262 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005263 switch( steps[i] )
5264 {
5265 case 0:
5266 break;
5267 case PSA_KEY_DERIVATION_INPUT_SECRET:
5268 PSA_ASSERT( psa_import_key( &attributes,
5269 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005270 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005271
5272 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5273 {
5274 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5275 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5276 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5277 }
5278
Gilles Peskine1468da72019-05-29 17:35:49 +02005279 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005280 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005281 break;
5282 default:
5283 PSA_ASSERT( psa_key_derivation_input_bytes(
5284 &operation, steps[i],
5285 inputs[i]->x, inputs[i]->len ) );
5286 break;
5287 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005288 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005289
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005291 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005292 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005293 expected_capacity = requested_capacity;
5294
5295 /* Expansion phase. */
5296 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5297 {
5298 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005299 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005300 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005301 if( expected_capacity == 0 && output_sizes[i] == 0 )
5302 {
5303 /* Reading 0 bytes when 0 bytes are available can go either way. */
5304 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005305 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005306 continue;
5307 }
5308 else if( expected_capacity == 0 ||
5309 output_sizes[i] > expected_capacity )
5310 {
5311 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005312 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005313 expected_capacity = 0;
5314 continue;
5315 }
5316 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005317 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005318 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005319 ASSERT_COMPARE( output_buffer, output_sizes[i],
5320 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005321 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005322 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005323 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005324 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005325 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005326 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005327 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005328
5329exit:
5330 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005332 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5333 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005334 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005335}
5336/* END_CASE */
5337
5338/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005339void derive_full( int alg_arg,
5340 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005341 data_t *input1,
5342 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005343 int requested_capacity_arg )
5344{
Ronald Cron5425a212020-08-04 14:58:35 +02005345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005346 psa_algorithm_t alg = alg_arg;
5347 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005349 unsigned char output_buffer[16];
5350 size_t expected_capacity = requested_capacity;
5351 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005353
Gilles Peskine8817f612018-12-18 00:18:46 +01005354 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005355
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005356 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5357 psa_set_key_algorithm( &attributes, alg );
5358 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005359
Gilles Peskine049c7532019-05-15 20:22:09 +02005360 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005361 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005362
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005363 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5364 input1->x, input1->len,
5365 input2->x, input2->len,
5366 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005367 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005368
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005369 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005370 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005371 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005372
5373 /* Expansion phase. */
5374 while( current_capacity > 0 )
5375 {
5376 size_t read_size = sizeof( output_buffer );
5377 if( read_size > current_capacity )
5378 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005379 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005380 output_buffer,
5381 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005382 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005383 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005384 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005385 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005386 }
5387
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005388 /* Check that the operation refuses to go over capacity. */
5389 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005390 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005391
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005392 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005393
5394exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005395 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005396 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005397 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005398}
5399/* END_CASE */
5400
Janos Follathe60c9052019-07-03 13:51:30 +01005401/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005402void derive_key_exercise( int alg_arg,
5403 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005404 data_t *input1,
5405 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005406 int derived_type_arg,
5407 int derived_bits_arg,
5408 int derived_usage_arg,
5409 int derived_alg_arg )
5410{
Ronald Cron5425a212020-08-04 14:58:35 +02005411 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5412 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005413 psa_algorithm_t alg = alg_arg;
5414 psa_key_type_t derived_type = derived_type_arg;
5415 size_t derived_bits = derived_bits_arg;
5416 psa_key_usage_t derived_usage = derived_usage_arg;
5417 psa_algorithm_t derived_alg = derived_alg_arg;
5418 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005419 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005421 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005422
Gilles Peskine8817f612018-12-18 00:18:46 +01005423 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005424
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005425 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5426 psa_set_key_algorithm( &attributes, alg );
5427 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005428 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005429 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005430
5431 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005432 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5433 input1->x, input1->len,
5434 input2->x, input2->len,
5435 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005436 goto exit;
5437
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005438 psa_set_key_usage_flags( &attributes, derived_usage );
5439 psa_set_key_algorithm( &attributes, derived_alg );
5440 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005441 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005443 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005444
5445 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005446 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005447 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5448 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005449
5450 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005451 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005452 goto exit;
5453
5454exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005455 /*
5456 * Key attributes may have been returned by psa_get_key_attributes()
5457 * thus reset them as required.
5458 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005459 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005460
5461 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005462 psa_destroy_key( base_key );
5463 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005464 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005465}
5466/* END_CASE */
5467
Janos Follath42fd8882019-07-03 14:17:09 +01005468/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005469void derive_key_export( int alg_arg,
5470 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005471 data_t *input1,
5472 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005473 int bytes1_arg,
5474 int bytes2_arg )
5475{
Ronald Cron5425a212020-08-04 14:58:35 +02005476 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5477 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005478 psa_algorithm_t alg = alg_arg;
5479 size_t bytes1 = bytes1_arg;
5480 size_t bytes2 = bytes2_arg;
5481 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005482 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005483 uint8_t *output_buffer = NULL;
5484 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005485 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5486 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005487 size_t length;
5488
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005489 ASSERT_ALLOC( output_buffer, capacity );
5490 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005491 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005492
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005493 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5494 psa_set_key_algorithm( &base_attributes, alg );
5495 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005496 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005497 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005498
5499 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005500 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5501 input1->x, input1->len,
5502 input2->x, input2->len,
5503 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005504 goto exit;
5505
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005506 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005507 output_buffer,
5508 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005509 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005510
5511 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005512 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5513 input1->x, input1->len,
5514 input2->x, input2->len,
5515 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005516 goto exit;
5517
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005518 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5519 psa_set_key_algorithm( &derived_attributes, 0 );
5520 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005521 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005523 &derived_key ) );
5524 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005525 export_buffer, bytes1,
5526 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005527 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005528 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005529 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005530 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005531 &derived_key ) );
5532 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005533 export_buffer + bytes1, bytes2,
5534 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005535 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005536
5537 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005538 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5539 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005540
5541exit:
5542 mbedtls_free( output_buffer );
5543 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005544 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005545 psa_destroy_key( base_key );
5546 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005547 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005548}
5549/* END_CASE */
5550
5551/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005552void derive_key( int alg_arg,
5553 data_t *key_data, data_t *input1, data_t *input2,
5554 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005555 int expected_status_arg,
5556 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005557{
Ronald Cron5425a212020-08-04 14:58:35 +02005558 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5559 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005560 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005561 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005562 size_t bits = bits_arg;
5563 psa_status_t expected_status = expected_status_arg;
5564 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5565 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5566 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5567
5568 PSA_ASSERT( psa_crypto_init( ) );
5569
5570 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5571 psa_set_key_algorithm( &base_attributes, alg );
5572 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5573 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005574 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005575
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005576 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5577 input1->x, input1->len,
5578 input2->x, input2->len,
5579 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005580 goto exit;
5581
5582 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5583 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005584 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005585 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005586
5587 psa_status_t status =
5588 psa_key_derivation_output_key( &derived_attributes,
5589 &operation,
5590 &derived_key );
5591 if( is_large_output > 0 )
5592 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5593 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005594
5595exit:
5596 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005597 psa_destroy_key( base_key );
5598 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005599 PSA_DONE( );
5600}
5601/* END_CASE */
5602
5603/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005604void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005605 int our_key_type_arg, int our_key_alg_arg,
5606 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005607 int expected_status_arg )
5608{
Ronald Cron5425a212020-08-04 14:58:35 +02005609 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005610 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005611 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005612 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005613 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005614 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005615 psa_status_t expected_status = expected_status_arg;
5616 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005617
Gilles Peskine8817f612018-12-18 00:18:46 +01005618 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005619
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005620 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005621 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005622 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005623 PSA_ASSERT( psa_import_key( &attributes,
5624 our_key_data->x, our_key_data->len,
5625 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005626
Gilles Peskine77f40d82019-04-11 21:27:06 +02005627 /* The tests currently include inputs that should fail at either step.
5628 * Test cases that fail at the setup step should be changed to call
5629 * key_derivation_setup instead, and this function should be renamed
5630 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005631 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005632 if( status == PSA_SUCCESS )
5633 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005634 TEST_EQUAL( psa_key_derivation_key_agreement(
5635 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5636 our_key,
5637 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005638 expected_status );
5639 }
5640 else
5641 {
5642 TEST_ASSERT( status == expected_status );
5643 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005644
5645exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005646 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005647 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005648 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005649}
5650/* END_CASE */
5651
5652/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005653void raw_key_agreement( int alg_arg,
5654 int our_key_type_arg, data_t *our_key_data,
5655 data_t *peer_key_data,
5656 data_t *expected_output )
5657{
Ronald Cron5425a212020-08-04 14:58:35 +02005658 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005659 psa_algorithm_t alg = alg_arg;
5660 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005661 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005662 unsigned char *output = NULL;
5663 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005664 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005665
5666 ASSERT_ALLOC( output, expected_output->len );
5667 PSA_ASSERT( psa_crypto_init( ) );
5668
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005669 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5670 psa_set_key_algorithm( &attributes, alg );
5671 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005672 PSA_ASSERT( psa_import_key( &attributes,
5673 our_key_data->x, our_key_data->len,
5674 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005675
gabor-mezei-armceface22021-01-21 12:26:17 +01005676 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5677 key_bits = psa_get_key_bits( &attributes );
5678
Gilles Peskinebe697d82019-05-16 18:00:41 +02005679 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5680 peer_key_data->x, peer_key_data->len,
5681 output, expected_output->len,
5682 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005683 ASSERT_COMPARE( output, output_length,
5684 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005685 TEST_ASSERT( output_length <=
5686 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5687 TEST_ASSERT( output_length <=
5688 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005689
5690exit:
5691 mbedtls_free( output );
5692 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005693 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005694}
5695/* END_CASE */
5696
5697/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005698void key_agreement_capacity( int alg_arg,
5699 int our_key_type_arg, data_t *our_key_data,
5700 data_t *peer_key_data,
5701 int expected_capacity_arg )
5702{
Ronald Cron5425a212020-08-04 14:58:35 +02005703 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005704 psa_algorithm_t alg = alg_arg;
5705 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005706 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005708 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005709 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005710
Gilles Peskine8817f612018-12-18 00:18:46 +01005711 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005712
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005713 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5714 psa_set_key_algorithm( &attributes, alg );
5715 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005716 PSA_ASSERT( psa_import_key( &attributes,
5717 our_key_data->x, our_key_data->len,
5718 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005719
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005721 PSA_ASSERT( psa_key_derivation_key_agreement(
5722 &operation,
5723 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5724 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005725 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5726 {
5727 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005728 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005729 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005730 NULL, 0 ) );
5731 }
Gilles Peskine59685592018-09-18 12:11:34 +02005732
Gilles Peskinebf491972018-10-25 22:36:12 +02005733 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005734 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005735 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005736 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005737
Gilles Peskinebf491972018-10-25 22:36:12 +02005738 /* Test the actual capacity by reading the output. */
5739 while( actual_capacity > sizeof( output ) )
5740 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005741 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005742 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005743 actual_capacity -= sizeof( output );
5744 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005746 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005747 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005748 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005749
Gilles Peskine59685592018-09-18 12:11:34 +02005750exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005751 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005752 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005753 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005754}
5755/* END_CASE */
5756
5757/* BEGIN_CASE */
5758void key_agreement_output( int alg_arg,
5759 int our_key_type_arg, data_t *our_key_data,
5760 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005761 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005762{
Ronald Cron5425a212020-08-04 14:58:35 +02005763 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005764 psa_algorithm_t alg = alg_arg;
5765 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005766 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005768 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005769
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005770 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5771 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005772
Gilles Peskine8817f612018-12-18 00:18:46 +01005773 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005774
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005775 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5776 psa_set_key_algorithm( &attributes, alg );
5777 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005778 PSA_ASSERT( psa_import_key( &attributes,
5779 our_key_data->x, our_key_data->len,
5780 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005781
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005782 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005783 PSA_ASSERT( psa_key_derivation_key_agreement(
5784 &operation,
5785 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5786 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005787 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5788 {
5789 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005790 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005791 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005792 NULL, 0 ) );
5793 }
Gilles Peskine59685592018-09-18 12:11:34 +02005794
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005795 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005796 actual_output,
5797 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005798 ASSERT_COMPARE( actual_output, expected_output1->len,
5799 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005800 if( expected_output2->len != 0 )
5801 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005802 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005803 actual_output,
5804 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005805 ASSERT_COMPARE( actual_output, expected_output2->len,
5806 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005807 }
Gilles Peskine59685592018-09-18 12:11:34 +02005808
5809exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005810 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005811 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005812 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005813 mbedtls_free( actual_output );
5814}
5815/* END_CASE */
5816
5817/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005818void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005819{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005820 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005821 unsigned char *output = NULL;
5822 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005823 size_t i;
5824 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005825
Simon Butcher49f8e312020-03-03 15:51:50 +00005826 TEST_ASSERT( bytes_arg >= 0 );
5827
Gilles Peskine91892022021-02-08 19:50:26 +01005828 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005829 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005830
Gilles Peskine8817f612018-12-18 00:18:46 +01005831 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005832
Gilles Peskinea50d7392018-06-21 10:22:13 +02005833 /* Run several times, to ensure that every output byte will be
5834 * nonzero at least once with overwhelming probability
5835 * (2^(-8*number_of_runs)). */
5836 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005837 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005838 if( bytes != 0 )
5839 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005840 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005841
Gilles Peskinea50d7392018-06-21 10:22:13 +02005842 for( i = 0; i < bytes; i++ )
5843 {
5844 if( output[i] != 0 )
5845 ++changed[i];
5846 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005847 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005848
5849 /* Check that every byte was changed to nonzero at least once. This
5850 * validates that psa_generate_random is overwriting every byte of
5851 * the output buffer. */
5852 for( i = 0; i < bytes; i++ )
5853 {
5854 TEST_ASSERT( changed[i] != 0 );
5855 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005856
5857exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005858 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005859 mbedtls_free( output );
5860 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005861}
5862/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005863
5864/* BEGIN_CASE */
5865void generate_key( int type_arg,
5866 int bits_arg,
5867 int usage_arg,
5868 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005869 int expected_status_arg,
5870 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005871{
Ronald Cron5425a212020-08-04 14:58:35 +02005872 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005873 psa_key_type_t type = type_arg;
5874 psa_key_usage_t usage = usage_arg;
5875 size_t bits = bits_arg;
5876 psa_algorithm_t alg = alg_arg;
5877 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005879 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005880
Gilles Peskine8817f612018-12-18 00:18:46 +01005881 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005882
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005883 psa_set_key_usage_flags( &attributes, usage );
5884 psa_set_key_algorithm( &attributes, alg );
5885 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005886 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005887
5888 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005889 psa_status_t status = psa_generate_key( &attributes, &key );
5890
5891 if( is_large_key > 0 )
5892 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5893 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005894 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005895 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005896
5897 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005898 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005899 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5900 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005901
Gilles Peskine818ca122018-06-20 18:16:48 +02005902 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005903 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005904 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005905
5906exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005907 /*
5908 * Key attributes may have been returned by psa_get_key_attributes()
5909 * thus reset them as required.
5910 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005911 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005912
Ronald Cron5425a212020-08-04 14:58:35 +02005913 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005914 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005915}
5916/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005917
Ronald Cronee414c72021-03-18 18:50:08 +01005918/* 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 +02005919void generate_key_rsa( int bits_arg,
5920 data_t *e_arg,
5921 int expected_status_arg )
5922{
Ronald Cron5425a212020-08-04 14:58:35 +02005923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005924 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005925 size_t bits = bits_arg;
5926 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5927 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5928 psa_status_t expected_status = expected_status_arg;
5929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5930 uint8_t *exported = NULL;
5931 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005932 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005933 size_t exported_length = SIZE_MAX;
5934 uint8_t *e_read_buffer = NULL;
5935 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005936 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005937 size_t e_read_length = SIZE_MAX;
5938
5939 if( e_arg->len == 0 ||
5940 ( e_arg->len == 3 &&
5941 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5942 {
5943 is_default_public_exponent = 1;
5944 e_read_size = 0;
5945 }
5946 ASSERT_ALLOC( e_read_buffer, e_read_size );
5947 ASSERT_ALLOC( exported, exported_size );
5948
5949 PSA_ASSERT( psa_crypto_init( ) );
5950
5951 psa_set_key_usage_flags( &attributes, usage );
5952 psa_set_key_algorithm( &attributes, alg );
5953 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5954 e_arg->x, e_arg->len ) );
5955 psa_set_key_bits( &attributes, bits );
5956
5957 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005958 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005959 if( expected_status != PSA_SUCCESS )
5960 goto exit;
5961
5962 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005963 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005964 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5965 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5966 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5967 e_read_buffer, e_read_size,
5968 &e_read_length ) );
5969 if( is_default_public_exponent )
5970 TEST_EQUAL( e_read_length, 0 );
5971 else
5972 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5973
5974 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005975 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005976 goto exit;
5977
5978 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005979 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005980 exported, exported_size,
5981 &exported_length ) );
5982 {
5983 uint8_t *p = exported;
5984 uint8_t *end = exported + exported_length;
5985 size_t len;
5986 /* RSAPublicKey ::= SEQUENCE {
5987 * modulus INTEGER, -- n
5988 * publicExponent INTEGER } -- e
5989 */
5990 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005991 MBEDTLS_ASN1_SEQUENCE |
5992 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005993 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005994 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5995 MBEDTLS_ASN1_INTEGER ) );
5996 if( len >= 1 && p[0] == 0 )
5997 {
5998 ++p;
5999 --len;
6000 }
6001 if( e_arg->len == 0 )
6002 {
6003 TEST_EQUAL( len, 3 );
6004 TEST_EQUAL( p[0], 1 );
6005 TEST_EQUAL( p[1], 0 );
6006 TEST_EQUAL( p[2], 1 );
6007 }
6008 else
6009 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6010 }
6011
6012exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006013 /*
6014 * Key attributes may have been returned by psa_get_key_attributes() or
6015 * set by psa_set_key_domain_parameters() thus reset them as required.
6016 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006017 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006018
Ronald Cron5425a212020-08-04 14:58:35 +02006019 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006020 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006021 mbedtls_free( e_read_buffer );
6022 mbedtls_free( exported );
6023}
6024/* END_CASE */
6025
Darryl Greend49a4992018-06-18 17:27:26 +01006026/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006027void persistent_key_load_key_from_storage( data_t *data,
6028 int type_arg, int bits_arg,
6029 int usage_flags_arg, int alg_arg,
6030 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006031{
Ronald Cron71016a92020-08-28 19:01:50 +02006032 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006033 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006034 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6035 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006036 psa_key_type_t type = type_arg;
6037 size_t bits = bits_arg;
6038 psa_key_usage_t usage_flags = usage_flags_arg;
6039 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006040 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006041 unsigned char *first_export = NULL;
6042 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006043 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006044 size_t first_exported_length;
6045 size_t second_exported_length;
6046
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006047 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6048 {
6049 ASSERT_ALLOC( first_export, export_size );
6050 ASSERT_ALLOC( second_export, export_size );
6051 }
Darryl Greend49a4992018-06-18 17:27:26 +01006052
Gilles Peskine8817f612018-12-18 00:18:46 +01006053 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006054
Gilles Peskinec87af662019-05-15 16:12:22 +02006055 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006056 psa_set_key_usage_flags( &attributes, usage_flags );
6057 psa_set_key_algorithm( &attributes, alg );
6058 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006059 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006060
Darryl Green0c6575a2018-11-07 16:05:30 +00006061 switch( generation_method )
6062 {
6063 case IMPORT_KEY:
6064 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006065 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006066 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006067 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006068
Darryl Green0c6575a2018-11-07 16:05:30 +00006069 case GENERATE_KEY:
6070 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006071 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006072 break;
6073
6074 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006075#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006076 {
6077 /* Create base key */
6078 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6079 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6080 psa_set_key_usage_flags( &base_attributes,
6081 PSA_KEY_USAGE_DERIVE );
6082 psa_set_key_algorithm( &base_attributes, derive_alg );
6083 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006084 PSA_ASSERT( psa_import_key( &base_attributes,
6085 data->x, data->len,
6086 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006087 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006088 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006089 PSA_ASSERT( psa_key_derivation_input_key(
6090 &operation,
6091 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006092 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006093 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006094 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006095 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6096 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006097 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006098 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006099 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006100 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006101 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006102#else
6103 TEST_ASSUME( ! "KDF not supported in this configuration" );
6104#endif
6105 break;
6106
6107 default:
6108 TEST_ASSERT( ! "generation_method not implemented in test" );
6109 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006110 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006111 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006112
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006113 /* Export the key if permitted by the key policy. */
6114 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6115 {
Ronald Cron5425a212020-08-04 14:58:35 +02006116 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006117 first_export, export_size,
6118 &first_exported_length ) );
6119 if( generation_method == IMPORT_KEY )
6120 ASSERT_COMPARE( data->x, data->len,
6121 first_export, first_exported_length );
6122 }
Darryl Greend49a4992018-06-18 17:27:26 +01006123
6124 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006125 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006126 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006127 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006128
Darryl Greend49a4992018-06-18 17:27:26 +01006129 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006130 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006131 TEST_ASSERT( mbedtls_svc_key_id_equal(
6132 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006133 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6134 PSA_KEY_LIFETIME_PERSISTENT );
6135 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6136 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6137 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6138 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006139
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006140 /* Export the key again if permitted by the key policy. */
6141 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006142 {
Ronald Cron5425a212020-08-04 14:58:35 +02006143 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006144 second_export, export_size,
6145 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006146 ASSERT_COMPARE( first_export, first_exported_length,
6147 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006148 }
6149
6150 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006151 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006152 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006153
6154exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006155 /*
6156 * Key attributes may have been returned by psa_get_key_attributes()
6157 * thus reset them as required.
6158 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006159 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006160
Darryl Greend49a4992018-06-18 17:27:26 +01006161 mbedtls_free( first_export );
6162 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006163 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006164 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006165 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006166 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006167}
6168/* END_CASE */