blob: b6d52f7d6696718ac5e11d2aa66499fbe1cb7e5e [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Paul Elliott33746aa2021-09-15 16:40:40 +0100267typedef enum
268{
269 DO_NOT_SET_LENGTHS = 0,
270 SET_LENGTHS_BEFORE_NONCE = 1,
271 SET_LENGTHS_AFTER_NONCE = 2
272} setlengths_method;
273
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100274/*!
275 * \brief Internal Function for AEAD multipart tests.
276 *
277 * \param key_type_arg Type of key passed in
278 * \param key_data The encryption / decryption key data
279 * \param alg_arg The type of algorithm used
280 * \param nonce Nonce data
281 * \param additional_data Additional data
282 * \param ad_part_len If not -1, the length of chunks to
283 * feed additional data in to be encrypted /
284 * decrypted. If -1, no chunking.
285 * \param input_data Data to encrypt / decrypt
286 * \param data_part_len If not -1, the length of chunks to feed the
287 * data in to be encrypted / decrypted. If -1,
288 * no chunking
289 * \param do_set_lengths If non-zero, then set lengths prior to
290 * calling encryption / decryption.
291 * \param expected_output Expected output
Paul Elliott41ffae12021-07-22 21:52:01 +0100292 * \param expect_valid_signature If non zero, we expect the signature to be
293 * valid
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100294 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100295 * \param do_zero_parts If non-zero, interleave zero length chunks
296 * with normal length chunks
297 * \param swap_set_functions If non-zero, swap the order of set lengths
298 * and set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100299 *
300 * \return int Zero on failure, non-zero on success.
301 *
302 */
303static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
304 int alg_arg,
305 data_t *nonce,
306 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100307 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100308 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100309 int data_part_len_arg,
Paul Elliott33746aa2021-09-15 16:40:40 +0100310 setlengths_method set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100311 data_t *expected_output,
312 int expect_valid_signature,
Paul Elliott329d5382021-07-22 17:10:45 +0100313 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100314 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100315{
316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
317 psa_key_type_t key_type = key_type_arg;
318 psa_algorithm_t alg = alg_arg;
319 psa_aead_operation_t operation;
320 unsigned char *output_data = NULL;
321 unsigned char *part_data = NULL;
322 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100324 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 size_t output_size = 0;
326 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100327 size_t output_length = 0;
328 size_t key_bits = 0;
329 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100330 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100331 size_t part_length = 0;
332 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100333 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100334 size_t ad_part_len = 0;
335 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
338 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
339
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100341 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342
Paul Elliottd3f82412021-06-16 16:52:21 +0100343 PSA_ASSERT( psa_crypto_init( ) );
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 if( is_encrypt )
346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
347 else
348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
349
Paul Elliottd3f82412021-06-16 16:52:21 +0100350 psa_set_key_algorithm( &attributes, alg );
351 psa_set_key_type( &attributes, key_type );
352
353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
354 &key ) );
355
356 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
357 key_bits = psa_get_key_bits( &attributes );
358
359 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 if( is_encrypt )
362 {
363 /* Tag gets written at end of buffer. */
364 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
365 ( input_data->len +
366 tag_length ) );
367 data_true_size = input_data->len;
368 }
369 else
370 {
371 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
372 ( input_data->len -
373 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 /* Do not want to attempt to decrypt tag. */
376 data_true_size = input_data->len - tag_length;
377 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100378
379 ASSERT_ALLOC( output_data, output_size );
380
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 if( is_encrypt )
382 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100383 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
384 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385 }
386 else
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100391
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 operation = psa_aead_operation_init( );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396
397 if( is_encrypt )
398 status = psa_aead_encrypt_setup( &operation, key, alg );
399 else
400 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100401
402 /* If the operation is not supported, just skip and not fail in case the
403 * encryption involves a common limitation of cryptography hardwares and
404 * an alternative implementation. */
405 if( status == PSA_ERROR_NOT_SUPPORTED )
406 {
407 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
408 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
409 }
410
411 PSA_ASSERT( status );
412
Paul Elliott33746aa2021-09-15 16:40:40 +0100413 if( set_lengths_method == DO_NOT_SET_LENGTHS )
414 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
415 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100416 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100417 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
418 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100421 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100422 {
423 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
426 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100427 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100428
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100429 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100430 {
431 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100432 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100434 for( part_offset = 0, part_count = 0;
435 part_offset < additional_data->len;
436 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100437 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100438 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 {
Paul Elliott329d5382021-07-22 17:10:45 +0100440 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100441 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100442 else if( additional_data->len - part_offset < ad_part_len )
443 {
444 part_length = additional_data->len - part_offset;
445 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 else
447 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100448 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 }
450
451 PSA_ASSERT( psa_aead_update_ad( &operation,
452 additional_data->x + part_offset,
453 part_length ) );
454
Paul Elliottd3f82412021-06-16 16:52:21 +0100455 }
456 }
457 else
458 {
459 /* Pass additional data in one go. */
460 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
461 additional_data->len ) );
462 }
463
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100464 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 {
466 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100467 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100468 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100469 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100470
471 ASSERT_ALLOC( part_data, part_data_size );
472
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100473 for( part_offset = 0, part_count = 0;
474 part_offset < data_true_size;
475 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100477 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 {
Paul Elliott329d5382021-07-22 17:10:45 +0100479 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100481 else if( ( data_true_size - part_offset ) < data_part_len )
482 {
483 part_length = ( data_true_size - part_offset );
484 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 else
486 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100487 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 }
489
490 PSA_ASSERT( psa_aead_update( &operation,
491 ( input_data->x + part_offset ),
492 part_length, part_data,
493 part_data_size,
494 &output_part_length ) );
495
496 if( output_data && output_part_length )
497 {
498 memcpy( ( output_data + part_offset ), part_data,
499 output_part_length );
500 }
501
Paul Elliottd3f82412021-06-16 16:52:21 +0100502 output_length += output_part_length;
503 }
504 }
505 else
506 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100507 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100508 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100509 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100510 output_size, &output_length ) );
511 }
512
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100513 if( is_encrypt )
514 PSA_ASSERT( psa_aead_finish( &operation, final_data,
515 final_output_size,
516 &output_part_length,
517 tag_buffer, tag_length,
518 &tag_size ) );
519 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 status = psa_aead_verify( &operation, final_data,
522 final_output_size,
523 &output_part_length,
524 ( input_data->x + data_true_size ),
525 tag_length );
526
Paul Elliottf38adbe2021-09-15 17:04:19 +0100527 if( expect_valid_signature )
528 PSA_ASSERT( status );
529 else
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100530 {
Paul Elliottf38adbe2021-09-15 17:04:19 +0100531 TEST_ASSERT( status != PSA_SUCCESS );
532
533 if( status != PSA_SUCCESS )
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 {
535 /* Expected failure. */
536 test_ok = 1;
537 goto exit;
538 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100539 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100540 }
541
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100542 if( output_data && output_part_length )
543 memcpy( ( output_data + output_length ), final_data,
544 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100545
546 output_length += output_part_length;
547
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100548
549 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
550 * should be exact.*/
551 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100552 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100553 TEST_EQUAL( tag_length, tag_size );
554
555 if( output_data && tag_length )
556 memcpy( ( output_data + output_length ), tag_buffer,
557 tag_length );
558
559 output_length += tag_length;
560
561 TEST_EQUAL( output_length,
562 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
563 input_data->len ) );
564 TEST_ASSERT( output_length <=
565 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
566 }
567 else
568 {
569 TEST_EQUAL( output_length,
570 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
571 input_data->len ) );
572 TEST_ASSERT( output_length <=
573 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100574 }
575
Paul Elliottd3f82412021-06-16 16:52:21 +0100576
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100577 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100578 output_data, output_length );
579
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100582
583exit:
584 psa_destroy_key( key );
585 psa_aead_abort( &operation );
586 mbedtls_free( output_data );
587 mbedtls_free( part_data );
588 mbedtls_free( final_data );
589 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100590
591 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100592}
593
Gilles Peskinee59236f2018-01-27 23:32:46 +0100594/* END_HEADER */
595
596/* BEGIN_DEPENDENCIES
597 * depends_on:MBEDTLS_PSA_CRYPTO_C
598 * END_DEPENDENCIES
599 */
600
601/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200602void static_checks( )
603{
604 size_t max_truncated_mac_size =
605 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
606
607 /* Check that the length for a truncated MAC always fits in the algorithm
608 * encoding. The shifted mask is the maximum truncated value. The
609 * untruncated algorithm may be one byte larger. */
610 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
611}
612/* END_CASE */
613
614/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200615void import_with_policy( int type_arg,
616 int usage_arg, int alg_arg,
617 int expected_status_arg )
618{
619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
620 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200621 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200622 psa_key_type_t type = type_arg;
623 psa_key_usage_t usage = usage_arg;
624 psa_algorithm_t alg = alg_arg;
625 psa_status_t expected_status = expected_status_arg;
626 const uint8_t key_material[16] = {0};
627 psa_status_t status;
628
629 PSA_ASSERT( psa_crypto_init( ) );
630
631 psa_set_key_type( &attributes, type );
632 psa_set_key_usage_flags( &attributes, usage );
633 psa_set_key_algorithm( &attributes, alg );
634
635 status = psa_import_key( &attributes,
636 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200637 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200638 TEST_EQUAL( status, expected_status );
639 if( status != PSA_SUCCESS )
640 goto exit;
641
Ronald Cron5425a212020-08-04 14:58:35 +0200642 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200643 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
644 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
645 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200646 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200647
Ronald Cron5425a212020-08-04 14:58:35 +0200648 PSA_ASSERT( psa_destroy_key( key ) );
649 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200650
651exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100652 /*
653 * Key attributes may have been returned by psa_get_key_attributes()
654 * thus reset them as required.
655 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200656 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100657
658 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200659 PSA_DONE( );
660}
661/* END_CASE */
662
663/* BEGIN_CASE */
664void import_with_data( data_t *data, int type_arg,
665 int attr_bits_arg,
666 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200667{
668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
669 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200670 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200671 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200672 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200673 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675
Gilles Peskine8817f612018-12-18 00:18:46 +0100676 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677
Gilles Peskine4747d192019-04-17 15:05:45 +0200678 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200679 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200680
Ronald Cron5425a212020-08-04 14:58:35 +0200681 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100682 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200683 if( status != PSA_SUCCESS )
684 goto exit;
685
Ronald Cron5425a212020-08-04 14:58:35 +0200686 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200687 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200688 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200689 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200690 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200691
Ronald Cron5425a212020-08-04 14:58:35 +0200692 PSA_ASSERT( psa_destroy_key( key ) );
693 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100694
695exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100696 /*
697 * Key attributes may have been returned by psa_get_key_attributes()
698 * thus reset them as required.
699 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200700 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100701
702 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200703 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704}
705/* END_CASE */
706
707/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200708void import_large_key( int type_arg, int byte_size_arg,
709 int expected_status_arg )
710{
711 psa_key_type_t type = type_arg;
712 size_t byte_size = byte_size_arg;
713 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
714 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200716 psa_status_t status;
717 uint8_t *buffer = NULL;
718 size_t buffer_size = byte_size + 1;
719 size_t n;
720
Steven Cooreman69967ce2021-01-18 18:01:08 +0100721 /* Skip the test case if the target running the test cannot
722 * accomodate large keys due to heap size constraints */
723 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200724 memset( buffer, 'K', byte_size );
725
726 PSA_ASSERT( psa_crypto_init( ) );
727
728 /* Try importing the key */
729 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
730 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200731 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100732 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200733 TEST_EQUAL( status, expected_status );
734
735 if( status == PSA_SUCCESS )
736 {
Ronald Cron5425a212020-08-04 14:58:35 +0200737 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200738 TEST_EQUAL( psa_get_key_type( &attributes ), type );
739 TEST_EQUAL( psa_get_key_bits( &attributes ),
740 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200741 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200742 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200743 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200744 for( n = 0; n < byte_size; n++ )
745 TEST_EQUAL( buffer[n], 'K' );
746 for( n = byte_size; n < buffer_size; n++ )
747 TEST_EQUAL( buffer[n], 0 );
748 }
749
750exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100751 /*
752 * Key attributes may have been returned by psa_get_key_attributes()
753 * thus reset them as required.
754 */
755 psa_reset_key_attributes( &attributes );
756
Ronald Cron5425a212020-08-04 14:58:35 +0200757 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200758 PSA_DONE( );
759 mbedtls_free( buffer );
760}
761/* END_CASE */
762
763/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200764void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
765{
Ronald Cron5425a212020-08-04 14:58:35 +0200766 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200767 size_t bits = bits_arg;
768 psa_status_t expected_status = expected_status_arg;
769 psa_status_t status;
770 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200771 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200772 size_t buffer_size = /* Slight overapproximations */
773 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200774 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200775 unsigned char *p;
776 int ret;
777 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200778 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200779
Gilles Peskine8817f612018-12-18 00:18:46 +0100780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200781 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200782
783 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
784 bits, keypair ) ) >= 0 );
785 length = ret;
786
787 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200788 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200789 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100790 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200791
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200792 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200793 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200794
795exit:
796 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200797 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200798}
799/* END_CASE */
800
801/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300802void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300803 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200804 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 int expected_bits,
806 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200807 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 int canonical_input )
809{
Ronald Cron5425a212020-08-04 14:58:35 +0200810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200812 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200813 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 unsigned char *exported = NULL;
816 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100817 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100818 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200821 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100822
Moran Pekercb088e72018-07-17 17:36:59 +0300823 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200824 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100825 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200826 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100827 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100828
Gilles Peskine4747d192019-04-17 15:05:45 +0200829 psa_set_key_usage_flags( &attributes, usage_arg );
830 psa_set_key_algorithm( &attributes, alg );
831 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700832
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100833 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200834 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835
836 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200837 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200838 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
839 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200840 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100841
842 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200843 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100844 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100845
846 /* The exported length must be set by psa_export_key() to a value between 0
847 * and export_size. On errors, the exported length must be 0. */
848 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
849 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
850 TEST_ASSERT( exported_length <= export_size );
851
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200852 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200853 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100854 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200855 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100856 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100857 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200858 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859
Gilles Peskineea38a922021-02-13 00:05:16 +0100860 /* Run sanity checks on the exported key. For non-canonical inputs,
861 * this validates the canonical representations. For canonical inputs,
862 * this doesn't directly validate the implementation, but it still helps
863 * by cross-validating the test data with the sanity check code. */
864 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200865 goto exit;
866
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200868 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100869 else
870 {
Ronald Cron5425a212020-08-04 14:58:35 +0200871 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200872 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200873 &key2 ) );
874 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100875 reexported,
876 export_size,
877 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200878 ASSERT_COMPARE( exported, exported_length,
879 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200880 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100882 TEST_ASSERT( exported_length <=
883 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
884 psa_get_key_bits( &got_attributes ) ) );
885 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886
887destroy:
888 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200889 PSA_ASSERT( psa_destroy_key( key ) );
890 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891
892exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100893 /*
894 * Key attributes may have been returned by psa_get_key_attributes()
895 * thus reset them as required.
896 */
897 psa_reset_key_attributes( &got_attributes );
898
itayzafrir3e02b3b2018-06-12 17:06:52 +0300899 mbedtls_free( exported );
900 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200901 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100902}
903/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100904
Moran Pekerf709f4a2018-06-06 17:26:04 +0300905/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300906void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200907 int type_arg,
908 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100909 int export_size_delta,
910 int expected_export_status_arg,
911 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300912{
Ronald Cron5425a212020-08-04 14:58:35 +0200913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300914 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200915 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200916 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300917 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100919 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100920 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300922
Gilles Peskine8817f612018-12-18 00:18:46 +0100923 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300924
Gilles Peskine4747d192019-04-17 15:05:45 +0200925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
926 psa_set_key_algorithm( &attributes, alg );
927 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300928
929 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200930 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300931
Gilles Peskine49c25912018-10-29 15:15:31 +0100932 /* Export the public key */
933 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200934 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200935 exported, export_size,
936 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100937 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100938 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100939 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200940 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100941 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200942 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200943 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100944 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100945 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100946 TEST_ASSERT( expected_public_key->len <=
947 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
948 TEST_ASSERT( expected_public_key->len <=
949 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100950 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
951 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100952 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300953
954exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100955 /*
956 * Key attributes may have been returned by psa_get_key_attributes()
957 * thus reset them as required.
958 */
959 psa_reset_key_attributes( &attributes );
960
itayzafrir3e02b3b2018-06-12 17:06:52 +0300961 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200962 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200963 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300964}
965/* END_CASE */
966
Gilles Peskine20035e32018-02-03 22:44:14 +0100967/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200968void import_and_exercise_key( data_t *data,
969 int type_arg,
970 int bits_arg,
971 int alg_arg )
972{
Ronald Cron5425a212020-08-04 14:58:35 +0200973 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200974 psa_key_type_t type = type_arg;
975 size_t bits = bits_arg;
976 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100977 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200978 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200979 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980
Gilles Peskine8817f612018-12-18 00:18:46 +0100981 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200982
Gilles Peskine4747d192019-04-17 15:05:45 +0200983 psa_set_key_usage_flags( &attributes, usage );
984 psa_set_key_algorithm( &attributes, alg );
985 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200986
987 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200988 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
990 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200991 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200992 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
993 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200994
995 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100996 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200997 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200998
Ronald Cron5425a212020-08-04 14:58:35 +0200999 PSA_ASSERT( psa_destroy_key( key ) );
1000 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001001
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001002exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001003 /*
1004 * Key attributes may have been returned by psa_get_key_attributes()
1005 * thus reset them as required.
1006 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001007 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001008
1009 psa_reset_key_attributes( &attributes );
1010 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001011 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001012}
1013/* END_CASE */
1014
1015/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001016void effective_key_attributes( int type_arg, int expected_type_arg,
1017 int bits_arg, int expected_bits_arg,
1018 int usage_arg, int expected_usage_arg,
1019 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001020{
Ronald Cron5425a212020-08-04 14:58:35 +02001021 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001022 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001023 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001024 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001025 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001026 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001027 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001028 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001029 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001030 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001031
Gilles Peskine8817f612018-12-18 00:18:46 +01001032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001033
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001034 psa_set_key_usage_flags( &attributes, usage );
1035 psa_set_key_algorithm( &attributes, alg );
1036 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001037 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
Ronald Cron5425a212020-08-04 14:58:35 +02001039 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001040 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001041
Ronald Cron5425a212020-08-04 14:58:35 +02001042 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001043 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1044 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1045 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1046 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001047
1048exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001049 /*
1050 * Key attributes may have been returned by psa_get_key_attributes()
1051 * thus reset them as required.
1052 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001053 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001054
1055 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001056 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001057}
1058/* END_CASE */
1059
1060/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001061void check_key_policy( int type_arg, int bits_arg,
1062 int usage_arg, int alg_arg )
1063{
1064 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1065 usage_arg, usage_arg, alg_arg, alg_arg );
1066 goto exit;
1067}
1068/* END_CASE */
1069
1070/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001071void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001072{
1073 /* Test each valid way of initializing the object, except for `= {0}`, as
1074 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1075 * though it's OK by the C standard. We could test for this, but we'd need
1076 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001077 psa_key_attributes_t func = psa_key_attributes_init( );
1078 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1079 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001080
1081 memset( &zero, 0, sizeof( zero ) );
1082
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001083 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1084 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1085 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001086
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001087 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1088 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1089 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1090
1091 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1092 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1093 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1094
1095 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1096 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1097 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1098
1099 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1100 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1101 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001102}
1103/* END_CASE */
1104
1105/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001106void mac_key_policy( int policy_usage,
1107 int policy_alg,
1108 int key_type,
1109 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001110 int exercise_alg,
1111 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001112{
Ronald Cron5425a212020-08-04 14:58:35 +02001113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001115 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001116 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001117 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001119
Gilles Peskine8817f612018-12-18 00:18:46 +01001120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001122 psa_set_key_usage_flags( &attributes, policy_usage );
1123 psa_set_key_algorithm( &attributes, policy_alg );
1124 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001125
Gilles Peskine049c7532019-05-15 20:22:09 +02001126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001127 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001128
Ronald Cron5425a212020-08-04 14:58:35 +02001129 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001130 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001131 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001132 else
1133 TEST_EQUAL( status, expected_status );
1134
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001135 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001136
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001137 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001138 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001139 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001140 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001141 else
1142 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001143
1144exit:
1145 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001146 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001147 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE */
1152void cipher_key_policy( int policy_usage,
1153 int policy_alg,
1154 int key_type,
1155 data_t *key_data,
1156 int exercise_alg )
1157{
Ronald Cron5425a212020-08-04 14:58:35 +02001158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001160 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001161 psa_status_t status;
1162
Gilles Peskine8817f612018-12-18 00:18:46 +01001163 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001165 psa_set_key_usage_flags( &attributes, policy_usage );
1166 psa_set_key_algorithm( &attributes, policy_alg );
1167 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001168
Gilles Peskine049c7532019-05-15 20:22:09 +02001169 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001170 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001171
Ronald Cron5425a212020-08-04 14:58:35 +02001172 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001173 if( policy_alg == exercise_alg &&
1174 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001175 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001176 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001177 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001178 psa_cipher_abort( &operation );
1179
Ronald Cron5425a212020-08-04 14:58:35 +02001180 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001181 if( policy_alg == exercise_alg &&
1182 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001183 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001184 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001185 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001186
1187exit:
1188 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001189 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001190 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191}
1192/* END_CASE */
1193
1194/* BEGIN_CASE */
1195void aead_key_policy( int policy_usage,
1196 int policy_alg,
1197 int key_type,
1198 data_t *key_data,
1199 int nonce_length_arg,
1200 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001201 int exercise_alg,
1202 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001203{
Ronald Cron5425a212020-08-04 14:58:35 +02001204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001206 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001207 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208 unsigned char nonce[16] = {0};
1209 size_t nonce_length = nonce_length_arg;
1210 unsigned char tag[16];
1211 size_t tag_length = tag_length_arg;
1212 size_t output_length;
1213
1214 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1215 TEST_ASSERT( tag_length <= sizeof( tag ) );
1216
Gilles Peskine8817f612018-12-18 00:18:46 +01001217 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001219 psa_set_key_usage_flags( &attributes, policy_usage );
1220 psa_set_key_algorithm( &attributes, policy_alg );
1221 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222
Gilles Peskine049c7532019-05-15 20:22:09 +02001223 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001224 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225
Ronald Cron5425a212020-08-04 14:58:35 +02001226 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001227 nonce, nonce_length,
1228 NULL, 0,
1229 NULL, 0,
1230 tag, tag_length,
1231 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001232 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1233 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001234 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001235 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001236
1237 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001238 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001239 nonce, nonce_length,
1240 NULL, 0,
1241 tag, tag_length,
1242 NULL, 0,
1243 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001244 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1245 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1246 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001247 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001248 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001249 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001250
1251exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001252 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001253 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001254}
1255/* END_CASE */
1256
1257/* BEGIN_CASE */
1258void asymmetric_encryption_key_policy( int policy_usage,
1259 int policy_alg,
1260 int key_type,
1261 data_t *key_data,
1262 int exercise_alg )
1263{
Ronald Cron5425a212020-08-04 14:58:35 +02001264 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001265 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266 psa_status_t status;
1267 size_t key_bits;
1268 size_t buffer_length;
1269 unsigned char *buffer = NULL;
1270 size_t output_length;
1271
Gilles Peskine8817f612018-12-18 00:18:46 +01001272 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001273
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001274 psa_set_key_usage_flags( &attributes, policy_usage );
1275 psa_set_key_algorithm( &attributes, policy_alg );
1276 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277
Gilles Peskine049c7532019-05-15 20:22:09 +02001278 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001279 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001280
Ronald Cron5425a212020-08-04 14:58:35 +02001281 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001282 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001283 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1284 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001285 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001286
Ronald Cron5425a212020-08-04 14:58:35 +02001287 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001288 NULL, 0,
1289 NULL, 0,
1290 buffer, buffer_length,
1291 &output_length );
1292 if( policy_alg == exercise_alg &&
1293 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001294 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001295 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001296 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001297
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001298 if( buffer_length != 0 )
1299 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001300 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001301 buffer, buffer_length,
1302 NULL, 0,
1303 buffer, buffer_length,
1304 &output_length );
1305 if( policy_alg == exercise_alg &&
1306 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001307 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001308 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001309 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001310
1311exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001312 /*
1313 * Key attributes may have been returned by psa_get_key_attributes()
1314 * thus reset them as required.
1315 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001316 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001317
1318 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001319 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 mbedtls_free( buffer );
1321}
1322/* END_CASE */
1323
1324/* BEGIN_CASE */
1325void asymmetric_signature_key_policy( int policy_usage,
1326 int policy_alg,
1327 int key_type,
1328 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001329 int exercise_alg,
1330 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001331{
Ronald Cron5425a212020-08-04 14:58:35 +02001332 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001333 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001334 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001335 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1336 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1337 * compatible with the policy and `payload_length_arg` is supposed to be
1338 * a valid input length to sign. If `payload_length_arg <= 0`,
1339 * `exercise_alg` is supposed to be forbidden by the policy. */
1340 int compatible_alg = payload_length_arg > 0;
1341 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001342 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001343 size_t signature_length;
1344
Gilles Peskine8817f612018-12-18 00:18:46 +01001345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001347 psa_set_key_usage_flags( &attributes, policy_usage );
1348 psa_set_key_algorithm( &attributes, policy_alg );
1349 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001350
Gilles Peskine049c7532019-05-15 20:22:09 +02001351 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001352 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001353
Ronald Cron5425a212020-08-04 14:58:35 +02001354 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001355 payload, payload_length,
1356 signature, sizeof( signature ),
1357 &signature_length );
1358 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001359 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001360 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001361 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001362
1363 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001364 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001365 payload, payload_length,
1366 signature, sizeof( signature ) );
1367 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001368 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001369 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001370 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001371
1372exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001373 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001374 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001375}
1376/* END_CASE */
1377
Janos Follathba3fab92019-06-11 14:50:16 +01001378/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001379void derive_key_policy( int policy_usage,
1380 int policy_alg,
1381 int key_type,
1382 data_t *key_data,
1383 int exercise_alg )
1384{
Ronald Cron5425a212020-08-04 14:58:35 +02001385 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001386 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001387 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001388 psa_status_t status;
1389
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001391
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001392 psa_set_key_usage_flags( &attributes, policy_usage );
1393 psa_set_key_algorithm( &attributes, policy_alg );
1394 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001395
Gilles Peskine049c7532019-05-15 20:22:09 +02001396 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001397 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001398
Janos Follathba3fab92019-06-11 14:50:16 +01001399 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1400
1401 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1402 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001403 {
Janos Follathba3fab92019-06-11 14:50:16 +01001404 PSA_ASSERT( psa_key_derivation_input_bytes(
1405 &operation,
1406 PSA_KEY_DERIVATION_INPUT_SEED,
1407 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001408 }
Janos Follathba3fab92019-06-11 14:50:16 +01001409
1410 status = psa_key_derivation_input_key( &operation,
1411 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001412 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001413
Gilles Peskineea0fb492018-07-12 17:17:20 +02001414 if( policy_alg == exercise_alg &&
1415 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001416 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001417 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001418 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001419
1420exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001421 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001422 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001423 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001424}
1425/* END_CASE */
1426
1427/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001428void agreement_key_policy( int policy_usage,
1429 int policy_alg,
1430 int key_type_arg,
1431 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001432 int exercise_alg,
1433 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001434{
Ronald Cron5425a212020-08-04 14:58:35 +02001435 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001437 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001438 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001439 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001440 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441
Gilles Peskine8817f612018-12-18 00:18:46 +01001442 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001443
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001444 psa_set_key_usage_flags( &attributes, policy_usage );
1445 psa_set_key_algorithm( &attributes, policy_alg );
1446 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001447
Gilles Peskine049c7532019-05-15 20:22:09 +02001448 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001449 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001450
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001451 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001452 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001453
Steven Cooremance48e852020-10-05 16:02:45 +02001454 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001455
1456exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001457 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001458 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001459 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001460}
1461/* END_CASE */
1462
1463/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001464void key_policy_alg2( int key_type_arg, data_t *key_data,
1465 int usage_arg, int alg_arg, int alg2_arg )
1466{
Ronald Cron5425a212020-08-04 14:58:35 +02001467 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001468 psa_key_type_t key_type = key_type_arg;
1469 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1470 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1471 psa_key_usage_t usage = usage_arg;
1472 psa_algorithm_t alg = alg_arg;
1473 psa_algorithm_t alg2 = alg2_arg;
1474
1475 PSA_ASSERT( psa_crypto_init( ) );
1476
1477 psa_set_key_usage_flags( &attributes, usage );
1478 psa_set_key_algorithm( &attributes, alg );
1479 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1480 psa_set_key_type( &attributes, key_type );
1481 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001482 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001483
Ronald Cron5425a212020-08-04 14:58:35 +02001484 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001485 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1486 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1487 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1488
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001489 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001490 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001491 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001492 goto exit;
1493
1494exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001495 /*
1496 * Key attributes may have been returned by psa_get_key_attributes()
1497 * thus reset them as required.
1498 */
1499 psa_reset_key_attributes( &got_attributes );
1500
Ronald Cron5425a212020-08-04 14:58:35 +02001501 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001502 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001503}
1504/* END_CASE */
1505
1506/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001507void raw_agreement_key_policy( int policy_usage,
1508 int policy_alg,
1509 int key_type_arg,
1510 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001511 int exercise_alg,
1512 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001513{
Ronald Cron5425a212020-08-04 14:58:35 +02001514 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001516 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001517 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001518 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001519 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001520
1521 PSA_ASSERT( psa_crypto_init( ) );
1522
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001523 psa_set_key_usage_flags( &attributes, policy_usage );
1524 psa_set_key_algorithm( &attributes, policy_alg );
1525 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001526
Gilles Peskine049c7532019-05-15 20:22:09 +02001527 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001528 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001529
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001530 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001531
Steven Cooremance48e852020-10-05 16:02:45 +02001532 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001533
1534exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001535 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001536 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001537 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001538}
1539/* END_CASE */
1540
1541/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001542void copy_success( int source_usage_arg,
1543 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001544 int type_arg, data_t *material,
1545 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001546 int target_usage_arg,
1547 int target_alg_arg, int target_alg2_arg,
1548 int expected_usage_arg,
1549 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001550{
Gilles Peskineca25db92019-04-19 11:43:08 +02001551 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1552 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001553 psa_key_usage_t expected_usage = expected_usage_arg;
1554 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001555 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001556 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1557 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001558 uint8_t *export_buffer = NULL;
1559
Gilles Peskine57ab7212019-01-28 13:03:09 +01001560 PSA_ASSERT( psa_crypto_init( ) );
1561
Gilles Peskineca25db92019-04-19 11:43:08 +02001562 /* Prepare the source key. */
1563 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1564 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001565 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001567 PSA_ASSERT( psa_import_key( &source_attributes,
1568 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001569 &source_key ) );
1570 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001571
Gilles Peskineca25db92019-04-19 11:43:08 +02001572 /* Prepare the target attributes. */
1573 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001574 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001575 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001576 /* Set volatile lifetime to reset the key identifier to 0. */
1577 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1578 }
1579
Gilles Peskineca25db92019-04-19 11:43:08 +02001580 if( target_usage_arg != -1 )
1581 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1582 if( target_alg_arg != -1 )
1583 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001584 if( target_alg2_arg != -1 )
1585 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001586
1587 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001588 PSA_ASSERT( psa_copy_key( source_key,
1589 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001590
1591 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001592 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001593
1594 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001595 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001596 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1597 psa_get_key_type( &target_attributes ) );
1598 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1599 psa_get_key_bits( &target_attributes ) );
1600 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1601 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001602 TEST_EQUAL( expected_alg2,
1603 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001604 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1605 {
1606 size_t length;
1607 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001608 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001609 material->len, &length ) );
1610 ASSERT_COMPARE( material->x, material->len,
1611 export_buffer, length );
1612 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001613
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001614 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001615 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001616 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001617 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001618
Ronald Cron5425a212020-08-04 14:58:35 +02001619 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001620
1621exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001622 /*
1623 * Source and target key attributes may have been returned by
1624 * psa_get_key_attributes() thus reset them as required.
1625 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001626 psa_reset_key_attributes( &source_attributes );
1627 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001628
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001629 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001630 mbedtls_free( export_buffer );
1631}
1632/* END_CASE */
1633
1634/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001635void copy_fail( int source_usage_arg,
1636 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001637 int type_arg, data_t *material,
1638 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001639 int target_usage_arg,
1640 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001641 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001642 int expected_status_arg )
1643{
1644 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1645 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001646 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1647 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001648 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001649
1650 PSA_ASSERT( psa_crypto_init( ) );
1651
1652 /* Prepare the source key. */
1653 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1654 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001655 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001656 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001657 PSA_ASSERT( psa_import_key( &source_attributes,
1658 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001659 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001660
1661 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001662 psa_set_key_id( &target_attributes, key_id );
1663 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001664 psa_set_key_type( &target_attributes, target_type_arg );
1665 psa_set_key_bits( &target_attributes, target_bits_arg );
1666 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1667 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001668 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001669
1670 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001671 TEST_EQUAL( psa_copy_key( source_key,
1672 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001673 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001674
Ronald Cron5425a212020-08-04 14:58:35 +02001675 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001676
Gilles Peskine4a644642019-05-03 17:14:08 +02001677exit:
1678 psa_reset_key_attributes( &source_attributes );
1679 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001680 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001681}
1682/* END_CASE */
1683
1684/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001685void hash_operation_init( )
1686{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001687 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001688 /* Test each valid way of initializing the object, except for `= {0}`, as
1689 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1690 * though it's OK by the C standard. We could test for this, but we'd need
1691 * to supress the Clang warning for the test. */
1692 psa_hash_operation_t func = psa_hash_operation_init( );
1693 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1694 psa_hash_operation_t zero;
1695
1696 memset( &zero, 0, sizeof( zero ) );
1697
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001698 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001699 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1700 PSA_ERROR_BAD_STATE );
1701 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1702 PSA_ERROR_BAD_STATE );
1703 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1704 PSA_ERROR_BAD_STATE );
1705
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001706 /* A default hash operation should be abortable without error. */
1707 PSA_ASSERT( psa_hash_abort( &func ) );
1708 PSA_ASSERT( psa_hash_abort( &init ) );
1709 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001710}
1711/* END_CASE */
1712
1713/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001714void hash_setup( int alg_arg,
1715 int expected_status_arg )
1716{
1717 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001718 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001719 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001720 psa_status_t status;
1721
Gilles Peskine8817f612018-12-18 00:18:46 +01001722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001723
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001724 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001725 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001726
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001727 /* Whether setup succeeded or failed, abort must succeed. */
1728 PSA_ASSERT( psa_hash_abort( &operation ) );
1729
1730 /* If setup failed, reproduce the failure, so as to
1731 * test the resulting state of the operation object. */
1732 if( status != PSA_SUCCESS )
1733 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1734
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001735 /* Now the operation object should be reusable. */
1736#if defined(KNOWN_SUPPORTED_HASH_ALG)
1737 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1738 PSA_ASSERT( psa_hash_abort( &operation ) );
1739#endif
1740
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001741exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001742 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001743}
1744/* END_CASE */
1745
1746/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001747void hash_compute_fail( int alg_arg, data_t *input,
1748 int output_size_arg, int expected_status_arg )
1749{
1750 psa_algorithm_t alg = alg_arg;
1751 uint8_t *output = NULL;
1752 size_t output_size = output_size_arg;
1753 size_t output_length = INVALID_EXPORT_LENGTH;
1754 psa_status_t expected_status = expected_status_arg;
1755 psa_status_t status;
1756
1757 ASSERT_ALLOC( output, output_size );
1758
1759 PSA_ASSERT( psa_crypto_init( ) );
1760
1761 status = psa_hash_compute( alg, input->x, input->len,
1762 output, output_size, &output_length );
1763 TEST_EQUAL( status, expected_status );
1764 TEST_ASSERT( output_length <= output_size );
1765
1766exit:
1767 mbedtls_free( output );
1768 PSA_DONE( );
1769}
1770/* END_CASE */
1771
1772/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001773void hash_compare_fail( int alg_arg, data_t *input,
1774 data_t *reference_hash,
1775 int expected_status_arg )
1776{
1777 psa_algorithm_t alg = alg_arg;
1778 psa_status_t expected_status = expected_status_arg;
1779 psa_status_t status;
1780
1781 PSA_ASSERT( psa_crypto_init( ) );
1782
1783 status = psa_hash_compare( alg, input->x, input->len,
1784 reference_hash->x, reference_hash->len );
1785 TEST_EQUAL( status, expected_status );
1786
1787exit:
1788 PSA_DONE( );
1789}
1790/* END_CASE */
1791
1792/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001793void hash_compute_compare( int alg_arg, data_t *input,
1794 data_t *expected_output )
1795{
1796 psa_algorithm_t alg = alg_arg;
1797 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1798 size_t output_length = INVALID_EXPORT_LENGTH;
1799 size_t i;
1800
1801 PSA_ASSERT( psa_crypto_init( ) );
1802
1803 /* Compute with tight buffer */
1804 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001805 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001806 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001807 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001808 ASSERT_COMPARE( output, output_length,
1809 expected_output->x, expected_output->len );
1810
1811 /* Compute with larger buffer */
1812 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1813 output, sizeof( output ),
1814 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001815 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001816 ASSERT_COMPARE( output, output_length,
1817 expected_output->x, expected_output->len );
1818
1819 /* Compare with correct hash */
1820 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1821 output, output_length ) );
1822
1823 /* Compare with trailing garbage */
1824 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1825 output, output_length + 1 ),
1826 PSA_ERROR_INVALID_SIGNATURE );
1827
1828 /* Compare with truncated hash */
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 corrupted value */
1834 for( i = 0; i < output_length; i++ )
1835 {
Chris Jones9634bb12021-01-20 15:56:42 +00001836 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001837 output[i] ^= 1;
1838 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1839 output, output_length ),
1840 PSA_ERROR_INVALID_SIGNATURE );
1841 output[i] ^= 1;
1842 }
1843
1844exit:
1845 PSA_DONE( );
1846}
1847/* END_CASE */
1848
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001849/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001850void hash_bad_order( )
1851{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001852 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001853 unsigned char input[] = "";
1854 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001855 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001856 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1857 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1858 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001859 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001860 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001861 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001862
Gilles Peskine8817f612018-12-18 00:18:46 +01001863 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001864
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001865 /* Call setup twice in a row. */
1866 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1867 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1868 PSA_ERROR_BAD_STATE );
1869 PSA_ASSERT( psa_hash_abort( &operation ) );
1870
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001871 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001872 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001873 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001874 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001875
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001876 /* Call update after finish. */
1877 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1878 PSA_ASSERT( psa_hash_finish( &operation,
1879 hash, sizeof( hash ), &hash_len ) );
1880 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001881 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001882 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001883
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001884 /* Call verify without calling setup beforehand. */
1885 TEST_EQUAL( psa_hash_verify( &operation,
1886 valid_hash, sizeof( valid_hash ) ),
1887 PSA_ERROR_BAD_STATE );
1888 PSA_ASSERT( psa_hash_abort( &operation ) );
1889
1890 /* Call verify after finish. */
1891 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1892 PSA_ASSERT( psa_hash_finish( &operation,
1893 hash, sizeof( hash ), &hash_len ) );
1894 TEST_EQUAL( psa_hash_verify( &operation,
1895 valid_hash, sizeof( valid_hash ) ),
1896 PSA_ERROR_BAD_STATE );
1897 PSA_ASSERT( psa_hash_abort( &operation ) );
1898
1899 /* Call verify twice in a row. */
1900 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1901 PSA_ASSERT( psa_hash_verify( &operation,
1902 valid_hash, sizeof( valid_hash ) ) );
1903 TEST_EQUAL( psa_hash_verify( &operation,
1904 valid_hash, sizeof( valid_hash ) ),
1905 PSA_ERROR_BAD_STATE );
1906 PSA_ASSERT( psa_hash_abort( &operation ) );
1907
1908 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001909 TEST_EQUAL( psa_hash_finish( &operation,
1910 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001911 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001912 PSA_ASSERT( psa_hash_abort( &operation ) );
1913
1914 /* Call finish twice in a row. */
1915 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1916 PSA_ASSERT( psa_hash_finish( &operation,
1917 hash, sizeof( hash ), &hash_len ) );
1918 TEST_EQUAL( psa_hash_finish( &operation,
1919 hash, sizeof( hash ), &hash_len ),
1920 PSA_ERROR_BAD_STATE );
1921 PSA_ASSERT( psa_hash_abort( &operation ) );
1922
1923 /* Call finish after calling verify. */
1924 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1925 PSA_ASSERT( psa_hash_verify( &operation,
1926 valid_hash, sizeof( valid_hash ) ) );
1927 TEST_EQUAL( psa_hash_finish( &operation,
1928 hash, sizeof( hash ), &hash_len ),
1929 PSA_ERROR_BAD_STATE );
1930 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001931
1932exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001933 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001934}
1935/* END_CASE */
1936
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001937/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001938void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001939{
1940 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001941 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1942 * appended to it */
1943 unsigned char hash[] = {
1944 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1945 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1946 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001947 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001948 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001949
Gilles Peskine8817f612018-12-18 00:18:46 +01001950 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001951
itayzafrir27e69452018-11-01 14:26:34 +02001952 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001953 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001954 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001955 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001956
itayzafrir27e69452018-11-01 14:26:34 +02001957 /* psa_hash_verify with a non-matching hash */
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 + 1, expected_size ),
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 hash longer than expected */
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, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001965 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001966
itayzafrirec93d302018-10-18 18:01:10 +03001967exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001968 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001969}
1970/* END_CASE */
1971
Ronald Cronee414c72021-03-18 18:50:08 +01001972/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001973void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001974{
1975 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001976 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001977 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001978 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001979 size_t hash_len;
1980
Gilles Peskine8817f612018-12-18 00:18:46 +01001981 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001982
itayzafrir58028322018-10-25 10:22:01 +03001983 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001984 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001985 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001986 hash, expected_size - 1, &hash_len ),
1987 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001988
1989exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001990 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001991}
1992/* END_CASE */
1993
Ronald Cronee414c72021-03-18 18:50:08 +01001994/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001995void hash_clone_source_state( )
1996{
1997 psa_algorithm_t alg = PSA_ALG_SHA_256;
1998 unsigned char hash[PSA_HASH_MAX_SIZE];
1999 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2000 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2001 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2002 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2003 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2004 size_t hash_len;
2005
2006 PSA_ASSERT( psa_crypto_init( ) );
2007 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2008
2009 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2010 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2011 PSA_ASSERT( psa_hash_finish( &op_finished,
2012 hash, sizeof( hash ), &hash_len ) );
2013 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2014 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2015
2016 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2017 PSA_ERROR_BAD_STATE );
2018
2019 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2020 PSA_ASSERT( psa_hash_finish( &op_init,
2021 hash, sizeof( hash ), &hash_len ) );
2022 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2023 PSA_ASSERT( psa_hash_finish( &op_finished,
2024 hash, sizeof( hash ), &hash_len ) );
2025 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2026 PSA_ASSERT( psa_hash_finish( &op_aborted,
2027 hash, sizeof( hash ), &hash_len ) );
2028
2029exit:
2030 psa_hash_abort( &op_source );
2031 psa_hash_abort( &op_init );
2032 psa_hash_abort( &op_setup );
2033 psa_hash_abort( &op_finished );
2034 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002035 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002036}
2037/* END_CASE */
2038
Ronald Cronee414c72021-03-18 18:50:08 +01002039/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002040void hash_clone_target_state( )
2041{
2042 psa_algorithm_t alg = PSA_ALG_SHA_256;
2043 unsigned char hash[PSA_HASH_MAX_SIZE];
2044 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2045 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2046 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2047 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2048 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2049 size_t hash_len;
2050
2051 PSA_ASSERT( psa_crypto_init( ) );
2052
2053 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2054 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2055 PSA_ASSERT( psa_hash_finish( &op_finished,
2056 hash, sizeof( hash ), &hash_len ) );
2057 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2058 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2059
2060 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2061 PSA_ASSERT( psa_hash_finish( &op_target,
2062 hash, sizeof( hash ), &hash_len ) );
2063
2064 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2065 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2066 PSA_ERROR_BAD_STATE );
2067 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2068 PSA_ERROR_BAD_STATE );
2069
2070exit:
2071 psa_hash_abort( &op_target );
2072 psa_hash_abort( &op_init );
2073 psa_hash_abort( &op_setup );
2074 psa_hash_abort( &op_finished );
2075 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002076 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002077}
2078/* END_CASE */
2079
itayzafrir58028322018-10-25 10:22:01 +03002080/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002081void mac_operation_init( )
2082{
Jaeden Amero252ef282019-02-15 14:05:35 +00002083 const uint8_t input[1] = { 0 };
2084
Jaeden Amero769ce272019-01-04 11:48:03 +00002085 /* Test each valid way of initializing the object, except for `= {0}`, as
2086 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2087 * though it's OK by the C standard. We could test for this, but we'd need
2088 * to supress the Clang warning for the test. */
2089 psa_mac_operation_t func = psa_mac_operation_init( );
2090 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2091 psa_mac_operation_t zero;
2092
2093 memset( &zero, 0, sizeof( zero ) );
2094
Jaeden Amero252ef282019-02-15 14:05:35 +00002095 /* A freshly-initialized MAC operation should not be usable. */
2096 TEST_EQUAL( psa_mac_update( &func,
2097 input, sizeof( input ) ),
2098 PSA_ERROR_BAD_STATE );
2099 TEST_EQUAL( psa_mac_update( &init,
2100 input, sizeof( input ) ),
2101 PSA_ERROR_BAD_STATE );
2102 TEST_EQUAL( psa_mac_update( &zero,
2103 input, sizeof( input ) ),
2104 PSA_ERROR_BAD_STATE );
2105
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002106 /* A default MAC operation should be abortable without error. */
2107 PSA_ASSERT( psa_mac_abort( &func ) );
2108 PSA_ASSERT( psa_mac_abort( &init ) );
2109 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002110}
2111/* END_CASE */
2112
2113/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002114void mac_setup( int key_type_arg,
2115 data_t *key,
2116 int alg_arg,
2117 int expected_status_arg )
2118{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002119 psa_key_type_t key_type = key_type_arg;
2120 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002121 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002122 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002123 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2124#if defined(KNOWN_SUPPORTED_MAC_ALG)
2125 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2126#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002127
Gilles Peskine8817f612018-12-18 00:18:46 +01002128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002129
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002130 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2131 &operation, &status ) )
2132 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002133 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002135 /* The operation object should be reusable. */
2136#if defined(KNOWN_SUPPORTED_MAC_ALG)
2137 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2138 smoke_test_key_data,
2139 sizeof( smoke_test_key_data ),
2140 KNOWN_SUPPORTED_MAC_ALG,
2141 &operation, &status ) )
2142 goto exit;
2143 TEST_EQUAL( status, PSA_SUCCESS );
2144#endif
2145
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002146exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002147 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002148}
2149/* END_CASE */
2150
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002151/* 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 +00002152void mac_bad_order( )
2153{
Ronald Cron5425a212020-08-04 14:58:35 +02002154 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002155 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2156 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002157 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002158 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2159 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2160 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002161 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002162 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2163 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2164 size_t sign_mac_length = 0;
2165 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2166 const uint8_t verify_mac[] = {
2167 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2168 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2169 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2170
2171 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002172 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002173 psa_set_key_algorithm( &attributes, alg );
2174 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002175
Ronald Cron5425a212020-08-04 14:58:35 +02002176 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2177 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002178
Jaeden Amero252ef282019-02-15 14:05:35 +00002179 /* Call update without calling setup beforehand. */
2180 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2181 PSA_ERROR_BAD_STATE );
2182 PSA_ASSERT( psa_mac_abort( &operation ) );
2183
2184 /* Call sign finish without calling setup beforehand. */
2185 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2186 &sign_mac_length),
2187 PSA_ERROR_BAD_STATE );
2188 PSA_ASSERT( psa_mac_abort( &operation ) );
2189
2190 /* Call verify finish without calling setup beforehand. */
2191 TEST_EQUAL( psa_mac_verify_finish( &operation,
2192 verify_mac, sizeof( verify_mac ) ),
2193 PSA_ERROR_BAD_STATE );
2194 PSA_ASSERT( psa_mac_abort( &operation ) );
2195
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002196 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002197 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2198 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002199 PSA_ERROR_BAD_STATE );
2200 PSA_ASSERT( psa_mac_abort( &operation ) );
2201
Jaeden Amero252ef282019-02-15 14:05:35 +00002202 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002203 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002204 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2205 PSA_ASSERT( psa_mac_sign_finish( &operation,
2206 sign_mac, sizeof( sign_mac ),
2207 &sign_mac_length ) );
2208 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2209 PSA_ERROR_BAD_STATE );
2210 PSA_ASSERT( psa_mac_abort( &operation ) );
2211
2212 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002213 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002214 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2215 PSA_ASSERT( psa_mac_verify_finish( &operation,
2216 verify_mac, sizeof( verify_mac ) ) );
2217 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2218 PSA_ERROR_BAD_STATE );
2219 PSA_ASSERT( psa_mac_abort( &operation ) );
2220
2221 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002222 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002223 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2224 PSA_ASSERT( psa_mac_sign_finish( &operation,
2225 sign_mac, sizeof( sign_mac ),
2226 &sign_mac_length ) );
2227 TEST_EQUAL( psa_mac_sign_finish( &operation,
2228 sign_mac, sizeof( sign_mac ),
2229 &sign_mac_length ),
2230 PSA_ERROR_BAD_STATE );
2231 PSA_ASSERT( psa_mac_abort( &operation ) );
2232
2233 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002234 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002235 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2236 PSA_ASSERT( psa_mac_verify_finish( &operation,
2237 verify_mac, sizeof( verify_mac ) ) );
2238 TEST_EQUAL( psa_mac_verify_finish( &operation,
2239 verify_mac, sizeof( verify_mac ) ),
2240 PSA_ERROR_BAD_STATE );
2241 PSA_ASSERT( psa_mac_abort( &operation ) );
2242
2243 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002244 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002245 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2246 TEST_EQUAL( psa_mac_verify_finish( &operation,
2247 verify_mac, sizeof( verify_mac ) ),
2248 PSA_ERROR_BAD_STATE );
2249 PSA_ASSERT( psa_mac_abort( &operation ) );
2250
2251 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002252 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002253 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2254 TEST_EQUAL( psa_mac_sign_finish( &operation,
2255 sign_mac, sizeof( sign_mac ),
2256 &sign_mac_length ),
2257 PSA_ERROR_BAD_STATE );
2258 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002259
Ronald Cron5425a212020-08-04 14:58:35 +02002260 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002261
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002262exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002263 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264}
2265/* END_CASE */
2266
2267/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002268void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002269 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002270 int alg_arg,
2271 data_t *input,
2272 data_t *expected_mac )
2273{
Ronald Cron5425a212020-08-04 14:58:35 +02002274 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002275 psa_key_type_t key_type = key_type_arg;
2276 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002277 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002278 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002279 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002280 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002281 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002282 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002283 const size_t output_sizes_to_test[] = {
2284 0,
2285 1,
2286 expected_mac->len - 1,
2287 expected_mac->len,
2288 expected_mac->len + 1,
2289 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002290
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002291 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002292 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002293 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002294
Gilles Peskine8817f612018-12-18 00:18:46 +01002295 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002296
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002297 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002298 psa_set_key_algorithm( &attributes, alg );
2299 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002300
Ronald Cron5425a212020-08-04 14:58:35 +02002301 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2302 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002303
Gilles Peskine8b356b52020-08-25 23:44:59 +02002304 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2305 {
2306 const size_t output_size = output_sizes_to_test[i];
2307 psa_status_t expected_status =
2308 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2309 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002310
Chris Jones9634bb12021-01-20 15:56:42 +00002311 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002312 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002313
Gilles Peskine8b356b52020-08-25 23:44:59 +02002314 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002315 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002316 PSA_ASSERT( psa_mac_update( &operation,
2317 input->x, input->len ) );
2318 TEST_EQUAL( psa_mac_sign_finish( &operation,
2319 actual_mac, output_size,
2320 &mac_length ),
2321 expected_status );
2322 PSA_ASSERT( psa_mac_abort( &operation ) );
2323
2324 if( expected_status == PSA_SUCCESS )
2325 {
2326 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2327 actual_mac, mac_length );
2328 }
2329 mbedtls_free( actual_mac );
2330 actual_mac = NULL;
2331 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002332
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002333exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002334 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002335 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002336 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002337 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002338}
2339/* END_CASE */
2340
2341/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002342void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002343 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002344 int alg_arg,
2345 data_t *input,
2346 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002347{
Ronald Cron5425a212020-08-04 14:58:35 +02002348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002349 psa_key_type_t key_type = key_type_arg;
2350 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002351 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002353 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002354
Gilles Peskine69c12672018-06-28 00:07:19 +02002355 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2356
Gilles Peskine8817f612018-12-18 00:18:46 +01002357 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002358
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002359 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002360 psa_set_key_algorithm( &attributes, alg );
2361 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002362
Ronald Cron5425a212020-08-04 14:58:35 +02002363 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2364 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002365
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002366 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002368 PSA_ASSERT( psa_mac_update( &operation,
2369 input->x, input->len ) );
2370 PSA_ASSERT( psa_mac_verify_finish( &operation,
2371 expected_mac->x,
2372 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002373
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002374 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002375 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002376 PSA_ASSERT( psa_mac_update( &operation,
2377 input->x, input->len ) );
2378 TEST_EQUAL( psa_mac_verify_finish( &operation,
2379 expected_mac->x,
2380 expected_mac->len - 1 ),
2381 PSA_ERROR_INVALID_SIGNATURE );
2382
2383 /* Test a MAC that's too long. */
2384 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2385 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002386 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002387 PSA_ASSERT( psa_mac_update( &operation,
2388 input->x, input->len ) );
2389 TEST_EQUAL( psa_mac_verify_finish( &operation,
2390 perturbed_mac,
2391 expected_mac->len + 1 ),
2392 PSA_ERROR_INVALID_SIGNATURE );
2393
2394 /* Test changing one byte. */
2395 for( size_t i = 0; i < expected_mac->len; i++ )
2396 {
Chris Jones9634bb12021-01-20 15:56:42 +00002397 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002398 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002399 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002400 PSA_ASSERT( psa_mac_update( &operation,
2401 input->x, input->len ) );
2402 TEST_EQUAL( psa_mac_verify_finish( &operation,
2403 perturbed_mac,
2404 expected_mac->len ),
2405 PSA_ERROR_INVALID_SIGNATURE );
2406 perturbed_mac[i] ^= 1;
2407 }
2408
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002410 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002411 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002412 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002413 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414}
2415/* END_CASE */
2416
2417/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002418void cipher_operation_init( )
2419{
Jaeden Ameroab439972019-02-15 14:12:05 +00002420 const uint8_t input[1] = { 0 };
2421 unsigned char output[1] = { 0 };
2422 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002423 /* Test each valid way of initializing the object, except for `= {0}`, as
2424 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2425 * though it's OK by the C standard. We could test for this, but we'd need
2426 * to supress the Clang warning for the test. */
2427 psa_cipher_operation_t func = psa_cipher_operation_init( );
2428 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2429 psa_cipher_operation_t zero;
2430
2431 memset( &zero, 0, sizeof( zero ) );
2432
Jaeden Ameroab439972019-02-15 14:12:05 +00002433 /* A freshly-initialized cipher operation should not be usable. */
2434 TEST_EQUAL( psa_cipher_update( &func,
2435 input, sizeof( input ),
2436 output, sizeof( output ),
2437 &output_length ),
2438 PSA_ERROR_BAD_STATE );
2439 TEST_EQUAL( psa_cipher_update( &init,
2440 input, sizeof( input ),
2441 output, sizeof( output ),
2442 &output_length ),
2443 PSA_ERROR_BAD_STATE );
2444 TEST_EQUAL( psa_cipher_update( &zero,
2445 input, sizeof( input ),
2446 output, sizeof( output ),
2447 &output_length ),
2448 PSA_ERROR_BAD_STATE );
2449
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002450 /* A default cipher operation should be abortable without error. */
2451 PSA_ASSERT( psa_cipher_abort( &func ) );
2452 PSA_ASSERT( psa_cipher_abort( &init ) );
2453 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002454}
2455/* END_CASE */
2456
2457/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002458void cipher_setup( int key_type_arg,
2459 data_t *key,
2460 int alg_arg,
2461 int expected_status_arg )
2462{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002463 psa_key_type_t key_type = key_type_arg;
2464 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002465 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002466 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002467 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002468#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002469 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2470#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471
Gilles Peskine8817f612018-12-18 00:18:46 +01002472 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002473
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002474 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2475 &operation, &status ) )
2476 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002477 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002478
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002479 /* The operation object should be reusable. */
2480#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2481 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2482 smoke_test_key_data,
2483 sizeof( smoke_test_key_data ),
2484 KNOWN_SUPPORTED_CIPHER_ALG,
2485 &operation, &status ) )
2486 goto exit;
2487 TEST_EQUAL( status, PSA_SUCCESS );
2488#endif
2489
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002490exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002491 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002492 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002493}
2494/* END_CASE */
2495
Ronald Cronee414c72021-03-18 18:50:08 +01002496/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002497void cipher_bad_order( )
2498{
Ronald Cron5425a212020-08-04 14:58:35 +02002499 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002500 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2501 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002503 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002504 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002505 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002506 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2507 0xaa, 0xaa, 0xaa, 0xaa };
2508 const uint8_t text[] = {
2509 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2510 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002511 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002512 size_t length = 0;
2513
2514 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002515 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2516 psa_set_key_algorithm( &attributes, alg );
2517 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002518 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2519 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002520
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002521 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002522 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2523 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002524 PSA_ERROR_BAD_STATE );
2525 PSA_ASSERT( psa_cipher_abort( &operation ) );
2526
2527 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002528 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2529 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002530 PSA_ERROR_BAD_STATE );
2531 PSA_ASSERT( psa_cipher_abort( &operation ) );
2532
Jaeden Ameroab439972019-02-15 14:12:05 +00002533 /* Generate an IV without calling setup beforehand. */
2534 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2535 buffer, sizeof( buffer ),
2536 &length ),
2537 PSA_ERROR_BAD_STATE );
2538 PSA_ASSERT( psa_cipher_abort( &operation ) );
2539
2540 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002541 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002542 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2543 buffer, sizeof( buffer ),
2544 &length ) );
2545 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2546 buffer, sizeof( buffer ),
2547 &length ),
2548 PSA_ERROR_BAD_STATE );
2549 PSA_ASSERT( psa_cipher_abort( &operation ) );
2550
2551 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002552 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002553 PSA_ASSERT( psa_cipher_set_iv( &operation,
2554 iv, sizeof( iv ) ) );
2555 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2556 buffer, sizeof( buffer ),
2557 &length ),
2558 PSA_ERROR_BAD_STATE );
2559 PSA_ASSERT( psa_cipher_abort( &operation ) );
2560
2561 /* Set an IV without calling setup beforehand. */
2562 TEST_EQUAL( psa_cipher_set_iv( &operation,
2563 iv, sizeof( iv ) ),
2564 PSA_ERROR_BAD_STATE );
2565 PSA_ASSERT( psa_cipher_abort( &operation ) );
2566
2567 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002568 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002569 PSA_ASSERT( psa_cipher_set_iv( &operation,
2570 iv, sizeof( iv ) ) );
2571 TEST_EQUAL( psa_cipher_set_iv( &operation,
2572 iv, sizeof( iv ) ),
2573 PSA_ERROR_BAD_STATE );
2574 PSA_ASSERT( psa_cipher_abort( &operation ) );
2575
2576 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002577 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002578 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2579 buffer, sizeof( buffer ),
2580 &length ) );
2581 TEST_EQUAL( psa_cipher_set_iv( &operation,
2582 iv, sizeof( iv ) ),
2583 PSA_ERROR_BAD_STATE );
2584 PSA_ASSERT( psa_cipher_abort( &operation ) );
2585
2586 /* Call update without calling setup beforehand. */
2587 TEST_EQUAL( psa_cipher_update( &operation,
2588 text, sizeof( text ),
2589 buffer, sizeof( buffer ),
2590 &length ),
2591 PSA_ERROR_BAD_STATE );
2592 PSA_ASSERT( psa_cipher_abort( &operation ) );
2593
2594 /* Call update without an IV where an IV is required. */
2595 TEST_EQUAL( psa_cipher_update( &operation,
2596 text, sizeof( text ),
2597 buffer, sizeof( buffer ),
2598 &length ),
2599 PSA_ERROR_BAD_STATE );
2600 PSA_ASSERT( psa_cipher_abort( &operation ) );
2601
2602 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002603 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002604 PSA_ASSERT( psa_cipher_set_iv( &operation,
2605 iv, sizeof( iv ) ) );
2606 PSA_ASSERT( psa_cipher_finish( &operation,
2607 buffer, sizeof( buffer ), &length ) );
2608 TEST_EQUAL( psa_cipher_update( &operation,
2609 text, sizeof( text ),
2610 buffer, sizeof( buffer ),
2611 &length ),
2612 PSA_ERROR_BAD_STATE );
2613 PSA_ASSERT( psa_cipher_abort( &operation ) );
2614
2615 /* Call finish without calling setup beforehand. */
2616 TEST_EQUAL( psa_cipher_finish( &operation,
2617 buffer, sizeof( buffer ), &length ),
2618 PSA_ERROR_BAD_STATE );
2619 PSA_ASSERT( psa_cipher_abort( &operation ) );
2620
2621 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002622 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002623 /* Not calling update means we are encrypting an empty buffer, which is OK
2624 * for cipher modes with padding. */
2625 TEST_EQUAL( psa_cipher_finish( &operation,
2626 buffer, sizeof( buffer ), &length ),
2627 PSA_ERROR_BAD_STATE );
2628 PSA_ASSERT( psa_cipher_abort( &operation ) );
2629
2630 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002631 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002632 PSA_ASSERT( psa_cipher_set_iv( &operation,
2633 iv, sizeof( iv ) ) );
2634 PSA_ASSERT( psa_cipher_finish( &operation,
2635 buffer, sizeof( buffer ), &length ) );
2636 TEST_EQUAL( psa_cipher_finish( &operation,
2637 buffer, sizeof( buffer ), &length ),
2638 PSA_ERROR_BAD_STATE );
2639 PSA_ASSERT( psa_cipher_abort( &operation ) );
2640
Ronald Cron5425a212020-08-04 14:58:35 +02002641 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002642
Jaeden Ameroab439972019-02-15 14:12:05 +00002643exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002644 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002645 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002646}
2647/* END_CASE */
2648
2649/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002650void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002651 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002652 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002653 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654{
Ronald Cron5425a212020-08-04 14:58:35 +02002655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002656 psa_status_t status;
2657 psa_key_type_t key_type = key_type_arg;
2658 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002659 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002660 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661 size_t output_buffer_size = 0;
2662 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002663 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002664 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666
Gilles Peskine8817f612018-12-18 00:18:46 +01002667 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002668
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2670 psa_set_key_algorithm( &attributes, alg );
2671 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002672
Ronald Cron5425a212020-08-04 14:58:35 +02002673 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2674 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675
Ronald Cron5425a212020-08-04 14:58:35 +02002676 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002677
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002678 if( iv->len > 0 )
2679 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002680 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002681 }
2682
gabor-mezei-armceface22021-01-21 12:26:17 +01002683 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2684 TEST_ASSERT( output_buffer_size <=
2685 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002686 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002687
Gilles Peskine8817f612018-12-18 00:18:46 +01002688 PSA_ASSERT( psa_cipher_update( &operation,
2689 input->x, input->len,
2690 output, output_buffer_size,
2691 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002692 TEST_ASSERT( function_output_length <=
2693 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2694 TEST_ASSERT( function_output_length <=
2695 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002696 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002697
Gilles Peskine50e586b2018-06-08 14:28:46 +02002698 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002699 ( output_buffer_size == 0 ? NULL :
2700 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002701 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002702 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002703 TEST_ASSERT( function_output_length <=
2704 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2705 TEST_ASSERT( function_output_length <=
2706 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002707 total_output_length += function_output_length;
2708
Gilles Peskinefe11b722018-12-18 00:24:04 +01002709 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710 if( expected_status == PSA_SUCCESS )
2711 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002713 ASSERT_COMPARE( expected_output->x, expected_output->len,
2714 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002716
Gilles Peskine50e586b2018-06-08 14:28:46 +02002717exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002718 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002719 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002720 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002721 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002722}
2723/* END_CASE */
2724
2725/* BEGIN_CASE */
2726void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002727 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002728 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002729 int first_part_size_arg,
2730 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002731 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002732{
Ronald Cron5425a212020-08-04 14:58:35 +02002733 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002734 psa_key_type_t key_type = key_type_arg;
2735 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002736 size_t first_part_size = first_part_size_arg;
2737 size_t output1_length = output1_length_arg;
2738 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002739 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740 size_t output_buffer_size = 0;
2741 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002742 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002743 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002745
Gilles Peskine8817f612018-12-18 00:18:46 +01002746 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002747
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002748 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2749 psa_set_key_algorithm( &attributes, alg );
2750 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002751
Ronald Cron5425a212020-08-04 14:58:35 +02002752 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2753 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002754
Ronald Cron5425a212020-08-04 14:58:35 +02002755 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002756
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002757 if( iv->len > 0 )
2758 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002759 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002760 }
2761
gabor-mezei-armceface22021-01-21 12:26:17 +01002762 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2763 TEST_ASSERT( output_buffer_size <=
2764 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002765 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002766
Gilles Peskinee0866522019-02-19 19:44:00 +01002767 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002768 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2769 output, output_buffer_size,
2770 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002771 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002772 TEST_ASSERT( function_output_length <=
2773 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2774 TEST_ASSERT( function_output_length <=
2775 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002776 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002777
Gilles Peskine8817f612018-12-18 00:18:46 +01002778 PSA_ASSERT( psa_cipher_update( &operation,
2779 input->x + first_part_size,
2780 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002781 ( output_buffer_size == 0 ? NULL :
2782 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002783 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002784 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002785 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002786 TEST_ASSERT( function_output_length <=
2787 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2788 alg,
2789 input->len - first_part_size ) );
2790 TEST_ASSERT( function_output_length <=
2791 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002792 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002793
Gilles Peskine8817f612018-12-18 00:18:46 +01002794 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002795 ( output_buffer_size == 0 ? NULL :
2796 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002797 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002799 TEST_ASSERT( function_output_length <=
2800 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2801 TEST_ASSERT( function_output_length <=
2802 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002803 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002804 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002806 ASSERT_COMPARE( expected_output->x, expected_output->len,
2807 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002808
2809exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002810 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002811 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002812 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002813 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002814}
2815/* END_CASE */
2816
2817/* BEGIN_CASE */
2818void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002819 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002820 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002821 int first_part_size_arg,
2822 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002823 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002824{
Ronald Cron5425a212020-08-04 14:58:35 +02002825 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002826 psa_key_type_t key_type = key_type_arg;
2827 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002828 size_t first_part_size = first_part_size_arg;
2829 size_t output1_length = output1_length_arg;
2830 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002831 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002832 size_t output_buffer_size = 0;
2833 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002834 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002835 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837
Gilles Peskine8817f612018-12-18 00:18:46 +01002838 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002840 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2841 psa_set_key_algorithm( &attributes, alg );
2842 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002843
Ronald Cron5425a212020-08-04 14:58:35 +02002844 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2845 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002846
Ronald Cron5425a212020-08-04 14:58:35 +02002847 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002848
Steven Cooreman177deba2020-09-07 17:14:14 +02002849 if( iv->len > 0 )
2850 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002851 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002852 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002853
gabor-mezei-armceface22021-01-21 12:26:17 +01002854 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2855 TEST_ASSERT( output_buffer_size <=
2856 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002857 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002858
Gilles Peskinee0866522019-02-19 19:44:00 +01002859 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002860 PSA_ASSERT( psa_cipher_update( &operation,
2861 input->x, first_part_size,
2862 output, output_buffer_size,
2863 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002864 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002865 TEST_ASSERT( function_output_length <=
2866 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2867 TEST_ASSERT( function_output_length <=
2868 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002869 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002870
Gilles Peskine8817f612018-12-18 00:18:46 +01002871 PSA_ASSERT( psa_cipher_update( &operation,
2872 input->x + first_part_size,
2873 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002874 ( output_buffer_size == 0 ? NULL :
2875 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002876 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002877 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002878 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002879 TEST_ASSERT( function_output_length <=
2880 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2881 alg,
2882 input->len - first_part_size ) );
2883 TEST_ASSERT( function_output_length <=
2884 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002885 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002886
Gilles Peskine8817f612018-12-18 00:18:46 +01002887 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002888 ( output_buffer_size == 0 ? NULL :
2889 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002890 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002891 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002892 TEST_ASSERT( function_output_length <=
2893 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2894 TEST_ASSERT( function_output_length <=
2895 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002896 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002897 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002899 ASSERT_COMPARE( expected_output->x, expected_output->len,
2900 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901
2902exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002903 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002904 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002905 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002906 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002907}
2908/* END_CASE */
2909
Gilles Peskine50e586b2018-06-08 14:28:46 +02002910/* BEGIN_CASE */
2911void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002912 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002913 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002914 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915{
Ronald Cron5425a212020-08-04 14:58:35 +02002916 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002917 psa_status_t status;
2918 psa_key_type_t key_type = key_type_arg;
2919 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002920 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002921 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922 size_t output_buffer_size = 0;
2923 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002924 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002925 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927
Gilles Peskine8817f612018-12-18 00:18:46 +01002928 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002930 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2931 psa_set_key_algorithm( &attributes, alg );
2932 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002933
Ronald Cron5425a212020-08-04 14:58:35 +02002934 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2935 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002936
Ronald Cron5425a212020-08-04 14:58:35 +02002937 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002938
Steven Cooreman177deba2020-09-07 17:14:14 +02002939 if( iv->len > 0 )
2940 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002941 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002942 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002943
gabor-mezei-armceface22021-01-21 12:26:17 +01002944 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2945 TEST_ASSERT( output_buffer_size <=
2946 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002947 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002948
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 PSA_ASSERT( psa_cipher_update( &operation,
2950 input->x, input->len,
2951 output, output_buffer_size,
2952 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002953 TEST_ASSERT( function_output_length <=
2954 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2955 TEST_ASSERT( function_output_length <=
2956 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002957 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002958
Gilles Peskine50e586b2018-06-08 14:28:46 +02002959 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002960 ( output_buffer_size == 0 ? NULL :
2961 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002962 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002964 TEST_ASSERT( function_output_length <=
2965 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2966 TEST_ASSERT( function_output_length <=
2967 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002968 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002969 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002970
2971 if( expected_status == PSA_SUCCESS )
2972 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002974 ASSERT_COMPARE( expected_output->x, expected_output->len,
2975 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976 }
2977
Gilles Peskine50e586b2018-06-08 14:28:46 +02002978exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002979 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002980 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002981 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002982 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002983}
2984/* END_CASE */
2985
Gilles Peskine50e586b2018-06-08 14:28:46 +02002986/* BEGIN_CASE */
2987void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002988 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002989 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002990{
Ronald Cron5425a212020-08-04 14:58:35 +02002991 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002992 psa_key_type_t key_type = key_type_arg;
2993 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002994 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002995 size_t iv_size = 16;
2996 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002997 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002998 size_t output1_size = 0;
2999 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003000 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003001 size_t output2_size = 0;
3002 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003003 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003004 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3005 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003007
Gilles Peskine8817f612018-12-18 00:18:46 +01003008 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003009
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003010 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3011 psa_set_key_algorithm( &attributes, alg );
3012 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003013
Ronald Cron5425a212020-08-04 14:58:35 +02003014 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3015 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003016
Ronald Cron5425a212020-08-04 14:58:35 +02003017 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3018 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003019
Steven Cooreman177deba2020-09-07 17:14:14 +02003020 if( alg != PSA_ALG_ECB_NO_PADDING )
3021 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003022 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3023 iv, iv_size,
3024 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003025 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003026 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3027 TEST_ASSERT( output1_size <=
3028 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003029 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003030
Gilles Peskine8817f612018-12-18 00:18:46 +01003031 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3032 output1, output1_size,
3033 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003034 TEST_ASSERT( output1_length <=
3035 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3036 TEST_ASSERT( output1_length <=
3037 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3038
Gilles Peskine8817f612018-12-18 00:18:46 +01003039 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003040 output1 + output1_length,
3041 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003042 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003043 TEST_ASSERT( function_output_length <=
3044 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3045 TEST_ASSERT( function_output_length <=
3046 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003047
Gilles Peskine048b7f02018-06-08 14:20:49 +02003048 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003049
Gilles Peskine8817f612018-12-18 00:18:46 +01003050 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003051
3052 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003053 TEST_ASSERT( output2_size <=
3054 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3055 TEST_ASSERT( output2_size <=
3056 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003057 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003058
Steven Cooreman177deba2020-09-07 17:14:14 +02003059 if( iv_length > 0 )
3060 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003061 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3062 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003063 }
3064
Gilles Peskine8817f612018-12-18 00:18:46 +01003065 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3066 output2, output2_size,
3067 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003068 TEST_ASSERT( output2_length <=
3069 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3070 TEST_ASSERT( output2_length <=
3071 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3072
Gilles Peskine048b7f02018-06-08 14:20:49 +02003073 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003074 PSA_ASSERT( psa_cipher_finish( &operation2,
3075 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003076 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003077 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003078 TEST_ASSERT( function_output_length <=
3079 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3080 TEST_ASSERT( function_output_length <=
3081 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003082
Gilles Peskine048b7f02018-06-08 14:20:49 +02003083 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003084
Gilles Peskine8817f612018-12-18 00:18:46 +01003085 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003086
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003087 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003088
3089exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003090 psa_cipher_abort( &operation1 );
3091 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003092 mbedtls_free( output1 );
3093 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003094 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003095 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003096}
3097/* END_CASE */
3098
3099/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003100void cipher_verify_output_multipart( int alg_arg,
3101 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003102 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003103 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003104 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003105{
Ronald Cron5425a212020-08-04 14:58:35 +02003106 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003107 psa_key_type_t key_type = key_type_arg;
3108 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003109 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003110 unsigned char iv[16] = {0};
3111 size_t iv_size = 16;
3112 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003113 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003114 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003115 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003116 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003117 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003118 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003119 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003120 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3121 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003123
Gilles Peskine8817f612018-12-18 00:18:46 +01003124 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003126 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3127 psa_set_key_algorithm( &attributes, alg );
3128 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003129
Ronald Cron5425a212020-08-04 14:58:35 +02003130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3131 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003132
Ronald Cron5425a212020-08-04 14:58:35 +02003133 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3134 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003135
Steven Cooreman177deba2020-09-07 17:14:14 +02003136 if( alg != PSA_ALG_ECB_NO_PADDING )
3137 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003138 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3139 iv, iv_size,
3140 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003141 }
3142
gabor-mezei-armceface22021-01-21 12:26:17 +01003143 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3144 TEST_ASSERT( output1_buffer_size <=
3145 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003146 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003147
Gilles Peskinee0866522019-02-19 19:44:00 +01003148 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003149
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3151 output1, output1_buffer_size,
3152 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003153 TEST_ASSERT( function_output_length <=
3154 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3155 TEST_ASSERT( function_output_length <=
3156 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003157 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003158
Gilles Peskine8817f612018-12-18 00:18:46 +01003159 PSA_ASSERT( psa_cipher_update( &operation1,
3160 input->x + first_part_size,
3161 input->len - first_part_size,
3162 output1, output1_buffer_size,
3163 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003164 TEST_ASSERT( function_output_length <=
3165 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3166 alg,
3167 input->len - first_part_size ) );
3168 TEST_ASSERT( function_output_length <=
3169 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003170 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003171
Gilles Peskine8817f612018-12-18 00:18:46 +01003172 PSA_ASSERT( psa_cipher_finish( &operation1,
3173 output1 + output1_length,
3174 output1_buffer_size - output1_length,
3175 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003176 TEST_ASSERT( function_output_length <=
3177 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3178 TEST_ASSERT( function_output_length <=
3179 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003180 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003181
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003183
Gilles Peskine048b7f02018-06-08 14:20:49 +02003184 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003185 TEST_ASSERT( output2_buffer_size <=
3186 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3187 TEST_ASSERT( output2_buffer_size <=
3188 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003189 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003190
Steven Cooreman177deba2020-09-07 17:14:14 +02003191 if( iv_length > 0 )
3192 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003193 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3194 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003195 }
Moran Pekerded84402018-06-06 16:36:50 +03003196
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3198 output2, output2_buffer_size,
3199 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003200 TEST_ASSERT( function_output_length <=
3201 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3202 TEST_ASSERT( function_output_length <=
3203 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003204 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003205
Gilles Peskine8817f612018-12-18 00:18:46 +01003206 PSA_ASSERT( psa_cipher_update( &operation2,
3207 output1 + first_part_size,
3208 output1_length - first_part_size,
3209 output2, output2_buffer_size,
3210 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003211 TEST_ASSERT( function_output_length <=
3212 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3213 alg,
3214 output1_length - first_part_size ) );
3215 TEST_ASSERT( function_output_length <=
3216 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003217 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003218
Gilles Peskine8817f612018-12-18 00:18:46 +01003219 PSA_ASSERT( psa_cipher_finish( &operation2,
3220 output2 + output2_length,
3221 output2_buffer_size - output2_length,
3222 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003223 TEST_ASSERT( function_output_length <=
3224 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3225 TEST_ASSERT( function_output_length <=
3226 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003227 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003228
Gilles Peskine8817f612018-12-18 00:18:46 +01003229 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003230
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003231 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003232
3233exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003234 psa_cipher_abort( &operation1 );
3235 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003236 mbedtls_free( output1 );
3237 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003239 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003240}
3241/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003242
Gilles Peskine20035e32018-02-03 22:44:14 +01003243/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003244void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003245 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003246 data_t *nonce,
3247 data_t *additional_data,
3248 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003249 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003250{
Ronald Cron5425a212020-08-04 14:58:35 +02003251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003252 psa_key_type_t key_type = key_type_arg;
3253 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003254 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003255 unsigned char *output_data = NULL;
3256 size_t output_size = 0;
3257 size_t output_length = 0;
3258 unsigned char *output_data2 = NULL;
3259 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003260 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003261 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003262 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003263
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003265
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003266 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3267 psa_set_key_algorithm( &attributes, alg );
3268 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003269
Gilles Peskine049c7532019-05-15 20:22:09 +02003270 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003271 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003272 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3273 key_bits = psa_get_key_bits( &attributes );
3274
3275 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3276 alg );
3277 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3278 * should be exact. */
3279 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3280 expected_result != PSA_ERROR_NOT_SUPPORTED )
3281 {
3282 TEST_EQUAL( output_size,
3283 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3284 TEST_ASSERT( output_size <=
3285 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3286 }
3287 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003288
Steven Cooremanf49478b2021-02-15 15:19:25 +01003289 status = psa_aead_encrypt( key, alg,
3290 nonce->x, nonce->len,
3291 additional_data->x,
3292 additional_data->len,
3293 input_data->x, input_data->len,
3294 output_data, output_size,
3295 &output_length );
3296
3297 /* If the operation is not supported, just skip and not fail in case the
3298 * encryption involves a common limitation of cryptography hardwares and
3299 * an alternative implementation. */
3300 if( status == PSA_ERROR_NOT_SUPPORTED )
3301 {
3302 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3303 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3304 }
3305
3306 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307
3308 if( PSA_SUCCESS == expected_result )
3309 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003310 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311
Gilles Peskine003a4a92019-05-14 16:09:40 +02003312 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3313 * should be exact. */
3314 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003315 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003316
gabor-mezei-armceface22021-01-21 12:26:17 +01003317 TEST_ASSERT( input_data->len <=
3318 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3319
Ronald Cron5425a212020-08-04 14:58:35 +02003320 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003321 nonce->x, nonce->len,
3322 additional_data->x,
3323 additional_data->len,
3324 output_data, output_length,
3325 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003326 &output_length2 ),
3327 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003328
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003329 ASSERT_COMPARE( input_data->x, input_data->len,
3330 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003331 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003332
Gilles Peskinea1cac842018-06-11 19:33:02 +02003333exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003334 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003335 mbedtls_free( output_data );
3336 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003337 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003338}
3339/* END_CASE */
3340
3341/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003342void aead_encrypt( int key_type_arg, data_t *key_data,
3343 int alg_arg,
3344 data_t *nonce,
3345 data_t *additional_data,
3346 data_t *input_data,
3347 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003348{
Ronald Cron5425a212020-08-04 14:58:35 +02003349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003350 psa_key_type_t key_type = key_type_arg;
3351 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003352 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003353 unsigned char *output_data = NULL;
3354 size_t output_size = 0;
3355 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003357 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003358
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003360
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3362 psa_set_key_algorithm( &attributes, alg );
3363 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Gilles Peskine049c7532019-05-15 20:22:09 +02003365 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003366 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003367 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3368 key_bits = psa_get_key_bits( &attributes );
3369
3370 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3371 alg );
3372 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3373 * should be exact. */
3374 TEST_EQUAL( output_size,
3375 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3376 TEST_ASSERT( output_size <=
3377 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3378 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003379
Steven Cooremand588ea12021-01-11 19:36:04 +01003380 status = psa_aead_encrypt( key, alg,
3381 nonce->x, nonce->len,
3382 additional_data->x, additional_data->len,
3383 input_data->x, input_data->len,
3384 output_data, output_size,
3385 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003386
Ronald Cron28a45ed2021-02-09 20:35:42 +01003387 /* If the operation is not supported, just skip and not fail in case the
3388 * encryption involves a common limitation of cryptography hardwares and
3389 * an alternative implementation. */
3390 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003391 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003392 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3393 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003394 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003395
3396 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003397 ASSERT_COMPARE( expected_result->x, expected_result->len,
3398 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003399
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003401 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003403 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404}
3405/* END_CASE */
3406
3407/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003408void aead_decrypt( int key_type_arg, data_t *key_data,
3409 int alg_arg,
3410 data_t *nonce,
3411 data_t *additional_data,
3412 data_t *input_data,
3413 data_t *expected_data,
3414 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003415{
Ronald Cron5425a212020-08-04 14:58:35 +02003416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417 psa_key_type_t key_type = key_type_arg;
3418 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003419 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420 unsigned char *output_data = NULL;
3421 size_t output_size = 0;
3422 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003423 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003424 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003425 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003426
Gilles Peskine8817f612018-12-18 00:18:46 +01003427 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003428
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003429 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3430 psa_set_key_algorithm( &attributes, alg );
3431 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003432
Gilles Peskine049c7532019-05-15 20:22:09 +02003433 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003434 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003435 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3436 key_bits = psa_get_key_bits( &attributes );
3437
3438 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3439 alg );
3440 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3441 expected_result != PSA_ERROR_NOT_SUPPORTED )
3442 {
3443 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3444 * should be exact. */
3445 TEST_EQUAL( output_size,
3446 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3447 TEST_ASSERT( output_size <=
3448 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3449 }
3450 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003451
Steven Cooremand588ea12021-01-11 19:36:04 +01003452 status = psa_aead_decrypt( key, alg,
3453 nonce->x, nonce->len,
3454 additional_data->x,
3455 additional_data->len,
3456 input_data->x, input_data->len,
3457 output_data, output_size,
3458 &output_length );
3459
Ronald Cron28a45ed2021-02-09 20:35:42 +01003460 /* If the operation is not supported, just skip and not fail in case the
3461 * decryption involves a common limitation of cryptography hardwares and
3462 * an alternative implementation. */
3463 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003464 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003465 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3466 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003467 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003468
3469 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470
Gilles Peskine2d277862018-06-18 15:41:12 +02003471 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003472 ASSERT_COMPARE( expected_data->x, expected_data->len,
3473 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003476 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003477 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003478 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479}
3480/* END_CASE */
3481
3482/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003483void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3484 int alg_arg,
3485 data_t *nonce,
3486 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003487 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003488 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003489 int do_test_data_chunked,
3490 int do_set_lengths,
3491 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003492{
Paul Elliottd3f82412021-06-16 16:52:21 +01003493 size_t ad_part_len = 0;
3494 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003495 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003496
Paul Elliotte64deda2021-09-09 14:07:23 +01003497 /* Ensure that either one part of the test or the other is done, i.e this
3498 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003499 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3500
3501 /* Temporary whilst we have algorithms that cannot support chunking */
3502 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003503 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003504 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3505 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003506 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003507 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003508
Paul Elliott33746aa2021-09-15 16:40:40 +01003509 if( do_set_lengths )
3510 {
3511 if( ad_part_len & 0x01 )
3512 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3513 else
3514 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3515 }
3516
Paul Elliott329d5382021-07-22 17:10:45 +01003517 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003518 if( !aead_multipart_internal_func( key_type_arg, key_data,
3519 alg_arg, nonce,
3520 additional_data,
3521 ad_part_len,
3522 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003523 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003524 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003525 1, 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003526 break;
3527
3528 /* length(0) part, length(ad_part_len) part, length(0) part... */
3529 mbedtls_test_set_step( 1000 + ad_part_len );
3530
3531 if( !aead_multipart_internal_func( key_type_arg, key_data,
3532 alg_arg, nonce,
3533 additional_data,
3534 ad_part_len,
3535 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003536 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003537 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003538 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003539 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003540 }
3541 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003542
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003543 /* Temporary whilst we have algorithms that cannot support chunking */
3544 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003545 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003546 for( data_part_len = 1; data_part_len <= input_data->len;
3547 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003548 {
Paul Elliott329d5382021-07-22 17:10:45 +01003549 /* Split data into length(data_part_len) parts. */
3550 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003551
Paul Elliott33746aa2021-09-15 16:40:40 +01003552 if( do_set_lengths )
3553 {
3554 if( data_part_len & 0x01 )
3555 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3556 else
3557 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3558 }
3559
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003560 if( !aead_multipart_internal_func( key_type_arg, key_data,
3561 alg_arg, nonce,
3562 additional_data, -1,
3563 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003564 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003565 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003566 1, 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003567 break;
3568
3569 /* length(0) part, length(data_part_len) part, length(0) part... */
3570 mbedtls_test_set_step( 3000 + data_part_len );
3571
3572 if( !aead_multipart_internal_func( key_type_arg, key_data,
3573 alg_arg, nonce,
3574 additional_data, -1,
3575 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003576 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003577 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003578 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003579 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003580 }
3581 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582
Paul Elliott0023e0a2021-04-27 10:06:22 +01003583
Paul Elliott8fc45162021-06-23 16:06:01 +01003584 /* Goto is required to silence warnings about unused labels, as we
3585 * don't actually do any test assertions in this function. */
3586 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003587}
3588/* END_CASE */
3589
3590/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003591void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3592 int alg_arg,
3593 data_t *nonce,
3594 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003595 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003596 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003597 int do_test_data_chunked,
3598 int do_set_lengths,
3599 data_t *expected_output,
3600 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003601{
Paul Elliottd3f82412021-06-16 16:52:21 +01003602 size_t ad_part_len = 0;
3603 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003604 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003605
Paul Elliotte64deda2021-09-09 14:07:23 +01003606 /* Ensure that either one part of the test or the other is done, i.e this
3607 * test does something. */
3608 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3609
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003610 /* Temporary whilst we have algorithms that cannot support chunking */
3611 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003612 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003613 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3614 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003615 {
Paul Elliott329d5382021-07-22 17:10:45 +01003616 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003617 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003618
Paul Elliott33746aa2021-09-15 16:40:40 +01003619 if( do_set_lengths )
3620 {
3621 if( ad_part_len & 0x01 )
3622 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3623 else
3624 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3625 }
3626
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003627 if( !aead_multipart_internal_func( key_type_arg, key_data,
3628 alg_arg, nonce,
3629 additional_data,
3630 ad_part_len,
3631 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003632 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003633 expected_output,
3634 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003635 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003636 break;
3637
3638 /* length(0) part, length(ad_part_len) part, length(0) part... */
3639 mbedtls_test_set_step( 1000 + ad_part_len );
3640
3641 if( !aead_multipart_internal_func( key_type_arg, key_data,
3642 alg_arg, nonce,
3643 additional_data,
3644 ad_part_len,
3645 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003646 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003647 expected_output,
3648 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003649 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003650 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003651 }
3652 }
3653
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003654 /* Temporary whilst we have algorithms that cannot support chunking */
3655 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003656 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003657 for( data_part_len = 1; data_part_len <= input_data->len;
3658 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003659 {
Paul Elliott329d5382021-07-22 17:10:45 +01003660 /* Split data into length(data_part_len) parts. */
3661 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003662
Paul Elliott33746aa2021-09-15 16:40:40 +01003663 if( do_set_lengths )
3664 {
3665 if( data_part_len & 0x01 )
3666 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3667 else
3668 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3669 }
3670
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003671 if( !aead_multipart_internal_func( key_type_arg, key_data,
3672 alg_arg, nonce,
3673 additional_data, -1,
3674 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003675 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003676 expected_output,
3677 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003678 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003679 break;
3680
3681 /* length(0) part, length(data_part_len) part, length(0) part... */
3682 mbedtls_test_set_step( 3000 + data_part_len );
3683
3684 if( !aead_multipart_internal_func( key_type_arg, key_data,
3685 alg_arg, nonce,
3686 additional_data, -1,
3687 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003688 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003689 expected_output,
3690 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003691 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003692 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003693 }
3694 }
3695
Paul Elliott8fc45162021-06-23 16:06:01 +01003696 /* Goto is required to silence warnings about unused labels, as we
3697 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003698 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003699}
3700/* END_CASE */
3701
3702/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003703void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3704 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003705 int nonce_length,
3706 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003707 data_t *additional_data,
3708 data_t *input_data,
3709 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003710{
3711
3712 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3713 psa_key_type_t key_type = key_type_arg;
3714 psa_algorithm_t alg = alg_arg;
3715 psa_aead_operation_t operation;
3716 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3718 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003719 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003720 size_t actual_nonce_length = 0;
3721 size_t expected_nonce_length = expected_nonce_length_arg;
3722 unsigned char *output = NULL;
3723 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003724 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003725 size_t ciphertext_size = 0;
3726 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003727 size_t tag_length = 0;
3728 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003729
3730 PSA_ASSERT( psa_crypto_init( ) );
3731
3732 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3733 psa_set_key_algorithm( & attributes, alg );
3734 psa_set_key_type( & attributes, key_type );
3735
3736 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3737 &key ) );
3738
3739 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3740
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003741 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3742
Paul Elliottf1277632021-08-24 18:11:37 +01003743 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003744
Paul Elliottf1277632021-08-24 18:11:37 +01003745 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003746
Paul Elliottf1277632021-08-24 18:11:37 +01003747 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003748
Paul Elliottf1277632021-08-24 18:11:37 +01003749 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003750
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003751 operation = psa_aead_operation_init( );
3752
3753 status = psa_aead_encrypt_setup( &operation, key, alg );
3754
3755 /* If the operation is not supported, just skip and not fail in case the
3756 * encryption involves a common limitation of cryptography hardwares and
3757 * an alternative implementation. */
3758 if( status == PSA_ERROR_NOT_SUPPORTED )
3759 {
3760 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003761 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003762 }
3763
3764 PSA_ASSERT( status );
3765
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003766 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003767 nonce_length,
3768 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003769
Paul Elliott693bf312021-07-23 17:40:41 +01003770 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003771
Paul Elliottf1277632021-08-24 18:11:37 +01003772 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003773
Paul Elliottf1277632021-08-24 18:11:37 +01003774 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003775
Paul Elliott693bf312021-07-23 17:40:41 +01003776 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003777 {
3778
3779 /* Ensure we can still complete operation. */
3780
3781 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3782 additional_data->len ) );
3783
3784 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003785 output, output_size,
3786 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003787
Paul Elliottf1277632021-08-24 18:11:37 +01003788 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3789 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003790 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3791 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003792
3793exit:
3794 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003795 mbedtls_free( output );
3796 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003797 psa_aead_abort( &operation );
3798 PSA_DONE( );
3799}
3800/* END_CASE */
3801
3802/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003803void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3804 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003805 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003806 data_t *additional_data,
3807 data_t *input_data,
3808 int expected_status_arg )
3809{
3810
3811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3812 psa_key_type_t key_type = key_type_arg;
3813 psa_algorithm_t alg = alg_arg;
3814 psa_aead_operation_t operation;
3815 uint8_t *nonce_buffer = NULL;
3816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3817 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3818 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003819 unsigned char *output = NULL;
3820 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003821 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003822 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003823 size_t ciphertext_size = 0;
3824 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003825 size_t tag_length = 0;
3826 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003827 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003828
3829 PSA_ASSERT( psa_crypto_init( ) );
3830
3831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3832 psa_set_key_algorithm( &attributes, alg );
3833 psa_set_key_type( &attributes, key_type );
3834
3835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3836 &key ) );
3837
3838 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3839
3840 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3841
Paul Elliott6f0e7202021-08-25 12:57:18 +01003842 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003843
Paul Elliott6f0e7202021-08-25 12:57:18 +01003844 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003845
Paul Elliott6f0e7202021-08-25 12:57:18 +01003846 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003847
Paul Elliott6f0e7202021-08-25 12:57:18 +01003848 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003849
3850 operation = psa_aead_operation_init( );
3851
3852 status = psa_aead_encrypt_setup( &operation, key, alg );
3853
3854 /* If the operation is not supported, just skip and not fail in case the
3855 * encryption involves a common limitation of cryptography hardwares and
3856 * an alternative implementation. */
3857 if( status == PSA_ERROR_NOT_SUPPORTED )
3858 {
3859 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003860 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003861 }
3862
3863 PSA_ASSERT( status );
3864
Paul Elliott4023ffd2021-09-10 16:21:22 +01003865 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3866 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003867 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003868 /* Arbitrary size buffer, to test zero length valid buffer. */
3869 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003870 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003871 }
3872 else
3873 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003874 /* If length is zero, then this will return NULL. */
3875 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003876 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003877
Paul Elliott4023ffd2021-09-10 16:21:22 +01003878 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003879 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003880 for( index = 0; index < nonce_length - 1; ++index )
3881 {
3882 nonce_buffer[index] = 'a' + index;
3883 }
Paul Elliott66696b52021-08-16 18:42:41 +01003884 }
Paul Elliott863864a2021-07-23 17:28:31 +01003885 }
3886
Paul Elliott6f0e7202021-08-25 12:57:18 +01003887 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003888
Paul Elliott693bf312021-07-23 17:40:41 +01003889 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003890
3891 if( expected_status == PSA_SUCCESS )
3892 {
3893 /* Ensure we can still complete operation. */
3894
3895 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3896 additional_data->len ) );
3897
3898 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003899 output, output_size,
3900 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003901
Paul Elliott6f0e7202021-08-25 12:57:18 +01003902 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3903 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003904 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3905 }
3906
3907exit:
3908 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003909 mbedtls_free( output );
3910 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003911 mbedtls_free( nonce_buffer );
3912 psa_aead_abort( &operation );
3913 PSA_DONE( );
3914}
3915/* END_CASE */
3916
3917/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003918void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3919 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003920 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003921 data_t *nonce,
3922 data_t *additional_data,
3923 data_t *input_data,
3924 int expected_status_arg )
3925{
3926
3927 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3928 psa_key_type_t key_type = key_type_arg;
3929 psa_algorithm_t alg = alg_arg;
3930 psa_aead_operation_t operation;
3931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3932 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3933 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003934 unsigned char *output = NULL;
3935 unsigned char *ciphertext = NULL;
3936 size_t output_size = output_size_arg;
3937 size_t ciphertext_size = 0;
3938 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003939 size_t tag_length = 0;
3940 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3941
3942 PSA_ASSERT( psa_crypto_init( ) );
3943
3944 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3945 psa_set_key_algorithm( &attributes, alg );
3946 psa_set_key_type( &attributes, key_type );
3947
3948 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3949 &key ) );
3950
3951 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3952
Paul Elliottc6d11d02021-09-01 12:04:23 +01003953 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003954
Paul Elliottc6d11d02021-09-01 12:04:23 +01003955 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003956
Paul Elliottc6d11d02021-09-01 12:04:23 +01003957 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003958
3959 operation = psa_aead_operation_init( );
3960
3961 status = psa_aead_encrypt_setup( &operation, key, alg );
3962
3963 /* If the operation is not supported, just skip and not fail in case the
3964 * encryption involves a common limitation of cryptography hardwares and
3965 * an alternative implementation. */
3966 if( status == PSA_ERROR_NOT_SUPPORTED )
3967 {
3968 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3969 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3970 }
3971
3972 PSA_ASSERT( status );
3973
3974 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3975
3976 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3977 additional_data->len ) );
3978
3979 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003980 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003981
3982 TEST_EQUAL( status, expected_status );
3983
3984 if( expected_status == PSA_SUCCESS )
3985 {
3986 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003987 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3988 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003989 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3990 }
3991
3992exit:
3993 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003994 mbedtls_free( output );
3995 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003996 psa_aead_abort( &operation );
3997 PSA_DONE( );
3998}
3999/* END_CASE */
4000
Paul Elliott91b021e2021-07-23 18:52:31 +01004001/* BEGIN_CASE */
4002void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4003 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004004 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004005 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004006 data_t *nonce,
4007 data_t *additional_data,
4008 data_t *input_data,
4009 int expected_status_arg )
4010{
4011
4012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4013 psa_key_type_t key_type = key_type_arg;
4014 psa_algorithm_t alg = alg_arg;
4015 psa_aead_operation_t operation;
4016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4017 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4018 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004019 unsigned char *ciphertext = NULL;
4020 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004021 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004022 size_t ciphertext_size = 0;
4023 size_t ciphertext_length = 0;
4024 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004025 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004026 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004027
4028 PSA_ASSERT( psa_crypto_init( ) );
4029
4030 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4031 psa_set_key_algorithm( &attributes, alg );
4032 psa_set_key_type( &attributes, key_type );
4033
4034 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4035 &key ) );
4036
4037 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4038
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004039 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004040
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004041 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004042
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004043 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004044
Paul Elliott719c1322021-09-13 18:27:22 +01004045 ASSERT_ALLOC( tag_buffer, tag_size );
4046
Paul Elliott91b021e2021-07-23 18:52:31 +01004047 operation = psa_aead_operation_init( );
4048
4049 status = psa_aead_encrypt_setup( &operation, key, alg );
4050
4051 /* If the operation is not supported, just skip and not fail in case the
4052 * encryption involves a common limitation of cryptography hardwares and
4053 * an alternative implementation. */
4054 if( status == PSA_ERROR_NOT_SUPPORTED )
4055 {
4056 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4057 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4058 }
4059
4060 PSA_ASSERT( status );
4061
4062 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4063
4064 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4065 additional_data->len ) );
4066
4067 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004068 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004069
4070 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004071 status = psa_aead_finish( &operation, finish_ciphertext,
4072 finish_ciphertext_size,
4073 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004074 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004075
4076 TEST_EQUAL( status, expected_status );
4077
4078exit:
4079 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004080 mbedtls_free( ciphertext );
4081 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004082 psa_aead_abort( &operation );
4083 PSA_DONE( );
4084}
4085/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004086
4087/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004088void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4089 int alg_arg,
4090 data_t *nonce,
4091 data_t *additional_data,
4092 data_t *input_data )
4093{
4094 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4095 psa_key_type_t key_type = key_type_arg;
4096 psa_algorithm_t alg = alg_arg;
4097 psa_aead_operation_t operation;
4098 unsigned char *output_data = NULL;
4099 unsigned char *final_data = NULL;
4100 size_t output_size = 0;
4101 size_t finish_output_size = 0;
4102 size_t output_length = 0;
4103 size_t key_bits = 0;
4104 size_t tag_length = 0;
4105 size_t tag_size = 0;
4106 size_t nonce_length = 0;
4107 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4108 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4109 size_t output_part_length = 0;
4110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4111
4112 PSA_ASSERT( psa_crypto_init( ) );
4113
4114 psa_set_key_usage_flags( & attributes,
4115 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4116 psa_set_key_algorithm( & attributes, alg );
4117 psa_set_key_type( & attributes, key_type );
4118
4119 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4120 &key ) );
4121
4122 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4123 key_bits = psa_get_key_bits( &attributes );
4124
4125 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4126
4127 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4128
4129 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4130
4131 ASSERT_ALLOC( output_data, output_size );
4132
4133 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4134
4135 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4136
4137 ASSERT_ALLOC( final_data, finish_output_size );
4138
4139 /* Test all operations error without calling setup first. */
4140
4141 operation = psa_aead_operation_init( );
4142
4143 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4144 PSA_ERROR_BAD_STATE );
4145
4146 psa_aead_abort( &operation );
4147
4148 operation = psa_aead_operation_init( );
4149
4150 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4151 PSA_AEAD_NONCE_MAX_SIZE,
4152 &nonce_length ),
4153 PSA_ERROR_BAD_STATE );
4154
4155 psa_aead_abort( &operation );
4156
Paul Elliott481be342021-07-16 17:38:47 +01004157 /* ------------------------------------------------------- */
4158
Paul Elliottc23a9a02021-06-21 18:32:46 +01004159 operation = psa_aead_operation_init( );
4160
4161 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4162 input_data->len ),
4163 PSA_ERROR_BAD_STATE );
4164
4165 psa_aead_abort( &operation );
4166
Paul Elliott481be342021-07-16 17:38:47 +01004167 /* ------------------------------------------------------- */
4168
Paul Elliottc23a9a02021-06-21 18:32:46 +01004169 operation = psa_aead_operation_init( );
4170
4171 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4172 additional_data->len ),
4173 PSA_ERROR_BAD_STATE );
4174
4175 psa_aead_abort( &operation );
4176
Paul Elliott481be342021-07-16 17:38:47 +01004177 /* ------------------------------------------------------- */
4178
Paul Elliottc23a9a02021-06-21 18:32:46 +01004179 operation = psa_aead_operation_init( );
4180
4181 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4182 input_data->len, output_data,
4183 output_size, &output_length ),
4184 PSA_ERROR_BAD_STATE );
4185
4186 psa_aead_abort( &operation );
4187
Paul Elliott481be342021-07-16 17:38:47 +01004188 /* ------------------------------------------------------- */
4189
Paul Elliottc23a9a02021-06-21 18:32:46 +01004190 operation = psa_aead_operation_init( );
4191
4192 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4193 finish_output_size,
4194 &output_part_length,
4195 tag_buffer, tag_length,
4196 &tag_size ),
4197 PSA_ERROR_BAD_STATE );
4198
4199 psa_aead_abort( &operation );
4200
Paul Elliott481be342021-07-16 17:38:47 +01004201 /* ------------------------------------------------------- */
4202
Paul Elliottc23a9a02021-06-21 18:32:46 +01004203 operation = psa_aead_operation_init( );
4204
4205 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4206 finish_output_size,
4207 &output_part_length,
4208 tag_buffer,
4209 tag_length ),
4210 PSA_ERROR_BAD_STATE );
4211
4212 psa_aead_abort( &operation );
4213
4214 /* Test for double setups. */
4215
4216 operation = psa_aead_operation_init( );
4217
4218 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4219
4220 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4221 PSA_ERROR_BAD_STATE );
4222
4223 psa_aead_abort( &operation );
4224
Paul Elliott481be342021-07-16 17:38:47 +01004225 /* ------------------------------------------------------- */
4226
Paul Elliottc23a9a02021-06-21 18:32:46 +01004227 operation = psa_aead_operation_init( );
4228
4229 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4230
4231 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4232 PSA_ERROR_BAD_STATE );
4233
4234 psa_aead_abort( &operation );
4235
Paul Elliott374a2be2021-07-16 17:53:40 +01004236 /* ------------------------------------------------------- */
4237
4238 operation = psa_aead_operation_init( );
4239
4240 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4241
4242 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4243 PSA_ERROR_BAD_STATE );
4244
4245 psa_aead_abort( &operation );
4246
4247 /* ------------------------------------------------------- */
4248
4249 operation = psa_aead_operation_init( );
4250
4251 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4252
4253 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4254 PSA_ERROR_BAD_STATE );
4255
4256 psa_aead_abort( &operation );
4257
Paul Elliottc23a9a02021-06-21 18:32:46 +01004258 /* Test for not setting a nonce. */
4259
4260 operation = psa_aead_operation_init( );
4261
4262 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4263
4264 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4265 additional_data->len ),
4266 PSA_ERROR_BAD_STATE );
4267
4268 psa_aead_abort( &operation );
4269
Paul Elliott7f628422021-09-01 12:08:29 +01004270 /* ------------------------------------------------------- */
4271
4272 operation = psa_aead_operation_init( );
4273
4274 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4275
4276 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4277 input_data->len, output_data,
4278 output_size, &output_length ),
4279 PSA_ERROR_BAD_STATE );
4280
4281 psa_aead_abort( &operation );
4282
Paul Elliottc23a9a02021-06-21 18:32:46 +01004283 /* Test for double setting nonce. */
4284
4285 operation = psa_aead_operation_init( );
4286
4287 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4288
4289 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4290
4291 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4292 PSA_ERROR_BAD_STATE );
4293
4294 psa_aead_abort( &operation );
4295
Paul Elliott374a2be2021-07-16 17:53:40 +01004296 /* Test for double generating nonce. */
4297
4298 operation = psa_aead_operation_init( );
4299
4300 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4301
4302 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4303 PSA_AEAD_NONCE_MAX_SIZE,
4304 &nonce_length ) );
4305
4306 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4307 PSA_AEAD_NONCE_MAX_SIZE,
4308 &nonce_length ),
4309 PSA_ERROR_BAD_STATE );
4310
4311
4312 psa_aead_abort( &operation );
4313
4314 /* Test for generate nonce then set and vice versa */
4315
4316 operation = psa_aead_operation_init( );
4317
4318 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4319
4320 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4321 PSA_AEAD_NONCE_MAX_SIZE,
4322 &nonce_length ) );
4323
4324 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4325 PSA_ERROR_BAD_STATE );
4326
4327 psa_aead_abort( &operation );
4328
4329 /* ------------------------------------------------------- */
4330
4331 operation = psa_aead_operation_init( );
4332
4333 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4334
4335 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4336
4337 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4338 PSA_AEAD_NONCE_MAX_SIZE,
4339 &nonce_length ),
4340 PSA_ERROR_BAD_STATE );
4341
4342 psa_aead_abort( &operation );
4343
Paul Elliott7220cae2021-06-22 17:25:57 +01004344 /* Test for generating nonce in decrypt setup. */
4345
4346 operation = psa_aead_operation_init( );
4347
4348 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4349
4350 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4351 PSA_AEAD_NONCE_MAX_SIZE,
4352 &nonce_length ),
4353 PSA_ERROR_BAD_STATE );
4354
4355 psa_aead_abort( &operation );
4356
Paul Elliottc23a9a02021-06-21 18:32:46 +01004357 /* Test for setting lengths twice. */
4358
4359 operation = psa_aead_operation_init( );
4360
4361 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4362
4363 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4364
4365 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4366 input_data->len ) );
4367
4368 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4369 input_data->len ),
4370 PSA_ERROR_BAD_STATE );
4371
4372 psa_aead_abort( &operation );
4373
4374 /* Test for setting lengths after already starting data. */
4375
4376 operation = psa_aead_operation_init( );
4377
4378 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4379
4380 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4381
4382 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4383 input_data->len, output_data,
4384 output_size, &output_length ) );
4385
4386 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4387 input_data->len ),
4388 PSA_ERROR_BAD_STATE );
4389
4390 psa_aead_abort( &operation );
4391
Paul Elliott243080c2021-07-21 19:01:17 +01004392 /* Test for not sending any additional data or data after setting non zero
4393 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004394
4395 operation = psa_aead_operation_init( );
4396
4397 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4398
4399 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4400
4401 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4402 input_data->len ) );
4403
4404 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4405 finish_output_size,
4406 &output_part_length,
4407 tag_buffer, tag_length,
4408 &tag_size ),
4409 PSA_ERROR_INVALID_ARGUMENT );
4410
4411 psa_aead_abort( &operation );
4412
Paul Elliott243080c2021-07-21 19:01:17 +01004413 /* Test for not sending any additional data or data after setting non-zero
4414 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004415
4416 operation = psa_aead_operation_init( );
4417
4418 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4419
4420 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4421
4422 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4423 input_data->len ) );
4424
4425 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4426 finish_output_size,
4427 &output_part_length,
4428 tag_buffer,
4429 tag_length ),
4430 PSA_ERROR_INVALID_ARGUMENT );
4431
4432 psa_aead_abort( &operation );
4433
Paul Elliott243080c2021-07-21 19:01:17 +01004434 /* Test for not sending any additional data after setting a non-zero length
4435 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004436
4437 operation = psa_aead_operation_init( );
4438
4439 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4440
4441 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4442
4443 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4444 input_data->len ) );
4445
4446 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4447 input_data->len, output_data,
4448 output_size, &output_length ),
4449 PSA_ERROR_INVALID_ARGUMENT );
4450
4451 psa_aead_abort( &operation );
4452
Paul Elliottb0450fe2021-09-01 15:06:26 +01004453 /* Test for sending too much additional data after setting lengths. */
4454
4455 operation = psa_aead_operation_init( );
4456
4457 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4458
4459 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4460
4461 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4462
4463
4464 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4465 additional_data->len ),
4466 PSA_ERROR_INVALID_ARGUMENT );
4467
4468 psa_aead_abort( &operation );
4469
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004470 operation = psa_aead_operation_init( );
4471
4472 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4473
4474 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4475
4476 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4477 input_data->len ) );
4478
4479 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4480 additional_data->len ) );
4481
4482 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4483 1 ),
4484 PSA_ERROR_INVALID_ARGUMENT );
4485
4486 psa_aead_abort( &operation );
4487
Paul Elliottb0450fe2021-09-01 15:06:26 +01004488 /* Test for sending too much data after setting lengths. */
4489
4490 operation = psa_aead_operation_init( );
4491
4492 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4493
4494 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4495
4496 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4497
4498 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4499 input_data->len, output_data,
4500 output_size, &output_length ),
4501 PSA_ERROR_INVALID_ARGUMENT );
4502
4503 psa_aead_abort( &operation );
4504
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004505 operation = psa_aead_operation_init( );
4506
4507 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4508
4509 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4510
4511 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4512 input_data->len ) );
4513
4514 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4515 additional_data->len ) );
4516
4517 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4518 input_data->len, output_data,
4519 output_size, &output_length ) );
4520
4521 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4522 1, output_data,
4523 output_size, &output_length ),
4524 PSA_ERROR_INVALID_ARGUMENT );
4525
4526 psa_aead_abort( &operation );
4527
Paul Elliottc23a9a02021-06-21 18:32:46 +01004528 /* Test sending additional data after data. */
4529
4530 operation = psa_aead_operation_init( );
4531
4532 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4533
4534 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4535
4536 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4537 input_data->len, output_data,
4538 output_size, &output_length ) );
4539
4540 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4541 additional_data->len ),
4542 PSA_ERROR_BAD_STATE );
4543
4544 psa_aead_abort( &operation );
4545
Paul Elliott534d0b42021-06-22 19:15:20 +01004546 /* Test calling finish on decryption. */
4547
4548 operation = psa_aead_operation_init( );
4549
4550 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4551
4552 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4553
4554 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4555 finish_output_size,
4556 &output_part_length,
4557 tag_buffer, tag_length,
4558 &tag_size ),
4559 PSA_ERROR_BAD_STATE );
4560
4561 psa_aead_abort( &operation );
4562
4563 /* Test calling verify on encryption. */
4564
4565 operation = psa_aead_operation_init( );
4566
4567 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4568
4569 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4570
4571 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4572 finish_output_size,
4573 &output_part_length,
4574 tag_buffer,
4575 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004576 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004577
4578 psa_aead_abort( &operation );
4579
4580
Paul Elliottc23a9a02021-06-21 18:32:46 +01004581exit:
4582 psa_destroy_key( key );
4583 psa_aead_abort( &operation );
4584 mbedtls_free( output_data );
4585 mbedtls_free( final_data );
4586 PSA_DONE( );
4587}
4588/* END_CASE */
4589
4590/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004591void signature_size( int type_arg,
4592 int bits,
4593 int alg_arg,
4594 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004595{
4596 psa_key_type_t type = type_arg;
4597 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004598 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004599
Gilles Peskinefe11b722018-12-18 00:24:04 +01004600 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004601
Gilles Peskinee59236f2018-01-27 23:32:46 +01004602exit:
4603 ;
4604}
4605/* END_CASE */
4606
4607/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004608void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4609 int alg_arg, data_t *input_data,
4610 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004611{
Ronald Cron5425a212020-08-04 14:58:35 +02004612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004613 psa_key_type_t key_type = key_type_arg;
4614 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004615 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004616 unsigned char *signature = NULL;
4617 size_t signature_size;
4618 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004620
Gilles Peskine8817f612018-12-18 00:18:46 +01004621 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004622
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004623 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004624 psa_set_key_algorithm( &attributes, alg );
4625 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004626
Gilles Peskine049c7532019-05-15 20:22:09 +02004627 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004628 &key ) );
4629 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004630 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004631
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004632 /* Allocate a buffer which has the size advertized by the
4633 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004634 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004635 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004636 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004637 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004638 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004639
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004640 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004641 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004642 input_data->x, input_data->len,
4643 signature, signature_size,
4644 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004645 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004646 ASSERT_COMPARE( output_data->x, output_data->len,
4647 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004648
4649exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004650 /*
4651 * Key attributes may have been returned by psa_get_key_attributes()
4652 * thus reset them as required.
4653 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004654 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004655
Ronald Cron5425a212020-08-04 14:58:35 +02004656 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004657 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004658 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004659}
4660/* END_CASE */
4661
4662/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004663void sign_hash_fail( int key_type_arg, data_t *key_data,
4664 int alg_arg, data_t *input_data,
4665 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004666{
Ronald Cron5425a212020-08-04 14:58:35 +02004667 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004668 psa_key_type_t key_type = key_type_arg;
4669 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004670 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004671 psa_status_t actual_status;
4672 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004673 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004674 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004676
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004677 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004678
Gilles Peskine8817f612018-12-18 00:18:46 +01004679 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004680
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004681 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004682 psa_set_key_algorithm( &attributes, alg );
4683 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004684
Gilles Peskine049c7532019-05-15 20:22:09 +02004685 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004686 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004687
Ronald Cron5425a212020-08-04 14:58:35 +02004688 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004689 input_data->x, input_data->len,
4690 signature, signature_size,
4691 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004692 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004693 /* The value of *signature_length is unspecified on error, but
4694 * whatever it is, it should be less than signature_size, so that
4695 * if the caller tries to read *signature_length bytes without
4696 * checking the error code then they don't overflow a buffer. */
4697 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004698
4699exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004700 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004701 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004702 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004703 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004704}
4705/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004706
4707/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004708void sign_verify_hash( int key_type_arg, data_t *key_data,
4709 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004710{
Ronald Cron5425a212020-08-04 14:58:35 +02004711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004712 psa_key_type_t key_type = key_type_arg;
4713 psa_algorithm_t alg = alg_arg;
4714 size_t key_bits;
4715 unsigned char *signature = NULL;
4716 size_t signature_size;
4717 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004719
Gilles Peskine8817f612018-12-18 00:18:46 +01004720 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004721
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004722 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004723 psa_set_key_algorithm( &attributes, alg );
4724 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004725
Gilles Peskine049c7532019-05-15 20:22:09 +02004726 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004727 &key ) );
4728 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004729 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004730
4731 /* Allocate a buffer which has the size advertized by the
4732 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004733 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004734 key_bits, alg );
4735 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004736 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004737 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004738
4739 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004740 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004741 input_data->x, input_data->len,
4742 signature, signature_size,
4743 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004744 /* Check that the signature length looks sensible. */
4745 TEST_ASSERT( signature_length <= signature_size );
4746 TEST_ASSERT( signature_length > 0 );
4747
4748 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004749 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004750 input_data->x, input_data->len,
4751 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004752
4753 if( input_data->len != 0 )
4754 {
4755 /* Flip a bit in the input and verify that the signature is now
4756 * detected as invalid. Flip a bit at the beginning, not at the end,
4757 * because ECDSA may ignore the last few bits of the input. */
4758 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004759 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004760 input_data->x, input_data->len,
4761 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004762 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004763 }
4764
4765exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004766 /*
4767 * Key attributes may have been returned by psa_get_key_attributes()
4768 * thus reset them as required.
4769 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004770 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004771
Ronald Cron5425a212020-08-04 14:58:35 +02004772 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004773 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004774 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004775}
4776/* END_CASE */
4777
4778/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004779void verify_hash( int key_type_arg, data_t *key_data,
4780 int alg_arg, data_t *hash_data,
4781 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004782{
Ronald Cron5425a212020-08-04 14:58:35 +02004783 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004784 psa_key_type_t key_type = key_type_arg;
4785 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004786 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004787
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004788 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004789
Gilles Peskine8817f612018-12-18 00:18:46 +01004790 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004791
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004792 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004793 psa_set_key_algorithm( &attributes, alg );
4794 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004795
Gilles Peskine049c7532019-05-15 20:22:09 +02004796 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004797 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004798
Ronald Cron5425a212020-08-04 14:58:35 +02004799 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004800 hash_data->x, hash_data->len,
4801 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004802
itayzafrir5c753392018-05-08 11:18:38 +03004803exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004804 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004805 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004806 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004807}
4808/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004809
4810/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004811void verify_hash_fail( int key_type_arg, data_t *key_data,
4812 int alg_arg, data_t *hash_data,
4813 data_t *signature_data,
4814 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004815{
Ronald Cron5425a212020-08-04 14:58:35 +02004816 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004817 psa_key_type_t key_type = key_type_arg;
4818 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004819 psa_status_t actual_status;
4820 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004822
Gilles Peskine8817f612018-12-18 00:18:46 +01004823 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004824
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004825 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004826 psa_set_key_algorithm( &attributes, alg );
4827 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004828
Gilles Peskine049c7532019-05-15 20:22:09 +02004829 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004830 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004831
Ronald Cron5425a212020-08-04 14:58:35 +02004832 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004833 hash_data->x, hash_data->len,
4834 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004835 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004836
4837exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004838 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004839 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004840 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004841}
4842/* END_CASE */
4843
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004844/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004845void sign_message_deterministic( int key_type_arg,
4846 data_t *key_data,
4847 int alg_arg,
4848 data_t *input_data,
4849 data_t *output_data )
4850{
4851 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4852 psa_key_type_t key_type = key_type_arg;
4853 psa_algorithm_t alg = alg_arg;
4854 size_t key_bits;
4855 unsigned char *signature = NULL;
4856 size_t signature_size;
4857 size_t signature_length = 0xdeadbeef;
4858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4859
4860 PSA_ASSERT( psa_crypto_init( ) );
4861
4862 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4863 psa_set_key_algorithm( &attributes, alg );
4864 psa_set_key_type( &attributes, key_type );
4865
4866 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4867 &key ) );
4868 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4869 key_bits = psa_get_key_bits( &attributes );
4870
4871 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4872 TEST_ASSERT( signature_size != 0 );
4873 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4874 ASSERT_ALLOC( signature, signature_size );
4875
4876 PSA_ASSERT( psa_sign_message( key, alg,
4877 input_data->x, input_data->len,
4878 signature, signature_size,
4879 &signature_length ) );
4880
4881 ASSERT_COMPARE( output_data->x, output_data->len,
4882 signature, signature_length );
4883
4884exit:
4885 psa_reset_key_attributes( &attributes );
4886
4887 psa_destroy_key( key );
4888 mbedtls_free( signature );
4889 PSA_DONE( );
4890
4891}
4892/* END_CASE */
4893
4894/* BEGIN_CASE */
4895void sign_message_fail( int key_type_arg,
4896 data_t *key_data,
4897 int alg_arg,
4898 data_t *input_data,
4899 int signature_size_arg,
4900 int expected_status_arg )
4901{
4902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4903 psa_key_type_t key_type = key_type_arg;
4904 psa_algorithm_t alg = alg_arg;
4905 size_t signature_size = signature_size_arg;
4906 psa_status_t actual_status;
4907 psa_status_t expected_status = expected_status_arg;
4908 unsigned char *signature = NULL;
4909 size_t signature_length = 0xdeadbeef;
4910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4911
4912 ASSERT_ALLOC( signature, signature_size );
4913
4914 PSA_ASSERT( psa_crypto_init( ) );
4915
4916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4917 psa_set_key_algorithm( &attributes, alg );
4918 psa_set_key_type( &attributes, key_type );
4919
4920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4921 &key ) );
4922
4923 actual_status = psa_sign_message( key, alg,
4924 input_data->x, input_data->len,
4925 signature, signature_size,
4926 &signature_length );
4927 TEST_EQUAL( actual_status, expected_status );
4928 /* The value of *signature_length is unspecified on error, but
4929 * whatever it is, it should be less than signature_size, so that
4930 * if the caller tries to read *signature_length bytes without
4931 * checking the error code then they don't overflow a buffer. */
4932 TEST_ASSERT( signature_length <= signature_size );
4933
4934exit:
4935 psa_reset_key_attributes( &attributes );
4936 psa_destroy_key( key );
4937 mbedtls_free( signature );
4938 PSA_DONE( );
4939}
4940/* END_CASE */
4941
4942/* BEGIN_CASE */
4943void sign_verify_message( int key_type_arg,
4944 data_t *key_data,
4945 int alg_arg,
4946 data_t *input_data )
4947{
4948 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4949 psa_key_type_t key_type = key_type_arg;
4950 psa_algorithm_t alg = alg_arg;
4951 size_t key_bits;
4952 unsigned char *signature = NULL;
4953 size_t signature_size;
4954 size_t signature_length = 0xdeadbeef;
4955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4956
4957 PSA_ASSERT( psa_crypto_init( ) );
4958
4959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4960 PSA_KEY_USAGE_VERIFY_MESSAGE );
4961 psa_set_key_algorithm( &attributes, alg );
4962 psa_set_key_type( &attributes, key_type );
4963
4964 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4965 &key ) );
4966 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4967 key_bits = psa_get_key_bits( &attributes );
4968
4969 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4970 TEST_ASSERT( signature_size != 0 );
4971 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4972 ASSERT_ALLOC( signature, signature_size );
4973
4974 PSA_ASSERT( psa_sign_message( key, alg,
4975 input_data->x, input_data->len,
4976 signature, signature_size,
4977 &signature_length ) );
4978 TEST_ASSERT( signature_length <= signature_size );
4979 TEST_ASSERT( signature_length > 0 );
4980
4981 PSA_ASSERT( psa_verify_message( key, alg,
4982 input_data->x, input_data->len,
4983 signature, signature_length ) );
4984
4985 if( input_data->len != 0 )
4986 {
4987 /* Flip a bit in the input and verify that the signature is now
4988 * detected as invalid. Flip a bit at the beginning, not at the end,
4989 * because ECDSA may ignore the last few bits of the input. */
4990 input_data->x[0] ^= 1;
4991 TEST_EQUAL( psa_verify_message( key, alg,
4992 input_data->x, input_data->len,
4993 signature, signature_length ),
4994 PSA_ERROR_INVALID_SIGNATURE );
4995 }
4996
4997exit:
4998 psa_reset_key_attributes( &attributes );
4999
5000 psa_destroy_key( key );
5001 mbedtls_free( signature );
5002 PSA_DONE( );
5003}
5004/* END_CASE */
5005
5006/* BEGIN_CASE */
5007void verify_message( int key_type_arg,
5008 data_t *key_data,
5009 int alg_arg,
5010 data_t *input_data,
5011 data_t *signature_data )
5012{
5013 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5014 psa_key_type_t key_type = key_type_arg;
5015 psa_algorithm_t alg = alg_arg;
5016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5017
5018 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5019
5020 PSA_ASSERT( psa_crypto_init( ) );
5021
5022 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5023 psa_set_key_algorithm( &attributes, alg );
5024 psa_set_key_type( &attributes, key_type );
5025
5026 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5027 &key ) );
5028
5029 PSA_ASSERT( psa_verify_message( key, alg,
5030 input_data->x, input_data->len,
5031 signature_data->x, signature_data->len ) );
5032
5033exit:
5034 psa_reset_key_attributes( &attributes );
5035 psa_destroy_key( key );
5036 PSA_DONE( );
5037}
5038/* END_CASE */
5039
5040/* BEGIN_CASE */
5041void verify_message_fail( int key_type_arg,
5042 data_t *key_data,
5043 int alg_arg,
5044 data_t *hash_data,
5045 data_t *signature_data,
5046 int expected_status_arg )
5047{
5048 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5049 psa_key_type_t key_type = key_type_arg;
5050 psa_algorithm_t alg = alg_arg;
5051 psa_status_t actual_status;
5052 psa_status_t expected_status = expected_status_arg;
5053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5054
5055 PSA_ASSERT( psa_crypto_init( ) );
5056
5057 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5058 psa_set_key_algorithm( &attributes, alg );
5059 psa_set_key_type( &attributes, key_type );
5060
5061 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5062 &key ) );
5063
5064 actual_status = psa_verify_message( key, alg,
5065 hash_data->x, hash_data->len,
5066 signature_data->x,
5067 signature_data->len );
5068 TEST_EQUAL( actual_status, expected_status );
5069
5070exit:
5071 psa_reset_key_attributes( &attributes );
5072 psa_destroy_key( key );
5073 PSA_DONE( );
5074}
5075/* END_CASE */
5076
5077/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005078void asymmetric_encrypt( int key_type_arg,
5079 data_t *key_data,
5080 int alg_arg,
5081 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005082 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005083 int expected_output_length_arg,
5084 int expected_status_arg )
5085{
Ronald Cron5425a212020-08-04 14:58:35 +02005086 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005087 psa_key_type_t key_type = key_type_arg;
5088 psa_algorithm_t alg = alg_arg;
5089 size_t expected_output_length = expected_output_length_arg;
5090 size_t key_bits;
5091 unsigned char *output = NULL;
5092 size_t output_size;
5093 size_t output_length = ~0;
5094 psa_status_t actual_status;
5095 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005097
Gilles Peskine8817f612018-12-18 00:18:46 +01005098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005099
Gilles Peskine656896e2018-06-29 19:12:28 +02005100 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005101 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5102 psa_set_key_algorithm( &attributes, alg );
5103 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005104 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005105 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005106
5107 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005108 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005109 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005110
Gilles Peskine656896e2018-06-29 19:12:28 +02005111 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005112 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005113 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005114
5115 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005116 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005117 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005118 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005119 output, output_size,
5120 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005121 TEST_EQUAL( actual_status, expected_status );
5122 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005123
Gilles Peskine68428122018-06-30 18:42:41 +02005124 /* If the label is empty, the test framework puts a non-null pointer
5125 * in label->x. Test that a null pointer works as well. */
5126 if( label->len == 0 )
5127 {
5128 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005129 if( output_size != 0 )
5130 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005131 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005132 input_data->x, input_data->len,
5133 NULL, label->len,
5134 output, output_size,
5135 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005136 TEST_EQUAL( actual_status, expected_status );
5137 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005138 }
5139
Gilles Peskine656896e2018-06-29 19:12:28 +02005140exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005141 /*
5142 * Key attributes may have been returned by psa_get_key_attributes()
5143 * thus reset them as required.
5144 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005145 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005146
Ronald Cron5425a212020-08-04 14:58:35 +02005147 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005148 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005149 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005150}
5151/* END_CASE */
5152
5153/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005154void asymmetric_encrypt_decrypt( int key_type_arg,
5155 data_t *key_data,
5156 int alg_arg,
5157 data_t *input_data,
5158 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005159{
Ronald Cron5425a212020-08-04 14:58:35 +02005160 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005161 psa_key_type_t key_type = key_type_arg;
5162 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005163 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005164 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005165 size_t output_size;
5166 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005167 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005168 size_t output2_size;
5169 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005171
Gilles Peskine8817f612018-12-18 00:18:46 +01005172 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005173
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5175 psa_set_key_algorithm( &attributes, alg );
5176 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005177
Gilles Peskine049c7532019-05-15 20:22:09 +02005178 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005179 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005180
5181 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005182 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005183 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005184
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005185 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005186 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005187 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005188
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005189 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005190 TEST_ASSERT( output2_size <=
5191 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5192 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005193 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005194
Gilles Peskineeebd7382018-06-08 18:11:54 +02005195 /* We test encryption by checking that encrypt-then-decrypt gives back
5196 * the original plaintext because of the non-optional random
5197 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005198 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005199 input_data->x, input_data->len,
5200 label->x, label->len,
5201 output, output_size,
5202 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005203 /* We don't know what ciphertext length to expect, but check that
5204 * it looks sensible. */
5205 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005206
Ronald Cron5425a212020-08-04 14:58:35 +02005207 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005208 output, output_length,
5209 label->x, label->len,
5210 output2, output2_size,
5211 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005212 ASSERT_COMPARE( input_data->x, input_data->len,
5213 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005214
5215exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005216 /*
5217 * Key attributes may have been returned by psa_get_key_attributes()
5218 * thus reset them as required.
5219 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005220 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005221
Ronald Cron5425a212020-08-04 14:58:35 +02005222 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005223 mbedtls_free( output );
5224 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005225 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005226}
5227/* END_CASE */
5228
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005229/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005230void asymmetric_decrypt( int key_type_arg,
5231 data_t *key_data,
5232 int alg_arg,
5233 data_t *input_data,
5234 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005235 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005236{
Ronald Cron5425a212020-08-04 14:58:35 +02005237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005238 psa_key_type_t key_type = key_type_arg;
5239 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005240 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005241 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005242 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005243 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005244 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005245
Gilles Peskine8817f612018-12-18 00:18:46 +01005246 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005247
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005248 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5249 psa_set_key_algorithm( &attributes, alg );
5250 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005251
Gilles Peskine049c7532019-05-15 20:22:09 +02005252 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005253 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005254
gabor-mezei-armceface22021-01-21 12:26:17 +01005255 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5256 key_bits = psa_get_key_bits( &attributes );
5257
5258 /* Determine the maximum ciphertext length */
5259 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5260 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5261 ASSERT_ALLOC( output, output_size );
5262
Ronald Cron5425a212020-08-04 14:58:35 +02005263 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005264 input_data->x, input_data->len,
5265 label->x, label->len,
5266 output,
5267 output_size,
5268 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005269 ASSERT_COMPARE( expected_data->x, expected_data->len,
5270 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005271
Gilles Peskine68428122018-06-30 18:42:41 +02005272 /* If the label is empty, the test framework puts a non-null pointer
5273 * in label->x. Test that a null pointer works as well. */
5274 if( label->len == 0 )
5275 {
5276 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005277 if( output_size != 0 )
5278 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005279 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005280 input_data->x, input_data->len,
5281 NULL, label->len,
5282 output,
5283 output_size,
5284 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005285 ASSERT_COMPARE( expected_data->x, expected_data->len,
5286 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005287 }
5288
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005289exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005290 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005291 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005292 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005293 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005294}
5295/* END_CASE */
5296
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005297/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005298void asymmetric_decrypt_fail( int key_type_arg,
5299 data_t *key_data,
5300 int alg_arg,
5301 data_t *input_data,
5302 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005303 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005304 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005305{
Ronald Cron5425a212020-08-04 14:58:35 +02005306 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005307 psa_key_type_t key_type = key_type_arg;
5308 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005309 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005310 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005311 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005312 psa_status_t actual_status;
5313 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005315
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005316 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005317
Gilles Peskine8817f612018-12-18 00:18:46 +01005318 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005319
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005320 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5321 psa_set_key_algorithm( &attributes, alg );
5322 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005323
Gilles Peskine049c7532019-05-15 20:22:09 +02005324 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005325 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005326
Ronald Cron5425a212020-08-04 14:58:35 +02005327 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005328 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005329 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005330 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005331 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005332 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005333 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005334
Gilles Peskine68428122018-06-30 18:42:41 +02005335 /* If the label is empty, the test framework puts a non-null pointer
5336 * in label->x. Test that a null pointer works as well. */
5337 if( label->len == 0 )
5338 {
5339 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005340 if( output_size != 0 )
5341 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005342 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005343 input_data->x, input_data->len,
5344 NULL, label->len,
5345 output, output_size,
5346 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005347 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005348 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005349 }
5350
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005351exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005352 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005353 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005354 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005355 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005356}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005357/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005358
5359/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005360void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005361{
5362 /* Test each valid way of initializing the object, except for `= {0}`, as
5363 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5364 * though it's OK by the C standard. We could test for this, but we'd need
5365 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005366 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005367 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5368 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5369 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005370
5371 memset( &zero, 0, sizeof( zero ) );
5372
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005373 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005374 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005375 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005376 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005377 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005378 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005379 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005380
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005381 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005382 PSA_ASSERT( psa_key_derivation_abort(&func) );
5383 PSA_ASSERT( psa_key_derivation_abort(&init) );
5384 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005385}
5386/* END_CASE */
5387
Janos Follath16de4a42019-06-13 16:32:24 +01005388/* BEGIN_CASE */
5389void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005390{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005391 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005392 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005394
Gilles Peskine8817f612018-12-18 00:18:46 +01005395 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005396
Janos Follath16de4a42019-06-13 16:32:24 +01005397 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005398 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005399
5400exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005401 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005402 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005403}
5404/* END_CASE */
5405
Janos Follathaf3c2a02019-06-12 12:34:34 +01005406/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005407void derive_set_capacity( int alg_arg, int capacity_arg,
5408 int expected_status_arg )
5409{
5410 psa_algorithm_t alg = alg_arg;
5411 size_t capacity = capacity_arg;
5412 psa_status_t expected_status = expected_status_arg;
5413 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5414
5415 PSA_ASSERT( psa_crypto_init( ) );
5416
5417 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5418
5419 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5420 expected_status );
5421
5422exit:
5423 psa_key_derivation_abort( &operation );
5424 PSA_DONE( );
5425}
5426/* END_CASE */
5427
5428/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005429void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005430 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005431 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005432 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005433 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005434 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005435 int expected_status_arg3,
5436 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005437{
5438 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005439 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5440 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005441 psa_status_t expected_statuses[] = {expected_status_arg1,
5442 expected_status_arg2,
5443 expected_status_arg3};
5444 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005445 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5446 MBEDTLS_SVC_KEY_ID_INIT,
5447 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005448 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5450 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005451 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005452 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005453 psa_status_t expected_output_status = expected_output_status_arg;
5454 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005455
5456 PSA_ASSERT( psa_crypto_init( ) );
5457
5458 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5459 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005460
5461 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5462
5463 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5464 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005465 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005466 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005467 psa_set_key_type( &attributes, key_types[i] );
5468 PSA_ASSERT( psa_import_key( &attributes,
5469 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005470 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005471 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5472 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5473 {
5474 // When taking a private key as secret input, use key agreement
5475 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005476 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5477 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005478 expected_statuses[i] );
5479 }
5480 else
5481 {
5482 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005483 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005484 expected_statuses[i] );
5485 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005486 }
5487 else
5488 {
5489 TEST_EQUAL( psa_key_derivation_input_bytes(
5490 &operation, steps[i],
5491 inputs[i]->x, inputs[i]->len ),
5492 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005493 }
5494 }
5495
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005496 if( output_key_type != PSA_KEY_TYPE_NONE )
5497 {
5498 psa_reset_key_attributes( &attributes );
5499 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5500 psa_set_key_bits( &attributes, 8 );
5501 actual_output_status =
5502 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005503 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005504 }
5505 else
5506 {
5507 uint8_t buffer[1];
5508 actual_output_status =
5509 psa_key_derivation_output_bytes( &operation,
5510 buffer, sizeof( buffer ) );
5511 }
5512 TEST_EQUAL( actual_output_status, expected_output_status );
5513
Janos Follathaf3c2a02019-06-12 12:34:34 +01005514exit:
5515 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005516 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5517 psa_destroy_key( keys[i] );
5518 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005519 PSA_DONE( );
5520}
5521/* END_CASE */
5522
Janos Follathd958bb72019-07-03 15:02:16 +01005523/* BEGIN_CASE */
5524void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005525{
Janos Follathd958bb72019-07-03 15:02:16 +01005526 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005527 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005528 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005529 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005530 unsigned char input1[] = "Input 1";
5531 size_t input1_length = sizeof( input1 );
5532 unsigned char input2[] = "Input 2";
5533 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005534 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005535 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005536 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5537 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5538 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005540
Gilles Peskine8817f612018-12-18 00:18:46 +01005541 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005542
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005543 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5544 psa_set_key_algorithm( &attributes, alg );
5545 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005546
Gilles Peskine73676cb2019-05-15 20:15:10 +02005547 PSA_ASSERT( psa_import_key( &attributes,
5548 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005549 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005550
5551 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005552 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5553 input1, input1_length,
5554 input2, input2_length,
5555 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005556 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005557
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005558 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005559 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005560 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005561
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005562 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005563
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005564 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005565 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005566
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005567exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005568 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005569 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005570 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005571}
5572/* END_CASE */
5573
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005574/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005575void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005576{
5577 uint8_t output_buffer[16];
5578 size_t buffer_size = 16;
5579 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005580 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005581
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005582 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5583 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005584 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005585
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005586 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005587 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005588
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005589 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005590
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005591 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5592 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005593 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005594
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005595 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005596 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005597
5598exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005599 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005600}
5601/* END_CASE */
5602
5603/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005604void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005605 int step1_arg, data_t *input1,
5606 int step2_arg, data_t *input2,
5607 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005608 int requested_capacity_arg,
5609 data_t *expected_output1,
5610 data_t *expected_output2 )
5611{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005612 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005613 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5614 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005615 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5616 MBEDTLS_SVC_KEY_ID_INIT,
5617 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005618 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005619 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005620 uint8_t *expected_outputs[2] =
5621 {expected_output1->x, expected_output2->x};
5622 size_t output_sizes[2] =
5623 {expected_output1->len, expected_output2->len};
5624 size_t output_buffer_size = 0;
5625 uint8_t *output_buffer = NULL;
5626 size_t expected_capacity;
5627 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005628 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005629 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005630 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005631
5632 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5633 {
5634 if( output_sizes[i] > output_buffer_size )
5635 output_buffer_size = output_sizes[i];
5636 if( output_sizes[i] == 0 )
5637 expected_outputs[i] = NULL;
5638 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005639 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005640 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005641
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005642 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5643 psa_set_key_algorithm( &attributes, alg );
5644 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005645
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005646 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005647 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5648 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5649 requested_capacity ) );
5650 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005651 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005652 switch( steps[i] )
5653 {
5654 case 0:
5655 break;
5656 case PSA_KEY_DERIVATION_INPUT_SECRET:
5657 PSA_ASSERT( psa_import_key( &attributes,
5658 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005659 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005660
5661 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5662 {
5663 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5664 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5665 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5666 }
5667
Gilles Peskine1468da72019-05-29 17:35:49 +02005668 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005669 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005670 break;
5671 default:
5672 PSA_ASSERT( psa_key_derivation_input_bytes(
5673 &operation, steps[i],
5674 inputs[i]->x, inputs[i]->len ) );
5675 break;
5676 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005677 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005678
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005679 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005680 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005681 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005682 expected_capacity = requested_capacity;
5683
5684 /* Expansion phase. */
5685 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5686 {
5687 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005688 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005689 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005690 if( expected_capacity == 0 && output_sizes[i] == 0 )
5691 {
5692 /* Reading 0 bytes when 0 bytes are available can go either way. */
5693 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005694 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005695 continue;
5696 }
5697 else if( expected_capacity == 0 ||
5698 output_sizes[i] > expected_capacity )
5699 {
5700 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005701 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005702 expected_capacity = 0;
5703 continue;
5704 }
5705 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005706 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005707 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005708 ASSERT_COMPARE( output_buffer, output_sizes[i],
5709 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005710 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005711 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005712 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005713 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005714 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005715 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005716 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005717
5718exit:
5719 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005721 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5722 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005723 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005724}
5725/* END_CASE */
5726
5727/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005728void derive_full( int alg_arg,
5729 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005730 data_t *input1,
5731 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005732 int requested_capacity_arg )
5733{
Ronald Cron5425a212020-08-04 14:58:35 +02005734 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005735 psa_algorithm_t alg = alg_arg;
5736 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005737 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005738 unsigned char output_buffer[16];
5739 size_t expected_capacity = requested_capacity;
5740 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005741 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005742
Gilles Peskine8817f612018-12-18 00:18:46 +01005743 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005744
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005745 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5746 psa_set_key_algorithm( &attributes, alg );
5747 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005748
Gilles Peskine049c7532019-05-15 20:22:09 +02005749 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005750 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005751
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005752 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5753 input1->x, input1->len,
5754 input2->x, input2->len,
5755 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005756 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005757
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005758 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005759 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005760 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005761
5762 /* Expansion phase. */
5763 while( current_capacity > 0 )
5764 {
5765 size_t read_size = sizeof( output_buffer );
5766 if( read_size > current_capacity )
5767 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005768 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005769 output_buffer,
5770 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005771 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005772 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005773 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005774 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005775 }
5776
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005777 /* Check that the operation refuses to go over capacity. */
5778 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005779 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005780
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005781 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005782
5783exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005784 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005785 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005786 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005787}
5788/* END_CASE */
5789
Janos Follathe60c9052019-07-03 13:51:30 +01005790/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005791void derive_key_exercise( int alg_arg,
5792 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005793 data_t *input1,
5794 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005795 int derived_type_arg,
5796 int derived_bits_arg,
5797 int derived_usage_arg,
5798 int derived_alg_arg )
5799{
Ronald Cron5425a212020-08-04 14:58:35 +02005800 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5801 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005802 psa_algorithm_t alg = alg_arg;
5803 psa_key_type_t derived_type = derived_type_arg;
5804 size_t derived_bits = derived_bits_arg;
5805 psa_key_usage_t derived_usage = derived_usage_arg;
5806 psa_algorithm_t derived_alg = derived_alg_arg;
5807 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005808 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005809 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005810 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005811
Gilles Peskine8817f612018-12-18 00:18:46 +01005812 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005813
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005814 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5815 psa_set_key_algorithm( &attributes, alg );
5816 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005817 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005818 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005819
5820 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005821 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5822 input1->x, input1->len,
5823 input2->x, input2->len,
5824 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005825 goto exit;
5826
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005827 psa_set_key_usage_flags( &attributes, derived_usage );
5828 psa_set_key_algorithm( &attributes, derived_alg );
5829 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005830 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005831 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005832 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005833
5834 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005835 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005836 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5837 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005838
5839 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005840 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005841 goto exit;
5842
5843exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005844 /*
5845 * Key attributes may have been returned by psa_get_key_attributes()
5846 * thus reset them as required.
5847 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005848 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005849
5850 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005851 psa_destroy_key( base_key );
5852 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005853 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005854}
5855/* END_CASE */
5856
Janos Follath42fd8882019-07-03 14:17:09 +01005857/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005858void derive_key_export( int alg_arg,
5859 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005860 data_t *input1,
5861 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005862 int bytes1_arg,
5863 int bytes2_arg )
5864{
Ronald Cron5425a212020-08-04 14:58:35 +02005865 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5866 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005867 psa_algorithm_t alg = alg_arg;
5868 size_t bytes1 = bytes1_arg;
5869 size_t bytes2 = bytes2_arg;
5870 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005871 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005872 uint8_t *output_buffer = NULL;
5873 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005874 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5875 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005876 size_t length;
5877
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005878 ASSERT_ALLOC( output_buffer, capacity );
5879 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005880 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005881
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005882 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5883 psa_set_key_algorithm( &base_attributes, alg );
5884 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005885 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005886 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005887
5888 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005889 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5890 input1->x, input1->len,
5891 input2->x, input2->len,
5892 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005893 goto exit;
5894
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005895 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005896 output_buffer,
5897 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005898 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005899
5900 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005901 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5902 input1->x, input1->len,
5903 input2->x, input2->len,
5904 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005905 goto exit;
5906
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005907 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5908 psa_set_key_algorithm( &derived_attributes, 0 );
5909 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005910 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005911 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005912 &derived_key ) );
5913 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005914 export_buffer, bytes1,
5915 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005916 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005917 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005918 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005919 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005920 &derived_key ) );
5921 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005922 export_buffer + bytes1, bytes2,
5923 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005924 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005925
5926 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005927 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5928 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005929
5930exit:
5931 mbedtls_free( output_buffer );
5932 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005933 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005934 psa_destroy_key( base_key );
5935 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005936 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005937}
5938/* END_CASE */
5939
5940/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005941void derive_key( int alg_arg,
5942 data_t *key_data, data_t *input1, data_t *input2,
5943 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005944 int expected_status_arg,
5945 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005946{
Ronald Cron5425a212020-08-04 14:58:35 +02005947 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5948 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005949 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005950 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005951 size_t bits = bits_arg;
5952 psa_status_t expected_status = expected_status_arg;
5953 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5954 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5955 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5956
5957 PSA_ASSERT( psa_crypto_init( ) );
5958
5959 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5960 psa_set_key_algorithm( &base_attributes, alg );
5961 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5962 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005963 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005964
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005965 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5966 input1->x, input1->len,
5967 input2->x, input2->len,
5968 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005969 goto exit;
5970
5971 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5972 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005973 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005974 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005975
5976 psa_status_t status =
5977 psa_key_derivation_output_key( &derived_attributes,
5978 &operation,
5979 &derived_key );
5980 if( is_large_output > 0 )
5981 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5982 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005983
5984exit:
5985 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005986 psa_destroy_key( base_key );
5987 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005988 PSA_DONE( );
5989}
5990/* END_CASE */
5991
5992/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005993void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005994 int our_key_type_arg, int our_key_alg_arg,
5995 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005996 int expected_status_arg )
5997{
Ronald Cron5425a212020-08-04 14:58:35 +02005998 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005999 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006000 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006001 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006002 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006004 psa_status_t expected_status = expected_status_arg;
6005 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006006
Gilles Peskine8817f612018-12-18 00:18:46 +01006007 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006008
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006009 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006010 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006011 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006012 PSA_ASSERT( psa_import_key( &attributes,
6013 our_key_data->x, our_key_data->len,
6014 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006015
Gilles Peskine77f40d82019-04-11 21:27:06 +02006016 /* The tests currently include inputs that should fail at either step.
6017 * Test cases that fail at the setup step should be changed to call
6018 * key_derivation_setup instead, and this function should be renamed
6019 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006020 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006021 if( status == PSA_SUCCESS )
6022 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006023 TEST_EQUAL( psa_key_derivation_key_agreement(
6024 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6025 our_key,
6026 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006027 expected_status );
6028 }
6029 else
6030 {
6031 TEST_ASSERT( status == expected_status );
6032 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006033
6034exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006035 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006036 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006037 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006038}
6039/* END_CASE */
6040
6041/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006042void raw_key_agreement( int alg_arg,
6043 int our_key_type_arg, data_t *our_key_data,
6044 data_t *peer_key_data,
6045 data_t *expected_output )
6046{
Ronald Cron5425a212020-08-04 14:58:35 +02006047 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006048 psa_algorithm_t alg = alg_arg;
6049 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006050 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006051 unsigned char *output = NULL;
6052 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006053 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006054
6055 ASSERT_ALLOC( output, expected_output->len );
6056 PSA_ASSERT( psa_crypto_init( ) );
6057
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006058 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6059 psa_set_key_algorithm( &attributes, alg );
6060 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006061 PSA_ASSERT( psa_import_key( &attributes,
6062 our_key_data->x, our_key_data->len,
6063 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006064
gabor-mezei-armceface22021-01-21 12:26:17 +01006065 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6066 key_bits = psa_get_key_bits( &attributes );
6067
Gilles Peskinebe697d82019-05-16 18:00:41 +02006068 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6069 peer_key_data->x, peer_key_data->len,
6070 output, expected_output->len,
6071 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006072 ASSERT_COMPARE( output, output_length,
6073 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006074 TEST_ASSERT( output_length <=
6075 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6076 TEST_ASSERT( output_length <=
6077 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006078
6079exit:
6080 mbedtls_free( output );
6081 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006082 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006083}
6084/* END_CASE */
6085
6086/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006087void key_agreement_capacity( int alg_arg,
6088 int our_key_type_arg, data_t *our_key_data,
6089 data_t *peer_key_data,
6090 int expected_capacity_arg )
6091{
Ronald Cron5425a212020-08-04 14:58:35 +02006092 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006093 psa_algorithm_t alg = alg_arg;
6094 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006095 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006097 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006098 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006099
Gilles Peskine8817f612018-12-18 00:18:46 +01006100 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006101
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006102 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6103 psa_set_key_algorithm( &attributes, alg );
6104 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006105 PSA_ASSERT( psa_import_key( &attributes,
6106 our_key_data->x, our_key_data->len,
6107 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006108
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006109 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006110 PSA_ASSERT( psa_key_derivation_key_agreement(
6111 &operation,
6112 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6113 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006114 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6115 {
6116 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006117 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006118 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006119 NULL, 0 ) );
6120 }
Gilles Peskine59685592018-09-18 12:11:34 +02006121
Gilles Peskinebf491972018-10-25 22:36:12 +02006122 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006123 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006124 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006125 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006126
Gilles Peskinebf491972018-10-25 22:36:12 +02006127 /* Test the actual capacity by reading the output. */
6128 while( actual_capacity > sizeof( output ) )
6129 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006130 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006131 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006132 actual_capacity -= sizeof( output );
6133 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006134 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006135 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006136 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006137 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006138
Gilles Peskine59685592018-09-18 12:11:34 +02006139exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006140 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006141 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006142 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006143}
6144/* END_CASE */
6145
6146/* BEGIN_CASE */
6147void key_agreement_output( int alg_arg,
6148 int our_key_type_arg, data_t *our_key_data,
6149 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006150 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006151{
Ronald Cron5425a212020-08-04 14:58:35 +02006152 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006153 psa_algorithm_t alg = alg_arg;
6154 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006155 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006157 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006158
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006159 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6160 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006161
Gilles Peskine8817f612018-12-18 00:18:46 +01006162 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006163
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006164 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6165 psa_set_key_algorithm( &attributes, alg );
6166 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006167 PSA_ASSERT( psa_import_key( &attributes,
6168 our_key_data->x, our_key_data->len,
6169 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006170
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006171 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006172 PSA_ASSERT( psa_key_derivation_key_agreement(
6173 &operation,
6174 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6175 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006176 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6177 {
6178 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006179 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006180 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006181 NULL, 0 ) );
6182 }
Gilles Peskine59685592018-09-18 12:11:34 +02006183
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006184 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006185 actual_output,
6186 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006187 ASSERT_COMPARE( actual_output, expected_output1->len,
6188 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006189 if( expected_output2->len != 0 )
6190 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006191 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006192 actual_output,
6193 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006194 ASSERT_COMPARE( actual_output, expected_output2->len,
6195 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006196 }
Gilles Peskine59685592018-09-18 12:11:34 +02006197
6198exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006199 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006200 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006201 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006202 mbedtls_free( actual_output );
6203}
6204/* END_CASE */
6205
6206/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006207void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006208{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006209 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006210 unsigned char *output = NULL;
6211 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006212 size_t i;
6213 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006214
Simon Butcher49f8e312020-03-03 15:51:50 +00006215 TEST_ASSERT( bytes_arg >= 0 );
6216
Gilles Peskine91892022021-02-08 19:50:26 +01006217 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006218 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006219
Gilles Peskine8817f612018-12-18 00:18:46 +01006220 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006221
Gilles Peskinea50d7392018-06-21 10:22:13 +02006222 /* Run several times, to ensure that every output byte will be
6223 * nonzero at least once with overwhelming probability
6224 * (2^(-8*number_of_runs)). */
6225 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006226 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006227 if( bytes != 0 )
6228 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006229 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006230
Gilles Peskinea50d7392018-06-21 10:22:13 +02006231 for( i = 0; i < bytes; i++ )
6232 {
6233 if( output[i] != 0 )
6234 ++changed[i];
6235 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006236 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006237
6238 /* Check that every byte was changed to nonzero at least once. This
6239 * validates that psa_generate_random is overwriting every byte of
6240 * the output buffer. */
6241 for( i = 0; i < bytes; i++ )
6242 {
6243 TEST_ASSERT( changed[i] != 0 );
6244 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006245
6246exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006247 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006248 mbedtls_free( output );
6249 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006250}
6251/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006252
6253/* BEGIN_CASE */
6254void generate_key( int type_arg,
6255 int bits_arg,
6256 int usage_arg,
6257 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006258 int expected_status_arg,
6259 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006260{
Ronald Cron5425a212020-08-04 14:58:35 +02006261 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006262 psa_key_type_t type = type_arg;
6263 psa_key_usage_t usage = usage_arg;
6264 size_t bits = bits_arg;
6265 psa_algorithm_t alg = alg_arg;
6266 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006268 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006269
Gilles Peskine8817f612018-12-18 00:18:46 +01006270 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006271
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006272 psa_set_key_usage_flags( &attributes, usage );
6273 psa_set_key_algorithm( &attributes, alg );
6274 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006275 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006276
6277 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006278 psa_status_t status = psa_generate_key( &attributes, &key );
6279
6280 if( is_large_key > 0 )
6281 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6282 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006283 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006284 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006285
6286 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006287 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006288 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6289 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006290
Gilles Peskine818ca122018-06-20 18:16:48 +02006291 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006292 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006293 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006294
6295exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006296 /*
6297 * Key attributes may have been returned by psa_get_key_attributes()
6298 * thus reset them as required.
6299 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006300 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006301
Ronald Cron5425a212020-08-04 14:58:35 +02006302 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006303 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006304}
6305/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006306
Ronald Cronee414c72021-03-18 18:50:08 +01006307/* 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 +02006308void generate_key_rsa( int bits_arg,
6309 data_t *e_arg,
6310 int expected_status_arg )
6311{
Ronald Cron5425a212020-08-04 14:58:35 +02006312 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006313 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006314 size_t bits = bits_arg;
6315 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6316 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6317 psa_status_t expected_status = expected_status_arg;
6318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6319 uint8_t *exported = NULL;
6320 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006321 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006322 size_t exported_length = SIZE_MAX;
6323 uint8_t *e_read_buffer = NULL;
6324 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006325 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006326 size_t e_read_length = SIZE_MAX;
6327
6328 if( e_arg->len == 0 ||
6329 ( e_arg->len == 3 &&
6330 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6331 {
6332 is_default_public_exponent = 1;
6333 e_read_size = 0;
6334 }
6335 ASSERT_ALLOC( e_read_buffer, e_read_size );
6336 ASSERT_ALLOC( exported, exported_size );
6337
6338 PSA_ASSERT( psa_crypto_init( ) );
6339
6340 psa_set_key_usage_flags( &attributes, usage );
6341 psa_set_key_algorithm( &attributes, alg );
6342 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6343 e_arg->x, e_arg->len ) );
6344 psa_set_key_bits( &attributes, bits );
6345
6346 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006347 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006348 if( expected_status != PSA_SUCCESS )
6349 goto exit;
6350
6351 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006352 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006353 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6354 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6355 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6356 e_read_buffer, e_read_size,
6357 &e_read_length ) );
6358 if( is_default_public_exponent )
6359 TEST_EQUAL( e_read_length, 0 );
6360 else
6361 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6362
6363 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006364 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006365 goto exit;
6366
6367 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006368 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006369 exported, exported_size,
6370 &exported_length ) );
6371 {
6372 uint8_t *p = exported;
6373 uint8_t *end = exported + exported_length;
6374 size_t len;
6375 /* RSAPublicKey ::= SEQUENCE {
6376 * modulus INTEGER, -- n
6377 * publicExponent INTEGER } -- e
6378 */
6379 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006380 MBEDTLS_ASN1_SEQUENCE |
6381 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006382 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006383 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6384 MBEDTLS_ASN1_INTEGER ) );
6385 if( len >= 1 && p[0] == 0 )
6386 {
6387 ++p;
6388 --len;
6389 }
6390 if( e_arg->len == 0 )
6391 {
6392 TEST_EQUAL( len, 3 );
6393 TEST_EQUAL( p[0], 1 );
6394 TEST_EQUAL( p[1], 0 );
6395 TEST_EQUAL( p[2], 1 );
6396 }
6397 else
6398 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6399 }
6400
6401exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006402 /*
6403 * Key attributes may have been returned by psa_get_key_attributes() or
6404 * set by psa_set_key_domain_parameters() thus reset them as required.
6405 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006406 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006407
Ronald Cron5425a212020-08-04 14:58:35 +02006408 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006409 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006410 mbedtls_free( e_read_buffer );
6411 mbedtls_free( exported );
6412}
6413/* END_CASE */
6414
Darryl Greend49a4992018-06-18 17:27:26 +01006415/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006416void persistent_key_load_key_from_storage( data_t *data,
6417 int type_arg, int bits_arg,
6418 int usage_flags_arg, int alg_arg,
6419 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006420{
Ronald Cron71016a92020-08-28 19:01:50 +02006421 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006423 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6424 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006425 psa_key_type_t type = type_arg;
6426 size_t bits = bits_arg;
6427 psa_key_usage_t usage_flags = usage_flags_arg;
6428 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006429 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006430 unsigned char *first_export = NULL;
6431 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006432 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006433 size_t first_exported_length;
6434 size_t second_exported_length;
6435
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006436 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6437 {
6438 ASSERT_ALLOC( first_export, export_size );
6439 ASSERT_ALLOC( second_export, export_size );
6440 }
Darryl Greend49a4992018-06-18 17:27:26 +01006441
Gilles Peskine8817f612018-12-18 00:18:46 +01006442 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006443
Gilles Peskinec87af662019-05-15 16:12:22 +02006444 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006445 psa_set_key_usage_flags( &attributes, usage_flags );
6446 psa_set_key_algorithm( &attributes, alg );
6447 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006448 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006449
Darryl Green0c6575a2018-11-07 16:05:30 +00006450 switch( generation_method )
6451 {
6452 case IMPORT_KEY:
6453 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006454 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006455 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006456 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006457
Darryl Green0c6575a2018-11-07 16:05:30 +00006458 case GENERATE_KEY:
6459 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006460 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006461 break;
6462
6463 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006464#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006465 {
6466 /* Create base key */
6467 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6468 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6469 psa_set_key_usage_flags( &base_attributes,
6470 PSA_KEY_USAGE_DERIVE );
6471 psa_set_key_algorithm( &base_attributes, derive_alg );
6472 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006473 PSA_ASSERT( psa_import_key( &base_attributes,
6474 data->x, data->len,
6475 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006476 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006477 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006478 PSA_ASSERT( psa_key_derivation_input_key(
6479 &operation,
6480 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006481 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006482 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006483 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006484 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6485 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006486 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006487 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006488 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006489 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006490 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006491#else
6492 TEST_ASSUME( ! "KDF not supported in this configuration" );
6493#endif
6494 break;
6495
6496 default:
6497 TEST_ASSERT( ! "generation_method not implemented in test" );
6498 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006499 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006500 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006501
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006502 /* Export the key if permitted by the key policy. */
6503 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6504 {
Ronald Cron5425a212020-08-04 14:58:35 +02006505 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006506 first_export, export_size,
6507 &first_exported_length ) );
6508 if( generation_method == IMPORT_KEY )
6509 ASSERT_COMPARE( data->x, data->len,
6510 first_export, first_exported_length );
6511 }
Darryl Greend49a4992018-06-18 17:27:26 +01006512
6513 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006514 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006515 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006516 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006517
Darryl Greend49a4992018-06-18 17:27:26 +01006518 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006519 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006520 TEST_ASSERT( mbedtls_svc_key_id_equal(
6521 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006522 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6523 PSA_KEY_LIFETIME_PERSISTENT );
6524 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6525 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6526 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6527 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006528
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006529 /* Export the key again if permitted by the key policy. */
6530 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006531 {
Ronald Cron5425a212020-08-04 14:58:35 +02006532 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006533 second_export, export_size,
6534 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006535 ASSERT_COMPARE( first_export, first_exported_length,
6536 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006537 }
6538
6539 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006540 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006541 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006542
6543exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006544 /*
6545 * Key attributes may have been returned by psa_get_key_attributes()
6546 * thus reset them as required.
6547 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006548 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006549
Darryl Greend49a4992018-06-18 17:27:26 +01006550 mbedtls_free( first_export );
6551 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006552 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006553 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006554 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006555 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006556}
6557/* END_CASE */