blob: 29cda92ebf19b1e8fbb9ce58a1d3c0f679c0b13e [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 Elliott1c67e0b2021-09-19 13:11:50 +0100274typedef enum
275{
276 USE_NULL_TAG = 0,
277 USE_GIVEN_TAG = 1,
278} tagusage_method;
279
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100280/*!
281 * \brief Internal Function for AEAD multipart tests.
282 *
283 * \param key_type_arg Type of key passed in
284 * \param key_data The encryption / decryption key data
285 * \param alg_arg The type of algorithm used
286 * \param nonce Nonce data
287 * \param additional_data Additional data
288 * \param ad_part_len If not -1, the length of chunks to
289 * feed additional data in to be encrypted /
290 * decrypted. If -1, no chunking.
291 * \param input_data Data to encrypt / decrypt
292 * \param data_part_len If not -1, the length of chunks to feed the
293 * data in to be encrypted / decrypted. If -1,
294 * no chunking
295 * \param do_set_lengths If non-zero, then set lengths prior to
296 * calling encryption / decryption.
297 * \param expected_output Expected output
Paul Elliott41ffae12021-07-22 21:52:01 +0100298 * \param expect_valid_signature If non zero, we expect the signature to be
299 * valid
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100300 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100301 * \param do_zero_parts If non-zero, interleave zero length chunks
302 * with normal length chunks
303 * \param swap_set_functions If non-zero, swap the order of set lengths
304 * and set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100305 *
306 * \return int Zero on failure, non-zero on success.
307 *
308 */
309static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
310 int alg_arg,
311 data_t *nonce,
312 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100313 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100315 int data_part_len_arg,
Paul Elliott33746aa2021-09-15 16:40:40 +0100316 setlengths_method set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100318 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100319 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100320{
321 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
322 psa_key_type_t key_type = key_type_arg;
323 psa_algorithm_t alg = alg_arg;
324 psa_aead_operation_t operation;
325 unsigned char *output_data = NULL;
326 unsigned char *part_data = NULL;
327 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100328 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100329 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100330 size_t output_size = 0;
331 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100332 size_t output_length = 0;
333 size_t key_bits = 0;
334 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100335 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100336 size_t part_length = 0;
337 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100339 size_t ad_part_len = 0;
340 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
343 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100346 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100347
Paul Elliottd3f82412021-06-16 16:52:21 +0100348 PSA_ASSERT( psa_crypto_init( ) );
349
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100350 if( is_encrypt )
351 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
352 else
353 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
354
Paul Elliottd3f82412021-06-16 16:52:21 +0100355 psa_set_key_algorithm( &attributes, alg );
356 psa_set_key_type( &attributes, key_type );
357
358 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
359 &key ) );
360
361 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
362 key_bits = psa_get_key_bits( &attributes );
363
364 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
365
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100366 if( is_encrypt )
367 {
368 /* Tag gets written at end of buffer. */
369 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
370 ( input_data->len +
371 tag_length ) );
372 data_true_size = input_data->len;
373 }
374 else
375 {
376 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
377 ( input_data->len -
378 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100379
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100380 /* Do not want to attempt to decrypt tag. */
381 data_true_size = input_data->len - tag_length;
382 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100383
384 ASSERT_ALLOC( output_data, output_size );
385
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100386 if( is_encrypt )
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
391 else
392 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100393 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
394 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100395 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100396
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100397 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100398
399 operation = psa_aead_operation_init( );
400
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100401
402 if( is_encrypt )
403 status = psa_aead_encrypt_setup( &operation, key, alg );
404 else
405 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
407 /* If the operation is not supported, just skip and not fail in case the
408 * encryption involves a common limitation of cryptography hardwares and
409 * an alternative implementation. */
410 if( status == PSA_ERROR_NOT_SUPPORTED )
411 {
412 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
413 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
414 }
415
416 PSA_ASSERT( status );
417
Paul Elliott33746aa2021-09-15 16:40:40 +0100418 if( set_lengths_method == DO_NOT_SET_LENGTHS )
419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100421 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100422 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
423 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100424 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
425 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100426 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100427 {
428 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
429
Paul Elliott33746aa2021-09-15 16:40:40 +0100430 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
431 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100432 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100434 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100435 {
436 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100437 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100438
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100439 for( part_offset = 0, part_count = 0;
440 part_offset < additional_data->len;
441 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100443 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100444 {
Paul Elliott329d5382021-07-22 17:10:45 +0100445 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100447 else if( additional_data->len - part_offset < ad_part_len )
448 {
449 part_length = additional_data->len - part_offset;
450 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 else
452 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100453 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100454 }
455
456 PSA_ASSERT( psa_aead_update_ad( &operation,
457 additional_data->x + part_offset,
458 part_length ) );
459
Paul Elliottd3f82412021-06-16 16:52:21 +0100460 }
461 }
462 else
463 {
464 /* Pass additional data in one go. */
465 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
466 additional_data->len ) );
467 }
468
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100469 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100470 {
471 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100472 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100473 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100474 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100475
476 ASSERT_ALLOC( part_data, part_data_size );
477
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100478 for( part_offset = 0, part_count = 0;
479 part_offset < data_true_size;
480 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100482 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 {
Paul Elliott329d5382021-07-22 17:10:45 +0100484 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100486 else if( ( data_true_size - part_offset ) < data_part_len )
487 {
488 part_length = ( data_true_size - part_offset );
489 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 else
491 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100492 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100493 }
494
495 PSA_ASSERT( psa_aead_update( &operation,
496 ( input_data->x + part_offset ),
497 part_length, part_data,
498 part_data_size,
499 &output_part_length ) );
500
501 if( output_data && output_part_length )
502 {
503 memcpy( ( output_data + part_offset ), part_data,
504 output_part_length );
505 }
506
Paul Elliottd3f82412021-06-16 16:52:21 +0100507 output_length += output_part_length;
508 }
509 }
510 else
511 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100512 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100514 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100515 output_size, &output_length ) );
516 }
517
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100518 if( is_encrypt )
519 PSA_ASSERT( psa_aead_finish( &operation, final_data,
520 final_output_size,
521 &output_part_length,
522 tag_buffer, tag_length,
523 &tag_size ) );
524 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100525 {
Paul Elliott9961a662021-09-17 19:19:02 +0100526 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100527 final_output_size,
528 &output_part_length,
529 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100530 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100531 }
532
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100533 if( output_data && output_part_length )
534 memcpy( ( output_data + output_length ), final_data,
535 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100536
537 output_length += output_part_length;
538
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100539
540 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
541 * should be exact.*/
542 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100543 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100544 TEST_EQUAL( tag_length, tag_size );
545
546 if( output_data && tag_length )
547 memcpy( ( output_data + output_length ), tag_buffer,
548 tag_length );
549
550 output_length += tag_length;
551
552 TEST_EQUAL( output_length,
553 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
554 input_data->len ) );
555 TEST_ASSERT( output_length <=
556 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
557 }
558 else
559 {
560 TEST_EQUAL( output_length,
561 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
562 input_data->len ) );
563 TEST_ASSERT( output_length <=
564 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100565 }
566
Paul Elliottd3f82412021-06-16 16:52:21 +0100567
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100568 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100569 output_data, output_length );
570
Paul Elliottd3f82412021-06-16 16:52:21 +0100571
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100572 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100573
574exit:
575 psa_destroy_key( key );
576 psa_aead_abort( &operation );
577 mbedtls_free( output_data );
578 mbedtls_free( part_data );
579 mbedtls_free( final_data );
580 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581
582 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100583}
584
Gilles Peskinee59236f2018-01-27 23:32:46 +0100585/* END_HEADER */
586
587/* BEGIN_DEPENDENCIES
588 * depends_on:MBEDTLS_PSA_CRYPTO_C
589 * END_DEPENDENCIES
590 */
591
592/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200593void static_checks( )
594{
595 size_t max_truncated_mac_size =
596 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
597
598 /* Check that the length for a truncated MAC always fits in the algorithm
599 * encoding. The shifted mask is the maximum truncated value. The
600 * untruncated algorithm may be one byte larger. */
601 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
602}
603/* END_CASE */
604
605/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200606void import_with_policy( int type_arg,
607 int usage_arg, int alg_arg,
608 int expected_status_arg )
609{
610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
611 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200613 psa_key_type_t type = type_arg;
614 psa_key_usage_t usage = usage_arg;
615 psa_algorithm_t alg = alg_arg;
616 psa_status_t expected_status = expected_status_arg;
617 const uint8_t key_material[16] = {0};
618 psa_status_t status;
619
620 PSA_ASSERT( psa_crypto_init( ) );
621
622 psa_set_key_type( &attributes, type );
623 psa_set_key_usage_flags( &attributes, usage );
624 psa_set_key_algorithm( &attributes, alg );
625
626 status = psa_import_key( &attributes,
627 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200628 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200629 TEST_EQUAL( status, expected_status );
630 if( status != PSA_SUCCESS )
631 goto exit;
632
Ronald Cron5425a212020-08-04 14:58:35 +0200633 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200634 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
635 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
636 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200637 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200638
Ronald Cron5425a212020-08-04 14:58:35 +0200639 PSA_ASSERT( psa_destroy_key( key ) );
640 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641
642exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100643 /*
644 * Key attributes may have been returned by psa_get_key_attributes()
645 * thus reset them as required.
646 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200647 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100648
649 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200650 PSA_DONE( );
651}
652/* END_CASE */
653
654/* BEGIN_CASE */
655void import_with_data( data_t *data, int type_arg,
656 int attr_bits_arg,
657 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200658{
659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
660 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200662 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200663 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200664 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666
Gilles Peskine8817f612018-12-18 00:18:46 +0100667 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668
Gilles Peskine4747d192019-04-17 15:05:45 +0200669 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200670 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200671
Ronald Cron5425a212020-08-04 14:58:35 +0200672 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100673 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 if( status != PSA_SUCCESS )
675 goto exit;
676
Ronald Cron5425a212020-08-04 14:58:35 +0200677 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200678 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200679 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200680 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200681 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200682
Ronald Cron5425a212020-08-04 14:58:35 +0200683 PSA_ASSERT( psa_destroy_key( key ) );
684 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100685
686exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100687 /*
688 * Key attributes may have been returned by psa_get_key_attributes()
689 * thus reset them as required.
690 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200691 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100692
693 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200694 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100695}
696/* END_CASE */
697
698/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200699void import_large_key( int type_arg, int byte_size_arg,
700 int expected_status_arg )
701{
702 psa_key_type_t type = type_arg;
703 size_t byte_size = byte_size_arg;
704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
705 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200706 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200707 psa_status_t status;
708 uint8_t *buffer = NULL;
709 size_t buffer_size = byte_size + 1;
710 size_t n;
711
Steven Cooreman69967ce2021-01-18 18:01:08 +0100712 /* Skip the test case if the target running the test cannot
713 * accomodate large keys due to heap size constraints */
714 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200715 memset( buffer, 'K', byte_size );
716
717 PSA_ASSERT( psa_crypto_init( ) );
718
719 /* Try importing the key */
720 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
721 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200722 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100723 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200724 TEST_EQUAL( status, expected_status );
725
726 if( status == PSA_SUCCESS )
727 {
Ronald Cron5425a212020-08-04 14:58:35 +0200728 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200729 TEST_EQUAL( psa_get_key_type( &attributes ), type );
730 TEST_EQUAL( psa_get_key_bits( &attributes ),
731 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200732 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200733 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200734 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200735 for( n = 0; n < byte_size; n++ )
736 TEST_EQUAL( buffer[n], 'K' );
737 for( n = byte_size; n < buffer_size; n++ )
738 TEST_EQUAL( buffer[n], 0 );
739 }
740
741exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100742 /*
743 * Key attributes may have been returned by psa_get_key_attributes()
744 * thus reset them as required.
745 */
746 psa_reset_key_attributes( &attributes );
747
Ronald Cron5425a212020-08-04 14:58:35 +0200748 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200749 PSA_DONE( );
750 mbedtls_free( buffer );
751}
752/* END_CASE */
753
754/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200755void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
756{
Ronald Cron5425a212020-08-04 14:58:35 +0200757 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200758 size_t bits = bits_arg;
759 psa_status_t expected_status = expected_status_arg;
760 psa_status_t status;
761 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200762 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763 size_t buffer_size = /* Slight overapproximations */
764 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200765 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200766 unsigned char *p;
767 int ret;
768 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200769 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200770
Gilles Peskine8817f612018-12-18 00:18:46 +0100771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200772 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200773
774 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
775 bits, keypair ) ) >= 0 );
776 length = ret;
777
778 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200779 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200780 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100781 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200782
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200783 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200784 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200785
786exit:
787 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200788 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200789}
790/* END_CASE */
791
792/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300793void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300794 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200795 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100796 int expected_bits,
797 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200798 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100799 int canonical_input )
800{
Ronald Cron5425a212020-08-04 14:58:35 +0200801 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100802 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200803 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200804 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100806 unsigned char *exported = NULL;
807 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100809 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100810 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200812 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100813
Moran Pekercb088e72018-07-17 17:36:59 +0300814 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200815 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200817 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819
Gilles Peskine4747d192019-04-17 15:05:45 +0200820 psa_set_key_usage_flags( &attributes, usage_arg );
821 psa_set_key_algorithm( &attributes, alg );
822 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700823
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200825 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826
827 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200828 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200829 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
830 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200831 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100832
833 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200834 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100835 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100836
837 /* The exported length must be set by psa_export_key() to a value between 0
838 * and export_size. On errors, the exported length must be 0. */
839 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
840 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
841 TEST_ASSERT( exported_length <= export_size );
842
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200843 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200844 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200846 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100847 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100848 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200849 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850
Gilles Peskineea38a922021-02-13 00:05:16 +0100851 /* Run sanity checks on the exported key. For non-canonical inputs,
852 * this validates the canonical representations. For canonical inputs,
853 * this doesn't directly validate the implementation, but it still helps
854 * by cross-validating the test data with the sanity check code. */
855 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200856 goto exit;
857
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200859 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100860 else
861 {
Ronald Cron5425a212020-08-04 14:58:35 +0200862 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200863 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200864 &key2 ) );
865 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100866 reexported,
867 export_size,
868 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200869 ASSERT_COMPARE( exported, exported_length,
870 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200871 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100873 TEST_ASSERT( exported_length <=
874 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
875 psa_get_key_bits( &got_attributes ) ) );
876 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877
878destroy:
879 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200880 PSA_ASSERT( psa_destroy_key( key ) );
881 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100882
883exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100884 /*
885 * Key attributes may have been returned by psa_get_key_attributes()
886 * thus reset them as required.
887 */
888 psa_reset_key_attributes( &got_attributes );
889
itayzafrir3e02b3b2018-06-12 17:06:52 +0300890 mbedtls_free( exported );
891 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200892 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893}
894/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100895
Moran Pekerf709f4a2018-06-06 17:26:04 +0300896/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300897void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200898 int type_arg,
899 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100900 int export_size_delta,
901 int expected_export_status_arg,
902 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300903{
Ronald Cron5425a212020-08-04 14:58:35 +0200904 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300905 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200906 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200907 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300908 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300909 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100910 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100911 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300913
Gilles Peskine8817f612018-12-18 00:18:46 +0100914 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300915
Gilles Peskine4747d192019-04-17 15:05:45 +0200916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
917 psa_set_key_algorithm( &attributes, alg );
918 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300919
920 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200921 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300922
Gilles Peskine49c25912018-10-29 15:15:31 +0100923 /* Export the public key */
924 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200925 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200926 exported, export_size,
927 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100928 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100929 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100930 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200931 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100932 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200933 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200934 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100935 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100936 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100937 TEST_ASSERT( expected_public_key->len <=
938 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
939 TEST_ASSERT( expected_public_key->len <=
940 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100941 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
942 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100943 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300944
945exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100946 /*
947 * Key attributes may have been returned by psa_get_key_attributes()
948 * thus reset them as required.
949 */
950 psa_reset_key_attributes( &attributes );
951
itayzafrir3e02b3b2018-06-12 17:06:52 +0300952 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200953 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200954 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300955}
956/* END_CASE */
957
Gilles Peskine20035e32018-02-03 22:44:14 +0100958/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200959void import_and_exercise_key( data_t *data,
960 int type_arg,
961 int bits_arg,
962 int alg_arg )
963{
Ronald Cron5425a212020-08-04 14:58:35 +0200964 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200965 psa_key_type_t type = type_arg;
966 size_t bits = bits_arg;
967 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100968 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200969 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200970 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200971
Gilles Peskine8817f612018-12-18 00:18:46 +0100972 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200973
Gilles Peskine4747d192019-04-17 15:05:45 +0200974 psa_set_key_usage_flags( &attributes, usage );
975 psa_set_key_algorithm( &attributes, alg );
976 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200977
978 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200979 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980
981 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200982 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200983 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
984 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200985
986 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100987 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200988 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
Ronald Cron5425a212020-08-04 14:58:35 +0200990 PSA_ASSERT( psa_destroy_key( key ) );
991 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200992
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100994 /*
995 * Key attributes may have been returned by psa_get_key_attributes()
996 * thus reset them as required.
997 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200998 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100999
1000 psa_reset_key_attributes( &attributes );
1001 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001002 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001003}
1004/* END_CASE */
1005
1006/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001007void effective_key_attributes( int type_arg, int expected_type_arg,
1008 int bits_arg, int expected_bits_arg,
1009 int usage_arg, int expected_usage_arg,
1010 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001011{
Ronald Cron5425a212020-08-04 14:58:35 +02001012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001013 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001014 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001015 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001016 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001017 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001018 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001019 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001020 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001022
Gilles Peskine8817f612018-12-18 00:18:46 +01001023 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001024
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001025 psa_set_key_usage_flags( &attributes, usage );
1026 psa_set_key_algorithm( &attributes, alg );
1027 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001028 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001029
Ronald Cron5425a212020-08-04 14:58:35 +02001030 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001031 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001032
Ronald Cron5425a212020-08-04 14:58:35 +02001033 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001034 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1035 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1036 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1037 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
1039exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001040 /*
1041 * Key attributes may have been returned by psa_get_key_attributes()
1042 * thus reset them as required.
1043 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001044 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001045
1046 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001047 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048}
1049/* END_CASE */
1050
1051/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001052void check_key_policy( int type_arg, int bits_arg,
1053 int usage_arg, int alg_arg )
1054{
1055 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1056 usage_arg, usage_arg, alg_arg, alg_arg );
1057 goto exit;
1058}
1059/* END_CASE */
1060
1061/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001062void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001063{
1064 /* Test each valid way of initializing the object, except for `= {0}`, as
1065 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1066 * though it's OK by the C standard. We could test for this, but we'd need
1067 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001068 psa_key_attributes_t func = psa_key_attributes_init( );
1069 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1070 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001071
1072 memset( &zero, 0, sizeof( zero ) );
1073
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001074 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1075 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1076 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001077
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001078 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1079 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1080 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1081
1082 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1083 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1084 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1085
1086 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1087 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1088 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1089
1090 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1091 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1092 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001093}
1094/* END_CASE */
1095
1096/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001097void mac_key_policy( int policy_usage,
1098 int policy_alg,
1099 int key_type,
1100 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001101 int exercise_alg,
1102 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001103{
Ronald Cron5425a212020-08-04 14:58:35 +02001104 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001106 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001107 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001108 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001109 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001110
Gilles Peskine8817f612018-12-18 00:18:46 +01001111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001112
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001113 psa_set_key_usage_flags( &attributes, policy_usage );
1114 psa_set_key_algorithm( &attributes, policy_alg );
1115 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001116
Gilles Peskine049c7532019-05-15 20:22:09 +02001117 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001118 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001119
Ronald Cron5425a212020-08-04 14:58:35 +02001120 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001121 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001122 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001123 else
1124 TEST_EQUAL( status, expected_status );
1125
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001126 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001127
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001128 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001129 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001130 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001131 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001132 else
1133 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001134
1135exit:
1136 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001137 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001138 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139}
1140/* END_CASE */
1141
1142/* BEGIN_CASE */
1143void cipher_key_policy( int policy_usage,
1144 int policy_alg,
1145 int key_type,
1146 data_t *key_data,
1147 int exercise_alg )
1148{
Ronald Cron5425a212020-08-04 14:58:35 +02001149 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001151 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001152 psa_status_t status;
1153
Gilles Peskine8817f612018-12-18 00:18:46 +01001154 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001155
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001156 psa_set_key_usage_flags( &attributes, policy_usage );
1157 psa_set_key_algorithm( &attributes, policy_alg );
1158 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159
Gilles Peskine049c7532019-05-15 20:22:09 +02001160 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001161 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001162
Ronald Cron5425a212020-08-04 14:58:35 +02001163 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164 if( policy_alg == exercise_alg &&
1165 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001166 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001168 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169 psa_cipher_abort( &operation );
1170
Ronald Cron5425a212020-08-04 14:58:35 +02001171 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172 if( policy_alg == exercise_alg &&
1173 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001174 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001175 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001176 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177
1178exit:
1179 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001180 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001181 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182}
1183/* END_CASE */
1184
1185/* BEGIN_CASE */
1186void aead_key_policy( int policy_usage,
1187 int policy_alg,
1188 int key_type,
1189 data_t *key_data,
1190 int nonce_length_arg,
1191 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001192 int exercise_alg,
1193 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194{
Ronald Cron5425a212020-08-04 14:58:35 +02001195 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001197 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001198 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001199 unsigned char nonce[16] = {0};
1200 size_t nonce_length = nonce_length_arg;
1201 unsigned char tag[16];
1202 size_t tag_length = tag_length_arg;
1203 size_t output_length;
1204
1205 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1206 TEST_ASSERT( tag_length <= sizeof( tag ) );
1207
Gilles Peskine8817f612018-12-18 00:18:46 +01001208 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001210 psa_set_key_usage_flags( &attributes, policy_usage );
1211 psa_set_key_algorithm( &attributes, policy_alg );
1212 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213
Gilles Peskine049c7532019-05-15 20:22:09 +02001214 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001215 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001216
Ronald Cron5425a212020-08-04 14:58:35 +02001217 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218 nonce, nonce_length,
1219 NULL, 0,
1220 NULL, 0,
1221 tag, tag_length,
1222 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001223 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1224 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001226 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001227
1228 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001229 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 nonce, nonce_length,
1231 NULL, 0,
1232 tag, tag_length,
1233 NULL, 0,
1234 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001235 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1236 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1237 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001238 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001239 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001240 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001241
1242exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001243 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001244 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001245}
1246/* END_CASE */
1247
1248/* BEGIN_CASE */
1249void asymmetric_encryption_key_policy( int policy_usage,
1250 int policy_alg,
1251 int key_type,
1252 data_t *key_data,
1253 int exercise_alg )
1254{
Ronald Cron5425a212020-08-04 14:58:35 +02001255 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001257 psa_status_t status;
1258 size_t key_bits;
1259 size_t buffer_length;
1260 unsigned char *buffer = NULL;
1261 size_t output_length;
1262
Gilles Peskine8817f612018-12-18 00:18:46 +01001263 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001264
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001265 psa_set_key_usage_flags( &attributes, policy_usage );
1266 psa_set_key_algorithm( &attributes, policy_alg );
1267 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001268
Gilles Peskine049c7532019-05-15 20:22:09 +02001269 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001270 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001271
Ronald Cron5425a212020-08-04 14:58:35 +02001272 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001273 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001274 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1275 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001276 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277
Ronald Cron5425a212020-08-04 14:58:35 +02001278 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001279 NULL, 0,
1280 NULL, 0,
1281 buffer, buffer_length,
1282 &output_length );
1283 if( policy_alg == exercise_alg &&
1284 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001285 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001286 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001287 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001288
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001289 if( buffer_length != 0 )
1290 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001291 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001292 buffer, buffer_length,
1293 NULL, 0,
1294 buffer, buffer_length,
1295 &output_length );
1296 if( policy_alg == exercise_alg &&
1297 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001298 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001299 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001300 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001301
1302exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001303 /*
1304 * Key attributes may have been returned by psa_get_key_attributes()
1305 * thus reset them as required.
1306 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001307 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001308
1309 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001310 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001311 mbedtls_free( buffer );
1312}
1313/* END_CASE */
1314
1315/* BEGIN_CASE */
1316void asymmetric_signature_key_policy( int policy_usage,
1317 int policy_alg,
1318 int key_type,
1319 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001320 int exercise_alg,
1321 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001322{
Ronald Cron5425a212020-08-04 14:58:35 +02001323 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001325 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001326 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1327 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1328 * compatible with the policy and `payload_length_arg` is supposed to be
1329 * a valid input length to sign. If `payload_length_arg <= 0`,
1330 * `exercise_alg` is supposed to be forbidden by the policy. */
1331 int compatible_alg = payload_length_arg > 0;
1332 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001333 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001334 size_t signature_length;
1335
Gilles Peskine8817f612018-12-18 00:18:46 +01001336 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001337
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001338 psa_set_key_usage_flags( &attributes, policy_usage );
1339 psa_set_key_algorithm( &attributes, policy_alg );
1340 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001341
Gilles Peskine049c7532019-05-15 20:22:09 +02001342 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001343 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001344
Ronald Cron5425a212020-08-04 14:58:35 +02001345 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001346 payload, payload_length,
1347 signature, sizeof( signature ),
1348 &signature_length );
1349 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001350 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001351 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001352 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001353
1354 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001355 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001356 payload, payload_length,
1357 signature, sizeof( signature ) );
1358 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001359 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001360 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001361 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001362
1363exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001364 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001365 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001366}
1367/* END_CASE */
1368
Janos Follathba3fab92019-06-11 14:50:16 +01001369/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001370void derive_key_policy( int policy_usage,
1371 int policy_alg,
1372 int key_type,
1373 data_t *key_data,
1374 int exercise_alg )
1375{
Ronald Cron5425a212020-08-04 14:58:35 +02001376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001378 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001379 psa_status_t status;
1380
Gilles Peskine8817f612018-12-18 00:18:46 +01001381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001382
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001383 psa_set_key_usage_flags( &attributes, policy_usage );
1384 psa_set_key_algorithm( &attributes, policy_alg );
1385 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001386
Gilles Peskine049c7532019-05-15 20:22:09 +02001387 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001388 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001389
Janos Follathba3fab92019-06-11 14:50:16 +01001390 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1391
1392 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1393 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001394 {
Janos Follathba3fab92019-06-11 14:50:16 +01001395 PSA_ASSERT( psa_key_derivation_input_bytes(
1396 &operation,
1397 PSA_KEY_DERIVATION_INPUT_SEED,
1398 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001399 }
Janos Follathba3fab92019-06-11 14:50:16 +01001400
1401 status = psa_key_derivation_input_key( &operation,
1402 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001403 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001404
Gilles Peskineea0fb492018-07-12 17:17:20 +02001405 if( policy_alg == exercise_alg &&
1406 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001407 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001408 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001409 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001410
1411exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001412 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001413 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001414 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001415}
1416/* END_CASE */
1417
1418/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001419void agreement_key_policy( int policy_usage,
1420 int policy_alg,
1421 int key_type_arg,
1422 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001423 int exercise_alg,
1424 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001425{
Ronald Cron5425a212020-08-04 14:58:35 +02001426 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001428 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001429 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001430 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001431 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001432
Gilles Peskine8817f612018-12-18 00:18:46 +01001433 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001434
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001435 psa_set_key_usage_flags( &attributes, policy_usage );
1436 psa_set_key_algorithm( &attributes, policy_alg );
1437 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001438
Gilles Peskine049c7532019-05-15 20:22:09 +02001439 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001440 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001442 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001443 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001444
Steven Cooremance48e852020-10-05 16:02:45 +02001445 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001446
1447exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001448 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001449 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001450 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001451}
1452/* END_CASE */
1453
1454/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001455void key_policy_alg2( int key_type_arg, data_t *key_data,
1456 int usage_arg, int alg_arg, int alg2_arg )
1457{
Ronald Cron5425a212020-08-04 14:58:35 +02001458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001459 psa_key_type_t key_type = key_type_arg;
1460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1461 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1462 psa_key_usage_t usage = usage_arg;
1463 psa_algorithm_t alg = alg_arg;
1464 psa_algorithm_t alg2 = alg2_arg;
1465
1466 PSA_ASSERT( psa_crypto_init( ) );
1467
1468 psa_set_key_usage_flags( &attributes, usage );
1469 psa_set_key_algorithm( &attributes, alg );
1470 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1471 psa_set_key_type( &attributes, key_type );
1472 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001473 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001474
Ronald Cron5425a212020-08-04 14:58:35 +02001475 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001476 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1477 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1478 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1479
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001480 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001481 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001482 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001483 goto exit;
1484
1485exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001486 /*
1487 * Key attributes may have been returned by psa_get_key_attributes()
1488 * thus reset them as required.
1489 */
1490 psa_reset_key_attributes( &got_attributes );
1491
Ronald Cron5425a212020-08-04 14:58:35 +02001492 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001493 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001494}
1495/* END_CASE */
1496
1497/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001498void raw_agreement_key_policy( int policy_usage,
1499 int policy_alg,
1500 int key_type_arg,
1501 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001502 int exercise_alg,
1503 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001504{
Ronald Cron5425a212020-08-04 14:58:35 +02001505 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001507 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001508 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001509 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001510 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001511
1512 PSA_ASSERT( psa_crypto_init( ) );
1513
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001514 psa_set_key_usage_flags( &attributes, policy_usage );
1515 psa_set_key_algorithm( &attributes, policy_alg );
1516 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517
Gilles Peskine049c7532019-05-15 20:22:09 +02001518 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001519 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001520
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001521 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001522
Steven Cooremance48e852020-10-05 16:02:45 +02001523 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001524
1525exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001526 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001527 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001528 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001529}
1530/* END_CASE */
1531
1532/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001533void copy_success( int source_usage_arg,
1534 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001535 int type_arg, data_t *material,
1536 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001537 int target_usage_arg,
1538 int target_alg_arg, int target_alg2_arg,
1539 int expected_usage_arg,
1540 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001541{
Gilles Peskineca25db92019-04-19 11:43:08 +02001542 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1543 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001544 psa_key_usage_t expected_usage = expected_usage_arg;
1545 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001546 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001547 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1548 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001549 uint8_t *export_buffer = NULL;
1550
Gilles Peskine57ab7212019-01-28 13:03:09 +01001551 PSA_ASSERT( psa_crypto_init( ) );
1552
Gilles Peskineca25db92019-04-19 11:43:08 +02001553 /* Prepare the source key. */
1554 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1555 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001556 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001557 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001558 PSA_ASSERT( psa_import_key( &source_attributes,
1559 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001560 &source_key ) );
1561 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001562
Gilles Peskineca25db92019-04-19 11:43:08 +02001563 /* Prepare the target attributes. */
1564 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001565 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001567 /* Set volatile lifetime to reset the key identifier to 0. */
1568 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1569 }
1570
Gilles Peskineca25db92019-04-19 11:43:08 +02001571 if( target_usage_arg != -1 )
1572 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1573 if( target_alg_arg != -1 )
1574 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001575 if( target_alg2_arg != -1 )
1576 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001577
1578 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001579 PSA_ASSERT( psa_copy_key( source_key,
1580 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001581
1582 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001583 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001584
1585 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001586 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001587 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1588 psa_get_key_type( &target_attributes ) );
1589 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1590 psa_get_key_bits( &target_attributes ) );
1591 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1592 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001593 TEST_EQUAL( expected_alg2,
1594 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001595 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1596 {
1597 size_t length;
1598 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001599 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001600 material->len, &length ) );
1601 ASSERT_COMPARE( material->x, material->len,
1602 export_buffer, length );
1603 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001604
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001605 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001606 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001607 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001608 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001609
Ronald Cron5425a212020-08-04 14:58:35 +02001610 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001611
1612exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001613 /*
1614 * Source and target key attributes may have been returned by
1615 * psa_get_key_attributes() thus reset them as required.
1616 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001617 psa_reset_key_attributes( &source_attributes );
1618 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001619
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001620 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001621 mbedtls_free( export_buffer );
1622}
1623/* END_CASE */
1624
1625/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001626void copy_fail( int source_usage_arg,
1627 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001628 int type_arg, data_t *material,
1629 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001630 int target_usage_arg,
1631 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001632 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001633 int expected_status_arg )
1634{
1635 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1636 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001637 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1638 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001639 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001640
1641 PSA_ASSERT( psa_crypto_init( ) );
1642
1643 /* Prepare the source key. */
1644 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1645 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001646 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001647 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001648 PSA_ASSERT( psa_import_key( &source_attributes,
1649 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001650 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001651
1652 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001653 psa_set_key_id( &target_attributes, key_id );
1654 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001655 psa_set_key_type( &target_attributes, target_type_arg );
1656 psa_set_key_bits( &target_attributes, target_bits_arg );
1657 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1658 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001659 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001660
1661 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001662 TEST_EQUAL( psa_copy_key( source_key,
1663 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001664 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001665
Ronald Cron5425a212020-08-04 14:58:35 +02001666 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001667
Gilles Peskine4a644642019-05-03 17:14:08 +02001668exit:
1669 psa_reset_key_attributes( &source_attributes );
1670 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001671 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001672}
1673/* END_CASE */
1674
1675/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001676void hash_operation_init( )
1677{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001678 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001679 /* Test each valid way of initializing the object, except for `= {0}`, as
1680 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1681 * though it's OK by the C standard. We could test for this, but we'd need
1682 * to supress the Clang warning for the test. */
1683 psa_hash_operation_t func = psa_hash_operation_init( );
1684 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1685 psa_hash_operation_t zero;
1686
1687 memset( &zero, 0, sizeof( zero ) );
1688
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001689 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001690 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1691 PSA_ERROR_BAD_STATE );
1692 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1693 PSA_ERROR_BAD_STATE );
1694 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1695 PSA_ERROR_BAD_STATE );
1696
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001697 /* A default hash operation should be abortable without error. */
1698 PSA_ASSERT( psa_hash_abort( &func ) );
1699 PSA_ASSERT( psa_hash_abort( &init ) );
1700 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001701}
1702/* END_CASE */
1703
1704/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001705void hash_setup( int alg_arg,
1706 int expected_status_arg )
1707{
1708 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001709 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001710 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001711 psa_status_t status;
1712
Gilles Peskine8817f612018-12-18 00:18:46 +01001713 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001714
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001715 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001716 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001717
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001718 /* Whether setup succeeded or failed, abort must succeed. */
1719 PSA_ASSERT( psa_hash_abort( &operation ) );
1720
1721 /* If setup failed, reproduce the failure, so as to
1722 * test the resulting state of the operation object. */
1723 if( status != PSA_SUCCESS )
1724 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1725
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001726 /* Now the operation object should be reusable. */
1727#if defined(KNOWN_SUPPORTED_HASH_ALG)
1728 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1729 PSA_ASSERT( psa_hash_abort( &operation ) );
1730#endif
1731
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001732exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001733 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001734}
1735/* END_CASE */
1736
1737/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001738void hash_compute_fail( int alg_arg, data_t *input,
1739 int output_size_arg, int expected_status_arg )
1740{
1741 psa_algorithm_t alg = alg_arg;
1742 uint8_t *output = NULL;
1743 size_t output_size = output_size_arg;
1744 size_t output_length = INVALID_EXPORT_LENGTH;
1745 psa_status_t expected_status = expected_status_arg;
1746 psa_status_t status;
1747
1748 ASSERT_ALLOC( output, output_size );
1749
1750 PSA_ASSERT( psa_crypto_init( ) );
1751
1752 status = psa_hash_compute( alg, input->x, input->len,
1753 output, output_size, &output_length );
1754 TEST_EQUAL( status, expected_status );
1755 TEST_ASSERT( output_length <= output_size );
1756
1757exit:
1758 mbedtls_free( output );
1759 PSA_DONE( );
1760}
1761/* END_CASE */
1762
1763/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001764void hash_compare_fail( int alg_arg, data_t *input,
1765 data_t *reference_hash,
1766 int expected_status_arg )
1767{
1768 psa_algorithm_t alg = alg_arg;
1769 psa_status_t expected_status = expected_status_arg;
1770 psa_status_t status;
1771
1772 PSA_ASSERT( psa_crypto_init( ) );
1773
1774 status = psa_hash_compare( alg, input->x, input->len,
1775 reference_hash->x, reference_hash->len );
1776 TEST_EQUAL( status, expected_status );
1777
1778exit:
1779 PSA_DONE( );
1780}
1781/* END_CASE */
1782
1783/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001784void hash_compute_compare( int alg_arg, data_t *input,
1785 data_t *expected_output )
1786{
1787 psa_algorithm_t alg = alg_arg;
1788 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1789 size_t output_length = INVALID_EXPORT_LENGTH;
1790 size_t i;
1791
1792 PSA_ASSERT( psa_crypto_init( ) );
1793
1794 /* Compute with tight buffer */
1795 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001796 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001797 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001798 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001799 ASSERT_COMPARE( output, output_length,
1800 expected_output->x, expected_output->len );
1801
1802 /* Compute with larger buffer */
1803 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1804 output, sizeof( output ),
1805 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001806 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001807 ASSERT_COMPARE( output, output_length,
1808 expected_output->x, expected_output->len );
1809
1810 /* Compare with correct hash */
1811 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1812 output, output_length ) );
1813
1814 /* Compare with trailing garbage */
1815 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1816 output, output_length + 1 ),
1817 PSA_ERROR_INVALID_SIGNATURE );
1818
1819 /* Compare with truncated hash */
1820 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1821 output, output_length - 1 ),
1822 PSA_ERROR_INVALID_SIGNATURE );
1823
1824 /* Compare with corrupted value */
1825 for( i = 0; i < output_length; i++ )
1826 {
Chris Jones9634bb12021-01-20 15:56:42 +00001827 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001828 output[i] ^= 1;
1829 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1830 output, output_length ),
1831 PSA_ERROR_INVALID_SIGNATURE );
1832 output[i] ^= 1;
1833 }
1834
1835exit:
1836 PSA_DONE( );
1837}
1838/* END_CASE */
1839
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001840/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001841void hash_bad_order( )
1842{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001843 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001844 unsigned char input[] = "";
1845 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001846 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001847 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1848 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1849 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001850 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001851 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001852 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001853
Gilles Peskine8817f612018-12-18 00:18:46 +01001854 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001855
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001856 /* Call setup twice in a row. */
1857 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1858 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1859 PSA_ERROR_BAD_STATE );
1860 PSA_ASSERT( psa_hash_abort( &operation ) );
1861
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001862 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001863 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001864 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001865 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001866
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001867 /* Call update after finish. */
1868 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1869 PSA_ASSERT( psa_hash_finish( &operation,
1870 hash, sizeof( hash ), &hash_len ) );
1871 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001872 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001873 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001874
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001875 /* Call verify without calling setup beforehand. */
1876 TEST_EQUAL( psa_hash_verify( &operation,
1877 valid_hash, sizeof( valid_hash ) ),
1878 PSA_ERROR_BAD_STATE );
1879 PSA_ASSERT( psa_hash_abort( &operation ) );
1880
1881 /* Call verify after finish. */
1882 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1883 PSA_ASSERT( psa_hash_finish( &operation,
1884 hash, sizeof( hash ), &hash_len ) );
1885 TEST_EQUAL( psa_hash_verify( &operation,
1886 valid_hash, sizeof( valid_hash ) ),
1887 PSA_ERROR_BAD_STATE );
1888 PSA_ASSERT( psa_hash_abort( &operation ) );
1889
1890 /* Call verify twice in a row. */
1891 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1892 PSA_ASSERT( psa_hash_verify( &operation,
1893 valid_hash, sizeof( valid_hash ) ) );
1894 TEST_EQUAL( psa_hash_verify( &operation,
1895 valid_hash, sizeof( valid_hash ) ),
1896 PSA_ERROR_BAD_STATE );
1897 PSA_ASSERT( psa_hash_abort( &operation ) );
1898
1899 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001900 TEST_EQUAL( psa_hash_finish( &operation,
1901 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001902 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001903 PSA_ASSERT( psa_hash_abort( &operation ) );
1904
1905 /* Call finish twice in a row. */
1906 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1907 PSA_ASSERT( psa_hash_finish( &operation,
1908 hash, sizeof( hash ), &hash_len ) );
1909 TEST_EQUAL( psa_hash_finish( &operation,
1910 hash, sizeof( hash ), &hash_len ),
1911 PSA_ERROR_BAD_STATE );
1912 PSA_ASSERT( psa_hash_abort( &operation ) );
1913
1914 /* Call finish after calling verify. */
1915 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1916 PSA_ASSERT( psa_hash_verify( &operation,
1917 valid_hash, sizeof( valid_hash ) ) );
1918 TEST_EQUAL( psa_hash_finish( &operation,
1919 hash, sizeof( hash ), &hash_len ),
1920 PSA_ERROR_BAD_STATE );
1921 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001922
1923exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001924 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001925}
1926/* END_CASE */
1927
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001928/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001929void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001930{
1931 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001932 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1933 * appended to it */
1934 unsigned char hash[] = {
1935 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1936 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1937 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001938 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001939 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001940
Gilles Peskine8817f612018-12-18 00:18:46 +01001941 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001942
itayzafrir27e69452018-11-01 14:26:34 +02001943 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001944 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001945 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001946 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001947
itayzafrir27e69452018-11-01 14:26:34 +02001948 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001950 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001951 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001952
itayzafrir27e69452018-11-01 14:26:34 +02001953 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001954 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001955 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001956 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001957
itayzafrirec93d302018-10-18 18:01:10 +03001958exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001959 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001960}
1961/* END_CASE */
1962
Ronald Cronee414c72021-03-18 18:50:08 +01001963/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001964void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001965{
1966 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001967 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001968 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001969 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001970 size_t hash_len;
1971
Gilles Peskine8817f612018-12-18 00:18:46 +01001972 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001973
itayzafrir58028322018-10-25 10:22:01 +03001974 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001975 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001976 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001977 hash, expected_size - 1, &hash_len ),
1978 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001979
1980exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001981 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001982}
1983/* END_CASE */
1984
Ronald Cronee414c72021-03-18 18:50:08 +01001985/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001986void hash_clone_source_state( )
1987{
1988 psa_algorithm_t alg = PSA_ALG_SHA_256;
1989 unsigned char hash[PSA_HASH_MAX_SIZE];
1990 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1991 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1992 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1993 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1994 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1995 size_t hash_len;
1996
1997 PSA_ASSERT( psa_crypto_init( ) );
1998 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1999
2000 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2001 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2002 PSA_ASSERT( psa_hash_finish( &op_finished,
2003 hash, sizeof( hash ), &hash_len ) );
2004 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2005 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2006
2007 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2008 PSA_ERROR_BAD_STATE );
2009
2010 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2011 PSA_ASSERT( psa_hash_finish( &op_init,
2012 hash, sizeof( hash ), &hash_len ) );
2013 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2014 PSA_ASSERT( psa_hash_finish( &op_finished,
2015 hash, sizeof( hash ), &hash_len ) );
2016 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2017 PSA_ASSERT( psa_hash_finish( &op_aborted,
2018 hash, sizeof( hash ), &hash_len ) );
2019
2020exit:
2021 psa_hash_abort( &op_source );
2022 psa_hash_abort( &op_init );
2023 psa_hash_abort( &op_setup );
2024 psa_hash_abort( &op_finished );
2025 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002026 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002027}
2028/* END_CASE */
2029
Ronald Cronee414c72021-03-18 18:50:08 +01002030/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002031void hash_clone_target_state( )
2032{
2033 psa_algorithm_t alg = PSA_ALG_SHA_256;
2034 unsigned char hash[PSA_HASH_MAX_SIZE];
2035 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2036 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2037 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2038 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2039 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2040 size_t hash_len;
2041
2042 PSA_ASSERT( psa_crypto_init( ) );
2043
2044 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2045 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2046 PSA_ASSERT( psa_hash_finish( &op_finished,
2047 hash, sizeof( hash ), &hash_len ) );
2048 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2049 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2050
2051 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2052 PSA_ASSERT( psa_hash_finish( &op_target,
2053 hash, sizeof( hash ), &hash_len ) );
2054
2055 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2056 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2057 PSA_ERROR_BAD_STATE );
2058 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2059 PSA_ERROR_BAD_STATE );
2060
2061exit:
2062 psa_hash_abort( &op_target );
2063 psa_hash_abort( &op_init );
2064 psa_hash_abort( &op_setup );
2065 psa_hash_abort( &op_finished );
2066 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002067 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002068}
2069/* END_CASE */
2070
itayzafrir58028322018-10-25 10:22:01 +03002071/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002072void mac_operation_init( )
2073{
Jaeden Amero252ef282019-02-15 14:05:35 +00002074 const uint8_t input[1] = { 0 };
2075
Jaeden Amero769ce272019-01-04 11:48:03 +00002076 /* Test each valid way of initializing the object, except for `= {0}`, as
2077 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2078 * though it's OK by the C standard. We could test for this, but we'd need
2079 * to supress the Clang warning for the test. */
2080 psa_mac_operation_t func = psa_mac_operation_init( );
2081 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2082 psa_mac_operation_t zero;
2083
2084 memset( &zero, 0, sizeof( zero ) );
2085
Jaeden Amero252ef282019-02-15 14:05:35 +00002086 /* A freshly-initialized MAC operation should not be usable. */
2087 TEST_EQUAL( psa_mac_update( &func,
2088 input, sizeof( input ) ),
2089 PSA_ERROR_BAD_STATE );
2090 TEST_EQUAL( psa_mac_update( &init,
2091 input, sizeof( input ) ),
2092 PSA_ERROR_BAD_STATE );
2093 TEST_EQUAL( psa_mac_update( &zero,
2094 input, sizeof( input ) ),
2095 PSA_ERROR_BAD_STATE );
2096
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002097 /* A default MAC operation should be abortable without error. */
2098 PSA_ASSERT( psa_mac_abort( &func ) );
2099 PSA_ASSERT( psa_mac_abort( &init ) );
2100 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002101}
2102/* END_CASE */
2103
2104/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105void mac_setup( int key_type_arg,
2106 data_t *key,
2107 int alg_arg,
2108 int expected_status_arg )
2109{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002110 psa_key_type_t key_type = key_type_arg;
2111 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002112 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002113 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002114 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2115#if defined(KNOWN_SUPPORTED_MAC_ALG)
2116 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2117#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002118
Gilles Peskine8817f612018-12-18 00:18:46 +01002119 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002120
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002121 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2122 &operation, &status ) )
2123 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002124 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002125
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002126 /* The operation object should be reusable. */
2127#if defined(KNOWN_SUPPORTED_MAC_ALG)
2128 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2129 smoke_test_key_data,
2130 sizeof( smoke_test_key_data ),
2131 KNOWN_SUPPORTED_MAC_ALG,
2132 &operation, &status ) )
2133 goto exit;
2134 TEST_EQUAL( status, PSA_SUCCESS );
2135#endif
2136
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002137exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002138 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002139}
2140/* END_CASE */
2141
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002142/* 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 +00002143void mac_bad_order( )
2144{
Ronald Cron5425a212020-08-04 14:58:35 +02002145 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002146 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2147 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002148 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002149 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2150 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2151 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002153 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2154 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2155 size_t sign_mac_length = 0;
2156 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2157 const uint8_t verify_mac[] = {
2158 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2159 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2160 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2161
2162 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002163 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002164 psa_set_key_algorithm( &attributes, alg );
2165 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002166
Ronald Cron5425a212020-08-04 14:58:35 +02002167 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2168 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002169
Jaeden Amero252ef282019-02-15 14:05:35 +00002170 /* Call update without calling setup beforehand. */
2171 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2172 PSA_ERROR_BAD_STATE );
2173 PSA_ASSERT( psa_mac_abort( &operation ) );
2174
2175 /* Call sign finish without calling setup beforehand. */
2176 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2177 &sign_mac_length),
2178 PSA_ERROR_BAD_STATE );
2179 PSA_ASSERT( psa_mac_abort( &operation ) );
2180
2181 /* Call verify finish without calling setup beforehand. */
2182 TEST_EQUAL( psa_mac_verify_finish( &operation,
2183 verify_mac, sizeof( verify_mac ) ),
2184 PSA_ERROR_BAD_STATE );
2185 PSA_ASSERT( psa_mac_abort( &operation ) );
2186
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002187 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002188 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2189 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002190 PSA_ERROR_BAD_STATE );
2191 PSA_ASSERT( psa_mac_abort( &operation ) );
2192
Jaeden Amero252ef282019-02-15 14:05:35 +00002193 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002194 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002195 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2196 PSA_ASSERT( psa_mac_sign_finish( &operation,
2197 sign_mac, sizeof( sign_mac ),
2198 &sign_mac_length ) );
2199 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2200 PSA_ERROR_BAD_STATE );
2201 PSA_ASSERT( psa_mac_abort( &operation ) );
2202
2203 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002204 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002205 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2206 PSA_ASSERT( psa_mac_verify_finish( &operation,
2207 verify_mac, sizeof( verify_mac ) ) );
2208 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2209 PSA_ERROR_BAD_STATE );
2210 PSA_ASSERT( psa_mac_abort( &operation ) );
2211
2212 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002213 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002214 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2215 PSA_ASSERT( psa_mac_sign_finish( &operation,
2216 sign_mac, sizeof( sign_mac ),
2217 &sign_mac_length ) );
2218 TEST_EQUAL( psa_mac_sign_finish( &operation,
2219 sign_mac, sizeof( sign_mac ),
2220 &sign_mac_length ),
2221 PSA_ERROR_BAD_STATE );
2222 PSA_ASSERT( psa_mac_abort( &operation ) );
2223
2224 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002225 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002226 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2227 PSA_ASSERT( psa_mac_verify_finish( &operation,
2228 verify_mac, sizeof( verify_mac ) ) );
2229 TEST_EQUAL( psa_mac_verify_finish( &operation,
2230 verify_mac, sizeof( verify_mac ) ),
2231 PSA_ERROR_BAD_STATE );
2232 PSA_ASSERT( psa_mac_abort( &operation ) );
2233
2234 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002235 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002236 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2237 TEST_EQUAL( psa_mac_verify_finish( &operation,
2238 verify_mac, sizeof( verify_mac ) ),
2239 PSA_ERROR_BAD_STATE );
2240 PSA_ASSERT( psa_mac_abort( &operation ) );
2241
2242 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002243 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002244 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2245 TEST_EQUAL( psa_mac_sign_finish( &operation,
2246 sign_mac, sizeof( sign_mac ),
2247 &sign_mac_length ),
2248 PSA_ERROR_BAD_STATE );
2249 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002250
Ronald Cron5425a212020-08-04 14:58:35 +02002251 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002252
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002253exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002254 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002255}
2256/* END_CASE */
2257
2258/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002259void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002260 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002261 int alg_arg,
2262 data_t *input,
2263 data_t *expected_mac )
2264{
Ronald Cron5425a212020-08-04 14:58:35 +02002265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002266 psa_key_type_t key_type = key_type_arg;
2267 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002268 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002270 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002271 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002272 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002273 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002274 const size_t output_sizes_to_test[] = {
2275 0,
2276 1,
2277 expected_mac->len - 1,
2278 expected_mac->len,
2279 expected_mac->len + 1,
2280 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002281
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002282 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002283 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002284 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002285
Gilles Peskine8817f612018-12-18 00:18:46 +01002286 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002287
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002288 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_set_key_algorithm( &attributes, alg );
2290 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002291
Ronald Cron5425a212020-08-04 14:58:35 +02002292 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2293 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002294
Gilles Peskine8b356b52020-08-25 23:44:59 +02002295 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2296 {
2297 const size_t output_size = output_sizes_to_test[i];
2298 psa_status_t expected_status =
2299 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2300 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002301
Chris Jones9634bb12021-01-20 15:56:42 +00002302 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002303 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002304
Gilles Peskine8b356b52020-08-25 23:44:59 +02002305 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002306 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002307 PSA_ASSERT( psa_mac_update( &operation,
2308 input->x, input->len ) );
2309 TEST_EQUAL( psa_mac_sign_finish( &operation,
2310 actual_mac, output_size,
2311 &mac_length ),
2312 expected_status );
2313 PSA_ASSERT( psa_mac_abort( &operation ) );
2314
2315 if( expected_status == PSA_SUCCESS )
2316 {
2317 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2318 actual_mac, mac_length );
2319 }
2320 mbedtls_free( actual_mac );
2321 actual_mac = NULL;
2322 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002323
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002324exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002325 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002326 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002327 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002328 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002329}
2330/* END_CASE */
2331
2332/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002333void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002334 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002335 int alg_arg,
2336 data_t *input,
2337 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002338{
Ronald Cron5425a212020-08-04 14:58:35 +02002339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002340 psa_key_type_t key_type = key_type_arg;
2341 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002342 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002344 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002345
Gilles Peskine69c12672018-06-28 00:07:19 +02002346 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2347
Gilles Peskine8817f612018-12-18 00:18:46 +01002348 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002349
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002350 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002351 psa_set_key_algorithm( &attributes, alg );
2352 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002353
Ronald Cron5425a212020-08-04 14:58:35 +02002354 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2355 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002356
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002357 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002358 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002359 PSA_ASSERT( psa_mac_update( &operation,
2360 input->x, input->len ) );
2361 PSA_ASSERT( psa_mac_verify_finish( &operation,
2362 expected_mac->x,
2363 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002364
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002365 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002366 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002367 PSA_ASSERT( psa_mac_update( &operation,
2368 input->x, input->len ) );
2369 TEST_EQUAL( psa_mac_verify_finish( &operation,
2370 expected_mac->x,
2371 expected_mac->len - 1 ),
2372 PSA_ERROR_INVALID_SIGNATURE );
2373
2374 /* Test a MAC that's too long. */
2375 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2376 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002377 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002378 PSA_ASSERT( psa_mac_update( &operation,
2379 input->x, input->len ) );
2380 TEST_EQUAL( psa_mac_verify_finish( &operation,
2381 perturbed_mac,
2382 expected_mac->len + 1 ),
2383 PSA_ERROR_INVALID_SIGNATURE );
2384
2385 /* Test changing one byte. */
2386 for( size_t i = 0; i < expected_mac->len; i++ )
2387 {
Chris Jones9634bb12021-01-20 15:56:42 +00002388 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002389 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002390 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002391 PSA_ASSERT( psa_mac_update( &operation,
2392 input->x, input->len ) );
2393 TEST_EQUAL( psa_mac_verify_finish( &operation,
2394 perturbed_mac,
2395 expected_mac->len ),
2396 PSA_ERROR_INVALID_SIGNATURE );
2397 perturbed_mac[i] ^= 1;
2398 }
2399
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002401 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002402 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002403 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002404 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002405}
2406/* END_CASE */
2407
2408/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002409void cipher_operation_init( )
2410{
Jaeden Ameroab439972019-02-15 14:12:05 +00002411 const uint8_t input[1] = { 0 };
2412 unsigned char output[1] = { 0 };
2413 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002414 /* Test each valid way of initializing the object, except for `= {0}`, as
2415 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2416 * though it's OK by the C standard. We could test for this, but we'd need
2417 * to supress the Clang warning for the test. */
2418 psa_cipher_operation_t func = psa_cipher_operation_init( );
2419 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2420 psa_cipher_operation_t zero;
2421
2422 memset( &zero, 0, sizeof( zero ) );
2423
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 /* A freshly-initialized cipher operation should not be usable. */
2425 TEST_EQUAL( psa_cipher_update( &func,
2426 input, sizeof( input ),
2427 output, sizeof( output ),
2428 &output_length ),
2429 PSA_ERROR_BAD_STATE );
2430 TEST_EQUAL( psa_cipher_update( &init,
2431 input, sizeof( input ),
2432 output, sizeof( output ),
2433 &output_length ),
2434 PSA_ERROR_BAD_STATE );
2435 TEST_EQUAL( psa_cipher_update( &zero,
2436 input, sizeof( input ),
2437 output, sizeof( output ),
2438 &output_length ),
2439 PSA_ERROR_BAD_STATE );
2440
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002441 /* A default cipher operation should be abortable without error. */
2442 PSA_ASSERT( psa_cipher_abort( &func ) );
2443 PSA_ASSERT( psa_cipher_abort( &init ) );
2444 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002445}
2446/* END_CASE */
2447
2448/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449void cipher_setup( int key_type_arg,
2450 data_t *key,
2451 int alg_arg,
2452 int expected_status_arg )
2453{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002454 psa_key_type_t key_type = key_type_arg;
2455 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002456 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002457 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002458 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002459#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002460 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2461#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002462
Gilles Peskine8817f612018-12-18 00:18:46 +01002463 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002464
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002465 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2466 &operation, &status ) )
2467 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002468 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002469
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002470 /* The operation object should be reusable. */
2471#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2472 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2473 smoke_test_key_data,
2474 sizeof( smoke_test_key_data ),
2475 KNOWN_SUPPORTED_CIPHER_ALG,
2476 &operation, &status ) )
2477 goto exit;
2478 TEST_EQUAL( status, PSA_SUCCESS );
2479#endif
2480
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002481exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002482 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002483 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002484}
2485/* END_CASE */
2486
Ronald Cronee414c72021-03-18 18:50:08 +01002487/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002488void cipher_bad_order( )
2489{
Ronald Cron5425a212020-08-04 14:58:35 +02002490 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002491 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2492 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002494 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002495 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002496 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002497 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2498 0xaa, 0xaa, 0xaa, 0xaa };
2499 const uint8_t text[] = {
2500 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2501 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002502 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002503 size_t length = 0;
2504
2505 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002506 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2507 psa_set_key_algorithm( &attributes, alg );
2508 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002509 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2510 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002511
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002512 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002513 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2514 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002515 PSA_ERROR_BAD_STATE );
2516 PSA_ASSERT( psa_cipher_abort( &operation ) );
2517
2518 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002519 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2520 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002521 PSA_ERROR_BAD_STATE );
2522 PSA_ASSERT( psa_cipher_abort( &operation ) );
2523
Jaeden Ameroab439972019-02-15 14:12:05 +00002524 /* Generate an IV without calling setup beforehand. */
2525 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2526 buffer, sizeof( buffer ),
2527 &length ),
2528 PSA_ERROR_BAD_STATE );
2529 PSA_ASSERT( psa_cipher_abort( &operation ) );
2530
2531 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002532 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002533 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2534 buffer, sizeof( buffer ),
2535 &length ) );
2536 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2537 buffer, sizeof( buffer ),
2538 &length ),
2539 PSA_ERROR_BAD_STATE );
2540 PSA_ASSERT( psa_cipher_abort( &operation ) );
2541
2542 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002543 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002544 PSA_ASSERT( psa_cipher_set_iv( &operation,
2545 iv, sizeof( iv ) ) );
2546 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2547 buffer, sizeof( buffer ),
2548 &length ),
2549 PSA_ERROR_BAD_STATE );
2550 PSA_ASSERT( psa_cipher_abort( &operation ) );
2551
2552 /* Set an IV without calling setup beforehand. */
2553 TEST_EQUAL( psa_cipher_set_iv( &operation,
2554 iv, sizeof( iv ) ),
2555 PSA_ERROR_BAD_STATE );
2556 PSA_ASSERT( psa_cipher_abort( &operation ) );
2557
2558 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002559 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002560 PSA_ASSERT( psa_cipher_set_iv( &operation,
2561 iv, sizeof( iv ) ) );
2562 TEST_EQUAL( psa_cipher_set_iv( &operation,
2563 iv, sizeof( iv ) ),
2564 PSA_ERROR_BAD_STATE );
2565 PSA_ASSERT( psa_cipher_abort( &operation ) );
2566
2567 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002568 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002569 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2570 buffer, sizeof( buffer ),
2571 &length ) );
2572 TEST_EQUAL( psa_cipher_set_iv( &operation,
2573 iv, sizeof( iv ) ),
2574 PSA_ERROR_BAD_STATE );
2575 PSA_ASSERT( psa_cipher_abort( &operation ) );
2576
2577 /* Call update without calling setup beforehand. */
2578 TEST_EQUAL( psa_cipher_update( &operation,
2579 text, sizeof( text ),
2580 buffer, sizeof( buffer ),
2581 &length ),
2582 PSA_ERROR_BAD_STATE );
2583 PSA_ASSERT( psa_cipher_abort( &operation ) );
2584
2585 /* Call update without an IV where an IV is required. */
2586 TEST_EQUAL( psa_cipher_update( &operation,
2587 text, sizeof( text ),
2588 buffer, sizeof( buffer ),
2589 &length ),
2590 PSA_ERROR_BAD_STATE );
2591 PSA_ASSERT( psa_cipher_abort( &operation ) );
2592
2593 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002594 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002595 PSA_ASSERT( psa_cipher_set_iv( &operation,
2596 iv, sizeof( iv ) ) );
2597 PSA_ASSERT( psa_cipher_finish( &operation,
2598 buffer, sizeof( buffer ), &length ) );
2599 TEST_EQUAL( psa_cipher_update( &operation,
2600 text, sizeof( text ),
2601 buffer, sizeof( buffer ),
2602 &length ),
2603 PSA_ERROR_BAD_STATE );
2604 PSA_ASSERT( psa_cipher_abort( &operation ) );
2605
2606 /* Call finish without calling setup beforehand. */
2607 TEST_EQUAL( psa_cipher_finish( &operation,
2608 buffer, sizeof( buffer ), &length ),
2609 PSA_ERROR_BAD_STATE );
2610 PSA_ASSERT( psa_cipher_abort( &operation ) );
2611
2612 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002613 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002614 /* Not calling update means we are encrypting an empty buffer, which is OK
2615 * for cipher modes with padding. */
2616 TEST_EQUAL( psa_cipher_finish( &operation,
2617 buffer, sizeof( buffer ), &length ),
2618 PSA_ERROR_BAD_STATE );
2619 PSA_ASSERT( psa_cipher_abort( &operation ) );
2620
2621 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002622 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002623 PSA_ASSERT( psa_cipher_set_iv( &operation,
2624 iv, sizeof( iv ) ) );
2625 PSA_ASSERT( psa_cipher_finish( &operation,
2626 buffer, sizeof( buffer ), &length ) );
2627 TEST_EQUAL( psa_cipher_finish( &operation,
2628 buffer, sizeof( buffer ), &length ),
2629 PSA_ERROR_BAD_STATE );
2630 PSA_ASSERT( psa_cipher_abort( &operation ) );
2631
Ronald Cron5425a212020-08-04 14:58:35 +02002632 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002633
Jaeden Ameroab439972019-02-15 14:12:05 +00002634exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002635 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002636 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002637}
2638/* END_CASE */
2639
2640/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002641void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002642 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002643 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002644 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002645{
Ronald Cron5425a212020-08-04 14:58:35 +02002646 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647 psa_status_t status;
2648 psa_key_type_t key_type = key_type_arg;
2649 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002650 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002651 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652 size_t output_buffer_size = 0;
2653 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002654 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002655 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657
Gilles Peskine8817f612018-12-18 00:18:46 +01002658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002660 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2661 psa_set_key_algorithm( &attributes, alg );
2662 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002663
Ronald Cron5425a212020-08-04 14:58:35 +02002664 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2665 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666
Ronald Cron5425a212020-08-04 14:58:35 +02002667 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002668
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002669 if( iv->len > 0 )
2670 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002671 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002672 }
2673
gabor-mezei-armceface22021-01-21 12:26:17 +01002674 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2675 TEST_ASSERT( output_buffer_size <=
2676 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002677 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678
Gilles Peskine8817f612018-12-18 00:18:46 +01002679 PSA_ASSERT( psa_cipher_update( &operation,
2680 input->x, input->len,
2681 output, output_buffer_size,
2682 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002683 TEST_ASSERT( function_output_length <=
2684 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2685 TEST_ASSERT( function_output_length <=
2686 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002687 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002688
Gilles Peskine50e586b2018-06-08 14:28:46 +02002689 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002690 ( output_buffer_size == 0 ? NULL :
2691 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002692 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002693 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002694 TEST_ASSERT( function_output_length <=
2695 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2696 TEST_ASSERT( function_output_length <=
2697 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002698 total_output_length += function_output_length;
2699
Gilles Peskinefe11b722018-12-18 00:24:04 +01002700 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002701 if( expected_status == PSA_SUCCESS )
2702 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002704 ASSERT_COMPARE( expected_output->x, expected_output->len,
2705 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002707
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002709 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002710 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002711 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002712 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713}
2714/* END_CASE */
2715
2716/* BEGIN_CASE */
2717void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002718 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002719 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002720 int first_part_size_arg,
2721 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002722 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002723{
Ronald Cron5425a212020-08-04 14:58:35 +02002724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002725 psa_key_type_t key_type = key_type_arg;
2726 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002727 size_t first_part_size = first_part_size_arg;
2728 size_t output1_length = output1_length_arg;
2729 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002730 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002731 size_t output_buffer_size = 0;
2732 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002733 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002734 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002735 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736
Gilles Peskine8817f612018-12-18 00:18:46 +01002737 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002739 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2740 psa_set_key_algorithm( &attributes, alg );
2741 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002742
Ronald Cron5425a212020-08-04 14:58:35 +02002743 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2744 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002745
Ronald Cron5425a212020-08-04 14:58:35 +02002746 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002747
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002748 if( iv->len > 0 )
2749 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002750 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002751 }
2752
gabor-mezei-armceface22021-01-21 12:26:17 +01002753 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2754 TEST_ASSERT( output_buffer_size <=
2755 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002756 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002757
Gilles Peskinee0866522019-02-19 19:44:00 +01002758 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002759 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2760 output, output_buffer_size,
2761 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002762 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002763 TEST_ASSERT( function_output_length <=
2764 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2765 TEST_ASSERT( function_output_length <=
2766 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002767 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002768
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 PSA_ASSERT( psa_cipher_update( &operation,
2770 input->x + first_part_size,
2771 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002772 ( output_buffer_size == 0 ? NULL :
2773 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002774 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002775 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002776 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002777 TEST_ASSERT( function_output_length <=
2778 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2779 alg,
2780 input->len - first_part_size ) );
2781 TEST_ASSERT( function_output_length <=
2782 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002783 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002784
Gilles Peskine8817f612018-12-18 00:18:46 +01002785 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002786 ( output_buffer_size == 0 ? NULL :
2787 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002788 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002790 TEST_ASSERT( function_output_length <=
2791 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2792 TEST_ASSERT( function_output_length <=
2793 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002794 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002796
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002797 ASSERT_COMPARE( expected_output->x, expected_output->len,
2798 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002799
2800exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002801 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002802 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002803 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002804 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805}
2806/* END_CASE */
2807
2808/* BEGIN_CASE */
2809void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002810 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002811 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002812 int first_part_size_arg,
2813 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815{
Ronald Cron5425a212020-08-04 14:58:35 +02002816 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002817 psa_key_type_t key_type = key_type_arg;
2818 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002819 size_t first_part_size = first_part_size_arg;
2820 size_t output1_length = output1_length_arg;
2821 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002822 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823 size_t output_buffer_size = 0;
2824 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002825 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002826 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828
Gilles Peskine8817f612018-12-18 00:18:46 +01002829 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2832 psa_set_key_algorithm( &attributes, alg );
2833 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002834
Ronald Cron5425a212020-08-04 14:58:35 +02002835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2836 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837
Ronald Cron5425a212020-08-04 14:58:35 +02002838 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839
Steven Cooreman177deba2020-09-07 17:14:14 +02002840 if( iv->len > 0 )
2841 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002842 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002843 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
gabor-mezei-armceface22021-01-21 12:26:17 +01002845 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2846 TEST_ASSERT( output_buffer_size <=
2847 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002848 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002849
Gilles Peskinee0866522019-02-19 19:44:00 +01002850 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002851 PSA_ASSERT( psa_cipher_update( &operation,
2852 input->x, first_part_size,
2853 output, output_buffer_size,
2854 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002855 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002856 TEST_ASSERT( function_output_length <=
2857 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2858 TEST_ASSERT( function_output_length <=
2859 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002860 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002861
Gilles Peskine8817f612018-12-18 00:18:46 +01002862 PSA_ASSERT( psa_cipher_update( &operation,
2863 input->x + first_part_size,
2864 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002865 ( output_buffer_size == 0 ? NULL :
2866 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002867 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002868 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002869 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002870 TEST_ASSERT( function_output_length <=
2871 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2872 alg,
2873 input->len - first_part_size ) );
2874 TEST_ASSERT( function_output_length <=
2875 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002876 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002877
Gilles Peskine8817f612018-12-18 00:18:46 +01002878 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002879 ( output_buffer_size == 0 ? NULL :
2880 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002881 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002882 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002883 TEST_ASSERT( function_output_length <=
2884 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2885 TEST_ASSERT( function_output_length <=
2886 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002887 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002888 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002890 ASSERT_COMPARE( expected_output->x, expected_output->len,
2891 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892
2893exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002894 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002895 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002896 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002897 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898}
2899/* END_CASE */
2900
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901/* BEGIN_CASE */
2902void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002903 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002904 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002905 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002906{
Ronald Cron5425a212020-08-04 14:58:35 +02002907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908 psa_status_t status;
2909 psa_key_type_t key_type = key_type_arg;
2910 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002911 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002912 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913 size_t output_buffer_size = 0;
2914 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002915 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002916 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002918
Gilles Peskine8817f612018-12-18 00:18:46 +01002919 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002921 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2922 psa_set_key_algorithm( &attributes, alg );
2923 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002924
Ronald Cron5425a212020-08-04 14:58:35 +02002925 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2926 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927
Ronald Cron5425a212020-08-04 14:58:35 +02002928 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929
Steven Cooreman177deba2020-09-07 17:14:14 +02002930 if( iv->len > 0 )
2931 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002932 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002933 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934
gabor-mezei-armceface22021-01-21 12:26:17 +01002935 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2936 TEST_ASSERT( output_buffer_size <=
2937 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002938 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002939
Gilles Peskine8817f612018-12-18 00:18:46 +01002940 PSA_ASSERT( psa_cipher_update( &operation,
2941 input->x, input->len,
2942 output, output_buffer_size,
2943 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002944 TEST_ASSERT( function_output_length <=
2945 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2946 TEST_ASSERT( function_output_length <=
2947 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002948 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002949
Gilles Peskine50e586b2018-06-08 14:28:46 +02002950 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002951 ( output_buffer_size == 0 ? NULL :
2952 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002953 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002954 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002955 TEST_ASSERT( function_output_length <=
2956 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2957 TEST_ASSERT( function_output_length <=
2958 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002959 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002960 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961
2962 if( expected_status == PSA_SUCCESS )
2963 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002964 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002965 ASSERT_COMPARE( expected_output->x, expected_output->len,
2966 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967 }
2968
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002970 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002971 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002972 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002973 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974}
2975/* END_CASE */
2976
Gilles Peskine50e586b2018-06-08 14:28:46 +02002977/* BEGIN_CASE */
2978void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002979 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002980 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981{
Ronald Cron5425a212020-08-04 14:58:35 +02002982 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002983 psa_key_type_t key_type = key_type_arg;
2984 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002985 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002986 size_t iv_size = 16;
2987 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002988 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002989 size_t output1_size = 0;
2990 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002991 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002992 size_t output2_size = 0;
2993 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002994 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002995 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2996 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002998
Gilles Peskine8817f612018-12-18 00:18:46 +01002999 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003000
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003001 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3002 psa_set_key_algorithm( &attributes, alg );
3003 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003004
Ronald Cron5425a212020-08-04 14:58:35 +02003005 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3006 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003007
Ronald Cron5425a212020-08-04 14:58:35 +02003008 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3009 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003010
Steven Cooreman177deba2020-09-07 17:14:14 +02003011 if( alg != PSA_ALG_ECB_NO_PADDING )
3012 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003013 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3014 iv, iv_size,
3015 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003016 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003017 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3018 TEST_ASSERT( output1_size <=
3019 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003020 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003021
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3023 output1, output1_size,
3024 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003025 TEST_ASSERT( output1_length <=
3026 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3027 TEST_ASSERT( output1_length <=
3028 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3029
Gilles Peskine8817f612018-12-18 00:18:46 +01003030 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003031 output1 + output1_length,
3032 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003033 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003034 TEST_ASSERT( function_output_length <=
3035 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3036 TEST_ASSERT( function_output_length <=
3037 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003038
Gilles Peskine048b7f02018-06-08 14:20:49 +02003039 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003040
Gilles Peskine8817f612018-12-18 00:18:46 +01003041 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003042
3043 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003044 TEST_ASSERT( output2_size <=
3045 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3046 TEST_ASSERT( output2_size <=
3047 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003048 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003049
Steven Cooreman177deba2020-09-07 17:14:14 +02003050 if( iv_length > 0 )
3051 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003052 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3053 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003054 }
3055
Gilles Peskine8817f612018-12-18 00:18:46 +01003056 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3057 output2, output2_size,
3058 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003059 TEST_ASSERT( output2_length <=
3060 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3061 TEST_ASSERT( output2_length <=
3062 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3063
Gilles Peskine048b7f02018-06-08 14:20:49 +02003064 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003065 PSA_ASSERT( psa_cipher_finish( &operation2,
3066 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003067 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003068 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003069 TEST_ASSERT( function_output_length <=
3070 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3071 TEST_ASSERT( function_output_length <=
3072 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003073
Gilles Peskine048b7f02018-06-08 14:20:49 +02003074 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003075
Gilles Peskine8817f612018-12-18 00:18:46 +01003076 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003077
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003078 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003079
3080exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003081 psa_cipher_abort( &operation1 );
3082 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003083 mbedtls_free( output1 );
3084 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003085 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003086 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003087}
3088/* END_CASE */
3089
3090/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003091void cipher_verify_output_multipart( int alg_arg,
3092 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003093 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003094 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003095 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003096{
Ronald Cron5425a212020-08-04 14:58:35 +02003097 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003098 psa_key_type_t key_type = key_type_arg;
3099 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003100 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003101 unsigned char iv[16] = {0};
3102 size_t iv_size = 16;
3103 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003104 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003105 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003106 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003107 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003108 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003109 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003110 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003111 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3112 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003114
Gilles Peskine8817f612018-12-18 00:18:46 +01003115 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003116
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003117 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3118 psa_set_key_algorithm( &attributes, alg );
3119 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003120
Ronald Cron5425a212020-08-04 14:58:35 +02003121 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3122 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003123
Ronald Cron5425a212020-08-04 14:58:35 +02003124 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3125 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003126
Steven Cooreman177deba2020-09-07 17:14:14 +02003127 if( alg != PSA_ALG_ECB_NO_PADDING )
3128 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003129 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3130 iv, iv_size,
3131 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003132 }
3133
gabor-mezei-armceface22021-01-21 12:26:17 +01003134 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3135 TEST_ASSERT( output1_buffer_size <=
3136 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003137 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003138
Gilles Peskinee0866522019-02-19 19:44:00 +01003139 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003140
Gilles Peskine8817f612018-12-18 00:18:46 +01003141 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3142 output1, output1_buffer_size,
3143 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003144 TEST_ASSERT( function_output_length <=
3145 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3146 TEST_ASSERT( function_output_length <=
3147 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003148 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003149
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 PSA_ASSERT( psa_cipher_update( &operation1,
3151 input->x + first_part_size,
3152 input->len - first_part_size,
3153 output1, output1_buffer_size,
3154 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003155 TEST_ASSERT( function_output_length <=
3156 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3157 alg,
3158 input->len - first_part_size ) );
3159 TEST_ASSERT( function_output_length <=
3160 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003161 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003162
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 PSA_ASSERT( psa_cipher_finish( &operation1,
3164 output1 + output1_length,
3165 output1_buffer_size - output1_length,
3166 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003167 TEST_ASSERT( function_output_length <=
3168 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3169 TEST_ASSERT( function_output_length <=
3170 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003171 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003172
Gilles Peskine8817f612018-12-18 00:18:46 +01003173 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003174
Gilles Peskine048b7f02018-06-08 14:20:49 +02003175 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003176 TEST_ASSERT( output2_buffer_size <=
3177 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3178 TEST_ASSERT( output2_buffer_size <=
3179 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003180 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003181
Steven Cooreman177deba2020-09-07 17:14:14 +02003182 if( iv_length > 0 )
3183 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003184 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3185 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003186 }
Moran Pekerded84402018-06-06 16:36:50 +03003187
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3189 output2, output2_buffer_size,
3190 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003191 TEST_ASSERT( function_output_length <=
3192 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3193 TEST_ASSERT( function_output_length <=
3194 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003195 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003196
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_cipher_update( &operation2,
3198 output1 + first_part_size,
3199 output1_length - first_part_size,
3200 output2, output2_buffer_size,
3201 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003202 TEST_ASSERT( function_output_length <=
3203 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3204 alg,
3205 output1_length - first_part_size ) );
3206 TEST_ASSERT( function_output_length <=
3207 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003208 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003209
Gilles Peskine8817f612018-12-18 00:18:46 +01003210 PSA_ASSERT( psa_cipher_finish( &operation2,
3211 output2 + output2_length,
3212 output2_buffer_size - output2_length,
3213 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003214 TEST_ASSERT( function_output_length <=
3215 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3216 TEST_ASSERT( function_output_length <=
3217 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003218 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003219
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003221
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003222 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003223
3224exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003225 psa_cipher_abort( &operation1 );
3226 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003227 mbedtls_free( output1 );
3228 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003229 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003230 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003231}
3232/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003233
Gilles Peskine20035e32018-02-03 22:44:14 +01003234/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003235void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003236 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003237 data_t *nonce,
3238 data_t *additional_data,
3239 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003240 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003241{
Ronald Cron5425a212020-08-04 14:58:35 +02003242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003243 psa_key_type_t key_type = key_type_arg;
3244 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003245 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003246 unsigned char *output_data = NULL;
3247 size_t output_size = 0;
3248 size_t output_length = 0;
3249 unsigned char *output_data2 = NULL;
3250 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003251 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003252 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254
Gilles Peskine8817f612018-12-18 00:18:46 +01003255 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003256
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3258 psa_set_key_algorithm( &attributes, alg );
3259 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003260
Gilles Peskine049c7532019-05-15 20:22:09 +02003261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003262 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003263 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3264 key_bits = psa_get_key_bits( &attributes );
3265
3266 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3267 alg );
3268 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3269 * should be exact. */
3270 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3271 expected_result != PSA_ERROR_NOT_SUPPORTED )
3272 {
3273 TEST_EQUAL( output_size,
3274 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3275 TEST_ASSERT( output_size <=
3276 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3277 }
3278 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003279
Steven Cooremanf49478b2021-02-15 15:19:25 +01003280 status = psa_aead_encrypt( key, alg,
3281 nonce->x, nonce->len,
3282 additional_data->x,
3283 additional_data->len,
3284 input_data->x, input_data->len,
3285 output_data, output_size,
3286 &output_length );
3287
3288 /* If the operation is not supported, just skip and not fail in case the
3289 * encryption involves a common limitation of cryptography hardwares and
3290 * an alternative implementation. */
3291 if( status == PSA_ERROR_NOT_SUPPORTED )
3292 {
3293 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3294 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3295 }
3296
3297 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003298
3299 if( PSA_SUCCESS == expected_result )
3300 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003301 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003302
Gilles Peskine003a4a92019-05-14 16:09:40 +02003303 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3304 * should be exact. */
3305 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003306 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003307
gabor-mezei-armceface22021-01-21 12:26:17 +01003308 TEST_ASSERT( input_data->len <=
3309 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3310
Ronald Cron5425a212020-08-04 14:58:35 +02003311 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003312 nonce->x, nonce->len,
3313 additional_data->x,
3314 additional_data->len,
3315 output_data, output_length,
3316 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003317 &output_length2 ),
3318 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003319
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003320 ASSERT_COMPARE( input_data->x, input_data->len,
3321 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003322 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003323
Gilles Peskinea1cac842018-06-11 19:33:02 +02003324exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003325 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003326 mbedtls_free( output_data );
3327 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003328 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329}
3330/* END_CASE */
3331
3332/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003333void aead_encrypt( int key_type_arg, data_t *key_data,
3334 int alg_arg,
3335 data_t *nonce,
3336 data_t *additional_data,
3337 data_t *input_data,
3338 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339{
Ronald Cron5425a212020-08-04 14:58:35 +02003340 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341 psa_key_type_t key_type = key_type_arg;
3342 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003343 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344 unsigned char *output_data = NULL;
3345 size_t output_size = 0;
3346 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003348 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003349
Gilles Peskine8817f612018-12-18 00:18:46 +01003350 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3353 psa_set_key_algorithm( &attributes, alg );
3354 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003355
Gilles Peskine049c7532019-05-15 20:22:09 +02003356 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003357 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003358 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3359 key_bits = psa_get_key_bits( &attributes );
3360
3361 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3362 alg );
3363 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3364 * should be exact. */
3365 TEST_EQUAL( output_size,
3366 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3367 TEST_ASSERT( output_size <=
3368 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3369 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003370
Steven Cooremand588ea12021-01-11 19:36:04 +01003371 status = psa_aead_encrypt( key, alg,
3372 nonce->x, nonce->len,
3373 additional_data->x, additional_data->len,
3374 input_data->x, input_data->len,
3375 output_data, output_size,
3376 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377
Ronald Cron28a45ed2021-02-09 20:35:42 +01003378 /* If the operation is not supported, just skip and not fail in case the
3379 * encryption involves a common limitation of cryptography hardwares and
3380 * an alternative implementation. */
3381 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003382 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003383 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3384 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003385 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003386
3387 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003388 ASSERT_COMPARE( expected_result->x, expected_result->len,
3389 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003390
Gilles Peskinea1cac842018-06-11 19:33:02 +02003391exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003392 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003393 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003394 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003395}
3396/* END_CASE */
3397
3398/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003399void aead_decrypt( int key_type_arg, data_t *key_data,
3400 int alg_arg,
3401 data_t *nonce,
3402 data_t *additional_data,
3403 data_t *input_data,
3404 data_t *expected_data,
3405 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406{
Ronald Cron5425a212020-08-04 14:58:35 +02003407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003408 psa_key_type_t key_type = key_type_arg;
3409 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003410 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003411 unsigned char *output_data = NULL;
3412 size_t output_size = 0;
3413 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003415 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003416 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417
Gilles Peskine8817f612018-12-18 00:18:46 +01003418 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003419
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003420 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3421 psa_set_key_algorithm( &attributes, alg );
3422 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003423
Gilles Peskine049c7532019-05-15 20:22:09 +02003424 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003425 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003426 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3427 key_bits = psa_get_key_bits( &attributes );
3428
3429 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3430 alg );
3431 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3432 expected_result != PSA_ERROR_NOT_SUPPORTED )
3433 {
3434 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3435 * should be exact. */
3436 TEST_EQUAL( output_size,
3437 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3438 TEST_ASSERT( output_size <=
3439 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3440 }
3441 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442
Steven Cooremand588ea12021-01-11 19:36:04 +01003443 status = psa_aead_decrypt( key, alg,
3444 nonce->x, nonce->len,
3445 additional_data->x,
3446 additional_data->len,
3447 input_data->x, input_data->len,
3448 output_data, output_size,
3449 &output_length );
3450
Ronald Cron28a45ed2021-02-09 20:35:42 +01003451 /* If the operation is not supported, just skip and not fail in case the
3452 * decryption involves a common limitation of cryptography hardwares and
3453 * an alternative implementation. */
3454 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003455 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003456 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3457 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003458 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003459
3460 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461
Gilles Peskine2d277862018-06-18 15:41:12 +02003462 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003463 ASSERT_COMPARE( expected_data->x, expected_data->len,
3464 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465
Gilles Peskinea1cac842018-06-11 19:33:02 +02003466exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003467 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003468 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003469 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470}
3471/* END_CASE */
3472
3473/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003474void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3475 int alg_arg,
3476 data_t *nonce,
3477 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003478 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003479 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003480 int do_test_data_chunked,
3481 int do_set_lengths,
3482 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003483{
Paul Elliottd3f82412021-06-16 16:52:21 +01003484 size_t ad_part_len = 0;
3485 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003486 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003487
Paul Elliotte64deda2021-09-09 14:07:23 +01003488 /* Ensure that either one part of the test or the other is done, i.e this
3489 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003490 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3491
3492 /* Temporary whilst we have algorithms that cannot support chunking */
3493 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003494 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003495 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3496 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003497 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003498 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003499
Paul Elliott33746aa2021-09-15 16:40:40 +01003500 if( do_set_lengths )
3501 {
3502 if( ad_part_len & 0x01 )
3503 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3504 else
3505 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3506 }
3507
Paul Elliott329d5382021-07-22 17:10:45 +01003508 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003509 if( !aead_multipart_internal_func( key_type_arg, key_data,
3510 alg_arg, nonce,
3511 additional_data,
3512 ad_part_len,
3513 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003514 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003515 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003516 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003517 break;
3518
3519 /* length(0) part, length(ad_part_len) part, length(0) part... */
3520 mbedtls_test_set_step( 1000 + ad_part_len );
3521
3522 if( !aead_multipart_internal_func( key_type_arg, key_data,
3523 alg_arg, nonce,
3524 additional_data,
3525 ad_part_len,
3526 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003527 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003528 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003529 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003530 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003531 }
3532 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003533
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003534 /* Temporary whilst we have algorithms that cannot support chunking */
3535 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003536 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003537 for( data_part_len = 1; data_part_len <= input_data->len;
3538 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003539 {
Paul Elliott329d5382021-07-22 17:10:45 +01003540 /* Split data into length(data_part_len) parts. */
3541 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003542
Paul Elliott33746aa2021-09-15 16:40:40 +01003543 if( do_set_lengths )
3544 {
3545 if( data_part_len & 0x01 )
3546 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3547 else
3548 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3549 }
3550
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003551 if( !aead_multipart_internal_func( key_type_arg, key_data,
3552 alg_arg, nonce,
3553 additional_data, -1,
3554 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003555 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003556 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003557 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003558 break;
3559
3560 /* length(0) part, length(data_part_len) part, length(0) part... */
3561 mbedtls_test_set_step( 3000 + data_part_len );
3562
3563 if( !aead_multipart_internal_func( key_type_arg, key_data,
3564 alg_arg, nonce,
3565 additional_data, -1,
3566 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003567 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003568 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003569 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003570 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003571 }
3572 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003573
Paul Elliott0023e0a2021-04-27 10:06:22 +01003574
Paul Elliott8fc45162021-06-23 16:06:01 +01003575 /* Goto is required to silence warnings about unused labels, as we
3576 * don't actually do any test assertions in this function. */
3577 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003578}
3579/* END_CASE */
3580
3581/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3583 int alg_arg,
3584 data_t *nonce,
3585 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003586 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003587 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003588 int do_test_data_chunked,
3589 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003590 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003591{
Paul Elliottd3f82412021-06-16 16:52:21 +01003592 size_t ad_part_len = 0;
3593 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003594 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003595
Paul Elliotte64deda2021-09-09 14:07:23 +01003596 /* Ensure that either one part of the test or the other is done, i.e this
3597 * test does something. */
3598 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3599
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003600 /* Temporary whilst we have algorithms that cannot support chunking */
3601 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003602 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003603 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3604 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003605 {
Paul Elliott329d5382021-07-22 17:10:45 +01003606 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003607 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003608
Paul Elliott33746aa2021-09-15 16:40:40 +01003609 if( do_set_lengths )
3610 {
3611 if( ad_part_len & 0x01 )
3612 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3613 else
3614 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3615 }
3616
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003617 if( !aead_multipart_internal_func( key_type_arg, key_data,
3618 alg_arg, nonce,
3619 additional_data,
3620 ad_part_len,
3621 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003622 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003623 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003624 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003625 break;
3626
3627 /* length(0) part, length(ad_part_len) part, length(0) part... */
3628 mbedtls_test_set_step( 1000 + ad_part_len );
3629
3630 if( !aead_multipart_internal_func( key_type_arg, key_data,
3631 alg_arg, nonce,
3632 additional_data,
3633 ad_part_len,
3634 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003635 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003636 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003637 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003638 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003639 }
3640 }
3641
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003642 /* Temporary whilst we have algorithms that cannot support chunking */
3643 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003644 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003645 for( data_part_len = 1; data_part_len <= input_data->len;
3646 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003647 {
Paul Elliott329d5382021-07-22 17:10:45 +01003648 /* Split data into length(data_part_len) parts. */
3649 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003650
Paul Elliott33746aa2021-09-15 16:40:40 +01003651 if( do_set_lengths )
3652 {
3653 if( data_part_len & 0x01 )
3654 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3655 else
3656 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3657 }
3658
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003659 if( !aead_multipart_internal_func( key_type_arg, key_data,
3660 alg_arg, nonce,
3661 additional_data, -1,
3662 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003663 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003664 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003665 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003666 break;
3667
3668 /* length(0) part, length(data_part_len) part, length(0) part... */
3669 mbedtls_test_set_step( 3000 + data_part_len );
3670
3671 if( !aead_multipart_internal_func( key_type_arg, key_data,
3672 alg_arg, nonce,
3673 additional_data, -1,
3674 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003675 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003676 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003677 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003678 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003679 }
3680 }
3681
Paul Elliott8fc45162021-06-23 16:06:01 +01003682 /* Goto is required to silence warnings about unused labels, as we
3683 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003684 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003685}
3686/* END_CASE */
3687
3688/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003689void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3690 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003691 int nonce_length,
3692 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003693 data_t *additional_data,
3694 data_t *input_data,
3695 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003696{
3697
3698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3699 psa_key_type_t key_type = key_type_arg;
3700 psa_algorithm_t alg = alg_arg;
3701 psa_aead_operation_t operation;
3702 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3704 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003705 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003706 size_t actual_nonce_length = 0;
3707 size_t expected_nonce_length = expected_nonce_length_arg;
3708 unsigned char *output = NULL;
3709 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003710 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003711 size_t ciphertext_size = 0;
3712 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003713 size_t tag_length = 0;
3714 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003715
3716 PSA_ASSERT( psa_crypto_init( ) );
3717
3718 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3719 psa_set_key_algorithm( & attributes, alg );
3720 psa_set_key_type( & attributes, key_type );
3721
3722 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3723 &key ) );
3724
3725 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3726
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003727 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3728
Paul Elliottf1277632021-08-24 18:11:37 +01003729 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003730
Paul Elliottf1277632021-08-24 18:11:37 +01003731 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003732
Paul Elliottf1277632021-08-24 18:11:37 +01003733 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003734
Paul Elliottf1277632021-08-24 18:11:37 +01003735 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003736
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003737 operation = psa_aead_operation_init( );
3738
3739 status = psa_aead_encrypt_setup( &operation, key, alg );
3740
3741 /* If the operation is not supported, just skip and not fail in case the
3742 * encryption involves a common limitation of cryptography hardwares and
3743 * an alternative implementation. */
3744 if( status == PSA_ERROR_NOT_SUPPORTED )
3745 {
3746 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003747 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003748 }
3749
3750 PSA_ASSERT( status );
3751
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003752 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003753 nonce_length,
3754 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003755
Paul Elliott693bf312021-07-23 17:40:41 +01003756 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003757
Paul Elliottf1277632021-08-24 18:11:37 +01003758 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003759
Paul Elliottf1277632021-08-24 18:11:37 +01003760 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003761
Paul Elliott693bf312021-07-23 17:40:41 +01003762 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003763 {
3764
3765 /* Ensure we can still complete operation. */
3766
3767 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3768 additional_data->len ) );
3769
3770 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003771 output, output_size,
3772 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003773
Paul Elliottf1277632021-08-24 18:11:37 +01003774 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3775 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003776 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3777 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003778
3779exit:
3780 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003781 mbedtls_free( output );
3782 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003783 psa_aead_abort( &operation );
3784 PSA_DONE( );
3785}
3786/* END_CASE */
3787
3788/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003789void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3790 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003791 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003792 data_t *additional_data,
3793 data_t *input_data,
3794 int expected_status_arg )
3795{
3796
3797 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3798 psa_key_type_t key_type = key_type_arg;
3799 psa_algorithm_t alg = alg_arg;
3800 psa_aead_operation_t operation;
3801 uint8_t *nonce_buffer = NULL;
3802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3803 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3804 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003805 unsigned char *output = NULL;
3806 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003807 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003808 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003809 size_t ciphertext_size = 0;
3810 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003811 size_t tag_length = 0;
3812 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003813 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003814
3815 PSA_ASSERT( psa_crypto_init( ) );
3816
3817 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3818 psa_set_key_algorithm( &attributes, alg );
3819 psa_set_key_type( &attributes, key_type );
3820
3821 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3822 &key ) );
3823
3824 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3825
3826 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3827
Paul Elliott6f0e7202021-08-25 12:57:18 +01003828 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003829
Paul Elliott6f0e7202021-08-25 12:57:18 +01003830 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003831
Paul Elliott6f0e7202021-08-25 12:57:18 +01003832 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003833
Paul Elliott6f0e7202021-08-25 12:57:18 +01003834 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003835
3836 operation = psa_aead_operation_init( );
3837
3838 status = psa_aead_encrypt_setup( &operation, key, alg );
3839
3840 /* If the operation is not supported, just skip and not fail in case the
3841 * encryption involves a common limitation of cryptography hardwares and
3842 * an alternative implementation. */
3843 if( status == PSA_ERROR_NOT_SUPPORTED )
3844 {
3845 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003846 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003847 }
3848
3849 PSA_ASSERT( status );
3850
Paul Elliott4023ffd2021-09-10 16:21:22 +01003851 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3852 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003853 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003854 /* Arbitrary size buffer, to test zero length valid buffer. */
3855 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003856 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003857 }
3858 else
3859 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003860 /* If length is zero, then this will return NULL. */
3861 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003862 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003863
Paul Elliott4023ffd2021-09-10 16:21:22 +01003864 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003865 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003866 for( index = 0; index < nonce_length - 1; ++index )
3867 {
3868 nonce_buffer[index] = 'a' + index;
3869 }
Paul Elliott66696b52021-08-16 18:42:41 +01003870 }
Paul Elliott863864a2021-07-23 17:28:31 +01003871 }
3872
Paul Elliott6f0e7202021-08-25 12:57:18 +01003873 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003874
Paul Elliott693bf312021-07-23 17:40:41 +01003875 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003876
3877 if( expected_status == PSA_SUCCESS )
3878 {
3879 /* Ensure we can still complete operation. */
3880
3881 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3882 additional_data->len ) );
3883
3884 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003885 output, output_size,
3886 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003887
Paul Elliott6f0e7202021-08-25 12:57:18 +01003888 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3889 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003890 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3891 }
3892
3893exit:
3894 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003895 mbedtls_free( output );
3896 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003897 mbedtls_free( nonce_buffer );
3898 psa_aead_abort( &operation );
3899 PSA_DONE( );
3900}
3901/* END_CASE */
3902
3903/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003904void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3905 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003906 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003907 data_t *nonce,
3908 data_t *additional_data,
3909 data_t *input_data,
3910 int expected_status_arg )
3911{
3912
3913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3914 psa_key_type_t key_type = key_type_arg;
3915 psa_algorithm_t alg = alg_arg;
3916 psa_aead_operation_t operation;
3917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3918 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3919 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003920 unsigned char *output = NULL;
3921 unsigned char *ciphertext = NULL;
3922 size_t output_size = output_size_arg;
3923 size_t ciphertext_size = 0;
3924 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003925 size_t tag_length = 0;
3926 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3927
3928 PSA_ASSERT( psa_crypto_init( ) );
3929
3930 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3931 psa_set_key_algorithm( &attributes, alg );
3932 psa_set_key_type( &attributes, key_type );
3933
3934 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3935 &key ) );
3936
3937 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3938
Paul Elliottc6d11d02021-09-01 12:04:23 +01003939 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003940
Paul Elliottc6d11d02021-09-01 12:04:23 +01003941 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003942
Paul Elliottc6d11d02021-09-01 12:04:23 +01003943 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003944
3945 operation = psa_aead_operation_init( );
3946
3947 status = psa_aead_encrypt_setup( &operation, key, alg );
3948
3949 /* If the operation is not supported, just skip and not fail in case the
3950 * encryption involves a common limitation of cryptography hardwares and
3951 * an alternative implementation. */
3952 if( status == PSA_ERROR_NOT_SUPPORTED )
3953 {
3954 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3955 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3956 }
3957
3958 PSA_ASSERT( status );
3959
3960 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3961
3962 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3963 additional_data->len ) );
3964
3965 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003966 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003967
3968 TEST_EQUAL( status, expected_status );
3969
3970 if( expected_status == PSA_SUCCESS )
3971 {
3972 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003973 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3974 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003975 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3976 }
3977
3978exit:
3979 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003980 mbedtls_free( output );
3981 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003982 psa_aead_abort( &operation );
3983 PSA_DONE( );
3984}
3985/* END_CASE */
3986
Paul Elliott91b021e2021-07-23 18:52:31 +01003987/* BEGIN_CASE */
3988void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3989 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003990 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003991 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003992 data_t *nonce,
3993 data_t *additional_data,
3994 data_t *input_data,
3995 int expected_status_arg )
3996{
3997
3998 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3999 psa_key_type_t key_type = key_type_arg;
4000 psa_algorithm_t alg = alg_arg;
4001 psa_aead_operation_t operation;
4002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4003 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4004 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004005 unsigned char *ciphertext = NULL;
4006 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004007 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004008 size_t ciphertext_size = 0;
4009 size_t ciphertext_length = 0;
4010 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004011 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004012 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004013
4014 PSA_ASSERT( psa_crypto_init( ) );
4015
4016 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4017 psa_set_key_algorithm( &attributes, alg );
4018 psa_set_key_type( &attributes, key_type );
4019
4020 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4021 &key ) );
4022
4023 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4024
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004025 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004026
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004027 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004028
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004029 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004030
Paul Elliott719c1322021-09-13 18:27:22 +01004031 ASSERT_ALLOC( tag_buffer, tag_size );
4032
Paul Elliott91b021e2021-07-23 18:52:31 +01004033 operation = psa_aead_operation_init( );
4034
4035 status = psa_aead_encrypt_setup( &operation, key, alg );
4036
4037 /* If the operation is not supported, just skip and not fail in case the
4038 * encryption involves a common limitation of cryptography hardwares and
4039 * an alternative implementation. */
4040 if( status == PSA_ERROR_NOT_SUPPORTED )
4041 {
4042 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4043 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4044 }
4045
4046 PSA_ASSERT( status );
4047
4048 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4049
4050 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4051 additional_data->len ) );
4052
4053 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004054 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004055
4056 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004057 status = psa_aead_finish( &operation, finish_ciphertext,
4058 finish_ciphertext_size,
4059 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004060 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004061
4062 TEST_EQUAL( status, expected_status );
4063
4064exit:
4065 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004066 mbedtls_free( ciphertext );
4067 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004068 psa_aead_abort( &operation );
4069 PSA_DONE( );
4070}
4071/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004072
4073/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004074void aead_multipart_verify( int key_type_arg, data_t *key_data,
4075 int alg_arg,
4076 data_t *nonce,
4077 data_t *additional_data,
4078 data_t *input_data,
4079 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004080 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004081 int expected_status_arg )
4082{
4083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4084 psa_key_type_t key_type = key_type_arg;
4085 psa_algorithm_t alg = alg_arg;
4086 psa_aead_operation_t operation;
4087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4088 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4089 psa_status_t expected_status = expected_status_arg;
4090 unsigned char *plaintext = NULL;
4091 unsigned char *finish_plaintext = NULL;
4092 size_t plaintext_size = 0;
4093 size_t plaintext_length = 0;
4094 size_t verify_plaintext_size = 0;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004095 tagusage_method tag_usage = tag_usage_arg;
4096 unsigned char *tag_buffer = NULL;
4097 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004098
4099 PSA_ASSERT( psa_crypto_init( ) );
4100
4101 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4102 psa_set_key_algorithm( &attributes, alg );
4103 psa_set_key_type( &attributes, key_type );
4104
4105 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4106 &key ) );
4107
4108 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4109
4110 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4111 input_data->len );
4112
4113 ASSERT_ALLOC( plaintext, plaintext_size );
4114
4115 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4116
4117 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4118
4119 operation = psa_aead_operation_init( );
4120
4121 status = psa_aead_decrypt_setup( &operation, key, alg );
4122
4123 /* If the operation is not supported, just skip and not fail in case the
4124 * encryption involves a common limitation of cryptography hardwares and
4125 * an alternative implementation. */
4126 if( status == PSA_ERROR_NOT_SUPPORTED )
4127 {
4128 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4129 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4130 }
4131
4132 PSA_ASSERT( status );
4133
4134 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4135
4136 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4137 additional_data->len ) );
4138
4139 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4140 input_data->len,
4141 plaintext, plaintext_size,
4142 &plaintext_length ) );
4143
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004144 if( tag_usage == USE_GIVEN_TAG )
4145 {
4146 tag_buffer = tag->x;
4147 tag_size = tag->len;
4148 }
4149
Paul Elliott9961a662021-09-17 19:19:02 +01004150 status = psa_aead_verify( &operation, finish_plaintext,
4151 verify_plaintext_size,
4152 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004153 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004154
4155 TEST_EQUAL( status, expected_status );
4156
4157exit:
4158 psa_destroy_key( key );
4159 mbedtls_free( plaintext );
4160 mbedtls_free( finish_plaintext );
4161 psa_aead_abort( &operation );
4162 PSA_DONE( );
4163}
4164/* END_CASE */
4165
Paul Elliott9961a662021-09-17 19:19:02 +01004166/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004167void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4168 int alg_arg,
4169 data_t *nonce,
4170 data_t *additional_data,
4171 data_t *input_data )
4172{
4173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4174 psa_key_type_t key_type = key_type_arg;
4175 psa_algorithm_t alg = alg_arg;
4176 psa_aead_operation_t operation;
4177 unsigned char *output_data = NULL;
4178 unsigned char *final_data = NULL;
4179 size_t output_size = 0;
4180 size_t finish_output_size = 0;
4181 size_t output_length = 0;
4182 size_t key_bits = 0;
4183 size_t tag_length = 0;
4184 size_t tag_size = 0;
4185 size_t nonce_length = 0;
4186 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4187 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4188 size_t output_part_length = 0;
4189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4190
4191 PSA_ASSERT( psa_crypto_init( ) );
4192
4193 psa_set_key_usage_flags( & attributes,
4194 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4195 psa_set_key_algorithm( & attributes, alg );
4196 psa_set_key_type( & attributes, key_type );
4197
4198 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4199 &key ) );
4200
4201 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4202 key_bits = psa_get_key_bits( &attributes );
4203
4204 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4205
4206 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4207
4208 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4209
4210 ASSERT_ALLOC( output_data, output_size );
4211
4212 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4213
4214 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4215
4216 ASSERT_ALLOC( final_data, finish_output_size );
4217
4218 /* Test all operations error without calling setup first. */
4219
4220 operation = psa_aead_operation_init( );
4221
4222 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4223 PSA_ERROR_BAD_STATE );
4224
4225 psa_aead_abort( &operation );
4226
4227 operation = psa_aead_operation_init( );
4228
4229 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4230 PSA_AEAD_NONCE_MAX_SIZE,
4231 &nonce_length ),
4232 PSA_ERROR_BAD_STATE );
4233
4234 psa_aead_abort( &operation );
4235
Paul Elliott481be342021-07-16 17:38:47 +01004236 /* ------------------------------------------------------- */
4237
Paul Elliottc23a9a02021-06-21 18:32:46 +01004238 operation = psa_aead_operation_init( );
4239
4240 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4241 input_data->len ),
4242 PSA_ERROR_BAD_STATE );
4243
4244 psa_aead_abort( &operation );
4245
Paul Elliott481be342021-07-16 17:38:47 +01004246 /* ------------------------------------------------------- */
4247
Paul Elliottc23a9a02021-06-21 18:32:46 +01004248 operation = psa_aead_operation_init( );
4249
4250 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4251 additional_data->len ),
4252 PSA_ERROR_BAD_STATE );
4253
4254 psa_aead_abort( &operation );
4255
Paul Elliott481be342021-07-16 17:38:47 +01004256 /* ------------------------------------------------------- */
4257
Paul Elliottc23a9a02021-06-21 18:32:46 +01004258 operation = psa_aead_operation_init( );
4259
4260 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4261 input_data->len, output_data,
4262 output_size, &output_length ),
4263 PSA_ERROR_BAD_STATE );
4264
4265 psa_aead_abort( &operation );
4266
Paul Elliott481be342021-07-16 17:38:47 +01004267 /* ------------------------------------------------------- */
4268
Paul Elliottc23a9a02021-06-21 18:32:46 +01004269 operation = psa_aead_operation_init( );
4270
4271 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4272 finish_output_size,
4273 &output_part_length,
4274 tag_buffer, tag_length,
4275 &tag_size ),
4276 PSA_ERROR_BAD_STATE );
4277
4278 psa_aead_abort( &operation );
4279
Paul Elliott481be342021-07-16 17:38:47 +01004280 /* ------------------------------------------------------- */
4281
Paul Elliottc23a9a02021-06-21 18:32:46 +01004282 operation = psa_aead_operation_init( );
4283
4284 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4285 finish_output_size,
4286 &output_part_length,
4287 tag_buffer,
4288 tag_length ),
4289 PSA_ERROR_BAD_STATE );
4290
4291 psa_aead_abort( &operation );
4292
4293 /* Test for double setups. */
4294
4295 operation = psa_aead_operation_init( );
4296
4297 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4298
4299 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4300 PSA_ERROR_BAD_STATE );
4301
4302 psa_aead_abort( &operation );
4303
Paul Elliott481be342021-07-16 17:38:47 +01004304 /* ------------------------------------------------------- */
4305
Paul Elliottc23a9a02021-06-21 18:32:46 +01004306 operation = psa_aead_operation_init( );
4307
4308 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4309
4310 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4311 PSA_ERROR_BAD_STATE );
4312
4313 psa_aead_abort( &operation );
4314
Paul Elliott374a2be2021-07-16 17:53:40 +01004315 /* ------------------------------------------------------- */
4316
4317 operation = psa_aead_operation_init( );
4318
4319 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4320
4321 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4322 PSA_ERROR_BAD_STATE );
4323
4324 psa_aead_abort( &operation );
4325
4326 /* ------------------------------------------------------- */
4327
4328 operation = psa_aead_operation_init( );
4329
4330 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4331
4332 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4333 PSA_ERROR_BAD_STATE );
4334
4335 psa_aead_abort( &operation );
4336
Paul Elliottc23a9a02021-06-21 18:32:46 +01004337 /* Test for not setting a nonce. */
4338
4339 operation = psa_aead_operation_init( );
4340
4341 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4342
4343 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4344 additional_data->len ),
4345 PSA_ERROR_BAD_STATE );
4346
4347 psa_aead_abort( &operation );
4348
Paul Elliott7f628422021-09-01 12:08:29 +01004349 /* ------------------------------------------------------- */
4350
4351 operation = psa_aead_operation_init( );
4352
4353 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4354
4355 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4356 input_data->len, output_data,
4357 output_size, &output_length ),
4358 PSA_ERROR_BAD_STATE );
4359
4360 psa_aead_abort( &operation );
4361
Paul Elliottc23a9a02021-06-21 18:32:46 +01004362 /* Test for double setting nonce. */
4363
4364 operation = psa_aead_operation_init( );
4365
4366 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4367
4368 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4369
4370 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4371 PSA_ERROR_BAD_STATE );
4372
4373 psa_aead_abort( &operation );
4374
Paul Elliott374a2be2021-07-16 17:53:40 +01004375 /* Test for double generating nonce. */
4376
4377 operation = psa_aead_operation_init( );
4378
4379 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4380
4381 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4382 PSA_AEAD_NONCE_MAX_SIZE,
4383 &nonce_length ) );
4384
4385 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4386 PSA_AEAD_NONCE_MAX_SIZE,
4387 &nonce_length ),
4388 PSA_ERROR_BAD_STATE );
4389
4390
4391 psa_aead_abort( &operation );
4392
4393 /* Test for generate nonce then set and vice versa */
4394
4395 operation = psa_aead_operation_init( );
4396
4397 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4398
4399 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4400 PSA_AEAD_NONCE_MAX_SIZE,
4401 &nonce_length ) );
4402
4403 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4404 PSA_ERROR_BAD_STATE );
4405
4406 psa_aead_abort( &operation );
4407
4408 /* ------------------------------------------------------- */
4409
4410 operation = psa_aead_operation_init( );
4411
4412 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4413
4414 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4415
4416 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4417 PSA_AEAD_NONCE_MAX_SIZE,
4418 &nonce_length ),
4419 PSA_ERROR_BAD_STATE );
4420
4421 psa_aead_abort( &operation );
4422
Paul Elliott7220cae2021-06-22 17:25:57 +01004423 /* Test for generating nonce in decrypt setup. */
4424
4425 operation = psa_aead_operation_init( );
4426
4427 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4428
4429 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4430 PSA_AEAD_NONCE_MAX_SIZE,
4431 &nonce_length ),
4432 PSA_ERROR_BAD_STATE );
4433
4434 psa_aead_abort( &operation );
4435
Paul Elliottc23a9a02021-06-21 18:32:46 +01004436 /* Test for setting lengths twice. */
4437
4438 operation = psa_aead_operation_init( );
4439
4440 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4441
4442 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4443
4444 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4445 input_data->len ) );
4446
4447 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4448 input_data->len ),
4449 PSA_ERROR_BAD_STATE );
4450
4451 psa_aead_abort( &operation );
4452
4453 /* Test for setting lengths after already starting data. */
4454
4455 operation = psa_aead_operation_init( );
4456
4457 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4458
4459 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4460
4461 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4462 input_data->len, output_data,
4463 output_size, &output_length ) );
4464
4465 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4466 input_data->len ),
4467 PSA_ERROR_BAD_STATE );
4468
4469 psa_aead_abort( &operation );
4470
Paul Elliott243080c2021-07-21 19:01:17 +01004471 /* Test for not sending any additional data or data after setting non zero
4472 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004473
4474 operation = psa_aead_operation_init( );
4475
4476 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4477
4478 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4479
4480 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4481 input_data->len ) );
4482
4483 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4484 finish_output_size,
4485 &output_part_length,
4486 tag_buffer, tag_length,
4487 &tag_size ),
4488 PSA_ERROR_INVALID_ARGUMENT );
4489
4490 psa_aead_abort( &operation );
4491
Paul Elliott243080c2021-07-21 19:01:17 +01004492 /* Test for not sending any additional data or data after setting non-zero
4493 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004494
4495 operation = psa_aead_operation_init( );
4496
4497 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4498
4499 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4500
4501 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4502 input_data->len ) );
4503
4504 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4505 finish_output_size,
4506 &output_part_length,
4507 tag_buffer,
4508 tag_length ),
4509 PSA_ERROR_INVALID_ARGUMENT );
4510
4511 psa_aead_abort( &operation );
4512
Paul Elliott243080c2021-07-21 19:01:17 +01004513 /* Test for not sending any additional data after setting a non-zero length
4514 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004515
4516 operation = psa_aead_operation_init( );
4517
4518 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4519
4520 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4521
4522 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4523 input_data->len ) );
4524
4525 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4526 input_data->len, output_data,
4527 output_size, &output_length ),
4528 PSA_ERROR_INVALID_ARGUMENT );
4529
4530 psa_aead_abort( &operation );
4531
Paul Elliottb0450fe2021-09-01 15:06:26 +01004532 /* Test for sending too much additional data after setting lengths. */
4533
4534 operation = psa_aead_operation_init( );
4535
4536 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4537
4538 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4539
4540 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4541
4542
4543 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4544 additional_data->len ),
4545 PSA_ERROR_INVALID_ARGUMENT );
4546
4547 psa_aead_abort( &operation );
4548
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004549 operation = psa_aead_operation_init( );
4550
4551 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4552
4553 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4554
4555 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4556 input_data->len ) );
4557
4558 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4559 additional_data->len ) );
4560
4561 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4562 1 ),
4563 PSA_ERROR_INVALID_ARGUMENT );
4564
4565 psa_aead_abort( &operation );
4566
Paul Elliottb0450fe2021-09-01 15:06:26 +01004567 /* Test for sending too much data after setting lengths. */
4568
4569 operation = psa_aead_operation_init( );
4570
4571 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4572
4573 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4574
4575 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4576
4577 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4578 input_data->len, output_data,
4579 output_size, &output_length ),
4580 PSA_ERROR_INVALID_ARGUMENT );
4581
4582 psa_aead_abort( &operation );
4583
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004584 operation = psa_aead_operation_init( );
4585
4586 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4587
4588 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4589
4590 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4591 input_data->len ) );
4592
4593 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4594 additional_data->len ) );
4595
4596 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4597 input_data->len, output_data,
4598 output_size, &output_length ) );
4599
4600 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4601 1, output_data,
4602 output_size, &output_length ),
4603 PSA_ERROR_INVALID_ARGUMENT );
4604
4605 psa_aead_abort( &operation );
4606
Paul Elliottc23a9a02021-06-21 18:32:46 +01004607 /* Test sending additional data after data. */
4608
4609 operation = psa_aead_operation_init( );
4610
4611 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4612
4613 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4614
4615 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4616 input_data->len, output_data,
4617 output_size, &output_length ) );
4618
4619 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4620 additional_data->len ),
4621 PSA_ERROR_BAD_STATE );
4622
4623 psa_aead_abort( &operation );
4624
Paul Elliott534d0b42021-06-22 19:15:20 +01004625 /* Test calling finish on decryption. */
4626
4627 operation = psa_aead_operation_init( );
4628
4629 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4630
4631 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4632
4633 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4634 finish_output_size,
4635 &output_part_length,
4636 tag_buffer, tag_length,
4637 &tag_size ),
4638 PSA_ERROR_BAD_STATE );
4639
4640 psa_aead_abort( &operation );
4641
4642 /* Test calling verify on encryption. */
4643
4644 operation = psa_aead_operation_init( );
4645
4646 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4647
4648 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4649
4650 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4651 finish_output_size,
4652 &output_part_length,
4653 tag_buffer,
4654 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004655 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004656
4657 psa_aead_abort( &operation );
4658
4659
Paul Elliottc23a9a02021-06-21 18:32:46 +01004660exit:
4661 psa_destroy_key( key );
4662 psa_aead_abort( &operation );
4663 mbedtls_free( output_data );
4664 mbedtls_free( final_data );
4665 PSA_DONE( );
4666}
4667/* END_CASE */
4668
4669/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004670void signature_size( int type_arg,
4671 int bits,
4672 int alg_arg,
4673 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004674{
4675 psa_key_type_t type = type_arg;
4676 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004677 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004678
Gilles Peskinefe11b722018-12-18 00:24:04 +01004679 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004680
Gilles Peskinee59236f2018-01-27 23:32:46 +01004681exit:
4682 ;
4683}
4684/* END_CASE */
4685
4686/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004687void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4688 int alg_arg, data_t *input_data,
4689 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004690{
Ronald Cron5425a212020-08-04 14:58:35 +02004691 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004692 psa_key_type_t key_type = key_type_arg;
4693 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004694 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004695 unsigned char *signature = NULL;
4696 size_t signature_size;
4697 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004699
Gilles Peskine8817f612018-12-18 00:18:46 +01004700 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004701
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004702 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004703 psa_set_key_algorithm( &attributes, alg );
4704 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004705
Gilles Peskine049c7532019-05-15 20:22:09 +02004706 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004707 &key ) );
4708 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004709 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004710
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004711 /* Allocate a buffer which has the size advertized by the
4712 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004713 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004714 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004715 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004716 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004717 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004718
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004719 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004720 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004721 input_data->x, input_data->len,
4722 signature, signature_size,
4723 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004724 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004725 ASSERT_COMPARE( output_data->x, output_data->len,
4726 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004727
4728exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004729 /*
4730 * Key attributes may have been returned by psa_get_key_attributes()
4731 * thus reset them as required.
4732 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004733 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004734
Ronald Cron5425a212020-08-04 14:58:35 +02004735 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004736 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004737 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004738}
4739/* END_CASE */
4740
4741/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004742void sign_hash_fail( int key_type_arg, data_t *key_data,
4743 int alg_arg, data_t *input_data,
4744 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004745{
Ronald Cron5425a212020-08-04 14:58:35 +02004746 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004747 psa_key_type_t key_type = key_type_arg;
4748 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004749 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004750 psa_status_t actual_status;
4751 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004752 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004753 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004754 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004755
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004756 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004757
Gilles Peskine8817f612018-12-18 00:18:46 +01004758 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004759
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004760 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004761 psa_set_key_algorithm( &attributes, alg );
4762 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004763
Gilles Peskine049c7532019-05-15 20:22:09 +02004764 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004765 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004766
Ronald Cron5425a212020-08-04 14:58:35 +02004767 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004768 input_data->x, input_data->len,
4769 signature, signature_size,
4770 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004771 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004772 /* The value of *signature_length is unspecified on error, but
4773 * whatever it is, it should be less than signature_size, so that
4774 * if the caller tries to read *signature_length bytes without
4775 * checking the error code then they don't overflow a buffer. */
4776 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004777
4778exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004779 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004780 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004781 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004782 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004783}
4784/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004785
4786/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004787void sign_verify_hash( int key_type_arg, data_t *key_data,
4788 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004789{
Ronald Cron5425a212020-08-04 14:58:35 +02004790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004791 psa_key_type_t key_type = key_type_arg;
4792 psa_algorithm_t alg = alg_arg;
4793 size_t key_bits;
4794 unsigned char *signature = NULL;
4795 size_t signature_size;
4796 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004798
Gilles Peskine8817f612018-12-18 00:18:46 +01004799 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004800
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004801 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004802 psa_set_key_algorithm( &attributes, alg );
4803 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004804
Gilles Peskine049c7532019-05-15 20:22:09 +02004805 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004806 &key ) );
4807 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004808 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004809
4810 /* Allocate a buffer which has the size advertized by the
4811 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004812 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004813 key_bits, alg );
4814 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004815 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004816 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004817
4818 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004819 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004820 input_data->x, input_data->len,
4821 signature, signature_size,
4822 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004823 /* Check that the signature length looks sensible. */
4824 TEST_ASSERT( signature_length <= signature_size );
4825 TEST_ASSERT( signature_length > 0 );
4826
4827 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004828 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004829 input_data->x, input_data->len,
4830 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004831
4832 if( input_data->len != 0 )
4833 {
4834 /* Flip a bit in the input and verify that the signature is now
4835 * detected as invalid. Flip a bit at the beginning, not at the end,
4836 * because ECDSA may ignore the last few bits of the input. */
4837 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004838 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004839 input_data->x, input_data->len,
4840 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004841 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004842 }
4843
4844exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004845 /*
4846 * Key attributes may have been returned by psa_get_key_attributes()
4847 * thus reset them as required.
4848 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004849 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004850
Ronald Cron5425a212020-08-04 14:58:35 +02004851 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004852 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004853 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004854}
4855/* END_CASE */
4856
4857/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004858void verify_hash( int key_type_arg, data_t *key_data,
4859 int alg_arg, data_t *hash_data,
4860 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004861{
Ronald Cron5425a212020-08-04 14:58:35 +02004862 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004863 psa_key_type_t key_type = key_type_arg;
4864 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004866
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004867 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004868
Gilles Peskine8817f612018-12-18 00:18:46 +01004869 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004870
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004871 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004872 psa_set_key_algorithm( &attributes, alg );
4873 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004874
Gilles Peskine049c7532019-05-15 20:22:09 +02004875 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004876 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004877
Ronald Cron5425a212020-08-04 14:58:35 +02004878 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004879 hash_data->x, hash_data->len,
4880 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004881
itayzafrir5c753392018-05-08 11:18:38 +03004882exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004883 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004884 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004885 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004886}
4887/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004888
4889/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004890void verify_hash_fail( int key_type_arg, data_t *key_data,
4891 int alg_arg, data_t *hash_data,
4892 data_t *signature_data,
4893 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004894{
Ronald Cron5425a212020-08-04 14:58:35 +02004895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004896 psa_key_type_t key_type = key_type_arg;
4897 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004898 psa_status_t actual_status;
4899 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004900 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004901
Gilles Peskine8817f612018-12-18 00:18:46 +01004902 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004903
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004904 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004905 psa_set_key_algorithm( &attributes, alg );
4906 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004907
Gilles Peskine049c7532019-05-15 20:22:09 +02004908 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004909 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004910
Ronald Cron5425a212020-08-04 14:58:35 +02004911 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004912 hash_data->x, hash_data->len,
4913 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004914 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004915
4916exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004917 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004918 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004919 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004920}
4921/* END_CASE */
4922
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004923/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004924void sign_message_deterministic( int key_type_arg,
4925 data_t *key_data,
4926 int alg_arg,
4927 data_t *input_data,
4928 data_t *output_data )
4929{
4930 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4931 psa_key_type_t key_type = key_type_arg;
4932 psa_algorithm_t alg = alg_arg;
4933 size_t key_bits;
4934 unsigned char *signature = NULL;
4935 size_t signature_size;
4936 size_t signature_length = 0xdeadbeef;
4937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4938
4939 PSA_ASSERT( psa_crypto_init( ) );
4940
4941 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4942 psa_set_key_algorithm( &attributes, alg );
4943 psa_set_key_type( &attributes, key_type );
4944
4945 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4946 &key ) );
4947 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4948 key_bits = psa_get_key_bits( &attributes );
4949
4950 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4951 TEST_ASSERT( signature_size != 0 );
4952 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4953 ASSERT_ALLOC( signature, signature_size );
4954
4955 PSA_ASSERT( psa_sign_message( key, alg,
4956 input_data->x, input_data->len,
4957 signature, signature_size,
4958 &signature_length ) );
4959
4960 ASSERT_COMPARE( output_data->x, output_data->len,
4961 signature, signature_length );
4962
4963exit:
4964 psa_reset_key_attributes( &attributes );
4965
4966 psa_destroy_key( key );
4967 mbedtls_free( signature );
4968 PSA_DONE( );
4969
4970}
4971/* END_CASE */
4972
4973/* BEGIN_CASE */
4974void sign_message_fail( int key_type_arg,
4975 data_t *key_data,
4976 int alg_arg,
4977 data_t *input_data,
4978 int signature_size_arg,
4979 int expected_status_arg )
4980{
4981 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4982 psa_key_type_t key_type = key_type_arg;
4983 psa_algorithm_t alg = alg_arg;
4984 size_t signature_size = signature_size_arg;
4985 psa_status_t actual_status;
4986 psa_status_t expected_status = expected_status_arg;
4987 unsigned char *signature = NULL;
4988 size_t signature_length = 0xdeadbeef;
4989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4990
4991 ASSERT_ALLOC( signature, signature_size );
4992
4993 PSA_ASSERT( psa_crypto_init( ) );
4994
4995 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4996 psa_set_key_algorithm( &attributes, alg );
4997 psa_set_key_type( &attributes, key_type );
4998
4999 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5000 &key ) );
5001
5002 actual_status = psa_sign_message( key, alg,
5003 input_data->x, input_data->len,
5004 signature, signature_size,
5005 &signature_length );
5006 TEST_EQUAL( actual_status, expected_status );
5007 /* The value of *signature_length is unspecified on error, but
5008 * whatever it is, it should be less than signature_size, so that
5009 * if the caller tries to read *signature_length bytes without
5010 * checking the error code then they don't overflow a buffer. */
5011 TEST_ASSERT( signature_length <= signature_size );
5012
5013exit:
5014 psa_reset_key_attributes( &attributes );
5015 psa_destroy_key( key );
5016 mbedtls_free( signature );
5017 PSA_DONE( );
5018}
5019/* END_CASE */
5020
5021/* BEGIN_CASE */
5022void sign_verify_message( int key_type_arg,
5023 data_t *key_data,
5024 int alg_arg,
5025 data_t *input_data )
5026{
5027 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5028 psa_key_type_t key_type = key_type_arg;
5029 psa_algorithm_t alg = alg_arg;
5030 size_t key_bits;
5031 unsigned char *signature = NULL;
5032 size_t signature_size;
5033 size_t signature_length = 0xdeadbeef;
5034 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5035
5036 PSA_ASSERT( psa_crypto_init( ) );
5037
5038 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5039 PSA_KEY_USAGE_VERIFY_MESSAGE );
5040 psa_set_key_algorithm( &attributes, alg );
5041 psa_set_key_type( &attributes, key_type );
5042
5043 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5044 &key ) );
5045 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5046 key_bits = psa_get_key_bits( &attributes );
5047
5048 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5049 TEST_ASSERT( signature_size != 0 );
5050 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5051 ASSERT_ALLOC( signature, signature_size );
5052
5053 PSA_ASSERT( psa_sign_message( key, alg,
5054 input_data->x, input_data->len,
5055 signature, signature_size,
5056 &signature_length ) );
5057 TEST_ASSERT( signature_length <= signature_size );
5058 TEST_ASSERT( signature_length > 0 );
5059
5060 PSA_ASSERT( psa_verify_message( key, alg,
5061 input_data->x, input_data->len,
5062 signature, signature_length ) );
5063
5064 if( input_data->len != 0 )
5065 {
5066 /* Flip a bit in the input and verify that the signature is now
5067 * detected as invalid. Flip a bit at the beginning, not at the end,
5068 * because ECDSA may ignore the last few bits of the input. */
5069 input_data->x[0] ^= 1;
5070 TEST_EQUAL( psa_verify_message( key, alg,
5071 input_data->x, input_data->len,
5072 signature, signature_length ),
5073 PSA_ERROR_INVALID_SIGNATURE );
5074 }
5075
5076exit:
5077 psa_reset_key_attributes( &attributes );
5078
5079 psa_destroy_key( key );
5080 mbedtls_free( signature );
5081 PSA_DONE( );
5082}
5083/* END_CASE */
5084
5085/* BEGIN_CASE */
5086void verify_message( int key_type_arg,
5087 data_t *key_data,
5088 int alg_arg,
5089 data_t *input_data,
5090 data_t *signature_data )
5091{
5092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5093 psa_key_type_t key_type = key_type_arg;
5094 psa_algorithm_t alg = alg_arg;
5095 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5096
5097 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5098
5099 PSA_ASSERT( psa_crypto_init( ) );
5100
5101 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5102 psa_set_key_algorithm( &attributes, alg );
5103 psa_set_key_type( &attributes, key_type );
5104
5105 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5106 &key ) );
5107
5108 PSA_ASSERT( psa_verify_message( key, alg,
5109 input_data->x, input_data->len,
5110 signature_data->x, signature_data->len ) );
5111
5112exit:
5113 psa_reset_key_attributes( &attributes );
5114 psa_destroy_key( key );
5115 PSA_DONE( );
5116}
5117/* END_CASE */
5118
5119/* BEGIN_CASE */
5120void verify_message_fail( int key_type_arg,
5121 data_t *key_data,
5122 int alg_arg,
5123 data_t *hash_data,
5124 data_t *signature_data,
5125 int expected_status_arg )
5126{
5127 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5128 psa_key_type_t key_type = key_type_arg;
5129 psa_algorithm_t alg = alg_arg;
5130 psa_status_t actual_status;
5131 psa_status_t expected_status = expected_status_arg;
5132 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5133
5134 PSA_ASSERT( psa_crypto_init( ) );
5135
5136 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5137 psa_set_key_algorithm( &attributes, alg );
5138 psa_set_key_type( &attributes, key_type );
5139
5140 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5141 &key ) );
5142
5143 actual_status = psa_verify_message( key, alg,
5144 hash_data->x, hash_data->len,
5145 signature_data->x,
5146 signature_data->len );
5147 TEST_EQUAL( actual_status, expected_status );
5148
5149exit:
5150 psa_reset_key_attributes( &attributes );
5151 psa_destroy_key( key );
5152 PSA_DONE( );
5153}
5154/* END_CASE */
5155
5156/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005157void asymmetric_encrypt( int key_type_arg,
5158 data_t *key_data,
5159 int alg_arg,
5160 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005161 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005162 int expected_output_length_arg,
5163 int expected_status_arg )
5164{
Ronald Cron5425a212020-08-04 14:58:35 +02005165 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005166 psa_key_type_t key_type = key_type_arg;
5167 psa_algorithm_t alg = alg_arg;
5168 size_t expected_output_length = expected_output_length_arg;
5169 size_t key_bits;
5170 unsigned char *output = NULL;
5171 size_t output_size;
5172 size_t output_length = ~0;
5173 psa_status_t actual_status;
5174 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005176
Gilles Peskine8817f612018-12-18 00:18:46 +01005177 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005178
Gilles Peskine656896e2018-06-29 19:12:28 +02005179 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005180 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5181 psa_set_key_algorithm( &attributes, alg );
5182 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005183 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005184 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005185
5186 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005187 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005188 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005189
Gilles Peskine656896e2018-06-29 19:12:28 +02005190 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005191 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005192 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005193
5194 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005195 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005196 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005197 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005198 output, output_size,
5199 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005200 TEST_EQUAL( actual_status, expected_status );
5201 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005202
Gilles Peskine68428122018-06-30 18:42:41 +02005203 /* If the label is empty, the test framework puts a non-null pointer
5204 * in label->x. Test that a null pointer works as well. */
5205 if( label->len == 0 )
5206 {
5207 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005208 if( output_size != 0 )
5209 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005210 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005211 input_data->x, input_data->len,
5212 NULL, label->len,
5213 output, output_size,
5214 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005215 TEST_EQUAL( actual_status, expected_status );
5216 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005217 }
5218
Gilles Peskine656896e2018-06-29 19:12:28 +02005219exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005220 /*
5221 * Key attributes may have been returned by psa_get_key_attributes()
5222 * thus reset them as required.
5223 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005224 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005225
Ronald Cron5425a212020-08-04 14:58:35 +02005226 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005227 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005228 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005229}
5230/* END_CASE */
5231
5232/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005233void asymmetric_encrypt_decrypt( int key_type_arg,
5234 data_t *key_data,
5235 int alg_arg,
5236 data_t *input_data,
5237 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005238{
Ronald Cron5425a212020-08-04 14:58:35 +02005239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005240 psa_key_type_t key_type = key_type_arg;
5241 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005242 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005243 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005244 size_t output_size;
5245 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005246 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005247 size_t output2_size;
5248 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005250
Gilles Peskine8817f612018-12-18 00:18:46 +01005251 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005252
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005253 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5254 psa_set_key_algorithm( &attributes, alg );
5255 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005256
Gilles Peskine049c7532019-05-15 20:22:09 +02005257 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005258 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005259
5260 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005261 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005262 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005263
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005264 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005265 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005266 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005267
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005268 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005269 TEST_ASSERT( output2_size <=
5270 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5271 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005272 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005273
Gilles Peskineeebd7382018-06-08 18:11:54 +02005274 /* We test encryption by checking that encrypt-then-decrypt gives back
5275 * the original plaintext because of the non-optional random
5276 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005277 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005278 input_data->x, input_data->len,
5279 label->x, label->len,
5280 output, output_size,
5281 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005282 /* We don't know what ciphertext length to expect, but check that
5283 * it looks sensible. */
5284 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005285
Ronald Cron5425a212020-08-04 14:58:35 +02005286 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005287 output, output_length,
5288 label->x, label->len,
5289 output2, output2_size,
5290 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005291 ASSERT_COMPARE( input_data->x, input_data->len,
5292 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005293
5294exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005295 /*
5296 * Key attributes may have been returned by psa_get_key_attributes()
5297 * thus reset them as required.
5298 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005299 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005300
Ronald Cron5425a212020-08-04 14:58:35 +02005301 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005302 mbedtls_free( output );
5303 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005304 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005305}
5306/* END_CASE */
5307
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005308/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005309void asymmetric_decrypt( int key_type_arg,
5310 data_t *key_data,
5311 int alg_arg,
5312 data_t *input_data,
5313 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005314 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005315{
Ronald Cron5425a212020-08-04 14:58:35 +02005316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005317 psa_key_type_t key_type = key_type_arg;
5318 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005319 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005320 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005321 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005322 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005323 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005324
Gilles Peskine8817f612018-12-18 00:18:46 +01005325 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005326
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005327 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5328 psa_set_key_algorithm( &attributes, alg );
5329 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005330
Gilles Peskine049c7532019-05-15 20:22:09 +02005331 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005332 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005333
gabor-mezei-armceface22021-01-21 12:26:17 +01005334 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5335 key_bits = psa_get_key_bits( &attributes );
5336
5337 /* Determine the maximum ciphertext length */
5338 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5339 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5340 ASSERT_ALLOC( output, output_size );
5341
Ronald Cron5425a212020-08-04 14:58:35 +02005342 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005343 input_data->x, input_data->len,
5344 label->x, label->len,
5345 output,
5346 output_size,
5347 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005348 ASSERT_COMPARE( expected_data->x, expected_data->len,
5349 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005350
Gilles Peskine68428122018-06-30 18:42:41 +02005351 /* If the label is empty, the test framework puts a non-null pointer
5352 * in label->x. Test that a null pointer works as well. */
5353 if( label->len == 0 )
5354 {
5355 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005356 if( output_size != 0 )
5357 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005358 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005359 input_data->x, input_data->len,
5360 NULL, label->len,
5361 output,
5362 output_size,
5363 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005364 ASSERT_COMPARE( expected_data->x, expected_data->len,
5365 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005366 }
5367
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005368exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005369 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005370 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005371 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005372 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005373}
5374/* END_CASE */
5375
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005376/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005377void asymmetric_decrypt_fail( int key_type_arg,
5378 data_t *key_data,
5379 int alg_arg,
5380 data_t *input_data,
5381 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005382 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005383 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005384{
Ronald Cron5425a212020-08-04 14:58:35 +02005385 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005386 psa_key_type_t key_type = key_type_arg;
5387 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005388 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005389 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005390 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005391 psa_status_t actual_status;
5392 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005394
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005395 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005396
Gilles Peskine8817f612018-12-18 00:18:46 +01005397 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005398
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005399 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5400 psa_set_key_algorithm( &attributes, alg );
5401 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005402
Gilles Peskine049c7532019-05-15 20:22:09 +02005403 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005404 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005405
Ronald Cron5425a212020-08-04 14:58:35 +02005406 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005407 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005408 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005409 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005410 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005411 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005412 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005413
Gilles Peskine68428122018-06-30 18:42:41 +02005414 /* If the label is empty, the test framework puts a non-null pointer
5415 * in label->x. Test that a null pointer works as well. */
5416 if( label->len == 0 )
5417 {
5418 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005419 if( output_size != 0 )
5420 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005421 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005422 input_data->x, input_data->len,
5423 NULL, label->len,
5424 output, output_size,
5425 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005426 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005427 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005428 }
5429
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005430exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005431 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005432 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005433 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005434 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005435}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005436/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005437
5438/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005439void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005440{
5441 /* Test each valid way of initializing the object, except for `= {0}`, as
5442 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5443 * though it's OK by the C standard. We could test for this, but we'd need
5444 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005445 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005446 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5447 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5448 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005449
5450 memset( &zero, 0, sizeof( zero ) );
5451
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005453 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005454 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005455 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005456 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005457 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005458 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005459
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005460 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005461 PSA_ASSERT( psa_key_derivation_abort(&func) );
5462 PSA_ASSERT( psa_key_derivation_abort(&init) );
5463 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005464}
5465/* END_CASE */
5466
Janos Follath16de4a42019-06-13 16:32:24 +01005467/* BEGIN_CASE */
5468void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005469{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005470 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005471 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005472 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005473
Gilles Peskine8817f612018-12-18 00:18:46 +01005474 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005475
Janos Follath16de4a42019-06-13 16:32:24 +01005476 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005477 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005478
5479exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005480 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005481 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005482}
5483/* END_CASE */
5484
Janos Follathaf3c2a02019-06-12 12:34:34 +01005485/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005486void derive_set_capacity( int alg_arg, int capacity_arg,
5487 int expected_status_arg )
5488{
5489 psa_algorithm_t alg = alg_arg;
5490 size_t capacity = capacity_arg;
5491 psa_status_t expected_status = expected_status_arg;
5492 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5493
5494 PSA_ASSERT( psa_crypto_init( ) );
5495
5496 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5497
5498 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5499 expected_status );
5500
5501exit:
5502 psa_key_derivation_abort( &operation );
5503 PSA_DONE( );
5504}
5505/* END_CASE */
5506
5507/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005508void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005509 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005510 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005511 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005512 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005513 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005514 int expected_status_arg3,
5515 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005516{
5517 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005518 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5519 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005520 psa_status_t expected_statuses[] = {expected_status_arg1,
5521 expected_status_arg2,
5522 expected_status_arg3};
5523 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005524 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5525 MBEDTLS_SVC_KEY_ID_INIT,
5526 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005527 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5529 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005530 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005531 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005532 psa_status_t expected_output_status = expected_output_status_arg;
5533 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005534
5535 PSA_ASSERT( psa_crypto_init( ) );
5536
5537 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5538 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005539
5540 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5541
5542 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5543 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005544 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005545 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005546 psa_set_key_type( &attributes, key_types[i] );
5547 PSA_ASSERT( psa_import_key( &attributes,
5548 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005549 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005550 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5551 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5552 {
5553 // When taking a private key as secret input, use key agreement
5554 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005555 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5556 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005557 expected_statuses[i] );
5558 }
5559 else
5560 {
5561 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005562 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005563 expected_statuses[i] );
5564 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005565 }
5566 else
5567 {
5568 TEST_EQUAL( psa_key_derivation_input_bytes(
5569 &operation, steps[i],
5570 inputs[i]->x, inputs[i]->len ),
5571 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005572 }
5573 }
5574
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005575 if( output_key_type != PSA_KEY_TYPE_NONE )
5576 {
5577 psa_reset_key_attributes( &attributes );
5578 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5579 psa_set_key_bits( &attributes, 8 );
5580 actual_output_status =
5581 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005582 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005583 }
5584 else
5585 {
5586 uint8_t buffer[1];
5587 actual_output_status =
5588 psa_key_derivation_output_bytes( &operation,
5589 buffer, sizeof( buffer ) );
5590 }
5591 TEST_EQUAL( actual_output_status, expected_output_status );
5592
Janos Follathaf3c2a02019-06-12 12:34:34 +01005593exit:
5594 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005595 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5596 psa_destroy_key( keys[i] );
5597 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005598 PSA_DONE( );
5599}
5600/* END_CASE */
5601
Janos Follathd958bb72019-07-03 15:02:16 +01005602/* BEGIN_CASE */
5603void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005604{
Janos Follathd958bb72019-07-03 15:02:16 +01005605 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005606 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005607 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005608 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005609 unsigned char input1[] = "Input 1";
5610 size_t input1_length = sizeof( input1 );
5611 unsigned char input2[] = "Input 2";
5612 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005613 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005614 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005615 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5616 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5617 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005618 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005619
Gilles Peskine8817f612018-12-18 00:18:46 +01005620 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005621
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005622 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5623 psa_set_key_algorithm( &attributes, alg );
5624 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005625
Gilles Peskine73676cb2019-05-15 20:15:10 +02005626 PSA_ASSERT( psa_import_key( &attributes,
5627 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005628 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005629
5630 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005631 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5632 input1, input1_length,
5633 input2, input2_length,
5634 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005635 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005636
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005637 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005638 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005639 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005640
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005641 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005642
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005643 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005644 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005645
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005646exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005647 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005648 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005649 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005650}
5651/* END_CASE */
5652
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005653/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005654void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005655{
5656 uint8_t output_buffer[16];
5657 size_t buffer_size = 16;
5658 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005659 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005660
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005661 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5662 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005663 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005664
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005665 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005666 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005667
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005668 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005669
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005670 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5671 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005672 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005673
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005674 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005675 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005676
5677exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005678 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005679}
5680/* END_CASE */
5681
5682/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005683void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005684 int step1_arg, data_t *input1,
5685 int step2_arg, data_t *input2,
5686 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005687 int requested_capacity_arg,
5688 data_t *expected_output1,
5689 data_t *expected_output2 )
5690{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005691 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005692 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5693 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005694 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5695 MBEDTLS_SVC_KEY_ID_INIT,
5696 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005697 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005698 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005699 uint8_t *expected_outputs[2] =
5700 {expected_output1->x, expected_output2->x};
5701 size_t output_sizes[2] =
5702 {expected_output1->len, expected_output2->len};
5703 size_t output_buffer_size = 0;
5704 uint8_t *output_buffer = NULL;
5705 size_t expected_capacity;
5706 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005708 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005709 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005710
5711 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5712 {
5713 if( output_sizes[i] > output_buffer_size )
5714 output_buffer_size = output_sizes[i];
5715 if( output_sizes[i] == 0 )
5716 expected_outputs[i] = NULL;
5717 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005718 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005720
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005721 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5722 psa_set_key_algorithm( &attributes, alg );
5723 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005724
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005725 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005726 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5727 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5728 requested_capacity ) );
5729 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005730 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005731 switch( steps[i] )
5732 {
5733 case 0:
5734 break;
5735 case PSA_KEY_DERIVATION_INPUT_SECRET:
5736 PSA_ASSERT( psa_import_key( &attributes,
5737 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005738 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005739
5740 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5741 {
5742 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5743 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5744 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5745 }
5746
Gilles Peskine1468da72019-05-29 17:35:49 +02005747 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005748 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005749 break;
5750 default:
5751 PSA_ASSERT( psa_key_derivation_input_bytes(
5752 &operation, steps[i],
5753 inputs[i]->x, inputs[i]->len ) );
5754 break;
5755 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005756 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005757
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005758 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005759 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005760 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005761 expected_capacity = requested_capacity;
5762
5763 /* Expansion phase. */
5764 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5765 {
5766 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005767 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005768 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005769 if( expected_capacity == 0 && output_sizes[i] == 0 )
5770 {
5771 /* Reading 0 bytes when 0 bytes are available can go either way. */
5772 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005773 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005774 continue;
5775 }
5776 else if( expected_capacity == 0 ||
5777 output_sizes[i] > expected_capacity )
5778 {
5779 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005780 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005781 expected_capacity = 0;
5782 continue;
5783 }
5784 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005785 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005786 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005787 ASSERT_COMPARE( output_buffer, output_sizes[i],
5788 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005789 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005790 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005791 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005792 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005793 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005794 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005795 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005796
5797exit:
5798 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005799 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005800 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5801 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005802 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005803}
5804/* END_CASE */
5805
5806/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005807void derive_full( int alg_arg,
5808 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005809 data_t *input1,
5810 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005811 int requested_capacity_arg )
5812{
Ronald Cron5425a212020-08-04 14:58:35 +02005813 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005814 psa_algorithm_t alg = alg_arg;
5815 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005816 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005817 unsigned char output_buffer[16];
5818 size_t expected_capacity = requested_capacity;
5819 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005821
Gilles Peskine8817f612018-12-18 00:18:46 +01005822 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005823
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005824 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5825 psa_set_key_algorithm( &attributes, alg );
5826 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005827
Gilles Peskine049c7532019-05-15 20:22:09 +02005828 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005829 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005830
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005831 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5832 input1->x, input1->len,
5833 input2->x, input2->len,
5834 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005835 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005836
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005837 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005838 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005839 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005840
5841 /* Expansion phase. */
5842 while( current_capacity > 0 )
5843 {
5844 size_t read_size = sizeof( output_buffer );
5845 if( read_size > current_capacity )
5846 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005847 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005848 output_buffer,
5849 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005850 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005851 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005852 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005853 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005854 }
5855
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005856 /* Check that the operation refuses to go over capacity. */
5857 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005858 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005859
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005860 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005861
5862exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005863 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005864 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005865 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005866}
5867/* END_CASE */
5868
Janos Follathe60c9052019-07-03 13:51:30 +01005869/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005870void derive_key_exercise( int alg_arg,
5871 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005872 data_t *input1,
5873 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005874 int derived_type_arg,
5875 int derived_bits_arg,
5876 int derived_usage_arg,
5877 int derived_alg_arg )
5878{
Ronald Cron5425a212020-08-04 14:58:35 +02005879 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5880 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005881 psa_algorithm_t alg = alg_arg;
5882 psa_key_type_t derived_type = derived_type_arg;
5883 size_t derived_bits = derived_bits_arg;
5884 psa_key_usage_t derived_usage = derived_usage_arg;
5885 psa_algorithm_t derived_alg = derived_alg_arg;
5886 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005887 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005888 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005889 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005890
Gilles Peskine8817f612018-12-18 00:18:46 +01005891 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005892
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005893 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5894 psa_set_key_algorithm( &attributes, alg );
5895 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005896 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005897 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005898
5899 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005900 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5901 input1->x, input1->len,
5902 input2->x, input2->len,
5903 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005904 goto exit;
5905
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005906 psa_set_key_usage_flags( &attributes, derived_usage );
5907 psa_set_key_algorithm( &attributes, derived_alg );
5908 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005909 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005910 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005911 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005912
5913 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005914 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005915 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5916 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005917
5918 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005919 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005920 goto exit;
5921
5922exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005923 /*
5924 * Key attributes may have been returned by psa_get_key_attributes()
5925 * thus reset them as required.
5926 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005927 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005928
5929 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005930 psa_destroy_key( base_key );
5931 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005932 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005933}
5934/* END_CASE */
5935
Janos Follath42fd8882019-07-03 14:17:09 +01005936/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005937void derive_key_export( int alg_arg,
5938 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005939 data_t *input1,
5940 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005941 int bytes1_arg,
5942 int bytes2_arg )
5943{
Ronald Cron5425a212020-08-04 14:58:35 +02005944 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5945 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005946 psa_algorithm_t alg = alg_arg;
5947 size_t bytes1 = bytes1_arg;
5948 size_t bytes2 = bytes2_arg;
5949 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005950 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005951 uint8_t *output_buffer = NULL;
5952 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005953 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5954 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005955 size_t length;
5956
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005957 ASSERT_ALLOC( output_buffer, capacity );
5958 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005959 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005960
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005961 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5962 psa_set_key_algorithm( &base_attributes, alg );
5963 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005964 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005965 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005966
5967 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005968 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5969 input1->x, input1->len,
5970 input2->x, input2->len,
5971 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005972 goto exit;
5973
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005974 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005975 output_buffer,
5976 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005977 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005978
5979 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005980 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5981 input1->x, input1->len,
5982 input2->x, input2->len,
5983 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005984 goto exit;
5985
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005986 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5987 psa_set_key_algorithm( &derived_attributes, 0 );
5988 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005989 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005990 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005991 &derived_key ) );
5992 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005993 export_buffer, bytes1,
5994 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005995 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005996 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005997 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005998 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005999 &derived_key ) );
6000 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006001 export_buffer + bytes1, bytes2,
6002 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006003 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006004
6005 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006006 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6007 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006008
6009exit:
6010 mbedtls_free( output_buffer );
6011 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006012 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006013 psa_destroy_key( base_key );
6014 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006015 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006016}
6017/* END_CASE */
6018
6019/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006020void derive_key( int alg_arg,
6021 data_t *key_data, data_t *input1, data_t *input2,
6022 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006023 int expected_status_arg,
6024 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006025{
Ronald Cron5425a212020-08-04 14:58:35 +02006026 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6027 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006028 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006029 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006030 size_t bits = bits_arg;
6031 psa_status_t expected_status = expected_status_arg;
6032 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6033 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6034 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6035
6036 PSA_ASSERT( psa_crypto_init( ) );
6037
6038 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6039 psa_set_key_algorithm( &base_attributes, alg );
6040 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6041 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006042 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006043
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006044 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6045 input1->x, input1->len,
6046 input2->x, input2->len,
6047 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006048 goto exit;
6049
6050 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6051 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006052 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006053 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006054
6055 psa_status_t status =
6056 psa_key_derivation_output_key( &derived_attributes,
6057 &operation,
6058 &derived_key );
6059 if( is_large_output > 0 )
6060 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6061 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006062
6063exit:
6064 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006065 psa_destroy_key( base_key );
6066 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006067 PSA_DONE( );
6068}
6069/* END_CASE */
6070
6071/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006072void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006073 int our_key_type_arg, int our_key_alg_arg,
6074 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006075 int expected_status_arg )
6076{
Ronald Cron5425a212020-08-04 14:58:35 +02006077 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006078 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006079 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006080 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006081 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006083 psa_status_t expected_status = expected_status_arg;
6084 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006085
Gilles Peskine8817f612018-12-18 00:18:46 +01006086 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006087
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006088 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006089 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006090 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006091 PSA_ASSERT( psa_import_key( &attributes,
6092 our_key_data->x, our_key_data->len,
6093 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006094
Gilles Peskine77f40d82019-04-11 21:27:06 +02006095 /* The tests currently include inputs that should fail at either step.
6096 * Test cases that fail at the setup step should be changed to call
6097 * key_derivation_setup instead, and this function should be renamed
6098 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006099 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006100 if( status == PSA_SUCCESS )
6101 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006102 TEST_EQUAL( psa_key_derivation_key_agreement(
6103 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6104 our_key,
6105 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006106 expected_status );
6107 }
6108 else
6109 {
6110 TEST_ASSERT( status == expected_status );
6111 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006112
6113exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006114 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006115 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006116 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006117}
6118/* END_CASE */
6119
6120/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006121void raw_key_agreement( int alg_arg,
6122 int our_key_type_arg, data_t *our_key_data,
6123 data_t *peer_key_data,
6124 data_t *expected_output )
6125{
Ronald Cron5425a212020-08-04 14:58:35 +02006126 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006127 psa_algorithm_t alg = alg_arg;
6128 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006129 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006130 unsigned char *output = NULL;
6131 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006132 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006133
6134 ASSERT_ALLOC( output, expected_output->len );
6135 PSA_ASSERT( psa_crypto_init( ) );
6136
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6138 psa_set_key_algorithm( &attributes, alg );
6139 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006140 PSA_ASSERT( psa_import_key( &attributes,
6141 our_key_data->x, our_key_data->len,
6142 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006143
gabor-mezei-armceface22021-01-21 12:26:17 +01006144 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6145 key_bits = psa_get_key_bits( &attributes );
6146
Gilles Peskinebe697d82019-05-16 18:00:41 +02006147 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6148 peer_key_data->x, peer_key_data->len,
6149 output, expected_output->len,
6150 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006151 ASSERT_COMPARE( output, output_length,
6152 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006153 TEST_ASSERT( output_length <=
6154 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6155 TEST_ASSERT( output_length <=
6156 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006157
6158exit:
6159 mbedtls_free( output );
6160 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006161 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006162}
6163/* END_CASE */
6164
6165/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006166void key_agreement_capacity( int alg_arg,
6167 int our_key_type_arg, data_t *our_key_data,
6168 data_t *peer_key_data,
6169 int expected_capacity_arg )
6170{
Ronald Cron5425a212020-08-04 14:58:35 +02006171 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006172 psa_algorithm_t alg = alg_arg;
6173 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006174 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006176 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006177 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006178
Gilles Peskine8817f612018-12-18 00:18:46 +01006179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006180
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006181 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6182 psa_set_key_algorithm( &attributes, alg );
6183 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006184 PSA_ASSERT( psa_import_key( &attributes,
6185 our_key_data->x, our_key_data->len,
6186 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006187
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006188 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006189 PSA_ASSERT( psa_key_derivation_key_agreement(
6190 &operation,
6191 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6192 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006193 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6194 {
6195 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006196 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006197 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006198 NULL, 0 ) );
6199 }
Gilles Peskine59685592018-09-18 12:11:34 +02006200
Gilles Peskinebf491972018-10-25 22:36:12 +02006201 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006202 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006203 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006204 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006205
Gilles Peskinebf491972018-10-25 22:36:12 +02006206 /* Test the actual capacity by reading the output. */
6207 while( actual_capacity > sizeof( output ) )
6208 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006209 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006210 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006211 actual_capacity -= sizeof( output );
6212 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006213 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006214 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006215 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006216 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006217
Gilles Peskine59685592018-09-18 12:11:34 +02006218exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006219 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006220 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006221 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006222}
6223/* END_CASE */
6224
6225/* BEGIN_CASE */
6226void key_agreement_output( int alg_arg,
6227 int our_key_type_arg, data_t *our_key_data,
6228 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006229 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006230{
Ronald Cron5425a212020-08-04 14:58:35 +02006231 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006232 psa_algorithm_t alg = alg_arg;
6233 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006234 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006236 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006237
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006238 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6239 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006240
Gilles Peskine8817f612018-12-18 00:18:46 +01006241 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006242
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006243 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6244 psa_set_key_algorithm( &attributes, alg );
6245 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006246 PSA_ASSERT( psa_import_key( &attributes,
6247 our_key_data->x, our_key_data->len,
6248 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006249
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006250 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006251 PSA_ASSERT( psa_key_derivation_key_agreement(
6252 &operation,
6253 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6254 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006255 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6256 {
6257 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006258 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006259 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006260 NULL, 0 ) );
6261 }
Gilles Peskine59685592018-09-18 12:11:34 +02006262
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006263 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006264 actual_output,
6265 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006266 ASSERT_COMPARE( actual_output, expected_output1->len,
6267 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006268 if( expected_output2->len != 0 )
6269 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006270 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006271 actual_output,
6272 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006273 ASSERT_COMPARE( actual_output, expected_output2->len,
6274 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006275 }
Gilles Peskine59685592018-09-18 12:11:34 +02006276
6277exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006278 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006279 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006280 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006281 mbedtls_free( actual_output );
6282}
6283/* END_CASE */
6284
6285/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006286void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006287{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006288 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006289 unsigned char *output = NULL;
6290 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006291 size_t i;
6292 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006293
Simon Butcher49f8e312020-03-03 15:51:50 +00006294 TEST_ASSERT( bytes_arg >= 0 );
6295
Gilles Peskine91892022021-02-08 19:50:26 +01006296 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006297 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006298
Gilles Peskine8817f612018-12-18 00:18:46 +01006299 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006300
Gilles Peskinea50d7392018-06-21 10:22:13 +02006301 /* Run several times, to ensure that every output byte will be
6302 * nonzero at least once with overwhelming probability
6303 * (2^(-8*number_of_runs)). */
6304 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006305 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006306 if( bytes != 0 )
6307 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006308 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006309
Gilles Peskinea50d7392018-06-21 10:22:13 +02006310 for( i = 0; i < bytes; i++ )
6311 {
6312 if( output[i] != 0 )
6313 ++changed[i];
6314 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006315 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006316
6317 /* Check that every byte was changed to nonzero at least once. This
6318 * validates that psa_generate_random is overwriting every byte of
6319 * the output buffer. */
6320 for( i = 0; i < bytes; i++ )
6321 {
6322 TEST_ASSERT( changed[i] != 0 );
6323 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006324
6325exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006326 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006327 mbedtls_free( output );
6328 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006329}
6330/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006331
6332/* BEGIN_CASE */
6333void generate_key( int type_arg,
6334 int bits_arg,
6335 int usage_arg,
6336 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006337 int expected_status_arg,
6338 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006339{
Ronald Cron5425a212020-08-04 14:58:35 +02006340 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006341 psa_key_type_t type = type_arg;
6342 psa_key_usage_t usage = usage_arg;
6343 size_t bits = bits_arg;
6344 psa_algorithm_t alg = alg_arg;
6345 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006347 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006348
Gilles Peskine8817f612018-12-18 00:18:46 +01006349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006350
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006351 psa_set_key_usage_flags( &attributes, usage );
6352 psa_set_key_algorithm( &attributes, alg );
6353 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006354 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006355
6356 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006357 psa_status_t status = psa_generate_key( &attributes, &key );
6358
6359 if( is_large_key > 0 )
6360 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6361 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006362 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006363 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006364
6365 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006366 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006367 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6368 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006369
Gilles Peskine818ca122018-06-20 18:16:48 +02006370 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006371 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006372 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006373
6374exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006375 /*
6376 * Key attributes may have been returned by psa_get_key_attributes()
6377 * thus reset them as required.
6378 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006379 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006380
Ronald Cron5425a212020-08-04 14:58:35 +02006381 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006382 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006383}
6384/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006385
Ronald Cronee414c72021-03-18 18:50:08 +01006386/* 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 +02006387void generate_key_rsa( int bits_arg,
6388 data_t *e_arg,
6389 int expected_status_arg )
6390{
Ronald Cron5425a212020-08-04 14:58:35 +02006391 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006392 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006393 size_t bits = bits_arg;
6394 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6395 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6396 psa_status_t expected_status = expected_status_arg;
6397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6398 uint8_t *exported = NULL;
6399 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006400 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006401 size_t exported_length = SIZE_MAX;
6402 uint8_t *e_read_buffer = NULL;
6403 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006404 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006405 size_t e_read_length = SIZE_MAX;
6406
6407 if( e_arg->len == 0 ||
6408 ( e_arg->len == 3 &&
6409 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6410 {
6411 is_default_public_exponent = 1;
6412 e_read_size = 0;
6413 }
6414 ASSERT_ALLOC( e_read_buffer, e_read_size );
6415 ASSERT_ALLOC( exported, exported_size );
6416
6417 PSA_ASSERT( psa_crypto_init( ) );
6418
6419 psa_set_key_usage_flags( &attributes, usage );
6420 psa_set_key_algorithm( &attributes, alg );
6421 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6422 e_arg->x, e_arg->len ) );
6423 psa_set_key_bits( &attributes, bits );
6424
6425 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006426 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006427 if( expected_status != PSA_SUCCESS )
6428 goto exit;
6429
6430 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006431 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006432 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6433 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6434 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6435 e_read_buffer, e_read_size,
6436 &e_read_length ) );
6437 if( is_default_public_exponent )
6438 TEST_EQUAL( e_read_length, 0 );
6439 else
6440 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6441
6442 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006443 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006444 goto exit;
6445
6446 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006447 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006448 exported, exported_size,
6449 &exported_length ) );
6450 {
6451 uint8_t *p = exported;
6452 uint8_t *end = exported + exported_length;
6453 size_t len;
6454 /* RSAPublicKey ::= SEQUENCE {
6455 * modulus INTEGER, -- n
6456 * publicExponent INTEGER } -- e
6457 */
6458 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006459 MBEDTLS_ASN1_SEQUENCE |
6460 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006461 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006462 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6463 MBEDTLS_ASN1_INTEGER ) );
6464 if( len >= 1 && p[0] == 0 )
6465 {
6466 ++p;
6467 --len;
6468 }
6469 if( e_arg->len == 0 )
6470 {
6471 TEST_EQUAL( len, 3 );
6472 TEST_EQUAL( p[0], 1 );
6473 TEST_EQUAL( p[1], 0 );
6474 TEST_EQUAL( p[2], 1 );
6475 }
6476 else
6477 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6478 }
6479
6480exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006481 /*
6482 * Key attributes may have been returned by psa_get_key_attributes() or
6483 * set by psa_set_key_domain_parameters() thus reset them as required.
6484 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006485 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006486
Ronald Cron5425a212020-08-04 14:58:35 +02006487 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006488 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006489 mbedtls_free( e_read_buffer );
6490 mbedtls_free( exported );
6491}
6492/* END_CASE */
6493
Darryl Greend49a4992018-06-18 17:27:26 +01006494/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006495void persistent_key_load_key_from_storage( data_t *data,
6496 int type_arg, int bits_arg,
6497 int usage_flags_arg, int alg_arg,
6498 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006499{
Ronald Cron71016a92020-08-28 19:01:50 +02006500 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006502 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6503 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006504 psa_key_type_t type = type_arg;
6505 size_t bits = bits_arg;
6506 psa_key_usage_t usage_flags = usage_flags_arg;
6507 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006508 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006509 unsigned char *first_export = NULL;
6510 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006511 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006512 size_t first_exported_length;
6513 size_t second_exported_length;
6514
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006515 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6516 {
6517 ASSERT_ALLOC( first_export, export_size );
6518 ASSERT_ALLOC( second_export, export_size );
6519 }
Darryl Greend49a4992018-06-18 17:27:26 +01006520
Gilles Peskine8817f612018-12-18 00:18:46 +01006521 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006522
Gilles Peskinec87af662019-05-15 16:12:22 +02006523 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006524 psa_set_key_usage_flags( &attributes, usage_flags );
6525 psa_set_key_algorithm( &attributes, alg );
6526 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006527 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006528
Darryl Green0c6575a2018-11-07 16:05:30 +00006529 switch( generation_method )
6530 {
6531 case IMPORT_KEY:
6532 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006533 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006534 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006535 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006536
Darryl Green0c6575a2018-11-07 16:05:30 +00006537 case GENERATE_KEY:
6538 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006539 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006540 break;
6541
6542 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006543#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006544 {
6545 /* Create base key */
6546 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6547 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6548 psa_set_key_usage_flags( &base_attributes,
6549 PSA_KEY_USAGE_DERIVE );
6550 psa_set_key_algorithm( &base_attributes, derive_alg );
6551 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006552 PSA_ASSERT( psa_import_key( &base_attributes,
6553 data->x, data->len,
6554 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006555 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006556 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006557 PSA_ASSERT( psa_key_derivation_input_key(
6558 &operation,
6559 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006560 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006561 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006562 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006563 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6564 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006565 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006566 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006567 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006568 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006569 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006570#else
6571 TEST_ASSUME( ! "KDF not supported in this configuration" );
6572#endif
6573 break;
6574
6575 default:
6576 TEST_ASSERT( ! "generation_method not implemented in test" );
6577 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006578 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006579 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006580
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006581 /* Export the key if permitted by the key policy. */
6582 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6583 {
Ronald Cron5425a212020-08-04 14:58:35 +02006584 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006585 first_export, export_size,
6586 &first_exported_length ) );
6587 if( generation_method == IMPORT_KEY )
6588 ASSERT_COMPARE( data->x, data->len,
6589 first_export, first_exported_length );
6590 }
Darryl Greend49a4992018-06-18 17:27:26 +01006591
6592 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006593 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006594 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006595 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006596
Darryl Greend49a4992018-06-18 17:27:26 +01006597 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006598 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006599 TEST_ASSERT( mbedtls_svc_key_id_equal(
6600 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006601 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6602 PSA_KEY_LIFETIME_PERSISTENT );
6603 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6604 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6605 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6606 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006607
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006608 /* Export the key again if permitted by the key policy. */
6609 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006610 {
Ronald Cron5425a212020-08-04 14:58:35 +02006611 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006612 second_export, export_size,
6613 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006614 ASSERT_COMPARE( first_export, first_exported_length,
6615 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006616 }
6617
6618 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006619 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006620 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006621
6622exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006623 /*
6624 * Key attributes may have been returned by psa_get_key_attributes()
6625 * thus reset them as required.
6626 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006627 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006628
Darryl Greend49a4992018-06-18 17:27:26 +01006629 mbedtls_free( first_export );
6630 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006631 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006632 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006633 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006634 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006635}
6636/* END_CASE */