blob: 99183991d062f36d42a1378df2fb6c397a594cdb [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Paul Elliott33746aa2021-09-15 16:40:40 +0100267typedef enum
268{
269 DO_NOT_SET_LENGTHS = 0,
270 SET_LENGTHS_BEFORE_NONCE = 1,
271 SET_LENGTHS_AFTER_NONCE = 2
272} setlengths_method;
273
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100274/*!
275 * \brief Internal Function for AEAD multipart tests.
276 *
277 * \param key_type_arg Type of key passed in
278 * \param key_data The encryption / decryption key data
279 * \param alg_arg The type of algorithm used
280 * \param nonce Nonce data
281 * \param additional_data Additional data
282 * \param ad_part_len If not -1, the length of chunks to
283 * feed additional data in to be encrypted /
284 * decrypted. If -1, no chunking.
285 * \param input_data Data to encrypt / decrypt
286 * \param data_part_len If not -1, the length of chunks to feed the
287 * data in to be encrypted / decrypted. If -1,
288 * no chunking
289 * \param do_set_lengths If non-zero, then set lengths prior to
290 * calling encryption / decryption.
291 * \param expected_output Expected output
Paul Elliott41ffae12021-07-22 21:52:01 +0100292 * \param expect_valid_signature If non zero, we expect the signature to be
293 * valid
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100294 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100295 * \param do_zero_parts If non-zero, interleave zero length chunks
296 * with normal length chunks
297 * \param swap_set_functions If non-zero, swap the order of set lengths
298 * and set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100299 *
300 * \return int Zero on failure, non-zero on success.
301 *
302 */
303static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
304 int alg_arg,
305 data_t *nonce,
306 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100307 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100308 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100309 int data_part_len_arg,
Paul Elliott33746aa2021-09-15 16:40:40 +0100310 setlengths_method set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100311 data_t *expected_output,
312 int expect_valid_signature,
Paul Elliott329d5382021-07-22 17:10:45 +0100313 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100314 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100315{
316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
317 psa_key_type_t key_type = key_type_arg;
318 psa_algorithm_t alg = alg_arg;
319 psa_aead_operation_t operation;
320 unsigned char *output_data = NULL;
321 unsigned char *part_data = NULL;
322 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100324 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 size_t output_size = 0;
326 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100327 size_t output_length = 0;
328 size_t key_bits = 0;
329 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100330 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100331 size_t part_length = 0;
332 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100333 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100334 size_t ad_part_len = 0;
335 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
338 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
339
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100341 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342
Paul Elliottd3f82412021-06-16 16:52:21 +0100343 PSA_ASSERT( psa_crypto_init( ) );
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 if( is_encrypt )
346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
347 else
348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
349
Paul Elliottd3f82412021-06-16 16:52:21 +0100350 psa_set_key_algorithm( &attributes, alg );
351 psa_set_key_type( &attributes, key_type );
352
353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
354 &key ) );
355
356 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
357 key_bits = psa_get_key_bits( &attributes );
358
359 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 if( is_encrypt )
362 {
363 /* Tag gets written at end of buffer. */
364 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
365 ( input_data->len +
366 tag_length ) );
367 data_true_size = input_data->len;
368 }
369 else
370 {
371 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
372 ( input_data->len -
373 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 /* Do not want to attempt to decrypt tag. */
376 data_true_size = input_data->len - tag_length;
377 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100378
379 ASSERT_ALLOC( output_data, output_size );
380
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 if( is_encrypt )
382 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100383 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
384 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385 }
386 else
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100391
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 operation = psa_aead_operation_init( );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396
397 if( is_encrypt )
398 status = psa_aead_encrypt_setup( &operation, key, alg );
399 else
400 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100401
402 /* If the operation is not supported, just skip and not fail in case the
403 * encryption involves a common limitation of cryptography hardwares and
404 * an alternative implementation. */
405 if( status == PSA_ERROR_NOT_SUPPORTED )
406 {
407 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
408 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
409 }
410
411 PSA_ASSERT( status );
412
Paul Elliott33746aa2021-09-15 16:40:40 +0100413 if( set_lengths_method == DO_NOT_SET_LENGTHS )
414 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
415 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100416 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100417 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
418 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100421 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100422 {
423 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
426 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100427 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100428
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100429 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100430 {
431 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100432 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100433 part_offset = 0;
434
435 while( part_offset < additional_data->len )
436 {
Paul Elliott329d5382021-07-22 17:10:45 +0100437 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100438 {
Paul Elliott329d5382021-07-22 17:10:45 +0100439 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100440 }
441 else
442 {
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100443 if( additional_data->len - part_offset < ad_part_len )
Paul Elliott329d5382021-07-22 17:10:45 +0100444 {
445 part_length = additional_data->len - part_offset;
446 }
447 else
448 {
449 part_length = ad_part_len;
450 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 }
452
453 PSA_ASSERT( psa_aead_update_ad( &operation,
454 additional_data->x + part_offset,
455 part_length ) );
456
457 part_offset += part_length;
458 }
459 }
460 else
461 {
462 /* Pass additional data in one go. */
463 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
464 additional_data->len ) );
465 }
466
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100467 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100468 {
469 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100470 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100471 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100472 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100473
474 ASSERT_ALLOC( part_data, part_data_size );
475
476 part_offset = 0;
477
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100478 while( part_offset < data_true_size )
Paul Elliottd3f82412021-06-16 16:52:21 +0100479 {
Paul Elliott329d5382021-07-22 17:10:45 +0100480 if( do_zero_parts && part_count++ & 0x01 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 {
Paul Elliott329d5382021-07-22 17:10:45 +0100482 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 }
484 else
485 {
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100486 if( ( data_true_size - part_offset ) < data_part_len )
Paul Elliott329d5382021-07-22 17:10:45 +0100487 {
488 part_length = ( data_true_size - part_offset );
489 }
490 else
491 {
492 part_length = data_part_len;
493 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100494 }
495
496 PSA_ASSERT( psa_aead_update( &operation,
497 ( input_data->x + part_offset ),
498 part_length, part_data,
499 part_data_size,
500 &output_part_length ) );
501
502 if( output_data && output_part_length )
503 {
504 memcpy( ( output_data + part_offset ), part_data,
505 output_part_length );
506 }
507
508 part_offset += part_length;
509 output_length += output_part_length;
510 }
511 }
512 else
513 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100514 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100515 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100516 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100517 output_size, &output_length ) );
518 }
519
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100520 if( is_encrypt )
521 PSA_ASSERT( psa_aead_finish( &operation, final_data,
522 final_output_size,
523 &output_part_length,
524 tag_buffer, tag_length,
525 &tag_size ) );
526 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100527 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100528 status = psa_aead_verify( &operation, final_data,
529 final_output_size,
530 &output_part_length,
531 ( input_data->x + data_true_size ),
532 tag_length );
533
534 if( status != PSA_SUCCESS )
535 {
536 if( !expect_valid_signature )
537 {
538 /* Expected failure. */
539 test_ok = 1;
540 goto exit;
541 }
542 else
543 PSA_ASSERT( status );
544 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100545 }
546
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100547 if( output_data && output_part_length )
548 memcpy( ( output_data + output_length ), final_data,
549 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100550
551 output_length += output_part_length;
552
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100553
554 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
555 * should be exact.*/
556 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100557 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100558 TEST_EQUAL( tag_length, tag_size );
559
560 if( output_data && tag_length )
561 memcpy( ( output_data + output_length ), tag_buffer,
562 tag_length );
563
564 output_length += tag_length;
565
566 TEST_EQUAL( output_length,
567 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
568 input_data->len ) );
569 TEST_ASSERT( output_length <=
570 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
571 }
572 else
573 {
574 TEST_EQUAL( output_length,
575 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
576 input_data->len ) );
577 TEST_ASSERT( output_length <=
578 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100579 }
580
Paul Elliottd3f82412021-06-16 16:52:21 +0100581
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100582 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100583 output_data, output_length );
584
Paul Elliottd3f82412021-06-16 16:52:21 +0100585
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100586 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100587
588exit:
589 psa_destroy_key( key );
590 psa_aead_abort( &operation );
591 mbedtls_free( output_data );
592 mbedtls_free( part_data );
593 mbedtls_free( final_data );
594 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100595
596 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100597}
598
Gilles Peskinee59236f2018-01-27 23:32:46 +0100599/* END_HEADER */
600
601/* BEGIN_DEPENDENCIES
602 * depends_on:MBEDTLS_PSA_CRYPTO_C
603 * END_DEPENDENCIES
604 */
605
606/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200607void static_checks( )
608{
609 size_t max_truncated_mac_size =
610 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
611
612 /* Check that the length for a truncated MAC always fits in the algorithm
613 * encoding. The shifted mask is the maximum truncated value. The
614 * untruncated algorithm may be one byte larger. */
615 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
616}
617/* END_CASE */
618
619/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200620void import_with_policy( int type_arg,
621 int usage_arg, int alg_arg,
622 int expected_status_arg )
623{
624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
625 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200627 psa_key_type_t type = type_arg;
628 psa_key_usage_t usage = usage_arg;
629 psa_algorithm_t alg = alg_arg;
630 psa_status_t expected_status = expected_status_arg;
631 const uint8_t key_material[16] = {0};
632 psa_status_t status;
633
634 PSA_ASSERT( psa_crypto_init( ) );
635
636 psa_set_key_type( &attributes, type );
637 psa_set_key_usage_flags( &attributes, usage );
638 psa_set_key_algorithm( &attributes, alg );
639
640 status = psa_import_key( &attributes,
641 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200642 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200643 TEST_EQUAL( status, expected_status );
644 if( status != PSA_SUCCESS )
645 goto exit;
646
Ronald Cron5425a212020-08-04 14:58:35 +0200647 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200648 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
649 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
650 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200651 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200652
Ronald Cron5425a212020-08-04 14:58:35 +0200653 PSA_ASSERT( psa_destroy_key( key ) );
654 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200655
656exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100657 /*
658 * Key attributes may have been returned by psa_get_key_attributes()
659 * thus reset them as required.
660 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200661 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100662
663 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200664 PSA_DONE( );
665}
666/* END_CASE */
667
668/* BEGIN_CASE */
669void import_with_data( data_t *data, int type_arg,
670 int attr_bits_arg,
671 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200672{
673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
674 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200675 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200676 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200677 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200678 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680
Gilles Peskine8817f612018-12-18 00:18:46 +0100681 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682
Gilles Peskine4747d192019-04-17 15:05:45 +0200683 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200684 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200685
Ronald Cron5425a212020-08-04 14:58:35 +0200686 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100687 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200688 if( status != PSA_SUCCESS )
689 goto exit;
690
Ronald Cron5425a212020-08-04 14:58:35 +0200691 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200692 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200693 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200694 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200695 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200696
Ronald Cron5425a212020-08-04 14:58:35 +0200697 PSA_ASSERT( psa_destroy_key( key ) );
698 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100699
700exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100701 /*
702 * Key attributes may have been returned by psa_get_key_attributes()
703 * thus reset them as required.
704 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200705 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100706
707 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200708 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100709}
710/* END_CASE */
711
712/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200713void import_large_key( int type_arg, int byte_size_arg,
714 int expected_status_arg )
715{
716 psa_key_type_t type = type_arg;
717 size_t byte_size = byte_size_arg;
718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
719 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200720 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200721 psa_status_t status;
722 uint8_t *buffer = NULL;
723 size_t buffer_size = byte_size + 1;
724 size_t n;
725
Steven Cooreman69967ce2021-01-18 18:01:08 +0100726 /* Skip the test case if the target running the test cannot
727 * accomodate large keys due to heap size constraints */
728 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200729 memset( buffer, 'K', byte_size );
730
731 PSA_ASSERT( psa_crypto_init( ) );
732
733 /* Try importing the key */
734 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
735 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200736 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100737 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200738 TEST_EQUAL( status, expected_status );
739
740 if( status == PSA_SUCCESS )
741 {
Ronald Cron5425a212020-08-04 14:58:35 +0200742 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200743 TEST_EQUAL( psa_get_key_type( &attributes ), type );
744 TEST_EQUAL( psa_get_key_bits( &attributes ),
745 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200746 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200747 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200748 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200749 for( n = 0; n < byte_size; n++ )
750 TEST_EQUAL( buffer[n], 'K' );
751 for( n = byte_size; n < buffer_size; n++ )
752 TEST_EQUAL( buffer[n], 0 );
753 }
754
755exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100756 /*
757 * Key attributes may have been returned by psa_get_key_attributes()
758 * thus reset them as required.
759 */
760 psa_reset_key_attributes( &attributes );
761
Ronald Cron5425a212020-08-04 14:58:35 +0200762 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200763 PSA_DONE( );
764 mbedtls_free( buffer );
765}
766/* END_CASE */
767
768/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200769void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
770{
Ronald Cron5425a212020-08-04 14:58:35 +0200771 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200772 size_t bits = bits_arg;
773 psa_status_t expected_status = expected_status_arg;
774 psa_status_t status;
775 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200776 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200777 size_t buffer_size = /* Slight overapproximations */
778 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200779 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200780 unsigned char *p;
781 int ret;
782 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200783 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200784
Gilles Peskine8817f612018-12-18 00:18:46 +0100785 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200786 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200787
788 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
789 bits, keypair ) ) >= 0 );
790 length = ret;
791
792 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200793 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200794 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100795 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200796
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200797 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200798 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200799
800exit:
801 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200802 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200803}
804/* END_CASE */
805
806/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300807void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300808 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200809 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100810 int expected_bits,
811 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200812 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100813 int canonical_input )
814{
Ronald Cron5425a212020-08-04 14:58:35 +0200815 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200817 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200818 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820 unsigned char *exported = NULL;
821 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100822 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100823 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200826 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100827
Moran Pekercb088e72018-07-17 17:36:59 +0300828 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200829 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100830 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200831 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100832 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100833
Gilles Peskine4747d192019-04-17 15:05:45 +0200834 psa_set_key_usage_flags( &attributes, usage_arg );
835 psa_set_key_algorithm( &attributes, alg );
836 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700837
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100838 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200839 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840
841 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200842 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200843 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
844 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200845 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100846
847 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200848 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100849 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100850
851 /* The exported length must be set by psa_export_key() to a value between 0
852 * and export_size. On errors, the exported length must be 0. */
853 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
854 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
855 TEST_ASSERT( exported_length <= export_size );
856
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200857 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200858 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200860 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100861 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100862 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200863 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100864
Gilles Peskineea38a922021-02-13 00:05:16 +0100865 /* Run sanity checks on the exported key. For non-canonical inputs,
866 * this validates the canonical representations. For canonical inputs,
867 * this doesn't directly validate the implementation, but it still helps
868 * by cross-validating the test data with the sanity check code. */
869 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200870 goto exit;
871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200873 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 else
875 {
Ronald Cron5425a212020-08-04 14:58:35 +0200876 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200877 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200878 &key2 ) );
879 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100880 reexported,
881 export_size,
882 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200883 ASSERT_COMPARE( exported, exported_length,
884 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100887 TEST_ASSERT( exported_length <=
888 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
889 psa_get_key_bits( &got_attributes ) ) );
890 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891
892destroy:
893 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200894 PSA_ASSERT( psa_destroy_key( key ) );
895 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896
897exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100898 /*
899 * Key attributes may have been returned by psa_get_key_attributes()
900 * thus reset them as required.
901 */
902 psa_reset_key_attributes( &got_attributes );
903
itayzafrir3e02b3b2018-06-12 17:06:52 +0300904 mbedtls_free( exported );
905 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200906 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907}
908/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100909
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300911void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200912 int type_arg,
913 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100914 int export_size_delta,
915 int expected_export_status_arg,
916 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300917{
Ronald Cron5425a212020-08-04 14:58:35 +0200918 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300919 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200920 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200921 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300922 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300923 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100924 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100925 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300927
Gilles Peskine8817f612018-12-18 00:18:46 +0100928 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929
Gilles Peskine4747d192019-04-17 15:05:45 +0200930 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
931 psa_set_key_algorithm( &attributes, alg );
932 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300933
934 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200935 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300936
Gilles Peskine49c25912018-10-29 15:15:31 +0100937 /* Export the public key */
938 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200939 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200940 exported, export_size,
941 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100942 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100943 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100944 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200945 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100946 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200947 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200948 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100949 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100950 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100951 TEST_ASSERT( expected_public_key->len <=
952 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
953 TEST_ASSERT( expected_public_key->len <=
954 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100955 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
956 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100957 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300958
959exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100960 /*
961 * Key attributes may have been returned by psa_get_key_attributes()
962 * thus reset them as required.
963 */
964 psa_reset_key_attributes( &attributes );
965
itayzafrir3e02b3b2018-06-12 17:06:52 +0300966 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200967 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200968 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300969}
970/* END_CASE */
971
Gilles Peskine20035e32018-02-03 22:44:14 +0100972/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200973void import_and_exercise_key( data_t *data,
974 int type_arg,
975 int bits_arg,
976 int alg_arg )
977{
Ronald Cron5425a212020-08-04 14:58:35 +0200978 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200979 psa_key_type_t type = type_arg;
980 size_t bits = bits_arg;
981 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100982 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200983 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200984 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200985
Gilles Peskine8817f612018-12-18 00:18:46 +0100986 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200987
Gilles Peskine4747d192019-04-17 15:05:45 +0200988 psa_set_key_usage_flags( &attributes, usage );
989 psa_set_key_algorithm( &attributes, alg );
990 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200991
992 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200993 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200994
995 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200996 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200997 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
998 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200999
1000 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001001 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001002 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001003
Ronald Cron5425a212020-08-04 14:58:35 +02001004 PSA_ASSERT( psa_destroy_key( key ) );
1005 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001006
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001007exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001008 /*
1009 * Key attributes may have been returned by psa_get_key_attributes()
1010 * thus reset them as required.
1011 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001012 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001013
1014 psa_reset_key_attributes( &attributes );
1015 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001016 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001017}
1018/* END_CASE */
1019
1020/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001021void effective_key_attributes( int type_arg, int expected_type_arg,
1022 int bits_arg, int expected_bits_arg,
1023 int usage_arg, int expected_usage_arg,
1024 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001025{
Ronald Cron5425a212020-08-04 14:58:35 +02001026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001027 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001028 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001029 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001030 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001031 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001032 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001033 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001034 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001036
Gilles Peskine8817f612018-12-18 00:18:46 +01001037 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001039 psa_set_key_usage_flags( &attributes, usage );
1040 psa_set_key_algorithm( &attributes, alg );
1041 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001042 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001043
Ronald Cron5425a212020-08-04 14:58:35 +02001044 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001045 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001046
Ronald Cron5425a212020-08-04 14:58:35 +02001047 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001048 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1049 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1050 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1051 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001052
1053exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001054 /*
1055 * Key attributes may have been returned by psa_get_key_attributes()
1056 * thus reset them as required.
1057 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001058 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001059
1060 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001061 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001062}
1063/* END_CASE */
1064
1065/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001066void check_key_policy( int type_arg, int bits_arg,
1067 int usage_arg, int alg_arg )
1068{
1069 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1070 usage_arg, usage_arg, alg_arg, alg_arg );
1071 goto exit;
1072}
1073/* END_CASE */
1074
1075/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001076void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001077{
1078 /* Test each valid way of initializing the object, except for `= {0}`, as
1079 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1080 * though it's OK by the C standard. We could test for this, but we'd need
1081 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001082 psa_key_attributes_t func = psa_key_attributes_init( );
1083 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1084 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001085
1086 memset( &zero, 0, sizeof( zero ) );
1087
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001088 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1089 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1090 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001091
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001092 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1093 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1094 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1095
1096 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1097 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1098 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1099
1100 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1101 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1102 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1103
1104 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1105 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1106 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001107}
1108/* END_CASE */
1109
1110/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001111void mac_key_policy( int policy_usage,
1112 int policy_alg,
1113 int key_type,
1114 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001115 int exercise_alg,
1116 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001117{
Ronald Cron5425a212020-08-04 14:58:35 +02001118 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001119 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001120 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001121 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001122 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001123 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001124
Gilles Peskine8817f612018-12-18 00:18:46 +01001125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001126
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001127 psa_set_key_usage_flags( &attributes, policy_usage );
1128 psa_set_key_algorithm( &attributes, policy_alg );
1129 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001130
Gilles Peskine049c7532019-05-15 20:22:09 +02001131 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001132 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001133
Ronald Cron5425a212020-08-04 14:58:35 +02001134 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001135 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001136 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001137 else
1138 TEST_EQUAL( status, expected_status );
1139
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001140 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001141
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001142 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001143 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001144 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001145 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001146 else
1147 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001148
1149exit:
1150 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001151 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001152 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001153}
1154/* END_CASE */
1155
1156/* BEGIN_CASE */
1157void cipher_key_policy( int policy_usage,
1158 int policy_alg,
1159 int key_type,
1160 data_t *key_data,
1161 int exercise_alg )
1162{
Ronald Cron5425a212020-08-04 14:58:35 +02001163 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001165 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001166 psa_status_t status;
1167
Gilles Peskine8817f612018-12-18 00:18:46 +01001168 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001170 psa_set_key_usage_flags( &attributes, policy_usage );
1171 psa_set_key_algorithm( &attributes, policy_alg );
1172 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001173
Gilles Peskine049c7532019-05-15 20:22:09 +02001174 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001175 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001176
Ronald Cron5425a212020-08-04 14:58:35 +02001177 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001178 if( policy_alg == exercise_alg &&
1179 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001180 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001181 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001182 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001183 psa_cipher_abort( &operation );
1184
Ronald Cron5425a212020-08-04 14:58:35 +02001185 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001186 if( policy_alg == exercise_alg &&
1187 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001188 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001189 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001190 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191
1192exit:
1193 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001194 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001195 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001196}
1197/* END_CASE */
1198
1199/* BEGIN_CASE */
1200void aead_key_policy( int policy_usage,
1201 int policy_alg,
1202 int key_type,
1203 data_t *key_data,
1204 int nonce_length_arg,
1205 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001206 int exercise_alg,
1207 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208{
Ronald Cron5425a212020-08-04 14:58:35 +02001209 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001210 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001212 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213 unsigned char nonce[16] = {0};
1214 size_t nonce_length = nonce_length_arg;
1215 unsigned char tag[16];
1216 size_t tag_length = tag_length_arg;
1217 size_t output_length;
1218
1219 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1220 TEST_ASSERT( tag_length <= sizeof( tag ) );
1221
Gilles Peskine8817f612018-12-18 00:18:46 +01001222 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001223
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001224 psa_set_key_usage_flags( &attributes, policy_usage );
1225 psa_set_key_algorithm( &attributes, policy_alg );
1226 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001227
Gilles Peskine049c7532019-05-15 20:22:09 +02001228 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001229 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230
Ronald Cron5425a212020-08-04 14:58:35 +02001231 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001232 nonce, nonce_length,
1233 NULL, 0,
1234 NULL, 0,
1235 tag, tag_length,
1236 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001237 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1238 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001239 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001240 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001241
1242 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001243 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001244 nonce, nonce_length,
1245 NULL, 0,
1246 tag, tag_length,
1247 NULL, 0,
1248 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001249 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1250 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1251 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001252 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001253 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001254 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001255
1256exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001257 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001258 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001259}
1260/* END_CASE */
1261
1262/* BEGIN_CASE */
1263void asymmetric_encryption_key_policy( int policy_usage,
1264 int policy_alg,
1265 int key_type,
1266 data_t *key_data,
1267 int exercise_alg )
1268{
Ronald Cron5425a212020-08-04 14:58:35 +02001269 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001271 psa_status_t status;
1272 size_t key_bits;
1273 size_t buffer_length;
1274 unsigned char *buffer = NULL;
1275 size_t output_length;
1276
Gilles Peskine8817f612018-12-18 00:18:46 +01001277 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001278
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001279 psa_set_key_usage_flags( &attributes, policy_usage );
1280 psa_set_key_algorithm( &attributes, policy_alg );
1281 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001282
Gilles Peskine049c7532019-05-15 20:22:09 +02001283 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001284 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001285
Ronald Cron5425a212020-08-04 14:58:35 +02001286 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001287 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001288 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1289 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001290 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001291
Ronald Cron5425a212020-08-04 14:58:35 +02001292 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001293 NULL, 0,
1294 NULL, 0,
1295 buffer, buffer_length,
1296 &output_length );
1297 if( policy_alg == exercise_alg &&
1298 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001299 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001300 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001301 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001302
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001303 if( buffer_length != 0 )
1304 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001305 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001306 buffer, buffer_length,
1307 NULL, 0,
1308 buffer, buffer_length,
1309 &output_length );
1310 if( policy_alg == exercise_alg &&
1311 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001312 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001313 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001314 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001315
1316exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001317 /*
1318 * Key attributes may have been returned by psa_get_key_attributes()
1319 * thus reset them as required.
1320 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001321 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001322
1323 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001324 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001325 mbedtls_free( buffer );
1326}
1327/* END_CASE */
1328
1329/* BEGIN_CASE */
1330void asymmetric_signature_key_policy( int policy_usage,
1331 int policy_alg,
1332 int key_type,
1333 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001334 int exercise_alg,
1335 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001336{
Ronald Cron5425a212020-08-04 14:58:35 +02001337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001340 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1341 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1342 * compatible with the policy and `payload_length_arg` is supposed to be
1343 * a valid input length to sign. If `payload_length_arg <= 0`,
1344 * `exercise_alg` is supposed to be forbidden by the policy. */
1345 int compatible_alg = payload_length_arg > 0;
1346 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001347 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001348 size_t signature_length;
1349
Gilles Peskine8817f612018-12-18 00:18:46 +01001350 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001351
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001352 psa_set_key_usage_flags( &attributes, policy_usage );
1353 psa_set_key_algorithm( &attributes, policy_alg );
1354 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001355
Gilles Peskine049c7532019-05-15 20:22:09 +02001356 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001357 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001358
Ronald Cron5425a212020-08-04 14:58:35 +02001359 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001360 payload, payload_length,
1361 signature, sizeof( signature ),
1362 &signature_length );
1363 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001364 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001365 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001366 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001367
1368 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001369 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001370 payload, payload_length,
1371 signature, sizeof( signature ) );
1372 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001373 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001374 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001375 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001376
1377exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001378 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001379 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001380}
1381/* END_CASE */
1382
Janos Follathba3fab92019-06-11 14:50:16 +01001383/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001384void derive_key_policy( int policy_usage,
1385 int policy_alg,
1386 int key_type,
1387 data_t *key_data,
1388 int exercise_alg )
1389{
Ronald Cron5425a212020-08-04 14:58:35 +02001390 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001391 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001392 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001393 psa_status_t status;
1394
Gilles Peskine8817f612018-12-18 00:18:46 +01001395 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001396
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001397 psa_set_key_usage_flags( &attributes, policy_usage );
1398 psa_set_key_algorithm( &attributes, policy_alg );
1399 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001400
Gilles Peskine049c7532019-05-15 20:22:09 +02001401 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001402 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001403
Janos Follathba3fab92019-06-11 14:50:16 +01001404 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1405
1406 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1407 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001408 {
Janos Follathba3fab92019-06-11 14:50:16 +01001409 PSA_ASSERT( psa_key_derivation_input_bytes(
1410 &operation,
1411 PSA_KEY_DERIVATION_INPUT_SEED,
1412 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001413 }
Janos Follathba3fab92019-06-11 14:50:16 +01001414
1415 status = psa_key_derivation_input_key( &operation,
1416 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001417 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001418
Gilles Peskineea0fb492018-07-12 17:17:20 +02001419 if( policy_alg == exercise_alg &&
1420 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001421 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001422 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001423 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001424
1425exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001426 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001427 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001428 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001429}
1430/* END_CASE */
1431
1432/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001433void agreement_key_policy( int policy_usage,
1434 int policy_alg,
1435 int key_type_arg,
1436 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001437 int exercise_alg,
1438 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001439{
Ronald Cron5425a212020-08-04 14:58:35 +02001440 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001442 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001443 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001444 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001445 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001446
Gilles Peskine8817f612018-12-18 00:18:46 +01001447 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001448
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001449 psa_set_key_usage_flags( &attributes, policy_usage );
1450 psa_set_key_algorithm( &attributes, policy_alg );
1451 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001452
Gilles Peskine049c7532019-05-15 20:22:09 +02001453 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001454 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001455
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001456 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001457 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001458
Steven Cooremance48e852020-10-05 16:02:45 +02001459 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001460
1461exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001462 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001463 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001464 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001465}
1466/* END_CASE */
1467
1468/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001469void key_policy_alg2( int key_type_arg, data_t *key_data,
1470 int usage_arg, int alg_arg, int alg2_arg )
1471{
Ronald Cron5425a212020-08-04 14:58:35 +02001472 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001473 psa_key_type_t key_type = key_type_arg;
1474 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1475 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1476 psa_key_usage_t usage = usage_arg;
1477 psa_algorithm_t alg = alg_arg;
1478 psa_algorithm_t alg2 = alg2_arg;
1479
1480 PSA_ASSERT( psa_crypto_init( ) );
1481
1482 psa_set_key_usage_flags( &attributes, usage );
1483 psa_set_key_algorithm( &attributes, alg );
1484 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1485 psa_set_key_type( &attributes, key_type );
1486 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001487 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001488
Ronald Cron5425a212020-08-04 14:58:35 +02001489 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001490 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1491 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1492 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1493
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001494 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001495 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001496 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001497 goto exit;
1498
1499exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001500 /*
1501 * Key attributes may have been returned by psa_get_key_attributes()
1502 * thus reset them as required.
1503 */
1504 psa_reset_key_attributes( &got_attributes );
1505
Ronald Cron5425a212020-08-04 14:58:35 +02001506 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001507 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001508}
1509/* END_CASE */
1510
1511/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001512void raw_agreement_key_policy( int policy_usage,
1513 int policy_alg,
1514 int key_type_arg,
1515 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001516 int exercise_alg,
1517 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001518{
Ronald Cron5425a212020-08-04 14:58:35 +02001519 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001521 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001522 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001523 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001524 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001525
1526 PSA_ASSERT( psa_crypto_init( ) );
1527
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001528 psa_set_key_usage_flags( &attributes, policy_usage );
1529 psa_set_key_algorithm( &attributes, policy_alg );
1530 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001531
Gilles Peskine049c7532019-05-15 20:22:09 +02001532 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001533 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001534
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001535 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001536
Steven Cooremance48e852020-10-05 16:02:45 +02001537 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001538
1539exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001540 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001541 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001542 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001543}
1544/* END_CASE */
1545
1546/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001547void copy_success( int source_usage_arg,
1548 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001549 int type_arg, data_t *material,
1550 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001551 int target_usage_arg,
1552 int target_alg_arg, int target_alg2_arg,
1553 int expected_usage_arg,
1554 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001555{
Gilles Peskineca25db92019-04-19 11:43:08 +02001556 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1557 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001558 psa_key_usage_t expected_usage = expected_usage_arg;
1559 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001560 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001561 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1562 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001563 uint8_t *export_buffer = NULL;
1564
Gilles Peskine57ab7212019-01-28 13:03:09 +01001565 PSA_ASSERT( psa_crypto_init( ) );
1566
Gilles Peskineca25db92019-04-19 11:43:08 +02001567 /* Prepare the source key. */
1568 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1569 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001570 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001571 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001572 PSA_ASSERT( psa_import_key( &source_attributes,
1573 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001574 &source_key ) );
1575 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001576
Gilles Peskineca25db92019-04-19 11:43:08 +02001577 /* Prepare the target attributes. */
1578 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001579 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001580 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001581 /* Set volatile lifetime to reset the key identifier to 0. */
1582 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1583 }
1584
Gilles Peskineca25db92019-04-19 11:43:08 +02001585 if( target_usage_arg != -1 )
1586 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1587 if( target_alg_arg != -1 )
1588 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001589 if( target_alg2_arg != -1 )
1590 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001591
1592 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001593 PSA_ASSERT( psa_copy_key( source_key,
1594 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001595
1596 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001597 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001598
1599 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001600 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001601 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1602 psa_get_key_type( &target_attributes ) );
1603 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1604 psa_get_key_bits( &target_attributes ) );
1605 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1606 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001607 TEST_EQUAL( expected_alg2,
1608 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001609 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1610 {
1611 size_t length;
1612 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001613 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001614 material->len, &length ) );
1615 ASSERT_COMPARE( material->x, material->len,
1616 export_buffer, length );
1617 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001618
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001619 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001620 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001621 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001622 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001623
Ronald Cron5425a212020-08-04 14:58:35 +02001624 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001625
1626exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001627 /*
1628 * Source and target key attributes may have been returned by
1629 * psa_get_key_attributes() thus reset them as required.
1630 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001631 psa_reset_key_attributes( &source_attributes );
1632 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001633
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001634 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001635 mbedtls_free( export_buffer );
1636}
1637/* END_CASE */
1638
1639/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001640void copy_fail( int source_usage_arg,
1641 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001642 int type_arg, data_t *material,
1643 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001644 int target_usage_arg,
1645 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001646 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001647 int expected_status_arg )
1648{
1649 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1650 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001651 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1652 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001653 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001654
1655 PSA_ASSERT( psa_crypto_init( ) );
1656
1657 /* Prepare the source key. */
1658 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1659 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001660 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001661 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001662 PSA_ASSERT( psa_import_key( &source_attributes,
1663 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001664 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001665
1666 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001667 psa_set_key_id( &target_attributes, key_id );
1668 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001669 psa_set_key_type( &target_attributes, target_type_arg );
1670 psa_set_key_bits( &target_attributes, target_bits_arg );
1671 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1672 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001673 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001674
1675 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001676 TEST_EQUAL( psa_copy_key( source_key,
1677 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001678 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001679
Ronald Cron5425a212020-08-04 14:58:35 +02001680 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001681
Gilles Peskine4a644642019-05-03 17:14:08 +02001682exit:
1683 psa_reset_key_attributes( &source_attributes );
1684 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001685 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001686}
1687/* END_CASE */
1688
1689/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001690void hash_operation_init( )
1691{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001692 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001693 /* Test each valid way of initializing the object, except for `= {0}`, as
1694 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1695 * though it's OK by the C standard. We could test for this, but we'd need
1696 * to supress the Clang warning for the test. */
1697 psa_hash_operation_t func = psa_hash_operation_init( );
1698 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1699 psa_hash_operation_t zero;
1700
1701 memset( &zero, 0, sizeof( zero ) );
1702
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001703 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001704 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1705 PSA_ERROR_BAD_STATE );
1706 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1707 PSA_ERROR_BAD_STATE );
1708 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1709 PSA_ERROR_BAD_STATE );
1710
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001711 /* A default hash operation should be abortable without error. */
1712 PSA_ASSERT( psa_hash_abort( &func ) );
1713 PSA_ASSERT( psa_hash_abort( &init ) );
1714 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001715}
1716/* END_CASE */
1717
1718/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001719void hash_setup( int alg_arg,
1720 int expected_status_arg )
1721{
1722 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001723 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001724 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001725 psa_status_t status;
1726
Gilles Peskine8817f612018-12-18 00:18:46 +01001727 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001728
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001729 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001730 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001731
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001732 /* Whether setup succeeded or failed, abort must succeed. */
1733 PSA_ASSERT( psa_hash_abort( &operation ) );
1734
1735 /* If setup failed, reproduce the failure, so as to
1736 * test the resulting state of the operation object. */
1737 if( status != PSA_SUCCESS )
1738 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1739
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001740 /* Now the operation object should be reusable. */
1741#if defined(KNOWN_SUPPORTED_HASH_ALG)
1742 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1743 PSA_ASSERT( psa_hash_abort( &operation ) );
1744#endif
1745
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001746exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001747 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001748}
1749/* END_CASE */
1750
1751/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001752void hash_compute_fail( int alg_arg, data_t *input,
1753 int output_size_arg, int expected_status_arg )
1754{
1755 psa_algorithm_t alg = alg_arg;
1756 uint8_t *output = NULL;
1757 size_t output_size = output_size_arg;
1758 size_t output_length = INVALID_EXPORT_LENGTH;
1759 psa_status_t expected_status = expected_status_arg;
1760 psa_status_t status;
1761
1762 ASSERT_ALLOC( output, output_size );
1763
1764 PSA_ASSERT( psa_crypto_init( ) );
1765
1766 status = psa_hash_compute( alg, input->x, input->len,
1767 output, output_size, &output_length );
1768 TEST_EQUAL( status, expected_status );
1769 TEST_ASSERT( output_length <= output_size );
1770
1771exit:
1772 mbedtls_free( output );
1773 PSA_DONE( );
1774}
1775/* END_CASE */
1776
1777/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001778void hash_compare_fail( int alg_arg, data_t *input,
1779 data_t *reference_hash,
1780 int expected_status_arg )
1781{
1782 psa_algorithm_t alg = alg_arg;
1783 psa_status_t expected_status = expected_status_arg;
1784 psa_status_t status;
1785
1786 PSA_ASSERT( psa_crypto_init( ) );
1787
1788 status = psa_hash_compare( alg, input->x, input->len,
1789 reference_hash->x, reference_hash->len );
1790 TEST_EQUAL( status, expected_status );
1791
1792exit:
1793 PSA_DONE( );
1794}
1795/* END_CASE */
1796
1797/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001798void hash_compute_compare( int alg_arg, data_t *input,
1799 data_t *expected_output )
1800{
1801 psa_algorithm_t alg = alg_arg;
1802 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1803 size_t output_length = INVALID_EXPORT_LENGTH;
1804 size_t i;
1805
1806 PSA_ASSERT( psa_crypto_init( ) );
1807
1808 /* Compute with tight buffer */
1809 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001810 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001811 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001812 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001813 ASSERT_COMPARE( output, output_length,
1814 expected_output->x, expected_output->len );
1815
1816 /* Compute with larger buffer */
1817 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1818 output, sizeof( output ),
1819 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001820 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001821 ASSERT_COMPARE( output, output_length,
1822 expected_output->x, expected_output->len );
1823
1824 /* Compare with correct hash */
1825 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1826 output, output_length ) );
1827
1828 /* Compare with trailing garbage */
1829 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1830 output, output_length + 1 ),
1831 PSA_ERROR_INVALID_SIGNATURE );
1832
1833 /* Compare with truncated hash */
1834 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1835 output, output_length - 1 ),
1836 PSA_ERROR_INVALID_SIGNATURE );
1837
1838 /* Compare with corrupted value */
1839 for( i = 0; i < output_length; i++ )
1840 {
Chris Jones9634bb12021-01-20 15:56:42 +00001841 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001842 output[i] ^= 1;
1843 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1844 output, output_length ),
1845 PSA_ERROR_INVALID_SIGNATURE );
1846 output[i] ^= 1;
1847 }
1848
1849exit:
1850 PSA_DONE( );
1851}
1852/* END_CASE */
1853
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001854/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001855void hash_bad_order( )
1856{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001857 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001858 unsigned char input[] = "";
1859 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001860 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001861 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1862 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1863 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001864 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001865 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001866 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001867
Gilles Peskine8817f612018-12-18 00:18:46 +01001868 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001869
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001870 /* Call setup twice in a row. */
1871 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1872 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1873 PSA_ERROR_BAD_STATE );
1874 PSA_ASSERT( psa_hash_abort( &operation ) );
1875
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001876 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001877 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001878 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001879 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001880
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001881 /* Call update after finish. */
1882 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1883 PSA_ASSERT( psa_hash_finish( &operation,
1884 hash, sizeof( hash ), &hash_len ) );
1885 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001886 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001887 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001888
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001889 /* Call verify without calling setup beforehand. */
1890 TEST_EQUAL( psa_hash_verify( &operation,
1891 valid_hash, sizeof( valid_hash ) ),
1892 PSA_ERROR_BAD_STATE );
1893 PSA_ASSERT( psa_hash_abort( &operation ) );
1894
1895 /* Call verify after finish. */
1896 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1897 PSA_ASSERT( psa_hash_finish( &operation,
1898 hash, sizeof( hash ), &hash_len ) );
1899 TEST_EQUAL( psa_hash_verify( &operation,
1900 valid_hash, sizeof( valid_hash ) ),
1901 PSA_ERROR_BAD_STATE );
1902 PSA_ASSERT( psa_hash_abort( &operation ) );
1903
1904 /* Call verify twice in a row. */
1905 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1906 PSA_ASSERT( psa_hash_verify( &operation,
1907 valid_hash, sizeof( valid_hash ) ) );
1908 TEST_EQUAL( psa_hash_verify( &operation,
1909 valid_hash, sizeof( valid_hash ) ),
1910 PSA_ERROR_BAD_STATE );
1911 PSA_ASSERT( psa_hash_abort( &operation ) );
1912
1913 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001914 TEST_EQUAL( psa_hash_finish( &operation,
1915 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001916 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001917 PSA_ASSERT( psa_hash_abort( &operation ) );
1918
1919 /* Call finish twice in a row. */
1920 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1921 PSA_ASSERT( psa_hash_finish( &operation,
1922 hash, sizeof( hash ), &hash_len ) );
1923 TEST_EQUAL( psa_hash_finish( &operation,
1924 hash, sizeof( hash ), &hash_len ),
1925 PSA_ERROR_BAD_STATE );
1926 PSA_ASSERT( psa_hash_abort( &operation ) );
1927
1928 /* Call finish after calling verify. */
1929 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1930 PSA_ASSERT( psa_hash_verify( &operation,
1931 valid_hash, sizeof( valid_hash ) ) );
1932 TEST_EQUAL( psa_hash_finish( &operation,
1933 hash, sizeof( hash ), &hash_len ),
1934 PSA_ERROR_BAD_STATE );
1935 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001936
1937exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001938 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001939}
1940/* END_CASE */
1941
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001942/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001943void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001944{
1945 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001946 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1947 * appended to it */
1948 unsigned char hash[] = {
1949 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1950 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1951 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001952 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001953 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001954
Gilles Peskine8817f612018-12-18 00:18:46 +01001955 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001956
itayzafrir27e69452018-11-01 14:26:34 +02001957 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001958 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001959 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001960 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001961
itayzafrir27e69452018-11-01 14:26:34 +02001962 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001963 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001964 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001965 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001966
itayzafrir27e69452018-11-01 14:26:34 +02001967 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001968 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001969 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001970 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001971
itayzafrirec93d302018-10-18 18:01:10 +03001972exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001973 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001974}
1975/* END_CASE */
1976
Ronald Cronee414c72021-03-18 18:50:08 +01001977/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001978void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001979{
1980 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001981 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001982 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001983 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001984 size_t hash_len;
1985
Gilles Peskine8817f612018-12-18 00:18:46 +01001986 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001987
itayzafrir58028322018-10-25 10:22:01 +03001988 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001989 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001990 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001991 hash, expected_size - 1, &hash_len ),
1992 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001993
1994exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001995 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001996}
1997/* END_CASE */
1998
Ronald Cronee414c72021-03-18 18:50:08 +01001999/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002000void hash_clone_source_state( )
2001{
2002 psa_algorithm_t alg = PSA_ALG_SHA_256;
2003 unsigned char hash[PSA_HASH_MAX_SIZE];
2004 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2005 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2006 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2007 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2008 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2009 size_t hash_len;
2010
2011 PSA_ASSERT( psa_crypto_init( ) );
2012 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2013
2014 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2015 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2016 PSA_ASSERT( psa_hash_finish( &op_finished,
2017 hash, sizeof( hash ), &hash_len ) );
2018 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2019 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2020
2021 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2022 PSA_ERROR_BAD_STATE );
2023
2024 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2025 PSA_ASSERT( psa_hash_finish( &op_init,
2026 hash, sizeof( hash ), &hash_len ) );
2027 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2028 PSA_ASSERT( psa_hash_finish( &op_finished,
2029 hash, sizeof( hash ), &hash_len ) );
2030 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2031 PSA_ASSERT( psa_hash_finish( &op_aborted,
2032 hash, sizeof( hash ), &hash_len ) );
2033
2034exit:
2035 psa_hash_abort( &op_source );
2036 psa_hash_abort( &op_init );
2037 psa_hash_abort( &op_setup );
2038 psa_hash_abort( &op_finished );
2039 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002040 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002041}
2042/* END_CASE */
2043
Ronald Cronee414c72021-03-18 18:50:08 +01002044/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002045void hash_clone_target_state( )
2046{
2047 psa_algorithm_t alg = PSA_ALG_SHA_256;
2048 unsigned char hash[PSA_HASH_MAX_SIZE];
2049 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2050 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2051 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2052 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2053 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2054 size_t hash_len;
2055
2056 PSA_ASSERT( psa_crypto_init( ) );
2057
2058 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2059 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2060 PSA_ASSERT( psa_hash_finish( &op_finished,
2061 hash, sizeof( hash ), &hash_len ) );
2062 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2063 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2064
2065 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2066 PSA_ASSERT( psa_hash_finish( &op_target,
2067 hash, sizeof( hash ), &hash_len ) );
2068
2069 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2070 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2071 PSA_ERROR_BAD_STATE );
2072 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2073 PSA_ERROR_BAD_STATE );
2074
2075exit:
2076 psa_hash_abort( &op_target );
2077 psa_hash_abort( &op_init );
2078 psa_hash_abort( &op_setup );
2079 psa_hash_abort( &op_finished );
2080 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002081 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002082}
2083/* END_CASE */
2084
itayzafrir58028322018-10-25 10:22:01 +03002085/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002086void mac_operation_init( )
2087{
Jaeden Amero252ef282019-02-15 14:05:35 +00002088 const uint8_t input[1] = { 0 };
2089
Jaeden Amero769ce272019-01-04 11:48:03 +00002090 /* Test each valid way of initializing the object, except for `= {0}`, as
2091 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2092 * though it's OK by the C standard. We could test for this, but we'd need
2093 * to supress the Clang warning for the test. */
2094 psa_mac_operation_t func = psa_mac_operation_init( );
2095 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2096 psa_mac_operation_t zero;
2097
2098 memset( &zero, 0, sizeof( zero ) );
2099
Jaeden Amero252ef282019-02-15 14:05:35 +00002100 /* A freshly-initialized MAC operation should not be usable. */
2101 TEST_EQUAL( psa_mac_update( &func,
2102 input, sizeof( input ) ),
2103 PSA_ERROR_BAD_STATE );
2104 TEST_EQUAL( psa_mac_update( &init,
2105 input, sizeof( input ) ),
2106 PSA_ERROR_BAD_STATE );
2107 TEST_EQUAL( psa_mac_update( &zero,
2108 input, sizeof( input ) ),
2109 PSA_ERROR_BAD_STATE );
2110
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002111 /* A default MAC operation should be abortable without error. */
2112 PSA_ASSERT( psa_mac_abort( &func ) );
2113 PSA_ASSERT( psa_mac_abort( &init ) );
2114 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002115}
2116/* END_CASE */
2117
2118/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002119void mac_setup( int key_type_arg,
2120 data_t *key,
2121 int alg_arg,
2122 int expected_status_arg )
2123{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002124 psa_key_type_t key_type = key_type_arg;
2125 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002126 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002127 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002128 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2129#if defined(KNOWN_SUPPORTED_MAC_ALG)
2130 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2131#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002132
Gilles Peskine8817f612018-12-18 00:18:46 +01002133 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002135 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2136 &operation, &status ) )
2137 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002138 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002139
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002140 /* The operation object should be reusable. */
2141#if defined(KNOWN_SUPPORTED_MAC_ALG)
2142 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2143 smoke_test_key_data,
2144 sizeof( smoke_test_key_data ),
2145 KNOWN_SUPPORTED_MAC_ALG,
2146 &operation, &status ) )
2147 goto exit;
2148 TEST_EQUAL( status, PSA_SUCCESS );
2149#endif
2150
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002151exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002152 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002153}
2154/* END_CASE */
2155
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002156/* 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 +00002157void mac_bad_order( )
2158{
Ronald Cron5425a212020-08-04 14:58:35 +02002159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002160 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2161 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002162 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002163 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2164 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2165 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002167 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2168 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2169 size_t sign_mac_length = 0;
2170 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2171 const uint8_t verify_mac[] = {
2172 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2173 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2174 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2175
2176 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002177 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002178 psa_set_key_algorithm( &attributes, alg );
2179 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002180
Ronald Cron5425a212020-08-04 14:58:35 +02002181 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2182 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002183
Jaeden Amero252ef282019-02-15 14:05:35 +00002184 /* Call update without calling setup beforehand. */
2185 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2186 PSA_ERROR_BAD_STATE );
2187 PSA_ASSERT( psa_mac_abort( &operation ) );
2188
2189 /* Call sign finish without calling setup beforehand. */
2190 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2191 &sign_mac_length),
2192 PSA_ERROR_BAD_STATE );
2193 PSA_ASSERT( psa_mac_abort( &operation ) );
2194
2195 /* Call verify finish without calling setup beforehand. */
2196 TEST_EQUAL( psa_mac_verify_finish( &operation,
2197 verify_mac, sizeof( verify_mac ) ),
2198 PSA_ERROR_BAD_STATE );
2199 PSA_ASSERT( psa_mac_abort( &operation ) );
2200
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002201 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002202 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2203 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002204 PSA_ERROR_BAD_STATE );
2205 PSA_ASSERT( psa_mac_abort( &operation ) );
2206
Jaeden Amero252ef282019-02-15 14:05:35 +00002207 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002208 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002209 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2210 PSA_ASSERT( psa_mac_sign_finish( &operation,
2211 sign_mac, sizeof( sign_mac ),
2212 &sign_mac_length ) );
2213 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2214 PSA_ERROR_BAD_STATE );
2215 PSA_ASSERT( psa_mac_abort( &operation ) );
2216
2217 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002218 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002219 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2220 PSA_ASSERT( psa_mac_verify_finish( &operation,
2221 verify_mac, sizeof( verify_mac ) ) );
2222 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2223 PSA_ERROR_BAD_STATE );
2224 PSA_ASSERT( psa_mac_abort( &operation ) );
2225
2226 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002227 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002228 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2229 PSA_ASSERT( psa_mac_sign_finish( &operation,
2230 sign_mac, sizeof( sign_mac ),
2231 &sign_mac_length ) );
2232 TEST_EQUAL( psa_mac_sign_finish( &operation,
2233 sign_mac, sizeof( sign_mac ),
2234 &sign_mac_length ),
2235 PSA_ERROR_BAD_STATE );
2236 PSA_ASSERT( psa_mac_abort( &operation ) );
2237
2238 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002239 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002240 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2241 PSA_ASSERT( psa_mac_verify_finish( &operation,
2242 verify_mac, sizeof( verify_mac ) ) );
2243 TEST_EQUAL( psa_mac_verify_finish( &operation,
2244 verify_mac, sizeof( verify_mac ) ),
2245 PSA_ERROR_BAD_STATE );
2246 PSA_ASSERT( psa_mac_abort( &operation ) );
2247
2248 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002249 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002250 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2251 TEST_EQUAL( psa_mac_verify_finish( &operation,
2252 verify_mac, sizeof( verify_mac ) ),
2253 PSA_ERROR_BAD_STATE );
2254 PSA_ASSERT( psa_mac_abort( &operation ) );
2255
2256 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002257 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002258 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2259 TEST_EQUAL( psa_mac_sign_finish( &operation,
2260 sign_mac, sizeof( sign_mac ),
2261 &sign_mac_length ),
2262 PSA_ERROR_BAD_STATE );
2263 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264
Ronald Cron5425a212020-08-04 14:58:35 +02002265 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002266
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002267exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002268 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002269}
2270/* END_CASE */
2271
2272/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002273void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002274 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002275 int alg_arg,
2276 data_t *input,
2277 data_t *expected_mac )
2278{
Ronald Cron5425a212020-08-04 14:58:35 +02002279 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002280 psa_key_type_t key_type = key_type_arg;
2281 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002282 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002283 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002284 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002285 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002286 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002287 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002288 const size_t output_sizes_to_test[] = {
2289 0,
2290 1,
2291 expected_mac->len - 1,
2292 expected_mac->len,
2293 expected_mac->len + 1,
2294 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002295
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002296 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002297 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002298 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002299
Gilles Peskine8817f612018-12-18 00:18:46 +01002300 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002301
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002302 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002303 psa_set_key_algorithm( &attributes, alg );
2304 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002305
Ronald Cron5425a212020-08-04 14:58:35 +02002306 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2307 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002308
Gilles Peskine8b356b52020-08-25 23:44:59 +02002309 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2310 {
2311 const size_t output_size = output_sizes_to_test[i];
2312 psa_status_t expected_status =
2313 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2314 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002315
Chris Jones9634bb12021-01-20 15:56:42 +00002316 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002317 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002318
Gilles Peskine8b356b52020-08-25 23:44:59 +02002319 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002320 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002321 PSA_ASSERT( psa_mac_update( &operation,
2322 input->x, input->len ) );
2323 TEST_EQUAL( psa_mac_sign_finish( &operation,
2324 actual_mac, output_size,
2325 &mac_length ),
2326 expected_status );
2327 PSA_ASSERT( psa_mac_abort( &operation ) );
2328
2329 if( expected_status == PSA_SUCCESS )
2330 {
2331 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2332 actual_mac, mac_length );
2333 }
2334 mbedtls_free( actual_mac );
2335 actual_mac = NULL;
2336 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002337
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002338exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002339 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002340 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002341 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002342 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002343}
2344/* END_CASE */
2345
2346/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002347void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002348 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002349 int alg_arg,
2350 data_t *input,
2351 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002352{
Ronald Cron5425a212020-08-04 14:58:35 +02002353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002354 psa_key_type_t key_type = key_type_arg;
2355 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002356 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002358 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002359
Gilles Peskine69c12672018-06-28 00:07:19 +02002360 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2361
Gilles Peskine8817f612018-12-18 00:18:46 +01002362 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002363
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002364 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002365 psa_set_key_algorithm( &attributes, alg );
2366 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002367
Ronald Cron5425a212020-08-04 14:58:35 +02002368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2369 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002370
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002371 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002372 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002373 PSA_ASSERT( psa_mac_update( &operation,
2374 input->x, input->len ) );
2375 PSA_ASSERT( psa_mac_verify_finish( &operation,
2376 expected_mac->x,
2377 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002378
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002379 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002380 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002381 PSA_ASSERT( psa_mac_update( &operation,
2382 input->x, input->len ) );
2383 TEST_EQUAL( psa_mac_verify_finish( &operation,
2384 expected_mac->x,
2385 expected_mac->len - 1 ),
2386 PSA_ERROR_INVALID_SIGNATURE );
2387
2388 /* Test a MAC that's too long. */
2389 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2390 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002391 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002392 PSA_ASSERT( psa_mac_update( &operation,
2393 input->x, input->len ) );
2394 TEST_EQUAL( psa_mac_verify_finish( &operation,
2395 perturbed_mac,
2396 expected_mac->len + 1 ),
2397 PSA_ERROR_INVALID_SIGNATURE );
2398
2399 /* Test changing one byte. */
2400 for( size_t i = 0; i < expected_mac->len; i++ )
2401 {
Chris Jones9634bb12021-01-20 15:56:42 +00002402 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002403 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002404 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002405 PSA_ASSERT( psa_mac_update( &operation,
2406 input->x, input->len ) );
2407 TEST_EQUAL( psa_mac_verify_finish( &operation,
2408 perturbed_mac,
2409 expected_mac->len ),
2410 PSA_ERROR_INVALID_SIGNATURE );
2411 perturbed_mac[i] ^= 1;
2412 }
2413
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002415 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002416 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002417 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002418 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002419}
2420/* END_CASE */
2421
2422/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002423void cipher_operation_init( )
2424{
Jaeden Ameroab439972019-02-15 14:12:05 +00002425 const uint8_t input[1] = { 0 };
2426 unsigned char output[1] = { 0 };
2427 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002428 /* Test each valid way of initializing the object, except for `= {0}`, as
2429 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2430 * though it's OK by the C standard. We could test for this, but we'd need
2431 * to supress the Clang warning for the test. */
2432 psa_cipher_operation_t func = psa_cipher_operation_init( );
2433 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2434 psa_cipher_operation_t zero;
2435
2436 memset( &zero, 0, sizeof( zero ) );
2437
Jaeden Ameroab439972019-02-15 14:12:05 +00002438 /* A freshly-initialized cipher operation should not be usable. */
2439 TEST_EQUAL( psa_cipher_update( &func,
2440 input, sizeof( input ),
2441 output, sizeof( output ),
2442 &output_length ),
2443 PSA_ERROR_BAD_STATE );
2444 TEST_EQUAL( psa_cipher_update( &init,
2445 input, sizeof( input ),
2446 output, sizeof( output ),
2447 &output_length ),
2448 PSA_ERROR_BAD_STATE );
2449 TEST_EQUAL( psa_cipher_update( &zero,
2450 input, sizeof( input ),
2451 output, sizeof( output ),
2452 &output_length ),
2453 PSA_ERROR_BAD_STATE );
2454
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002455 /* A default cipher operation should be abortable without error. */
2456 PSA_ASSERT( psa_cipher_abort( &func ) );
2457 PSA_ASSERT( psa_cipher_abort( &init ) );
2458 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002459}
2460/* END_CASE */
2461
2462/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002463void cipher_setup( int key_type_arg,
2464 data_t *key,
2465 int alg_arg,
2466 int expected_status_arg )
2467{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002468 psa_key_type_t key_type = key_type_arg;
2469 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002470 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002471 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002472 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002473#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002474 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2475#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476
Gilles Peskine8817f612018-12-18 00:18:46 +01002477 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002478
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002479 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2480 &operation, &status ) )
2481 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002482 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002483
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002484 /* The operation object should be reusable. */
2485#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2486 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2487 smoke_test_key_data,
2488 sizeof( smoke_test_key_data ),
2489 KNOWN_SUPPORTED_CIPHER_ALG,
2490 &operation, &status ) )
2491 goto exit;
2492 TEST_EQUAL( status, PSA_SUCCESS );
2493#endif
2494
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002495exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002496 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002497 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002498}
2499/* END_CASE */
2500
Ronald Cronee414c72021-03-18 18:50:08 +01002501/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002502void cipher_bad_order( )
2503{
Ronald Cron5425a212020-08-04 14:58:35 +02002504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002505 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2506 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002508 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002509 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002510 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002511 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2512 0xaa, 0xaa, 0xaa, 0xaa };
2513 const uint8_t text[] = {
2514 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2515 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002516 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002517 size_t length = 0;
2518
2519 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002520 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2521 psa_set_key_algorithm( &attributes, alg );
2522 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002523 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2524 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002525
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002526 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002527 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2528 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002529 PSA_ERROR_BAD_STATE );
2530 PSA_ASSERT( psa_cipher_abort( &operation ) );
2531
2532 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002533 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2534 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002535 PSA_ERROR_BAD_STATE );
2536 PSA_ASSERT( psa_cipher_abort( &operation ) );
2537
Jaeden Ameroab439972019-02-15 14:12:05 +00002538 /* Generate an IV without calling setup beforehand. */
2539 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2540 buffer, sizeof( buffer ),
2541 &length ),
2542 PSA_ERROR_BAD_STATE );
2543 PSA_ASSERT( psa_cipher_abort( &operation ) );
2544
2545 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002546 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002547 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2548 buffer, sizeof( buffer ),
2549 &length ) );
2550 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2551 buffer, sizeof( buffer ),
2552 &length ),
2553 PSA_ERROR_BAD_STATE );
2554 PSA_ASSERT( psa_cipher_abort( &operation ) );
2555
2556 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002557 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002558 PSA_ASSERT( psa_cipher_set_iv( &operation,
2559 iv, sizeof( iv ) ) );
2560 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2561 buffer, sizeof( buffer ),
2562 &length ),
2563 PSA_ERROR_BAD_STATE );
2564 PSA_ASSERT( psa_cipher_abort( &operation ) );
2565
2566 /* Set an IV without calling setup beforehand. */
2567 TEST_EQUAL( psa_cipher_set_iv( &operation,
2568 iv, sizeof( iv ) ),
2569 PSA_ERROR_BAD_STATE );
2570 PSA_ASSERT( psa_cipher_abort( &operation ) );
2571
2572 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002573 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002574 PSA_ASSERT( psa_cipher_set_iv( &operation,
2575 iv, sizeof( iv ) ) );
2576 TEST_EQUAL( psa_cipher_set_iv( &operation,
2577 iv, sizeof( iv ) ),
2578 PSA_ERROR_BAD_STATE );
2579 PSA_ASSERT( psa_cipher_abort( &operation ) );
2580
2581 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002582 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002583 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2584 buffer, sizeof( buffer ),
2585 &length ) );
2586 TEST_EQUAL( psa_cipher_set_iv( &operation,
2587 iv, sizeof( iv ) ),
2588 PSA_ERROR_BAD_STATE );
2589 PSA_ASSERT( psa_cipher_abort( &operation ) );
2590
2591 /* Call update without calling setup beforehand. */
2592 TEST_EQUAL( psa_cipher_update( &operation,
2593 text, sizeof( text ),
2594 buffer, sizeof( buffer ),
2595 &length ),
2596 PSA_ERROR_BAD_STATE );
2597 PSA_ASSERT( psa_cipher_abort( &operation ) );
2598
2599 /* Call update without an IV where an IV is required. */
2600 TEST_EQUAL( psa_cipher_update( &operation,
2601 text, sizeof( text ),
2602 buffer, sizeof( buffer ),
2603 &length ),
2604 PSA_ERROR_BAD_STATE );
2605 PSA_ASSERT( psa_cipher_abort( &operation ) );
2606
2607 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002608 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002609 PSA_ASSERT( psa_cipher_set_iv( &operation,
2610 iv, sizeof( iv ) ) );
2611 PSA_ASSERT( psa_cipher_finish( &operation,
2612 buffer, sizeof( buffer ), &length ) );
2613 TEST_EQUAL( psa_cipher_update( &operation,
2614 text, sizeof( text ),
2615 buffer, sizeof( buffer ),
2616 &length ),
2617 PSA_ERROR_BAD_STATE );
2618 PSA_ASSERT( psa_cipher_abort( &operation ) );
2619
2620 /* Call finish without calling setup beforehand. */
2621 TEST_EQUAL( psa_cipher_finish( &operation,
2622 buffer, sizeof( buffer ), &length ),
2623 PSA_ERROR_BAD_STATE );
2624 PSA_ASSERT( psa_cipher_abort( &operation ) );
2625
2626 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002627 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002628 /* Not calling update means we are encrypting an empty buffer, which is OK
2629 * for cipher modes with padding. */
2630 TEST_EQUAL( psa_cipher_finish( &operation,
2631 buffer, sizeof( buffer ), &length ),
2632 PSA_ERROR_BAD_STATE );
2633 PSA_ASSERT( psa_cipher_abort( &operation ) );
2634
2635 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002636 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002637 PSA_ASSERT( psa_cipher_set_iv( &operation,
2638 iv, sizeof( iv ) ) );
2639 PSA_ASSERT( psa_cipher_finish( &operation,
2640 buffer, sizeof( buffer ), &length ) );
2641 TEST_EQUAL( psa_cipher_finish( &operation,
2642 buffer, sizeof( buffer ), &length ),
2643 PSA_ERROR_BAD_STATE );
2644 PSA_ASSERT( psa_cipher_abort( &operation ) );
2645
Ronald Cron5425a212020-08-04 14:58:35 +02002646 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002647
Jaeden Ameroab439972019-02-15 14:12:05 +00002648exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002649 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002650 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002651}
2652/* END_CASE */
2653
2654/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002655void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002656 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002657 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002658 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659{
Ronald Cron5425a212020-08-04 14:58:35 +02002660 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661 psa_status_t status;
2662 psa_key_type_t key_type = key_type_arg;
2663 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002664 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002665 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666 size_t output_buffer_size = 0;
2667 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002668 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002669 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002671
Gilles Peskine8817f612018-12-18 00:18:46 +01002672 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002674 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2675 psa_set_key_algorithm( &attributes, alg );
2676 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002677
Ronald Cron5425a212020-08-04 14:58:35 +02002678 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2679 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002680
Ronald Cron5425a212020-08-04 14:58:35 +02002681 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002682
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002683 if( iv->len > 0 )
2684 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002685 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002686 }
2687
gabor-mezei-armceface22021-01-21 12:26:17 +01002688 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2689 TEST_ASSERT( output_buffer_size <=
2690 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002691 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002692
Gilles Peskine8817f612018-12-18 00:18:46 +01002693 PSA_ASSERT( psa_cipher_update( &operation,
2694 input->x, input->len,
2695 output, output_buffer_size,
2696 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002697 TEST_ASSERT( function_output_length <=
2698 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2699 TEST_ASSERT( function_output_length <=
2700 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002701 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002702
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002704 ( output_buffer_size == 0 ? NULL :
2705 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002706 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002707 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002708 TEST_ASSERT( function_output_length <=
2709 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2710 TEST_ASSERT( function_output_length <=
2711 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002712 total_output_length += function_output_length;
2713
Gilles Peskinefe11b722018-12-18 00:24:04 +01002714 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715 if( expected_status == PSA_SUCCESS )
2716 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002718 ASSERT_COMPARE( expected_output->x, expected_output->len,
2719 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002721
Gilles Peskine50e586b2018-06-08 14:28:46 +02002722exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002723 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002724 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002725 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002726 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002727}
2728/* END_CASE */
2729
2730/* BEGIN_CASE */
2731void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002732 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002733 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002734 int first_part_size_arg,
2735 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002736 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002737{
Ronald Cron5425a212020-08-04 14:58:35 +02002738 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002739 psa_key_type_t key_type = key_type_arg;
2740 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002741 size_t first_part_size = first_part_size_arg;
2742 size_t output1_length = output1_length_arg;
2743 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002744 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002745 size_t output_buffer_size = 0;
2746 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002747 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002748 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002749 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002750
Gilles Peskine8817f612018-12-18 00:18:46 +01002751 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002753 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2754 psa_set_key_algorithm( &attributes, alg );
2755 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002756
Ronald Cron5425a212020-08-04 14:58:35 +02002757 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2758 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002759
Ronald Cron5425a212020-08-04 14:58:35 +02002760 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002761
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002762 if( iv->len > 0 )
2763 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002764 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002765 }
2766
gabor-mezei-armceface22021-01-21 12:26:17 +01002767 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2768 TEST_ASSERT( output_buffer_size <=
2769 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002770 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002771
Gilles Peskinee0866522019-02-19 19:44:00 +01002772 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002773 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2774 output, output_buffer_size,
2775 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002776 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002777 TEST_ASSERT( function_output_length <=
2778 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2779 TEST_ASSERT( function_output_length <=
2780 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002781 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002782
Gilles Peskine8817f612018-12-18 00:18:46 +01002783 PSA_ASSERT( psa_cipher_update( &operation,
2784 input->x + first_part_size,
2785 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002786 ( output_buffer_size == 0 ? NULL :
2787 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002788 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002790 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002791 TEST_ASSERT( function_output_length <=
2792 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2793 alg,
2794 input->len - first_part_size ) );
2795 TEST_ASSERT( function_output_length <=
2796 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002797 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002798
Gilles Peskine8817f612018-12-18 00:18:46 +01002799 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002800 ( output_buffer_size == 0 ? NULL :
2801 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002802 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002804 TEST_ASSERT( function_output_length <=
2805 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2806 TEST_ASSERT( function_output_length <=
2807 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002808 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002809 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002810
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002811 ASSERT_COMPARE( expected_output->x, expected_output->len,
2812 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002813
2814exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002815 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002816 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002817 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002818 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002819}
2820/* END_CASE */
2821
2822/* BEGIN_CASE */
2823void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002824 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002825 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002826 int first_part_size_arg,
2827 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002828 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002829{
Ronald Cron5425a212020-08-04 14:58:35 +02002830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002831 psa_key_type_t key_type = key_type_arg;
2832 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002833 size_t first_part_size = first_part_size_arg;
2834 size_t output1_length = output1_length_arg;
2835 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002836 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837 size_t output_buffer_size = 0;
2838 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002839 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002840 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002841 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002842
Gilles Peskine8817f612018-12-18 00:18:46 +01002843 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002845 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2846 psa_set_key_algorithm( &attributes, alg );
2847 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002848
Ronald Cron5425a212020-08-04 14:58:35 +02002849 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2850 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002851
Ronald Cron5425a212020-08-04 14:58:35 +02002852 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002853
Steven Cooreman177deba2020-09-07 17:14:14 +02002854 if( iv->len > 0 )
2855 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002856 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002857 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002858
gabor-mezei-armceface22021-01-21 12:26:17 +01002859 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2860 TEST_ASSERT( output_buffer_size <=
2861 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002862 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002863
Gilles Peskinee0866522019-02-19 19:44:00 +01002864 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002865 PSA_ASSERT( psa_cipher_update( &operation,
2866 input->x, first_part_size,
2867 output, output_buffer_size,
2868 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002869 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002870 TEST_ASSERT( function_output_length <=
2871 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2872 TEST_ASSERT( function_output_length <=
2873 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002874 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002875
Gilles Peskine8817f612018-12-18 00:18:46 +01002876 PSA_ASSERT( psa_cipher_update( &operation,
2877 input->x + first_part_size,
2878 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002879 ( output_buffer_size == 0 ? NULL :
2880 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002881 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002882 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002883 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002884 TEST_ASSERT( function_output_length <=
2885 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2886 alg,
2887 input->len - first_part_size ) );
2888 TEST_ASSERT( function_output_length <=
2889 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002890 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002891
Gilles Peskine8817f612018-12-18 00:18:46 +01002892 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002893 ( output_buffer_size == 0 ? NULL :
2894 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002895 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002896 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002897 TEST_ASSERT( function_output_length <=
2898 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2899 TEST_ASSERT( function_output_length <=
2900 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002901 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002902 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002904 ASSERT_COMPARE( expected_output->x, expected_output->len,
2905 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002906
2907exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002908 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002909 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002910 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002911 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002912}
2913/* END_CASE */
2914
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915/* BEGIN_CASE */
2916void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002917 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002918 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002919 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920{
Ronald Cron5425a212020-08-04 14:58:35 +02002921 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922 psa_status_t status;
2923 psa_key_type_t key_type = key_type_arg;
2924 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002925 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002926 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927 size_t output_buffer_size = 0;
2928 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002929 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002930 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002932
Gilles Peskine8817f612018-12-18 00:18:46 +01002933 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002935 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2936 psa_set_key_algorithm( &attributes, alg );
2937 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002938
Ronald Cron5425a212020-08-04 14:58:35 +02002939 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2940 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002941
Ronald Cron5425a212020-08-04 14:58:35 +02002942 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002943
Steven Cooreman177deba2020-09-07 17:14:14 +02002944 if( iv->len > 0 )
2945 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002946 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002947 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002948
gabor-mezei-armceface22021-01-21 12:26:17 +01002949 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2950 TEST_ASSERT( output_buffer_size <=
2951 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002952 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002953
Gilles Peskine8817f612018-12-18 00:18:46 +01002954 PSA_ASSERT( psa_cipher_update( &operation,
2955 input->x, input->len,
2956 output, output_buffer_size,
2957 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002958 TEST_ASSERT( function_output_length <=
2959 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2960 TEST_ASSERT( function_output_length <=
2961 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002962 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002963
Gilles Peskine50e586b2018-06-08 14:28:46 +02002964 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002965 ( output_buffer_size == 0 ? NULL :
2966 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002967 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002968 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002969 TEST_ASSERT( function_output_length <=
2970 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2971 TEST_ASSERT( function_output_length <=
2972 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002973 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002974 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002975
2976 if( expected_status == PSA_SUCCESS )
2977 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002978 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002979 ASSERT_COMPARE( expected_output->x, expected_output->len,
2980 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002981 }
2982
Gilles Peskine50e586b2018-06-08 14:28:46 +02002983exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002984 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002985 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002986 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002987 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002988}
2989/* END_CASE */
2990
Gilles Peskine50e586b2018-06-08 14:28:46 +02002991/* BEGIN_CASE */
2992void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002993 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002994 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002995{
Ronald Cron5425a212020-08-04 14:58:35 +02002996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002997 psa_key_type_t key_type = key_type_arg;
2998 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002999 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003000 size_t iv_size = 16;
3001 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003002 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003003 size_t output1_size = 0;
3004 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003005 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003006 size_t output2_size = 0;
3007 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003008 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003009 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3010 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003012
Gilles Peskine8817f612018-12-18 00:18:46 +01003013 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003014
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003015 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3016 psa_set_key_algorithm( &attributes, alg );
3017 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003018
Ronald Cron5425a212020-08-04 14:58:35 +02003019 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3020 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003021
Ronald Cron5425a212020-08-04 14:58:35 +02003022 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3023 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003024
Steven Cooreman177deba2020-09-07 17:14:14 +02003025 if( alg != PSA_ALG_ECB_NO_PADDING )
3026 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003027 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3028 iv, iv_size,
3029 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003030 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003031 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3032 TEST_ASSERT( output1_size <=
3033 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003034 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003035
Gilles Peskine8817f612018-12-18 00:18:46 +01003036 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3037 output1, output1_size,
3038 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003039 TEST_ASSERT( output1_length <=
3040 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3041 TEST_ASSERT( output1_length <=
3042 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3043
Gilles Peskine8817f612018-12-18 00:18:46 +01003044 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003045 output1 + output1_length,
3046 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003047 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003048 TEST_ASSERT( function_output_length <=
3049 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3050 TEST_ASSERT( function_output_length <=
3051 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003052
Gilles Peskine048b7f02018-06-08 14:20:49 +02003053 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003054
Gilles Peskine8817f612018-12-18 00:18:46 +01003055 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003056
3057 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003058 TEST_ASSERT( output2_size <=
3059 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3060 TEST_ASSERT( output2_size <=
3061 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003062 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003063
Steven Cooreman177deba2020-09-07 17:14:14 +02003064 if( iv_length > 0 )
3065 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003066 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3067 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003068 }
3069
Gilles Peskine8817f612018-12-18 00:18:46 +01003070 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3071 output2, output2_size,
3072 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003073 TEST_ASSERT( output2_length <=
3074 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3075 TEST_ASSERT( output2_length <=
3076 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3077
Gilles Peskine048b7f02018-06-08 14:20:49 +02003078 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003079 PSA_ASSERT( psa_cipher_finish( &operation2,
3080 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003081 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003082 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003083 TEST_ASSERT( function_output_length <=
3084 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3085 TEST_ASSERT( function_output_length <=
3086 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003087
Gilles Peskine048b7f02018-06-08 14:20:49 +02003088 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003089
Gilles Peskine8817f612018-12-18 00:18:46 +01003090 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003091
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003092 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003093
3094exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003095 psa_cipher_abort( &operation1 );
3096 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003097 mbedtls_free( output1 );
3098 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003099 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003100 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003101}
3102/* END_CASE */
3103
3104/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003105void cipher_verify_output_multipart( int alg_arg,
3106 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003107 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003108 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003109 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003110{
Ronald Cron5425a212020-08-04 14:58:35 +02003111 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003112 psa_key_type_t key_type = key_type_arg;
3113 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003114 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003115 unsigned char iv[16] = {0};
3116 size_t iv_size = 16;
3117 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003118 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003119 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003120 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003121 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003122 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003123 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003124 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003125 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3126 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003127 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003128
Gilles Peskine8817f612018-12-18 00:18:46 +01003129 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003130
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003131 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3132 psa_set_key_algorithm( &attributes, alg );
3133 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003134
Ronald Cron5425a212020-08-04 14:58:35 +02003135 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3136 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003137
Ronald Cron5425a212020-08-04 14:58:35 +02003138 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3139 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003140
Steven Cooreman177deba2020-09-07 17:14:14 +02003141 if( alg != PSA_ALG_ECB_NO_PADDING )
3142 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003143 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3144 iv, iv_size,
3145 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003146 }
3147
gabor-mezei-armceface22021-01-21 12:26:17 +01003148 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3149 TEST_ASSERT( output1_buffer_size <=
3150 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003151 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003152
Gilles Peskinee0866522019-02-19 19:44:00 +01003153 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3156 output1, output1_buffer_size,
3157 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003158 TEST_ASSERT( function_output_length <=
3159 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3160 TEST_ASSERT( function_output_length <=
3161 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003162 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003163
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_cipher_update( &operation1,
3165 input->x + first_part_size,
3166 input->len - first_part_size,
3167 output1, output1_buffer_size,
3168 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003169 TEST_ASSERT( function_output_length <=
3170 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3171 alg,
3172 input->len - first_part_size ) );
3173 TEST_ASSERT( function_output_length <=
3174 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003175 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003176
Gilles Peskine8817f612018-12-18 00:18:46 +01003177 PSA_ASSERT( psa_cipher_finish( &operation1,
3178 output1 + output1_length,
3179 output1_buffer_size - output1_length,
3180 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003181 TEST_ASSERT( function_output_length <=
3182 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3183 TEST_ASSERT( function_output_length <=
3184 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003185 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003186
Gilles Peskine8817f612018-12-18 00:18:46 +01003187 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003188
Gilles Peskine048b7f02018-06-08 14:20:49 +02003189 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003190 TEST_ASSERT( output2_buffer_size <=
3191 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3192 TEST_ASSERT( output2_buffer_size <=
3193 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003194 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003195
Steven Cooreman177deba2020-09-07 17:14:14 +02003196 if( iv_length > 0 )
3197 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003198 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3199 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003200 }
Moran Pekerded84402018-06-06 16:36:50 +03003201
Gilles Peskine8817f612018-12-18 00:18:46 +01003202 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3203 output2, output2_buffer_size,
3204 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003205 TEST_ASSERT( function_output_length <=
3206 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3207 TEST_ASSERT( function_output_length <=
3208 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003209 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003210
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_cipher_update( &operation2,
3212 output1 + first_part_size,
3213 output1_length - first_part_size,
3214 output2, output2_buffer_size,
3215 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003216 TEST_ASSERT( function_output_length <=
3217 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3218 alg,
3219 output1_length - first_part_size ) );
3220 TEST_ASSERT( function_output_length <=
3221 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003222 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003223
Gilles Peskine8817f612018-12-18 00:18:46 +01003224 PSA_ASSERT( psa_cipher_finish( &operation2,
3225 output2 + output2_length,
3226 output2_buffer_size - output2_length,
3227 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003228 TEST_ASSERT( function_output_length <=
3229 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3230 TEST_ASSERT( function_output_length <=
3231 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003232 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003233
Gilles Peskine8817f612018-12-18 00:18:46 +01003234 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003235
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003236 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003237
3238exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003239 psa_cipher_abort( &operation1 );
3240 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003241 mbedtls_free( output1 );
3242 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003243 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003244 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003245}
3246/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003247
Gilles Peskine20035e32018-02-03 22:44:14 +01003248/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003249void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003250 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003251 data_t *nonce,
3252 data_t *additional_data,
3253 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003254 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003255{
Ronald Cron5425a212020-08-04 14:58:35 +02003256 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003257 psa_key_type_t key_type = key_type_arg;
3258 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003259 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003260 unsigned char *output_data = NULL;
3261 size_t output_size = 0;
3262 size_t output_length = 0;
3263 unsigned char *output_data2 = NULL;
3264 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003265 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003266 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003268
Gilles Peskine8817f612018-12-18 00:18:46 +01003269 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003270
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003271 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3272 psa_set_key_algorithm( &attributes, alg );
3273 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003274
Gilles Peskine049c7532019-05-15 20:22:09 +02003275 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003276 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003277 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3278 key_bits = psa_get_key_bits( &attributes );
3279
3280 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3281 alg );
3282 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3283 * should be exact. */
3284 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3285 expected_result != PSA_ERROR_NOT_SUPPORTED )
3286 {
3287 TEST_EQUAL( output_size,
3288 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3289 TEST_ASSERT( output_size <=
3290 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3291 }
3292 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003293
Steven Cooremanf49478b2021-02-15 15:19:25 +01003294 status = psa_aead_encrypt( key, alg,
3295 nonce->x, nonce->len,
3296 additional_data->x,
3297 additional_data->len,
3298 input_data->x, input_data->len,
3299 output_data, output_size,
3300 &output_length );
3301
3302 /* If the operation is not supported, just skip and not fail in case the
3303 * encryption involves a common limitation of cryptography hardwares and
3304 * an alternative implementation. */
3305 if( status == PSA_ERROR_NOT_SUPPORTED )
3306 {
3307 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3308 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3309 }
3310
3311 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003312
3313 if( PSA_SUCCESS == expected_result )
3314 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003315 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003316
Gilles Peskine003a4a92019-05-14 16:09:40 +02003317 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3318 * should be exact. */
3319 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003320 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003321
gabor-mezei-armceface22021-01-21 12:26:17 +01003322 TEST_ASSERT( input_data->len <=
3323 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3324
Ronald Cron5425a212020-08-04 14:58:35 +02003325 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003326 nonce->x, nonce->len,
3327 additional_data->x,
3328 additional_data->len,
3329 output_data, output_length,
3330 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003331 &output_length2 ),
3332 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003333
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003334 ASSERT_COMPARE( input_data->x, input_data->len,
3335 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003337
Gilles Peskinea1cac842018-06-11 19:33:02 +02003338exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003339 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003340 mbedtls_free( output_data );
3341 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003342 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343}
3344/* END_CASE */
3345
3346/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003347void aead_encrypt( int key_type_arg, data_t *key_data,
3348 int alg_arg,
3349 data_t *nonce,
3350 data_t *additional_data,
3351 data_t *input_data,
3352 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003353{
Ronald Cron5425a212020-08-04 14:58:35 +02003354 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003355 psa_key_type_t key_type = key_type_arg;
3356 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003357 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003358 unsigned char *output_data = NULL;
3359 size_t output_size = 0;
3360 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003362 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003363
Gilles Peskine8817f612018-12-18 00:18:46 +01003364 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003366 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3367 psa_set_key_algorithm( &attributes, alg );
3368 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003369
Gilles Peskine049c7532019-05-15 20:22:09 +02003370 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003371 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003372 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3373 key_bits = psa_get_key_bits( &attributes );
3374
3375 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3376 alg );
3377 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3378 * should be exact. */
3379 TEST_EQUAL( output_size,
3380 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3381 TEST_ASSERT( output_size <=
3382 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3383 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003384
Steven Cooremand588ea12021-01-11 19:36:04 +01003385 status = psa_aead_encrypt( key, alg,
3386 nonce->x, nonce->len,
3387 additional_data->x, additional_data->len,
3388 input_data->x, input_data->len,
3389 output_data, output_size,
3390 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003391
Ronald Cron28a45ed2021-02-09 20:35:42 +01003392 /* If the operation is not supported, just skip and not fail in case the
3393 * encryption involves a common limitation of cryptography hardwares and
3394 * an alternative implementation. */
3395 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003396 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003397 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3398 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003399 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003400
3401 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003402 ASSERT_COMPARE( expected_result->x, expected_result->len,
3403 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003404
Gilles Peskinea1cac842018-06-11 19:33:02 +02003405exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003406 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003407 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003408 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003409}
3410/* END_CASE */
3411
3412/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003413void aead_decrypt( int key_type_arg, data_t *key_data,
3414 int alg_arg,
3415 data_t *nonce,
3416 data_t *additional_data,
3417 data_t *input_data,
3418 data_t *expected_data,
3419 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420{
Ronald Cron5425a212020-08-04 14:58:35 +02003421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003422 psa_key_type_t key_type = key_type_arg;
3423 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003424 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003425 unsigned char *output_data = NULL;
3426 size_t output_size = 0;
3427 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003428 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003429 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003430 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003431
Gilles Peskine8817f612018-12-18 00:18:46 +01003432 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003433
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003434 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3435 psa_set_key_algorithm( &attributes, alg );
3436 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003437
Gilles Peskine049c7532019-05-15 20:22:09 +02003438 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003439 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003440 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3441 key_bits = psa_get_key_bits( &attributes );
3442
3443 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3444 alg );
3445 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3446 expected_result != PSA_ERROR_NOT_SUPPORTED )
3447 {
3448 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3449 * should be exact. */
3450 TEST_EQUAL( output_size,
3451 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3452 TEST_ASSERT( output_size <=
3453 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3454 }
3455 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456
Steven Cooremand588ea12021-01-11 19:36:04 +01003457 status = psa_aead_decrypt( key, alg,
3458 nonce->x, nonce->len,
3459 additional_data->x,
3460 additional_data->len,
3461 input_data->x, input_data->len,
3462 output_data, output_size,
3463 &output_length );
3464
Ronald Cron28a45ed2021-02-09 20:35:42 +01003465 /* If the operation is not supported, just skip and not fail in case the
3466 * decryption involves a common limitation of cryptography hardwares and
3467 * an alternative implementation. */
3468 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003469 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003470 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3471 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003472 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003473
3474 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475
Gilles Peskine2d277862018-06-18 15:41:12 +02003476 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003477 ASSERT_COMPARE( expected_data->x, expected_data->len,
3478 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479
Gilles Peskinea1cac842018-06-11 19:33:02 +02003480exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003481 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003482 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003483 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003484}
3485/* END_CASE */
3486
3487/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003488void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3489 int alg_arg,
3490 data_t *nonce,
3491 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003492 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003493 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003494 int do_test_data_chunked,
3495 int do_set_lengths,
3496 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003497{
Paul Elliottd3f82412021-06-16 16:52:21 +01003498 size_t ad_part_len = 0;
3499 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003500 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003501
Paul Elliotte64deda2021-09-09 14:07:23 +01003502 /* Ensure that either one part of the test or the other is done, i.e this
3503 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003504 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3505
3506 /* Temporary whilst we have algorithms that cannot support chunking */
3507 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003508 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003509 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3510 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003511 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003512 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003513
Paul Elliott33746aa2021-09-15 16:40:40 +01003514 if( do_set_lengths )
3515 {
3516 if( ad_part_len & 0x01 )
3517 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3518 else
3519 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3520 }
3521
Paul Elliott329d5382021-07-22 17:10:45 +01003522 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003523 if( !aead_multipart_internal_func( key_type_arg, key_data,
3524 alg_arg, nonce,
3525 additional_data,
3526 ad_part_len,
3527 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003528 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003529 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003530 1, 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003531 break;
3532
3533 /* length(0) part, length(ad_part_len) part, length(0) part... */
3534 mbedtls_test_set_step( 1000 + ad_part_len );
3535
3536 if( !aead_multipart_internal_func( key_type_arg, key_data,
3537 alg_arg, nonce,
3538 additional_data,
3539 ad_part_len,
3540 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003541 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003542 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003543 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003544 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003545 }
3546 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003547
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003548 /* Temporary whilst we have algorithms that cannot support chunking */
3549 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003550 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003551 for( data_part_len = 1; data_part_len <= input_data->len;
3552 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003553 {
Paul Elliott329d5382021-07-22 17:10:45 +01003554 /* Split data into length(data_part_len) parts. */
3555 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003556
Paul Elliott33746aa2021-09-15 16:40:40 +01003557 if( do_set_lengths )
3558 {
3559 if( data_part_len & 0x01 )
3560 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3561 else
3562 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3563 }
3564
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003565 if( !aead_multipart_internal_func( key_type_arg, key_data,
3566 alg_arg, nonce,
3567 additional_data, -1,
3568 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003569 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003570 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003571 1, 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003572 break;
3573
3574 /* length(0) part, length(data_part_len) part, length(0) part... */
3575 mbedtls_test_set_step( 3000 + data_part_len );
3576
3577 if( !aead_multipart_internal_func( key_type_arg, key_data,
3578 alg_arg, nonce,
3579 additional_data, -1,
3580 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003581 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003582 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003583 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003584 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003585 }
3586 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003587
Paul Elliott0023e0a2021-04-27 10:06:22 +01003588
Paul Elliott8fc45162021-06-23 16:06:01 +01003589 /* Goto is required to silence warnings about unused labels, as we
3590 * don't actually do any test assertions in this function. */
3591 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003592}
3593/* END_CASE */
3594
3595/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003596void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3597 int alg_arg,
3598 data_t *nonce,
3599 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003600 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003601 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003602 int do_test_data_chunked,
3603 int do_set_lengths,
3604 data_t *expected_output,
3605 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003606{
Paul Elliottd3f82412021-06-16 16:52:21 +01003607 size_t ad_part_len = 0;
3608 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003609 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003610
Paul Elliotte64deda2021-09-09 14:07:23 +01003611 /* Ensure that either one part of the test or the other is done, i.e this
3612 * test does something. */
3613 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3614
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003615 /* Temporary whilst we have algorithms that cannot support chunking */
3616 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003617 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003618 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3619 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003620 {
Paul Elliott329d5382021-07-22 17:10:45 +01003621 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003622 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003623
Paul Elliott33746aa2021-09-15 16:40:40 +01003624 if( do_set_lengths )
3625 {
3626 if( ad_part_len & 0x01 )
3627 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3628 else
3629 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3630 }
3631
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003632 if( !aead_multipart_internal_func( key_type_arg, key_data,
3633 alg_arg, nonce,
3634 additional_data,
3635 ad_part_len,
3636 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003637 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003638 expected_output,
3639 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003640 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003641 break;
3642
3643 /* length(0) part, length(ad_part_len) part, length(0) part... */
3644 mbedtls_test_set_step( 1000 + ad_part_len );
3645
3646 if( !aead_multipart_internal_func( key_type_arg, key_data,
3647 alg_arg, nonce,
3648 additional_data,
3649 ad_part_len,
3650 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003651 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003652 expected_output,
3653 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003654 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003655 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003656 }
3657 }
3658
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003659 /* Temporary whilst we have algorithms that cannot support chunking */
3660 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003661 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003662 for( data_part_len = 1; data_part_len <= input_data->len;
3663 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003664 {
Paul Elliott329d5382021-07-22 17:10:45 +01003665 /* Split data into length(data_part_len) parts. */
3666 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003667
Paul Elliott33746aa2021-09-15 16:40:40 +01003668 if( do_set_lengths )
3669 {
3670 if( data_part_len & 0x01 )
3671 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3672 else
3673 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3674 }
3675
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003676 if( !aead_multipart_internal_func( key_type_arg, key_data,
3677 alg_arg, nonce,
3678 additional_data, -1,
3679 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003680 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003681 expected_output,
3682 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003683 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003684 break;
3685
3686 /* length(0) part, length(data_part_len) part, length(0) part... */
3687 mbedtls_test_set_step( 3000 + data_part_len );
3688
3689 if( !aead_multipart_internal_func( key_type_arg, key_data,
3690 alg_arg, nonce,
3691 additional_data, -1,
3692 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003693 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003694 expected_output,
3695 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003696 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003697 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003698 }
3699 }
3700
Paul Elliott8fc45162021-06-23 16:06:01 +01003701 /* Goto is required to silence warnings about unused labels, as we
3702 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003703 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003704}
3705/* END_CASE */
3706
3707/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003708void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3709 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003710 int nonce_length,
3711 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003712 data_t *additional_data,
3713 data_t *input_data,
3714 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003715{
3716
3717 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3718 psa_key_type_t key_type = key_type_arg;
3719 psa_algorithm_t alg = alg_arg;
3720 psa_aead_operation_t operation;
3721 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3723 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003724 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003725 size_t actual_nonce_length = 0;
3726 size_t expected_nonce_length = expected_nonce_length_arg;
3727 unsigned char *output = NULL;
3728 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003729 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003730 size_t ciphertext_size = 0;
3731 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003732 size_t tag_length = 0;
3733 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003734
3735 PSA_ASSERT( psa_crypto_init( ) );
3736
3737 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3738 psa_set_key_algorithm( & attributes, alg );
3739 psa_set_key_type( & attributes, key_type );
3740
3741 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3742 &key ) );
3743
3744 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3745
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003746 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3747
Paul Elliottf1277632021-08-24 18:11:37 +01003748 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003749
Paul Elliottf1277632021-08-24 18:11:37 +01003750 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003751
Paul Elliottf1277632021-08-24 18:11:37 +01003752 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003753
Paul Elliottf1277632021-08-24 18:11:37 +01003754 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003755
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003756 operation = psa_aead_operation_init( );
3757
3758 status = psa_aead_encrypt_setup( &operation, key, alg );
3759
3760 /* If the operation is not supported, just skip and not fail in case the
3761 * encryption involves a common limitation of cryptography hardwares and
3762 * an alternative implementation. */
3763 if( status == PSA_ERROR_NOT_SUPPORTED )
3764 {
3765 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003766 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003767 }
3768
3769 PSA_ASSERT( status );
3770
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003771 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003772 nonce_length,
3773 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003774
Paul Elliott693bf312021-07-23 17:40:41 +01003775 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003776
Paul Elliottf1277632021-08-24 18:11:37 +01003777 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003778
Paul Elliottf1277632021-08-24 18:11:37 +01003779 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003780
Paul Elliott693bf312021-07-23 17:40:41 +01003781 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003782 {
3783
3784 /* Ensure we can still complete operation. */
3785
3786 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3787 additional_data->len ) );
3788
3789 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003790 output, output_size,
3791 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003792
Paul Elliottf1277632021-08-24 18:11:37 +01003793 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3794 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003795 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3796 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003797
3798exit:
3799 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003800 mbedtls_free( output );
3801 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003802 psa_aead_abort( &operation );
3803 PSA_DONE( );
3804}
3805/* END_CASE */
3806
3807/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003808void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3809 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003810 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003811 data_t *additional_data,
3812 data_t *input_data,
3813 int expected_status_arg )
3814{
3815
3816 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3817 psa_key_type_t key_type = key_type_arg;
3818 psa_algorithm_t alg = alg_arg;
3819 psa_aead_operation_t operation;
3820 uint8_t *nonce_buffer = NULL;
3821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3822 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3823 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003824 unsigned char *output = NULL;
3825 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003826 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003827 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003828 size_t ciphertext_size = 0;
3829 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003830 size_t tag_length = 0;
3831 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003832 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003833
3834 PSA_ASSERT( psa_crypto_init( ) );
3835
3836 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3837 psa_set_key_algorithm( &attributes, alg );
3838 psa_set_key_type( &attributes, key_type );
3839
3840 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3841 &key ) );
3842
3843 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3844
3845 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3846
Paul Elliott6f0e7202021-08-25 12:57:18 +01003847 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003848
Paul Elliott6f0e7202021-08-25 12:57:18 +01003849 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003850
Paul Elliott6f0e7202021-08-25 12:57:18 +01003851 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003852
Paul Elliott6f0e7202021-08-25 12:57:18 +01003853 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003854
3855 operation = psa_aead_operation_init( );
3856
3857 status = psa_aead_encrypt_setup( &operation, key, alg );
3858
3859 /* If the operation is not supported, just skip and not fail in case the
3860 * encryption involves a common limitation of cryptography hardwares and
3861 * an alternative implementation. */
3862 if( status == PSA_ERROR_NOT_SUPPORTED )
3863 {
3864 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003865 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003866 }
3867
3868 PSA_ASSERT( status );
3869
Paul Elliott4023ffd2021-09-10 16:21:22 +01003870 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3871 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003872 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003873 /* Arbitrary size buffer, to test zero length valid buffer. */
3874 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003875 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003876 }
3877 else
3878 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003879 /* If length is zero, then this will return NULL. */
3880 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003881 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003882
Paul Elliott4023ffd2021-09-10 16:21:22 +01003883 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003884 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003885 for( index = 0; index < nonce_length - 1; ++index )
3886 {
3887 nonce_buffer[index] = 'a' + index;
3888 }
Paul Elliott66696b52021-08-16 18:42:41 +01003889 }
Paul Elliott863864a2021-07-23 17:28:31 +01003890 }
3891
Paul Elliott6f0e7202021-08-25 12:57:18 +01003892 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003893
Paul Elliott693bf312021-07-23 17:40:41 +01003894 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003895
3896 if( expected_status == PSA_SUCCESS )
3897 {
3898 /* Ensure we can still complete operation. */
3899
3900 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3901 additional_data->len ) );
3902
3903 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003904 output, output_size,
3905 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003906
Paul Elliott6f0e7202021-08-25 12:57:18 +01003907 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3908 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003909 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3910 }
3911
3912exit:
3913 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003914 mbedtls_free( output );
3915 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003916 mbedtls_free( nonce_buffer );
3917 psa_aead_abort( &operation );
3918 PSA_DONE( );
3919}
3920/* END_CASE */
3921
3922/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003923void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3924 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003925 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003926 data_t *nonce,
3927 data_t *additional_data,
3928 data_t *input_data,
3929 int expected_status_arg )
3930{
3931
3932 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3933 psa_key_type_t key_type = key_type_arg;
3934 psa_algorithm_t alg = alg_arg;
3935 psa_aead_operation_t operation;
3936 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3937 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3938 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003939 unsigned char *output = NULL;
3940 unsigned char *ciphertext = NULL;
3941 size_t output_size = output_size_arg;
3942 size_t ciphertext_size = 0;
3943 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003944 size_t tag_length = 0;
3945 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3946
3947 PSA_ASSERT( psa_crypto_init( ) );
3948
3949 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3950 psa_set_key_algorithm( &attributes, alg );
3951 psa_set_key_type( &attributes, key_type );
3952
3953 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3954 &key ) );
3955
3956 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3957
Paul Elliottc6d11d02021-09-01 12:04:23 +01003958 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003959
Paul Elliottc6d11d02021-09-01 12:04:23 +01003960 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003961
Paul Elliottc6d11d02021-09-01 12:04:23 +01003962 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003963
3964 operation = psa_aead_operation_init( );
3965
3966 status = psa_aead_encrypt_setup( &operation, key, alg );
3967
3968 /* If the operation is not supported, just skip and not fail in case the
3969 * encryption involves a common limitation of cryptography hardwares and
3970 * an alternative implementation. */
3971 if( status == PSA_ERROR_NOT_SUPPORTED )
3972 {
3973 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3974 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3975 }
3976
3977 PSA_ASSERT( status );
3978
3979 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3980
3981 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3982 additional_data->len ) );
3983
3984 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003985 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003986
3987 TEST_EQUAL( status, expected_status );
3988
3989 if( expected_status == PSA_SUCCESS )
3990 {
3991 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003992 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3993 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003994 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3995 }
3996
3997exit:
3998 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003999 mbedtls_free( output );
4000 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004001 psa_aead_abort( &operation );
4002 PSA_DONE( );
4003}
4004/* END_CASE */
4005
Paul Elliott91b021e2021-07-23 18:52:31 +01004006/* BEGIN_CASE */
4007void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4008 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004009 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004010 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004011 data_t *nonce,
4012 data_t *additional_data,
4013 data_t *input_data,
4014 int expected_status_arg )
4015{
4016
4017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4018 psa_key_type_t key_type = key_type_arg;
4019 psa_algorithm_t alg = alg_arg;
4020 psa_aead_operation_t operation;
4021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4022 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4023 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004024 unsigned char *ciphertext = NULL;
4025 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004026 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004027 size_t ciphertext_size = 0;
4028 size_t ciphertext_length = 0;
4029 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004030 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004031 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004032
4033 PSA_ASSERT( psa_crypto_init( ) );
4034
4035 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4036 psa_set_key_algorithm( &attributes, alg );
4037 psa_set_key_type( &attributes, key_type );
4038
4039 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4040 &key ) );
4041
4042 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4043
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004044 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004045
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004046 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004047
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004048 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004049
Paul Elliott719c1322021-09-13 18:27:22 +01004050 ASSERT_ALLOC( tag_buffer, tag_size );
4051
Paul Elliott91b021e2021-07-23 18:52:31 +01004052 operation = psa_aead_operation_init( );
4053
4054 status = psa_aead_encrypt_setup( &operation, key, alg );
4055
4056 /* If the operation is not supported, just skip and not fail in case the
4057 * encryption involves a common limitation of cryptography hardwares and
4058 * an alternative implementation. */
4059 if( status == PSA_ERROR_NOT_SUPPORTED )
4060 {
4061 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4062 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4063 }
4064
4065 PSA_ASSERT( status );
4066
4067 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4068
4069 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4070 additional_data->len ) );
4071
4072 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004073 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004074
4075 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004076 status = psa_aead_finish( &operation, finish_ciphertext,
4077 finish_ciphertext_size,
4078 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004079 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004080
4081 TEST_EQUAL( status, expected_status );
4082
4083exit:
4084 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004085 mbedtls_free( ciphertext );
4086 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004087 psa_aead_abort( &operation );
4088 PSA_DONE( );
4089}
4090/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004091
4092/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004093void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4094 int alg_arg,
4095 data_t *nonce,
4096 data_t *additional_data,
4097 data_t *input_data )
4098{
4099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4100 psa_key_type_t key_type = key_type_arg;
4101 psa_algorithm_t alg = alg_arg;
4102 psa_aead_operation_t operation;
4103 unsigned char *output_data = NULL;
4104 unsigned char *final_data = NULL;
4105 size_t output_size = 0;
4106 size_t finish_output_size = 0;
4107 size_t output_length = 0;
4108 size_t key_bits = 0;
4109 size_t tag_length = 0;
4110 size_t tag_size = 0;
4111 size_t nonce_length = 0;
4112 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4113 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4114 size_t output_part_length = 0;
4115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4116
4117 PSA_ASSERT( psa_crypto_init( ) );
4118
4119 psa_set_key_usage_flags( & attributes,
4120 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4121 psa_set_key_algorithm( & attributes, alg );
4122 psa_set_key_type( & attributes, key_type );
4123
4124 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4125 &key ) );
4126
4127 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4128 key_bits = psa_get_key_bits( &attributes );
4129
4130 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4131
4132 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4133
4134 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4135
4136 ASSERT_ALLOC( output_data, output_size );
4137
4138 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4139
4140 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4141
4142 ASSERT_ALLOC( final_data, finish_output_size );
4143
4144 /* Test all operations error without calling setup first. */
4145
4146 operation = psa_aead_operation_init( );
4147
4148 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4149 PSA_ERROR_BAD_STATE );
4150
4151 psa_aead_abort( &operation );
4152
4153 operation = psa_aead_operation_init( );
4154
4155 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4156 PSA_AEAD_NONCE_MAX_SIZE,
4157 &nonce_length ),
4158 PSA_ERROR_BAD_STATE );
4159
4160 psa_aead_abort( &operation );
4161
Paul Elliott481be342021-07-16 17:38:47 +01004162 /* ------------------------------------------------------- */
4163
Paul Elliottc23a9a02021-06-21 18:32:46 +01004164 operation = psa_aead_operation_init( );
4165
4166 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4167 input_data->len ),
4168 PSA_ERROR_BAD_STATE );
4169
4170 psa_aead_abort( &operation );
4171
Paul Elliott481be342021-07-16 17:38:47 +01004172 /* ------------------------------------------------------- */
4173
Paul Elliottc23a9a02021-06-21 18:32:46 +01004174 operation = psa_aead_operation_init( );
4175
4176 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4177 additional_data->len ),
4178 PSA_ERROR_BAD_STATE );
4179
4180 psa_aead_abort( &operation );
4181
Paul Elliott481be342021-07-16 17:38:47 +01004182 /* ------------------------------------------------------- */
4183
Paul Elliottc23a9a02021-06-21 18:32:46 +01004184 operation = psa_aead_operation_init( );
4185
4186 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4187 input_data->len, output_data,
4188 output_size, &output_length ),
4189 PSA_ERROR_BAD_STATE );
4190
4191 psa_aead_abort( &operation );
4192
Paul Elliott481be342021-07-16 17:38:47 +01004193 /* ------------------------------------------------------- */
4194
Paul Elliottc23a9a02021-06-21 18:32:46 +01004195 operation = psa_aead_operation_init( );
4196
4197 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4198 finish_output_size,
4199 &output_part_length,
4200 tag_buffer, tag_length,
4201 &tag_size ),
4202 PSA_ERROR_BAD_STATE );
4203
4204 psa_aead_abort( &operation );
4205
Paul Elliott481be342021-07-16 17:38:47 +01004206 /* ------------------------------------------------------- */
4207
Paul Elliottc23a9a02021-06-21 18:32:46 +01004208 operation = psa_aead_operation_init( );
4209
4210 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4211 finish_output_size,
4212 &output_part_length,
4213 tag_buffer,
4214 tag_length ),
4215 PSA_ERROR_BAD_STATE );
4216
4217 psa_aead_abort( &operation );
4218
4219 /* Test for double setups. */
4220
4221 operation = psa_aead_operation_init( );
4222
4223 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4224
4225 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4226 PSA_ERROR_BAD_STATE );
4227
4228 psa_aead_abort( &operation );
4229
Paul Elliott481be342021-07-16 17:38:47 +01004230 /* ------------------------------------------------------- */
4231
Paul Elliottc23a9a02021-06-21 18:32:46 +01004232 operation = psa_aead_operation_init( );
4233
4234 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4235
4236 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4237 PSA_ERROR_BAD_STATE );
4238
4239 psa_aead_abort( &operation );
4240
Paul Elliott374a2be2021-07-16 17:53:40 +01004241 /* ------------------------------------------------------- */
4242
4243 operation = psa_aead_operation_init( );
4244
4245 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4246
4247 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4248 PSA_ERROR_BAD_STATE );
4249
4250 psa_aead_abort( &operation );
4251
4252 /* ------------------------------------------------------- */
4253
4254 operation = psa_aead_operation_init( );
4255
4256 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4257
4258 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4259 PSA_ERROR_BAD_STATE );
4260
4261 psa_aead_abort( &operation );
4262
Paul Elliottc23a9a02021-06-21 18:32:46 +01004263 /* Test for not setting a nonce. */
4264
4265 operation = psa_aead_operation_init( );
4266
4267 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4268
4269 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4270 additional_data->len ),
4271 PSA_ERROR_BAD_STATE );
4272
4273 psa_aead_abort( &operation );
4274
Paul Elliott7f628422021-09-01 12:08:29 +01004275 /* ------------------------------------------------------- */
4276
4277 operation = psa_aead_operation_init( );
4278
4279 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4280
4281 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4282 input_data->len, output_data,
4283 output_size, &output_length ),
4284 PSA_ERROR_BAD_STATE );
4285
4286 psa_aead_abort( &operation );
4287
Paul Elliottc23a9a02021-06-21 18:32:46 +01004288 /* Test for double setting nonce. */
4289
4290 operation = psa_aead_operation_init( );
4291
4292 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4293
4294 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4295
4296 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4297 PSA_ERROR_BAD_STATE );
4298
4299 psa_aead_abort( &operation );
4300
Paul Elliott374a2be2021-07-16 17:53:40 +01004301 /* Test for double generating nonce. */
4302
4303 operation = psa_aead_operation_init( );
4304
4305 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4306
4307 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4308 PSA_AEAD_NONCE_MAX_SIZE,
4309 &nonce_length ) );
4310
4311 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4312 PSA_AEAD_NONCE_MAX_SIZE,
4313 &nonce_length ),
4314 PSA_ERROR_BAD_STATE );
4315
4316
4317 psa_aead_abort( &operation );
4318
4319 /* Test for generate nonce then set and vice versa */
4320
4321 operation = psa_aead_operation_init( );
4322
4323 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4324
4325 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4326 PSA_AEAD_NONCE_MAX_SIZE,
4327 &nonce_length ) );
4328
4329 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4330 PSA_ERROR_BAD_STATE );
4331
4332 psa_aead_abort( &operation );
4333
4334 /* ------------------------------------------------------- */
4335
4336 operation = psa_aead_operation_init( );
4337
4338 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4339
4340 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4341
4342 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4343 PSA_AEAD_NONCE_MAX_SIZE,
4344 &nonce_length ),
4345 PSA_ERROR_BAD_STATE );
4346
4347 psa_aead_abort( &operation );
4348
Paul Elliott7220cae2021-06-22 17:25:57 +01004349 /* Test for generating nonce in decrypt setup. */
4350
4351 operation = psa_aead_operation_init( );
4352
4353 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4354
4355 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4356 PSA_AEAD_NONCE_MAX_SIZE,
4357 &nonce_length ),
4358 PSA_ERROR_BAD_STATE );
4359
4360 psa_aead_abort( &operation );
4361
Paul Elliottc23a9a02021-06-21 18:32:46 +01004362 /* Test for setting lengths twice. */
4363
4364 operation = psa_aead_operation_init( );
4365
4366 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4367
4368 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4369
4370 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4371 input_data->len ) );
4372
4373 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4374 input_data->len ),
4375 PSA_ERROR_BAD_STATE );
4376
4377 psa_aead_abort( &operation );
4378
4379 /* Test for setting lengths after already starting data. */
4380
4381 operation = psa_aead_operation_init( );
4382
4383 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4384
4385 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4386
4387 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4388 input_data->len, output_data,
4389 output_size, &output_length ) );
4390
4391 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4392 input_data->len ),
4393 PSA_ERROR_BAD_STATE );
4394
4395 psa_aead_abort( &operation );
4396
Paul Elliott243080c2021-07-21 19:01:17 +01004397 /* Test for not sending any additional data or data after setting non zero
4398 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004399
4400 operation = psa_aead_operation_init( );
4401
4402 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4403
4404 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4405
4406 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4407 input_data->len ) );
4408
4409 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4410 finish_output_size,
4411 &output_part_length,
4412 tag_buffer, tag_length,
4413 &tag_size ),
4414 PSA_ERROR_INVALID_ARGUMENT );
4415
4416 psa_aead_abort( &operation );
4417
Paul Elliott243080c2021-07-21 19:01:17 +01004418 /* Test for not sending any additional data or data after setting non-zero
4419 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004420
4421 operation = psa_aead_operation_init( );
4422
4423 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4424
4425 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4426
4427 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4428 input_data->len ) );
4429
4430 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4431 finish_output_size,
4432 &output_part_length,
4433 tag_buffer,
4434 tag_length ),
4435 PSA_ERROR_INVALID_ARGUMENT );
4436
4437 psa_aead_abort( &operation );
4438
Paul Elliott243080c2021-07-21 19:01:17 +01004439 /* Test for not sending any additional data after setting a non-zero length
4440 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004441
4442 operation = psa_aead_operation_init( );
4443
4444 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4445
4446 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4447
4448 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4449 input_data->len ) );
4450
4451 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4452 input_data->len, output_data,
4453 output_size, &output_length ),
4454 PSA_ERROR_INVALID_ARGUMENT );
4455
4456 psa_aead_abort( &operation );
4457
Paul Elliottb0450fe2021-09-01 15:06:26 +01004458 /* Test for sending too much additional data after setting lengths. */
4459
4460 operation = psa_aead_operation_init( );
4461
4462 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4463
4464 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4465
4466 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4467
4468
4469 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4470 additional_data->len ),
4471 PSA_ERROR_INVALID_ARGUMENT );
4472
4473 psa_aead_abort( &operation );
4474
4475 /* Test for sending too much data after setting lengths. */
4476
4477 operation = psa_aead_operation_init( );
4478
4479 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4480
4481 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4482
4483 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4484
4485 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4486 input_data->len, output_data,
4487 output_size, &output_length ),
4488 PSA_ERROR_INVALID_ARGUMENT );
4489
4490 psa_aead_abort( &operation );
4491
Paul Elliottc23a9a02021-06-21 18:32:46 +01004492 /* Test sending additional data after data. */
4493
4494 operation = psa_aead_operation_init( );
4495
4496 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4497
4498 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4499
4500 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4501 input_data->len, output_data,
4502 output_size, &output_length ) );
4503
4504 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4505 additional_data->len ),
4506 PSA_ERROR_BAD_STATE );
4507
4508 psa_aead_abort( &operation );
4509
Paul Elliott534d0b42021-06-22 19:15:20 +01004510 /* Test calling finish on decryption. */
4511
4512 operation = psa_aead_operation_init( );
4513
4514 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4515
4516 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4517
4518 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4519 finish_output_size,
4520 &output_part_length,
4521 tag_buffer, tag_length,
4522 &tag_size ),
4523 PSA_ERROR_BAD_STATE );
4524
4525 psa_aead_abort( &operation );
4526
4527 /* Test calling verify on encryption. */
4528
4529 operation = psa_aead_operation_init( );
4530
4531 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4532
4533 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4534
4535 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4536 finish_output_size,
4537 &output_part_length,
4538 tag_buffer,
4539 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004540 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004541
4542 psa_aead_abort( &operation );
4543
4544
Paul Elliottc23a9a02021-06-21 18:32:46 +01004545exit:
4546 psa_destroy_key( key );
4547 psa_aead_abort( &operation );
4548 mbedtls_free( output_data );
4549 mbedtls_free( final_data );
4550 PSA_DONE( );
4551}
4552/* END_CASE */
4553
4554/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004555void signature_size( int type_arg,
4556 int bits,
4557 int alg_arg,
4558 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004559{
4560 psa_key_type_t type = type_arg;
4561 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004562 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004563
Gilles Peskinefe11b722018-12-18 00:24:04 +01004564 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004565
Gilles Peskinee59236f2018-01-27 23:32:46 +01004566exit:
4567 ;
4568}
4569/* END_CASE */
4570
4571/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004572void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4573 int alg_arg, data_t *input_data,
4574 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004575{
Ronald Cron5425a212020-08-04 14:58:35 +02004576 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004577 psa_key_type_t key_type = key_type_arg;
4578 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004579 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004580 unsigned char *signature = NULL;
4581 size_t signature_size;
4582 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004584
Gilles Peskine8817f612018-12-18 00:18:46 +01004585 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004586
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004587 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004588 psa_set_key_algorithm( &attributes, alg );
4589 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004590
Gilles Peskine049c7532019-05-15 20:22:09 +02004591 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004592 &key ) );
4593 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004594 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004595
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004596 /* Allocate a buffer which has the size advertized by the
4597 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004598 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004599 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004600 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004601 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004602 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004603
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004604 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004605 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004606 input_data->x, input_data->len,
4607 signature, signature_size,
4608 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004609 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004610 ASSERT_COMPARE( output_data->x, output_data->len,
4611 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004612
4613exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004614 /*
4615 * Key attributes may have been returned by psa_get_key_attributes()
4616 * thus reset them as required.
4617 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004618 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004619
Ronald Cron5425a212020-08-04 14:58:35 +02004620 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004621 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004622 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004623}
4624/* END_CASE */
4625
4626/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004627void sign_hash_fail( int key_type_arg, data_t *key_data,
4628 int alg_arg, data_t *input_data,
4629 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004630{
Ronald Cron5425a212020-08-04 14:58:35 +02004631 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004632 psa_key_type_t key_type = key_type_arg;
4633 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004634 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004635 psa_status_t actual_status;
4636 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004637 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004638 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004639 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004640
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004641 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004642
Gilles Peskine8817f612018-12-18 00:18:46 +01004643 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004644
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004645 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004646 psa_set_key_algorithm( &attributes, alg );
4647 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004648
Gilles Peskine049c7532019-05-15 20:22:09 +02004649 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004650 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004651
Ronald Cron5425a212020-08-04 14:58:35 +02004652 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004653 input_data->x, input_data->len,
4654 signature, signature_size,
4655 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004656 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004657 /* The value of *signature_length is unspecified on error, but
4658 * whatever it is, it should be less than signature_size, so that
4659 * if the caller tries to read *signature_length bytes without
4660 * checking the error code then they don't overflow a buffer. */
4661 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004662
4663exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004664 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004665 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004666 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004667 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004668}
4669/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004670
4671/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004672void sign_verify_hash( int key_type_arg, data_t *key_data,
4673 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004674{
Ronald Cron5425a212020-08-04 14:58:35 +02004675 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004676 psa_key_type_t key_type = key_type_arg;
4677 psa_algorithm_t alg = alg_arg;
4678 size_t key_bits;
4679 unsigned char *signature = NULL;
4680 size_t signature_size;
4681 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004683
Gilles Peskine8817f612018-12-18 00:18:46 +01004684 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004685
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004686 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004687 psa_set_key_algorithm( &attributes, alg );
4688 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004689
Gilles Peskine049c7532019-05-15 20:22:09 +02004690 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004691 &key ) );
4692 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004693 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004694
4695 /* Allocate a buffer which has the size advertized by the
4696 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004697 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004698 key_bits, alg );
4699 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004700 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004701 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004702
4703 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004704 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004705 input_data->x, input_data->len,
4706 signature, signature_size,
4707 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004708 /* Check that the signature length looks sensible. */
4709 TEST_ASSERT( signature_length <= signature_size );
4710 TEST_ASSERT( signature_length > 0 );
4711
4712 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004713 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004714 input_data->x, input_data->len,
4715 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004716
4717 if( input_data->len != 0 )
4718 {
4719 /* Flip a bit in the input and verify that the signature is now
4720 * detected as invalid. Flip a bit at the beginning, not at the end,
4721 * because ECDSA may ignore the last few bits of the input. */
4722 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004723 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004724 input_data->x, input_data->len,
4725 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004726 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004727 }
4728
4729exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004730 /*
4731 * Key attributes may have been returned by psa_get_key_attributes()
4732 * thus reset them as required.
4733 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004734 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004735
Ronald Cron5425a212020-08-04 14:58:35 +02004736 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004737 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004738 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004739}
4740/* END_CASE */
4741
4742/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004743void verify_hash( int key_type_arg, data_t *key_data,
4744 int alg_arg, data_t *hash_data,
4745 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004746{
Ronald Cron5425a212020-08-04 14:58:35 +02004747 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004748 psa_key_type_t key_type = key_type_arg;
4749 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004750 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004751
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004752 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004753
Gilles Peskine8817f612018-12-18 00:18:46 +01004754 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004755
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004756 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004757 psa_set_key_algorithm( &attributes, alg );
4758 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004759
Gilles Peskine049c7532019-05-15 20:22:09 +02004760 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004761 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004762
Ronald Cron5425a212020-08-04 14:58:35 +02004763 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004764 hash_data->x, hash_data->len,
4765 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004766
itayzafrir5c753392018-05-08 11:18:38 +03004767exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004768 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004769 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004770 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004771}
4772/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004773
4774/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004775void verify_hash_fail( int key_type_arg, data_t *key_data,
4776 int alg_arg, data_t *hash_data,
4777 data_t *signature_data,
4778 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004779{
Ronald Cron5425a212020-08-04 14:58:35 +02004780 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004781 psa_key_type_t key_type = key_type_arg;
4782 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004783 psa_status_t actual_status;
4784 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004786
Gilles Peskine8817f612018-12-18 00:18:46 +01004787 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004788
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004789 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004790 psa_set_key_algorithm( &attributes, alg );
4791 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004792
Gilles Peskine049c7532019-05-15 20:22:09 +02004793 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004794 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004795
Ronald Cron5425a212020-08-04 14:58:35 +02004796 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004797 hash_data->x, hash_data->len,
4798 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004799 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004800
4801exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004802 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004803 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004804 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004805}
4806/* END_CASE */
4807
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004808/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004809void sign_message_deterministic( int key_type_arg,
4810 data_t *key_data,
4811 int alg_arg,
4812 data_t *input_data,
4813 data_t *output_data )
4814{
4815 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4816 psa_key_type_t key_type = key_type_arg;
4817 psa_algorithm_t alg = alg_arg;
4818 size_t key_bits;
4819 unsigned char *signature = NULL;
4820 size_t signature_size;
4821 size_t signature_length = 0xdeadbeef;
4822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4823
4824 PSA_ASSERT( psa_crypto_init( ) );
4825
4826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4827 psa_set_key_algorithm( &attributes, alg );
4828 psa_set_key_type( &attributes, key_type );
4829
4830 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4831 &key ) );
4832 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4833 key_bits = psa_get_key_bits( &attributes );
4834
4835 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4836 TEST_ASSERT( signature_size != 0 );
4837 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4838 ASSERT_ALLOC( signature, signature_size );
4839
4840 PSA_ASSERT( psa_sign_message( key, alg,
4841 input_data->x, input_data->len,
4842 signature, signature_size,
4843 &signature_length ) );
4844
4845 ASSERT_COMPARE( output_data->x, output_data->len,
4846 signature, signature_length );
4847
4848exit:
4849 psa_reset_key_attributes( &attributes );
4850
4851 psa_destroy_key( key );
4852 mbedtls_free( signature );
4853 PSA_DONE( );
4854
4855}
4856/* END_CASE */
4857
4858/* BEGIN_CASE */
4859void sign_message_fail( int key_type_arg,
4860 data_t *key_data,
4861 int alg_arg,
4862 data_t *input_data,
4863 int signature_size_arg,
4864 int expected_status_arg )
4865{
4866 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4867 psa_key_type_t key_type = key_type_arg;
4868 psa_algorithm_t alg = alg_arg;
4869 size_t signature_size = signature_size_arg;
4870 psa_status_t actual_status;
4871 psa_status_t expected_status = expected_status_arg;
4872 unsigned char *signature = NULL;
4873 size_t signature_length = 0xdeadbeef;
4874 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4875
4876 ASSERT_ALLOC( signature, signature_size );
4877
4878 PSA_ASSERT( psa_crypto_init( ) );
4879
4880 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4881 psa_set_key_algorithm( &attributes, alg );
4882 psa_set_key_type( &attributes, key_type );
4883
4884 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4885 &key ) );
4886
4887 actual_status = psa_sign_message( key, alg,
4888 input_data->x, input_data->len,
4889 signature, signature_size,
4890 &signature_length );
4891 TEST_EQUAL( actual_status, expected_status );
4892 /* The value of *signature_length is unspecified on error, but
4893 * whatever it is, it should be less than signature_size, so that
4894 * if the caller tries to read *signature_length bytes without
4895 * checking the error code then they don't overflow a buffer. */
4896 TEST_ASSERT( signature_length <= signature_size );
4897
4898exit:
4899 psa_reset_key_attributes( &attributes );
4900 psa_destroy_key( key );
4901 mbedtls_free( signature );
4902 PSA_DONE( );
4903}
4904/* END_CASE */
4905
4906/* BEGIN_CASE */
4907void sign_verify_message( int key_type_arg,
4908 data_t *key_data,
4909 int alg_arg,
4910 data_t *input_data )
4911{
4912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4913 psa_key_type_t key_type = key_type_arg;
4914 psa_algorithm_t alg = alg_arg;
4915 size_t key_bits;
4916 unsigned char *signature = NULL;
4917 size_t signature_size;
4918 size_t signature_length = 0xdeadbeef;
4919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4920
4921 PSA_ASSERT( psa_crypto_init( ) );
4922
4923 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4924 PSA_KEY_USAGE_VERIFY_MESSAGE );
4925 psa_set_key_algorithm( &attributes, alg );
4926 psa_set_key_type( &attributes, key_type );
4927
4928 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4929 &key ) );
4930 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4931 key_bits = psa_get_key_bits( &attributes );
4932
4933 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4934 TEST_ASSERT( signature_size != 0 );
4935 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4936 ASSERT_ALLOC( signature, signature_size );
4937
4938 PSA_ASSERT( psa_sign_message( key, alg,
4939 input_data->x, input_data->len,
4940 signature, signature_size,
4941 &signature_length ) );
4942 TEST_ASSERT( signature_length <= signature_size );
4943 TEST_ASSERT( signature_length > 0 );
4944
4945 PSA_ASSERT( psa_verify_message( key, alg,
4946 input_data->x, input_data->len,
4947 signature, signature_length ) );
4948
4949 if( input_data->len != 0 )
4950 {
4951 /* Flip a bit in the input and verify that the signature is now
4952 * detected as invalid. Flip a bit at the beginning, not at the end,
4953 * because ECDSA may ignore the last few bits of the input. */
4954 input_data->x[0] ^= 1;
4955 TEST_EQUAL( psa_verify_message( key, alg,
4956 input_data->x, input_data->len,
4957 signature, signature_length ),
4958 PSA_ERROR_INVALID_SIGNATURE );
4959 }
4960
4961exit:
4962 psa_reset_key_attributes( &attributes );
4963
4964 psa_destroy_key( key );
4965 mbedtls_free( signature );
4966 PSA_DONE( );
4967}
4968/* END_CASE */
4969
4970/* BEGIN_CASE */
4971void verify_message( int key_type_arg,
4972 data_t *key_data,
4973 int alg_arg,
4974 data_t *input_data,
4975 data_t *signature_data )
4976{
4977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4978 psa_key_type_t key_type = key_type_arg;
4979 psa_algorithm_t alg = alg_arg;
4980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4981
4982 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4983
4984 PSA_ASSERT( psa_crypto_init( ) );
4985
4986 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4987 psa_set_key_algorithm( &attributes, alg );
4988 psa_set_key_type( &attributes, key_type );
4989
4990 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4991 &key ) );
4992
4993 PSA_ASSERT( psa_verify_message( key, alg,
4994 input_data->x, input_data->len,
4995 signature_data->x, signature_data->len ) );
4996
4997exit:
4998 psa_reset_key_attributes( &attributes );
4999 psa_destroy_key( key );
5000 PSA_DONE( );
5001}
5002/* END_CASE */
5003
5004/* BEGIN_CASE */
5005void verify_message_fail( int key_type_arg,
5006 data_t *key_data,
5007 int alg_arg,
5008 data_t *hash_data,
5009 data_t *signature_data,
5010 int expected_status_arg )
5011{
5012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5013 psa_key_type_t key_type = key_type_arg;
5014 psa_algorithm_t alg = alg_arg;
5015 psa_status_t actual_status;
5016 psa_status_t expected_status = expected_status_arg;
5017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5018
5019 PSA_ASSERT( psa_crypto_init( ) );
5020
5021 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5022 psa_set_key_algorithm( &attributes, alg );
5023 psa_set_key_type( &attributes, key_type );
5024
5025 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5026 &key ) );
5027
5028 actual_status = psa_verify_message( key, alg,
5029 hash_data->x, hash_data->len,
5030 signature_data->x,
5031 signature_data->len );
5032 TEST_EQUAL( actual_status, expected_status );
5033
5034exit:
5035 psa_reset_key_attributes( &attributes );
5036 psa_destroy_key( key );
5037 PSA_DONE( );
5038}
5039/* END_CASE */
5040
5041/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005042void asymmetric_encrypt( int key_type_arg,
5043 data_t *key_data,
5044 int alg_arg,
5045 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005046 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005047 int expected_output_length_arg,
5048 int expected_status_arg )
5049{
Ronald Cron5425a212020-08-04 14:58:35 +02005050 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005051 psa_key_type_t key_type = key_type_arg;
5052 psa_algorithm_t alg = alg_arg;
5053 size_t expected_output_length = expected_output_length_arg;
5054 size_t key_bits;
5055 unsigned char *output = NULL;
5056 size_t output_size;
5057 size_t output_length = ~0;
5058 psa_status_t actual_status;
5059 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005061
Gilles Peskine8817f612018-12-18 00:18:46 +01005062 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005063
Gilles Peskine656896e2018-06-29 19:12:28 +02005064 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005065 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5066 psa_set_key_algorithm( &attributes, alg );
5067 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005068 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005069 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005070
5071 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005072 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005073 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005074
Gilles Peskine656896e2018-06-29 19:12:28 +02005075 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005076 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005077 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005078
5079 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005080 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005081 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005082 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005083 output, output_size,
5084 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005085 TEST_EQUAL( actual_status, expected_status );
5086 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005087
Gilles Peskine68428122018-06-30 18:42:41 +02005088 /* If the label is empty, the test framework puts a non-null pointer
5089 * in label->x. Test that a null pointer works as well. */
5090 if( label->len == 0 )
5091 {
5092 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005093 if( output_size != 0 )
5094 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005095 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005096 input_data->x, input_data->len,
5097 NULL, label->len,
5098 output, output_size,
5099 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005100 TEST_EQUAL( actual_status, expected_status );
5101 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005102 }
5103
Gilles Peskine656896e2018-06-29 19:12:28 +02005104exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005105 /*
5106 * Key attributes may have been returned by psa_get_key_attributes()
5107 * thus reset them as required.
5108 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005109 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005110
Ronald Cron5425a212020-08-04 14:58:35 +02005111 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005112 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005113 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005114}
5115/* END_CASE */
5116
5117/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005118void asymmetric_encrypt_decrypt( int key_type_arg,
5119 data_t *key_data,
5120 int alg_arg,
5121 data_t *input_data,
5122 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005123{
Ronald Cron5425a212020-08-04 14:58:35 +02005124 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005125 psa_key_type_t key_type = key_type_arg;
5126 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005127 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005128 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005129 size_t output_size;
5130 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005131 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005132 size_t output2_size;
5133 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005135
Gilles Peskine8817f612018-12-18 00:18:46 +01005136 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005137
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5139 psa_set_key_algorithm( &attributes, alg );
5140 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005141
Gilles Peskine049c7532019-05-15 20:22:09 +02005142 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005143 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005144
5145 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005146 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005147 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005148
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005149 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005150 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005151 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005152
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005153 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005154 TEST_ASSERT( output2_size <=
5155 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5156 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005157 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005158
Gilles Peskineeebd7382018-06-08 18:11:54 +02005159 /* We test encryption by checking that encrypt-then-decrypt gives back
5160 * the original plaintext because of the non-optional random
5161 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005162 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005163 input_data->x, input_data->len,
5164 label->x, label->len,
5165 output, output_size,
5166 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005167 /* We don't know what ciphertext length to expect, but check that
5168 * it looks sensible. */
5169 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005170
Ronald Cron5425a212020-08-04 14:58:35 +02005171 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005172 output, output_length,
5173 label->x, label->len,
5174 output2, output2_size,
5175 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005176 ASSERT_COMPARE( input_data->x, input_data->len,
5177 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005178
5179exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005180 /*
5181 * Key attributes may have been returned by psa_get_key_attributes()
5182 * thus reset them as required.
5183 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005184 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005185
Ronald Cron5425a212020-08-04 14:58:35 +02005186 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005187 mbedtls_free( output );
5188 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005189 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005190}
5191/* END_CASE */
5192
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005193/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005194void asymmetric_decrypt( int key_type_arg,
5195 data_t *key_data,
5196 int alg_arg,
5197 data_t *input_data,
5198 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005199 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005200{
Ronald Cron5425a212020-08-04 14:58:35 +02005201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005202 psa_key_type_t key_type = key_type_arg;
5203 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005204 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005205 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005206 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005207 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005208 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005209
Gilles Peskine8817f612018-12-18 00:18:46 +01005210 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005211
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5213 psa_set_key_algorithm( &attributes, alg );
5214 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005215
Gilles Peskine049c7532019-05-15 20:22:09 +02005216 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005217 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005218
gabor-mezei-armceface22021-01-21 12:26:17 +01005219 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5220 key_bits = psa_get_key_bits( &attributes );
5221
5222 /* Determine the maximum ciphertext length */
5223 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5224 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5225 ASSERT_ALLOC( output, output_size );
5226
Ronald Cron5425a212020-08-04 14:58:35 +02005227 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005228 input_data->x, input_data->len,
5229 label->x, label->len,
5230 output,
5231 output_size,
5232 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005233 ASSERT_COMPARE( expected_data->x, expected_data->len,
5234 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005235
Gilles Peskine68428122018-06-30 18:42:41 +02005236 /* If the label is empty, the test framework puts a non-null pointer
5237 * in label->x. Test that a null pointer works as well. */
5238 if( label->len == 0 )
5239 {
5240 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005241 if( output_size != 0 )
5242 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005243 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005244 input_data->x, input_data->len,
5245 NULL, label->len,
5246 output,
5247 output_size,
5248 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005249 ASSERT_COMPARE( expected_data->x, expected_data->len,
5250 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005251 }
5252
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005253exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005254 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005255 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005256 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005257 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005258}
5259/* END_CASE */
5260
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005261/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005262void asymmetric_decrypt_fail( int key_type_arg,
5263 data_t *key_data,
5264 int alg_arg,
5265 data_t *input_data,
5266 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005267 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005268 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005269{
Ronald Cron5425a212020-08-04 14:58:35 +02005270 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005271 psa_key_type_t key_type = key_type_arg;
5272 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005273 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005274 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005275 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005276 psa_status_t actual_status;
5277 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005278 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005279
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005280 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005281
Gilles Peskine8817f612018-12-18 00:18:46 +01005282 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005283
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005284 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5285 psa_set_key_algorithm( &attributes, alg );
5286 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005287
Gilles Peskine049c7532019-05-15 20:22:09 +02005288 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005289 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005290
Ronald Cron5425a212020-08-04 14:58:35 +02005291 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005292 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005293 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005294 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005295 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005296 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005297 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005298
Gilles Peskine68428122018-06-30 18:42:41 +02005299 /* If the label is empty, the test framework puts a non-null pointer
5300 * in label->x. Test that a null pointer works as well. */
5301 if( label->len == 0 )
5302 {
5303 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005304 if( output_size != 0 )
5305 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005306 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005307 input_data->x, input_data->len,
5308 NULL, label->len,
5309 output, output_size,
5310 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005311 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005312 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005313 }
5314
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005315exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005316 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005317 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005318 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005319 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005320}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005321/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005322
5323/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005324void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005325{
5326 /* Test each valid way of initializing the object, except for `= {0}`, as
5327 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5328 * though it's OK by the C standard. We could test for this, but we'd need
5329 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005330 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005331 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5332 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5333 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005334
5335 memset( &zero, 0, sizeof( zero ) );
5336
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005338 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005339 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005340 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005341 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005342 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005343 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005344
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005345 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005346 PSA_ASSERT( psa_key_derivation_abort(&func) );
5347 PSA_ASSERT( psa_key_derivation_abort(&init) );
5348 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005349}
5350/* END_CASE */
5351
Janos Follath16de4a42019-06-13 16:32:24 +01005352/* BEGIN_CASE */
5353void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005354{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005355 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005356 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005357 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005358
Gilles Peskine8817f612018-12-18 00:18:46 +01005359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005360
Janos Follath16de4a42019-06-13 16:32:24 +01005361 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005362 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005363
5364exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005365 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005366 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005367}
5368/* END_CASE */
5369
Janos Follathaf3c2a02019-06-12 12:34:34 +01005370/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005371void derive_set_capacity( int alg_arg, int capacity_arg,
5372 int expected_status_arg )
5373{
5374 psa_algorithm_t alg = alg_arg;
5375 size_t capacity = capacity_arg;
5376 psa_status_t expected_status = expected_status_arg;
5377 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5378
5379 PSA_ASSERT( psa_crypto_init( ) );
5380
5381 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5382
5383 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5384 expected_status );
5385
5386exit:
5387 psa_key_derivation_abort( &operation );
5388 PSA_DONE( );
5389}
5390/* END_CASE */
5391
5392/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005393void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005394 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005395 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005396 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005397 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005398 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005399 int expected_status_arg3,
5400 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005401{
5402 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005403 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5404 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005405 psa_status_t expected_statuses[] = {expected_status_arg1,
5406 expected_status_arg2,
5407 expected_status_arg3};
5408 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005409 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5410 MBEDTLS_SVC_KEY_ID_INIT,
5411 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005412 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5413 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5414 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005415 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005416 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005417 psa_status_t expected_output_status = expected_output_status_arg;
5418 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005419
5420 PSA_ASSERT( psa_crypto_init( ) );
5421
5422 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5423 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005424
5425 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5426
5427 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5428 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005429 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005430 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005431 psa_set_key_type( &attributes, key_types[i] );
5432 PSA_ASSERT( psa_import_key( &attributes,
5433 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005434 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005435 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5436 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5437 {
5438 // When taking a private key as secret input, use key agreement
5439 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005440 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5441 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005442 expected_statuses[i] );
5443 }
5444 else
5445 {
5446 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005447 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005448 expected_statuses[i] );
5449 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005450 }
5451 else
5452 {
5453 TEST_EQUAL( psa_key_derivation_input_bytes(
5454 &operation, steps[i],
5455 inputs[i]->x, inputs[i]->len ),
5456 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005457 }
5458 }
5459
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005460 if( output_key_type != PSA_KEY_TYPE_NONE )
5461 {
5462 psa_reset_key_attributes( &attributes );
5463 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5464 psa_set_key_bits( &attributes, 8 );
5465 actual_output_status =
5466 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005467 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005468 }
5469 else
5470 {
5471 uint8_t buffer[1];
5472 actual_output_status =
5473 psa_key_derivation_output_bytes( &operation,
5474 buffer, sizeof( buffer ) );
5475 }
5476 TEST_EQUAL( actual_output_status, expected_output_status );
5477
Janos Follathaf3c2a02019-06-12 12:34:34 +01005478exit:
5479 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005480 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5481 psa_destroy_key( keys[i] );
5482 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005483 PSA_DONE( );
5484}
5485/* END_CASE */
5486
Janos Follathd958bb72019-07-03 15:02:16 +01005487/* BEGIN_CASE */
5488void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005489{
Janos Follathd958bb72019-07-03 15:02:16 +01005490 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005491 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005492 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005493 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005494 unsigned char input1[] = "Input 1";
5495 size_t input1_length = sizeof( input1 );
5496 unsigned char input2[] = "Input 2";
5497 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005498 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005499 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005500 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5501 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5502 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005503 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005504
Gilles Peskine8817f612018-12-18 00:18:46 +01005505 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005506
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005507 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5508 psa_set_key_algorithm( &attributes, alg );
5509 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005510
Gilles Peskine73676cb2019-05-15 20:15:10 +02005511 PSA_ASSERT( psa_import_key( &attributes,
5512 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005513 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005514
5515 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005516 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5517 input1, input1_length,
5518 input2, input2_length,
5519 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005520 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005521
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005523 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005524 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005525
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005526 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005527
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005528 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005529 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005530
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005531exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005532 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005533 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005534 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005535}
5536/* END_CASE */
5537
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005538/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005539void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005540{
5541 uint8_t output_buffer[16];
5542 size_t buffer_size = 16;
5543 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005544 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005545
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005546 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5547 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005548 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005549
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005550 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005551 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005552
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005553 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005554
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005555 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5556 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005557 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005558
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005559 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005560 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005561
5562exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005563 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005564}
5565/* END_CASE */
5566
5567/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005568void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005569 int step1_arg, data_t *input1,
5570 int step2_arg, data_t *input2,
5571 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005572 int requested_capacity_arg,
5573 data_t *expected_output1,
5574 data_t *expected_output2 )
5575{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005576 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005577 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5578 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005579 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5580 MBEDTLS_SVC_KEY_ID_INIT,
5581 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005582 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005583 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005584 uint8_t *expected_outputs[2] =
5585 {expected_output1->x, expected_output2->x};
5586 size_t output_sizes[2] =
5587 {expected_output1->len, expected_output2->len};
5588 size_t output_buffer_size = 0;
5589 uint8_t *output_buffer = NULL;
5590 size_t expected_capacity;
5591 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005592 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005593 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005594 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005595
5596 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5597 {
5598 if( output_sizes[i] > output_buffer_size )
5599 output_buffer_size = output_sizes[i];
5600 if( output_sizes[i] == 0 )
5601 expected_outputs[i] = NULL;
5602 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005603 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005604 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005605
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005606 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5607 psa_set_key_algorithm( &attributes, alg );
5608 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005609
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005610 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005611 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5612 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5613 requested_capacity ) );
5614 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005615 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005616 switch( steps[i] )
5617 {
5618 case 0:
5619 break;
5620 case PSA_KEY_DERIVATION_INPUT_SECRET:
5621 PSA_ASSERT( psa_import_key( &attributes,
5622 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005623 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005624
5625 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5626 {
5627 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5628 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5629 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5630 }
5631
Gilles Peskine1468da72019-05-29 17:35:49 +02005632 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005633 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005634 break;
5635 default:
5636 PSA_ASSERT( psa_key_derivation_input_bytes(
5637 &operation, steps[i],
5638 inputs[i]->x, inputs[i]->len ) );
5639 break;
5640 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005641 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005642
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005643 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005644 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005645 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005646 expected_capacity = requested_capacity;
5647
5648 /* Expansion phase. */
5649 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5650 {
5651 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005652 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005653 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005654 if( expected_capacity == 0 && output_sizes[i] == 0 )
5655 {
5656 /* Reading 0 bytes when 0 bytes are available can go either way. */
5657 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005658 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005659 continue;
5660 }
5661 else if( expected_capacity == 0 ||
5662 output_sizes[i] > expected_capacity )
5663 {
5664 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005665 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005666 expected_capacity = 0;
5667 continue;
5668 }
5669 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005670 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005671 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005672 ASSERT_COMPARE( output_buffer, output_sizes[i],
5673 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005674 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005675 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005676 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005677 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005678 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005679 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005680 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005681
5682exit:
5683 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005684 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005685 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5686 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005687 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005688}
5689/* END_CASE */
5690
5691/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005692void derive_full( int alg_arg,
5693 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005694 data_t *input1,
5695 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005696 int requested_capacity_arg )
5697{
Ronald Cron5425a212020-08-04 14:58:35 +02005698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005699 psa_algorithm_t alg = alg_arg;
5700 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005701 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005702 unsigned char output_buffer[16];
5703 size_t expected_capacity = requested_capacity;
5704 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005706
Gilles Peskine8817f612018-12-18 00:18:46 +01005707 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005708
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005709 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5710 psa_set_key_algorithm( &attributes, alg );
5711 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005712
Gilles Peskine049c7532019-05-15 20:22:09 +02005713 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005714 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005715
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005716 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5717 input1->x, input1->len,
5718 input2->x, input2->len,
5719 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005720 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005721
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005722 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005723 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005724 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005725
5726 /* Expansion phase. */
5727 while( current_capacity > 0 )
5728 {
5729 size_t read_size = sizeof( output_buffer );
5730 if( read_size > current_capacity )
5731 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005732 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005733 output_buffer,
5734 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005735 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005736 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005737 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005738 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005739 }
5740
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005741 /* Check that the operation refuses to go over capacity. */
5742 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005743 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005744
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005746
5747exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005748 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005749 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005750 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005751}
5752/* END_CASE */
5753
Janos Follathe60c9052019-07-03 13:51:30 +01005754/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005755void derive_key_exercise( int alg_arg,
5756 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005757 data_t *input1,
5758 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005759 int derived_type_arg,
5760 int derived_bits_arg,
5761 int derived_usage_arg,
5762 int derived_alg_arg )
5763{
Ronald Cron5425a212020-08-04 14:58:35 +02005764 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5765 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005766 psa_algorithm_t alg = alg_arg;
5767 psa_key_type_t derived_type = derived_type_arg;
5768 size_t derived_bits = derived_bits_arg;
5769 psa_key_usage_t derived_usage = derived_usage_arg;
5770 psa_algorithm_t derived_alg = derived_alg_arg;
5771 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005772 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005774 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005775
Gilles Peskine8817f612018-12-18 00:18:46 +01005776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005777
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5779 psa_set_key_algorithm( &attributes, alg );
5780 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005781 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005782 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005783
5784 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005785 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5786 input1->x, input1->len,
5787 input2->x, input2->len,
5788 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005789 goto exit;
5790
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005791 psa_set_key_usage_flags( &attributes, derived_usage );
5792 psa_set_key_algorithm( &attributes, derived_alg );
5793 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005794 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005795 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005796 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005797
5798 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005799 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005800 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5801 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005802
5803 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005804 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005805 goto exit;
5806
5807exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005808 /*
5809 * Key attributes may have been returned by psa_get_key_attributes()
5810 * thus reset them as required.
5811 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005812 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005813
5814 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005815 psa_destroy_key( base_key );
5816 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005817 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005818}
5819/* END_CASE */
5820
Janos Follath42fd8882019-07-03 14:17:09 +01005821/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005822void derive_key_export( int alg_arg,
5823 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005824 data_t *input1,
5825 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005826 int bytes1_arg,
5827 int bytes2_arg )
5828{
Ronald Cron5425a212020-08-04 14:58:35 +02005829 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5830 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005831 psa_algorithm_t alg = alg_arg;
5832 size_t bytes1 = bytes1_arg;
5833 size_t bytes2 = bytes2_arg;
5834 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005835 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005836 uint8_t *output_buffer = NULL;
5837 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005838 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5839 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005840 size_t length;
5841
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005842 ASSERT_ALLOC( output_buffer, capacity );
5843 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005844 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005845
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005846 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5847 psa_set_key_algorithm( &base_attributes, alg );
5848 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005849 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005850 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005851
5852 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005853 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5854 input1->x, input1->len,
5855 input2->x, input2->len,
5856 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005857 goto exit;
5858
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005859 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005860 output_buffer,
5861 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005862 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005863
5864 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005865 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5866 input1->x, input1->len,
5867 input2->x, input2->len,
5868 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005869 goto exit;
5870
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005871 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5872 psa_set_key_algorithm( &derived_attributes, 0 );
5873 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005874 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005875 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005876 &derived_key ) );
5877 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005878 export_buffer, bytes1,
5879 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005880 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005881 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005882 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005883 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005884 &derived_key ) );
5885 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005886 export_buffer + bytes1, bytes2,
5887 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005888 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005889
5890 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005891 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5892 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005893
5894exit:
5895 mbedtls_free( output_buffer );
5896 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005897 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005898 psa_destroy_key( base_key );
5899 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005900 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005901}
5902/* END_CASE */
5903
5904/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005905void derive_key( int alg_arg,
5906 data_t *key_data, data_t *input1, data_t *input2,
5907 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005908 int expected_status_arg,
5909 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005910{
Ronald Cron5425a212020-08-04 14:58:35 +02005911 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5912 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005913 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005914 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005915 size_t bits = bits_arg;
5916 psa_status_t expected_status = expected_status_arg;
5917 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5918 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5919 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5920
5921 PSA_ASSERT( psa_crypto_init( ) );
5922
5923 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5924 psa_set_key_algorithm( &base_attributes, alg );
5925 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5926 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005927 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005928
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005929 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5930 input1->x, input1->len,
5931 input2->x, input2->len,
5932 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005933 goto exit;
5934
5935 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5936 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005937 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005938 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005939
5940 psa_status_t status =
5941 psa_key_derivation_output_key( &derived_attributes,
5942 &operation,
5943 &derived_key );
5944 if( is_large_output > 0 )
5945 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5946 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005947
5948exit:
5949 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005950 psa_destroy_key( base_key );
5951 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005952 PSA_DONE( );
5953}
5954/* END_CASE */
5955
5956/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005957void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005958 int our_key_type_arg, int our_key_alg_arg,
5959 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005960 int expected_status_arg )
5961{
Ronald Cron5425a212020-08-04 14:58:35 +02005962 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005963 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005964 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005965 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005966 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005967 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005968 psa_status_t expected_status = expected_status_arg;
5969 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005970
Gilles Peskine8817f612018-12-18 00:18:46 +01005971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005972
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005973 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005974 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005975 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005976 PSA_ASSERT( psa_import_key( &attributes,
5977 our_key_data->x, our_key_data->len,
5978 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005979
Gilles Peskine77f40d82019-04-11 21:27:06 +02005980 /* The tests currently include inputs that should fail at either step.
5981 * Test cases that fail at the setup step should be changed to call
5982 * key_derivation_setup instead, and this function should be renamed
5983 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005984 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005985 if( status == PSA_SUCCESS )
5986 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005987 TEST_EQUAL( psa_key_derivation_key_agreement(
5988 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5989 our_key,
5990 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005991 expected_status );
5992 }
5993 else
5994 {
5995 TEST_ASSERT( status == expected_status );
5996 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005997
5998exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005999 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006000 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006001 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006002}
6003/* END_CASE */
6004
6005/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006006void raw_key_agreement( int alg_arg,
6007 int our_key_type_arg, data_t *our_key_data,
6008 data_t *peer_key_data,
6009 data_t *expected_output )
6010{
Ronald Cron5425a212020-08-04 14:58:35 +02006011 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006012 psa_algorithm_t alg = alg_arg;
6013 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006015 unsigned char *output = NULL;
6016 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006017 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006018
6019 ASSERT_ALLOC( output, expected_output->len );
6020 PSA_ASSERT( psa_crypto_init( ) );
6021
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006022 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6023 psa_set_key_algorithm( &attributes, alg );
6024 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006025 PSA_ASSERT( psa_import_key( &attributes,
6026 our_key_data->x, our_key_data->len,
6027 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006028
gabor-mezei-armceface22021-01-21 12:26:17 +01006029 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6030 key_bits = psa_get_key_bits( &attributes );
6031
Gilles Peskinebe697d82019-05-16 18:00:41 +02006032 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6033 peer_key_data->x, peer_key_data->len,
6034 output, expected_output->len,
6035 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006036 ASSERT_COMPARE( output, output_length,
6037 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006038 TEST_ASSERT( output_length <=
6039 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6040 TEST_ASSERT( output_length <=
6041 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006042
6043exit:
6044 mbedtls_free( output );
6045 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006046 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006047}
6048/* END_CASE */
6049
6050/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006051void key_agreement_capacity( int alg_arg,
6052 int our_key_type_arg, data_t *our_key_data,
6053 data_t *peer_key_data,
6054 int expected_capacity_arg )
6055{
Ronald Cron5425a212020-08-04 14:58:35 +02006056 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006057 psa_algorithm_t alg = alg_arg;
6058 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006059 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006061 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006062 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006063
Gilles Peskine8817f612018-12-18 00:18:46 +01006064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006065
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006066 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6067 psa_set_key_algorithm( &attributes, alg );
6068 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006069 PSA_ASSERT( psa_import_key( &attributes,
6070 our_key_data->x, our_key_data->len,
6071 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006072
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006073 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006074 PSA_ASSERT( psa_key_derivation_key_agreement(
6075 &operation,
6076 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6077 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006078 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6079 {
6080 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006081 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006082 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006083 NULL, 0 ) );
6084 }
Gilles Peskine59685592018-09-18 12:11:34 +02006085
Gilles Peskinebf491972018-10-25 22:36:12 +02006086 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006087 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006088 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006089 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006090
Gilles Peskinebf491972018-10-25 22:36:12 +02006091 /* Test the actual capacity by reading the output. */
6092 while( actual_capacity > sizeof( output ) )
6093 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006094 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006095 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006096 actual_capacity -= sizeof( output );
6097 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006098 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006099 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006100 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006101 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006102
Gilles Peskine59685592018-09-18 12:11:34 +02006103exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006104 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006105 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006106 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006107}
6108/* END_CASE */
6109
6110/* BEGIN_CASE */
6111void key_agreement_output( int alg_arg,
6112 int our_key_type_arg, data_t *our_key_data,
6113 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006114 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006115{
Ronald Cron5425a212020-08-04 14:58:35 +02006116 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006117 psa_algorithm_t alg = alg_arg;
6118 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006119 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006121 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006122
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006123 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6124 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006125
Gilles Peskine8817f612018-12-18 00:18:46 +01006126 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006127
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006128 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6129 psa_set_key_algorithm( &attributes, alg );
6130 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006131 PSA_ASSERT( psa_import_key( &attributes,
6132 our_key_data->x, our_key_data->len,
6133 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006134
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006135 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006136 PSA_ASSERT( psa_key_derivation_key_agreement(
6137 &operation,
6138 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6139 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006140 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6141 {
6142 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006143 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006144 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006145 NULL, 0 ) );
6146 }
Gilles Peskine59685592018-09-18 12:11:34 +02006147
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006148 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006149 actual_output,
6150 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006151 ASSERT_COMPARE( actual_output, expected_output1->len,
6152 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006153 if( expected_output2->len != 0 )
6154 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006155 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006156 actual_output,
6157 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006158 ASSERT_COMPARE( actual_output, expected_output2->len,
6159 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006160 }
Gilles Peskine59685592018-09-18 12:11:34 +02006161
6162exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006163 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006164 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006165 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006166 mbedtls_free( actual_output );
6167}
6168/* END_CASE */
6169
6170/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006171void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006172{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006173 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006174 unsigned char *output = NULL;
6175 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006176 size_t i;
6177 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006178
Simon Butcher49f8e312020-03-03 15:51:50 +00006179 TEST_ASSERT( bytes_arg >= 0 );
6180
Gilles Peskine91892022021-02-08 19:50:26 +01006181 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006182 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006183
Gilles Peskine8817f612018-12-18 00:18:46 +01006184 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006185
Gilles Peskinea50d7392018-06-21 10:22:13 +02006186 /* Run several times, to ensure that every output byte will be
6187 * nonzero at least once with overwhelming probability
6188 * (2^(-8*number_of_runs)). */
6189 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006190 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006191 if( bytes != 0 )
6192 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006193 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006194
Gilles Peskinea50d7392018-06-21 10:22:13 +02006195 for( i = 0; i < bytes; i++ )
6196 {
6197 if( output[i] != 0 )
6198 ++changed[i];
6199 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006200 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006201
6202 /* Check that every byte was changed to nonzero at least once. This
6203 * validates that psa_generate_random is overwriting every byte of
6204 * the output buffer. */
6205 for( i = 0; i < bytes; i++ )
6206 {
6207 TEST_ASSERT( changed[i] != 0 );
6208 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006209
6210exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006211 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006212 mbedtls_free( output );
6213 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006214}
6215/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006216
6217/* BEGIN_CASE */
6218void generate_key( int type_arg,
6219 int bits_arg,
6220 int usage_arg,
6221 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006222 int expected_status_arg,
6223 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006224{
Ronald Cron5425a212020-08-04 14:58:35 +02006225 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006226 psa_key_type_t type = type_arg;
6227 psa_key_usage_t usage = usage_arg;
6228 size_t bits = bits_arg;
6229 psa_algorithm_t alg = alg_arg;
6230 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006232 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006233
Gilles Peskine8817f612018-12-18 00:18:46 +01006234 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006235
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006236 psa_set_key_usage_flags( &attributes, usage );
6237 psa_set_key_algorithm( &attributes, alg );
6238 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006239 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006240
6241 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006242 psa_status_t status = psa_generate_key( &attributes, &key );
6243
6244 if( is_large_key > 0 )
6245 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6246 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006247 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006248 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006249
6250 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006251 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006252 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6253 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006254
Gilles Peskine818ca122018-06-20 18:16:48 +02006255 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006256 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006257 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006258
6259exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006260 /*
6261 * Key attributes may have been returned by psa_get_key_attributes()
6262 * thus reset them as required.
6263 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006264 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006265
Ronald Cron5425a212020-08-04 14:58:35 +02006266 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006267 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006268}
6269/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006270
Ronald Cronee414c72021-03-18 18:50:08 +01006271/* 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 +02006272void generate_key_rsa( int bits_arg,
6273 data_t *e_arg,
6274 int expected_status_arg )
6275{
Ronald Cron5425a212020-08-04 14:58:35 +02006276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006277 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006278 size_t bits = bits_arg;
6279 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6280 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6281 psa_status_t expected_status = expected_status_arg;
6282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6283 uint8_t *exported = NULL;
6284 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006285 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006286 size_t exported_length = SIZE_MAX;
6287 uint8_t *e_read_buffer = NULL;
6288 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006289 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006290 size_t e_read_length = SIZE_MAX;
6291
6292 if( e_arg->len == 0 ||
6293 ( e_arg->len == 3 &&
6294 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6295 {
6296 is_default_public_exponent = 1;
6297 e_read_size = 0;
6298 }
6299 ASSERT_ALLOC( e_read_buffer, e_read_size );
6300 ASSERT_ALLOC( exported, exported_size );
6301
6302 PSA_ASSERT( psa_crypto_init( ) );
6303
6304 psa_set_key_usage_flags( &attributes, usage );
6305 psa_set_key_algorithm( &attributes, alg );
6306 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6307 e_arg->x, e_arg->len ) );
6308 psa_set_key_bits( &attributes, bits );
6309
6310 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006311 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006312 if( expected_status != PSA_SUCCESS )
6313 goto exit;
6314
6315 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006316 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006317 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6318 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6319 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6320 e_read_buffer, e_read_size,
6321 &e_read_length ) );
6322 if( is_default_public_exponent )
6323 TEST_EQUAL( e_read_length, 0 );
6324 else
6325 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6326
6327 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006328 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006329 goto exit;
6330
6331 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006332 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006333 exported, exported_size,
6334 &exported_length ) );
6335 {
6336 uint8_t *p = exported;
6337 uint8_t *end = exported + exported_length;
6338 size_t len;
6339 /* RSAPublicKey ::= SEQUENCE {
6340 * modulus INTEGER, -- n
6341 * publicExponent INTEGER } -- e
6342 */
6343 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006344 MBEDTLS_ASN1_SEQUENCE |
6345 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006346 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006347 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6348 MBEDTLS_ASN1_INTEGER ) );
6349 if( len >= 1 && p[0] == 0 )
6350 {
6351 ++p;
6352 --len;
6353 }
6354 if( e_arg->len == 0 )
6355 {
6356 TEST_EQUAL( len, 3 );
6357 TEST_EQUAL( p[0], 1 );
6358 TEST_EQUAL( p[1], 0 );
6359 TEST_EQUAL( p[2], 1 );
6360 }
6361 else
6362 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6363 }
6364
6365exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006366 /*
6367 * Key attributes may have been returned by psa_get_key_attributes() or
6368 * set by psa_set_key_domain_parameters() thus reset them as required.
6369 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006370 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006371
Ronald Cron5425a212020-08-04 14:58:35 +02006372 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006373 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006374 mbedtls_free( e_read_buffer );
6375 mbedtls_free( exported );
6376}
6377/* END_CASE */
6378
Darryl Greend49a4992018-06-18 17:27:26 +01006379/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006380void persistent_key_load_key_from_storage( data_t *data,
6381 int type_arg, int bits_arg,
6382 int usage_flags_arg, int alg_arg,
6383 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006384{
Ronald Cron71016a92020-08-28 19:01:50 +02006385 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006386 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006387 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6388 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006389 psa_key_type_t type = type_arg;
6390 size_t bits = bits_arg;
6391 psa_key_usage_t usage_flags = usage_flags_arg;
6392 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006393 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006394 unsigned char *first_export = NULL;
6395 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006396 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006397 size_t first_exported_length;
6398 size_t second_exported_length;
6399
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006400 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6401 {
6402 ASSERT_ALLOC( first_export, export_size );
6403 ASSERT_ALLOC( second_export, export_size );
6404 }
Darryl Greend49a4992018-06-18 17:27:26 +01006405
Gilles Peskine8817f612018-12-18 00:18:46 +01006406 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006407
Gilles Peskinec87af662019-05-15 16:12:22 +02006408 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006409 psa_set_key_usage_flags( &attributes, usage_flags );
6410 psa_set_key_algorithm( &attributes, alg );
6411 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006412 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006413
Darryl Green0c6575a2018-11-07 16:05:30 +00006414 switch( generation_method )
6415 {
6416 case IMPORT_KEY:
6417 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006418 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006419 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006420 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006421
Darryl Green0c6575a2018-11-07 16:05:30 +00006422 case GENERATE_KEY:
6423 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006424 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006425 break;
6426
6427 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006428#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006429 {
6430 /* Create base key */
6431 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6432 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6433 psa_set_key_usage_flags( &base_attributes,
6434 PSA_KEY_USAGE_DERIVE );
6435 psa_set_key_algorithm( &base_attributes, derive_alg );
6436 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006437 PSA_ASSERT( psa_import_key( &base_attributes,
6438 data->x, data->len,
6439 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006440 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006441 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006442 PSA_ASSERT( psa_key_derivation_input_key(
6443 &operation,
6444 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006445 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006446 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006447 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006448 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6449 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006450 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006451 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006452 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006453 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006454 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006455#else
6456 TEST_ASSUME( ! "KDF not supported in this configuration" );
6457#endif
6458 break;
6459
6460 default:
6461 TEST_ASSERT( ! "generation_method not implemented in test" );
6462 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006463 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006464 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006465
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006466 /* Export the key if permitted by the key policy. */
6467 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6468 {
Ronald Cron5425a212020-08-04 14:58:35 +02006469 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006470 first_export, export_size,
6471 &first_exported_length ) );
6472 if( generation_method == IMPORT_KEY )
6473 ASSERT_COMPARE( data->x, data->len,
6474 first_export, first_exported_length );
6475 }
Darryl Greend49a4992018-06-18 17:27:26 +01006476
6477 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006478 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006479 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006480 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006481
Darryl Greend49a4992018-06-18 17:27:26 +01006482 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006483 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006484 TEST_ASSERT( mbedtls_svc_key_id_equal(
6485 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006486 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6487 PSA_KEY_LIFETIME_PERSISTENT );
6488 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6489 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6490 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6491 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006492
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006493 /* Export the key again if permitted by the key policy. */
6494 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006495 {
Ronald Cron5425a212020-08-04 14:58:35 +02006496 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006497 second_export, export_size,
6498 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006499 ASSERT_COMPARE( first_export, first_exported_length,
6500 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006501 }
6502
6503 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006504 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006505 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006506
6507exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006508 /*
6509 * Key attributes may have been returned by psa_get_key_attributes()
6510 * thus reset them as required.
6511 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006512 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006513
Darryl Greend49a4992018-06-18 17:27:26 +01006514 mbedtls_free( first_export );
6515 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006516 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006517 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006518 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006519 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006520}
6521/* END_CASE */