blob: da395021465959844eccb60e380377e65f812805 [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
Paul Elliott41ffae12021-07-22 21:52:01 +0100285 * \param expect_valid_signature If non zero, we expect the signature to be
286 * valid
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100287 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100288 * \param do_zero_parts If non-zero, interleave zero length chunks
289 * with normal length chunks
290 * \param swap_set_functions If non-zero, swap the order of set lengths
291 * and set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100292 *
293 * \return int Zero on failure, non-zero on success.
294 *
295 */
296static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
297 int alg_arg,
298 data_t *nonce,
299 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100300 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100301 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100302 int data_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 int do_set_lengths,
304 data_t *expected_output,
305 int expect_valid_signature,
Paul Elliott329d5382021-07-22 17:10:45 +0100306 int is_encrypt,
Paul Elliottebf91632021-07-22 17:54:42 +0100307 int do_zero_parts,
308 int swap_set_functions )
Paul Elliottd3f82412021-06-16 16:52:21 +0100309{
310 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
311 psa_key_type_t key_type = key_type_arg;
312 psa_algorithm_t alg = alg_arg;
313 psa_aead_operation_t operation;
314 unsigned char *output_data = NULL;
315 unsigned char *part_data = NULL;
316 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100318 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100319 size_t output_size = 0;
320 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100321 size_t output_length = 0;
322 size_t key_bits = 0;
323 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100324 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100325 size_t part_length = 0;
326 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100328 size_t ad_part_len = 0;
329 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100330 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
332 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
333
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100334 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100335 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 PSA_ASSERT( psa_crypto_init( ) );
338
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100339 if( is_encrypt )
340 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
341 else
342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
343
Paul Elliottd3f82412021-06-16 16:52:21 +0100344 psa_set_key_algorithm( &attributes, alg );
345 psa_set_key_type( &attributes, key_type );
346
347 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
348 &key ) );
349
350 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
351 key_bits = psa_get_key_bits( &attributes );
352
353 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 if( is_encrypt )
356 {
357 /* Tag gets written at end of buffer. */
358 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
359 ( input_data->len +
360 tag_length ) );
361 data_true_size = input_data->len;
362 }
363 else
364 {
365 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
366 ( input_data->len -
367 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100368
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100369 /* Do not want to attempt to decrypt tag. */
370 data_true_size = input_data->len - tag_length;
371 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100372
373 ASSERT_ALLOC( output_data, output_size );
374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 if( is_encrypt )
376 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100377 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
378 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 }
380 else
381 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100382 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
383 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100384 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100385
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100386 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100387
388 operation = psa_aead_operation_init( );
389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390
391 if( is_encrypt )
392 status = psa_aead_encrypt_setup( &operation, key, alg );
393 else
394 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100395
396 /* If the operation is not supported, just skip and not fail in case the
397 * encryption involves a common limitation of cryptography hardwares and
398 * an alternative implementation. */
399 if( status == PSA_ERROR_NOT_SUPPORTED )
400 {
401 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
402 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
403 }
404
405 PSA_ASSERT( status );
406
Paul Elliottebf91632021-07-22 17:54:42 +0100407 if( swap_set_functions )
Paul Elliottd3f82412021-06-16 16:52:21 +0100408 {
Paul Elliottebf91632021-07-22 17:54:42 +0100409 if( do_set_lengths )
410 {
411 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
412 data_true_size ) );
413 }
414
415 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
416 }
417 else
418 {
419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420
421 if( do_set_lengths )
422 {
423 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
424 data_true_size ) );
425 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100426 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100427
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100428 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100429 {
430 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100431 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100432 part_offset = 0;
433
434 while( part_offset < additional_data->len )
435 {
Paul Elliott329d5382021-07-22 17:10:45 +0100436 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100437 {
Paul Elliott329d5382021-07-22 17:10:45 +0100438 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
440 else
441 {
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100442 if( additional_data->len - part_offset < ad_part_len )
Paul Elliott329d5382021-07-22 17:10:45 +0100443 {
444 part_length = additional_data->len - part_offset;
445 }
446 else
447 {
448 part_length = ad_part_len;
449 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100450 }
451
452 PSA_ASSERT( psa_aead_update_ad( &operation,
453 additional_data->x + part_offset,
454 part_length ) );
455
456 part_offset += part_length;
457 }
458 }
459 else
460 {
461 /* Pass additional data in one go. */
462 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
463 additional_data->len ) );
464 }
465
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100466 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 {
468 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100469 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100470 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100471 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100472
473 ASSERT_ALLOC( part_data, part_data_size );
474
475 part_offset = 0;
476
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100477 while( part_offset < data_true_size )
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 {
Paul Elliott329d5382021-07-22 17:10:45 +0100479 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 {
Paul Elliott329d5382021-07-22 17:10:45 +0100481 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100482 }
483 else
484 {
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100485 if( ( data_true_size - part_offset ) < data_part_len )
Paul Elliott329d5382021-07-22 17:10:45 +0100486 {
487 part_length = ( data_true_size - part_offset );
488 }
489 else
490 {
491 part_length = data_part_len;
492 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100493 }
494
495 PSA_ASSERT( psa_aead_update( &operation,
496 ( input_data->x + part_offset ),
497 part_length, part_data,
498 part_data_size,
499 &output_part_length ) );
500
501 if( output_data && output_part_length )
502 {
503 memcpy( ( output_data + part_offset ), part_data,
504 output_part_length );
505 }
506
507 part_offset += part_length;
508 output_length += output_part_length;
509 }
510 }
511 else
512 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100513 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100515 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100516 output_size, &output_length ) );
517 }
518
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 if( is_encrypt )
520 PSA_ASSERT( psa_aead_finish( &operation, final_data,
521 final_output_size,
522 &output_part_length,
523 tag_buffer, tag_length,
524 &tag_size ) );
525 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100526 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100527 status = psa_aead_verify( &operation, final_data,
528 final_output_size,
529 &output_part_length,
530 ( input_data->x + data_true_size ),
531 tag_length );
532
533 if( status != PSA_SUCCESS )
534 {
535 if( !expect_valid_signature )
536 {
537 /* Expected failure. */
538 test_ok = 1;
539 goto exit;
540 }
541 else
542 PSA_ASSERT( status );
543 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100544 }
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546 if( output_data && output_part_length )
547 memcpy( ( output_data + output_length ), final_data,
548 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100549
550 output_length += output_part_length;
551
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100552
553 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
554 * should be exact.*/
555 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100556 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100557 TEST_EQUAL( tag_length, tag_size );
558
559 if( output_data && tag_length )
560 memcpy( ( output_data + output_length ), tag_buffer,
561 tag_length );
562
563 output_length += tag_length;
564
565 TEST_EQUAL( output_length,
566 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
567 input_data->len ) );
568 TEST_ASSERT( output_length <=
569 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
570 }
571 else
572 {
573 TEST_EQUAL( output_length,
574 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
575 input_data->len ) );
576 TEST_ASSERT( output_length <=
577 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100578 }
579
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100582 output_data, output_length );
583
Paul Elliottd3f82412021-06-16 16:52:21 +0100584
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100585 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100586
587exit:
588 psa_destroy_key( key );
589 psa_aead_abort( &operation );
590 mbedtls_free( output_data );
591 mbedtls_free( part_data );
592 mbedtls_free( final_data );
593 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100594
595 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100596}
597
Gilles Peskinee59236f2018-01-27 23:32:46 +0100598/* END_HEADER */
599
600/* BEGIN_DEPENDENCIES
601 * depends_on:MBEDTLS_PSA_CRYPTO_C
602 * END_DEPENDENCIES
603 */
604
605/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200606void static_checks( )
607{
608 size_t max_truncated_mac_size =
609 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
610
611 /* Check that the length for a truncated MAC always fits in the algorithm
612 * encoding. The shifted mask is the maximum truncated value. The
613 * untruncated algorithm may be one byte larger. */
614 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
615}
616/* END_CASE */
617
618/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200619void import_with_policy( int type_arg,
620 int usage_arg, int alg_arg,
621 int expected_status_arg )
622{
623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
624 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200626 psa_key_type_t type = type_arg;
627 psa_key_usage_t usage = usage_arg;
628 psa_algorithm_t alg = alg_arg;
629 psa_status_t expected_status = expected_status_arg;
630 const uint8_t key_material[16] = {0};
631 psa_status_t status;
632
633 PSA_ASSERT( psa_crypto_init( ) );
634
635 psa_set_key_type( &attributes, type );
636 psa_set_key_usage_flags( &attributes, usage );
637 psa_set_key_algorithm( &attributes, alg );
638
639 status = psa_import_key( &attributes,
640 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200641 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200642 TEST_EQUAL( status, expected_status );
643 if( status != PSA_SUCCESS )
644 goto exit;
645
Ronald Cron5425a212020-08-04 14:58:35 +0200646 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200647 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
648 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
649 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200650 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200651
Ronald Cron5425a212020-08-04 14:58:35 +0200652 PSA_ASSERT( psa_destroy_key( key ) );
653 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200654
655exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100656 /*
657 * Key attributes may have been returned by psa_get_key_attributes()
658 * thus reset them as required.
659 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200660 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100661
662 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200663 PSA_DONE( );
664}
665/* END_CASE */
666
667/* BEGIN_CASE */
668void import_with_data( data_t *data, int type_arg,
669 int attr_bits_arg,
670 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200671{
672 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
673 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200675 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200676 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200677 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679
Gilles Peskine8817f612018-12-18 00:18:46 +0100680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100681
Gilles Peskine4747d192019-04-17 15:05:45 +0200682 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200683 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200684
Ronald Cron5425a212020-08-04 14:58:35 +0200685 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100686 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200687 if( status != PSA_SUCCESS )
688 goto exit;
689
Ronald Cron5425a212020-08-04 14:58:35 +0200690 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200691 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200692 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200693 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200694 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200695
Ronald Cron5425a212020-08-04 14:58:35 +0200696 PSA_ASSERT( psa_destroy_key( key ) );
697 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698
699exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100700 /*
701 * Key attributes may have been returned by psa_get_key_attributes()
702 * thus reset them as required.
703 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200704 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100705
706 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200707 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708}
709/* END_CASE */
710
711/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200712void import_large_key( int type_arg, int byte_size_arg,
713 int expected_status_arg )
714{
715 psa_key_type_t type = type_arg;
716 size_t byte_size = byte_size_arg;
717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
718 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200720 psa_status_t status;
721 uint8_t *buffer = NULL;
722 size_t buffer_size = byte_size + 1;
723 size_t n;
724
Steven Cooreman69967ce2021-01-18 18:01:08 +0100725 /* Skip the test case if the target running the test cannot
726 * accomodate large keys due to heap size constraints */
727 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200728 memset( buffer, 'K', byte_size );
729
730 PSA_ASSERT( psa_crypto_init( ) );
731
732 /* Try importing the key */
733 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
734 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200735 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100736 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200737 TEST_EQUAL( status, expected_status );
738
739 if( status == PSA_SUCCESS )
740 {
Ronald Cron5425a212020-08-04 14:58:35 +0200741 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200742 TEST_EQUAL( psa_get_key_type( &attributes ), type );
743 TEST_EQUAL( psa_get_key_bits( &attributes ),
744 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200745 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200746 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200747 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 for( n = 0; n < byte_size; n++ )
749 TEST_EQUAL( buffer[n], 'K' );
750 for( n = byte_size; n < buffer_size; n++ )
751 TEST_EQUAL( buffer[n], 0 );
752 }
753
754exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100755 /*
756 * Key attributes may have been returned by psa_get_key_attributes()
757 * thus reset them as required.
758 */
759 psa_reset_key_attributes( &attributes );
760
Ronald Cron5425a212020-08-04 14:58:35 +0200761 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200762 PSA_DONE( );
763 mbedtls_free( buffer );
764}
765/* END_CASE */
766
767/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200768void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
769{
Ronald Cron5425a212020-08-04 14:58:35 +0200770 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200771 size_t bits = bits_arg;
772 psa_status_t expected_status = expected_status_arg;
773 psa_status_t status;
774 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200775 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200776 size_t buffer_size = /* Slight overapproximations */
777 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200778 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200779 unsigned char *p;
780 int ret;
781 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200783
Gilles Peskine8817f612018-12-18 00:18:46 +0100784 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200785 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200786
787 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
788 bits, keypair ) ) >= 0 );
789 length = ret;
790
791 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200792 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200793 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100794 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200795
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200796 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200797 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200798
799exit:
800 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200801 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200802}
803/* END_CASE */
804
805/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300806void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300807 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200808 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100809 int expected_bits,
810 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200811 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100812 int canonical_input )
813{
Ronald Cron5425a212020-08-04 14:58:35 +0200814 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200816 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200817 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 unsigned char *exported = NULL;
820 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100822 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200825 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826
Moran Pekercb088e72018-07-17 17:36:59 +0300827 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200828 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200830 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100831 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100832
Gilles Peskine4747d192019-04-17 15:05:45 +0200833 psa_set_key_usage_flags( &attributes, usage_arg );
834 psa_set_key_algorithm( &attributes, alg );
835 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700836
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200838 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100839
840 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200841 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200842 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
843 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200844 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845
846 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200847 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100848 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100849
850 /* The exported length must be set by psa_export_key() to a value between 0
851 * and export_size. On errors, the exported length must be 0. */
852 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
853 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
854 TEST_ASSERT( exported_length <= export_size );
855
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200856 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200857 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200859 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100860 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200862 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100863
Gilles Peskineea38a922021-02-13 00:05:16 +0100864 /* Run sanity checks on the exported key. For non-canonical inputs,
865 * this validates the canonical representations. For canonical inputs,
866 * this doesn't directly validate the implementation, but it still helps
867 * by cross-validating the test data with the sanity check code. */
868 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200869 goto exit;
870
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200872 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873 else
874 {
Ronald Cron5425a212020-08-04 14:58:35 +0200875 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200876 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200877 &key2 ) );
878 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 reexported,
880 export_size,
881 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200882 ASSERT_COMPARE( exported, exported_length,
883 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200884 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100886 TEST_ASSERT( exported_length <=
887 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
888 psa_get_key_bits( &got_attributes ) ) );
889 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890
891destroy:
892 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200893 PSA_ASSERT( psa_destroy_key( key ) );
894 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100895
896exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100897 /*
898 * Key attributes may have been returned by psa_get_key_attributes()
899 * thus reset them as required.
900 */
901 psa_reset_key_attributes( &got_attributes );
902
itayzafrir3e02b3b2018-06-12 17:06:52 +0300903 mbedtls_free( exported );
904 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200905 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100906}
907/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100908
Moran Pekerf709f4a2018-06-06 17:26:04 +0300909/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300910void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200911 int type_arg,
912 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100913 int export_size_delta,
914 int expected_export_status_arg,
915 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300916{
Ronald Cron5425a212020-08-04 14:58:35 +0200917 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200919 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200920 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300921 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300922 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100923 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100924 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200925 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300926
Gilles Peskine8817f612018-12-18 00:18:46 +0100927 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300928
Gilles Peskine4747d192019-04-17 15:05:45 +0200929 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
930 psa_set_key_algorithm( &attributes, alg );
931 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300932
933 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200934 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300935
Gilles Peskine49c25912018-10-29 15:15:31 +0100936 /* Export the public key */
937 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200938 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200939 exported, export_size,
940 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100941 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100942 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100943 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200944 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100945 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200946 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200947 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100948 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100949 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100950 TEST_ASSERT( expected_public_key->len <=
951 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
952 TEST_ASSERT( expected_public_key->len <=
953 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100954 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
955 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100956 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300957
958exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100959 /*
960 * Key attributes may have been returned by psa_get_key_attributes()
961 * thus reset them as required.
962 */
963 psa_reset_key_attributes( &attributes );
964
itayzafrir3e02b3b2018-06-12 17:06:52 +0300965 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200966 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200967 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300968}
969/* END_CASE */
970
Gilles Peskine20035e32018-02-03 22:44:14 +0100971/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200972void import_and_exercise_key( data_t *data,
973 int type_arg,
974 int bits_arg,
975 int alg_arg )
976{
Ronald Cron5425a212020-08-04 14:58:35 +0200977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200978 psa_key_type_t type = type_arg;
979 size_t bits = bits_arg;
980 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100981 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200983 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200984
Gilles Peskine8817f612018-12-18 00:18:46 +0100985 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200986
Gilles Peskine4747d192019-04-17 15:05:45 +0200987 psa_set_key_usage_flags( &attributes, usage );
988 psa_set_key_algorithm( &attributes, alg );
989 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200990
991 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200992 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993
994 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200995 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200996 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
997 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200998
999 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001000 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001001 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001002
Ronald Cron5425a212020-08-04 14:58:35 +02001003 PSA_ASSERT( psa_destroy_key( key ) );
1004 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001005
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001006exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001007 /*
1008 * Key attributes may have been returned by psa_get_key_attributes()
1009 * thus reset them as required.
1010 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001011 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001012
1013 psa_reset_key_attributes( &attributes );
1014 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001015 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001016}
1017/* END_CASE */
1018
1019/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001020void effective_key_attributes( int type_arg, int expected_type_arg,
1021 int bits_arg, int expected_bits_arg,
1022 int usage_arg, int expected_usage_arg,
1023 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001024{
Ronald Cron5425a212020-08-04 14:58:35 +02001025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001026 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001027 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001028 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001029 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001030 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001031 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001032 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001033 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001034 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001035
Gilles Peskine8817f612018-12-18 00:18:46 +01001036 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001037
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001038 psa_set_key_usage_flags( &attributes, usage );
1039 psa_set_key_algorithm( &attributes, alg );
1040 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001041 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001042
Ronald Cron5425a212020-08-04 14:58:35 +02001043 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001044 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001047 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1048 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1049 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1050 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001051
1052exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001053 /*
1054 * Key attributes may have been returned by psa_get_key_attributes()
1055 * thus reset them as required.
1056 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001057 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001058
1059 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001060 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001061}
1062/* END_CASE */
1063
1064/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001065void check_key_policy( int type_arg, int bits_arg,
1066 int usage_arg, int alg_arg )
1067{
1068 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1069 usage_arg, usage_arg, alg_arg, alg_arg );
1070 goto exit;
1071}
1072/* END_CASE */
1073
1074/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001075void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001076{
1077 /* Test each valid way of initializing the object, except for `= {0}`, as
1078 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1079 * though it's OK by the C standard. We could test for this, but we'd need
1080 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001081 psa_key_attributes_t func = psa_key_attributes_init( );
1082 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1083 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001084
1085 memset( &zero, 0, sizeof( zero ) );
1086
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001087 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1088 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1089 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001090
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001091 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1092 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1093 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1094
1095 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1096 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1097 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1098
1099 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1100 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1101 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1102
1103 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1104 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1105 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001106}
1107/* END_CASE */
1108
1109/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001110void mac_key_policy( int policy_usage,
1111 int policy_alg,
1112 int key_type,
1113 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001114 int exercise_alg,
1115 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001116{
Ronald Cron5425a212020-08-04 14:58:35 +02001117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001119 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001120 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001121 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001122 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001123
Gilles Peskine8817f612018-12-18 00:18:46 +01001124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001126 psa_set_key_usage_flags( &attributes, policy_usage );
1127 psa_set_key_algorithm( &attributes, policy_alg );
1128 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001129
Gilles Peskine049c7532019-05-15 20:22:09 +02001130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001131 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001132
Ronald Cron5425a212020-08-04 14:58:35 +02001133 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001134 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001135 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001136 else
1137 TEST_EQUAL( status, expected_status );
1138
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001140
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001142 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001143 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001144 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001145 else
1146 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001147
1148exit:
1149 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001150 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001151 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001152}
1153/* END_CASE */
1154
1155/* BEGIN_CASE */
1156void cipher_key_policy( int policy_usage,
1157 int policy_alg,
1158 int key_type,
1159 data_t *key_data,
1160 int exercise_alg )
1161{
Ronald Cron5425a212020-08-04 14:58:35 +02001162 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001164 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001165 psa_status_t status;
1166
Gilles Peskine8817f612018-12-18 00:18:46 +01001167 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001168
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001169 psa_set_key_usage_flags( &attributes, policy_usage );
1170 psa_set_key_algorithm( &attributes, policy_alg );
1171 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
Gilles Peskine049c7532019-05-15 20:22:09 +02001173 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001174 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001175
Ronald Cron5425a212020-08-04 14:58:35 +02001176 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177 if( policy_alg == exercise_alg &&
1178 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001179 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001180 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001181 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182 psa_cipher_abort( &operation );
1183
Ronald Cron5425a212020-08-04 14:58:35 +02001184 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001185 if( policy_alg == exercise_alg &&
1186 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001188 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001189 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001190
1191exit:
1192 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001193 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001194 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001195}
1196/* END_CASE */
1197
1198/* BEGIN_CASE */
1199void aead_key_policy( int policy_usage,
1200 int policy_alg,
1201 int key_type,
1202 data_t *key_data,
1203 int nonce_length_arg,
1204 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001205 int exercise_alg,
1206 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207{
Ronald Cron5425a212020-08-04 14:58:35 +02001208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001211 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001212 unsigned char nonce[16] = {0};
1213 size_t nonce_length = nonce_length_arg;
1214 unsigned char tag[16];
1215 size_t tag_length = tag_length_arg;
1216 size_t output_length;
1217
1218 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1219 TEST_ASSERT( tag_length <= sizeof( tag ) );
1220
Gilles Peskine8817f612018-12-18 00:18:46 +01001221 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001223 psa_set_key_usage_flags( &attributes, policy_usage );
1224 psa_set_key_algorithm( &attributes, policy_alg );
1225 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001226
Gilles Peskine049c7532019-05-15 20:22:09 +02001227 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001228 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001229
Ronald Cron5425a212020-08-04 14:58:35 +02001230 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001231 nonce, nonce_length,
1232 NULL, 0,
1233 NULL, 0,
1234 tag, tag_length,
1235 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001236 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1237 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001238 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001239 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001240
1241 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001242 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001243 nonce, nonce_length,
1244 NULL, 0,
1245 tag, tag_length,
1246 NULL, 0,
1247 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001248 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1249 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1250 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001251 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001253 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001254
1255exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001256 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001257 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001258}
1259/* END_CASE */
1260
1261/* BEGIN_CASE */
1262void asymmetric_encryption_key_policy( int policy_usage,
1263 int policy_alg,
1264 int key_type,
1265 data_t *key_data,
1266 int exercise_alg )
1267{
Ronald Cron5425a212020-08-04 14:58:35 +02001268 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001270 psa_status_t status;
1271 size_t key_bits;
1272 size_t buffer_length;
1273 unsigned char *buffer = NULL;
1274 size_t output_length;
1275
Gilles Peskine8817f612018-12-18 00:18:46 +01001276 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001278 psa_set_key_usage_flags( &attributes, policy_usage );
1279 psa_set_key_algorithm( &attributes, policy_alg );
1280 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001281
Gilles Peskine049c7532019-05-15 20:22:09 +02001282 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001283 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001284
Ronald Cron5425a212020-08-04 14:58:35 +02001285 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001286 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1288 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001289 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001290
Ronald Cron5425a212020-08-04 14:58:35 +02001291 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001292 NULL, 0,
1293 NULL, 0,
1294 buffer, buffer_length,
1295 &output_length );
1296 if( policy_alg == exercise_alg &&
1297 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001298 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001299 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001300 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001301
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001302 if( buffer_length != 0 )
1303 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001304 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001305 buffer, buffer_length,
1306 NULL, 0,
1307 buffer, buffer_length,
1308 &output_length );
1309 if( policy_alg == exercise_alg &&
1310 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001311 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001312 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001313 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001314
1315exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001316 /*
1317 * Key attributes may have been returned by psa_get_key_attributes()
1318 * thus reset them as required.
1319 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001320 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001321
1322 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001323 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001324 mbedtls_free( buffer );
1325}
1326/* END_CASE */
1327
1328/* BEGIN_CASE */
1329void asymmetric_signature_key_policy( int policy_usage,
1330 int policy_alg,
1331 int key_type,
1332 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001333 int exercise_alg,
1334 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001335{
Ronald Cron5425a212020-08-04 14:58:35 +02001336 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001338 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001339 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1340 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1341 * compatible with the policy and `payload_length_arg` is supposed to be
1342 * a valid input length to sign. If `payload_length_arg <= 0`,
1343 * `exercise_alg` is supposed to be forbidden by the policy. */
1344 int compatible_alg = payload_length_arg > 0;
1345 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001346 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001347 size_t signature_length;
1348
Gilles Peskine8817f612018-12-18 00:18:46 +01001349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001350
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001351 psa_set_key_usage_flags( &attributes, policy_usage );
1352 psa_set_key_algorithm( &attributes, policy_alg );
1353 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354
Gilles Peskine049c7532019-05-15 20:22:09 +02001355 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001356 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001357
Ronald Cron5425a212020-08-04 14:58:35 +02001358 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001359 payload, payload_length,
1360 signature, sizeof( signature ),
1361 &signature_length );
1362 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001363 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001365 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001366
1367 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001368 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001369 payload, payload_length,
1370 signature, sizeof( signature ) );
1371 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001372 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001373 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001374 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001375
1376exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001377 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001378 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001379}
1380/* END_CASE */
1381
Janos Follathba3fab92019-06-11 14:50:16 +01001382/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001383void derive_key_policy( int policy_usage,
1384 int policy_alg,
1385 int key_type,
1386 data_t *key_data,
1387 int exercise_alg )
1388{
Ronald Cron5425a212020-08-04 14:58:35 +02001389 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001390 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001391 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001392 psa_status_t status;
1393
Gilles Peskine8817f612018-12-18 00:18:46 +01001394 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001395
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001396 psa_set_key_usage_flags( &attributes, policy_usage );
1397 psa_set_key_algorithm( &attributes, policy_alg );
1398 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001399
Gilles Peskine049c7532019-05-15 20:22:09 +02001400 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001401 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001402
Janos Follathba3fab92019-06-11 14:50:16 +01001403 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1404
1405 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1406 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001407 {
Janos Follathba3fab92019-06-11 14:50:16 +01001408 PSA_ASSERT( psa_key_derivation_input_bytes(
1409 &operation,
1410 PSA_KEY_DERIVATION_INPUT_SEED,
1411 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001412 }
Janos Follathba3fab92019-06-11 14:50:16 +01001413
1414 status = psa_key_derivation_input_key( &operation,
1415 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001416 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001417
Gilles Peskineea0fb492018-07-12 17:17:20 +02001418 if( policy_alg == exercise_alg &&
1419 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001420 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001421 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001422 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001423
1424exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001425 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001426 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001427 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001428}
1429/* END_CASE */
1430
1431/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001432void agreement_key_policy( int policy_usage,
1433 int policy_alg,
1434 int key_type_arg,
1435 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001436 int exercise_alg,
1437 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001438{
Ronald Cron5425a212020-08-04 14:58:35 +02001439 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001440 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001442 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001443 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001444 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001445
Gilles Peskine8817f612018-12-18 00:18:46 +01001446 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001447
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001448 psa_set_key_usage_flags( &attributes, policy_usage );
1449 psa_set_key_algorithm( &attributes, policy_alg );
1450 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001451
Gilles Peskine049c7532019-05-15 20:22:09 +02001452 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001453 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001454
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001455 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001456 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001457
Steven Cooremance48e852020-10-05 16:02:45 +02001458 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001459
1460exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001461 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001462 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001463 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001464}
1465/* END_CASE */
1466
1467/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001468void key_policy_alg2( int key_type_arg, data_t *key_data,
1469 int usage_arg, int alg_arg, int alg2_arg )
1470{
Ronald Cron5425a212020-08-04 14:58:35 +02001471 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001472 psa_key_type_t key_type = key_type_arg;
1473 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1474 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1475 psa_key_usage_t usage = usage_arg;
1476 psa_algorithm_t alg = alg_arg;
1477 psa_algorithm_t alg2 = alg2_arg;
1478
1479 PSA_ASSERT( psa_crypto_init( ) );
1480
1481 psa_set_key_usage_flags( &attributes, usage );
1482 psa_set_key_algorithm( &attributes, alg );
1483 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1484 psa_set_key_type( &attributes, key_type );
1485 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001486 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001487
Ronald Cron5425a212020-08-04 14:58:35 +02001488 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001489 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1490 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1491 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1492
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001493 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001494 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001495 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001496 goto exit;
1497
1498exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001499 /*
1500 * Key attributes may have been returned by psa_get_key_attributes()
1501 * thus reset them as required.
1502 */
1503 psa_reset_key_attributes( &got_attributes );
1504
Ronald Cron5425a212020-08-04 14:58:35 +02001505 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001506 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001507}
1508/* END_CASE */
1509
1510/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001511void raw_agreement_key_policy( int policy_usage,
1512 int policy_alg,
1513 int key_type_arg,
1514 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001515 int exercise_alg,
1516 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517{
Ronald Cron5425a212020-08-04 14:58:35 +02001518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001520 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001521 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001522 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001523 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001524
1525 PSA_ASSERT( psa_crypto_init( ) );
1526
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001527 psa_set_key_usage_flags( &attributes, policy_usage );
1528 psa_set_key_algorithm( &attributes, policy_alg );
1529 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001530
Gilles Peskine049c7532019-05-15 20:22:09 +02001531 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001532 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001533
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001534 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001535
Steven Cooremance48e852020-10-05 16:02:45 +02001536 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001537
1538exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001539 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001540 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001541 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001542}
1543/* END_CASE */
1544
1545/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001546void copy_success( int source_usage_arg,
1547 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001548 int type_arg, data_t *material,
1549 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001550 int target_usage_arg,
1551 int target_alg_arg, int target_alg2_arg,
1552 int expected_usage_arg,
1553 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001554{
Gilles Peskineca25db92019-04-19 11:43:08 +02001555 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1556 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001557 psa_key_usage_t expected_usage = expected_usage_arg;
1558 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001559 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001560 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1561 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001562 uint8_t *export_buffer = NULL;
1563
Gilles Peskine57ab7212019-01-28 13:03:09 +01001564 PSA_ASSERT( psa_crypto_init( ) );
1565
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 /* Prepare the source key. */
1567 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1568 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001569 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001570 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001571 PSA_ASSERT( psa_import_key( &source_attributes,
1572 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001573 &source_key ) );
1574 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001575
Gilles Peskineca25db92019-04-19 11:43:08 +02001576 /* Prepare the target attributes. */
1577 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001578 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001579 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001580 /* Set volatile lifetime to reset the key identifier to 0. */
1581 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1582 }
1583
Gilles Peskineca25db92019-04-19 11:43:08 +02001584 if( target_usage_arg != -1 )
1585 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1586 if( target_alg_arg != -1 )
1587 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001588 if( target_alg2_arg != -1 )
1589 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001590
1591 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001592 PSA_ASSERT( psa_copy_key( source_key,
1593 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001594
1595 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001596 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001597
1598 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001599 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001600 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1601 psa_get_key_type( &target_attributes ) );
1602 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1603 psa_get_key_bits( &target_attributes ) );
1604 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1605 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001606 TEST_EQUAL( expected_alg2,
1607 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001608 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1609 {
1610 size_t length;
1611 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001612 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001613 material->len, &length ) );
1614 ASSERT_COMPARE( material->x, material->len,
1615 export_buffer, length );
1616 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001617
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001618 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001619 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001620 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001621 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001622
Ronald Cron5425a212020-08-04 14:58:35 +02001623 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001624
1625exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001626 /*
1627 * Source and target key attributes may have been returned by
1628 * psa_get_key_attributes() thus reset them as required.
1629 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001630 psa_reset_key_attributes( &source_attributes );
1631 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001632
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001633 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001634 mbedtls_free( export_buffer );
1635}
1636/* END_CASE */
1637
1638/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001639void copy_fail( int source_usage_arg,
1640 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001641 int type_arg, data_t *material,
1642 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001643 int target_usage_arg,
1644 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001645 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001646 int expected_status_arg )
1647{
1648 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1649 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001650 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1651 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001652 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001653
1654 PSA_ASSERT( psa_crypto_init( ) );
1655
1656 /* Prepare the source key. */
1657 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1658 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001659 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001660 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001661 PSA_ASSERT( psa_import_key( &source_attributes,
1662 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001663 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001664
1665 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001666 psa_set_key_id( &target_attributes, key_id );
1667 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001668 psa_set_key_type( &target_attributes, target_type_arg );
1669 psa_set_key_bits( &target_attributes, target_bits_arg );
1670 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1671 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001672 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001673
1674 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001675 TEST_EQUAL( psa_copy_key( source_key,
1676 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001677 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001678
Ronald Cron5425a212020-08-04 14:58:35 +02001679 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001680
Gilles Peskine4a644642019-05-03 17:14:08 +02001681exit:
1682 psa_reset_key_attributes( &source_attributes );
1683 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001684 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001685}
1686/* END_CASE */
1687
1688/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001689void hash_operation_init( )
1690{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001691 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001692 /* Test each valid way of initializing the object, except for `= {0}`, as
1693 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1694 * though it's OK by the C standard. We could test for this, but we'd need
1695 * to supress the Clang warning for the test. */
1696 psa_hash_operation_t func = psa_hash_operation_init( );
1697 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1698 psa_hash_operation_t zero;
1699
1700 memset( &zero, 0, sizeof( zero ) );
1701
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001702 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001703 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1704 PSA_ERROR_BAD_STATE );
1705 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1706 PSA_ERROR_BAD_STATE );
1707 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1708 PSA_ERROR_BAD_STATE );
1709
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001710 /* A default hash operation should be abortable without error. */
1711 PSA_ASSERT( psa_hash_abort( &func ) );
1712 PSA_ASSERT( psa_hash_abort( &init ) );
1713 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001714}
1715/* END_CASE */
1716
1717/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001718void hash_setup( int alg_arg,
1719 int expected_status_arg )
1720{
1721 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001722 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001723 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001724 psa_status_t status;
1725
Gilles Peskine8817f612018-12-18 00:18:46 +01001726 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001727
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001728 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001729 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001730
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001731 /* Whether setup succeeded or failed, abort must succeed. */
1732 PSA_ASSERT( psa_hash_abort( &operation ) );
1733
1734 /* If setup failed, reproduce the failure, so as to
1735 * test the resulting state of the operation object. */
1736 if( status != PSA_SUCCESS )
1737 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1738
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001739 /* Now the operation object should be reusable. */
1740#if defined(KNOWN_SUPPORTED_HASH_ALG)
1741 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1742 PSA_ASSERT( psa_hash_abort( &operation ) );
1743#endif
1744
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001745exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001746 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001747}
1748/* END_CASE */
1749
1750/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001751void hash_compute_fail( int alg_arg, data_t *input,
1752 int output_size_arg, int expected_status_arg )
1753{
1754 psa_algorithm_t alg = alg_arg;
1755 uint8_t *output = NULL;
1756 size_t output_size = output_size_arg;
1757 size_t output_length = INVALID_EXPORT_LENGTH;
1758 psa_status_t expected_status = expected_status_arg;
1759 psa_status_t status;
1760
1761 ASSERT_ALLOC( output, output_size );
1762
1763 PSA_ASSERT( psa_crypto_init( ) );
1764
1765 status = psa_hash_compute( alg, input->x, input->len,
1766 output, output_size, &output_length );
1767 TEST_EQUAL( status, expected_status );
1768 TEST_ASSERT( output_length <= output_size );
1769
1770exit:
1771 mbedtls_free( output );
1772 PSA_DONE( );
1773}
1774/* END_CASE */
1775
1776/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001777void hash_compare_fail( int alg_arg, data_t *input,
1778 data_t *reference_hash,
1779 int expected_status_arg )
1780{
1781 psa_algorithm_t alg = alg_arg;
1782 psa_status_t expected_status = expected_status_arg;
1783 psa_status_t status;
1784
1785 PSA_ASSERT( psa_crypto_init( ) );
1786
1787 status = psa_hash_compare( alg, input->x, input->len,
1788 reference_hash->x, reference_hash->len );
1789 TEST_EQUAL( status, expected_status );
1790
1791exit:
1792 PSA_DONE( );
1793}
1794/* END_CASE */
1795
1796/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001797void hash_compute_compare( int alg_arg, data_t *input,
1798 data_t *expected_output )
1799{
1800 psa_algorithm_t alg = alg_arg;
1801 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1802 size_t output_length = INVALID_EXPORT_LENGTH;
1803 size_t i;
1804
1805 PSA_ASSERT( psa_crypto_init( ) );
1806
1807 /* Compute with tight buffer */
1808 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001809 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001810 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001811 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001812 ASSERT_COMPARE( output, output_length,
1813 expected_output->x, expected_output->len );
1814
1815 /* Compute with larger buffer */
1816 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1817 output, sizeof( output ),
1818 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001819 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001820 ASSERT_COMPARE( output, output_length,
1821 expected_output->x, expected_output->len );
1822
1823 /* Compare with correct hash */
1824 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1825 output, output_length ) );
1826
1827 /* Compare with trailing garbage */
1828 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1829 output, output_length + 1 ),
1830 PSA_ERROR_INVALID_SIGNATURE );
1831
1832 /* Compare with truncated hash */
1833 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1834 output, output_length - 1 ),
1835 PSA_ERROR_INVALID_SIGNATURE );
1836
1837 /* Compare with corrupted value */
1838 for( i = 0; i < output_length; i++ )
1839 {
Chris Jones9634bb12021-01-20 15:56:42 +00001840 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001841 output[i] ^= 1;
1842 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1843 output, output_length ),
1844 PSA_ERROR_INVALID_SIGNATURE );
1845 output[i] ^= 1;
1846 }
1847
1848exit:
1849 PSA_DONE( );
1850}
1851/* END_CASE */
1852
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001853/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001854void hash_bad_order( )
1855{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001856 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001857 unsigned char input[] = "";
1858 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001859 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001860 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1861 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1862 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001863 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001864 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001865 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001866
Gilles Peskine8817f612018-12-18 00:18:46 +01001867 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001868
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001869 /* Call setup twice in a row. */
1870 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1871 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1872 PSA_ERROR_BAD_STATE );
1873 PSA_ASSERT( psa_hash_abort( &operation ) );
1874
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001875 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001876 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001877 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001878 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001879
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001880 /* Call update after finish. */
1881 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1882 PSA_ASSERT( psa_hash_finish( &operation,
1883 hash, sizeof( hash ), &hash_len ) );
1884 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001885 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001886 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001887
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001888 /* Call verify without calling setup beforehand. */
1889 TEST_EQUAL( psa_hash_verify( &operation,
1890 valid_hash, sizeof( valid_hash ) ),
1891 PSA_ERROR_BAD_STATE );
1892 PSA_ASSERT( psa_hash_abort( &operation ) );
1893
1894 /* Call verify after finish. */
1895 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1896 PSA_ASSERT( psa_hash_finish( &operation,
1897 hash, sizeof( hash ), &hash_len ) );
1898 TEST_EQUAL( psa_hash_verify( &operation,
1899 valid_hash, sizeof( valid_hash ) ),
1900 PSA_ERROR_BAD_STATE );
1901 PSA_ASSERT( psa_hash_abort( &operation ) );
1902
1903 /* Call verify twice in a row. */
1904 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1905 PSA_ASSERT( psa_hash_verify( &operation,
1906 valid_hash, sizeof( valid_hash ) ) );
1907 TEST_EQUAL( psa_hash_verify( &operation,
1908 valid_hash, sizeof( valid_hash ) ),
1909 PSA_ERROR_BAD_STATE );
1910 PSA_ASSERT( psa_hash_abort( &operation ) );
1911
1912 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001913 TEST_EQUAL( psa_hash_finish( &operation,
1914 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001915 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001916 PSA_ASSERT( psa_hash_abort( &operation ) );
1917
1918 /* Call finish twice in a row. */
1919 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1920 PSA_ASSERT( psa_hash_finish( &operation,
1921 hash, sizeof( hash ), &hash_len ) );
1922 TEST_EQUAL( psa_hash_finish( &operation,
1923 hash, sizeof( hash ), &hash_len ),
1924 PSA_ERROR_BAD_STATE );
1925 PSA_ASSERT( psa_hash_abort( &operation ) );
1926
1927 /* Call finish after calling verify. */
1928 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1929 PSA_ASSERT( psa_hash_verify( &operation,
1930 valid_hash, sizeof( valid_hash ) ) );
1931 TEST_EQUAL( psa_hash_finish( &operation,
1932 hash, sizeof( hash ), &hash_len ),
1933 PSA_ERROR_BAD_STATE );
1934 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001935
1936exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001937 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001938}
1939/* END_CASE */
1940
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001941/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001942void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001943{
1944 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001945 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1946 * appended to it */
1947 unsigned char hash[] = {
1948 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1949 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1950 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001951 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001952 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001953
Gilles Peskine8817f612018-12-18 00:18:46 +01001954 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001955
itayzafrir27e69452018-11-01 14:26:34 +02001956 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001957 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001958 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001959 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001960
itayzafrir27e69452018-11-01 14:26:34 +02001961 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001962 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001963 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001964 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001965
itayzafrir27e69452018-11-01 14:26:34 +02001966 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001968 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001969 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001970
itayzafrirec93d302018-10-18 18:01:10 +03001971exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001972 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001973}
1974/* END_CASE */
1975
Ronald Cronee414c72021-03-18 18:50:08 +01001976/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001977void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001978{
1979 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001980 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001981 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001982 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001983 size_t hash_len;
1984
Gilles Peskine8817f612018-12-18 00:18:46 +01001985 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001986
itayzafrir58028322018-10-25 10:22:01 +03001987 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001988 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001989 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001990 hash, expected_size - 1, &hash_len ),
1991 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001992
1993exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001994 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001995}
1996/* END_CASE */
1997
Ronald Cronee414c72021-03-18 18:50:08 +01001998/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001999void hash_clone_source_state( )
2000{
2001 psa_algorithm_t alg = PSA_ALG_SHA_256;
2002 unsigned char hash[PSA_HASH_MAX_SIZE];
2003 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2004 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2005 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2006 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2007 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2008 size_t hash_len;
2009
2010 PSA_ASSERT( psa_crypto_init( ) );
2011 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2012
2013 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2014 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2015 PSA_ASSERT( psa_hash_finish( &op_finished,
2016 hash, sizeof( hash ), &hash_len ) );
2017 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2018 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2019
2020 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2021 PSA_ERROR_BAD_STATE );
2022
2023 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2024 PSA_ASSERT( psa_hash_finish( &op_init,
2025 hash, sizeof( hash ), &hash_len ) );
2026 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2027 PSA_ASSERT( psa_hash_finish( &op_finished,
2028 hash, sizeof( hash ), &hash_len ) );
2029 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2030 PSA_ASSERT( psa_hash_finish( &op_aborted,
2031 hash, sizeof( hash ), &hash_len ) );
2032
2033exit:
2034 psa_hash_abort( &op_source );
2035 psa_hash_abort( &op_init );
2036 psa_hash_abort( &op_setup );
2037 psa_hash_abort( &op_finished );
2038 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002039 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002040}
2041/* END_CASE */
2042
Ronald Cronee414c72021-03-18 18:50:08 +01002043/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002044void hash_clone_target_state( )
2045{
2046 psa_algorithm_t alg = PSA_ALG_SHA_256;
2047 unsigned char hash[PSA_HASH_MAX_SIZE];
2048 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2049 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2050 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2051 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2052 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2053 size_t hash_len;
2054
2055 PSA_ASSERT( psa_crypto_init( ) );
2056
2057 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2058 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2059 PSA_ASSERT( psa_hash_finish( &op_finished,
2060 hash, sizeof( hash ), &hash_len ) );
2061 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2062 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2063
2064 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2065 PSA_ASSERT( psa_hash_finish( &op_target,
2066 hash, sizeof( hash ), &hash_len ) );
2067
2068 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2069 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2070 PSA_ERROR_BAD_STATE );
2071 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2072 PSA_ERROR_BAD_STATE );
2073
2074exit:
2075 psa_hash_abort( &op_target );
2076 psa_hash_abort( &op_init );
2077 psa_hash_abort( &op_setup );
2078 psa_hash_abort( &op_finished );
2079 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002080 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002081}
2082/* END_CASE */
2083
itayzafrir58028322018-10-25 10:22:01 +03002084/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002085void mac_operation_init( )
2086{
Jaeden Amero252ef282019-02-15 14:05:35 +00002087 const uint8_t input[1] = { 0 };
2088
Jaeden Amero769ce272019-01-04 11:48:03 +00002089 /* Test each valid way of initializing the object, except for `= {0}`, as
2090 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2091 * though it's OK by the C standard. We could test for this, but we'd need
2092 * to supress the Clang warning for the test. */
2093 psa_mac_operation_t func = psa_mac_operation_init( );
2094 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2095 psa_mac_operation_t zero;
2096
2097 memset( &zero, 0, sizeof( zero ) );
2098
Jaeden Amero252ef282019-02-15 14:05:35 +00002099 /* A freshly-initialized MAC operation should not be usable. */
2100 TEST_EQUAL( psa_mac_update( &func,
2101 input, sizeof( input ) ),
2102 PSA_ERROR_BAD_STATE );
2103 TEST_EQUAL( psa_mac_update( &init,
2104 input, sizeof( input ) ),
2105 PSA_ERROR_BAD_STATE );
2106 TEST_EQUAL( psa_mac_update( &zero,
2107 input, sizeof( input ) ),
2108 PSA_ERROR_BAD_STATE );
2109
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002110 /* A default MAC operation should be abortable without error. */
2111 PSA_ASSERT( psa_mac_abort( &func ) );
2112 PSA_ASSERT( psa_mac_abort( &init ) );
2113 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002114}
2115/* END_CASE */
2116
2117/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002118void mac_setup( int key_type_arg,
2119 data_t *key,
2120 int alg_arg,
2121 int expected_status_arg )
2122{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002123 psa_key_type_t key_type = key_type_arg;
2124 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002125 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002126 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002127 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2128#if defined(KNOWN_SUPPORTED_MAC_ALG)
2129 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2130#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002131
Gilles Peskine8817f612018-12-18 00:18:46 +01002132 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002133
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002134 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2135 &operation, &status ) )
2136 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002137 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002138
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002139 /* The operation object should be reusable. */
2140#if defined(KNOWN_SUPPORTED_MAC_ALG)
2141 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2142 smoke_test_key_data,
2143 sizeof( smoke_test_key_data ),
2144 KNOWN_SUPPORTED_MAC_ALG,
2145 &operation, &status ) )
2146 goto exit;
2147 TEST_EQUAL( status, PSA_SUCCESS );
2148#endif
2149
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002150exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002151 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002152}
2153/* END_CASE */
2154
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002155/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00002156void mac_bad_order( )
2157{
Ronald Cron5425a212020-08-04 14:58:35 +02002158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002159 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2160 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002161 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002162 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2163 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2164 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002166 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2167 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2168 size_t sign_mac_length = 0;
2169 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2170 const uint8_t verify_mac[] = {
2171 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2172 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2173 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2174
2175 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002176 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002177 psa_set_key_algorithm( &attributes, alg );
2178 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002179
Ronald Cron5425a212020-08-04 14:58:35 +02002180 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2181 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002182
Jaeden Amero252ef282019-02-15 14:05:35 +00002183 /* Call update without calling setup beforehand. */
2184 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2185 PSA_ERROR_BAD_STATE );
2186 PSA_ASSERT( psa_mac_abort( &operation ) );
2187
2188 /* Call sign finish without calling setup beforehand. */
2189 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2190 &sign_mac_length),
2191 PSA_ERROR_BAD_STATE );
2192 PSA_ASSERT( psa_mac_abort( &operation ) );
2193
2194 /* Call verify finish without calling setup beforehand. */
2195 TEST_EQUAL( psa_mac_verify_finish( &operation,
2196 verify_mac, sizeof( verify_mac ) ),
2197 PSA_ERROR_BAD_STATE );
2198 PSA_ASSERT( psa_mac_abort( &operation ) );
2199
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002200 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002201 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2202 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002203 PSA_ERROR_BAD_STATE );
2204 PSA_ASSERT( psa_mac_abort( &operation ) );
2205
Jaeden Amero252ef282019-02-15 14:05:35 +00002206 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002207 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002208 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2209 PSA_ASSERT( psa_mac_sign_finish( &operation,
2210 sign_mac, sizeof( sign_mac ),
2211 &sign_mac_length ) );
2212 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2213 PSA_ERROR_BAD_STATE );
2214 PSA_ASSERT( psa_mac_abort( &operation ) );
2215
2216 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002217 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002218 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2219 PSA_ASSERT( psa_mac_verify_finish( &operation,
2220 verify_mac, sizeof( verify_mac ) ) );
2221 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2222 PSA_ERROR_BAD_STATE );
2223 PSA_ASSERT( psa_mac_abort( &operation ) );
2224
2225 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002226 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002227 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2228 PSA_ASSERT( psa_mac_sign_finish( &operation,
2229 sign_mac, sizeof( sign_mac ),
2230 &sign_mac_length ) );
2231 TEST_EQUAL( psa_mac_sign_finish( &operation,
2232 sign_mac, sizeof( sign_mac ),
2233 &sign_mac_length ),
2234 PSA_ERROR_BAD_STATE );
2235 PSA_ASSERT( psa_mac_abort( &operation ) );
2236
2237 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002238 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002239 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2240 PSA_ASSERT( psa_mac_verify_finish( &operation,
2241 verify_mac, sizeof( verify_mac ) ) );
2242 TEST_EQUAL( psa_mac_verify_finish( &operation,
2243 verify_mac, sizeof( verify_mac ) ),
2244 PSA_ERROR_BAD_STATE );
2245 PSA_ASSERT( psa_mac_abort( &operation ) );
2246
2247 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002248 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002249 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2250 TEST_EQUAL( psa_mac_verify_finish( &operation,
2251 verify_mac, sizeof( verify_mac ) ),
2252 PSA_ERROR_BAD_STATE );
2253 PSA_ASSERT( psa_mac_abort( &operation ) );
2254
2255 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002256 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002257 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2258 TEST_EQUAL( psa_mac_sign_finish( &operation,
2259 sign_mac, sizeof( sign_mac ),
2260 &sign_mac_length ),
2261 PSA_ERROR_BAD_STATE );
2262 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002263
Ronald Cron5425a212020-08-04 14:58:35 +02002264 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002265
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002266exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002267 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002268}
2269/* END_CASE */
2270
2271/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002272void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002273 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002274 int alg_arg,
2275 data_t *input,
2276 data_t *expected_mac )
2277{
Ronald Cron5425a212020-08-04 14:58:35 +02002278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002279 psa_key_type_t key_type = key_type_arg;
2280 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002281 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002283 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002284 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002285 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002286 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002287 const size_t output_sizes_to_test[] = {
2288 0,
2289 1,
2290 expected_mac->len - 1,
2291 expected_mac->len,
2292 expected_mac->len + 1,
2293 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002294
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002295 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002296 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002297 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002298
Gilles Peskine8817f612018-12-18 00:18:46 +01002299 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002300
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002301 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002302 psa_set_key_algorithm( &attributes, alg );
2303 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002304
Ronald Cron5425a212020-08-04 14:58:35 +02002305 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2306 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002307
Gilles Peskine8b356b52020-08-25 23:44:59 +02002308 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2309 {
2310 const size_t output_size = output_sizes_to_test[i];
2311 psa_status_t expected_status =
2312 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2313 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002314
Chris Jones9634bb12021-01-20 15:56:42 +00002315 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002316 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002317
Gilles Peskine8b356b52020-08-25 23:44:59 +02002318 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002319 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002320 PSA_ASSERT( psa_mac_update( &operation,
2321 input->x, input->len ) );
2322 TEST_EQUAL( psa_mac_sign_finish( &operation,
2323 actual_mac, output_size,
2324 &mac_length ),
2325 expected_status );
2326 PSA_ASSERT( psa_mac_abort( &operation ) );
2327
2328 if( expected_status == PSA_SUCCESS )
2329 {
2330 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2331 actual_mac, mac_length );
2332 }
2333 mbedtls_free( actual_mac );
2334 actual_mac = NULL;
2335 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002336
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002337exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002338 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002339 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002340 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002341 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002342}
2343/* END_CASE */
2344
2345/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002346void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002347 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002348 int alg_arg,
2349 data_t *input,
2350 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002351{
Ronald Cron5425a212020-08-04 14:58:35 +02002352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002353 psa_key_type_t key_type = key_type_arg;
2354 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002355 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002357 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002358
Gilles Peskine69c12672018-06-28 00:07:19 +02002359 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2360
Gilles Peskine8817f612018-12-18 00:18:46 +01002361 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002362
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002364 psa_set_key_algorithm( &attributes, alg );
2365 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002366
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2368 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002369
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002370 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002371 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002372 PSA_ASSERT( psa_mac_update( &operation,
2373 input->x, input->len ) );
2374 PSA_ASSERT( psa_mac_verify_finish( &operation,
2375 expected_mac->x,
2376 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002377
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002378 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002379 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002380 PSA_ASSERT( psa_mac_update( &operation,
2381 input->x, input->len ) );
2382 TEST_EQUAL( psa_mac_verify_finish( &operation,
2383 expected_mac->x,
2384 expected_mac->len - 1 ),
2385 PSA_ERROR_INVALID_SIGNATURE );
2386
2387 /* Test a MAC that's too long. */
2388 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2389 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002390 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002391 PSA_ASSERT( psa_mac_update( &operation,
2392 input->x, input->len ) );
2393 TEST_EQUAL( psa_mac_verify_finish( &operation,
2394 perturbed_mac,
2395 expected_mac->len + 1 ),
2396 PSA_ERROR_INVALID_SIGNATURE );
2397
2398 /* Test changing one byte. */
2399 for( size_t i = 0; i < expected_mac->len; i++ )
2400 {
Chris Jones9634bb12021-01-20 15:56:42 +00002401 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002402 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002403 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002404 PSA_ASSERT( psa_mac_update( &operation,
2405 input->x, input->len ) );
2406 TEST_EQUAL( psa_mac_verify_finish( &operation,
2407 perturbed_mac,
2408 expected_mac->len ),
2409 PSA_ERROR_INVALID_SIGNATURE );
2410 perturbed_mac[i] ^= 1;
2411 }
2412
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002414 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002415 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002416 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002417 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002418}
2419/* END_CASE */
2420
2421/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002422void cipher_operation_init( )
2423{
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 const uint8_t input[1] = { 0 };
2425 unsigned char output[1] = { 0 };
2426 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002427 /* Test each valid way of initializing the object, except for `= {0}`, as
2428 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2429 * though it's OK by the C standard. We could test for this, but we'd need
2430 * to supress the Clang warning for the test. */
2431 psa_cipher_operation_t func = psa_cipher_operation_init( );
2432 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2433 psa_cipher_operation_t zero;
2434
2435 memset( &zero, 0, sizeof( zero ) );
2436
Jaeden Ameroab439972019-02-15 14:12:05 +00002437 /* A freshly-initialized cipher operation should not be usable. */
2438 TEST_EQUAL( psa_cipher_update( &func,
2439 input, sizeof( input ),
2440 output, sizeof( output ),
2441 &output_length ),
2442 PSA_ERROR_BAD_STATE );
2443 TEST_EQUAL( psa_cipher_update( &init,
2444 input, sizeof( input ),
2445 output, sizeof( output ),
2446 &output_length ),
2447 PSA_ERROR_BAD_STATE );
2448 TEST_EQUAL( psa_cipher_update( &zero,
2449 input, sizeof( input ),
2450 output, sizeof( output ),
2451 &output_length ),
2452 PSA_ERROR_BAD_STATE );
2453
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002454 /* A default cipher operation should be abortable without error. */
2455 PSA_ASSERT( psa_cipher_abort( &func ) );
2456 PSA_ASSERT( psa_cipher_abort( &init ) );
2457 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002458}
2459/* END_CASE */
2460
2461/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002462void cipher_setup( int key_type_arg,
2463 data_t *key,
2464 int alg_arg,
2465 int expected_status_arg )
2466{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002467 psa_key_type_t key_type = key_type_arg;
2468 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002469 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002470 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002472#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002473 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2474#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002475
Gilles Peskine8817f612018-12-18 00:18:46 +01002476 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002477
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002478 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2479 &operation, &status ) )
2480 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002481 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002482
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002483 /* The operation object should be reusable. */
2484#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2485 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2486 smoke_test_key_data,
2487 sizeof( smoke_test_key_data ),
2488 KNOWN_SUPPORTED_CIPHER_ALG,
2489 &operation, &status ) )
2490 goto exit;
2491 TEST_EQUAL( status, PSA_SUCCESS );
2492#endif
2493
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002494exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002495 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002496 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002497}
2498/* END_CASE */
2499
Ronald Cronee414c72021-03-18 18:50:08 +01002500/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002501void cipher_bad_order( )
2502{
Ronald Cron5425a212020-08-04 14:58:35 +02002503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002504 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2505 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002507 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002508 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002509 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002510 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2511 0xaa, 0xaa, 0xaa, 0xaa };
2512 const uint8_t text[] = {
2513 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2514 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002515 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002516 size_t length = 0;
2517
2518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002519 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2520 psa_set_key_algorithm( &attributes, alg );
2521 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002522 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2523 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002524
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002525 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002526 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2527 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002528 PSA_ERROR_BAD_STATE );
2529 PSA_ASSERT( psa_cipher_abort( &operation ) );
2530
2531 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002532 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2533 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002534 PSA_ERROR_BAD_STATE );
2535 PSA_ASSERT( psa_cipher_abort( &operation ) );
2536
Jaeden Ameroab439972019-02-15 14:12:05 +00002537 /* Generate an IV without calling setup beforehand. */
2538 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2539 buffer, sizeof( buffer ),
2540 &length ),
2541 PSA_ERROR_BAD_STATE );
2542 PSA_ASSERT( psa_cipher_abort( &operation ) );
2543
2544 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002545 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002546 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2547 buffer, sizeof( buffer ),
2548 &length ) );
2549 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2550 buffer, sizeof( buffer ),
2551 &length ),
2552 PSA_ERROR_BAD_STATE );
2553 PSA_ASSERT( psa_cipher_abort( &operation ) );
2554
2555 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002556 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002557 PSA_ASSERT( psa_cipher_set_iv( &operation,
2558 iv, sizeof( iv ) ) );
2559 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2560 buffer, sizeof( buffer ),
2561 &length ),
2562 PSA_ERROR_BAD_STATE );
2563 PSA_ASSERT( psa_cipher_abort( &operation ) );
2564
2565 /* Set an IV without calling setup beforehand. */
2566 TEST_EQUAL( psa_cipher_set_iv( &operation,
2567 iv, sizeof( iv ) ),
2568 PSA_ERROR_BAD_STATE );
2569 PSA_ASSERT( psa_cipher_abort( &operation ) );
2570
2571 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002572 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002573 PSA_ASSERT( psa_cipher_set_iv( &operation,
2574 iv, sizeof( iv ) ) );
2575 TEST_EQUAL( psa_cipher_set_iv( &operation,
2576 iv, sizeof( iv ) ),
2577 PSA_ERROR_BAD_STATE );
2578 PSA_ASSERT( psa_cipher_abort( &operation ) );
2579
2580 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002581 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002582 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2583 buffer, sizeof( buffer ),
2584 &length ) );
2585 TEST_EQUAL( psa_cipher_set_iv( &operation,
2586 iv, sizeof( iv ) ),
2587 PSA_ERROR_BAD_STATE );
2588 PSA_ASSERT( psa_cipher_abort( &operation ) );
2589
2590 /* Call update without calling setup beforehand. */
2591 TEST_EQUAL( psa_cipher_update( &operation,
2592 text, sizeof( text ),
2593 buffer, sizeof( buffer ),
2594 &length ),
2595 PSA_ERROR_BAD_STATE );
2596 PSA_ASSERT( psa_cipher_abort( &operation ) );
2597
2598 /* Call update without an IV where an IV is required. */
2599 TEST_EQUAL( psa_cipher_update( &operation,
2600 text, sizeof( text ),
2601 buffer, sizeof( buffer ),
2602 &length ),
2603 PSA_ERROR_BAD_STATE );
2604 PSA_ASSERT( psa_cipher_abort( &operation ) );
2605
2606 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002607 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002608 PSA_ASSERT( psa_cipher_set_iv( &operation,
2609 iv, sizeof( iv ) ) );
2610 PSA_ASSERT( psa_cipher_finish( &operation,
2611 buffer, sizeof( buffer ), &length ) );
2612 TEST_EQUAL( psa_cipher_update( &operation,
2613 text, sizeof( text ),
2614 buffer, sizeof( buffer ),
2615 &length ),
2616 PSA_ERROR_BAD_STATE );
2617 PSA_ASSERT( psa_cipher_abort( &operation ) );
2618
2619 /* Call finish without calling setup beforehand. */
2620 TEST_EQUAL( psa_cipher_finish( &operation,
2621 buffer, sizeof( buffer ), &length ),
2622 PSA_ERROR_BAD_STATE );
2623 PSA_ASSERT( psa_cipher_abort( &operation ) );
2624
2625 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002626 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002627 /* Not calling update means we are encrypting an empty buffer, which is OK
2628 * for cipher modes with padding. */
2629 TEST_EQUAL( psa_cipher_finish( &operation,
2630 buffer, sizeof( buffer ), &length ),
2631 PSA_ERROR_BAD_STATE );
2632 PSA_ASSERT( psa_cipher_abort( &operation ) );
2633
2634 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002635 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002636 PSA_ASSERT( psa_cipher_set_iv( &operation,
2637 iv, sizeof( iv ) ) );
2638 PSA_ASSERT( psa_cipher_finish( &operation,
2639 buffer, sizeof( buffer ), &length ) );
2640 TEST_EQUAL( psa_cipher_finish( &operation,
2641 buffer, sizeof( buffer ), &length ),
2642 PSA_ERROR_BAD_STATE );
2643 PSA_ASSERT( psa_cipher_abort( &operation ) );
2644
Ronald Cron5425a212020-08-04 14:58:35 +02002645 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002646
Jaeden Ameroab439972019-02-15 14:12:05 +00002647exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002648 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002649 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002650}
2651/* END_CASE */
2652
2653/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002655 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002656 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002657 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002658{
Ronald Cron5425a212020-08-04 14:58:35 +02002659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002660 psa_status_t status;
2661 psa_key_type_t key_type = key_type_arg;
2662 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002663 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002664 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002665 size_t output_buffer_size = 0;
2666 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002667 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002668 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002670
Gilles Peskine8817f612018-12-18 00:18:46 +01002671 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002672
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002673 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2674 psa_set_key_algorithm( &attributes, alg );
2675 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002676
Ronald Cron5425a212020-08-04 14:58:35 +02002677 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2678 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002679
Ronald Cron5425a212020-08-04 14:58:35 +02002680 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002681
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002682 if( iv->len > 0 )
2683 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002684 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002685 }
2686
gabor-mezei-armceface22021-01-21 12:26:17 +01002687 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2688 TEST_ASSERT( output_buffer_size <=
2689 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002690 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002691
Gilles Peskine8817f612018-12-18 00:18:46 +01002692 PSA_ASSERT( psa_cipher_update( &operation,
2693 input->x, input->len,
2694 output, output_buffer_size,
2695 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002696 TEST_ASSERT( function_output_length <=
2697 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2698 TEST_ASSERT( function_output_length <=
2699 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002700 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002701
Gilles Peskine50e586b2018-06-08 14:28:46 +02002702 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002703 ( output_buffer_size == 0 ? NULL :
2704 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002705 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002707 TEST_ASSERT( function_output_length <=
2708 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2709 TEST_ASSERT( function_output_length <=
2710 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002711 total_output_length += function_output_length;
2712
Gilles Peskinefe11b722018-12-18 00:24:04 +01002713 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002714 if( expected_status == PSA_SUCCESS )
2715 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002716 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002717 ASSERT_COMPARE( expected_output->x, expected_output->len,
2718 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002719 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002720
Gilles Peskine50e586b2018-06-08 14:28:46 +02002721exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002722 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002723 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002724 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002725 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002726}
2727/* END_CASE */
2728
2729/* BEGIN_CASE */
2730void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002731 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002732 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002733 int first_part_size_arg,
2734 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002735 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736{
Ronald Cron5425a212020-08-04 14:58:35 +02002737 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738 psa_key_type_t key_type = key_type_arg;
2739 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002740 size_t first_part_size = first_part_size_arg;
2741 size_t output1_length = output1_length_arg;
2742 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002743 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002744 size_t output_buffer_size = 0;
2745 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002746 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002747 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002749
Gilles Peskine8817f612018-12-18 00:18:46 +01002750 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002751
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002752 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2753 psa_set_key_algorithm( &attributes, alg );
2754 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002755
Ronald Cron5425a212020-08-04 14:58:35 +02002756 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2757 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002758
Ronald Cron5425a212020-08-04 14:58:35 +02002759 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002760
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002761 if( iv->len > 0 )
2762 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002763 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002764 }
2765
gabor-mezei-armceface22021-01-21 12:26:17 +01002766 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2767 TEST_ASSERT( output_buffer_size <=
2768 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002769 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002770
Gilles Peskinee0866522019-02-19 19:44:00 +01002771 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002772 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2773 output, output_buffer_size,
2774 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002775 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002776 TEST_ASSERT( function_output_length <=
2777 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2778 TEST_ASSERT( function_output_length <=
2779 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002780 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002781
Gilles Peskine8817f612018-12-18 00:18:46 +01002782 PSA_ASSERT( psa_cipher_update( &operation,
2783 input->x + first_part_size,
2784 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002785 ( output_buffer_size == 0 ? NULL :
2786 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002787 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002788 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002789 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002790 TEST_ASSERT( function_output_length <=
2791 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2792 alg,
2793 input->len - first_part_size ) );
2794 TEST_ASSERT( function_output_length <=
2795 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002796 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002797
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002799 ( output_buffer_size == 0 ? NULL :
2800 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002801 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002803 TEST_ASSERT( function_output_length <=
2804 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2805 TEST_ASSERT( function_output_length <=
2806 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002807 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002810 ASSERT_COMPARE( expected_output->x, expected_output->len,
2811 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812
2813exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002814 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002815 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002816 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002817 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818}
2819/* END_CASE */
2820
2821/* BEGIN_CASE */
2822void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002823 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002824 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002825 int first_part_size_arg,
2826 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002827 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828{
Ronald Cron5425a212020-08-04 14:58:35 +02002829 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830 psa_key_type_t key_type = key_type_arg;
2831 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002832 size_t first_part_size = first_part_size_arg;
2833 size_t output1_length = output1_length_arg;
2834 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002835 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002836 size_t output_buffer_size = 0;
2837 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002838 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002839 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002841
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002843
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002844 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2845 psa_set_key_algorithm( &attributes, alg );
2846 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002847
Ronald Cron5425a212020-08-04 14:58:35 +02002848 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2849 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002850
Ronald Cron5425a212020-08-04 14:58:35 +02002851 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002852
Steven Cooreman177deba2020-09-07 17:14:14 +02002853 if( iv->len > 0 )
2854 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002855 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002856 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002857
gabor-mezei-armceface22021-01-21 12:26:17 +01002858 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2859 TEST_ASSERT( output_buffer_size <=
2860 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002861 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002862
Gilles Peskinee0866522019-02-19 19:44:00 +01002863 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002864 PSA_ASSERT( psa_cipher_update( &operation,
2865 input->x, first_part_size,
2866 output, output_buffer_size,
2867 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002868 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002869 TEST_ASSERT( function_output_length <=
2870 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2871 TEST_ASSERT( function_output_length <=
2872 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002873 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002874
Gilles Peskine8817f612018-12-18 00:18:46 +01002875 PSA_ASSERT( psa_cipher_update( &operation,
2876 input->x + first_part_size,
2877 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002878 ( output_buffer_size == 0 ? NULL :
2879 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002880 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002881 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002882 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002883 TEST_ASSERT( function_output_length <=
2884 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2885 alg,
2886 input->len - first_part_size ) );
2887 TEST_ASSERT( function_output_length <=
2888 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002889 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002890
Gilles Peskine8817f612018-12-18 00:18:46 +01002891 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002892 ( output_buffer_size == 0 ? NULL :
2893 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002894 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002895 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002896 TEST_ASSERT( function_output_length <=
2897 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2898 TEST_ASSERT( function_output_length <=
2899 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002900 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002901 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002902
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002903 ASSERT_COMPARE( expected_output->x, expected_output->len,
2904 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002905
2906exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002907 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002908 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002909 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002910 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002911}
2912/* END_CASE */
2913
Gilles Peskine50e586b2018-06-08 14:28:46 +02002914/* BEGIN_CASE */
2915void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002916 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002917 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002918 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002919{
Ronald Cron5425a212020-08-04 14:58:35 +02002920 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921 psa_status_t status;
2922 psa_key_type_t key_type = key_type_arg;
2923 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002924 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002925 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926 size_t output_buffer_size = 0;
2927 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002928 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002929 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002931
Gilles Peskine8817f612018-12-18 00:18:46 +01002932 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002933
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2935 psa_set_key_algorithm( &attributes, alg );
2936 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002937
Ronald Cron5425a212020-08-04 14:58:35 +02002938 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2939 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002940
Ronald Cron5425a212020-08-04 14:58:35 +02002941 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002942
Steven Cooreman177deba2020-09-07 17:14:14 +02002943 if( iv->len > 0 )
2944 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002945 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002946 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002947
gabor-mezei-armceface22021-01-21 12:26:17 +01002948 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2949 TEST_ASSERT( output_buffer_size <=
2950 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002951 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002952
Gilles Peskine8817f612018-12-18 00:18:46 +01002953 PSA_ASSERT( psa_cipher_update( &operation,
2954 input->x, input->len,
2955 output, output_buffer_size,
2956 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002957 TEST_ASSERT( function_output_length <=
2958 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2959 TEST_ASSERT( function_output_length <=
2960 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002961 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002962
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002964 ( output_buffer_size == 0 ? NULL :
2965 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002966 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002968 TEST_ASSERT( function_output_length <=
2969 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2970 TEST_ASSERT( function_output_length <=
2971 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002972 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002973 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974
2975 if( expected_status == PSA_SUCCESS )
2976 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002977 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002978 ASSERT_COMPARE( expected_output->x, expected_output->len,
2979 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002980 }
2981
Gilles Peskine50e586b2018-06-08 14:28:46 +02002982exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002983 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002984 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002985 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002986 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002987}
2988/* END_CASE */
2989
Gilles Peskine50e586b2018-06-08 14:28:46 +02002990/* BEGIN_CASE */
2991void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002992 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002993 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002994{
Ronald Cron5425a212020-08-04 14:58:35 +02002995 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002996 psa_key_type_t key_type = key_type_arg;
2997 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002998 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002999 size_t iv_size = 16;
3000 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003001 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003002 size_t output1_size = 0;
3003 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003004 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003005 size_t output2_size = 0;
3006 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003007 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003008 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3009 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003011
Gilles Peskine8817f612018-12-18 00:18:46 +01003012 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003013
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003014 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3015 psa_set_key_algorithm( &attributes, alg );
3016 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003017
Ronald Cron5425a212020-08-04 14:58:35 +02003018 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3019 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003020
Ronald Cron5425a212020-08-04 14:58:35 +02003021 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3022 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003023
Steven Cooreman177deba2020-09-07 17:14:14 +02003024 if( alg != PSA_ALG_ECB_NO_PADDING )
3025 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003026 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3027 iv, iv_size,
3028 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003029 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003030 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3031 TEST_ASSERT( output1_size <=
3032 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003033 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003034
Gilles Peskine8817f612018-12-18 00:18:46 +01003035 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3036 output1, output1_size,
3037 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003038 TEST_ASSERT( output1_length <=
3039 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3040 TEST_ASSERT( output1_length <=
3041 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3042
Gilles Peskine8817f612018-12-18 00:18:46 +01003043 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003044 output1 + output1_length,
3045 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003046 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003047 TEST_ASSERT( function_output_length <=
3048 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3049 TEST_ASSERT( function_output_length <=
3050 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003051
Gilles Peskine048b7f02018-06-08 14:20:49 +02003052 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003053
Gilles Peskine8817f612018-12-18 00:18:46 +01003054 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003055
3056 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003057 TEST_ASSERT( output2_size <=
3058 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3059 TEST_ASSERT( output2_size <=
3060 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003061 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003062
Steven Cooreman177deba2020-09-07 17:14:14 +02003063 if( iv_length > 0 )
3064 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003065 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3066 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003067 }
3068
Gilles Peskine8817f612018-12-18 00:18:46 +01003069 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3070 output2, output2_size,
3071 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003072 TEST_ASSERT( output2_length <=
3073 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3074 TEST_ASSERT( output2_length <=
3075 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3076
Gilles Peskine048b7f02018-06-08 14:20:49 +02003077 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003078 PSA_ASSERT( psa_cipher_finish( &operation2,
3079 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003080 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003081 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003082 TEST_ASSERT( function_output_length <=
3083 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3084 TEST_ASSERT( function_output_length <=
3085 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003086
Gilles Peskine048b7f02018-06-08 14:20:49 +02003087 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003090
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003091 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003092
3093exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003094 psa_cipher_abort( &operation1 );
3095 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003096 mbedtls_free( output1 );
3097 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003098 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003099 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003100}
3101/* END_CASE */
3102
3103/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003104void cipher_verify_output_multipart( int alg_arg,
3105 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003106 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003107 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003108 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003109{
Ronald Cron5425a212020-08-04 14:58:35 +02003110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003111 psa_key_type_t key_type = key_type_arg;
3112 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003113 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003114 unsigned char iv[16] = {0};
3115 size_t iv_size = 16;
3116 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003117 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003118 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003119 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003120 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003121 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003122 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003123 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003124 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3125 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003127
Gilles Peskine8817f612018-12-18 00:18:46 +01003128 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003130 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3131 psa_set_key_algorithm( &attributes, alg );
3132 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003133
Ronald Cron5425a212020-08-04 14:58:35 +02003134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3135 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003136
Ronald Cron5425a212020-08-04 14:58:35 +02003137 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3138 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003139
Steven Cooreman177deba2020-09-07 17:14:14 +02003140 if( alg != PSA_ALG_ECB_NO_PADDING )
3141 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003142 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3143 iv, iv_size,
3144 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003145 }
3146
gabor-mezei-armceface22021-01-21 12:26:17 +01003147 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3148 TEST_ASSERT( output1_buffer_size <=
3149 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003150 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003151
Gilles Peskinee0866522019-02-19 19:44:00 +01003152 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003153
Gilles Peskine8817f612018-12-18 00:18:46 +01003154 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3155 output1, output1_buffer_size,
3156 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003157 TEST_ASSERT( function_output_length <=
3158 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3159 TEST_ASSERT( function_output_length <=
3160 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003161 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003162
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 PSA_ASSERT( psa_cipher_update( &operation1,
3164 input->x + first_part_size,
3165 input->len - first_part_size,
3166 output1, output1_buffer_size,
3167 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003168 TEST_ASSERT( function_output_length <=
3169 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3170 alg,
3171 input->len - first_part_size ) );
3172 TEST_ASSERT( function_output_length <=
3173 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003174 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003175
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_cipher_finish( &operation1,
3177 output1 + output1_length,
3178 output1_buffer_size - output1_length,
3179 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003180 TEST_ASSERT( function_output_length <=
3181 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3182 TEST_ASSERT( function_output_length <=
3183 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003184 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003185
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003187
Gilles Peskine048b7f02018-06-08 14:20:49 +02003188 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003189 TEST_ASSERT( output2_buffer_size <=
3190 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3191 TEST_ASSERT( output2_buffer_size <=
3192 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003193 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003194
Steven Cooreman177deba2020-09-07 17:14:14 +02003195 if( iv_length > 0 )
3196 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003197 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3198 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003199 }
Moran Pekerded84402018-06-06 16:36:50 +03003200
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3202 output2, output2_buffer_size,
3203 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003204 TEST_ASSERT( function_output_length <=
3205 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3206 TEST_ASSERT( function_output_length <=
3207 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003208 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003209
Gilles Peskine8817f612018-12-18 00:18:46 +01003210 PSA_ASSERT( psa_cipher_update( &operation2,
3211 output1 + first_part_size,
3212 output1_length - first_part_size,
3213 output2, output2_buffer_size,
3214 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003215 TEST_ASSERT( function_output_length <=
3216 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3217 alg,
3218 output1_length - first_part_size ) );
3219 TEST_ASSERT( function_output_length <=
3220 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003221 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003222
Gilles Peskine8817f612018-12-18 00:18:46 +01003223 PSA_ASSERT( psa_cipher_finish( &operation2,
3224 output2 + output2_length,
3225 output2_buffer_size - output2_length,
3226 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003227 TEST_ASSERT( function_output_length <=
3228 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3229 TEST_ASSERT( function_output_length <=
3230 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003231 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003232
Gilles Peskine8817f612018-12-18 00:18:46 +01003233 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003234
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003235 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003236
3237exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003238 psa_cipher_abort( &operation1 );
3239 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003240 mbedtls_free( output1 );
3241 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003242 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003243 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003244}
3245/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003246
Gilles Peskine20035e32018-02-03 22:44:14 +01003247/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003248void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003249 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003250 data_t *nonce,
3251 data_t *additional_data,
3252 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003253 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254{
Ronald Cron5425a212020-08-04 14:58:35 +02003255 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003256 psa_key_type_t key_type = key_type_arg;
3257 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003258 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003259 unsigned char *output_data = NULL;
3260 size_t output_size = 0;
3261 size_t output_length = 0;
3262 unsigned char *output_data2 = NULL;
3263 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003264 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003265 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003266 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003267
Gilles Peskine8817f612018-12-18 00:18:46 +01003268 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003269
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003270 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3271 psa_set_key_algorithm( &attributes, alg );
3272 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003273
Gilles Peskine049c7532019-05-15 20:22:09 +02003274 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003275 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003276 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3277 key_bits = psa_get_key_bits( &attributes );
3278
3279 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3280 alg );
3281 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3282 * should be exact. */
3283 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3284 expected_result != PSA_ERROR_NOT_SUPPORTED )
3285 {
3286 TEST_EQUAL( output_size,
3287 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3288 TEST_ASSERT( output_size <=
3289 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3290 }
3291 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003292
Steven Cooremanf49478b2021-02-15 15:19:25 +01003293 status = psa_aead_encrypt( key, alg,
3294 nonce->x, nonce->len,
3295 additional_data->x,
3296 additional_data->len,
3297 input_data->x, input_data->len,
3298 output_data, output_size,
3299 &output_length );
3300
3301 /* If the operation is not supported, just skip and not fail in case the
3302 * encryption involves a common limitation of cryptography hardwares and
3303 * an alternative implementation. */
3304 if( status == PSA_ERROR_NOT_SUPPORTED )
3305 {
3306 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3307 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3308 }
3309
3310 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311
3312 if( PSA_SUCCESS == expected_result )
3313 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003314 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003315
Gilles Peskine003a4a92019-05-14 16:09:40 +02003316 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3317 * should be exact. */
3318 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003319 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003320
gabor-mezei-armceface22021-01-21 12:26:17 +01003321 TEST_ASSERT( input_data->len <=
3322 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3323
Ronald Cron5425a212020-08-04 14:58:35 +02003324 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003325 nonce->x, nonce->len,
3326 additional_data->x,
3327 additional_data->len,
3328 output_data, output_length,
3329 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003330 &output_length2 ),
3331 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003332
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003333 ASSERT_COMPARE( input_data->x, input_data->len,
3334 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003335 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003336
Gilles Peskinea1cac842018-06-11 19:33:02 +02003337exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003338 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339 mbedtls_free( output_data );
3340 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003341 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342}
3343/* END_CASE */
3344
3345/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003346void aead_encrypt( int key_type_arg, data_t *key_data,
3347 int alg_arg,
3348 data_t *nonce,
3349 data_t *additional_data,
3350 data_t *input_data,
3351 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003352{
Ronald Cron5425a212020-08-04 14:58:35 +02003353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003354 psa_key_type_t key_type = key_type_arg;
3355 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003356 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003357 unsigned char *output_data = NULL;
3358 size_t output_size = 0;
3359 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003361 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003362
Gilles Peskine8817f612018-12-18 00:18:46 +01003363 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003365 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3366 psa_set_key_algorithm( &attributes, alg );
3367 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003368
Gilles Peskine049c7532019-05-15 20:22:09 +02003369 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003370 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3372 key_bits = psa_get_key_bits( &attributes );
3373
3374 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3375 alg );
3376 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3377 * should be exact. */
3378 TEST_EQUAL( output_size,
3379 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3380 TEST_ASSERT( output_size <=
3381 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3382 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383
Steven Cooremand588ea12021-01-11 19:36:04 +01003384 status = psa_aead_encrypt( key, alg,
3385 nonce->x, nonce->len,
3386 additional_data->x, additional_data->len,
3387 input_data->x, input_data->len,
3388 output_data, output_size,
3389 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390
Ronald Cron28a45ed2021-02-09 20:35:42 +01003391 /* If the operation is not supported, just skip and not fail in case the
3392 * encryption involves a common limitation of cryptography hardwares and
3393 * an alternative implementation. */
3394 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003395 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003396 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3397 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003398 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003399
3400 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003401 ASSERT_COMPARE( expected_result->x, expected_result->len,
3402 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003403
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003405 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003407 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003408}
3409/* END_CASE */
3410
3411/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003412void aead_decrypt( int key_type_arg, data_t *key_data,
3413 int alg_arg,
3414 data_t *nonce,
3415 data_t *additional_data,
3416 data_t *input_data,
3417 data_t *expected_data,
3418 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003419{
Ronald Cron5425a212020-08-04 14:58:35 +02003420 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003421 psa_key_type_t key_type = key_type_arg;
3422 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003423 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003424 unsigned char *output_data = NULL;
3425 size_t output_size = 0;
3426 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003428 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003429 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003430
Gilles Peskine8817f612018-12-18 00:18:46 +01003431 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003432
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003433 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3434 psa_set_key_algorithm( &attributes, alg );
3435 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003436
Gilles Peskine049c7532019-05-15 20:22:09 +02003437 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003438 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003439 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3440 key_bits = psa_get_key_bits( &attributes );
3441
3442 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3443 alg );
3444 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3445 expected_result != PSA_ERROR_NOT_SUPPORTED )
3446 {
3447 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3448 * should be exact. */
3449 TEST_EQUAL( output_size,
3450 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3451 TEST_ASSERT( output_size <=
3452 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3453 }
3454 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003455
Steven Cooremand588ea12021-01-11 19:36:04 +01003456 status = psa_aead_decrypt( key, alg,
3457 nonce->x, nonce->len,
3458 additional_data->x,
3459 additional_data->len,
3460 input_data->x, input_data->len,
3461 output_data, output_size,
3462 &output_length );
3463
Ronald Cron28a45ed2021-02-09 20:35:42 +01003464 /* If the operation is not supported, just skip and not fail in case the
3465 * decryption involves a common limitation of cryptography hardwares and
3466 * an alternative implementation. */
3467 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003468 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003469 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3470 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003471 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003472
3473 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474
Gilles Peskine2d277862018-06-18 15:41:12 +02003475 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003476 ASSERT_COMPARE( expected_data->x, expected_data->len,
3477 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003480 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003481 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003482 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003483}
3484/* END_CASE */
3485
3486/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003487void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3488 int alg_arg,
3489 data_t *nonce,
3490 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003491 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003492 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003493 int do_test_data_chunked,
3494 int do_set_lengths,
3495 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003496{
Paul Elliottd3f82412021-06-16 16:52:21 +01003497 size_t ad_part_len = 0;
3498 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003499
Paul Elliotte64deda2021-09-09 14:07:23 +01003500 /* Ensure that either one part of the test or the other is done, i.e this
3501 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003502 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3503
3504 /* Temporary whilst we have algorithms that cannot support chunking */
3505 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003506 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003507 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3508 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003509 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003510 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003511
Paul Elliott329d5382021-07-22 17:10:45 +01003512 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003513 if( !aead_multipart_internal_func( key_type_arg, key_data,
3514 alg_arg, nonce,
3515 additional_data,
3516 ad_part_len,
3517 input_data, -1,
3518 do_set_lengths,
3519 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003520 1, 1, 0,
3521 ( ad_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003522 break;
3523
3524 /* length(0) part, length(ad_part_len) part, length(0) part... */
3525 mbedtls_test_set_step( 1000 + ad_part_len );
3526
3527 if( !aead_multipart_internal_func( key_type_arg, key_data,
3528 alg_arg, nonce,
3529 additional_data,
3530 ad_part_len,
3531 input_data, -1,
3532 do_set_lengths,
3533 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003534 1, 1, 1,
3535 ( ad_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003536 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003537 }
3538 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003539
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003540 /* Temporary whilst we have algorithms that cannot support chunking */
3541 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003542 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003543 for( data_part_len = 1; data_part_len <= input_data->len;
3544 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003545 {
Paul Elliott329d5382021-07-22 17:10:45 +01003546 /* Split data into length(data_part_len) parts. */
3547 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003548
3549 if( !aead_multipart_internal_func( key_type_arg, key_data,
3550 alg_arg, nonce,
3551 additional_data, -1,
3552 input_data, data_part_len,
3553 do_set_lengths,
3554 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003555 1, 1, 0,
3556 ( data_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003557 break;
3558
3559 /* length(0) part, length(data_part_len) part, length(0) part... */
3560 mbedtls_test_set_step( 3000 + data_part_len );
3561
3562 if( !aead_multipart_internal_func( key_type_arg, key_data,
3563 alg_arg, nonce,
3564 additional_data, -1,
3565 input_data, data_part_len,
3566 do_set_lengths,
3567 expected_output,
Paul Elliottebf91632021-07-22 17:54:42 +01003568 1, 1, 1,
3569 ( data_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003570 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003571 }
3572 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003573
Paul Elliott0023e0a2021-04-27 10:06:22 +01003574
Paul Elliott8fc45162021-06-23 16:06:01 +01003575 /* Goto is required to silence warnings about unused labels, as we
3576 * don't actually do any test assertions in this function. */
3577 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003578}
3579/* END_CASE */
3580
3581/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3583 int alg_arg,
3584 data_t *nonce,
3585 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003586 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003587 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003588 int do_test_data_chunked,
3589 int do_set_lengths,
3590 data_t *expected_output,
3591 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003592{
Paul Elliottd3f82412021-06-16 16:52:21 +01003593 size_t ad_part_len = 0;
3594 size_t data_part_len = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003595
Paul Elliotte64deda2021-09-09 14:07:23 +01003596 /* Ensure that either one part of the test or the other is done, i.e this
3597 * test does something. */
3598 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3599
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003600 /* Temporary whilst we have algorithms that cannot support chunking */
3601 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003602 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003603 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3604 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003605 {
Paul Elliott329d5382021-07-22 17:10:45 +01003606 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003607 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003608
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003609 if( !aead_multipart_internal_func( key_type_arg, key_data,
3610 alg_arg, nonce,
3611 additional_data,
3612 ad_part_len,
3613 input_data, -1,
3614 do_set_lengths,
3615 expected_output,
3616 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003617 0, 0,
3618 ( ad_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003619 break;
3620
3621 /* length(0) part, length(ad_part_len) part, length(0) part... */
3622 mbedtls_test_set_step( 1000 + ad_part_len );
3623
3624 if( !aead_multipart_internal_func( key_type_arg, key_data,
3625 alg_arg, nonce,
3626 additional_data,
3627 ad_part_len,
3628 input_data, -1,
3629 do_set_lengths,
3630 expected_output,
3631 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003632 0, 1,
3633 ( ad_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003634 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003635 }
3636 }
3637
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003638 /* Temporary whilst we have algorithms that cannot support chunking */
3639 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003640 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003641 for( data_part_len = 1; data_part_len <= input_data->len;
3642 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003643 {
Paul Elliott329d5382021-07-22 17:10:45 +01003644 /* Split data into length(data_part_len) parts. */
3645 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003646
3647 if( !aead_multipart_internal_func( key_type_arg, key_data,
3648 alg_arg, nonce,
3649 additional_data, -1,
3650 input_data, data_part_len,
3651 do_set_lengths,
3652 expected_output,
3653 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003654 0, 0,
3655 ( data_part_len & 0x01 ) ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003656 break;
3657
3658 /* length(0) part, length(data_part_len) part, length(0) part... */
3659 mbedtls_test_set_step( 3000 + data_part_len );
3660
3661 if( !aead_multipart_internal_func( key_type_arg, key_data,
3662 alg_arg, nonce,
3663 additional_data, -1,
3664 input_data, data_part_len,
3665 do_set_lengths,
3666 expected_output,
3667 expect_valid_signature,
Paul Elliottebf91632021-07-22 17:54:42 +01003668 0, 1,
3669 ( data_part_len & 0x01 ) ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003670 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003671 }
3672 }
3673
Paul Elliott8fc45162021-06-23 16:06:01 +01003674 /* Goto is required to silence warnings about unused labels, as we
3675 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003676 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003677}
3678/* END_CASE */
3679
3680/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003681void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3682 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003683 int nonce_length,
3684 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003685 data_t *additional_data,
3686 data_t *input_data,
3687 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003688{
3689
3690 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3691 psa_key_type_t key_type = key_type_arg;
3692 psa_algorithm_t alg = alg_arg;
3693 psa_aead_operation_t operation;
3694 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3696 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003697 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003698 size_t actual_nonce_length = 0;
3699 size_t expected_nonce_length = expected_nonce_length_arg;
3700 unsigned char *output = NULL;
3701 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003702 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003703 size_t ciphertext_size = 0;
3704 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003705 size_t tag_length = 0;
3706 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003707
3708 PSA_ASSERT( psa_crypto_init( ) );
3709
3710 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3711 psa_set_key_algorithm( & attributes, alg );
3712 psa_set_key_type( & attributes, key_type );
3713
3714 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3715 &key ) );
3716
3717 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3718
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003719 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3720
Paul Elliottf1277632021-08-24 18:11:37 +01003721 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003722
Paul Elliottf1277632021-08-24 18:11:37 +01003723 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003724
Paul Elliottf1277632021-08-24 18:11:37 +01003725 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003726
Paul Elliottf1277632021-08-24 18:11:37 +01003727 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003728
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003729 operation = psa_aead_operation_init( );
3730
3731 status = psa_aead_encrypt_setup( &operation, key, alg );
3732
3733 /* If the operation is not supported, just skip and not fail in case the
3734 * encryption involves a common limitation of cryptography hardwares and
3735 * an alternative implementation. */
3736 if( status == PSA_ERROR_NOT_SUPPORTED )
3737 {
3738 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003739 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003740 }
3741
3742 PSA_ASSERT( status );
3743
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003744 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003745 nonce_length,
3746 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003747
Paul Elliott693bf312021-07-23 17:40:41 +01003748 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003749
Paul Elliottf1277632021-08-24 18:11:37 +01003750 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003751
Paul Elliottf1277632021-08-24 18:11:37 +01003752 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003753
Paul Elliott693bf312021-07-23 17:40:41 +01003754 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003755 {
3756
3757 /* Ensure we can still complete operation. */
3758
3759 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3760 additional_data->len ) );
3761
3762 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003763 output, output_size,
3764 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003765
Paul Elliottf1277632021-08-24 18:11:37 +01003766 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3767 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003768 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3769 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003770
3771exit:
3772 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003773 mbedtls_free( output );
3774 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003775 psa_aead_abort( &operation );
3776 PSA_DONE( );
3777}
3778/* END_CASE */
3779
3780/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003781void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3782 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003783 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003784 data_t *additional_data,
3785 data_t *input_data,
3786 int expected_status_arg )
3787{
3788
3789 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3790 psa_key_type_t key_type = key_type_arg;
3791 psa_algorithm_t alg = alg_arg;
3792 psa_aead_operation_t operation;
3793 uint8_t *nonce_buffer = NULL;
3794 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3795 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3796 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003797 unsigned char *output = NULL;
3798 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003799 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003800 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003801 size_t ciphertext_size = 0;
3802 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003803 size_t tag_length = 0;
3804 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003805 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003806
3807 PSA_ASSERT( psa_crypto_init( ) );
3808
3809 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3810 psa_set_key_algorithm( &attributes, alg );
3811 psa_set_key_type( &attributes, key_type );
3812
3813 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3814 &key ) );
3815
3816 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3817
3818 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3819
Paul Elliott6f0e7202021-08-25 12:57:18 +01003820 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003821
Paul Elliott6f0e7202021-08-25 12:57:18 +01003822 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003823
Paul Elliott6f0e7202021-08-25 12:57:18 +01003824 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003825
Paul Elliott6f0e7202021-08-25 12:57:18 +01003826 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003827
3828 operation = psa_aead_operation_init( );
3829
3830 status = psa_aead_encrypt_setup( &operation, key, alg );
3831
3832 /* If the operation is not supported, just skip and not fail in case the
3833 * encryption involves a common limitation of cryptography hardwares and
3834 * an alternative implementation. */
3835 if( status == PSA_ERROR_NOT_SUPPORTED )
3836 {
3837 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003838 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003839 }
3840
3841 PSA_ASSERT( status );
3842
Paul Elliott4023ffd2021-09-10 16:21:22 +01003843 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3844 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003845 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003846 /* Arbitrary size buffer, to test zero length valid buffer. */
3847 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003848 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003849 }
3850 else
3851 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003852 /* If length is zero, then this will return NULL. */
3853 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003854 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003855
Paul Elliott4023ffd2021-09-10 16:21:22 +01003856 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003857 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003858 for( index = 0; index < nonce_length - 1; ++index )
3859 {
3860 nonce_buffer[index] = 'a' + index;
3861 }
Paul Elliott66696b52021-08-16 18:42:41 +01003862 }
Paul Elliott863864a2021-07-23 17:28:31 +01003863 }
3864
Paul Elliott6f0e7202021-08-25 12:57:18 +01003865 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003866
Paul Elliott693bf312021-07-23 17:40:41 +01003867 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003868
3869 if( expected_status == PSA_SUCCESS )
3870 {
3871 /* Ensure we can still complete operation. */
3872
3873 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3874 additional_data->len ) );
3875
3876 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003877 output, output_size,
3878 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003879
Paul Elliott6f0e7202021-08-25 12:57:18 +01003880 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3881 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003882 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3883 }
3884
3885exit:
3886 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003887 mbedtls_free( output );
3888 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003889 mbedtls_free( nonce_buffer );
3890 psa_aead_abort( &operation );
3891 PSA_DONE( );
3892}
3893/* END_CASE */
3894
3895/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003896void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3897 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003898 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003899 data_t *nonce,
3900 data_t *additional_data,
3901 data_t *input_data,
3902 int expected_status_arg )
3903{
3904
3905 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3906 psa_key_type_t key_type = key_type_arg;
3907 psa_algorithm_t alg = alg_arg;
3908 psa_aead_operation_t operation;
3909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3910 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3911 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003912 unsigned char *output = NULL;
3913 unsigned char *ciphertext = NULL;
3914 size_t output_size = output_size_arg;
3915 size_t ciphertext_size = 0;
3916 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003917 size_t tag_length = 0;
3918 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3919
3920 PSA_ASSERT( psa_crypto_init( ) );
3921
3922 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3923 psa_set_key_algorithm( &attributes, alg );
3924 psa_set_key_type( &attributes, key_type );
3925
3926 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3927 &key ) );
3928
3929 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3930
Paul Elliottc6d11d02021-09-01 12:04:23 +01003931 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003932
Paul Elliottc6d11d02021-09-01 12:04:23 +01003933 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003934
Paul Elliottc6d11d02021-09-01 12:04:23 +01003935 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003936
3937 operation = psa_aead_operation_init( );
3938
3939 status = psa_aead_encrypt_setup( &operation, key, alg );
3940
3941 /* If the operation is not supported, just skip and not fail in case the
3942 * encryption involves a common limitation of cryptography hardwares and
3943 * an alternative implementation. */
3944 if( status == PSA_ERROR_NOT_SUPPORTED )
3945 {
3946 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3947 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3948 }
3949
3950 PSA_ASSERT( status );
3951
3952 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3953
3954 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3955 additional_data->len ) );
3956
3957 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003958 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003959
3960 TEST_EQUAL( status, expected_status );
3961
3962 if( expected_status == PSA_SUCCESS )
3963 {
3964 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003965 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3966 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003967 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3968 }
3969
3970exit:
3971 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003972 mbedtls_free( output );
3973 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003974 psa_aead_abort( &operation );
3975 PSA_DONE( );
3976}
3977/* END_CASE */
3978
Paul Elliott91b021e2021-07-23 18:52:31 +01003979/* BEGIN_CASE */
3980void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3981 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003982 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003983 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003984 data_t *nonce,
3985 data_t *additional_data,
3986 data_t *input_data,
3987 int expected_status_arg )
3988{
3989
3990 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3991 psa_key_type_t key_type = key_type_arg;
3992 psa_algorithm_t alg = alg_arg;
3993 psa_aead_operation_t operation;
3994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3995 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3996 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003997 unsigned char *ciphertext = NULL;
3998 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01003999 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004000 size_t ciphertext_size = 0;
4001 size_t ciphertext_length = 0;
4002 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004003 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004004 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004005
4006 PSA_ASSERT( psa_crypto_init( ) );
4007
4008 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4009 psa_set_key_algorithm( &attributes, alg );
4010 psa_set_key_type( &attributes, key_type );
4011
4012 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4013 &key ) );
4014
4015 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4016
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004017 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004018
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004019 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004020
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004021 TEST_ASSERT( finish_ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott91b021e2021-07-23 18:52:31 +01004022
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004023 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004024
Paul Elliott719c1322021-09-13 18:27:22 +01004025 ASSERT_ALLOC( tag_buffer, tag_size );
4026
Paul Elliott91b021e2021-07-23 18:52:31 +01004027 operation = psa_aead_operation_init( );
4028
4029 status = psa_aead_encrypt_setup( &operation, key, alg );
4030
4031 /* If the operation is not supported, just skip and not fail in case the
4032 * encryption involves a common limitation of cryptography hardwares and
4033 * an alternative implementation. */
4034 if( status == PSA_ERROR_NOT_SUPPORTED )
4035 {
4036 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4037 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4038 }
4039
4040 PSA_ASSERT( status );
4041
4042 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4043
4044 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4045 additional_data->len ) );
4046
4047 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004048 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004049
4050 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004051 status = psa_aead_finish( &operation, finish_ciphertext,
4052 finish_ciphertext_size,
4053 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004054 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004055
4056 TEST_EQUAL( status, expected_status );
4057
4058exit:
4059 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004060 mbedtls_free( ciphertext );
4061 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004062 psa_aead_abort( &operation );
4063 PSA_DONE( );
4064}
4065/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004066
4067/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004068void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4069 int alg_arg,
4070 data_t *nonce,
4071 data_t *additional_data,
4072 data_t *input_data )
4073{
4074 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4075 psa_key_type_t key_type = key_type_arg;
4076 psa_algorithm_t alg = alg_arg;
4077 psa_aead_operation_t operation;
4078 unsigned char *output_data = NULL;
4079 unsigned char *final_data = NULL;
4080 size_t output_size = 0;
4081 size_t finish_output_size = 0;
4082 size_t output_length = 0;
4083 size_t key_bits = 0;
4084 size_t tag_length = 0;
4085 size_t tag_size = 0;
4086 size_t nonce_length = 0;
4087 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4088 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4089 size_t output_part_length = 0;
4090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4091
4092 PSA_ASSERT( psa_crypto_init( ) );
4093
4094 psa_set_key_usage_flags( & attributes,
4095 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4096 psa_set_key_algorithm( & attributes, alg );
4097 psa_set_key_type( & attributes, key_type );
4098
4099 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4100 &key ) );
4101
4102 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4103 key_bits = psa_get_key_bits( &attributes );
4104
4105 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4106
4107 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4108
4109 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4110
4111 ASSERT_ALLOC( output_data, output_size );
4112
4113 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4114
4115 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4116
4117 ASSERT_ALLOC( final_data, finish_output_size );
4118
4119 /* Test all operations error without calling setup first. */
4120
4121 operation = psa_aead_operation_init( );
4122
4123 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4124 PSA_ERROR_BAD_STATE );
4125
4126 psa_aead_abort( &operation );
4127
4128 operation = psa_aead_operation_init( );
4129
4130 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4131 PSA_AEAD_NONCE_MAX_SIZE,
4132 &nonce_length ),
4133 PSA_ERROR_BAD_STATE );
4134
4135 psa_aead_abort( &operation );
4136
Paul Elliott481be342021-07-16 17:38:47 +01004137 /* ------------------------------------------------------- */
4138
Paul Elliottc23a9a02021-06-21 18:32:46 +01004139 operation = psa_aead_operation_init( );
4140
4141 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4142 input_data->len ),
4143 PSA_ERROR_BAD_STATE );
4144
4145 psa_aead_abort( &operation );
4146
Paul Elliott481be342021-07-16 17:38:47 +01004147 /* ------------------------------------------------------- */
4148
Paul Elliottc23a9a02021-06-21 18:32:46 +01004149 operation = psa_aead_operation_init( );
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 Elliott481be342021-07-16 17:38:47 +01004157 /* ------------------------------------------------------- */
4158
Paul Elliottc23a9a02021-06-21 18:32:46 +01004159 operation = psa_aead_operation_init( );
4160
4161 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4162 input_data->len, output_data,
4163 output_size, &output_length ),
4164 PSA_ERROR_BAD_STATE );
4165
4166 psa_aead_abort( &operation );
4167
Paul Elliott481be342021-07-16 17:38:47 +01004168 /* ------------------------------------------------------- */
4169
Paul Elliottc23a9a02021-06-21 18:32:46 +01004170 operation = psa_aead_operation_init( );
4171
4172 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4173 finish_output_size,
4174 &output_part_length,
4175 tag_buffer, tag_length,
4176 &tag_size ),
4177 PSA_ERROR_BAD_STATE );
4178
4179 psa_aead_abort( &operation );
4180
Paul Elliott481be342021-07-16 17:38:47 +01004181 /* ------------------------------------------------------- */
4182
Paul Elliottc23a9a02021-06-21 18:32:46 +01004183 operation = psa_aead_operation_init( );
4184
4185 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4186 finish_output_size,
4187 &output_part_length,
4188 tag_buffer,
4189 tag_length ),
4190 PSA_ERROR_BAD_STATE );
4191
4192 psa_aead_abort( &operation );
4193
4194 /* Test for double setups. */
4195
4196 operation = psa_aead_operation_init( );
4197
4198 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4199
4200 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4201 PSA_ERROR_BAD_STATE );
4202
4203 psa_aead_abort( &operation );
4204
Paul Elliott481be342021-07-16 17:38:47 +01004205 /* ------------------------------------------------------- */
4206
Paul Elliottc23a9a02021-06-21 18:32:46 +01004207 operation = psa_aead_operation_init( );
4208
4209 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4210
4211 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4212 PSA_ERROR_BAD_STATE );
4213
4214 psa_aead_abort( &operation );
4215
Paul Elliott374a2be2021-07-16 17:53:40 +01004216 /* ------------------------------------------------------- */
4217
4218 operation = psa_aead_operation_init( );
4219
4220 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4221
4222 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4223 PSA_ERROR_BAD_STATE );
4224
4225 psa_aead_abort( &operation );
4226
4227 /* ------------------------------------------------------- */
4228
4229 operation = psa_aead_operation_init( );
4230
4231 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4232
4233 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4234 PSA_ERROR_BAD_STATE );
4235
4236 psa_aead_abort( &operation );
4237
Paul Elliottc23a9a02021-06-21 18:32:46 +01004238 /* Test for not setting a nonce. */
4239
4240 operation = psa_aead_operation_init( );
4241
4242 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4243
4244 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4245 additional_data->len ),
4246 PSA_ERROR_BAD_STATE );
4247
4248 psa_aead_abort( &operation );
4249
Paul Elliott7f628422021-09-01 12:08:29 +01004250 /* ------------------------------------------------------- */
4251
4252 operation = psa_aead_operation_init( );
4253
4254 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4255
4256 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4257 input_data->len, output_data,
4258 output_size, &output_length ),
4259 PSA_ERROR_BAD_STATE );
4260
4261 psa_aead_abort( &operation );
4262
Paul Elliottc23a9a02021-06-21 18:32:46 +01004263 /* Test for double setting nonce. */
4264
4265 operation = psa_aead_operation_init( );
4266
4267 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4268
4269 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4270
4271 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4272 PSA_ERROR_BAD_STATE );
4273
4274 psa_aead_abort( &operation );
4275
Paul Elliott374a2be2021-07-16 17:53:40 +01004276 /* Test for double generating nonce. */
4277
4278 operation = psa_aead_operation_init( );
4279
4280 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4281
4282 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4283 PSA_AEAD_NONCE_MAX_SIZE,
4284 &nonce_length ) );
4285
4286 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4287 PSA_AEAD_NONCE_MAX_SIZE,
4288 &nonce_length ),
4289 PSA_ERROR_BAD_STATE );
4290
4291
4292 psa_aead_abort( &operation );
4293
4294 /* Test for generate nonce then set and vice versa */
4295
4296 operation = psa_aead_operation_init( );
4297
4298 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4299
4300 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4301 PSA_AEAD_NONCE_MAX_SIZE,
4302 &nonce_length ) );
4303
4304 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4305 PSA_ERROR_BAD_STATE );
4306
4307 psa_aead_abort( &operation );
4308
4309 /* ------------------------------------------------------- */
4310
4311 operation = psa_aead_operation_init( );
4312
4313 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4314
4315 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4316
4317 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4318 PSA_AEAD_NONCE_MAX_SIZE,
4319 &nonce_length ),
4320 PSA_ERROR_BAD_STATE );
4321
4322 psa_aead_abort( &operation );
4323
Paul Elliott7220cae2021-06-22 17:25:57 +01004324 /* Test for generating nonce in decrypt setup. */
4325
4326 operation = psa_aead_operation_init( );
4327
4328 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4329
4330 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4331 PSA_AEAD_NONCE_MAX_SIZE,
4332 &nonce_length ),
4333 PSA_ERROR_BAD_STATE );
4334
4335 psa_aead_abort( &operation );
4336
Paul Elliottc23a9a02021-06-21 18:32:46 +01004337 /* Test for setting lengths twice. */
4338
4339 operation = psa_aead_operation_init( );
4340
4341 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4342
4343 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4344
4345 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4346 input_data->len ) );
4347
4348 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4349 input_data->len ),
4350 PSA_ERROR_BAD_STATE );
4351
4352 psa_aead_abort( &operation );
4353
4354 /* Test for setting lengths after already starting data. */
4355
4356 operation = psa_aead_operation_init( );
4357
4358 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4359
4360 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4361
4362 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4363 input_data->len, output_data,
4364 output_size, &output_length ) );
4365
4366 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4367 input_data->len ),
4368 PSA_ERROR_BAD_STATE );
4369
4370 psa_aead_abort( &operation );
4371
Paul Elliott243080c2021-07-21 19:01:17 +01004372 /* Test for not sending any additional data or data after setting non zero
4373 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004374
4375 operation = psa_aead_operation_init( );
4376
4377 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4378
4379 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4380
4381 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4382 input_data->len ) );
4383
4384 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4385 finish_output_size,
4386 &output_part_length,
4387 tag_buffer, tag_length,
4388 &tag_size ),
4389 PSA_ERROR_INVALID_ARGUMENT );
4390
4391 psa_aead_abort( &operation );
4392
Paul Elliott243080c2021-07-21 19:01:17 +01004393 /* Test for not sending any additional data or data after setting non-zero
4394 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004395
4396 operation = psa_aead_operation_init( );
4397
4398 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4399
4400 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4401
4402 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4403 input_data->len ) );
4404
4405 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4406 finish_output_size,
4407 &output_part_length,
4408 tag_buffer,
4409 tag_length ),
4410 PSA_ERROR_INVALID_ARGUMENT );
4411
4412 psa_aead_abort( &operation );
4413
Paul Elliott243080c2021-07-21 19:01:17 +01004414 /* Test for not sending any additional data after setting a non-zero length
4415 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004416
4417 operation = psa_aead_operation_init( );
4418
4419 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4420
4421 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4422
4423 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4424 input_data->len ) );
4425
4426 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4427 input_data->len, output_data,
4428 output_size, &output_length ),
4429 PSA_ERROR_INVALID_ARGUMENT );
4430
4431 psa_aead_abort( &operation );
4432
Paul Elliottb0450fe2021-09-01 15:06:26 +01004433 /* Test for sending too much additional data after setting lengths. */
4434
4435 operation = psa_aead_operation_init( );
4436
4437 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4438
4439 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4440
4441 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4442
4443
4444 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4445 additional_data->len ),
4446 PSA_ERROR_INVALID_ARGUMENT );
4447
4448 psa_aead_abort( &operation );
4449
4450 /* Test for sending too much data after setting lengths. */
4451
4452 operation = psa_aead_operation_init( );
4453
4454 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4455
4456 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4457
4458 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4459
4460 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4461 input_data->len, output_data,
4462 output_size, &output_length ),
4463 PSA_ERROR_INVALID_ARGUMENT );
4464
4465 psa_aead_abort( &operation );
4466
Paul Elliottc23a9a02021-06-21 18:32:46 +01004467 /* Test sending additional data after data. */
4468
4469 operation = psa_aead_operation_init( );
4470
4471 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4472
4473 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4474
4475 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4476 input_data->len, output_data,
4477 output_size, &output_length ) );
4478
4479 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4480 additional_data->len ),
4481 PSA_ERROR_BAD_STATE );
4482
4483 psa_aead_abort( &operation );
4484
Paul Elliott534d0b42021-06-22 19:15:20 +01004485 /* Test calling finish on decryption. */
4486
4487 operation = psa_aead_operation_init( );
4488
4489 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4490
4491 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4492
4493 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4494 finish_output_size,
4495 &output_part_length,
4496 tag_buffer, tag_length,
4497 &tag_size ),
4498 PSA_ERROR_BAD_STATE );
4499
4500 psa_aead_abort( &operation );
4501
4502 /* Test calling verify on encryption. */
4503
4504 operation = psa_aead_operation_init( );
4505
4506 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4507
4508 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4509
4510 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4511 finish_output_size,
4512 &output_part_length,
4513 tag_buffer,
4514 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004515 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004516
4517 psa_aead_abort( &operation );
4518
4519
Paul Elliottc23a9a02021-06-21 18:32:46 +01004520exit:
4521 psa_destroy_key( key );
4522 psa_aead_abort( &operation );
4523 mbedtls_free( output_data );
4524 mbedtls_free( final_data );
4525 PSA_DONE( );
4526}
4527/* END_CASE */
4528
4529/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004530void signature_size( int type_arg,
4531 int bits,
4532 int alg_arg,
4533 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004534{
4535 psa_key_type_t type = type_arg;
4536 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004537 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004538
Gilles Peskinefe11b722018-12-18 00:24:04 +01004539 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004540
Gilles Peskinee59236f2018-01-27 23:32:46 +01004541exit:
4542 ;
4543}
4544/* END_CASE */
4545
4546/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004547void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4548 int alg_arg, data_t *input_data,
4549 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004550{
Ronald Cron5425a212020-08-04 14:58:35 +02004551 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004552 psa_key_type_t key_type = key_type_arg;
4553 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004554 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004555 unsigned char *signature = NULL;
4556 size_t signature_size;
4557 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004559
Gilles Peskine8817f612018-12-18 00:18:46 +01004560 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004561
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004562 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004563 psa_set_key_algorithm( &attributes, alg );
4564 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004565
Gilles Peskine049c7532019-05-15 20:22:09 +02004566 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004567 &key ) );
4568 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004569 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004570
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004571 /* Allocate a buffer which has the size advertized by the
4572 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004573 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004574 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004575 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004576 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004577 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004578
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004579 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004580 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004581 input_data->x, input_data->len,
4582 signature, signature_size,
4583 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004584 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004585 ASSERT_COMPARE( output_data->x, output_data->len,
4586 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004587
4588exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004589 /*
4590 * Key attributes may have been returned by psa_get_key_attributes()
4591 * thus reset them as required.
4592 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004593 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004594
Ronald Cron5425a212020-08-04 14:58:35 +02004595 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004596 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004597 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004598}
4599/* END_CASE */
4600
4601/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004602void sign_hash_fail( int key_type_arg, data_t *key_data,
4603 int alg_arg, data_t *input_data,
4604 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004605{
Ronald Cron5425a212020-08-04 14:58:35 +02004606 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004607 psa_key_type_t key_type = key_type_arg;
4608 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004609 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004610 psa_status_t actual_status;
4611 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004612 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004613 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004614 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004615
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004616 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004617
Gilles Peskine8817f612018-12-18 00:18:46 +01004618 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004619
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004620 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004621 psa_set_key_algorithm( &attributes, alg );
4622 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004623
Gilles Peskine049c7532019-05-15 20:22:09 +02004624 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004625 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004626
Ronald Cron5425a212020-08-04 14:58:35 +02004627 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004628 input_data->x, input_data->len,
4629 signature, signature_size,
4630 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004631 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004632 /* The value of *signature_length is unspecified on error, but
4633 * whatever it is, it should be less than signature_size, so that
4634 * if the caller tries to read *signature_length bytes without
4635 * checking the error code then they don't overflow a buffer. */
4636 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004637
4638exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004639 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004640 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004641 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004642 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004643}
4644/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004645
4646/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004647void sign_verify_hash( int key_type_arg, data_t *key_data,
4648 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004649{
Ronald Cron5425a212020-08-04 14:58:35 +02004650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004651 psa_key_type_t key_type = key_type_arg;
4652 psa_algorithm_t alg = alg_arg;
4653 size_t key_bits;
4654 unsigned char *signature = NULL;
4655 size_t signature_size;
4656 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004657 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004658
Gilles Peskine8817f612018-12-18 00:18:46 +01004659 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004660
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004661 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004662 psa_set_key_algorithm( &attributes, alg );
4663 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004664
Gilles Peskine049c7532019-05-15 20:22:09 +02004665 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004666 &key ) );
4667 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004668 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004669
4670 /* Allocate a buffer which has the size advertized by the
4671 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004672 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004673 key_bits, alg );
4674 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004675 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004676 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004677
4678 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004679 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004680 input_data->x, input_data->len,
4681 signature, signature_size,
4682 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004683 /* Check that the signature length looks sensible. */
4684 TEST_ASSERT( signature_length <= signature_size );
4685 TEST_ASSERT( signature_length > 0 );
4686
4687 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004688 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004689 input_data->x, input_data->len,
4690 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004691
4692 if( input_data->len != 0 )
4693 {
4694 /* Flip a bit in the input and verify that the signature is now
4695 * detected as invalid. Flip a bit at the beginning, not at the end,
4696 * because ECDSA may ignore the last few bits of the input. */
4697 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004698 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004699 input_data->x, input_data->len,
4700 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004701 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004702 }
4703
4704exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004705 /*
4706 * Key attributes may have been returned by psa_get_key_attributes()
4707 * thus reset them as required.
4708 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004709 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004710
Ronald Cron5425a212020-08-04 14:58:35 +02004711 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004712 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004713 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004714}
4715/* END_CASE */
4716
4717/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004718void verify_hash( int key_type_arg, data_t *key_data,
4719 int alg_arg, data_t *hash_data,
4720 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004721{
Ronald Cron5425a212020-08-04 14:58:35 +02004722 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004723 psa_key_type_t key_type = key_type_arg;
4724 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004726
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004727 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004728
Gilles Peskine8817f612018-12-18 00:18:46 +01004729 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004730
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004731 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004732 psa_set_key_algorithm( &attributes, alg );
4733 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004734
Gilles Peskine049c7532019-05-15 20:22:09 +02004735 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004736 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004737
Ronald Cron5425a212020-08-04 14:58:35 +02004738 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004739 hash_data->x, hash_data->len,
4740 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004741
itayzafrir5c753392018-05-08 11:18:38 +03004742exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004743 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004744 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004745 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004746}
4747/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004748
4749/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004750void verify_hash_fail( int key_type_arg, data_t *key_data,
4751 int alg_arg, data_t *hash_data,
4752 data_t *signature_data,
4753 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004754{
Ronald Cron5425a212020-08-04 14:58:35 +02004755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004756 psa_key_type_t key_type = key_type_arg;
4757 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004758 psa_status_t actual_status;
4759 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004760 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004761
Gilles Peskine8817f612018-12-18 00:18:46 +01004762 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004763
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004764 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004765 psa_set_key_algorithm( &attributes, alg );
4766 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004767
Gilles Peskine049c7532019-05-15 20:22:09 +02004768 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004769 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004770
Ronald Cron5425a212020-08-04 14:58:35 +02004771 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004772 hash_data->x, hash_data->len,
4773 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004774 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004775
4776exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004777 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004778 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004779 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004780}
4781/* END_CASE */
4782
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004783/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004784void sign_message_deterministic( int key_type_arg,
4785 data_t *key_data,
4786 int alg_arg,
4787 data_t *input_data,
4788 data_t *output_data )
4789{
4790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4791 psa_key_type_t key_type = key_type_arg;
4792 psa_algorithm_t alg = alg_arg;
4793 size_t key_bits;
4794 unsigned char *signature = NULL;
4795 size_t signature_size;
4796 size_t signature_length = 0xdeadbeef;
4797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4798
4799 PSA_ASSERT( psa_crypto_init( ) );
4800
4801 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4802 psa_set_key_algorithm( &attributes, alg );
4803 psa_set_key_type( &attributes, key_type );
4804
4805 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4806 &key ) );
4807 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4808 key_bits = psa_get_key_bits( &attributes );
4809
4810 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4811 TEST_ASSERT( signature_size != 0 );
4812 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4813 ASSERT_ALLOC( signature, signature_size );
4814
4815 PSA_ASSERT( psa_sign_message( key, alg,
4816 input_data->x, input_data->len,
4817 signature, signature_size,
4818 &signature_length ) );
4819
4820 ASSERT_COMPARE( output_data->x, output_data->len,
4821 signature, signature_length );
4822
4823exit:
4824 psa_reset_key_attributes( &attributes );
4825
4826 psa_destroy_key( key );
4827 mbedtls_free( signature );
4828 PSA_DONE( );
4829
4830}
4831/* END_CASE */
4832
4833/* BEGIN_CASE */
4834void sign_message_fail( int key_type_arg,
4835 data_t *key_data,
4836 int alg_arg,
4837 data_t *input_data,
4838 int signature_size_arg,
4839 int expected_status_arg )
4840{
4841 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4842 psa_key_type_t key_type = key_type_arg;
4843 psa_algorithm_t alg = alg_arg;
4844 size_t signature_size = signature_size_arg;
4845 psa_status_t actual_status;
4846 psa_status_t expected_status = expected_status_arg;
4847 unsigned char *signature = NULL;
4848 size_t signature_length = 0xdeadbeef;
4849 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4850
4851 ASSERT_ALLOC( signature, signature_size );
4852
4853 PSA_ASSERT( psa_crypto_init( ) );
4854
4855 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4856 psa_set_key_algorithm( &attributes, alg );
4857 psa_set_key_type( &attributes, key_type );
4858
4859 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4860 &key ) );
4861
4862 actual_status = psa_sign_message( key, alg,
4863 input_data->x, input_data->len,
4864 signature, signature_size,
4865 &signature_length );
4866 TEST_EQUAL( actual_status, expected_status );
4867 /* The value of *signature_length is unspecified on error, but
4868 * whatever it is, it should be less than signature_size, so that
4869 * if the caller tries to read *signature_length bytes without
4870 * checking the error code then they don't overflow a buffer. */
4871 TEST_ASSERT( signature_length <= signature_size );
4872
4873exit:
4874 psa_reset_key_attributes( &attributes );
4875 psa_destroy_key( key );
4876 mbedtls_free( signature );
4877 PSA_DONE( );
4878}
4879/* END_CASE */
4880
4881/* BEGIN_CASE */
4882void sign_verify_message( int key_type_arg,
4883 data_t *key_data,
4884 int alg_arg,
4885 data_t *input_data )
4886{
4887 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4888 psa_key_type_t key_type = key_type_arg;
4889 psa_algorithm_t alg = alg_arg;
4890 size_t key_bits;
4891 unsigned char *signature = NULL;
4892 size_t signature_size;
4893 size_t signature_length = 0xdeadbeef;
4894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4895
4896 PSA_ASSERT( psa_crypto_init( ) );
4897
4898 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4899 PSA_KEY_USAGE_VERIFY_MESSAGE );
4900 psa_set_key_algorithm( &attributes, alg );
4901 psa_set_key_type( &attributes, key_type );
4902
4903 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4904 &key ) );
4905 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4906 key_bits = psa_get_key_bits( &attributes );
4907
4908 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4909 TEST_ASSERT( signature_size != 0 );
4910 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4911 ASSERT_ALLOC( signature, signature_size );
4912
4913 PSA_ASSERT( psa_sign_message( key, alg,
4914 input_data->x, input_data->len,
4915 signature, signature_size,
4916 &signature_length ) );
4917 TEST_ASSERT( signature_length <= signature_size );
4918 TEST_ASSERT( signature_length > 0 );
4919
4920 PSA_ASSERT( psa_verify_message( key, alg,
4921 input_data->x, input_data->len,
4922 signature, signature_length ) );
4923
4924 if( input_data->len != 0 )
4925 {
4926 /* Flip a bit in the input and verify that the signature is now
4927 * detected as invalid. Flip a bit at the beginning, not at the end,
4928 * because ECDSA may ignore the last few bits of the input. */
4929 input_data->x[0] ^= 1;
4930 TEST_EQUAL( psa_verify_message( key, alg,
4931 input_data->x, input_data->len,
4932 signature, signature_length ),
4933 PSA_ERROR_INVALID_SIGNATURE );
4934 }
4935
4936exit:
4937 psa_reset_key_attributes( &attributes );
4938
4939 psa_destroy_key( key );
4940 mbedtls_free( signature );
4941 PSA_DONE( );
4942}
4943/* END_CASE */
4944
4945/* BEGIN_CASE */
4946void verify_message( int key_type_arg,
4947 data_t *key_data,
4948 int alg_arg,
4949 data_t *input_data,
4950 data_t *signature_data )
4951{
4952 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4953 psa_key_type_t key_type = key_type_arg;
4954 psa_algorithm_t alg = alg_arg;
4955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4956
4957 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4958
4959 PSA_ASSERT( psa_crypto_init( ) );
4960
4961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4962 psa_set_key_algorithm( &attributes, alg );
4963 psa_set_key_type( &attributes, key_type );
4964
4965 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4966 &key ) );
4967
4968 PSA_ASSERT( psa_verify_message( key, alg,
4969 input_data->x, input_data->len,
4970 signature_data->x, signature_data->len ) );
4971
4972exit:
4973 psa_reset_key_attributes( &attributes );
4974 psa_destroy_key( key );
4975 PSA_DONE( );
4976}
4977/* END_CASE */
4978
4979/* BEGIN_CASE */
4980void verify_message_fail( int key_type_arg,
4981 data_t *key_data,
4982 int alg_arg,
4983 data_t *hash_data,
4984 data_t *signature_data,
4985 int expected_status_arg )
4986{
4987 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4988 psa_key_type_t key_type = key_type_arg;
4989 psa_algorithm_t alg = alg_arg;
4990 psa_status_t actual_status;
4991 psa_status_t expected_status = expected_status_arg;
4992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4993
4994 PSA_ASSERT( psa_crypto_init( ) );
4995
4996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4997 psa_set_key_algorithm( &attributes, alg );
4998 psa_set_key_type( &attributes, key_type );
4999
5000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5001 &key ) );
5002
5003 actual_status = psa_verify_message( key, alg,
5004 hash_data->x, hash_data->len,
5005 signature_data->x,
5006 signature_data->len );
5007 TEST_EQUAL( actual_status, expected_status );
5008
5009exit:
5010 psa_reset_key_attributes( &attributes );
5011 psa_destroy_key( key );
5012 PSA_DONE( );
5013}
5014/* END_CASE */
5015
5016/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005017void asymmetric_encrypt( int key_type_arg,
5018 data_t *key_data,
5019 int alg_arg,
5020 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005021 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005022 int expected_output_length_arg,
5023 int expected_status_arg )
5024{
Ronald Cron5425a212020-08-04 14:58:35 +02005025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005026 psa_key_type_t key_type = key_type_arg;
5027 psa_algorithm_t alg = alg_arg;
5028 size_t expected_output_length = expected_output_length_arg;
5029 size_t key_bits;
5030 unsigned char *output = NULL;
5031 size_t output_size;
5032 size_t output_length = ~0;
5033 psa_status_t actual_status;
5034 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005036
Gilles Peskine8817f612018-12-18 00:18:46 +01005037 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005038
Gilles Peskine656896e2018-06-29 19:12:28 +02005039 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005040 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5041 psa_set_key_algorithm( &attributes, alg );
5042 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005043 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005044 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005045
5046 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005047 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005048 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005049
Gilles Peskine656896e2018-06-29 19:12:28 +02005050 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005051 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005052 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005053
5054 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005055 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005056 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005057 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005058 output, output_size,
5059 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005060 TEST_EQUAL( actual_status, expected_status );
5061 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005062
Gilles Peskine68428122018-06-30 18:42:41 +02005063 /* If the label is empty, the test framework puts a non-null pointer
5064 * in label->x. Test that a null pointer works as well. */
5065 if( label->len == 0 )
5066 {
5067 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005068 if( output_size != 0 )
5069 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005070 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005071 input_data->x, input_data->len,
5072 NULL, label->len,
5073 output, output_size,
5074 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005075 TEST_EQUAL( actual_status, expected_status );
5076 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005077 }
5078
Gilles Peskine656896e2018-06-29 19:12:28 +02005079exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005080 /*
5081 * Key attributes may have been returned by psa_get_key_attributes()
5082 * thus reset them as required.
5083 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005084 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005085
Ronald Cron5425a212020-08-04 14:58:35 +02005086 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005087 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005088 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005089}
5090/* END_CASE */
5091
5092/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005093void asymmetric_encrypt_decrypt( int key_type_arg,
5094 data_t *key_data,
5095 int alg_arg,
5096 data_t *input_data,
5097 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005098{
Ronald Cron5425a212020-08-04 14:58:35 +02005099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005100 psa_key_type_t key_type = key_type_arg;
5101 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005102 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005103 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005104 size_t output_size;
5105 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005106 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005107 size_t output2_size;
5108 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005110
Gilles Peskine8817f612018-12-18 00:18:46 +01005111 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005112
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005113 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5114 psa_set_key_algorithm( &attributes, alg );
5115 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005116
Gilles Peskine049c7532019-05-15 20:22:09 +02005117 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005118 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005119
5120 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005121 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005122 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005123
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005124 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005125 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005126 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005127
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005128 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005129 TEST_ASSERT( output2_size <=
5130 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5131 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005132 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005133
Gilles Peskineeebd7382018-06-08 18:11:54 +02005134 /* We test encryption by checking that encrypt-then-decrypt gives back
5135 * the original plaintext because of the non-optional random
5136 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005137 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005138 input_data->x, input_data->len,
5139 label->x, label->len,
5140 output, output_size,
5141 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005142 /* We don't know what ciphertext length to expect, but check that
5143 * it looks sensible. */
5144 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005145
Ronald Cron5425a212020-08-04 14:58:35 +02005146 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005147 output, output_length,
5148 label->x, label->len,
5149 output2, output2_size,
5150 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005151 ASSERT_COMPARE( input_data->x, input_data->len,
5152 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005153
5154exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005155 /*
5156 * Key attributes may have been returned by psa_get_key_attributes()
5157 * thus reset them as required.
5158 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005159 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005160
Ronald Cron5425a212020-08-04 14:58:35 +02005161 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005162 mbedtls_free( output );
5163 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005164 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005165}
5166/* END_CASE */
5167
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005168/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005169void asymmetric_decrypt( int key_type_arg,
5170 data_t *key_data,
5171 int alg_arg,
5172 data_t *input_data,
5173 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005174 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005175{
Ronald Cron5425a212020-08-04 14:58:35 +02005176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005177 psa_key_type_t key_type = key_type_arg;
5178 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005179 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005180 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005181 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005182 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005184
Gilles Peskine8817f612018-12-18 00:18:46 +01005185 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005186
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005187 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5188 psa_set_key_algorithm( &attributes, alg );
5189 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005190
Gilles Peskine049c7532019-05-15 20:22:09 +02005191 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005192 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005193
gabor-mezei-armceface22021-01-21 12:26:17 +01005194 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5195 key_bits = psa_get_key_bits( &attributes );
5196
5197 /* Determine the maximum ciphertext length */
5198 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5199 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5200 ASSERT_ALLOC( output, output_size );
5201
Ronald Cron5425a212020-08-04 14:58:35 +02005202 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005203 input_data->x, input_data->len,
5204 label->x, label->len,
5205 output,
5206 output_size,
5207 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005208 ASSERT_COMPARE( expected_data->x, expected_data->len,
5209 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005210
Gilles Peskine68428122018-06-30 18:42:41 +02005211 /* If the label is empty, the test framework puts a non-null pointer
5212 * in label->x. Test that a null pointer works as well. */
5213 if( label->len == 0 )
5214 {
5215 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005216 if( output_size != 0 )
5217 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005218 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005219 input_data->x, input_data->len,
5220 NULL, label->len,
5221 output,
5222 output_size,
5223 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005224 ASSERT_COMPARE( expected_data->x, expected_data->len,
5225 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005226 }
5227
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005228exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005229 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005230 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005231 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005232 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005233}
5234/* END_CASE */
5235
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005236/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005237void asymmetric_decrypt_fail( int key_type_arg,
5238 data_t *key_data,
5239 int alg_arg,
5240 data_t *input_data,
5241 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005242 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005243 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005244{
Ronald Cron5425a212020-08-04 14:58:35 +02005245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005246 psa_key_type_t key_type = key_type_arg;
5247 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005248 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005249 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005250 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005251 psa_status_t actual_status;
5252 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005254
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005255 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005256
Gilles Peskine8817f612018-12-18 00:18:46 +01005257 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005258
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005259 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5260 psa_set_key_algorithm( &attributes, alg );
5261 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005262
Gilles Peskine049c7532019-05-15 20:22:09 +02005263 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005264 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005265
Ronald Cron5425a212020-08-04 14:58:35 +02005266 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005267 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005268 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005269 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005270 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005271 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005272 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005273
Gilles Peskine68428122018-06-30 18:42:41 +02005274 /* If the label is empty, the test framework puts a non-null pointer
5275 * in label->x. Test that a null pointer works as well. */
5276 if( label->len == 0 )
5277 {
5278 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005279 if( output_size != 0 )
5280 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005281 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005282 input_data->x, input_data->len,
5283 NULL, label->len,
5284 output, output_size,
5285 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005286 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005287 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005288 }
5289
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005290exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005291 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005292 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005293 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005294 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005295}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005296/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005297
5298/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005299void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005300{
5301 /* Test each valid way of initializing the object, except for `= {0}`, as
5302 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5303 * though it's OK by the C standard. We could test for this, but we'd need
5304 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005305 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005306 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5307 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5308 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005309
5310 memset( &zero, 0, sizeof( zero ) );
5311
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005312 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005313 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005314 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005315 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005316 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005317 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005318 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005319
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005321 PSA_ASSERT( psa_key_derivation_abort(&func) );
5322 PSA_ASSERT( psa_key_derivation_abort(&init) );
5323 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005324}
5325/* END_CASE */
5326
Janos Follath16de4a42019-06-13 16:32:24 +01005327/* BEGIN_CASE */
5328void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005329{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005330 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005331 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005333
Gilles Peskine8817f612018-12-18 00:18:46 +01005334 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005335
Janos Follath16de4a42019-06-13 16:32:24 +01005336 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005337 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005338
5339exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005340 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005341 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005342}
5343/* END_CASE */
5344
Janos Follathaf3c2a02019-06-12 12:34:34 +01005345/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005346void derive_set_capacity( int alg_arg, int capacity_arg,
5347 int expected_status_arg )
5348{
5349 psa_algorithm_t alg = alg_arg;
5350 size_t capacity = capacity_arg;
5351 psa_status_t expected_status = expected_status_arg;
5352 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5353
5354 PSA_ASSERT( psa_crypto_init( ) );
5355
5356 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5357
5358 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5359 expected_status );
5360
5361exit:
5362 psa_key_derivation_abort( &operation );
5363 PSA_DONE( );
5364}
5365/* END_CASE */
5366
5367/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005368void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005369 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005370 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005371 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005372 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005373 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005374 int expected_status_arg3,
5375 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005376{
5377 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005378 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5379 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005380 psa_status_t expected_statuses[] = {expected_status_arg1,
5381 expected_status_arg2,
5382 expected_status_arg3};
5383 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005384 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5385 MBEDTLS_SVC_KEY_ID_INIT,
5386 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005387 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5388 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5389 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005390 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005391 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005392 psa_status_t expected_output_status = expected_output_status_arg;
5393 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005394
5395 PSA_ASSERT( psa_crypto_init( ) );
5396
5397 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5398 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005399
5400 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5401
5402 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5403 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005404 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005405 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005406 psa_set_key_type( &attributes, key_types[i] );
5407 PSA_ASSERT( psa_import_key( &attributes,
5408 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005409 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005410 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5411 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5412 {
5413 // When taking a private key as secret input, use key agreement
5414 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005415 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5416 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005417 expected_statuses[i] );
5418 }
5419 else
5420 {
5421 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005422 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005423 expected_statuses[i] );
5424 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005425 }
5426 else
5427 {
5428 TEST_EQUAL( psa_key_derivation_input_bytes(
5429 &operation, steps[i],
5430 inputs[i]->x, inputs[i]->len ),
5431 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005432 }
5433 }
5434
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005435 if( output_key_type != PSA_KEY_TYPE_NONE )
5436 {
5437 psa_reset_key_attributes( &attributes );
5438 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5439 psa_set_key_bits( &attributes, 8 );
5440 actual_output_status =
5441 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005442 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005443 }
5444 else
5445 {
5446 uint8_t buffer[1];
5447 actual_output_status =
5448 psa_key_derivation_output_bytes( &operation,
5449 buffer, sizeof( buffer ) );
5450 }
5451 TEST_EQUAL( actual_output_status, expected_output_status );
5452
Janos Follathaf3c2a02019-06-12 12:34:34 +01005453exit:
5454 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005455 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5456 psa_destroy_key( keys[i] );
5457 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005458 PSA_DONE( );
5459}
5460/* END_CASE */
5461
Janos Follathd958bb72019-07-03 15:02:16 +01005462/* BEGIN_CASE */
5463void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005464{
Janos Follathd958bb72019-07-03 15:02:16 +01005465 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005466 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005467 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005469 unsigned char input1[] = "Input 1";
5470 size_t input1_length = sizeof( input1 );
5471 unsigned char input2[] = "Input 2";
5472 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005473 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005474 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005475 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5476 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5477 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005479
Gilles Peskine8817f612018-12-18 00:18:46 +01005480 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005481
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005482 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5483 psa_set_key_algorithm( &attributes, alg );
5484 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005485
Gilles Peskine73676cb2019-05-15 20:15:10 +02005486 PSA_ASSERT( psa_import_key( &attributes,
5487 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005488 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005489
5490 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005491 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5492 input1, input1_length,
5493 input2, input2_length,
5494 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005495 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005496
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005497 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005498 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005499 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005500
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005501 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005502
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005503 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005504 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005505
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005506exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005507 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005508 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005509 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005510}
5511/* END_CASE */
5512
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005513/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005514void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005515{
5516 uint8_t output_buffer[16];
5517 size_t buffer_size = 16;
5518 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005519 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005520
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005521 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5522 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005523 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005524
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005526 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005527
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005528 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005529
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005530 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5531 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005532 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005533
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005534 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005535 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005536
5537exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005538 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005539}
5540/* END_CASE */
5541
5542/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005543void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005544 int step1_arg, data_t *input1,
5545 int step2_arg, data_t *input2,
5546 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005547 int requested_capacity_arg,
5548 data_t *expected_output1,
5549 data_t *expected_output2 )
5550{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005551 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005552 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5553 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005554 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5555 MBEDTLS_SVC_KEY_ID_INIT,
5556 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005557 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005558 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005559 uint8_t *expected_outputs[2] =
5560 {expected_output1->x, expected_output2->x};
5561 size_t output_sizes[2] =
5562 {expected_output1->len, expected_output2->len};
5563 size_t output_buffer_size = 0;
5564 uint8_t *output_buffer = NULL;
5565 size_t expected_capacity;
5566 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005568 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005569 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005570
5571 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5572 {
5573 if( output_sizes[i] > output_buffer_size )
5574 output_buffer_size = output_sizes[i];
5575 if( output_sizes[i] == 0 )
5576 expected_outputs[i] = NULL;
5577 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005578 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005579 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005580
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005581 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5582 psa_set_key_algorithm( &attributes, alg );
5583 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005584
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005585 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005586 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5587 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5588 requested_capacity ) );
5589 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005590 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005591 switch( steps[i] )
5592 {
5593 case 0:
5594 break;
5595 case PSA_KEY_DERIVATION_INPUT_SECRET:
5596 PSA_ASSERT( psa_import_key( &attributes,
5597 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005598 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005599
5600 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5601 {
5602 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5603 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5604 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5605 }
5606
Gilles Peskine1468da72019-05-29 17:35:49 +02005607 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005608 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005609 break;
5610 default:
5611 PSA_ASSERT( psa_key_derivation_input_bytes(
5612 &operation, steps[i],
5613 inputs[i]->x, inputs[i]->len ) );
5614 break;
5615 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005616 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005617
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005618 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005619 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005620 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005621 expected_capacity = requested_capacity;
5622
5623 /* Expansion phase. */
5624 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5625 {
5626 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005627 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005628 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005629 if( expected_capacity == 0 && output_sizes[i] == 0 )
5630 {
5631 /* Reading 0 bytes when 0 bytes are available can go either way. */
5632 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005633 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005634 continue;
5635 }
5636 else if( expected_capacity == 0 ||
5637 output_sizes[i] > expected_capacity )
5638 {
5639 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005640 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005641 expected_capacity = 0;
5642 continue;
5643 }
5644 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005645 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005646 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005647 ASSERT_COMPARE( output_buffer, output_sizes[i],
5648 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005649 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005650 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005651 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005652 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005653 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005654 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005655 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005656
5657exit:
5658 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005659 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005660 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5661 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005662 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005663}
5664/* END_CASE */
5665
5666/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005667void derive_full( int alg_arg,
5668 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005669 data_t *input1,
5670 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005671 int requested_capacity_arg )
5672{
Ronald Cron5425a212020-08-04 14:58:35 +02005673 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005674 psa_algorithm_t alg = alg_arg;
5675 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005676 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005677 unsigned char output_buffer[16];
5678 size_t expected_capacity = requested_capacity;
5679 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005680 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005681
Gilles Peskine8817f612018-12-18 00:18:46 +01005682 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005683
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005684 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5685 psa_set_key_algorithm( &attributes, alg );
5686 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005687
Gilles Peskine049c7532019-05-15 20:22:09 +02005688 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005689 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005690
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005691 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5692 input1->x, input1->len,
5693 input2->x, input2->len,
5694 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005695 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005696
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005697 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005698 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005699 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005700
5701 /* Expansion phase. */
5702 while( current_capacity > 0 )
5703 {
5704 size_t read_size = sizeof( output_buffer );
5705 if( read_size > current_capacity )
5706 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005707 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005708 output_buffer,
5709 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005710 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005711 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005712 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005713 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005714 }
5715
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005716 /* Check that the operation refuses to go over capacity. */
5717 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005718 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005719
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005721
5722exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005724 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005725 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005726}
5727/* END_CASE */
5728
Janos Follathe60c9052019-07-03 13:51:30 +01005729/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005730void derive_key_exercise( int alg_arg,
5731 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005732 data_t *input1,
5733 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005734 int derived_type_arg,
5735 int derived_bits_arg,
5736 int derived_usage_arg,
5737 int derived_alg_arg )
5738{
Ronald Cron5425a212020-08-04 14:58:35 +02005739 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5740 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005741 psa_algorithm_t alg = alg_arg;
5742 psa_key_type_t derived_type = derived_type_arg;
5743 size_t derived_bits = derived_bits_arg;
5744 psa_key_usage_t derived_usage = derived_usage_arg;
5745 psa_algorithm_t derived_alg = derived_alg_arg;
5746 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005747 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005749 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005750
Gilles Peskine8817f612018-12-18 00:18:46 +01005751 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005752
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005753 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5754 psa_set_key_algorithm( &attributes, alg );
5755 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005756 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005757 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005758
5759 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005760 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5761 input1->x, input1->len,
5762 input2->x, input2->len,
5763 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005764 goto exit;
5765
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005766 psa_set_key_usage_flags( &attributes, derived_usage );
5767 psa_set_key_algorithm( &attributes, derived_alg );
5768 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005769 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005770 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005771 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005772
5773 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005774 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005775 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5776 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005777
5778 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005779 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005780 goto exit;
5781
5782exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005783 /*
5784 * Key attributes may have been returned by psa_get_key_attributes()
5785 * thus reset them as required.
5786 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005787 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005788
5789 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005790 psa_destroy_key( base_key );
5791 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005792 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005793}
5794/* END_CASE */
5795
Janos Follath42fd8882019-07-03 14:17:09 +01005796/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005797void derive_key_export( int alg_arg,
5798 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005799 data_t *input1,
5800 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005801 int bytes1_arg,
5802 int bytes2_arg )
5803{
Ronald Cron5425a212020-08-04 14:58:35 +02005804 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5805 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005806 psa_algorithm_t alg = alg_arg;
5807 size_t bytes1 = bytes1_arg;
5808 size_t bytes2 = bytes2_arg;
5809 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005810 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005811 uint8_t *output_buffer = NULL;
5812 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005813 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5814 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005815 size_t length;
5816
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005817 ASSERT_ALLOC( output_buffer, capacity );
5818 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005819 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005820
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005821 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5822 psa_set_key_algorithm( &base_attributes, alg );
5823 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005824 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005825 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005826
5827 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005828 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5829 input1->x, input1->len,
5830 input2->x, input2->len,
5831 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005832 goto exit;
5833
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005834 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005835 output_buffer,
5836 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005837 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005838
5839 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005840 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5841 input1->x, input1->len,
5842 input2->x, input2->len,
5843 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005844 goto exit;
5845
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005846 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5847 psa_set_key_algorithm( &derived_attributes, 0 );
5848 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005849 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005850 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005851 &derived_key ) );
5852 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005853 export_buffer, bytes1,
5854 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005855 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005856 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005857 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005858 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005859 &derived_key ) );
5860 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005861 export_buffer + bytes1, bytes2,
5862 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005863 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005864
5865 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005866 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5867 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005868
5869exit:
5870 mbedtls_free( output_buffer );
5871 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005872 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005873 psa_destroy_key( base_key );
5874 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005875 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005876}
5877/* END_CASE */
5878
5879/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005880void derive_key( int alg_arg,
5881 data_t *key_data, data_t *input1, data_t *input2,
5882 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005883 int expected_status_arg,
5884 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005885{
Ronald Cron5425a212020-08-04 14:58:35 +02005886 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5887 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005888 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005889 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005890 size_t bits = bits_arg;
5891 psa_status_t expected_status = expected_status_arg;
5892 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5893 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5894 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5895
5896 PSA_ASSERT( psa_crypto_init( ) );
5897
5898 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5899 psa_set_key_algorithm( &base_attributes, alg );
5900 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5901 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005902 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005903
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005904 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5905 input1->x, input1->len,
5906 input2->x, input2->len,
5907 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005908 goto exit;
5909
5910 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5911 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005912 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005913 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005914
5915 psa_status_t status =
5916 psa_key_derivation_output_key( &derived_attributes,
5917 &operation,
5918 &derived_key );
5919 if( is_large_output > 0 )
5920 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5921 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005922
5923exit:
5924 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005925 psa_destroy_key( base_key );
5926 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005927 PSA_DONE( );
5928}
5929/* END_CASE */
5930
5931/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005932void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005933 int our_key_type_arg, int our_key_alg_arg,
5934 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005935 int expected_status_arg )
5936{
Ronald Cron5425a212020-08-04 14:58:35 +02005937 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005938 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005939 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005940 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005941 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005943 psa_status_t expected_status = expected_status_arg;
5944 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005945
Gilles Peskine8817f612018-12-18 00:18:46 +01005946 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005947
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005948 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005949 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005950 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005951 PSA_ASSERT( psa_import_key( &attributes,
5952 our_key_data->x, our_key_data->len,
5953 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005954
Gilles Peskine77f40d82019-04-11 21:27:06 +02005955 /* The tests currently include inputs that should fail at either step.
5956 * Test cases that fail at the setup step should be changed to call
5957 * key_derivation_setup instead, and this function should be renamed
5958 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005959 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005960 if( status == PSA_SUCCESS )
5961 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005962 TEST_EQUAL( psa_key_derivation_key_agreement(
5963 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5964 our_key,
5965 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005966 expected_status );
5967 }
5968 else
5969 {
5970 TEST_ASSERT( status == expected_status );
5971 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005972
5973exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005974 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005975 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005976 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005977}
5978/* END_CASE */
5979
5980/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005981void raw_key_agreement( int alg_arg,
5982 int our_key_type_arg, data_t *our_key_data,
5983 data_t *peer_key_data,
5984 data_t *expected_output )
5985{
Ronald Cron5425a212020-08-04 14:58:35 +02005986 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005987 psa_algorithm_t alg = alg_arg;
5988 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005990 unsigned char *output = NULL;
5991 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005992 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005993
5994 ASSERT_ALLOC( output, expected_output->len );
5995 PSA_ASSERT( psa_crypto_init( ) );
5996
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005997 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5998 psa_set_key_algorithm( &attributes, alg );
5999 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006000 PSA_ASSERT( psa_import_key( &attributes,
6001 our_key_data->x, our_key_data->len,
6002 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006003
gabor-mezei-armceface22021-01-21 12:26:17 +01006004 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6005 key_bits = psa_get_key_bits( &attributes );
6006
Gilles Peskinebe697d82019-05-16 18:00:41 +02006007 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6008 peer_key_data->x, peer_key_data->len,
6009 output, expected_output->len,
6010 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006011 ASSERT_COMPARE( output, output_length,
6012 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006013 TEST_ASSERT( output_length <=
6014 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6015 TEST_ASSERT( output_length <=
6016 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006017
6018exit:
6019 mbedtls_free( output );
6020 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006021 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006022}
6023/* END_CASE */
6024
6025/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006026void key_agreement_capacity( int alg_arg,
6027 int our_key_type_arg, data_t *our_key_data,
6028 data_t *peer_key_data,
6029 int expected_capacity_arg )
6030{
Ronald Cron5425a212020-08-04 14:58:35 +02006031 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006032 psa_algorithm_t alg = alg_arg;
6033 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006034 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006036 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006037 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006038
Gilles Peskine8817f612018-12-18 00:18:46 +01006039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006040
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006041 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6042 psa_set_key_algorithm( &attributes, alg );
6043 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006044 PSA_ASSERT( psa_import_key( &attributes,
6045 our_key_data->x, our_key_data->len,
6046 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006047
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006048 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006049 PSA_ASSERT( psa_key_derivation_key_agreement(
6050 &operation,
6051 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6052 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006053 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6054 {
6055 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006056 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006057 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006058 NULL, 0 ) );
6059 }
Gilles Peskine59685592018-09-18 12:11:34 +02006060
Gilles Peskinebf491972018-10-25 22:36:12 +02006061 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006062 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006063 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006064 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006065
Gilles Peskinebf491972018-10-25 22:36:12 +02006066 /* Test the actual capacity by reading the output. */
6067 while( actual_capacity > sizeof( output ) )
6068 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006069 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006070 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006071 actual_capacity -= sizeof( output );
6072 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006073 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006074 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006075 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006076 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006077
Gilles Peskine59685592018-09-18 12:11:34 +02006078exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006079 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006080 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006081 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006082}
6083/* END_CASE */
6084
6085/* BEGIN_CASE */
6086void key_agreement_output( int alg_arg,
6087 int our_key_type_arg, data_t *our_key_data,
6088 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006089 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006090{
Ronald Cron5425a212020-08-04 14:58:35 +02006091 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006092 psa_algorithm_t alg = alg_arg;
6093 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006094 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006095 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006096 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006097
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006098 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6099 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006100
Gilles Peskine8817f612018-12-18 00:18:46 +01006101 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006102
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006103 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6104 psa_set_key_algorithm( &attributes, alg );
6105 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006106 PSA_ASSERT( psa_import_key( &attributes,
6107 our_key_data->x, our_key_data->len,
6108 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006109
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006110 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006111 PSA_ASSERT( psa_key_derivation_key_agreement(
6112 &operation,
6113 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6114 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006115 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6116 {
6117 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006118 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006119 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006120 NULL, 0 ) );
6121 }
Gilles Peskine59685592018-09-18 12:11:34 +02006122
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006123 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006124 actual_output,
6125 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006126 ASSERT_COMPARE( actual_output, expected_output1->len,
6127 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006128 if( expected_output2->len != 0 )
6129 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006130 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006131 actual_output,
6132 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006133 ASSERT_COMPARE( actual_output, expected_output2->len,
6134 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006135 }
Gilles Peskine59685592018-09-18 12:11:34 +02006136
6137exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006138 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006139 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006140 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006141 mbedtls_free( actual_output );
6142}
6143/* END_CASE */
6144
6145/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006146void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006147{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006148 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006149 unsigned char *output = NULL;
6150 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006151 size_t i;
6152 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006153
Simon Butcher49f8e312020-03-03 15:51:50 +00006154 TEST_ASSERT( bytes_arg >= 0 );
6155
Gilles Peskine91892022021-02-08 19:50:26 +01006156 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006157 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006158
Gilles Peskine8817f612018-12-18 00:18:46 +01006159 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006160
Gilles Peskinea50d7392018-06-21 10:22:13 +02006161 /* Run several times, to ensure that every output byte will be
6162 * nonzero at least once with overwhelming probability
6163 * (2^(-8*number_of_runs)). */
6164 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006165 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006166 if( bytes != 0 )
6167 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006168 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006169
Gilles Peskinea50d7392018-06-21 10:22:13 +02006170 for( i = 0; i < bytes; i++ )
6171 {
6172 if( output[i] != 0 )
6173 ++changed[i];
6174 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006175 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006176
6177 /* Check that every byte was changed to nonzero at least once. This
6178 * validates that psa_generate_random is overwriting every byte of
6179 * the output buffer. */
6180 for( i = 0; i < bytes; i++ )
6181 {
6182 TEST_ASSERT( changed[i] != 0 );
6183 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006184
6185exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006186 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006187 mbedtls_free( output );
6188 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006189}
6190/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006191
6192/* BEGIN_CASE */
6193void generate_key( int type_arg,
6194 int bits_arg,
6195 int usage_arg,
6196 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006197 int expected_status_arg,
6198 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006199{
Ronald Cron5425a212020-08-04 14:58:35 +02006200 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006201 psa_key_type_t type = type_arg;
6202 psa_key_usage_t usage = usage_arg;
6203 size_t bits = bits_arg;
6204 psa_algorithm_t alg = alg_arg;
6205 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006207 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006208
Gilles Peskine8817f612018-12-18 00:18:46 +01006209 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006210
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006211 psa_set_key_usage_flags( &attributes, usage );
6212 psa_set_key_algorithm( &attributes, alg );
6213 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006214 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006215
6216 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006217 psa_status_t status = psa_generate_key( &attributes, &key );
6218
6219 if( is_large_key > 0 )
6220 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6221 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006222 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006223 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006224
6225 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006226 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006227 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6228 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006229
Gilles Peskine818ca122018-06-20 18:16:48 +02006230 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006231 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006232 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006233
6234exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006235 /*
6236 * Key attributes may have been returned by psa_get_key_attributes()
6237 * thus reset them as required.
6238 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006239 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006240
Ronald Cron5425a212020-08-04 14:58:35 +02006241 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006242 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006243}
6244/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006245
Ronald Cronee414c72021-03-18 18:50:08 +01006246/* 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 +02006247void generate_key_rsa( int bits_arg,
6248 data_t *e_arg,
6249 int expected_status_arg )
6250{
Ronald Cron5425a212020-08-04 14:58:35 +02006251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006252 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006253 size_t bits = bits_arg;
6254 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6255 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6256 psa_status_t expected_status = expected_status_arg;
6257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6258 uint8_t *exported = NULL;
6259 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006260 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006261 size_t exported_length = SIZE_MAX;
6262 uint8_t *e_read_buffer = NULL;
6263 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006264 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006265 size_t e_read_length = SIZE_MAX;
6266
6267 if( e_arg->len == 0 ||
6268 ( e_arg->len == 3 &&
6269 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6270 {
6271 is_default_public_exponent = 1;
6272 e_read_size = 0;
6273 }
6274 ASSERT_ALLOC( e_read_buffer, e_read_size );
6275 ASSERT_ALLOC( exported, exported_size );
6276
6277 PSA_ASSERT( psa_crypto_init( ) );
6278
6279 psa_set_key_usage_flags( &attributes, usage );
6280 psa_set_key_algorithm( &attributes, alg );
6281 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6282 e_arg->x, e_arg->len ) );
6283 psa_set_key_bits( &attributes, bits );
6284
6285 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006286 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006287 if( expected_status != PSA_SUCCESS )
6288 goto exit;
6289
6290 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006291 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006292 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6293 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6294 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6295 e_read_buffer, e_read_size,
6296 &e_read_length ) );
6297 if( is_default_public_exponent )
6298 TEST_EQUAL( e_read_length, 0 );
6299 else
6300 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6301
6302 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006303 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006304 goto exit;
6305
6306 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006307 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006308 exported, exported_size,
6309 &exported_length ) );
6310 {
6311 uint8_t *p = exported;
6312 uint8_t *end = exported + exported_length;
6313 size_t len;
6314 /* RSAPublicKey ::= SEQUENCE {
6315 * modulus INTEGER, -- n
6316 * publicExponent INTEGER } -- e
6317 */
6318 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006319 MBEDTLS_ASN1_SEQUENCE |
6320 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006321 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006322 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6323 MBEDTLS_ASN1_INTEGER ) );
6324 if( len >= 1 && p[0] == 0 )
6325 {
6326 ++p;
6327 --len;
6328 }
6329 if( e_arg->len == 0 )
6330 {
6331 TEST_EQUAL( len, 3 );
6332 TEST_EQUAL( p[0], 1 );
6333 TEST_EQUAL( p[1], 0 );
6334 TEST_EQUAL( p[2], 1 );
6335 }
6336 else
6337 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6338 }
6339
6340exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006341 /*
6342 * Key attributes may have been returned by psa_get_key_attributes() or
6343 * set by psa_set_key_domain_parameters() thus reset them as required.
6344 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006345 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006346
Ronald Cron5425a212020-08-04 14:58:35 +02006347 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006348 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006349 mbedtls_free( e_read_buffer );
6350 mbedtls_free( exported );
6351}
6352/* END_CASE */
6353
Darryl Greend49a4992018-06-18 17:27:26 +01006354/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006355void persistent_key_load_key_from_storage( data_t *data,
6356 int type_arg, int bits_arg,
6357 int usage_flags_arg, int alg_arg,
6358 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006359{
Ronald Cron71016a92020-08-28 19:01:50 +02006360 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6363 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006364 psa_key_type_t type = type_arg;
6365 size_t bits = bits_arg;
6366 psa_key_usage_t usage_flags = usage_flags_arg;
6367 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006368 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006369 unsigned char *first_export = NULL;
6370 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006371 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006372 size_t first_exported_length;
6373 size_t second_exported_length;
6374
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006375 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6376 {
6377 ASSERT_ALLOC( first_export, export_size );
6378 ASSERT_ALLOC( second_export, export_size );
6379 }
Darryl Greend49a4992018-06-18 17:27:26 +01006380
Gilles Peskine8817f612018-12-18 00:18:46 +01006381 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006382
Gilles Peskinec87af662019-05-15 16:12:22 +02006383 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006384 psa_set_key_usage_flags( &attributes, usage_flags );
6385 psa_set_key_algorithm( &attributes, alg );
6386 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006387 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006388
Darryl Green0c6575a2018-11-07 16:05:30 +00006389 switch( generation_method )
6390 {
6391 case IMPORT_KEY:
6392 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006393 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006394 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006395 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006396
Darryl Green0c6575a2018-11-07 16:05:30 +00006397 case GENERATE_KEY:
6398 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006399 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006400 break;
6401
6402 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006403#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006404 {
6405 /* Create base key */
6406 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6407 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6408 psa_set_key_usage_flags( &base_attributes,
6409 PSA_KEY_USAGE_DERIVE );
6410 psa_set_key_algorithm( &base_attributes, derive_alg );
6411 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006412 PSA_ASSERT( psa_import_key( &base_attributes,
6413 data->x, data->len,
6414 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006415 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006416 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006417 PSA_ASSERT( psa_key_derivation_input_key(
6418 &operation,
6419 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006420 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006421 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006422 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006423 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6424 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006425 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006426 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006427 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006428 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006429 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006430#else
6431 TEST_ASSUME( ! "KDF not supported in this configuration" );
6432#endif
6433 break;
6434
6435 default:
6436 TEST_ASSERT( ! "generation_method not implemented in test" );
6437 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006438 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006439 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006440
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006441 /* Export the key if permitted by the key policy. */
6442 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6443 {
Ronald Cron5425a212020-08-04 14:58:35 +02006444 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006445 first_export, export_size,
6446 &first_exported_length ) );
6447 if( generation_method == IMPORT_KEY )
6448 ASSERT_COMPARE( data->x, data->len,
6449 first_export, first_exported_length );
6450 }
Darryl Greend49a4992018-06-18 17:27:26 +01006451
6452 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006453 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006454 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006455 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006456
Darryl Greend49a4992018-06-18 17:27:26 +01006457 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006458 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006459 TEST_ASSERT( mbedtls_svc_key_id_equal(
6460 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006461 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6462 PSA_KEY_LIFETIME_PERSISTENT );
6463 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6464 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6465 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6466 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006467
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006468 /* Export the key again if permitted by the key policy. */
6469 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006470 {
Ronald Cron5425a212020-08-04 14:58:35 +02006471 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006472 second_export, export_size,
6473 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006474 ASSERT_COMPARE( first_export, first_exported_length,
6475 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006476 }
6477
6478 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006479 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006480 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006481
6482exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006483 /*
6484 * Key attributes may have been returned by psa_get_key_attributes()
6485 * thus reset them as required.
6486 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006487 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006488
Darryl Greend49a4992018-06-18 17:27:26 +01006489 mbedtls_free( first_export );
6490 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006491 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006492 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006493 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006494 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006495}
6496/* END_CASE */