blob: 7c3e9904d8954bec112aa5fd698289032c44ec49 [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
527 if( status != PSA_SUCCESS )
528 {
529 if( !expect_valid_signature )
530 {
531 /* Expected failure. */
532 test_ok = 1;
533 goto exit;
534 }
535 else
536 PSA_ASSERT( status );
537 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100540 if( output_data && output_part_length )
541 memcpy( ( output_data + output_length ), final_data,
542 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
549 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551 TEST_EQUAL( tag_length, tag_size );
552
553 if( output_data && tag_length )
554 memcpy( ( output_data + output_length ), tag_buffer,
555 tag_length );
556
557 output_length += tag_length;
558
559 TEST_EQUAL( output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_ASSERT( output_length <=
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
570 TEST_ASSERT( output_length <=
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Gilles Peskinee59236f2018-01-27 23:32:46 +0100592/* END_HEADER */
593
594/* BEGIN_DEPENDENCIES
595 * depends_on:MBEDTLS_PSA_CRYPTO_C
596 * END_DEPENDENCIES
597 */
598
599/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200600void static_checks( )
601{
602 size_t max_truncated_mac_size =
603 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
604
605 /* Check that the length for a truncated MAC always fits in the algorithm
606 * encoding. The shifted mask is the maximum truncated value. The
607 * untruncated algorithm may be one byte larger. */
608 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
609}
610/* END_CASE */
611
612/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200613void import_with_policy( int type_arg,
614 int usage_arg, int alg_arg,
615 int expected_status_arg )
616{
617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
618 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200620 psa_key_type_t type = type_arg;
621 psa_key_usage_t usage = usage_arg;
622 psa_algorithm_t alg = alg_arg;
623 psa_status_t expected_status = expected_status_arg;
624 const uint8_t key_material[16] = {0};
625 psa_status_t status;
626
627 PSA_ASSERT( psa_crypto_init( ) );
628
629 psa_set_key_type( &attributes, type );
630 psa_set_key_usage_flags( &attributes, usage );
631 psa_set_key_algorithm( &attributes, alg );
632
633 status = psa_import_key( &attributes,
634 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200635 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636 TEST_EQUAL( status, expected_status );
637 if( status != PSA_SUCCESS )
638 goto exit;
639
Ronald Cron5425a212020-08-04 14:58:35 +0200640 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
642 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
643 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200644 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200645
Ronald Cron5425a212020-08-04 14:58:35 +0200646 PSA_ASSERT( psa_destroy_key( key ) );
647 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200648
649exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100650 /*
651 * Key attributes may have been returned by psa_get_key_attributes()
652 * thus reset them as required.
653 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200654 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100655
656 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200657 PSA_DONE( );
658}
659/* END_CASE */
660
661/* BEGIN_CASE */
662void import_with_data( data_t *data, int type_arg,
663 int attr_bits_arg,
664 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200665{
666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
667 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200669 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200670 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200671 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100672 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Gilles Peskine8817f612018-12-18 00:18:46 +0100674 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675
Gilles Peskine4747d192019-04-17 15:05:45 +0200676 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200677 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200678
Ronald Cron5425a212020-08-04 14:58:35 +0200679 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100680 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200681 if( status != PSA_SUCCESS )
682 goto exit;
683
Ronald Cron5425a212020-08-04 14:58:35 +0200684 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200685 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200686 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200687 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200688 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200689
Ronald Cron5425a212020-08-04 14:58:35 +0200690 PSA_ASSERT( psa_destroy_key( key ) );
691 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100692
693exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100694 /*
695 * Key attributes may have been returned by psa_get_key_attributes()
696 * thus reset them as required.
697 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200698 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100699
700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200701 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100702}
703/* END_CASE */
704
705/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200706void import_large_key( int type_arg, int byte_size_arg,
707 int expected_status_arg )
708{
709 psa_key_type_t type = type_arg;
710 size_t byte_size = byte_size_arg;
711 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
712 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200713 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200714 psa_status_t status;
715 uint8_t *buffer = NULL;
716 size_t buffer_size = byte_size + 1;
717 size_t n;
718
Steven Cooreman69967ce2021-01-18 18:01:08 +0100719 /* Skip the test case if the target running the test cannot
720 * accomodate large keys due to heap size constraints */
721 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200722 memset( buffer, 'K', byte_size );
723
724 PSA_ASSERT( psa_crypto_init( ) );
725
726 /* Try importing the key */
727 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
728 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200729 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100730 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200731 TEST_EQUAL( status, expected_status );
732
733 if( status == PSA_SUCCESS )
734 {
Ronald Cron5425a212020-08-04 14:58:35 +0200735 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200736 TEST_EQUAL( psa_get_key_type( &attributes ), type );
737 TEST_EQUAL( psa_get_key_bits( &attributes ),
738 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200739 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200740 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200741 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200742 for( n = 0; n < byte_size; n++ )
743 TEST_EQUAL( buffer[n], 'K' );
744 for( n = byte_size; n < buffer_size; n++ )
745 TEST_EQUAL( buffer[n], 0 );
746 }
747
748exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100749 /*
750 * Key attributes may have been returned by psa_get_key_attributes()
751 * thus reset them as required.
752 */
753 psa_reset_key_attributes( &attributes );
754
Ronald Cron5425a212020-08-04 14:58:35 +0200755 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200756 PSA_DONE( );
757 mbedtls_free( buffer );
758}
759/* END_CASE */
760
761/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200762void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
763{
Ronald Cron5425a212020-08-04 14:58:35 +0200764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200765 size_t bits = bits_arg;
766 psa_status_t expected_status = expected_status_arg;
767 psa_status_t status;
768 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200769 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200770 size_t buffer_size = /* Slight overapproximations */
771 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200772 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200773 unsigned char *p;
774 int ret;
775 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200777
Gilles Peskine8817f612018-12-18 00:18:46 +0100778 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200779 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200780
781 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
782 bits, keypair ) ) >= 0 );
783 length = ret;
784
785 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200786 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200787 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100788 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200789
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200790 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200791 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200792
793exit:
794 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200795 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200796}
797/* END_CASE */
798
799/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300800void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300801 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200802 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803 int expected_bits,
804 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200805 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100806 int canonical_input )
807{
Ronald Cron5425a212020-08-04 14:58:35 +0200808 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100809 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200810 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200811 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100812 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100813 unsigned char *exported = NULL;
814 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100816 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100817 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200819 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820
Moran Pekercb088e72018-07-17 17:36:59 +0300821 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200822 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200824 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100825 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826
Gilles Peskine4747d192019-04-17 15:05:45 +0200827 psa_set_key_usage_flags( &attributes, usage_arg );
828 psa_set_key_algorithm( &attributes, alg );
829 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700830
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100831 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200832 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100833
834 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200835 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200836 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
837 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200838 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100839
840 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200841 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100842 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100843
844 /* The exported length must be set by psa_export_key() to a value between 0
845 * and export_size. On errors, the exported length must be 0. */
846 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
847 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
848 TEST_ASSERT( exported_length <= export_size );
849
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200850 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200851 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100852 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200853 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100854 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200856 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100857
Gilles Peskineea38a922021-02-13 00:05:16 +0100858 /* Run sanity checks on the exported key. For non-canonical inputs,
859 * this validates the canonical representations. For canonical inputs,
860 * this doesn't directly validate the implementation, but it still helps
861 * by cross-validating the test data with the sanity check code. */
862 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200863 goto exit;
864
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100865 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200866 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867 else
868 {
Ronald Cron5425a212020-08-04 14:58:35 +0200869 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200870 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200871 &key2 ) );
872 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100873 reexported,
874 export_size,
875 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200876 ASSERT_COMPARE( exported, exported_length,
877 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200878 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100879 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100880 TEST_ASSERT( exported_length <=
881 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
882 psa_get_key_bits( &got_attributes ) ) );
883 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884
885destroy:
886 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200887 PSA_ASSERT( psa_destroy_key( key ) );
888 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100889
890exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100891 /*
892 * Key attributes may have been returned by psa_get_key_attributes()
893 * thus reset them as required.
894 */
895 psa_reset_key_attributes( &got_attributes );
896
itayzafrir3e02b3b2018-06-12 17:06:52 +0300897 mbedtls_free( exported );
898 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200899 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100900}
901/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100902
Moran Pekerf709f4a2018-06-06 17:26:04 +0300903/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300904void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200905 int type_arg,
906 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100907 int export_size_delta,
908 int expected_export_status_arg,
909 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910{
Ronald Cron5425a212020-08-04 14:58:35 +0200911 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300912 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200913 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200914 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300915 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300916 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100917 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100918 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300920
Gilles Peskine8817f612018-12-18 00:18:46 +0100921 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300922
Gilles Peskine4747d192019-04-17 15:05:45 +0200923 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
924 psa_set_key_algorithm( &attributes, alg );
925 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300926
927 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200928 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929
Gilles Peskine49c25912018-10-29 15:15:31 +0100930 /* Export the public key */
931 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200932 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200933 exported, export_size,
934 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100935 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100936 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100937 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200938 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100939 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200940 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200941 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100942 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100943 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100944 TEST_ASSERT( expected_public_key->len <=
945 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
946 TEST_ASSERT( expected_public_key->len <=
947 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100948 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
949 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100950 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300951
952exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100953 /*
954 * Key attributes may have been returned by psa_get_key_attributes()
955 * thus reset them as required.
956 */
957 psa_reset_key_attributes( &attributes );
958
itayzafrir3e02b3b2018-06-12 17:06:52 +0300959 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200960 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200961 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300962}
963/* END_CASE */
964
Gilles Peskine20035e32018-02-03 22:44:14 +0100965/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200966void import_and_exercise_key( data_t *data,
967 int type_arg,
968 int bits_arg,
969 int alg_arg )
970{
Ronald Cron5425a212020-08-04 14:58:35 +0200971 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200972 psa_key_type_t type = type_arg;
973 size_t bits = bits_arg;
974 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100975 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200976 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200977 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200978
Gilles Peskine8817f612018-12-18 00:18:46 +0100979 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980
Gilles Peskine4747d192019-04-17 15:05:45 +0200981 psa_set_key_usage_flags( &attributes, usage );
982 psa_set_key_algorithm( &attributes, alg );
983 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200984
985 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200986 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200987
988 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200989 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200990 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
991 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200992
993 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100994 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200995 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200996
Ronald Cron5425a212020-08-04 14:58:35 +0200997 PSA_ASSERT( psa_destroy_key( key ) );
998 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200999
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001000exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001001 /*
1002 * Key attributes may have been returned by psa_get_key_attributes()
1003 * thus reset them as required.
1004 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001005 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001006
1007 psa_reset_key_attributes( &attributes );
1008 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001009 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001010}
1011/* END_CASE */
1012
1013/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001014void effective_key_attributes( int type_arg, int expected_type_arg,
1015 int bits_arg, int expected_bits_arg,
1016 int usage_arg, int expected_usage_arg,
1017 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001018{
Ronald Cron5425a212020-08-04 14:58:35 +02001019 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001020 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001021 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001022 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001023 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001024 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001025 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001026 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001027 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001029
Gilles Peskine8817f612018-12-18 00:18:46 +01001030 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001031
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001032 psa_set_key_usage_flags( &attributes, usage );
1033 psa_set_key_algorithm( &attributes, alg );
1034 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001035 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001036
Ronald Cron5425a212020-08-04 14:58:35 +02001037 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001038 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001039
Ronald Cron5425a212020-08-04 14:58:35 +02001040 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001041 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1042 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1043 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1044 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045
1046exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001047 /*
1048 * Key attributes may have been returned by psa_get_key_attributes()
1049 * thus reset them as required.
1050 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001051 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001052
1053 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001054 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001055}
1056/* END_CASE */
1057
1058/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001059void check_key_policy( int type_arg, int bits_arg,
1060 int usage_arg, int alg_arg )
1061{
1062 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1063 usage_arg, usage_arg, alg_arg, alg_arg );
1064 goto exit;
1065}
1066/* END_CASE */
1067
1068/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001069void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001070{
1071 /* Test each valid way of initializing the object, except for `= {0}`, as
1072 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1073 * though it's OK by the C standard. We could test for this, but we'd need
1074 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001075 psa_key_attributes_t func = psa_key_attributes_init( );
1076 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1077 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001078
1079 memset( &zero, 0, sizeof( zero ) );
1080
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001081 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1082 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1083 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001084
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001085 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1086 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1087 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1088
1089 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1090 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1091 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1092
1093 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1094 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1095 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1096
1097 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1098 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1099 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001100}
1101/* END_CASE */
1102
1103/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001104void mac_key_policy( int policy_usage,
1105 int policy_alg,
1106 int key_type,
1107 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001108 int exercise_alg,
1109 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001110{
Ronald Cron5425a212020-08-04 14:58:35 +02001111 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001113 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001114 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001115 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001116 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001117
Gilles Peskine8817f612018-12-18 00:18:46 +01001118 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001119
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001120 psa_set_key_usage_flags( &attributes, policy_usage );
1121 psa_set_key_algorithm( &attributes, policy_alg );
1122 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001123
Gilles Peskine049c7532019-05-15 20:22:09 +02001124 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001125 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001126
Ronald Cron5425a212020-08-04 14:58:35 +02001127 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001128 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001129 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001130 else
1131 TEST_EQUAL( status, expected_status );
1132
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001134
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001135 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001136 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001137 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001138 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001139 else
1140 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141
1142exit:
1143 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001144 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001145 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001146}
1147/* END_CASE */
1148
1149/* BEGIN_CASE */
1150void cipher_key_policy( int policy_usage,
1151 int policy_alg,
1152 int key_type,
1153 data_t *key_data,
1154 int exercise_alg )
1155{
Ronald Cron5425a212020-08-04 14:58:35 +02001156 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001158 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159 psa_status_t status;
1160
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001162
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001163 psa_set_key_usage_flags( &attributes, policy_usage );
1164 psa_set_key_algorithm( &attributes, policy_alg );
1165 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001166
Gilles Peskine049c7532019-05-15 20:22:09 +02001167 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001168 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169
Ronald Cron5425a212020-08-04 14:58:35 +02001170 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001171 if( policy_alg == exercise_alg &&
1172 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001173 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001174 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001175 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001176 psa_cipher_abort( &operation );
1177
Ronald Cron5425a212020-08-04 14:58:35 +02001178 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001179 if( policy_alg == exercise_alg &&
1180 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001181 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001183 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001184
1185exit:
1186 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001187 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001188 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001189}
1190/* END_CASE */
1191
1192/* BEGIN_CASE */
1193void aead_key_policy( int policy_usage,
1194 int policy_alg,
1195 int key_type,
1196 data_t *key_data,
1197 int nonce_length_arg,
1198 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001199 int exercise_alg,
1200 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201{
Ronald Cron5425a212020-08-04 14:58:35 +02001202 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001205 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001206 unsigned char nonce[16] = {0};
1207 size_t nonce_length = nonce_length_arg;
1208 unsigned char tag[16];
1209 size_t tag_length = tag_length_arg;
1210 size_t output_length;
1211
1212 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1213 TEST_ASSERT( tag_length <= sizeof( tag ) );
1214
Gilles Peskine8817f612018-12-18 00:18:46 +01001215 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001216
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001217 psa_set_key_usage_flags( &attributes, policy_usage );
1218 psa_set_key_algorithm( &attributes, policy_alg );
1219 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220
Gilles Peskine049c7532019-05-15 20:22:09 +02001221 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001222 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001223
Ronald Cron5425a212020-08-04 14:58:35 +02001224 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225 nonce, nonce_length,
1226 NULL, 0,
1227 NULL, 0,
1228 tag, tag_length,
1229 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001230 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1231 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001232 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001233 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001234
1235 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001236 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001237 nonce, nonce_length,
1238 NULL, 0,
1239 tag, tag_length,
1240 NULL, 0,
1241 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001242 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1243 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1244 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001245 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001246 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001247 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001248
1249exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001250 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001251 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252}
1253/* END_CASE */
1254
1255/* BEGIN_CASE */
1256void asymmetric_encryption_key_policy( int policy_usage,
1257 int policy_alg,
1258 int key_type,
1259 data_t *key_data,
1260 int exercise_alg )
1261{
Ronald Cron5425a212020-08-04 14:58:35 +02001262 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001264 psa_status_t status;
1265 size_t key_bits;
1266 size_t buffer_length;
1267 unsigned char *buffer = NULL;
1268 size_t output_length;
1269
Gilles Peskine8817f612018-12-18 00:18:46 +01001270 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001271
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001272 psa_set_key_usage_flags( &attributes, policy_usage );
1273 psa_set_key_algorithm( &attributes, policy_alg );
1274 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001275
Gilles Peskine049c7532019-05-15 20:22:09 +02001276 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001277 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001278
Ronald Cron5425a212020-08-04 14:58:35 +02001279 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001280 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001281 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1282 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001283 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001284
Ronald Cron5425a212020-08-04 14:58:35 +02001285 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001286 NULL, 0,
1287 NULL, 0,
1288 buffer, buffer_length,
1289 &output_length );
1290 if( policy_alg == exercise_alg &&
1291 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001292 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001293 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001294 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001295
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001296 if( buffer_length != 0 )
1297 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001298 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001299 buffer, buffer_length,
1300 NULL, 0,
1301 buffer, buffer_length,
1302 &output_length );
1303 if( policy_alg == exercise_alg &&
1304 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001305 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001306 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001307 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001308
1309exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001310 /*
1311 * Key attributes may have been returned by psa_get_key_attributes()
1312 * thus reset them as required.
1313 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001314 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001315
1316 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001317 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001318 mbedtls_free( buffer );
1319}
1320/* END_CASE */
1321
1322/* BEGIN_CASE */
1323void asymmetric_signature_key_policy( int policy_usage,
1324 int policy_alg,
1325 int key_type,
1326 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001327 int exercise_alg,
1328 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001329{
Ronald Cron5425a212020-08-04 14:58:35 +02001330 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001333 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1334 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1335 * compatible with the policy and `payload_length_arg` is supposed to be
1336 * a valid input length to sign. If `payload_length_arg <= 0`,
1337 * `exercise_alg` is supposed to be forbidden by the policy. */
1338 int compatible_alg = payload_length_arg > 0;
1339 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001340 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001341 size_t signature_length;
1342
Gilles Peskine8817f612018-12-18 00:18:46 +01001343 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001344
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001345 psa_set_key_usage_flags( &attributes, policy_usage );
1346 psa_set_key_algorithm( &attributes, policy_alg );
1347 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001348
Gilles Peskine049c7532019-05-15 20:22:09 +02001349 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001350 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001351
Ronald Cron5425a212020-08-04 14:58:35 +02001352 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001353 payload, payload_length,
1354 signature, sizeof( signature ),
1355 &signature_length );
1356 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001357 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001358 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001359 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001360
1361 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001362 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001363 payload, payload_length,
1364 signature, sizeof( signature ) );
1365 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001366 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001367 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001368 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001369
1370exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001371 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001372 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001373}
1374/* END_CASE */
1375
Janos Follathba3fab92019-06-11 14:50:16 +01001376/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001377void derive_key_policy( int policy_usage,
1378 int policy_alg,
1379 int key_type,
1380 data_t *key_data,
1381 int exercise_alg )
1382{
Ronald Cron5425a212020-08-04 14:58:35 +02001383 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001384 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001385 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001386 psa_status_t status;
1387
Gilles Peskine8817f612018-12-18 00:18:46 +01001388 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001389
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001390 psa_set_key_usage_flags( &attributes, policy_usage );
1391 psa_set_key_algorithm( &attributes, policy_alg );
1392 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001393
Gilles Peskine049c7532019-05-15 20:22:09 +02001394 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001395 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001396
Janos Follathba3fab92019-06-11 14:50:16 +01001397 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1398
1399 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1400 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001401 {
Janos Follathba3fab92019-06-11 14:50:16 +01001402 PSA_ASSERT( psa_key_derivation_input_bytes(
1403 &operation,
1404 PSA_KEY_DERIVATION_INPUT_SEED,
1405 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001406 }
Janos Follathba3fab92019-06-11 14:50:16 +01001407
1408 status = psa_key_derivation_input_key( &operation,
1409 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001410 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001411
Gilles Peskineea0fb492018-07-12 17:17:20 +02001412 if( policy_alg == exercise_alg &&
1413 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001414 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001415 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001416 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001417
1418exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001419 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001420 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001421 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001422}
1423/* END_CASE */
1424
1425/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001426void agreement_key_policy( int policy_usage,
1427 int policy_alg,
1428 int key_type_arg,
1429 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001430 int exercise_alg,
1431 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001432{
Ronald Cron5425a212020-08-04 14:58:35 +02001433 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001434 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001435 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001436 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001437 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001438 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001439
Gilles Peskine8817f612018-12-18 00:18:46 +01001440 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001442 psa_set_key_usage_flags( &attributes, policy_usage );
1443 psa_set_key_algorithm( &attributes, policy_alg );
1444 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001445
Gilles Peskine049c7532019-05-15 20:22:09 +02001446 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001447 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001448
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001449 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001450 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001451
Steven Cooremance48e852020-10-05 16:02:45 +02001452 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001453
1454exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001455 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001456 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001457 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001458}
1459/* END_CASE */
1460
1461/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001462void key_policy_alg2( int key_type_arg, data_t *key_data,
1463 int usage_arg, int alg_arg, int alg2_arg )
1464{
Ronald Cron5425a212020-08-04 14:58:35 +02001465 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001466 psa_key_type_t key_type = key_type_arg;
1467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1468 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1469 psa_key_usage_t usage = usage_arg;
1470 psa_algorithm_t alg = alg_arg;
1471 psa_algorithm_t alg2 = alg2_arg;
1472
1473 PSA_ASSERT( psa_crypto_init( ) );
1474
1475 psa_set_key_usage_flags( &attributes, usage );
1476 psa_set_key_algorithm( &attributes, alg );
1477 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1478 psa_set_key_type( &attributes, key_type );
1479 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001480 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001481
Ronald Cron5425a212020-08-04 14:58:35 +02001482 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001483 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1484 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1485 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1486
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001487 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001488 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001489 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001490 goto exit;
1491
1492exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001493 /*
1494 * Key attributes may have been returned by psa_get_key_attributes()
1495 * thus reset them as required.
1496 */
1497 psa_reset_key_attributes( &got_attributes );
1498
Ronald Cron5425a212020-08-04 14:58:35 +02001499 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001500 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001501}
1502/* END_CASE */
1503
1504/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001505void raw_agreement_key_policy( int policy_usage,
1506 int policy_alg,
1507 int key_type_arg,
1508 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001509 int exercise_alg,
1510 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001511{
Ronald Cron5425a212020-08-04 14:58:35 +02001512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001514 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001515 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001516 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001517 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001518
1519 PSA_ASSERT( psa_crypto_init( ) );
1520
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001521 psa_set_key_usage_flags( &attributes, policy_usage );
1522 psa_set_key_algorithm( &attributes, policy_alg );
1523 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001524
Gilles Peskine049c7532019-05-15 20:22:09 +02001525 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001526 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001527
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001528 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001529
Steven Cooremance48e852020-10-05 16:02:45 +02001530 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001531
1532exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001533 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001534 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001535 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001536}
1537/* END_CASE */
1538
1539/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001540void copy_success( int source_usage_arg,
1541 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001542 int type_arg, data_t *material,
1543 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001544 int target_usage_arg,
1545 int target_alg_arg, int target_alg2_arg,
1546 int expected_usage_arg,
1547 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001548{
Gilles Peskineca25db92019-04-19 11:43:08 +02001549 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1550 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001551 psa_key_usage_t expected_usage = expected_usage_arg;
1552 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001553 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001554 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1555 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001556 uint8_t *export_buffer = NULL;
1557
Gilles Peskine57ab7212019-01-28 13:03:09 +01001558 PSA_ASSERT( psa_crypto_init( ) );
1559
Gilles Peskineca25db92019-04-19 11:43:08 +02001560 /* Prepare the source key. */
1561 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1562 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001563 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001564 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001565 PSA_ASSERT( psa_import_key( &source_attributes,
1566 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001567 &source_key ) );
1568 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001569
Gilles Peskineca25db92019-04-19 11:43:08 +02001570 /* Prepare the target attributes. */
1571 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001572 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001573 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001574 /* Set volatile lifetime to reset the key identifier to 0. */
1575 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1576 }
1577
Gilles Peskineca25db92019-04-19 11:43:08 +02001578 if( target_usage_arg != -1 )
1579 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1580 if( target_alg_arg != -1 )
1581 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001582 if( target_alg2_arg != -1 )
1583 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001584
1585 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001586 PSA_ASSERT( psa_copy_key( source_key,
1587 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001588
1589 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001590 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001591
1592 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001593 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001594 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1595 psa_get_key_type( &target_attributes ) );
1596 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1597 psa_get_key_bits( &target_attributes ) );
1598 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1599 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001600 TEST_EQUAL( expected_alg2,
1601 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001602 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1603 {
1604 size_t length;
1605 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001606 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001607 material->len, &length ) );
1608 ASSERT_COMPARE( material->x, material->len,
1609 export_buffer, length );
1610 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001611
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001612 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001613 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001614 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001615 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001616
Ronald Cron5425a212020-08-04 14:58:35 +02001617 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001618
1619exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001620 /*
1621 * Source and target key attributes may have been returned by
1622 * psa_get_key_attributes() thus reset them as required.
1623 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001624 psa_reset_key_attributes( &source_attributes );
1625 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001626
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001627 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001628 mbedtls_free( export_buffer );
1629}
1630/* END_CASE */
1631
1632/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001633void copy_fail( int source_usage_arg,
1634 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001635 int type_arg, data_t *material,
1636 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001637 int target_usage_arg,
1638 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001639 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001640 int expected_status_arg )
1641{
1642 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1643 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001644 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1645 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001646 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001647
1648 PSA_ASSERT( psa_crypto_init( ) );
1649
1650 /* Prepare the source key. */
1651 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1652 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001653 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001654 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001655 PSA_ASSERT( psa_import_key( &source_attributes,
1656 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001657 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001658
1659 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001660 psa_set_key_id( &target_attributes, key_id );
1661 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001662 psa_set_key_type( &target_attributes, target_type_arg );
1663 psa_set_key_bits( &target_attributes, target_bits_arg );
1664 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1665 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001666 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001667
1668 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001669 TEST_EQUAL( psa_copy_key( source_key,
1670 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001671 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001672
Ronald Cron5425a212020-08-04 14:58:35 +02001673 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001674
Gilles Peskine4a644642019-05-03 17:14:08 +02001675exit:
1676 psa_reset_key_attributes( &source_attributes );
1677 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001678 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001679}
1680/* END_CASE */
1681
1682/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001683void hash_operation_init( )
1684{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001685 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001686 /* Test each valid way of initializing the object, except for `= {0}`, as
1687 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1688 * though it's OK by the C standard. We could test for this, but we'd need
1689 * to supress the Clang warning for the test. */
1690 psa_hash_operation_t func = psa_hash_operation_init( );
1691 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1692 psa_hash_operation_t zero;
1693
1694 memset( &zero, 0, sizeof( zero ) );
1695
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001696 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001697 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1698 PSA_ERROR_BAD_STATE );
1699 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1700 PSA_ERROR_BAD_STATE );
1701 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1702 PSA_ERROR_BAD_STATE );
1703
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001704 /* A default hash operation should be abortable without error. */
1705 PSA_ASSERT( psa_hash_abort( &func ) );
1706 PSA_ASSERT( psa_hash_abort( &init ) );
1707 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001708}
1709/* END_CASE */
1710
1711/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001712void hash_setup( int alg_arg,
1713 int expected_status_arg )
1714{
1715 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001716 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001717 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001718 psa_status_t status;
1719
Gilles Peskine8817f612018-12-18 00:18:46 +01001720 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001721
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001722 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001723 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001724
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001725 /* Whether setup succeeded or failed, abort must succeed. */
1726 PSA_ASSERT( psa_hash_abort( &operation ) );
1727
1728 /* If setup failed, reproduce the failure, so as to
1729 * test the resulting state of the operation object. */
1730 if( status != PSA_SUCCESS )
1731 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1732
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001733 /* Now the operation object should be reusable. */
1734#if defined(KNOWN_SUPPORTED_HASH_ALG)
1735 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1736 PSA_ASSERT( psa_hash_abort( &operation ) );
1737#endif
1738
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001739exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001740 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001741}
1742/* END_CASE */
1743
1744/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001745void hash_compute_fail( int alg_arg, data_t *input,
1746 int output_size_arg, int expected_status_arg )
1747{
1748 psa_algorithm_t alg = alg_arg;
1749 uint8_t *output = NULL;
1750 size_t output_size = output_size_arg;
1751 size_t output_length = INVALID_EXPORT_LENGTH;
1752 psa_status_t expected_status = expected_status_arg;
1753 psa_status_t status;
1754
1755 ASSERT_ALLOC( output, output_size );
1756
1757 PSA_ASSERT( psa_crypto_init( ) );
1758
1759 status = psa_hash_compute( alg, input->x, input->len,
1760 output, output_size, &output_length );
1761 TEST_EQUAL( status, expected_status );
1762 TEST_ASSERT( output_length <= output_size );
1763
1764exit:
1765 mbedtls_free( output );
1766 PSA_DONE( );
1767}
1768/* END_CASE */
1769
1770/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001771void hash_compare_fail( int alg_arg, data_t *input,
1772 data_t *reference_hash,
1773 int expected_status_arg )
1774{
1775 psa_algorithm_t alg = alg_arg;
1776 psa_status_t expected_status = expected_status_arg;
1777 psa_status_t status;
1778
1779 PSA_ASSERT( psa_crypto_init( ) );
1780
1781 status = psa_hash_compare( alg, input->x, input->len,
1782 reference_hash->x, reference_hash->len );
1783 TEST_EQUAL( status, expected_status );
1784
1785exit:
1786 PSA_DONE( );
1787}
1788/* END_CASE */
1789
1790/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001791void hash_compute_compare( int alg_arg, data_t *input,
1792 data_t *expected_output )
1793{
1794 psa_algorithm_t alg = alg_arg;
1795 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1796 size_t output_length = INVALID_EXPORT_LENGTH;
1797 size_t i;
1798
1799 PSA_ASSERT( psa_crypto_init( ) );
1800
1801 /* Compute with tight buffer */
1802 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001803 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001804 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001805 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001806 ASSERT_COMPARE( output, output_length,
1807 expected_output->x, expected_output->len );
1808
1809 /* Compute with larger buffer */
1810 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1811 output, sizeof( output ),
1812 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001813 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001814 ASSERT_COMPARE( output, output_length,
1815 expected_output->x, expected_output->len );
1816
1817 /* Compare with correct hash */
1818 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1819 output, output_length ) );
1820
1821 /* Compare with trailing garbage */
1822 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1823 output, output_length + 1 ),
1824 PSA_ERROR_INVALID_SIGNATURE );
1825
1826 /* Compare with truncated hash */
1827 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1828 output, output_length - 1 ),
1829 PSA_ERROR_INVALID_SIGNATURE );
1830
1831 /* Compare with corrupted value */
1832 for( i = 0; i < output_length; i++ )
1833 {
Chris Jones9634bb12021-01-20 15:56:42 +00001834 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001835 output[i] ^= 1;
1836 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1837 output, output_length ),
1838 PSA_ERROR_INVALID_SIGNATURE );
1839 output[i] ^= 1;
1840 }
1841
1842exit:
1843 PSA_DONE( );
1844}
1845/* END_CASE */
1846
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001847/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001848void hash_bad_order( )
1849{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001850 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001851 unsigned char input[] = "";
1852 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001853 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001854 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1855 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1856 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001857 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001858 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001859 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001860
Gilles Peskine8817f612018-12-18 00:18:46 +01001861 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001862
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001863 /* Call setup twice in a row. */
1864 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1865 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1866 PSA_ERROR_BAD_STATE );
1867 PSA_ASSERT( psa_hash_abort( &operation ) );
1868
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001869 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001870 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001871 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001872 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001873
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001874 /* Call update after finish. */
1875 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1876 PSA_ASSERT( psa_hash_finish( &operation,
1877 hash, sizeof( hash ), &hash_len ) );
1878 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001879 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001880 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001881
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001882 /* Call verify without calling setup beforehand. */
1883 TEST_EQUAL( psa_hash_verify( &operation,
1884 valid_hash, sizeof( valid_hash ) ),
1885 PSA_ERROR_BAD_STATE );
1886 PSA_ASSERT( psa_hash_abort( &operation ) );
1887
1888 /* Call verify after finish. */
1889 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1890 PSA_ASSERT( psa_hash_finish( &operation,
1891 hash, sizeof( hash ), &hash_len ) );
1892 TEST_EQUAL( psa_hash_verify( &operation,
1893 valid_hash, sizeof( valid_hash ) ),
1894 PSA_ERROR_BAD_STATE );
1895 PSA_ASSERT( psa_hash_abort( &operation ) );
1896
1897 /* Call verify twice in a row. */
1898 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1899 PSA_ASSERT( psa_hash_verify( &operation,
1900 valid_hash, sizeof( valid_hash ) ) );
1901 TEST_EQUAL( psa_hash_verify( &operation,
1902 valid_hash, sizeof( valid_hash ) ),
1903 PSA_ERROR_BAD_STATE );
1904 PSA_ASSERT( psa_hash_abort( &operation ) );
1905
1906 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001907 TEST_EQUAL( psa_hash_finish( &operation,
1908 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001909 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001910 PSA_ASSERT( psa_hash_abort( &operation ) );
1911
1912 /* Call finish twice in a row. */
1913 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1914 PSA_ASSERT( psa_hash_finish( &operation,
1915 hash, sizeof( hash ), &hash_len ) );
1916 TEST_EQUAL( psa_hash_finish( &operation,
1917 hash, sizeof( hash ), &hash_len ),
1918 PSA_ERROR_BAD_STATE );
1919 PSA_ASSERT( psa_hash_abort( &operation ) );
1920
1921 /* Call finish after calling verify. */
1922 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1923 PSA_ASSERT( psa_hash_verify( &operation,
1924 valid_hash, sizeof( valid_hash ) ) );
1925 TEST_EQUAL( psa_hash_finish( &operation,
1926 hash, sizeof( hash ), &hash_len ),
1927 PSA_ERROR_BAD_STATE );
1928 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001929
1930exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001931 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001932}
1933/* END_CASE */
1934
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001935/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001936void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001937{
1938 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001939 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1940 * appended to it */
1941 unsigned char hash[] = {
1942 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1943 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1944 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001945 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001946 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001947
Gilles Peskine8817f612018-12-18 00:18:46 +01001948 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001949
itayzafrir27e69452018-11-01 14:26:34 +02001950 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001951 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001952 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001953 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001954
itayzafrir27e69452018-11-01 14:26:34 +02001955 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001956 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001957 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001958 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001959
itayzafrir27e69452018-11-01 14:26:34 +02001960 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001961 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001962 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001963 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001964
itayzafrirec93d302018-10-18 18:01:10 +03001965exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001966 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001967}
1968/* END_CASE */
1969
Ronald Cronee414c72021-03-18 18:50:08 +01001970/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001971void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001972{
1973 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001974 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001975 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001976 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001977 size_t hash_len;
1978
Gilles Peskine8817f612018-12-18 00:18:46 +01001979 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001980
itayzafrir58028322018-10-25 10:22:01 +03001981 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001982 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001983 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001984 hash, expected_size - 1, &hash_len ),
1985 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001986
1987exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001988 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001989}
1990/* END_CASE */
1991
Ronald Cronee414c72021-03-18 18:50:08 +01001992/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001993void hash_clone_source_state( )
1994{
1995 psa_algorithm_t alg = PSA_ALG_SHA_256;
1996 unsigned char hash[PSA_HASH_MAX_SIZE];
1997 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1998 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1999 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2000 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2001 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2002 size_t hash_len;
2003
2004 PSA_ASSERT( psa_crypto_init( ) );
2005 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2006
2007 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2008 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2009 PSA_ASSERT( psa_hash_finish( &op_finished,
2010 hash, sizeof( hash ), &hash_len ) );
2011 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2012 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2013
2014 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2015 PSA_ERROR_BAD_STATE );
2016
2017 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2018 PSA_ASSERT( psa_hash_finish( &op_init,
2019 hash, sizeof( hash ), &hash_len ) );
2020 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2021 PSA_ASSERT( psa_hash_finish( &op_finished,
2022 hash, sizeof( hash ), &hash_len ) );
2023 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2024 PSA_ASSERT( psa_hash_finish( &op_aborted,
2025 hash, sizeof( hash ), &hash_len ) );
2026
2027exit:
2028 psa_hash_abort( &op_source );
2029 psa_hash_abort( &op_init );
2030 psa_hash_abort( &op_setup );
2031 psa_hash_abort( &op_finished );
2032 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002033 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002034}
2035/* END_CASE */
2036
Ronald Cronee414c72021-03-18 18:50:08 +01002037/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002038void hash_clone_target_state( )
2039{
2040 psa_algorithm_t alg = PSA_ALG_SHA_256;
2041 unsigned char hash[PSA_HASH_MAX_SIZE];
2042 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2043 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2044 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2045 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2046 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2047 size_t hash_len;
2048
2049 PSA_ASSERT( psa_crypto_init( ) );
2050
2051 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2052 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2053 PSA_ASSERT( psa_hash_finish( &op_finished,
2054 hash, sizeof( hash ), &hash_len ) );
2055 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2056 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2057
2058 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2059 PSA_ASSERT( psa_hash_finish( &op_target,
2060 hash, sizeof( hash ), &hash_len ) );
2061
2062 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2063 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2064 PSA_ERROR_BAD_STATE );
2065 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2066 PSA_ERROR_BAD_STATE );
2067
2068exit:
2069 psa_hash_abort( &op_target );
2070 psa_hash_abort( &op_init );
2071 psa_hash_abort( &op_setup );
2072 psa_hash_abort( &op_finished );
2073 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002074 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002075}
2076/* END_CASE */
2077
itayzafrir58028322018-10-25 10:22:01 +03002078/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002079void mac_operation_init( )
2080{
Jaeden Amero252ef282019-02-15 14:05:35 +00002081 const uint8_t input[1] = { 0 };
2082
Jaeden Amero769ce272019-01-04 11:48:03 +00002083 /* Test each valid way of initializing the object, except for `= {0}`, as
2084 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2085 * though it's OK by the C standard. We could test for this, but we'd need
2086 * to supress the Clang warning for the test. */
2087 psa_mac_operation_t func = psa_mac_operation_init( );
2088 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2089 psa_mac_operation_t zero;
2090
2091 memset( &zero, 0, sizeof( zero ) );
2092
Jaeden Amero252ef282019-02-15 14:05:35 +00002093 /* A freshly-initialized MAC operation should not be usable. */
2094 TEST_EQUAL( psa_mac_update( &func,
2095 input, sizeof( input ) ),
2096 PSA_ERROR_BAD_STATE );
2097 TEST_EQUAL( psa_mac_update( &init,
2098 input, sizeof( input ) ),
2099 PSA_ERROR_BAD_STATE );
2100 TEST_EQUAL( psa_mac_update( &zero,
2101 input, sizeof( input ) ),
2102 PSA_ERROR_BAD_STATE );
2103
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002104 /* A default MAC operation should be abortable without error. */
2105 PSA_ASSERT( psa_mac_abort( &func ) );
2106 PSA_ASSERT( psa_mac_abort( &init ) );
2107 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002108}
2109/* END_CASE */
2110
2111/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002112void mac_setup( int key_type_arg,
2113 data_t *key,
2114 int alg_arg,
2115 int expected_status_arg )
2116{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002117 psa_key_type_t key_type = key_type_arg;
2118 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002119 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002120 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002121 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2122#if defined(KNOWN_SUPPORTED_MAC_ALG)
2123 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2124#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002125
Gilles Peskine8817f612018-12-18 00:18:46 +01002126 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002127
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002128 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2129 &operation, &status ) )
2130 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002131 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002132
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002133 /* The operation object should be reusable. */
2134#if defined(KNOWN_SUPPORTED_MAC_ALG)
2135 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2136 smoke_test_key_data,
2137 sizeof( smoke_test_key_data ),
2138 KNOWN_SUPPORTED_MAC_ALG,
2139 &operation, &status ) )
2140 goto exit;
2141 TEST_EQUAL( status, PSA_SUCCESS );
2142#endif
2143
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002144exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002145 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002146}
2147/* END_CASE */
2148
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002149/* 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 +00002150void mac_bad_order( )
2151{
Ronald Cron5425a212020-08-04 14:58:35 +02002152 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002153 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2154 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002155 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002156 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2157 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2158 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002160 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2161 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2162 size_t sign_mac_length = 0;
2163 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2164 const uint8_t verify_mac[] = {
2165 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2166 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2167 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2168
2169 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002170 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002171 psa_set_key_algorithm( &attributes, alg );
2172 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002173
Ronald Cron5425a212020-08-04 14:58:35 +02002174 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2175 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002176
Jaeden Amero252ef282019-02-15 14:05:35 +00002177 /* Call update without calling setup beforehand. */
2178 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2179 PSA_ERROR_BAD_STATE );
2180 PSA_ASSERT( psa_mac_abort( &operation ) );
2181
2182 /* Call sign finish without calling setup beforehand. */
2183 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2184 &sign_mac_length),
2185 PSA_ERROR_BAD_STATE );
2186 PSA_ASSERT( psa_mac_abort( &operation ) );
2187
2188 /* Call verify finish without calling setup beforehand. */
2189 TEST_EQUAL( psa_mac_verify_finish( &operation,
2190 verify_mac, sizeof( verify_mac ) ),
2191 PSA_ERROR_BAD_STATE );
2192 PSA_ASSERT( psa_mac_abort( &operation ) );
2193
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002194 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002195 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2196 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002197 PSA_ERROR_BAD_STATE );
2198 PSA_ASSERT( psa_mac_abort( &operation ) );
2199
Jaeden Amero252ef282019-02-15 14:05:35 +00002200 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002201 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002202 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2203 PSA_ASSERT( psa_mac_sign_finish( &operation,
2204 sign_mac, sizeof( sign_mac ),
2205 &sign_mac_length ) );
2206 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2207 PSA_ERROR_BAD_STATE );
2208 PSA_ASSERT( psa_mac_abort( &operation ) );
2209
2210 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002211 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002212 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2213 PSA_ASSERT( psa_mac_verify_finish( &operation,
2214 verify_mac, sizeof( verify_mac ) ) );
2215 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2216 PSA_ERROR_BAD_STATE );
2217 PSA_ASSERT( psa_mac_abort( &operation ) );
2218
2219 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002220 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002221 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2222 PSA_ASSERT( psa_mac_sign_finish( &operation,
2223 sign_mac, sizeof( sign_mac ),
2224 &sign_mac_length ) );
2225 TEST_EQUAL( psa_mac_sign_finish( &operation,
2226 sign_mac, sizeof( sign_mac ),
2227 &sign_mac_length ),
2228 PSA_ERROR_BAD_STATE );
2229 PSA_ASSERT( psa_mac_abort( &operation ) );
2230
2231 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002232 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002233 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2234 PSA_ASSERT( psa_mac_verify_finish( &operation,
2235 verify_mac, sizeof( verify_mac ) ) );
2236 TEST_EQUAL( psa_mac_verify_finish( &operation,
2237 verify_mac, sizeof( verify_mac ) ),
2238 PSA_ERROR_BAD_STATE );
2239 PSA_ASSERT( psa_mac_abort( &operation ) );
2240
2241 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002242 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002243 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2244 TEST_EQUAL( psa_mac_verify_finish( &operation,
2245 verify_mac, sizeof( verify_mac ) ),
2246 PSA_ERROR_BAD_STATE );
2247 PSA_ASSERT( psa_mac_abort( &operation ) );
2248
2249 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002250 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002251 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2252 TEST_EQUAL( psa_mac_sign_finish( &operation,
2253 sign_mac, sizeof( sign_mac ),
2254 &sign_mac_length ),
2255 PSA_ERROR_BAD_STATE );
2256 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002257
Ronald Cron5425a212020-08-04 14:58:35 +02002258 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002259
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002260exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002261 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002262}
2263/* END_CASE */
2264
2265/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002266void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002267 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002268 int alg_arg,
2269 data_t *input,
2270 data_t *expected_mac )
2271{
Ronald Cron5425a212020-08-04 14:58:35 +02002272 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002273 psa_key_type_t key_type = key_type_arg;
2274 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002275 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002277 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002278 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002279 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002280 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002281 const size_t output_sizes_to_test[] = {
2282 0,
2283 1,
2284 expected_mac->len - 1,
2285 expected_mac->len,
2286 expected_mac->len + 1,
2287 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002288
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002289 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002290 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002291 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002292
Gilles Peskine8817f612018-12-18 00:18:46 +01002293 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002294
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002295 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002296 psa_set_key_algorithm( &attributes, alg );
2297 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002298
Ronald Cron5425a212020-08-04 14:58:35 +02002299 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2300 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002301
Gilles Peskine8b356b52020-08-25 23:44:59 +02002302 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2303 {
2304 const size_t output_size = output_sizes_to_test[i];
2305 psa_status_t expected_status =
2306 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2307 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002308
Chris Jones9634bb12021-01-20 15:56:42 +00002309 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002310 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002311
Gilles Peskine8b356b52020-08-25 23:44:59 +02002312 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002313 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002314 PSA_ASSERT( psa_mac_update( &operation,
2315 input->x, input->len ) );
2316 TEST_EQUAL( psa_mac_sign_finish( &operation,
2317 actual_mac, output_size,
2318 &mac_length ),
2319 expected_status );
2320 PSA_ASSERT( psa_mac_abort( &operation ) );
2321
2322 if( expected_status == PSA_SUCCESS )
2323 {
2324 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2325 actual_mac, mac_length );
2326 }
2327 mbedtls_free( actual_mac );
2328 actual_mac = NULL;
2329 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002330
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002331exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002332 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002333 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002334 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002335 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002336}
2337/* END_CASE */
2338
2339/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002340void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002341 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002342 int alg_arg,
2343 data_t *input,
2344 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002345{
Ronald Cron5425a212020-08-04 14:58:35 +02002346 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002347 psa_key_type_t key_type = key_type_arg;
2348 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002349 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002351 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002352
Gilles Peskine69c12672018-06-28 00:07:19 +02002353 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2354
Gilles Peskine8817f612018-12-18 00:18:46 +01002355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002356
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002357 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002358 psa_set_key_algorithm( &attributes, alg );
2359 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002360
Ronald Cron5425a212020-08-04 14:58:35 +02002361 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2362 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002363
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002364 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002365 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002366 PSA_ASSERT( psa_mac_update( &operation,
2367 input->x, input->len ) );
2368 PSA_ASSERT( psa_mac_verify_finish( &operation,
2369 expected_mac->x,
2370 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002371
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002372 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002373 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002374 PSA_ASSERT( psa_mac_update( &operation,
2375 input->x, input->len ) );
2376 TEST_EQUAL( psa_mac_verify_finish( &operation,
2377 expected_mac->x,
2378 expected_mac->len - 1 ),
2379 PSA_ERROR_INVALID_SIGNATURE );
2380
2381 /* Test a MAC that's too long. */
2382 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2383 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002384 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002385 PSA_ASSERT( psa_mac_update( &operation,
2386 input->x, input->len ) );
2387 TEST_EQUAL( psa_mac_verify_finish( &operation,
2388 perturbed_mac,
2389 expected_mac->len + 1 ),
2390 PSA_ERROR_INVALID_SIGNATURE );
2391
2392 /* Test changing one byte. */
2393 for( size_t i = 0; i < expected_mac->len; i++ )
2394 {
Chris Jones9634bb12021-01-20 15:56:42 +00002395 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002396 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002397 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002398 PSA_ASSERT( psa_mac_update( &operation,
2399 input->x, input->len ) );
2400 TEST_EQUAL( psa_mac_verify_finish( &operation,
2401 perturbed_mac,
2402 expected_mac->len ),
2403 PSA_ERROR_INVALID_SIGNATURE );
2404 perturbed_mac[i] ^= 1;
2405 }
2406
Gilles Peskine8c9def32018-02-08 10:02:12 +01002407exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002408 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002409 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002410 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002411 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002412}
2413/* END_CASE */
2414
2415/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002416void cipher_operation_init( )
2417{
Jaeden Ameroab439972019-02-15 14:12:05 +00002418 const uint8_t input[1] = { 0 };
2419 unsigned char output[1] = { 0 };
2420 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002421 /* Test each valid way of initializing the object, except for `= {0}`, as
2422 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2423 * though it's OK by the C standard. We could test for this, but we'd need
2424 * to supress the Clang warning for the test. */
2425 psa_cipher_operation_t func = psa_cipher_operation_init( );
2426 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2427 psa_cipher_operation_t zero;
2428
2429 memset( &zero, 0, sizeof( zero ) );
2430
Jaeden Ameroab439972019-02-15 14:12:05 +00002431 /* A freshly-initialized cipher operation should not be usable. */
2432 TEST_EQUAL( psa_cipher_update( &func,
2433 input, sizeof( input ),
2434 output, sizeof( output ),
2435 &output_length ),
2436 PSA_ERROR_BAD_STATE );
2437 TEST_EQUAL( psa_cipher_update( &init,
2438 input, sizeof( input ),
2439 output, sizeof( output ),
2440 &output_length ),
2441 PSA_ERROR_BAD_STATE );
2442 TEST_EQUAL( psa_cipher_update( &zero,
2443 input, sizeof( input ),
2444 output, sizeof( output ),
2445 &output_length ),
2446 PSA_ERROR_BAD_STATE );
2447
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002448 /* A default cipher operation should be abortable without error. */
2449 PSA_ASSERT( psa_cipher_abort( &func ) );
2450 PSA_ASSERT( psa_cipher_abort( &init ) );
2451 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002452}
2453/* END_CASE */
2454
2455/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002456void cipher_setup( int key_type_arg,
2457 data_t *key,
2458 int alg_arg,
2459 int expected_status_arg )
2460{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002461 psa_key_type_t key_type = key_type_arg;
2462 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002463 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002464 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002465 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002466#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002467 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2468#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002469
Gilles Peskine8817f612018-12-18 00:18:46 +01002470 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002472 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2473 &operation, &status ) )
2474 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002475 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002477 /* The operation object should be reusable. */
2478#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2479 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2480 smoke_test_key_data,
2481 sizeof( smoke_test_key_data ),
2482 KNOWN_SUPPORTED_CIPHER_ALG,
2483 &operation, &status ) )
2484 goto exit;
2485 TEST_EQUAL( status, PSA_SUCCESS );
2486#endif
2487
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002488exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002489 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002490 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002491}
2492/* END_CASE */
2493
Ronald Cronee414c72021-03-18 18:50:08 +01002494/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002495void cipher_bad_order( )
2496{
Ronald Cron5425a212020-08-04 14:58:35 +02002497 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002498 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2499 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002501 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002502 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002503 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002504 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2505 0xaa, 0xaa, 0xaa, 0xaa };
2506 const uint8_t text[] = {
2507 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2508 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002509 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002510 size_t length = 0;
2511
2512 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002513 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2514 psa_set_key_algorithm( &attributes, alg );
2515 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002516 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2517 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002518
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002519 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002520 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2521 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002522 PSA_ERROR_BAD_STATE );
2523 PSA_ASSERT( psa_cipher_abort( &operation ) );
2524
2525 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002526 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2527 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002528 PSA_ERROR_BAD_STATE );
2529 PSA_ASSERT( psa_cipher_abort( &operation ) );
2530
Jaeden Ameroab439972019-02-15 14:12:05 +00002531 /* Generate an IV without calling setup beforehand. */
2532 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2533 buffer, sizeof( buffer ),
2534 &length ),
2535 PSA_ERROR_BAD_STATE );
2536 PSA_ASSERT( psa_cipher_abort( &operation ) );
2537
2538 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002539 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002540 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2541 buffer, sizeof( buffer ),
2542 &length ) );
2543 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2544 buffer, sizeof( buffer ),
2545 &length ),
2546 PSA_ERROR_BAD_STATE );
2547 PSA_ASSERT( psa_cipher_abort( &operation ) );
2548
2549 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002550 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002551 PSA_ASSERT( psa_cipher_set_iv( &operation,
2552 iv, sizeof( iv ) ) );
2553 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2554 buffer, sizeof( buffer ),
2555 &length ),
2556 PSA_ERROR_BAD_STATE );
2557 PSA_ASSERT( psa_cipher_abort( &operation ) );
2558
2559 /* Set an IV without calling setup beforehand. */
2560 TEST_EQUAL( psa_cipher_set_iv( &operation,
2561 iv, sizeof( iv ) ),
2562 PSA_ERROR_BAD_STATE );
2563 PSA_ASSERT( psa_cipher_abort( &operation ) );
2564
2565 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002566 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002567 PSA_ASSERT( psa_cipher_set_iv( &operation,
2568 iv, sizeof( iv ) ) );
2569 TEST_EQUAL( psa_cipher_set_iv( &operation,
2570 iv, sizeof( iv ) ),
2571 PSA_ERROR_BAD_STATE );
2572 PSA_ASSERT( psa_cipher_abort( &operation ) );
2573
2574 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002575 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002576 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2577 buffer, sizeof( buffer ),
2578 &length ) );
2579 TEST_EQUAL( psa_cipher_set_iv( &operation,
2580 iv, sizeof( iv ) ),
2581 PSA_ERROR_BAD_STATE );
2582 PSA_ASSERT( psa_cipher_abort( &operation ) );
2583
2584 /* Call update without calling setup beforehand. */
2585 TEST_EQUAL( psa_cipher_update( &operation,
2586 text, sizeof( text ),
2587 buffer, sizeof( buffer ),
2588 &length ),
2589 PSA_ERROR_BAD_STATE );
2590 PSA_ASSERT( psa_cipher_abort( &operation ) );
2591
2592 /* Call update without an IV where an IV is required. */
2593 TEST_EQUAL( psa_cipher_update( &operation,
2594 text, sizeof( text ),
2595 buffer, sizeof( buffer ),
2596 &length ),
2597 PSA_ERROR_BAD_STATE );
2598 PSA_ASSERT( psa_cipher_abort( &operation ) );
2599
2600 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002601 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002602 PSA_ASSERT( psa_cipher_set_iv( &operation,
2603 iv, sizeof( iv ) ) );
2604 PSA_ASSERT( psa_cipher_finish( &operation,
2605 buffer, sizeof( buffer ), &length ) );
2606 TEST_EQUAL( psa_cipher_update( &operation,
2607 text, sizeof( text ),
2608 buffer, sizeof( buffer ),
2609 &length ),
2610 PSA_ERROR_BAD_STATE );
2611 PSA_ASSERT( psa_cipher_abort( &operation ) );
2612
2613 /* Call finish without calling setup beforehand. */
2614 TEST_EQUAL( psa_cipher_finish( &operation,
2615 buffer, sizeof( buffer ), &length ),
2616 PSA_ERROR_BAD_STATE );
2617 PSA_ASSERT( psa_cipher_abort( &operation ) );
2618
2619 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002620 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002621 /* Not calling update means we are encrypting an empty buffer, which is OK
2622 * for cipher modes with padding. */
2623 TEST_EQUAL( psa_cipher_finish( &operation,
2624 buffer, sizeof( buffer ), &length ),
2625 PSA_ERROR_BAD_STATE );
2626 PSA_ASSERT( psa_cipher_abort( &operation ) );
2627
2628 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002629 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002630 PSA_ASSERT( psa_cipher_set_iv( &operation,
2631 iv, sizeof( iv ) ) );
2632 PSA_ASSERT( psa_cipher_finish( &operation,
2633 buffer, sizeof( buffer ), &length ) );
2634 TEST_EQUAL( psa_cipher_finish( &operation,
2635 buffer, sizeof( buffer ), &length ),
2636 PSA_ERROR_BAD_STATE );
2637 PSA_ASSERT( psa_cipher_abort( &operation ) );
2638
Ronald Cron5425a212020-08-04 14:58:35 +02002639 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002640
Jaeden Ameroab439972019-02-15 14:12:05 +00002641exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002642 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002643 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002644}
2645/* END_CASE */
2646
2647/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002648void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002649 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002650 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002651 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652{
Ronald Cron5425a212020-08-04 14:58:35 +02002653 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654 psa_status_t status;
2655 psa_key_type_t key_type = key_type_arg;
2656 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002657 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002658 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659 size_t output_buffer_size = 0;
2660 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002661 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002662 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002664
Gilles Peskine8817f612018-12-18 00:18:46 +01002665 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002667 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2668 psa_set_key_algorithm( &attributes, alg );
2669 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002670
Ronald Cron5425a212020-08-04 14:58:35 +02002671 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2672 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Ronald Cron5425a212020-08-04 14:58:35 +02002674 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002676 if( iv->len > 0 )
2677 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002678 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002679 }
2680
gabor-mezei-armceface22021-01-21 12:26:17 +01002681 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2682 TEST_ASSERT( output_buffer_size <=
2683 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002684 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685
Gilles Peskine8817f612018-12-18 00:18:46 +01002686 PSA_ASSERT( psa_cipher_update( &operation,
2687 input->x, input->len,
2688 output, output_buffer_size,
2689 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002690 TEST_ASSERT( function_output_length <=
2691 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2692 TEST_ASSERT( function_output_length <=
2693 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002694 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002695
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002697 ( output_buffer_size == 0 ? NULL :
2698 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002699 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002701 TEST_ASSERT( function_output_length <=
2702 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2703 TEST_ASSERT( function_output_length <=
2704 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002705 total_output_length += function_output_length;
2706
Gilles Peskinefe11b722018-12-18 00:24:04 +01002707 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708 if( expected_status == PSA_SUCCESS )
2709 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002710 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002711 ASSERT_COMPARE( expected_output->x, expected_output->len,
2712 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002714
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002716 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002717 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002718 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002719 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720}
2721/* END_CASE */
2722
2723/* BEGIN_CASE */
2724void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002725 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002726 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002727 int first_part_size_arg,
2728 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002729 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002730{
Ronald Cron5425a212020-08-04 14:58:35 +02002731 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002732 psa_key_type_t key_type = key_type_arg;
2733 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002734 size_t first_part_size = first_part_size_arg;
2735 size_t output1_length = output1_length_arg;
2736 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002737 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738 size_t output_buffer_size = 0;
2739 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002740 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002741 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002742 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002743
Gilles Peskine8817f612018-12-18 00:18:46 +01002744 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002745
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002746 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2747 psa_set_key_algorithm( &attributes, alg );
2748 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002749
Ronald Cron5425a212020-08-04 14:58:35 +02002750 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2751 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752
Ronald Cron5425a212020-08-04 14:58:35 +02002753 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002754
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002755 if( iv->len > 0 )
2756 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002757 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002758 }
2759
gabor-mezei-armceface22021-01-21 12:26:17 +01002760 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2761 TEST_ASSERT( output_buffer_size <=
2762 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002763 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002764
Gilles Peskinee0866522019-02-19 19:44:00 +01002765 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002766 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2767 output, output_buffer_size,
2768 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002769 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002770 TEST_ASSERT( function_output_length <=
2771 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2772 TEST_ASSERT( function_output_length <=
2773 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002774 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002775
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 PSA_ASSERT( psa_cipher_update( &operation,
2777 input->x + first_part_size,
2778 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002779 ( output_buffer_size == 0 ? NULL :
2780 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002781 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002782 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002783 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002784 TEST_ASSERT( function_output_length <=
2785 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2786 alg,
2787 input->len - first_part_size ) );
2788 TEST_ASSERT( function_output_length <=
2789 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002790 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002791
Gilles Peskine8817f612018-12-18 00:18:46 +01002792 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002793 ( output_buffer_size == 0 ? NULL :
2794 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002795 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002796 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002797 TEST_ASSERT( function_output_length <=
2798 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2799 TEST_ASSERT( function_output_length <=
2800 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002801 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002803
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002804 ASSERT_COMPARE( expected_output->x, expected_output->len,
2805 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002806
2807exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002808 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002809 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002810 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002811 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812}
2813/* END_CASE */
2814
2815/* BEGIN_CASE */
2816void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002817 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002818 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002819 int first_part_size_arg,
2820 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002821 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822{
Ronald Cron5425a212020-08-04 14:58:35 +02002823 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002824 psa_key_type_t key_type = key_type_arg;
2825 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002826 size_t first_part_size = first_part_size_arg;
2827 size_t output1_length = output1_length_arg;
2828 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002829 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830 size_t output_buffer_size = 0;
2831 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002832 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002833 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002834 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002835
Gilles Peskine8817f612018-12-18 00:18:46 +01002836 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002838 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2839 psa_set_key_algorithm( &attributes, alg );
2840 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002841
Ronald Cron5425a212020-08-04 14:58:35 +02002842 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2843 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
Ronald Cron5425a212020-08-04 14:58:35 +02002845 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002846
Steven Cooreman177deba2020-09-07 17:14:14 +02002847 if( iv->len > 0 )
2848 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002849 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002850 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002851
gabor-mezei-armceface22021-01-21 12:26:17 +01002852 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2853 TEST_ASSERT( output_buffer_size <=
2854 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002855 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002856
Gilles Peskinee0866522019-02-19 19:44:00 +01002857 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002858 PSA_ASSERT( psa_cipher_update( &operation,
2859 input->x, first_part_size,
2860 output, output_buffer_size,
2861 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002862 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002863 TEST_ASSERT( function_output_length <=
2864 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2865 TEST_ASSERT( function_output_length <=
2866 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002867 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002868
Gilles Peskine8817f612018-12-18 00:18:46 +01002869 PSA_ASSERT( psa_cipher_update( &operation,
2870 input->x + first_part_size,
2871 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002872 ( output_buffer_size == 0 ? NULL :
2873 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002874 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002875 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002876 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002877 TEST_ASSERT( function_output_length <=
2878 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2879 alg,
2880 input->len - first_part_size ) );
2881 TEST_ASSERT( function_output_length <=
2882 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002883 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002884
Gilles Peskine8817f612018-12-18 00:18:46 +01002885 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002886 ( output_buffer_size == 0 ? NULL :
2887 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002888 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002889 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002890 TEST_ASSERT( function_output_length <=
2891 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2892 TEST_ASSERT( function_output_length <=
2893 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002894 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002895 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002896
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002897 ASSERT_COMPARE( expected_output->x, expected_output->len,
2898 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002899
2900exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002901 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002902 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002903 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002904 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002905}
2906/* END_CASE */
2907
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908/* BEGIN_CASE */
2909void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002910 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002911 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002912 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913{
Ronald Cron5425a212020-08-04 14:58:35 +02002914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915 psa_status_t status;
2916 psa_key_type_t key_type = key_type_arg;
2917 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002918 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002919 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920 size_t output_buffer_size = 0;
2921 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002922 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002923 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002925
Gilles Peskine8817f612018-12-18 00:18:46 +01002926 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002928 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2929 psa_set_key_algorithm( &attributes, alg );
2930 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002931
Ronald Cron5425a212020-08-04 14:58:35 +02002932 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2933 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934
Ronald Cron5425a212020-08-04 14:58:35 +02002935 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002936
Steven Cooreman177deba2020-09-07 17:14:14 +02002937 if( iv->len > 0 )
2938 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002939 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002940 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002941
gabor-mezei-armceface22021-01-21 12:26:17 +01002942 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2943 TEST_ASSERT( output_buffer_size <=
2944 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002945 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002946
Gilles Peskine8817f612018-12-18 00:18:46 +01002947 PSA_ASSERT( psa_cipher_update( &operation,
2948 input->x, input->len,
2949 output, output_buffer_size,
2950 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002951 TEST_ASSERT( function_output_length <=
2952 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2953 TEST_ASSERT( function_output_length <=
2954 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002955 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002956
Gilles Peskine50e586b2018-06-08 14:28:46 +02002957 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002958 ( output_buffer_size == 0 ? NULL :
2959 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002960 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002962 TEST_ASSERT( function_output_length <=
2963 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2964 TEST_ASSERT( function_output_length <=
2965 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002966 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002967 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002968
2969 if( expected_status == PSA_SUCCESS )
2970 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002971 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002972 ASSERT_COMPARE( expected_output->x, expected_output->len,
2973 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974 }
2975
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002977 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002978 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002979 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002980 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002981}
2982/* END_CASE */
2983
Gilles Peskine50e586b2018-06-08 14:28:46 +02002984/* BEGIN_CASE */
2985void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002986 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002987 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002988{
Ronald Cron5425a212020-08-04 14:58:35 +02002989 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002990 psa_key_type_t key_type = key_type_arg;
2991 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002992 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002993 size_t iv_size = 16;
2994 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002995 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002996 size_t output1_size = 0;
2997 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002998 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002999 size_t output2_size = 0;
3000 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003001 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003002 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3003 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003004 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003005
Gilles Peskine8817f612018-12-18 00:18:46 +01003006 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003007
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003008 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3009 psa_set_key_algorithm( &attributes, alg );
3010 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003011
Ronald Cron5425a212020-08-04 14:58:35 +02003012 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3013 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003014
Ronald Cron5425a212020-08-04 14:58:35 +02003015 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3016 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003017
Steven Cooreman177deba2020-09-07 17:14:14 +02003018 if( alg != PSA_ALG_ECB_NO_PADDING )
3019 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003020 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3021 iv, iv_size,
3022 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003023 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003024 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3025 TEST_ASSERT( output1_size <=
3026 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003027 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003028
Gilles Peskine8817f612018-12-18 00:18:46 +01003029 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3030 output1, output1_size,
3031 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003032 TEST_ASSERT( output1_length <=
3033 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3034 TEST_ASSERT( output1_length <=
3035 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3036
Gilles Peskine8817f612018-12-18 00:18:46 +01003037 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003038 output1 + output1_length,
3039 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003040 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003041 TEST_ASSERT( function_output_length <=
3042 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3043 TEST_ASSERT( function_output_length <=
3044 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003045
Gilles Peskine048b7f02018-06-08 14:20:49 +02003046 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003047
Gilles Peskine8817f612018-12-18 00:18:46 +01003048 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003049
3050 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003051 TEST_ASSERT( output2_size <=
3052 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3053 TEST_ASSERT( output2_size <=
3054 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003055 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003056
Steven Cooreman177deba2020-09-07 17:14:14 +02003057 if( iv_length > 0 )
3058 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003059 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3060 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003061 }
3062
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3064 output2, output2_size,
3065 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003066 TEST_ASSERT( output2_length <=
3067 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3068 TEST_ASSERT( output2_length <=
3069 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3070
Gilles Peskine048b7f02018-06-08 14:20:49 +02003071 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003072 PSA_ASSERT( psa_cipher_finish( &operation2,
3073 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003074 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003075 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003076 TEST_ASSERT( function_output_length <=
3077 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3078 TEST_ASSERT( function_output_length <=
3079 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003080
Gilles Peskine048b7f02018-06-08 14:20:49 +02003081 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003082
Gilles Peskine8817f612018-12-18 00:18:46 +01003083 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003084
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003085 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003086
3087exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003088 psa_cipher_abort( &operation1 );
3089 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003090 mbedtls_free( output1 );
3091 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003092 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003093 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003094}
3095/* END_CASE */
3096
3097/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003098void cipher_verify_output_multipart( int alg_arg,
3099 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003100 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003101 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003102 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003103{
Ronald Cron5425a212020-08-04 14:58:35 +02003104 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003105 psa_key_type_t key_type = key_type_arg;
3106 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003107 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003108 unsigned char iv[16] = {0};
3109 size_t iv_size = 16;
3110 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003111 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003112 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003113 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003114 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003115 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003116 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003117 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003118 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3119 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003121
Gilles Peskine8817f612018-12-18 00:18:46 +01003122 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003123
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003124 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3125 psa_set_key_algorithm( &attributes, alg );
3126 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003127
Ronald Cron5425a212020-08-04 14:58:35 +02003128 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3129 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003130
Ronald Cron5425a212020-08-04 14:58:35 +02003131 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3132 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003133
Steven Cooreman177deba2020-09-07 17:14:14 +02003134 if( alg != PSA_ALG_ECB_NO_PADDING )
3135 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003136 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3137 iv, iv_size,
3138 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003139 }
3140
gabor-mezei-armceface22021-01-21 12:26:17 +01003141 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3142 TEST_ASSERT( output1_buffer_size <=
3143 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003144 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003145
Gilles Peskinee0866522019-02-19 19:44:00 +01003146 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003147
Gilles Peskine8817f612018-12-18 00:18:46 +01003148 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3149 output1, output1_buffer_size,
3150 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003151 TEST_ASSERT( function_output_length <=
3152 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3153 TEST_ASSERT( function_output_length <=
3154 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003155 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003156
Gilles Peskine8817f612018-12-18 00:18:46 +01003157 PSA_ASSERT( psa_cipher_update( &operation1,
3158 input->x + first_part_size,
3159 input->len - first_part_size,
3160 output1, output1_buffer_size,
3161 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003162 TEST_ASSERT( function_output_length <=
3163 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3164 alg,
3165 input->len - first_part_size ) );
3166 TEST_ASSERT( function_output_length <=
3167 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003168 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003169
Gilles Peskine8817f612018-12-18 00:18:46 +01003170 PSA_ASSERT( psa_cipher_finish( &operation1,
3171 output1 + output1_length,
3172 output1_buffer_size - output1_length,
3173 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003174 TEST_ASSERT( function_output_length <=
3175 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3176 TEST_ASSERT( function_output_length <=
3177 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003178 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003179
Gilles Peskine8817f612018-12-18 00:18:46 +01003180 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003181
Gilles Peskine048b7f02018-06-08 14:20:49 +02003182 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003183 TEST_ASSERT( output2_buffer_size <=
3184 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3185 TEST_ASSERT( output2_buffer_size <=
3186 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003187 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003188
Steven Cooreman177deba2020-09-07 17:14:14 +02003189 if( iv_length > 0 )
3190 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003191 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3192 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003193 }
Moran Pekerded84402018-06-06 16:36:50 +03003194
Gilles Peskine8817f612018-12-18 00:18:46 +01003195 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3196 output2, output2_buffer_size,
3197 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003198 TEST_ASSERT( function_output_length <=
3199 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3200 TEST_ASSERT( function_output_length <=
3201 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003202 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003203
Gilles Peskine8817f612018-12-18 00:18:46 +01003204 PSA_ASSERT( psa_cipher_update( &operation2,
3205 output1 + first_part_size,
3206 output1_length - first_part_size,
3207 output2, output2_buffer_size,
3208 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003209 TEST_ASSERT( function_output_length <=
3210 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3211 alg,
3212 output1_length - first_part_size ) );
3213 TEST_ASSERT( function_output_length <=
3214 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003215 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003216
Gilles Peskine8817f612018-12-18 00:18:46 +01003217 PSA_ASSERT( psa_cipher_finish( &operation2,
3218 output2 + output2_length,
3219 output2_buffer_size - output2_length,
3220 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003221 TEST_ASSERT( function_output_length <=
3222 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3223 TEST_ASSERT( function_output_length <=
3224 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003225 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003226
Gilles Peskine8817f612018-12-18 00:18:46 +01003227 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003228
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003229 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003230
3231exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003232 psa_cipher_abort( &operation1 );
3233 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003234 mbedtls_free( output1 );
3235 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003236 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003237 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003238}
3239/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003240
Gilles Peskine20035e32018-02-03 22:44:14 +01003241/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003242void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003243 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003244 data_t *nonce,
3245 data_t *additional_data,
3246 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003247 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003248{
Ronald Cron5425a212020-08-04 14:58:35 +02003249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003250 psa_key_type_t key_type = key_type_arg;
3251 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003252 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003253 unsigned char *output_data = NULL;
3254 size_t output_size = 0;
3255 size_t output_length = 0;
3256 unsigned char *output_data2 = NULL;
3257 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003258 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003259 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003261
Gilles Peskine8817f612018-12-18 00:18:46 +01003262 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003263
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003264 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3265 psa_set_key_algorithm( &attributes, alg );
3266 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003267
Gilles Peskine049c7532019-05-15 20:22:09 +02003268 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003269 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003270 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3271 key_bits = psa_get_key_bits( &attributes );
3272
3273 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3274 alg );
3275 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3276 * should be exact. */
3277 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3278 expected_result != PSA_ERROR_NOT_SUPPORTED )
3279 {
3280 TEST_EQUAL( output_size,
3281 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3282 TEST_ASSERT( output_size <=
3283 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3284 }
3285 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003286
Steven Cooremanf49478b2021-02-15 15:19:25 +01003287 status = psa_aead_encrypt( key, alg,
3288 nonce->x, nonce->len,
3289 additional_data->x,
3290 additional_data->len,
3291 input_data->x, input_data->len,
3292 output_data, output_size,
3293 &output_length );
3294
3295 /* If the operation is not supported, just skip and not fail in case the
3296 * encryption involves a common limitation of cryptography hardwares and
3297 * an alternative implementation. */
3298 if( status == PSA_ERROR_NOT_SUPPORTED )
3299 {
3300 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3301 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3302 }
3303
3304 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003305
3306 if( PSA_SUCCESS == expected_result )
3307 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003308 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003309
Gilles Peskine003a4a92019-05-14 16:09:40 +02003310 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3311 * should be exact. */
3312 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003313 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003314
gabor-mezei-armceface22021-01-21 12:26:17 +01003315 TEST_ASSERT( input_data->len <=
3316 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3317
Ronald Cron5425a212020-08-04 14:58:35 +02003318 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003319 nonce->x, nonce->len,
3320 additional_data->x,
3321 additional_data->len,
3322 output_data, output_length,
3323 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003324 &output_length2 ),
3325 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003326
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003327 ASSERT_COMPARE( input_data->x, input_data->len,
3328 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003330
Gilles Peskinea1cac842018-06-11 19:33:02 +02003331exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003332 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003333 mbedtls_free( output_data );
3334 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003335 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336}
3337/* END_CASE */
3338
3339/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003340void aead_encrypt( int key_type_arg, data_t *key_data,
3341 int alg_arg,
3342 data_t *nonce,
3343 data_t *additional_data,
3344 data_t *input_data,
3345 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003346{
Ronald Cron5425a212020-08-04 14:58:35 +02003347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003348 psa_key_type_t key_type = key_type_arg;
3349 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003350 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351 unsigned char *output_data = NULL;
3352 size_t output_size = 0;
3353 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003354 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003355 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003356
Gilles Peskine8817f612018-12-18 00:18:46 +01003357 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003358
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003359 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3360 psa_set_key_algorithm( &attributes, alg );
3361 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003362
Gilles Peskine049c7532019-05-15 20:22:09 +02003363 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003364 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003365 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3366 key_bits = psa_get_key_bits( &attributes );
3367
3368 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3369 alg );
3370 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3371 * should be exact. */
3372 TEST_EQUAL( output_size,
3373 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3374 TEST_ASSERT( output_size <=
3375 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3376 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377
Steven Cooremand588ea12021-01-11 19:36:04 +01003378 status = psa_aead_encrypt( key, alg,
3379 nonce->x, nonce->len,
3380 additional_data->x, additional_data->len,
3381 input_data->x, input_data->len,
3382 output_data, output_size,
3383 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003384
Ronald Cron28a45ed2021-02-09 20:35:42 +01003385 /* If the operation is not supported, just skip and not fail in case the
3386 * encryption involves a common limitation of cryptography hardwares and
3387 * an alternative implementation. */
3388 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003389 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003390 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3391 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003392 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003393
3394 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003395 ASSERT_COMPARE( expected_result->x, expected_result->len,
3396 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003397
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003399 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003401 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402}
3403/* END_CASE */
3404
3405/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003406void aead_decrypt( int key_type_arg, data_t *key_data,
3407 int alg_arg,
3408 data_t *nonce,
3409 data_t *additional_data,
3410 data_t *input_data,
3411 data_t *expected_data,
3412 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003413{
Ronald Cron5425a212020-08-04 14:58:35 +02003414 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003415 psa_key_type_t key_type = key_type_arg;
3416 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003417 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418 unsigned char *output_data = NULL;
3419 size_t output_size = 0;
3420 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003422 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003423 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003424
Gilles Peskine8817f612018-12-18 00:18:46 +01003425 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003426
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003427 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3428 psa_set_key_algorithm( &attributes, alg );
3429 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003430
Gilles Peskine049c7532019-05-15 20:22:09 +02003431 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003432 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003433 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3434 key_bits = psa_get_key_bits( &attributes );
3435
3436 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3437 alg );
3438 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3439 expected_result != PSA_ERROR_NOT_SUPPORTED )
3440 {
3441 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3442 * should be exact. */
3443 TEST_EQUAL( output_size,
3444 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3445 TEST_ASSERT( output_size <=
3446 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3447 }
3448 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003449
Steven Cooremand588ea12021-01-11 19:36:04 +01003450 status = psa_aead_decrypt( key, alg,
3451 nonce->x, nonce->len,
3452 additional_data->x,
3453 additional_data->len,
3454 input_data->x, input_data->len,
3455 output_data, output_size,
3456 &output_length );
3457
Ronald Cron28a45ed2021-02-09 20:35:42 +01003458 /* If the operation is not supported, just skip and not fail in case the
3459 * decryption involves a common limitation of cryptography hardwares and
3460 * an alternative implementation. */
3461 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003462 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003463 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3464 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003465 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003466
3467 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003468
Gilles Peskine2d277862018-06-18 15:41:12 +02003469 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003470 ASSERT_COMPARE( expected_data->x, expected_data->len,
3471 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003472
Gilles Peskinea1cac842018-06-11 19:33:02 +02003473exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003474 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003476 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003477}
3478/* END_CASE */
3479
3480/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003481void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3482 int alg_arg,
3483 data_t *nonce,
3484 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003485 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003486 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003487 int do_test_data_chunked,
3488 int do_set_lengths,
3489 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003490{
Paul Elliottd3f82412021-06-16 16:52:21 +01003491 size_t ad_part_len = 0;
3492 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003493 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003494
Paul Elliotte64deda2021-09-09 14:07:23 +01003495 /* Ensure that either one part of the test or the other is done, i.e this
3496 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003497 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3498
3499 /* Temporary whilst we have algorithms that cannot support chunking */
3500 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003501 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003502 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3503 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003504 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003505 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003506
Paul Elliott33746aa2021-09-15 16:40:40 +01003507 if( do_set_lengths )
3508 {
3509 if( ad_part_len & 0x01 )
3510 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3511 else
3512 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3513 }
3514
Paul Elliott329d5382021-07-22 17:10:45 +01003515 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003516 if( !aead_multipart_internal_func( key_type_arg, key_data,
3517 alg_arg, nonce,
3518 additional_data,
3519 ad_part_len,
3520 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003521 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003522 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003523 1, 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003524 break;
3525
3526 /* length(0) part, length(ad_part_len) part, length(0) part... */
3527 mbedtls_test_set_step( 1000 + ad_part_len );
3528
3529 if( !aead_multipart_internal_func( key_type_arg, key_data,
3530 alg_arg, nonce,
3531 additional_data,
3532 ad_part_len,
3533 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003534 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003535 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003536 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003537 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003538 }
3539 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003540
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003541 /* Temporary whilst we have algorithms that cannot support chunking */
3542 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003543 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003544 for( data_part_len = 1; data_part_len <= input_data->len;
3545 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003546 {
Paul Elliott329d5382021-07-22 17:10:45 +01003547 /* Split data into length(data_part_len) parts. */
3548 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003549
Paul Elliott33746aa2021-09-15 16:40:40 +01003550 if( do_set_lengths )
3551 {
3552 if( data_part_len & 0x01 )
3553 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3554 else
3555 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3556 }
3557
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003558 if( !aead_multipart_internal_func( key_type_arg, key_data,
3559 alg_arg, nonce,
3560 additional_data, -1,
3561 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003562 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003563 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003564 1, 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003565 break;
3566
3567 /* length(0) part, length(data_part_len) part, length(0) part... */
3568 mbedtls_test_set_step( 3000 + data_part_len );
3569
3570 if( !aead_multipart_internal_func( key_type_arg, key_data,
3571 alg_arg, nonce,
3572 additional_data, -1,
3573 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003574 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003575 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003576 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003577 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003578 }
3579 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003580
Paul Elliott0023e0a2021-04-27 10:06:22 +01003581
Paul Elliott8fc45162021-06-23 16:06:01 +01003582 /* Goto is required to silence warnings about unused labels, as we
3583 * don't actually do any test assertions in this function. */
3584 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003585}
3586/* END_CASE */
3587
3588/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003589void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3590 int alg_arg,
3591 data_t *nonce,
3592 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003593 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003594 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003595 int do_test_data_chunked,
3596 int do_set_lengths,
3597 data_t *expected_output,
3598 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003599{
Paul Elliottd3f82412021-06-16 16:52:21 +01003600 size_t ad_part_len = 0;
3601 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003602 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003603
Paul Elliotte64deda2021-09-09 14:07:23 +01003604 /* Ensure that either one part of the test or the other is done, i.e this
3605 * test does something. */
3606 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3607
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003608 /* Temporary whilst we have algorithms that cannot support chunking */
3609 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003610 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003611 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3612 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003613 {
Paul Elliott329d5382021-07-22 17:10:45 +01003614 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003615 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003616
Paul Elliott33746aa2021-09-15 16:40:40 +01003617 if( do_set_lengths )
3618 {
3619 if( ad_part_len & 0x01 )
3620 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3621 else
3622 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3623 }
3624
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003625 if( !aead_multipart_internal_func( key_type_arg, key_data,
3626 alg_arg, nonce,
3627 additional_data,
3628 ad_part_len,
3629 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003630 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003631 expected_output,
3632 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003633 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003634 break;
3635
3636 /* length(0) part, length(ad_part_len) part, length(0) part... */
3637 mbedtls_test_set_step( 1000 + ad_part_len );
3638
3639 if( !aead_multipart_internal_func( key_type_arg, key_data,
3640 alg_arg, nonce,
3641 additional_data,
3642 ad_part_len,
3643 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003644 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003645 expected_output,
3646 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003647 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003648 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003649 }
3650 }
3651
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003652 /* Temporary whilst we have algorithms that cannot support chunking */
3653 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003654 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003655 for( data_part_len = 1; data_part_len <= input_data->len;
3656 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003657 {
Paul Elliott329d5382021-07-22 17:10:45 +01003658 /* Split data into length(data_part_len) parts. */
3659 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003660
Paul Elliott33746aa2021-09-15 16:40:40 +01003661 if( do_set_lengths )
3662 {
3663 if( data_part_len & 0x01 )
3664 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3665 else
3666 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3667 }
3668
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003669 if( !aead_multipart_internal_func( key_type_arg, key_data,
3670 alg_arg, nonce,
3671 additional_data, -1,
3672 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003673 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003674 expected_output,
3675 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003676 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003677 break;
3678
3679 /* length(0) part, length(data_part_len) part, length(0) part... */
3680 mbedtls_test_set_step( 3000 + data_part_len );
3681
3682 if( !aead_multipart_internal_func( key_type_arg, key_data,
3683 alg_arg, nonce,
3684 additional_data, -1,
3685 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003686 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003687 expected_output,
3688 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003689 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003690 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003691 }
3692 }
3693
Paul Elliott8fc45162021-06-23 16:06:01 +01003694 /* Goto is required to silence warnings about unused labels, as we
3695 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003696 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003697}
3698/* END_CASE */
3699
3700/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003701void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3702 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003703 int nonce_length,
3704 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003705 data_t *additional_data,
3706 data_t *input_data,
3707 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003708{
3709
3710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3711 psa_key_type_t key_type = key_type_arg;
3712 psa_algorithm_t alg = alg_arg;
3713 psa_aead_operation_t operation;
3714 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3715 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3716 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003717 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003718 size_t actual_nonce_length = 0;
3719 size_t expected_nonce_length = expected_nonce_length_arg;
3720 unsigned char *output = NULL;
3721 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003722 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003723 size_t ciphertext_size = 0;
3724 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003725 size_t tag_length = 0;
3726 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003727
3728 PSA_ASSERT( psa_crypto_init( ) );
3729
3730 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3731 psa_set_key_algorithm( & attributes, alg );
3732 psa_set_key_type( & attributes, key_type );
3733
3734 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3735 &key ) );
3736
3737 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3738
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003739 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3740
Paul Elliottf1277632021-08-24 18:11:37 +01003741 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003742
Paul Elliottf1277632021-08-24 18:11:37 +01003743 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003744
Paul Elliottf1277632021-08-24 18:11:37 +01003745 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003746
Paul Elliottf1277632021-08-24 18:11:37 +01003747 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003748
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003749 operation = psa_aead_operation_init( );
3750
3751 status = psa_aead_encrypt_setup( &operation, key, alg );
3752
3753 /* If the operation is not supported, just skip and not fail in case the
3754 * encryption involves a common limitation of cryptography hardwares and
3755 * an alternative implementation. */
3756 if( status == PSA_ERROR_NOT_SUPPORTED )
3757 {
3758 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003759 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003760 }
3761
3762 PSA_ASSERT( status );
3763
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003764 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003765 nonce_length,
3766 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003767
Paul Elliott693bf312021-07-23 17:40:41 +01003768 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003769
Paul Elliottf1277632021-08-24 18:11:37 +01003770 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003771
Paul Elliottf1277632021-08-24 18:11:37 +01003772 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003773
Paul Elliott693bf312021-07-23 17:40:41 +01003774 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003775 {
3776
3777 /* Ensure we can still complete operation. */
3778
3779 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3780 additional_data->len ) );
3781
3782 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003783 output, output_size,
3784 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003785
Paul Elliottf1277632021-08-24 18:11:37 +01003786 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3787 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003788 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3789 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003790
3791exit:
3792 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003793 mbedtls_free( output );
3794 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003795 psa_aead_abort( &operation );
3796 PSA_DONE( );
3797}
3798/* END_CASE */
3799
3800/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003801void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3802 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003803 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003804 data_t *additional_data,
3805 data_t *input_data,
3806 int expected_status_arg )
3807{
3808
3809 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3810 psa_key_type_t key_type = key_type_arg;
3811 psa_algorithm_t alg = alg_arg;
3812 psa_aead_operation_t operation;
3813 uint8_t *nonce_buffer = NULL;
3814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3815 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3816 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003817 unsigned char *output = NULL;
3818 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003819 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003820 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003821 size_t ciphertext_size = 0;
3822 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003823 size_t tag_length = 0;
3824 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003825 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003826
3827 PSA_ASSERT( psa_crypto_init( ) );
3828
3829 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3830 psa_set_key_algorithm( &attributes, alg );
3831 psa_set_key_type( &attributes, key_type );
3832
3833 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3834 &key ) );
3835
3836 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3837
3838 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3839
Paul Elliott6f0e7202021-08-25 12:57:18 +01003840 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003841
Paul Elliott6f0e7202021-08-25 12:57:18 +01003842 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003843
Paul Elliott6f0e7202021-08-25 12:57:18 +01003844 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003845
Paul Elliott6f0e7202021-08-25 12:57:18 +01003846 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003847
3848 operation = psa_aead_operation_init( );
3849
3850 status = psa_aead_encrypt_setup( &operation, key, alg );
3851
3852 /* If the operation is not supported, just skip and not fail in case the
3853 * encryption involves a common limitation of cryptography hardwares and
3854 * an alternative implementation. */
3855 if( status == PSA_ERROR_NOT_SUPPORTED )
3856 {
3857 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003858 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003859 }
3860
3861 PSA_ASSERT( status );
3862
Paul Elliott4023ffd2021-09-10 16:21:22 +01003863 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3864 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003865 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003866 /* Arbitrary size buffer, to test zero length valid buffer. */
3867 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003868 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003869 }
3870 else
3871 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003872 /* If length is zero, then this will return NULL. */
3873 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003874 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003875
Paul Elliott4023ffd2021-09-10 16:21:22 +01003876 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003877 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003878 for( index = 0; index < nonce_length - 1; ++index )
3879 {
3880 nonce_buffer[index] = 'a' + index;
3881 }
Paul Elliott66696b52021-08-16 18:42:41 +01003882 }
Paul Elliott863864a2021-07-23 17:28:31 +01003883 }
3884
Paul Elliott6f0e7202021-08-25 12:57:18 +01003885 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003886
Paul Elliott693bf312021-07-23 17:40:41 +01003887 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003888
3889 if( expected_status == PSA_SUCCESS )
3890 {
3891 /* Ensure we can still complete operation. */
3892
3893 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3894 additional_data->len ) );
3895
3896 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003897 output, output_size,
3898 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003899
Paul Elliott6f0e7202021-08-25 12:57:18 +01003900 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3901 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003902 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3903 }
3904
3905exit:
3906 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003907 mbedtls_free( output );
3908 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003909 mbedtls_free( nonce_buffer );
3910 psa_aead_abort( &operation );
3911 PSA_DONE( );
3912}
3913/* END_CASE */
3914
3915/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003916void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3917 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003918 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003919 data_t *nonce,
3920 data_t *additional_data,
3921 data_t *input_data,
3922 int expected_status_arg )
3923{
3924
3925 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3926 psa_key_type_t key_type = key_type_arg;
3927 psa_algorithm_t alg = alg_arg;
3928 psa_aead_operation_t operation;
3929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3930 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3931 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003932 unsigned char *output = NULL;
3933 unsigned char *ciphertext = NULL;
3934 size_t output_size = output_size_arg;
3935 size_t ciphertext_size = 0;
3936 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003937 size_t tag_length = 0;
3938 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3939
3940 PSA_ASSERT( psa_crypto_init( ) );
3941
3942 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3943 psa_set_key_algorithm( &attributes, alg );
3944 psa_set_key_type( &attributes, key_type );
3945
3946 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3947 &key ) );
3948
3949 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3950
Paul Elliottc6d11d02021-09-01 12:04:23 +01003951 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003952
Paul Elliottc6d11d02021-09-01 12:04:23 +01003953 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003954
Paul Elliottc6d11d02021-09-01 12:04:23 +01003955 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003956
3957 operation = psa_aead_operation_init( );
3958
3959 status = psa_aead_encrypt_setup( &operation, key, alg );
3960
3961 /* If the operation is not supported, just skip and not fail in case the
3962 * encryption involves a common limitation of cryptography hardwares and
3963 * an alternative implementation. */
3964 if( status == PSA_ERROR_NOT_SUPPORTED )
3965 {
3966 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3967 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3968 }
3969
3970 PSA_ASSERT( status );
3971
3972 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3973
3974 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3975 additional_data->len ) );
3976
3977 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003978 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003979
3980 TEST_EQUAL( status, expected_status );
3981
3982 if( expected_status == PSA_SUCCESS )
3983 {
3984 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003985 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3986 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003987 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3988 }
3989
3990exit:
3991 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003992 mbedtls_free( output );
3993 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003994 psa_aead_abort( &operation );
3995 PSA_DONE( );
3996}
3997/* END_CASE */
3998
Paul Elliott91b021e2021-07-23 18:52:31 +01003999/* BEGIN_CASE */
4000void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4001 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004002 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004003 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004004 data_t *nonce,
4005 data_t *additional_data,
4006 data_t *input_data,
4007 int expected_status_arg )
4008{
4009
4010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4011 psa_key_type_t key_type = key_type_arg;
4012 psa_algorithm_t alg = alg_arg;
4013 psa_aead_operation_t operation;
4014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4015 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4016 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004017 unsigned char *ciphertext = NULL;
4018 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004019 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004020 size_t ciphertext_size = 0;
4021 size_t ciphertext_length = 0;
4022 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004023 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004024 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004025
4026 PSA_ASSERT( psa_crypto_init( ) );
4027
4028 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4029 psa_set_key_algorithm( &attributes, alg );
4030 psa_set_key_type( &attributes, key_type );
4031
4032 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4033 &key ) );
4034
4035 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4036
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004037 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004038
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004039 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004040
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004041 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004042
Paul Elliott719c1322021-09-13 18:27:22 +01004043 ASSERT_ALLOC( tag_buffer, tag_size );
4044
Paul Elliott91b021e2021-07-23 18:52:31 +01004045 operation = psa_aead_operation_init( );
4046
4047 status = psa_aead_encrypt_setup( &operation, key, alg );
4048
4049 /* If the operation is not supported, just skip and not fail in case the
4050 * encryption involves a common limitation of cryptography hardwares and
4051 * an alternative implementation. */
4052 if( status == PSA_ERROR_NOT_SUPPORTED )
4053 {
4054 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4055 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4056 }
4057
4058 PSA_ASSERT( status );
4059
4060 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4061
4062 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4063 additional_data->len ) );
4064
4065 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004066 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004067
4068 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004069 status = psa_aead_finish( &operation, finish_ciphertext,
4070 finish_ciphertext_size,
4071 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004072 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004073
4074 TEST_EQUAL( status, expected_status );
4075
4076exit:
4077 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004078 mbedtls_free( ciphertext );
4079 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004080 psa_aead_abort( &operation );
4081 PSA_DONE( );
4082}
4083/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004084
4085/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004086void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4087 int alg_arg,
4088 data_t *nonce,
4089 data_t *additional_data,
4090 data_t *input_data )
4091{
4092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4093 psa_key_type_t key_type = key_type_arg;
4094 psa_algorithm_t alg = alg_arg;
4095 psa_aead_operation_t operation;
4096 unsigned char *output_data = NULL;
4097 unsigned char *final_data = NULL;
4098 size_t output_size = 0;
4099 size_t finish_output_size = 0;
4100 size_t output_length = 0;
4101 size_t key_bits = 0;
4102 size_t tag_length = 0;
4103 size_t tag_size = 0;
4104 size_t nonce_length = 0;
4105 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4106 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4107 size_t output_part_length = 0;
4108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4109
4110 PSA_ASSERT( psa_crypto_init( ) );
4111
4112 psa_set_key_usage_flags( & attributes,
4113 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4114 psa_set_key_algorithm( & attributes, alg );
4115 psa_set_key_type( & attributes, key_type );
4116
4117 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4118 &key ) );
4119
4120 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4121 key_bits = psa_get_key_bits( &attributes );
4122
4123 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4124
4125 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4126
4127 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4128
4129 ASSERT_ALLOC( output_data, output_size );
4130
4131 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4132
4133 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4134
4135 ASSERT_ALLOC( final_data, finish_output_size );
4136
4137 /* Test all operations error without calling setup first. */
4138
4139 operation = psa_aead_operation_init( );
4140
4141 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4142 PSA_ERROR_BAD_STATE );
4143
4144 psa_aead_abort( &operation );
4145
4146 operation = psa_aead_operation_init( );
4147
4148 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4149 PSA_AEAD_NONCE_MAX_SIZE,
4150 &nonce_length ),
4151 PSA_ERROR_BAD_STATE );
4152
4153 psa_aead_abort( &operation );
4154
Paul Elliott481be342021-07-16 17:38:47 +01004155 /* ------------------------------------------------------- */
4156
Paul Elliottc23a9a02021-06-21 18:32:46 +01004157 operation = psa_aead_operation_init( );
4158
4159 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4160 input_data->len ),
4161 PSA_ERROR_BAD_STATE );
4162
4163 psa_aead_abort( &operation );
4164
Paul Elliott481be342021-07-16 17:38:47 +01004165 /* ------------------------------------------------------- */
4166
Paul Elliottc23a9a02021-06-21 18:32:46 +01004167 operation = psa_aead_operation_init( );
4168
4169 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4170 additional_data->len ),
4171 PSA_ERROR_BAD_STATE );
4172
4173 psa_aead_abort( &operation );
4174
Paul Elliott481be342021-07-16 17:38:47 +01004175 /* ------------------------------------------------------- */
4176
Paul Elliottc23a9a02021-06-21 18:32:46 +01004177 operation = psa_aead_operation_init( );
4178
4179 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4180 input_data->len, output_data,
4181 output_size, &output_length ),
4182 PSA_ERROR_BAD_STATE );
4183
4184 psa_aead_abort( &operation );
4185
Paul Elliott481be342021-07-16 17:38:47 +01004186 /* ------------------------------------------------------- */
4187
Paul Elliottc23a9a02021-06-21 18:32:46 +01004188 operation = psa_aead_operation_init( );
4189
4190 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4191 finish_output_size,
4192 &output_part_length,
4193 tag_buffer, tag_length,
4194 &tag_size ),
4195 PSA_ERROR_BAD_STATE );
4196
4197 psa_aead_abort( &operation );
4198
Paul Elliott481be342021-07-16 17:38:47 +01004199 /* ------------------------------------------------------- */
4200
Paul Elliottc23a9a02021-06-21 18:32:46 +01004201 operation = psa_aead_operation_init( );
4202
4203 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4204 finish_output_size,
4205 &output_part_length,
4206 tag_buffer,
4207 tag_length ),
4208 PSA_ERROR_BAD_STATE );
4209
4210 psa_aead_abort( &operation );
4211
4212 /* Test for double setups. */
4213
4214 operation = psa_aead_operation_init( );
4215
4216 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4217
4218 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4219 PSA_ERROR_BAD_STATE );
4220
4221 psa_aead_abort( &operation );
4222
Paul Elliott481be342021-07-16 17:38:47 +01004223 /* ------------------------------------------------------- */
4224
Paul Elliottc23a9a02021-06-21 18:32:46 +01004225 operation = psa_aead_operation_init( );
4226
4227 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4228
4229 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4230 PSA_ERROR_BAD_STATE );
4231
4232 psa_aead_abort( &operation );
4233
Paul Elliott374a2be2021-07-16 17:53:40 +01004234 /* ------------------------------------------------------- */
4235
4236 operation = psa_aead_operation_init( );
4237
4238 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4239
4240 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4241 PSA_ERROR_BAD_STATE );
4242
4243 psa_aead_abort( &operation );
4244
4245 /* ------------------------------------------------------- */
4246
4247 operation = psa_aead_operation_init( );
4248
4249 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4250
4251 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4252 PSA_ERROR_BAD_STATE );
4253
4254 psa_aead_abort( &operation );
4255
Paul Elliottc23a9a02021-06-21 18:32:46 +01004256 /* Test for not setting a nonce. */
4257
4258 operation = psa_aead_operation_init( );
4259
4260 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4261
4262 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4263 additional_data->len ),
4264 PSA_ERROR_BAD_STATE );
4265
4266 psa_aead_abort( &operation );
4267
Paul Elliott7f628422021-09-01 12:08:29 +01004268 /* ------------------------------------------------------- */
4269
4270 operation = psa_aead_operation_init( );
4271
4272 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4273
4274 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4275 input_data->len, output_data,
4276 output_size, &output_length ),
4277 PSA_ERROR_BAD_STATE );
4278
4279 psa_aead_abort( &operation );
4280
Paul Elliottc23a9a02021-06-21 18:32:46 +01004281 /* Test for double setting nonce. */
4282
4283 operation = psa_aead_operation_init( );
4284
4285 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4286
4287 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4288
4289 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4290 PSA_ERROR_BAD_STATE );
4291
4292 psa_aead_abort( &operation );
4293
Paul Elliott374a2be2021-07-16 17:53:40 +01004294 /* Test for double generating nonce. */
4295
4296 operation = psa_aead_operation_init( );
4297
4298 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4299
4300 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4301 PSA_AEAD_NONCE_MAX_SIZE,
4302 &nonce_length ) );
4303
4304 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4305 PSA_AEAD_NONCE_MAX_SIZE,
4306 &nonce_length ),
4307 PSA_ERROR_BAD_STATE );
4308
4309
4310 psa_aead_abort( &operation );
4311
4312 /* Test for generate nonce then set and vice versa */
4313
4314 operation = psa_aead_operation_init( );
4315
4316 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4317
4318 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4319 PSA_AEAD_NONCE_MAX_SIZE,
4320 &nonce_length ) );
4321
4322 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4323 PSA_ERROR_BAD_STATE );
4324
4325 psa_aead_abort( &operation );
4326
4327 /* ------------------------------------------------------- */
4328
4329 operation = psa_aead_operation_init( );
4330
4331 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4332
4333 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4334
4335 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4336 PSA_AEAD_NONCE_MAX_SIZE,
4337 &nonce_length ),
4338 PSA_ERROR_BAD_STATE );
4339
4340 psa_aead_abort( &operation );
4341
Paul Elliott7220cae2021-06-22 17:25:57 +01004342 /* Test for generating nonce in decrypt setup. */
4343
4344 operation = psa_aead_operation_init( );
4345
4346 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4347
4348 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4349 PSA_AEAD_NONCE_MAX_SIZE,
4350 &nonce_length ),
4351 PSA_ERROR_BAD_STATE );
4352
4353 psa_aead_abort( &operation );
4354
Paul Elliottc23a9a02021-06-21 18:32:46 +01004355 /* Test for setting lengths twice. */
4356
4357 operation = psa_aead_operation_init( );
4358
4359 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4360
4361 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4362
4363 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4364 input_data->len ) );
4365
4366 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4367 input_data->len ),
4368 PSA_ERROR_BAD_STATE );
4369
4370 psa_aead_abort( &operation );
4371
4372 /* Test for setting lengths after already starting data. */
4373
4374 operation = psa_aead_operation_init( );
4375
4376 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4377
4378 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4379
4380 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4381 input_data->len, output_data,
4382 output_size, &output_length ) );
4383
4384 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4385 input_data->len ),
4386 PSA_ERROR_BAD_STATE );
4387
4388 psa_aead_abort( &operation );
4389
Paul Elliott243080c2021-07-21 19:01:17 +01004390 /* Test for not sending any additional data or data after setting non zero
4391 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004392
4393 operation = psa_aead_operation_init( );
4394
4395 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4396
4397 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4398
4399 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4400 input_data->len ) );
4401
4402 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4403 finish_output_size,
4404 &output_part_length,
4405 tag_buffer, tag_length,
4406 &tag_size ),
4407 PSA_ERROR_INVALID_ARGUMENT );
4408
4409 psa_aead_abort( &operation );
4410
Paul Elliott243080c2021-07-21 19:01:17 +01004411 /* Test for not sending any additional data or data after setting non-zero
4412 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004413
4414 operation = psa_aead_operation_init( );
4415
4416 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4417
4418 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4419
4420 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4421 input_data->len ) );
4422
4423 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4424 finish_output_size,
4425 &output_part_length,
4426 tag_buffer,
4427 tag_length ),
4428 PSA_ERROR_INVALID_ARGUMENT );
4429
4430 psa_aead_abort( &operation );
4431
Paul Elliott243080c2021-07-21 19:01:17 +01004432 /* Test for not sending any additional data after setting a non-zero length
4433 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004434
4435 operation = psa_aead_operation_init( );
4436
4437 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4438
4439 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4440
4441 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4442 input_data->len ) );
4443
4444 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4445 input_data->len, output_data,
4446 output_size, &output_length ),
4447 PSA_ERROR_INVALID_ARGUMENT );
4448
4449 psa_aead_abort( &operation );
4450
Paul Elliottb0450fe2021-09-01 15:06:26 +01004451 /* Test for sending too much additional data after setting lengths. */
4452
4453 operation = psa_aead_operation_init( );
4454
4455 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4456
4457 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4458
4459 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4460
4461
4462 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4463 additional_data->len ),
4464 PSA_ERROR_INVALID_ARGUMENT );
4465
4466 psa_aead_abort( &operation );
4467
4468 /* Test for sending too much data after setting lengths. */
4469
4470 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, 0, 0 ) );
4477
4478 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4479 input_data->len, output_data,
4480 output_size, &output_length ),
4481 PSA_ERROR_INVALID_ARGUMENT );
4482
4483 psa_aead_abort( &operation );
4484
Paul Elliottc23a9a02021-06-21 18:32:46 +01004485 /* Test sending additional data after data. */
4486
4487 operation = psa_aead_operation_init( );
4488
4489 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4490
4491 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4492
4493 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4494 input_data->len, output_data,
4495 output_size, &output_length ) );
4496
4497 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4498 additional_data->len ),
4499 PSA_ERROR_BAD_STATE );
4500
4501 psa_aead_abort( &operation );
4502
Paul Elliott534d0b42021-06-22 19:15:20 +01004503 /* Test calling finish on decryption. */
4504
4505 operation = psa_aead_operation_init( );
4506
4507 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4508
4509 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4510
4511 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4512 finish_output_size,
4513 &output_part_length,
4514 tag_buffer, tag_length,
4515 &tag_size ),
4516 PSA_ERROR_BAD_STATE );
4517
4518 psa_aead_abort( &operation );
4519
4520 /* Test calling verify on encryption. */
4521
4522 operation = psa_aead_operation_init( );
4523
4524 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4525
4526 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4527
4528 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4529 finish_output_size,
4530 &output_part_length,
4531 tag_buffer,
4532 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004533 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004534
4535 psa_aead_abort( &operation );
4536
4537
Paul Elliottc23a9a02021-06-21 18:32:46 +01004538exit:
4539 psa_destroy_key( key );
4540 psa_aead_abort( &operation );
4541 mbedtls_free( output_data );
4542 mbedtls_free( final_data );
4543 PSA_DONE( );
4544}
4545/* END_CASE */
4546
4547/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004548void signature_size( int type_arg,
4549 int bits,
4550 int alg_arg,
4551 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004552{
4553 psa_key_type_t type = type_arg;
4554 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004555 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004556
Gilles Peskinefe11b722018-12-18 00:24:04 +01004557 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004558
Gilles Peskinee59236f2018-01-27 23:32:46 +01004559exit:
4560 ;
4561}
4562/* END_CASE */
4563
4564/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004565void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4566 int alg_arg, data_t *input_data,
4567 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004568{
Ronald Cron5425a212020-08-04 14:58:35 +02004569 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004570 psa_key_type_t key_type = key_type_arg;
4571 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004572 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004573 unsigned char *signature = NULL;
4574 size_t signature_size;
4575 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004576 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004577
Gilles Peskine8817f612018-12-18 00:18:46 +01004578 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004579
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004580 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004581 psa_set_key_algorithm( &attributes, alg );
4582 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004583
Gilles Peskine049c7532019-05-15 20:22:09 +02004584 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004585 &key ) );
4586 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004587 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004588
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004589 /* Allocate a buffer which has the size advertized by the
4590 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004591 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004592 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004593 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004594 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004595 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004596
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004597 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004598 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004599 input_data->x, input_data->len,
4600 signature, signature_size,
4601 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004602 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004603 ASSERT_COMPARE( output_data->x, output_data->len,
4604 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004605
4606exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004607 /*
4608 * Key attributes may have been returned by psa_get_key_attributes()
4609 * thus reset them as required.
4610 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004611 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004612
Ronald Cron5425a212020-08-04 14:58:35 +02004613 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004614 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004615 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004616}
4617/* END_CASE */
4618
4619/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004620void sign_hash_fail( int key_type_arg, data_t *key_data,
4621 int alg_arg, data_t *input_data,
4622 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004623{
Ronald Cron5425a212020-08-04 14:58:35 +02004624 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004625 psa_key_type_t key_type = key_type_arg;
4626 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004627 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004628 psa_status_t actual_status;
4629 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004630 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004631 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004632 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004633
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004634 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004635
Gilles Peskine8817f612018-12-18 00:18:46 +01004636 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004637
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004638 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004639 psa_set_key_algorithm( &attributes, alg );
4640 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004641
Gilles Peskine049c7532019-05-15 20:22:09 +02004642 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004643 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004644
Ronald Cron5425a212020-08-04 14:58:35 +02004645 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004646 input_data->x, input_data->len,
4647 signature, signature_size,
4648 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004649 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004650 /* The value of *signature_length is unspecified on error, but
4651 * whatever it is, it should be less than signature_size, so that
4652 * if the caller tries to read *signature_length bytes without
4653 * checking the error code then they don't overflow a buffer. */
4654 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004655
4656exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004657 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004658 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004659 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004660 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004661}
4662/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004663
4664/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004665void sign_verify_hash( int key_type_arg, data_t *key_data,
4666 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004667{
Ronald Cron5425a212020-08-04 14:58:35 +02004668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004669 psa_key_type_t key_type = key_type_arg;
4670 psa_algorithm_t alg = alg_arg;
4671 size_t key_bits;
4672 unsigned char *signature = NULL;
4673 size_t signature_size;
4674 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004676
Gilles Peskine8817f612018-12-18 00:18:46 +01004677 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004678
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004679 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004680 psa_set_key_algorithm( &attributes, alg );
4681 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004682
Gilles Peskine049c7532019-05-15 20:22:09 +02004683 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004684 &key ) );
4685 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004686 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004687
4688 /* Allocate a buffer which has the size advertized by the
4689 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004690 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004691 key_bits, alg );
4692 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004693 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004694 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004695
4696 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004697 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004698 input_data->x, input_data->len,
4699 signature, signature_size,
4700 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004701 /* Check that the signature length looks sensible. */
4702 TEST_ASSERT( signature_length <= signature_size );
4703 TEST_ASSERT( signature_length > 0 );
4704
4705 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004706 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004707 input_data->x, input_data->len,
4708 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004709
4710 if( input_data->len != 0 )
4711 {
4712 /* Flip a bit in the input and verify that the signature is now
4713 * detected as invalid. Flip a bit at the beginning, not at the end,
4714 * because ECDSA may ignore the last few bits of the input. */
4715 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004716 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004717 input_data->x, input_data->len,
4718 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004719 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004720 }
4721
4722exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004723 /*
4724 * Key attributes may have been returned by psa_get_key_attributes()
4725 * thus reset them as required.
4726 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004727 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004728
Ronald Cron5425a212020-08-04 14:58:35 +02004729 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004730 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004731 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004732}
4733/* END_CASE */
4734
4735/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004736void verify_hash( int key_type_arg, data_t *key_data,
4737 int alg_arg, data_t *hash_data,
4738 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004739{
Ronald Cron5425a212020-08-04 14:58:35 +02004740 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004741 psa_key_type_t key_type = key_type_arg;
4742 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004744
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004745 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004746
Gilles Peskine8817f612018-12-18 00:18:46 +01004747 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004748
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004749 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004750 psa_set_key_algorithm( &attributes, alg );
4751 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004752
Gilles Peskine049c7532019-05-15 20:22:09 +02004753 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004754 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004755
Ronald Cron5425a212020-08-04 14:58:35 +02004756 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004757 hash_data->x, hash_data->len,
4758 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004759
itayzafrir5c753392018-05-08 11:18:38 +03004760exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004761 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004762 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004763 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004764}
4765/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004766
4767/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004768void verify_hash_fail( int key_type_arg, data_t *key_data,
4769 int alg_arg, data_t *hash_data,
4770 data_t *signature_data,
4771 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004772{
Ronald Cron5425a212020-08-04 14:58:35 +02004773 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004774 psa_key_type_t key_type = key_type_arg;
4775 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004776 psa_status_t actual_status;
4777 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004778 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004779
Gilles Peskine8817f612018-12-18 00:18:46 +01004780 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004781
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004782 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004783 psa_set_key_algorithm( &attributes, alg );
4784 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004785
Gilles Peskine049c7532019-05-15 20:22:09 +02004786 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004787 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004788
Ronald Cron5425a212020-08-04 14:58:35 +02004789 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004790 hash_data->x, hash_data->len,
4791 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004792 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004793
4794exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004795 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004796 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004797 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004798}
4799/* END_CASE */
4800
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004801/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004802void sign_message_deterministic( int key_type_arg,
4803 data_t *key_data,
4804 int alg_arg,
4805 data_t *input_data,
4806 data_t *output_data )
4807{
4808 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4809 psa_key_type_t key_type = key_type_arg;
4810 psa_algorithm_t alg = alg_arg;
4811 size_t key_bits;
4812 unsigned char *signature = NULL;
4813 size_t signature_size;
4814 size_t signature_length = 0xdeadbeef;
4815 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4816
4817 PSA_ASSERT( psa_crypto_init( ) );
4818
4819 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4820 psa_set_key_algorithm( &attributes, alg );
4821 psa_set_key_type( &attributes, key_type );
4822
4823 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4824 &key ) );
4825 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4826 key_bits = psa_get_key_bits( &attributes );
4827
4828 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4829 TEST_ASSERT( signature_size != 0 );
4830 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4831 ASSERT_ALLOC( signature, signature_size );
4832
4833 PSA_ASSERT( psa_sign_message( key, alg,
4834 input_data->x, input_data->len,
4835 signature, signature_size,
4836 &signature_length ) );
4837
4838 ASSERT_COMPARE( output_data->x, output_data->len,
4839 signature, signature_length );
4840
4841exit:
4842 psa_reset_key_attributes( &attributes );
4843
4844 psa_destroy_key( key );
4845 mbedtls_free( signature );
4846 PSA_DONE( );
4847
4848}
4849/* END_CASE */
4850
4851/* BEGIN_CASE */
4852void sign_message_fail( int key_type_arg,
4853 data_t *key_data,
4854 int alg_arg,
4855 data_t *input_data,
4856 int signature_size_arg,
4857 int expected_status_arg )
4858{
4859 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4860 psa_key_type_t key_type = key_type_arg;
4861 psa_algorithm_t alg = alg_arg;
4862 size_t signature_size = signature_size_arg;
4863 psa_status_t actual_status;
4864 psa_status_t expected_status = expected_status_arg;
4865 unsigned char *signature = NULL;
4866 size_t signature_length = 0xdeadbeef;
4867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4868
4869 ASSERT_ALLOC( signature, signature_size );
4870
4871 PSA_ASSERT( psa_crypto_init( ) );
4872
4873 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4874 psa_set_key_algorithm( &attributes, alg );
4875 psa_set_key_type( &attributes, key_type );
4876
4877 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4878 &key ) );
4879
4880 actual_status = psa_sign_message( key, alg,
4881 input_data->x, input_data->len,
4882 signature, signature_size,
4883 &signature_length );
4884 TEST_EQUAL( actual_status, expected_status );
4885 /* The value of *signature_length is unspecified on error, but
4886 * whatever it is, it should be less than signature_size, so that
4887 * if the caller tries to read *signature_length bytes without
4888 * checking the error code then they don't overflow a buffer. */
4889 TEST_ASSERT( signature_length <= signature_size );
4890
4891exit:
4892 psa_reset_key_attributes( &attributes );
4893 psa_destroy_key( key );
4894 mbedtls_free( signature );
4895 PSA_DONE( );
4896}
4897/* END_CASE */
4898
4899/* BEGIN_CASE */
4900void sign_verify_message( int key_type_arg,
4901 data_t *key_data,
4902 int alg_arg,
4903 data_t *input_data )
4904{
4905 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4906 psa_key_type_t key_type = key_type_arg;
4907 psa_algorithm_t alg = alg_arg;
4908 size_t key_bits;
4909 unsigned char *signature = NULL;
4910 size_t signature_size;
4911 size_t signature_length = 0xdeadbeef;
4912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4913
4914 PSA_ASSERT( psa_crypto_init( ) );
4915
4916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4917 PSA_KEY_USAGE_VERIFY_MESSAGE );
4918 psa_set_key_algorithm( &attributes, alg );
4919 psa_set_key_type( &attributes, key_type );
4920
4921 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4922 &key ) );
4923 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4924 key_bits = psa_get_key_bits( &attributes );
4925
4926 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4927 TEST_ASSERT( signature_size != 0 );
4928 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4929 ASSERT_ALLOC( signature, signature_size );
4930
4931 PSA_ASSERT( psa_sign_message( key, alg,
4932 input_data->x, input_data->len,
4933 signature, signature_size,
4934 &signature_length ) );
4935 TEST_ASSERT( signature_length <= signature_size );
4936 TEST_ASSERT( signature_length > 0 );
4937
4938 PSA_ASSERT( psa_verify_message( key, alg,
4939 input_data->x, input_data->len,
4940 signature, signature_length ) );
4941
4942 if( input_data->len != 0 )
4943 {
4944 /* Flip a bit in the input and verify that the signature is now
4945 * detected as invalid. Flip a bit at the beginning, not at the end,
4946 * because ECDSA may ignore the last few bits of the input. */
4947 input_data->x[0] ^= 1;
4948 TEST_EQUAL( psa_verify_message( key, alg,
4949 input_data->x, input_data->len,
4950 signature, signature_length ),
4951 PSA_ERROR_INVALID_SIGNATURE );
4952 }
4953
4954exit:
4955 psa_reset_key_attributes( &attributes );
4956
4957 psa_destroy_key( key );
4958 mbedtls_free( signature );
4959 PSA_DONE( );
4960}
4961/* END_CASE */
4962
4963/* BEGIN_CASE */
4964void verify_message( int key_type_arg,
4965 data_t *key_data,
4966 int alg_arg,
4967 data_t *input_data,
4968 data_t *signature_data )
4969{
4970 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4971 psa_key_type_t key_type = key_type_arg;
4972 psa_algorithm_t alg = alg_arg;
4973 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4974
4975 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4976
4977 PSA_ASSERT( psa_crypto_init( ) );
4978
4979 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4980 psa_set_key_algorithm( &attributes, alg );
4981 psa_set_key_type( &attributes, key_type );
4982
4983 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4984 &key ) );
4985
4986 PSA_ASSERT( psa_verify_message( key, alg,
4987 input_data->x, input_data->len,
4988 signature_data->x, signature_data->len ) );
4989
4990exit:
4991 psa_reset_key_attributes( &attributes );
4992 psa_destroy_key( key );
4993 PSA_DONE( );
4994}
4995/* END_CASE */
4996
4997/* BEGIN_CASE */
4998void verify_message_fail( int key_type_arg,
4999 data_t *key_data,
5000 int alg_arg,
5001 data_t *hash_data,
5002 data_t *signature_data,
5003 int expected_status_arg )
5004{
5005 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5006 psa_key_type_t key_type = key_type_arg;
5007 psa_algorithm_t alg = alg_arg;
5008 psa_status_t actual_status;
5009 psa_status_t expected_status = expected_status_arg;
5010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5011
5012 PSA_ASSERT( psa_crypto_init( ) );
5013
5014 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5015 psa_set_key_algorithm( &attributes, alg );
5016 psa_set_key_type( &attributes, key_type );
5017
5018 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5019 &key ) );
5020
5021 actual_status = psa_verify_message( key, alg,
5022 hash_data->x, hash_data->len,
5023 signature_data->x,
5024 signature_data->len );
5025 TEST_EQUAL( actual_status, expected_status );
5026
5027exit:
5028 psa_reset_key_attributes( &attributes );
5029 psa_destroy_key( key );
5030 PSA_DONE( );
5031}
5032/* END_CASE */
5033
5034/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005035void asymmetric_encrypt( int key_type_arg,
5036 data_t *key_data,
5037 int alg_arg,
5038 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005039 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005040 int expected_output_length_arg,
5041 int expected_status_arg )
5042{
Ronald Cron5425a212020-08-04 14:58:35 +02005043 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005044 psa_key_type_t key_type = key_type_arg;
5045 psa_algorithm_t alg = alg_arg;
5046 size_t expected_output_length = expected_output_length_arg;
5047 size_t key_bits;
5048 unsigned char *output = NULL;
5049 size_t output_size;
5050 size_t output_length = ~0;
5051 psa_status_t actual_status;
5052 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005054
Gilles Peskine8817f612018-12-18 00:18:46 +01005055 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005056
Gilles Peskine656896e2018-06-29 19:12:28 +02005057 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005058 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5059 psa_set_key_algorithm( &attributes, alg );
5060 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005061 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005062 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005063
5064 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005065 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005066 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005067
Gilles Peskine656896e2018-06-29 19:12:28 +02005068 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005069 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005070 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005071
5072 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005073 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005074 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005075 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005076 output, output_size,
5077 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005078 TEST_EQUAL( actual_status, expected_status );
5079 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005080
Gilles Peskine68428122018-06-30 18:42:41 +02005081 /* If the label is empty, the test framework puts a non-null pointer
5082 * in label->x. Test that a null pointer works as well. */
5083 if( label->len == 0 )
5084 {
5085 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005086 if( output_size != 0 )
5087 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005088 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005089 input_data->x, input_data->len,
5090 NULL, label->len,
5091 output, output_size,
5092 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005093 TEST_EQUAL( actual_status, expected_status );
5094 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005095 }
5096
Gilles Peskine656896e2018-06-29 19:12:28 +02005097exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005098 /*
5099 * Key attributes may have been returned by psa_get_key_attributes()
5100 * thus reset them as required.
5101 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005102 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005103
Ronald Cron5425a212020-08-04 14:58:35 +02005104 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005105 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005106 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005107}
5108/* END_CASE */
5109
5110/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005111void asymmetric_encrypt_decrypt( int key_type_arg,
5112 data_t *key_data,
5113 int alg_arg,
5114 data_t *input_data,
5115 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005116{
Ronald Cron5425a212020-08-04 14:58:35 +02005117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005118 psa_key_type_t key_type = key_type_arg;
5119 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005120 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005121 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005122 size_t output_size;
5123 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005124 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005125 size_t output2_size;
5126 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005127 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005128
Gilles Peskine8817f612018-12-18 00:18:46 +01005129 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005130
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005131 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5132 psa_set_key_algorithm( &attributes, alg );
5133 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005134
Gilles Peskine049c7532019-05-15 20:22:09 +02005135 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005136 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005137
5138 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005139 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005140 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005141
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005142 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005143 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005144 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005145
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005146 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005147 TEST_ASSERT( output2_size <=
5148 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5149 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005150 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005151
Gilles Peskineeebd7382018-06-08 18:11:54 +02005152 /* We test encryption by checking that encrypt-then-decrypt gives back
5153 * the original plaintext because of the non-optional random
5154 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005155 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005156 input_data->x, input_data->len,
5157 label->x, label->len,
5158 output, output_size,
5159 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005160 /* We don't know what ciphertext length to expect, but check that
5161 * it looks sensible. */
5162 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005163
Ronald Cron5425a212020-08-04 14:58:35 +02005164 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005165 output, output_length,
5166 label->x, label->len,
5167 output2, output2_size,
5168 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005169 ASSERT_COMPARE( input_data->x, input_data->len,
5170 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005171
5172exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005173 /*
5174 * Key attributes may have been returned by psa_get_key_attributes()
5175 * thus reset them as required.
5176 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005177 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005178
Ronald Cron5425a212020-08-04 14:58:35 +02005179 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005180 mbedtls_free( output );
5181 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005182 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005183}
5184/* END_CASE */
5185
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005186/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005187void asymmetric_decrypt( int key_type_arg,
5188 data_t *key_data,
5189 int alg_arg,
5190 data_t *input_data,
5191 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005192 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005193{
Ronald Cron5425a212020-08-04 14:58:35 +02005194 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005195 psa_key_type_t key_type = key_type_arg;
5196 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005197 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005198 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005199 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005200 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005202
Gilles Peskine8817f612018-12-18 00:18:46 +01005203 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005204
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005205 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5206 psa_set_key_algorithm( &attributes, alg );
5207 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005208
Gilles Peskine049c7532019-05-15 20:22:09 +02005209 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005210 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005211
gabor-mezei-armceface22021-01-21 12:26:17 +01005212 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5213 key_bits = psa_get_key_bits( &attributes );
5214
5215 /* Determine the maximum ciphertext length */
5216 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5217 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5218 ASSERT_ALLOC( output, output_size );
5219
Ronald Cron5425a212020-08-04 14:58:35 +02005220 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005221 input_data->x, input_data->len,
5222 label->x, label->len,
5223 output,
5224 output_size,
5225 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005226 ASSERT_COMPARE( expected_data->x, expected_data->len,
5227 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005228
Gilles Peskine68428122018-06-30 18:42:41 +02005229 /* If the label is empty, the test framework puts a non-null pointer
5230 * in label->x. Test that a null pointer works as well. */
5231 if( label->len == 0 )
5232 {
5233 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005234 if( output_size != 0 )
5235 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005236 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005237 input_data->x, input_data->len,
5238 NULL, label->len,
5239 output,
5240 output_size,
5241 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005242 ASSERT_COMPARE( expected_data->x, expected_data->len,
5243 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005244 }
5245
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005246exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005247 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005248 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005249 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005250 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005251}
5252/* END_CASE */
5253
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005254/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005255void asymmetric_decrypt_fail( int key_type_arg,
5256 data_t *key_data,
5257 int alg_arg,
5258 data_t *input_data,
5259 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005260 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005261 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005262{
Ronald Cron5425a212020-08-04 14:58:35 +02005263 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005264 psa_key_type_t key_type = key_type_arg;
5265 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005266 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005267 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005268 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005269 psa_status_t actual_status;
5270 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005272
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005273 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005274
Gilles Peskine8817f612018-12-18 00:18:46 +01005275 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005276
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005277 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5278 psa_set_key_algorithm( &attributes, alg );
5279 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005280
Gilles Peskine049c7532019-05-15 20:22:09 +02005281 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005282 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005283
Ronald Cron5425a212020-08-04 14:58:35 +02005284 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005285 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005286 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005287 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005288 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005289 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005290 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005291
Gilles Peskine68428122018-06-30 18:42:41 +02005292 /* If the label is empty, the test framework puts a non-null pointer
5293 * in label->x. Test that a null pointer works as well. */
5294 if( label->len == 0 )
5295 {
5296 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005297 if( output_size != 0 )
5298 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005299 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005300 input_data->x, input_data->len,
5301 NULL, label->len,
5302 output, output_size,
5303 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005304 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005305 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005306 }
5307
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005308exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005309 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005310 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005311 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005312 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005313}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005314/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005315
5316/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005317void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005318{
5319 /* Test each valid way of initializing the object, except for `= {0}`, as
5320 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5321 * though it's OK by the C standard. We could test for this, but we'd need
5322 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005323 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005324 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5325 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5326 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005327
5328 memset( &zero, 0, sizeof( zero ) );
5329
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005330 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005331 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005332 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005333 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005334 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005335 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005336 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005337
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005339 PSA_ASSERT( psa_key_derivation_abort(&func) );
5340 PSA_ASSERT( psa_key_derivation_abort(&init) );
5341 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005342}
5343/* END_CASE */
5344
Janos Follath16de4a42019-06-13 16:32:24 +01005345/* BEGIN_CASE */
5346void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005347{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005348 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005349 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005351
Gilles Peskine8817f612018-12-18 00:18:46 +01005352 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005353
Janos Follath16de4a42019-06-13 16:32:24 +01005354 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005355 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005356
5357exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005358 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005359 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005360}
5361/* END_CASE */
5362
Janos Follathaf3c2a02019-06-12 12:34:34 +01005363/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005364void derive_set_capacity( int alg_arg, int capacity_arg,
5365 int expected_status_arg )
5366{
5367 psa_algorithm_t alg = alg_arg;
5368 size_t capacity = capacity_arg;
5369 psa_status_t expected_status = expected_status_arg;
5370 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5371
5372 PSA_ASSERT( psa_crypto_init( ) );
5373
5374 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5375
5376 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5377 expected_status );
5378
5379exit:
5380 psa_key_derivation_abort( &operation );
5381 PSA_DONE( );
5382}
5383/* END_CASE */
5384
5385/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005386void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005387 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005388 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005389 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005390 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005391 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005392 int expected_status_arg3,
5393 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005394{
5395 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005396 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5397 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005398 psa_status_t expected_statuses[] = {expected_status_arg1,
5399 expected_status_arg2,
5400 expected_status_arg3};
5401 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005402 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5403 MBEDTLS_SVC_KEY_ID_INIT,
5404 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005405 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5407 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005408 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005409 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005410 psa_status_t expected_output_status = expected_output_status_arg;
5411 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005412
5413 PSA_ASSERT( psa_crypto_init( ) );
5414
5415 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5416 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005417
5418 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5419
5420 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5421 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005422 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005423 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005424 psa_set_key_type( &attributes, key_types[i] );
5425 PSA_ASSERT( psa_import_key( &attributes,
5426 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005427 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005428 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5429 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5430 {
5431 // When taking a private key as secret input, use key agreement
5432 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005433 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5434 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005435 expected_statuses[i] );
5436 }
5437 else
5438 {
5439 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005440 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005441 expected_statuses[i] );
5442 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005443 }
5444 else
5445 {
5446 TEST_EQUAL( psa_key_derivation_input_bytes(
5447 &operation, steps[i],
5448 inputs[i]->x, inputs[i]->len ),
5449 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005450 }
5451 }
5452
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005453 if( output_key_type != PSA_KEY_TYPE_NONE )
5454 {
5455 psa_reset_key_attributes( &attributes );
5456 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5457 psa_set_key_bits( &attributes, 8 );
5458 actual_output_status =
5459 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005460 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005461 }
5462 else
5463 {
5464 uint8_t buffer[1];
5465 actual_output_status =
5466 psa_key_derivation_output_bytes( &operation,
5467 buffer, sizeof( buffer ) );
5468 }
5469 TEST_EQUAL( actual_output_status, expected_output_status );
5470
Janos Follathaf3c2a02019-06-12 12:34:34 +01005471exit:
5472 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005473 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5474 psa_destroy_key( keys[i] );
5475 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005476 PSA_DONE( );
5477}
5478/* END_CASE */
5479
Janos Follathd958bb72019-07-03 15:02:16 +01005480/* BEGIN_CASE */
5481void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005482{
Janos Follathd958bb72019-07-03 15:02:16 +01005483 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005484 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005485 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005486 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005487 unsigned char input1[] = "Input 1";
5488 size_t input1_length = sizeof( input1 );
5489 unsigned char input2[] = "Input 2";
5490 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005491 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005492 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005493 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5494 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5495 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005496 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005497
Gilles Peskine8817f612018-12-18 00:18:46 +01005498 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005499
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005500 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5501 psa_set_key_algorithm( &attributes, alg );
5502 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005503
Gilles Peskine73676cb2019-05-15 20:15:10 +02005504 PSA_ASSERT( psa_import_key( &attributes,
5505 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005506 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005507
5508 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005509 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5510 input1, input1_length,
5511 input2, input2_length,
5512 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005513 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005514
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005515 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005516 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005517 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005518
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005519 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005520
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005521 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005522 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005523
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005524exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005526 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005527 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005528}
5529/* END_CASE */
5530
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005531/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005532void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005533{
5534 uint8_t output_buffer[16];
5535 size_t buffer_size = 16;
5536 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005538
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005539 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5540 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005541 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005542
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005543 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005544 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005545
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005546 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005547
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005548 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5549 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005550 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005551
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005552 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005553 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005554
5555exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005556 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005557}
5558/* END_CASE */
5559
5560/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005561void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005562 int step1_arg, data_t *input1,
5563 int step2_arg, data_t *input2,
5564 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005565 int requested_capacity_arg,
5566 data_t *expected_output1,
5567 data_t *expected_output2 )
5568{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005569 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005570 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5571 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005572 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5573 MBEDTLS_SVC_KEY_ID_INIT,
5574 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005575 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005576 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005577 uint8_t *expected_outputs[2] =
5578 {expected_output1->x, expected_output2->x};
5579 size_t output_sizes[2] =
5580 {expected_output1->len, expected_output2->len};
5581 size_t output_buffer_size = 0;
5582 uint8_t *output_buffer = NULL;
5583 size_t expected_capacity;
5584 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005586 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005587 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005588
5589 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5590 {
5591 if( output_sizes[i] > output_buffer_size )
5592 output_buffer_size = output_sizes[i];
5593 if( output_sizes[i] == 0 )
5594 expected_outputs[i] = NULL;
5595 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005596 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005597 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005598
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005599 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5600 psa_set_key_algorithm( &attributes, alg );
5601 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005602
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005603 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005604 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5605 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5606 requested_capacity ) );
5607 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005608 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005609 switch( steps[i] )
5610 {
5611 case 0:
5612 break;
5613 case PSA_KEY_DERIVATION_INPUT_SECRET:
5614 PSA_ASSERT( psa_import_key( &attributes,
5615 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005616 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005617
5618 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5619 {
5620 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5621 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5622 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5623 }
5624
Gilles Peskine1468da72019-05-29 17:35:49 +02005625 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005626 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005627 break;
5628 default:
5629 PSA_ASSERT( psa_key_derivation_input_bytes(
5630 &operation, steps[i],
5631 inputs[i]->x, inputs[i]->len ) );
5632 break;
5633 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005634 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005635
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005636 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005637 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005638 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005639 expected_capacity = requested_capacity;
5640
5641 /* Expansion phase. */
5642 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5643 {
5644 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005645 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005646 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005647 if( expected_capacity == 0 && output_sizes[i] == 0 )
5648 {
5649 /* Reading 0 bytes when 0 bytes are available can go either way. */
5650 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005651 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005652 continue;
5653 }
5654 else if( expected_capacity == 0 ||
5655 output_sizes[i] > expected_capacity )
5656 {
5657 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005658 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005659 expected_capacity = 0;
5660 continue;
5661 }
5662 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005663 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005664 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005665 ASSERT_COMPARE( output_buffer, output_sizes[i],
5666 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005667 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005668 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005669 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005670 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005671 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005672 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005673 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005674
5675exit:
5676 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005677 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005678 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5679 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005680 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005681}
5682/* END_CASE */
5683
5684/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005685void derive_full( int alg_arg,
5686 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005687 data_t *input1,
5688 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005689 int requested_capacity_arg )
5690{
Ronald Cron5425a212020-08-04 14:58:35 +02005691 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005692 psa_algorithm_t alg = alg_arg;
5693 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005694 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005695 unsigned char output_buffer[16];
5696 size_t expected_capacity = requested_capacity;
5697 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005699
Gilles Peskine8817f612018-12-18 00:18:46 +01005700 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005701
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005702 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5703 psa_set_key_algorithm( &attributes, alg );
5704 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005705
Gilles Peskine049c7532019-05-15 20:22:09 +02005706 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005707 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005708
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005709 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5710 input1->x, input1->len,
5711 input2->x, input2->len,
5712 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005713 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005714
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005715 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005716 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005717 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005718
5719 /* Expansion phase. */
5720 while( current_capacity > 0 )
5721 {
5722 size_t read_size = sizeof( output_buffer );
5723 if( read_size > current_capacity )
5724 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005725 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005726 output_buffer,
5727 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005728 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005729 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005730 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005731 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005732 }
5733
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005734 /* Check that the operation refuses to go over capacity. */
5735 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005736 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005737
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005738 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005739
5740exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005741 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005742 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005743 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005744}
5745/* END_CASE */
5746
Janos Follathe60c9052019-07-03 13:51:30 +01005747/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005748void derive_key_exercise( int alg_arg,
5749 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005750 data_t *input1,
5751 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005752 int derived_type_arg,
5753 int derived_bits_arg,
5754 int derived_usage_arg,
5755 int derived_alg_arg )
5756{
Ronald Cron5425a212020-08-04 14:58:35 +02005757 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5758 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005759 psa_algorithm_t alg = alg_arg;
5760 psa_key_type_t derived_type = derived_type_arg;
5761 size_t derived_bits = derived_bits_arg;
5762 psa_key_usage_t derived_usage = derived_usage_arg;
5763 psa_algorithm_t derived_alg = derived_alg_arg;
5764 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005765 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005767 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005768
Gilles Peskine8817f612018-12-18 00:18:46 +01005769 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005770
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005771 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5772 psa_set_key_algorithm( &attributes, alg );
5773 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005774 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005775 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005776
5777 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005778 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5779 input1->x, input1->len,
5780 input2->x, input2->len,
5781 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005782 goto exit;
5783
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005784 psa_set_key_usage_flags( &attributes, derived_usage );
5785 psa_set_key_algorithm( &attributes, derived_alg );
5786 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005787 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005788 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005789 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005790
5791 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005792 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005793 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5794 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005795
5796 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005797 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005798 goto exit;
5799
5800exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005801 /*
5802 * Key attributes may have been returned by psa_get_key_attributes()
5803 * thus reset them as required.
5804 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005805 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005806
5807 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005808 psa_destroy_key( base_key );
5809 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005810 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005811}
5812/* END_CASE */
5813
Janos Follath42fd8882019-07-03 14:17:09 +01005814/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005815void derive_key_export( int alg_arg,
5816 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005817 data_t *input1,
5818 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005819 int bytes1_arg,
5820 int bytes2_arg )
5821{
Ronald Cron5425a212020-08-04 14:58:35 +02005822 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5823 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005824 psa_algorithm_t alg = alg_arg;
5825 size_t bytes1 = bytes1_arg;
5826 size_t bytes2 = bytes2_arg;
5827 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005828 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005829 uint8_t *output_buffer = NULL;
5830 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005831 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5832 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005833 size_t length;
5834
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005835 ASSERT_ALLOC( output_buffer, capacity );
5836 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005837 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005838
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005839 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5840 psa_set_key_algorithm( &base_attributes, alg );
5841 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005842 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005843 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005844
5845 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005846 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5847 input1->x, input1->len,
5848 input2->x, input2->len,
5849 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005850 goto exit;
5851
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005852 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005853 output_buffer,
5854 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005855 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005856
5857 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005858 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5859 input1->x, input1->len,
5860 input2->x, input2->len,
5861 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005862 goto exit;
5863
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005864 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5865 psa_set_key_algorithm( &derived_attributes, 0 );
5866 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005867 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005868 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005869 &derived_key ) );
5870 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005871 export_buffer, bytes1,
5872 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005873 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005874 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005875 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005876 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005877 &derived_key ) );
5878 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005879 export_buffer + bytes1, bytes2,
5880 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005881 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005882
5883 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005884 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5885 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005886
5887exit:
5888 mbedtls_free( output_buffer );
5889 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005890 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005891 psa_destroy_key( base_key );
5892 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005893 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005894}
5895/* END_CASE */
5896
5897/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005898void derive_key( int alg_arg,
5899 data_t *key_data, data_t *input1, data_t *input2,
5900 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005901 int expected_status_arg,
5902 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005903{
Ronald Cron5425a212020-08-04 14:58:35 +02005904 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5905 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005906 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005907 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005908 size_t bits = bits_arg;
5909 psa_status_t expected_status = expected_status_arg;
5910 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5911 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5912 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5913
5914 PSA_ASSERT( psa_crypto_init( ) );
5915
5916 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5917 psa_set_key_algorithm( &base_attributes, alg );
5918 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5919 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005920 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005921
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005922 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5923 input1->x, input1->len,
5924 input2->x, input2->len,
5925 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005926 goto exit;
5927
5928 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5929 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005930 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005931 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005932
5933 psa_status_t status =
5934 psa_key_derivation_output_key( &derived_attributes,
5935 &operation,
5936 &derived_key );
5937 if( is_large_output > 0 )
5938 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5939 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005940
5941exit:
5942 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005943 psa_destroy_key( base_key );
5944 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005945 PSA_DONE( );
5946}
5947/* END_CASE */
5948
5949/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005950void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005951 int our_key_type_arg, int our_key_alg_arg,
5952 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005953 int expected_status_arg )
5954{
Ronald Cron5425a212020-08-04 14:58:35 +02005955 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005956 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005957 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005958 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005959 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005961 psa_status_t expected_status = expected_status_arg;
5962 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005963
Gilles Peskine8817f612018-12-18 00:18:46 +01005964 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005965
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005966 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005967 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005968 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005969 PSA_ASSERT( psa_import_key( &attributes,
5970 our_key_data->x, our_key_data->len,
5971 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005972
Gilles Peskine77f40d82019-04-11 21:27:06 +02005973 /* The tests currently include inputs that should fail at either step.
5974 * Test cases that fail at the setup step should be changed to call
5975 * key_derivation_setup instead, and this function should be renamed
5976 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005977 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005978 if( status == PSA_SUCCESS )
5979 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005980 TEST_EQUAL( psa_key_derivation_key_agreement(
5981 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5982 our_key,
5983 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005984 expected_status );
5985 }
5986 else
5987 {
5988 TEST_ASSERT( status == expected_status );
5989 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005990
5991exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005992 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005993 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005994 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005995}
5996/* END_CASE */
5997
5998/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005999void raw_key_agreement( int alg_arg,
6000 int our_key_type_arg, data_t *our_key_data,
6001 data_t *peer_key_data,
6002 data_t *expected_output )
6003{
Ronald Cron5425a212020-08-04 14:58:35 +02006004 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006005 psa_algorithm_t alg = alg_arg;
6006 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006008 unsigned char *output = NULL;
6009 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006010 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006011
6012 ASSERT_ALLOC( output, expected_output->len );
6013 PSA_ASSERT( psa_crypto_init( ) );
6014
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006015 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6016 psa_set_key_algorithm( &attributes, alg );
6017 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006018 PSA_ASSERT( psa_import_key( &attributes,
6019 our_key_data->x, our_key_data->len,
6020 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006021
gabor-mezei-armceface22021-01-21 12:26:17 +01006022 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6023 key_bits = psa_get_key_bits( &attributes );
6024
Gilles Peskinebe697d82019-05-16 18:00:41 +02006025 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6026 peer_key_data->x, peer_key_data->len,
6027 output, expected_output->len,
6028 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006029 ASSERT_COMPARE( output, output_length,
6030 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006031 TEST_ASSERT( output_length <=
6032 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6033 TEST_ASSERT( output_length <=
6034 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006035
6036exit:
6037 mbedtls_free( output );
6038 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006039 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006040}
6041/* END_CASE */
6042
6043/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006044void key_agreement_capacity( int alg_arg,
6045 int our_key_type_arg, data_t *our_key_data,
6046 data_t *peer_key_data,
6047 int expected_capacity_arg )
6048{
Ronald Cron5425a212020-08-04 14:58:35 +02006049 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006050 psa_algorithm_t alg = alg_arg;
6051 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006052 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006054 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006055 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006056
Gilles Peskine8817f612018-12-18 00:18:46 +01006057 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006058
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006059 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6060 psa_set_key_algorithm( &attributes, alg );
6061 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006062 PSA_ASSERT( psa_import_key( &attributes,
6063 our_key_data->x, our_key_data->len,
6064 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006065
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006066 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006067 PSA_ASSERT( psa_key_derivation_key_agreement(
6068 &operation,
6069 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6070 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006071 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6072 {
6073 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006074 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006075 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006076 NULL, 0 ) );
6077 }
Gilles Peskine59685592018-09-18 12:11:34 +02006078
Gilles Peskinebf491972018-10-25 22:36:12 +02006079 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006080 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006081 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006082 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006083
Gilles Peskinebf491972018-10-25 22:36:12 +02006084 /* Test the actual capacity by reading the output. */
6085 while( actual_capacity > sizeof( output ) )
6086 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006087 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006088 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006089 actual_capacity -= sizeof( output );
6090 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006091 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006092 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006093 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006094 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006095
Gilles Peskine59685592018-09-18 12:11:34 +02006096exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006097 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006098 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006099 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006100}
6101/* END_CASE */
6102
6103/* BEGIN_CASE */
6104void key_agreement_output( int alg_arg,
6105 int our_key_type_arg, data_t *our_key_data,
6106 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006107 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006108{
Ronald Cron5425a212020-08-04 14:58:35 +02006109 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006110 psa_algorithm_t alg = alg_arg;
6111 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006112 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006114 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006115
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006116 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6117 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006118
Gilles Peskine8817f612018-12-18 00:18:46 +01006119 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006120
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006121 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6122 psa_set_key_algorithm( &attributes, alg );
6123 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006124 PSA_ASSERT( psa_import_key( &attributes,
6125 our_key_data->x, our_key_data->len,
6126 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006127
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006128 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006129 PSA_ASSERT( psa_key_derivation_key_agreement(
6130 &operation,
6131 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6132 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006133 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6134 {
6135 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006136 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006137 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006138 NULL, 0 ) );
6139 }
Gilles Peskine59685592018-09-18 12:11:34 +02006140
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006141 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006142 actual_output,
6143 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006144 ASSERT_COMPARE( actual_output, expected_output1->len,
6145 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006146 if( expected_output2->len != 0 )
6147 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006148 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006149 actual_output,
6150 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006151 ASSERT_COMPARE( actual_output, expected_output2->len,
6152 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006153 }
Gilles Peskine59685592018-09-18 12:11:34 +02006154
6155exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006156 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006157 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006158 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006159 mbedtls_free( actual_output );
6160}
6161/* END_CASE */
6162
6163/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006164void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006165{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006166 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006167 unsigned char *output = NULL;
6168 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006169 size_t i;
6170 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006171
Simon Butcher49f8e312020-03-03 15:51:50 +00006172 TEST_ASSERT( bytes_arg >= 0 );
6173
Gilles Peskine91892022021-02-08 19:50:26 +01006174 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006175 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006176
Gilles Peskine8817f612018-12-18 00:18:46 +01006177 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006178
Gilles Peskinea50d7392018-06-21 10:22:13 +02006179 /* Run several times, to ensure that every output byte will be
6180 * nonzero at least once with overwhelming probability
6181 * (2^(-8*number_of_runs)). */
6182 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006183 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006184 if( bytes != 0 )
6185 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006186 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006187
Gilles Peskinea50d7392018-06-21 10:22:13 +02006188 for( i = 0; i < bytes; i++ )
6189 {
6190 if( output[i] != 0 )
6191 ++changed[i];
6192 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006193 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006194
6195 /* Check that every byte was changed to nonzero at least once. This
6196 * validates that psa_generate_random is overwriting every byte of
6197 * the output buffer. */
6198 for( i = 0; i < bytes; i++ )
6199 {
6200 TEST_ASSERT( changed[i] != 0 );
6201 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006202
6203exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006204 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006205 mbedtls_free( output );
6206 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006207}
6208/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006209
6210/* BEGIN_CASE */
6211void generate_key( int type_arg,
6212 int bits_arg,
6213 int usage_arg,
6214 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006215 int expected_status_arg,
6216 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006217{
Ronald Cron5425a212020-08-04 14:58:35 +02006218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006219 psa_key_type_t type = type_arg;
6220 psa_key_usage_t usage = usage_arg;
6221 size_t bits = bits_arg;
6222 psa_algorithm_t alg = alg_arg;
6223 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006224 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006225 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006226
Gilles Peskine8817f612018-12-18 00:18:46 +01006227 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006228
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006229 psa_set_key_usage_flags( &attributes, usage );
6230 psa_set_key_algorithm( &attributes, alg );
6231 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006232 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006233
6234 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006235 psa_status_t status = psa_generate_key( &attributes, &key );
6236
6237 if( is_large_key > 0 )
6238 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6239 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006240 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006241 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006242
6243 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006244 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006245 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6246 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006247
Gilles Peskine818ca122018-06-20 18:16:48 +02006248 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006249 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006250 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006251
6252exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006253 /*
6254 * Key attributes may have been returned by psa_get_key_attributes()
6255 * thus reset them as required.
6256 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006257 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006258
Ronald Cron5425a212020-08-04 14:58:35 +02006259 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006260 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006261}
6262/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006263
Ronald Cronee414c72021-03-18 18:50:08 +01006264/* 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 +02006265void generate_key_rsa( int bits_arg,
6266 data_t *e_arg,
6267 int expected_status_arg )
6268{
Ronald Cron5425a212020-08-04 14:58:35 +02006269 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006270 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006271 size_t bits = bits_arg;
6272 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6273 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6274 psa_status_t expected_status = expected_status_arg;
6275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6276 uint8_t *exported = NULL;
6277 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006278 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006279 size_t exported_length = SIZE_MAX;
6280 uint8_t *e_read_buffer = NULL;
6281 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006282 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006283 size_t e_read_length = SIZE_MAX;
6284
6285 if( e_arg->len == 0 ||
6286 ( e_arg->len == 3 &&
6287 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6288 {
6289 is_default_public_exponent = 1;
6290 e_read_size = 0;
6291 }
6292 ASSERT_ALLOC( e_read_buffer, e_read_size );
6293 ASSERT_ALLOC( exported, exported_size );
6294
6295 PSA_ASSERT( psa_crypto_init( ) );
6296
6297 psa_set_key_usage_flags( &attributes, usage );
6298 psa_set_key_algorithm( &attributes, alg );
6299 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6300 e_arg->x, e_arg->len ) );
6301 psa_set_key_bits( &attributes, bits );
6302
6303 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006304 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006305 if( expected_status != PSA_SUCCESS )
6306 goto exit;
6307
6308 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006309 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006310 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6311 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6312 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6313 e_read_buffer, e_read_size,
6314 &e_read_length ) );
6315 if( is_default_public_exponent )
6316 TEST_EQUAL( e_read_length, 0 );
6317 else
6318 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6319
6320 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006321 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006322 goto exit;
6323
6324 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006325 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006326 exported, exported_size,
6327 &exported_length ) );
6328 {
6329 uint8_t *p = exported;
6330 uint8_t *end = exported + exported_length;
6331 size_t len;
6332 /* RSAPublicKey ::= SEQUENCE {
6333 * modulus INTEGER, -- n
6334 * publicExponent INTEGER } -- e
6335 */
6336 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006337 MBEDTLS_ASN1_SEQUENCE |
6338 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006339 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006340 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6341 MBEDTLS_ASN1_INTEGER ) );
6342 if( len >= 1 && p[0] == 0 )
6343 {
6344 ++p;
6345 --len;
6346 }
6347 if( e_arg->len == 0 )
6348 {
6349 TEST_EQUAL( len, 3 );
6350 TEST_EQUAL( p[0], 1 );
6351 TEST_EQUAL( p[1], 0 );
6352 TEST_EQUAL( p[2], 1 );
6353 }
6354 else
6355 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6356 }
6357
6358exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006359 /*
6360 * Key attributes may have been returned by psa_get_key_attributes() or
6361 * set by psa_set_key_domain_parameters() thus reset them as required.
6362 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006363 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006364
Ronald Cron5425a212020-08-04 14:58:35 +02006365 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006366 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006367 mbedtls_free( e_read_buffer );
6368 mbedtls_free( exported );
6369}
6370/* END_CASE */
6371
Darryl Greend49a4992018-06-18 17:27:26 +01006372/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006373void persistent_key_load_key_from_storage( data_t *data,
6374 int type_arg, int bits_arg,
6375 int usage_flags_arg, int alg_arg,
6376 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006377{
Ronald Cron71016a92020-08-28 19:01:50 +02006378 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006379 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006380 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6381 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006382 psa_key_type_t type = type_arg;
6383 size_t bits = bits_arg;
6384 psa_key_usage_t usage_flags = usage_flags_arg;
6385 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006386 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006387 unsigned char *first_export = NULL;
6388 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006389 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006390 size_t first_exported_length;
6391 size_t second_exported_length;
6392
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006393 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6394 {
6395 ASSERT_ALLOC( first_export, export_size );
6396 ASSERT_ALLOC( second_export, export_size );
6397 }
Darryl Greend49a4992018-06-18 17:27:26 +01006398
Gilles Peskine8817f612018-12-18 00:18:46 +01006399 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006400
Gilles Peskinec87af662019-05-15 16:12:22 +02006401 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006402 psa_set_key_usage_flags( &attributes, usage_flags );
6403 psa_set_key_algorithm( &attributes, alg );
6404 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006405 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006406
Darryl Green0c6575a2018-11-07 16:05:30 +00006407 switch( generation_method )
6408 {
6409 case IMPORT_KEY:
6410 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006411 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006412 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006413 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006414
Darryl Green0c6575a2018-11-07 16:05:30 +00006415 case GENERATE_KEY:
6416 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006417 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006418 break;
6419
6420 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006421#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006422 {
6423 /* Create base key */
6424 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6425 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6426 psa_set_key_usage_flags( &base_attributes,
6427 PSA_KEY_USAGE_DERIVE );
6428 psa_set_key_algorithm( &base_attributes, derive_alg );
6429 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006430 PSA_ASSERT( psa_import_key( &base_attributes,
6431 data->x, data->len,
6432 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006433 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006434 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006435 PSA_ASSERT( psa_key_derivation_input_key(
6436 &operation,
6437 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006438 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006439 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006440 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006441 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6442 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006443 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006444 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006445 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006446 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006447 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006448#else
6449 TEST_ASSUME( ! "KDF not supported in this configuration" );
6450#endif
6451 break;
6452
6453 default:
6454 TEST_ASSERT( ! "generation_method not implemented in test" );
6455 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006456 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006457 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006458
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006459 /* Export the key if permitted by the key policy. */
6460 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6461 {
Ronald Cron5425a212020-08-04 14:58:35 +02006462 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006463 first_export, export_size,
6464 &first_exported_length ) );
6465 if( generation_method == IMPORT_KEY )
6466 ASSERT_COMPARE( data->x, data->len,
6467 first_export, first_exported_length );
6468 }
Darryl Greend49a4992018-06-18 17:27:26 +01006469
6470 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006471 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006472 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006473 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006474
Darryl Greend49a4992018-06-18 17:27:26 +01006475 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006476 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006477 TEST_ASSERT( mbedtls_svc_key_id_equal(
6478 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006479 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6480 PSA_KEY_LIFETIME_PERSISTENT );
6481 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6482 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6483 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6484 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006485
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006486 /* Export the key again if permitted by the key policy. */
6487 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006488 {
Ronald Cron5425a212020-08-04 14:58:35 +02006489 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006490 second_export, export_size,
6491 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006492 ASSERT_COMPARE( first_export, first_exported_length,
6493 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006494 }
6495
6496 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006497 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006498 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006499
6500exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006501 /*
6502 * Key attributes may have been returned by psa_get_key_attributes()
6503 * thus reset them as required.
6504 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006505 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006506
Darryl Greend49a4992018-06-18 17:27:26 +01006507 mbedtls_free( first_export );
6508 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006509 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006510 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006511 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006512 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006513}
6514/* END_CASE */