blob: fa579e45ad8a6b344e94f04d9023f5a85f83be66 [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.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100282 * \param key_type_arg Type of key passed in
283 * \param key_data The encryption / decryption key data
284 * \param alg_arg The type of algorithm used
285 * \param nonce Nonce data
286 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100287 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100288 * feed additional data in to be encrypted /
289 * decrypted. If -1, no chunking.
290 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100291 * \param data_part_len_arg If not -1, the length of chunks to feed
292 * the data in to be encrypted / decrypted. If
293 * -1, no chunking
294 * \param set_lengths_method A member of the setlengths_method enum is
295 * expected here, this controls whether or not
296 * to set lengths, and in what order with
297 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100298 * \param expected_output Expected output
Paul Elliott41ffae12021-07-22 21:52:01 +0100299 * \param expect_valid_signature If non zero, we expect the signature to be
300 * valid
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100301 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100302 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100303 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100304 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100305 */
306static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
307 int alg_arg,
308 data_t *nonce,
309 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100310 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100311 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100312 int data_part_len_arg,
Paul Elliott33746aa2021-09-15 16:40:40 +0100313 setlengths_method set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100315 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100316 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100317{
318 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
319 psa_key_type_t key_type = key_type_arg;
320 psa_algorithm_t alg = alg_arg;
321 psa_aead_operation_t operation;
322 unsigned char *output_data = NULL;
323 unsigned char *part_data = NULL;
324 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100326 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 size_t output_size = 0;
328 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100329 size_t output_length = 0;
330 size_t key_bits = 0;
331 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100332 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100333 size_t part_length = 0;
334 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100335 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100336 size_t ad_part_len = 0;
337 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
340 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
341
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100343 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100344
Paul Elliottd3f82412021-06-16 16:52:21 +0100345 PSA_ASSERT( psa_crypto_init( ) );
346
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100347 if( is_encrypt )
348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
349 else
350 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
351
Paul Elliottd3f82412021-06-16 16:52:21 +0100352 psa_set_key_algorithm( &attributes, alg );
353 psa_set_key_type( &attributes, key_type );
354
355 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
356 &key ) );
357
358 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
359 key_bits = psa_get_key_bits( &attributes );
360
361 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
362
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100363 if( is_encrypt )
364 {
365 /* Tag gets written at end of buffer. */
366 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
367 ( input_data->len +
368 tag_length ) );
369 data_true_size = input_data->len;
370 }
371 else
372 {
373 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
374 ( input_data->len -
375 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100376
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100377 /* Do not want to attempt to decrypt tag. */
378 data_true_size = input_data->len - tag_length;
379 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100380
381 ASSERT_ALLOC( output_data, output_size );
382
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100383 if( is_encrypt )
384 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100385 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
386 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100387 }
388 else
389 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100390 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
391 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100394 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100395
396 operation = psa_aead_operation_init( );
397
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100398
399 if( is_encrypt )
400 status = psa_aead_encrypt_setup( &operation, key, alg );
401 else
402 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100403
404 /* If the operation is not supported, just skip and not fail in case the
405 * encryption involves a common limitation of cryptography hardwares and
406 * an alternative implementation. */
407 if( status == PSA_ERROR_NOT_SUPPORTED )
408 {
409 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
410 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
411 }
412
413 PSA_ASSERT( status );
414
Paul Elliott33746aa2021-09-15 16:40:40 +0100415 if( set_lengths_method == DO_NOT_SET_LENGTHS )
416 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
417 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100418 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100419 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
420 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100421 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
422 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100423 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100424 {
425 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
426
Paul Elliott33746aa2021-09-15 16:40:40 +0100427 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
428 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100429 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100430
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100431 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100432 {
433 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100434 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100435
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100436 for( part_offset = 0, part_count = 0;
437 part_offset < additional_data->len;
438 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100440 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100441 {
Paul Elliott329d5382021-07-22 17:10:45 +0100442 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100443 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100444 else if( additional_data->len - part_offset < ad_part_len )
445 {
446 part_length = additional_data->len - part_offset;
447 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100448 else
449 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100450 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 }
452
453 PSA_ASSERT( psa_aead_update_ad( &operation,
454 additional_data->x + part_offset,
455 part_length ) );
456
Paul Elliottd3f82412021-06-16 16:52:21 +0100457 }
458 }
459 else
460 {
461 /* Pass additional data in one go. */
462 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
463 additional_data->len ) );
464 }
465
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100466 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 {
468 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100469 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100470 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100471 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100472
473 ASSERT_ALLOC( part_data, part_data_size );
474
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100475 for( part_offset = 0, part_count = 0;
476 part_offset < data_true_size;
477 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100479 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 {
Paul Elliott329d5382021-07-22 17:10:45 +0100481 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100482 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100483 else if( ( data_true_size - part_offset ) < data_part_len )
484 {
485 part_length = ( data_true_size - part_offset );
486 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100487 else
488 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100489 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 }
491
492 PSA_ASSERT( psa_aead_update( &operation,
493 ( input_data->x + part_offset ),
494 part_length, part_data,
495 part_data_size,
496 &output_part_length ) );
497
498 if( output_data && output_part_length )
499 {
500 memcpy( ( output_data + part_offset ), part_data,
501 output_part_length );
502 }
503
Paul Elliottd3f82412021-06-16 16:52:21 +0100504 output_length += output_part_length;
505 }
506 }
507 else
508 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100509 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100510 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100511 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100512 output_size, &output_length ) );
513 }
514
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100515 if( is_encrypt )
516 PSA_ASSERT( psa_aead_finish( &operation, final_data,
517 final_output_size,
518 &output_part_length,
519 tag_buffer, tag_length,
520 &tag_size ) );
521 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 {
Paul Elliott9961a662021-09-17 19:19:02 +0100523 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100524 final_output_size,
525 &output_part_length,
526 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100527 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100528 }
529
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100530 if( output_data && output_part_length )
531 memcpy( ( output_data + output_length ), final_data,
532 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100533
534 output_length += output_part_length;
535
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100536
537 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
538 * should be exact.*/
539 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100540 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100541 TEST_EQUAL( tag_length, tag_size );
542
543 if( output_data && tag_length )
544 memcpy( ( output_data + output_length ), tag_buffer,
545 tag_length );
546
547 output_length += tag_length;
548
549 TEST_EQUAL( output_length,
550 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
551 input_data->len ) );
552 TEST_ASSERT( output_length <=
553 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
554 }
555 else
556 {
557 TEST_EQUAL( output_length,
558 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
559 input_data->len ) );
560 TEST_ASSERT( output_length <=
561 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100562 }
563
Paul Elliottd3f82412021-06-16 16:52:21 +0100564
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100565 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100566 output_data, output_length );
567
Paul Elliottd3f82412021-06-16 16:52:21 +0100568
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100569 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100570
571exit:
572 psa_destroy_key( key );
573 psa_aead_abort( &operation );
574 mbedtls_free( output_data );
575 mbedtls_free( part_data );
576 mbedtls_free( final_data );
577 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100578
579 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100580}
581
Gilles Peskinee59236f2018-01-27 23:32:46 +0100582/* END_HEADER */
583
584/* BEGIN_DEPENDENCIES
585 * depends_on:MBEDTLS_PSA_CRYPTO_C
586 * END_DEPENDENCIES
587 */
588
589/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200590void static_checks( )
591{
592 size_t max_truncated_mac_size =
593 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
594
595 /* Check that the length for a truncated MAC always fits in the algorithm
596 * encoding. The shifted mask is the maximum truncated value. The
597 * untruncated algorithm may be one byte larger. */
598 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
599}
600/* END_CASE */
601
602/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200603void import_with_policy( int type_arg,
604 int usage_arg, int alg_arg,
605 int expected_status_arg )
606{
607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
608 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200609 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200610 psa_key_type_t type = type_arg;
611 psa_key_usage_t usage = usage_arg;
612 psa_algorithm_t alg = alg_arg;
613 psa_status_t expected_status = expected_status_arg;
614 const uint8_t key_material[16] = {0};
615 psa_status_t status;
616
617 PSA_ASSERT( psa_crypto_init( ) );
618
619 psa_set_key_type( &attributes, type );
620 psa_set_key_usage_flags( &attributes, usage );
621 psa_set_key_algorithm( &attributes, alg );
622
623 status = psa_import_key( &attributes,
624 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200625 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200626 TEST_EQUAL( status, expected_status );
627 if( status != PSA_SUCCESS )
628 goto exit;
629
Ronald Cron5425a212020-08-04 14:58:35 +0200630 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200631 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
632 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
633 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200634 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200635
Ronald Cron5425a212020-08-04 14:58:35 +0200636 PSA_ASSERT( psa_destroy_key( key ) );
637 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200638
639exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100640 /*
641 * Key attributes may have been returned by psa_get_key_attributes()
642 * thus reset them as required.
643 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200644 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100645
646 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200647 PSA_DONE( );
648}
649/* END_CASE */
650
651/* BEGIN_CASE */
652void import_with_data( data_t *data, int type_arg,
653 int attr_bits_arg,
654 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200655{
656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
657 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200658 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200659 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200660 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200661 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100662 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663
Gilles Peskine8817f612018-12-18 00:18:46 +0100664 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665
Gilles Peskine4747d192019-04-17 15:05:45 +0200666 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200667 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200668
Ronald Cron5425a212020-08-04 14:58:35 +0200669 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100670 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200671 if( status != PSA_SUCCESS )
672 goto exit;
673
Ronald Cron5425a212020-08-04 14:58:35 +0200674 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200675 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200676 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200677 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200678 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200679
Ronald Cron5425a212020-08-04 14:58:35 +0200680 PSA_ASSERT( psa_destroy_key( key ) );
681 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682
683exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100684 /*
685 * Key attributes may have been returned by psa_get_key_attributes()
686 * thus reset them as required.
687 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200688 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100689
690 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200691 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100692}
693/* END_CASE */
694
695/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200696void import_large_key( int type_arg, int byte_size_arg,
697 int expected_status_arg )
698{
699 psa_key_type_t type = type_arg;
700 size_t byte_size = byte_size_arg;
701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
702 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200704 psa_status_t status;
705 uint8_t *buffer = NULL;
706 size_t buffer_size = byte_size + 1;
707 size_t n;
708
Steven Cooreman69967ce2021-01-18 18:01:08 +0100709 /* Skip the test case if the target running the test cannot
710 * accomodate large keys due to heap size constraints */
711 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200712 memset( buffer, 'K', byte_size );
713
714 PSA_ASSERT( psa_crypto_init( ) );
715
716 /* Try importing the key */
717 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
718 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200719 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100720 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200721 TEST_EQUAL( status, expected_status );
722
723 if( status == PSA_SUCCESS )
724 {
Ronald Cron5425a212020-08-04 14:58:35 +0200725 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200726 TEST_EQUAL( psa_get_key_type( &attributes ), type );
727 TEST_EQUAL( psa_get_key_bits( &attributes ),
728 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200729 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200730 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200731 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200732 for( n = 0; n < byte_size; n++ )
733 TEST_EQUAL( buffer[n], 'K' );
734 for( n = byte_size; n < buffer_size; n++ )
735 TEST_EQUAL( buffer[n], 0 );
736 }
737
738exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100739 /*
740 * Key attributes may have been returned by psa_get_key_attributes()
741 * thus reset them as required.
742 */
743 psa_reset_key_attributes( &attributes );
744
Ronald Cron5425a212020-08-04 14:58:35 +0200745 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200746 PSA_DONE( );
747 mbedtls_free( buffer );
748}
749/* END_CASE */
750
751/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200752void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
753{
Ronald Cron5425a212020-08-04 14:58:35 +0200754 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200755 size_t bits = bits_arg;
756 psa_status_t expected_status = expected_status_arg;
757 psa_status_t status;
758 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200759 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200760 size_t buffer_size = /* Slight overapproximations */
761 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200762 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763 unsigned char *p;
764 int ret;
765 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200767
Gilles Peskine8817f612018-12-18 00:18:46 +0100768 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200769 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200770
771 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
772 bits, keypair ) ) >= 0 );
773 length = ret;
774
775 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200776 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200777 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100778 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200779
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200780 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200781 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200782
783exit:
784 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200785 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200786}
787/* END_CASE */
788
789/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300790void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300791 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200792 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100793 int expected_bits,
794 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200795 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100796 int canonical_input )
797{
Ronald Cron5425a212020-08-04 14:58:35 +0200798 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100799 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200800 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200801 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100802 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803 unsigned char *exported = NULL;
804 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100806 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100807 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200808 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200809 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100810
Moran Pekercb088e72018-07-17 17:36:59 +0300811 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200812 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100813 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200814 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100815 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816
Gilles Peskine4747d192019-04-17 15:05:45 +0200817 psa_set_key_usage_flags( &attributes, usage_arg );
818 psa_set_key_algorithm( &attributes, alg );
819 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700820
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200822 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823
824 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200825 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200826 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
827 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200828 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829
830 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200831 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100832 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100833
834 /* The exported length must be set by psa_export_key() to a value between 0
835 * and export_size. On errors, the exported length must be 0. */
836 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
837 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
838 TEST_ASSERT( exported_length <= export_size );
839
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200840 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200841 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100842 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200843 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100844 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200846 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100847
Gilles Peskineea38a922021-02-13 00:05:16 +0100848 /* Run sanity checks on the exported key. For non-canonical inputs,
849 * this validates the canonical representations. For canonical inputs,
850 * this doesn't directly validate the implementation, but it still helps
851 * by cross-validating the test data with the sanity check code. */
852 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200853 goto exit;
854
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200856 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100857 else
858 {
Ronald Cron5425a212020-08-04 14:58:35 +0200859 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200860 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200861 &key2 ) );
862 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100863 reexported,
864 export_size,
865 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200866 ASSERT_COMPARE( exported, exported_length,
867 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200868 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100869 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100870 TEST_ASSERT( exported_length <=
871 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
872 psa_get_key_bits( &got_attributes ) ) );
873 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874
875destroy:
876 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200877 PSA_ASSERT( psa_destroy_key( key ) );
878 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100879
880exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100881 /*
882 * Key attributes may have been returned by psa_get_key_attributes()
883 * thus reset them as required.
884 */
885 psa_reset_key_attributes( &got_attributes );
886
itayzafrir3e02b3b2018-06-12 17:06:52 +0300887 mbedtls_free( exported );
888 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200889 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890}
891/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100892
Moran Pekerf709f4a2018-06-06 17:26:04 +0300893/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300894void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200895 int type_arg,
896 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100897 int export_size_delta,
898 int expected_export_status_arg,
899 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300900{
Ronald Cron5425a212020-08-04 14:58:35 +0200901 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300902 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200903 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200904 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300905 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300906 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100907 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100908 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910
Gilles Peskine8817f612018-12-18 00:18:46 +0100911 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300912
Gilles Peskine4747d192019-04-17 15:05:45 +0200913 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
914 psa_set_key_algorithm( &attributes, alg );
915 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300916
917 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200918 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300919
Gilles Peskine49c25912018-10-29 15:15:31 +0100920 /* Export the public key */
921 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200922 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200923 exported, export_size,
924 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100925 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100926 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100927 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200928 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100929 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200930 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200931 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100932 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100933 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100934 TEST_ASSERT( expected_public_key->len <=
935 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
936 TEST_ASSERT( expected_public_key->len <=
937 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100938 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
939 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100940 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300941
942exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100943 /*
944 * Key attributes may have been returned by psa_get_key_attributes()
945 * thus reset them as required.
946 */
947 psa_reset_key_attributes( &attributes );
948
itayzafrir3e02b3b2018-06-12 17:06:52 +0300949 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200950 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200951 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300952}
953/* END_CASE */
954
Gilles Peskine20035e32018-02-03 22:44:14 +0100955/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200956void import_and_exercise_key( data_t *data,
957 int type_arg,
958 int bits_arg,
959 int alg_arg )
960{
Ronald Cron5425a212020-08-04 14:58:35 +0200961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200962 psa_key_type_t type = type_arg;
963 size_t bits = bits_arg;
964 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100965 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200967 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200968
Gilles Peskine8817f612018-12-18 00:18:46 +0100969 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200970
Gilles Peskine4747d192019-04-17 15:05:45 +0200971 psa_set_key_usage_flags( &attributes, usage );
972 psa_set_key_algorithm( &attributes, alg );
973 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200974
975 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200976 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200977
978 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200979 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200980 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
981 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200982
983 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100984 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200985 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200986
Ronald Cron5425a212020-08-04 14:58:35 +0200987 PSA_ASSERT( psa_destroy_key( key ) );
988 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200989
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200990exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100991 /*
992 * Key attributes may have been returned by psa_get_key_attributes()
993 * thus reset them as required.
994 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200995 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100996
997 psa_reset_key_attributes( &attributes );
998 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200999 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001000}
1001/* END_CASE */
1002
1003/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001004void effective_key_attributes( int type_arg, int expected_type_arg,
1005 int bits_arg, int expected_bits_arg,
1006 int usage_arg, int expected_usage_arg,
1007 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001008{
Ronald Cron5425a212020-08-04 14:58:35 +02001009 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001010 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001011 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001012 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001013 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001014 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001015 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001016 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001017 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001019
Gilles Peskine8817f612018-12-18 00:18:46 +01001020 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001021
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001022 psa_set_key_usage_flags( &attributes, usage );
1023 psa_set_key_algorithm( &attributes, alg );
1024 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001025 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001026
Ronald Cron5425a212020-08-04 14:58:35 +02001027 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001028 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001029
Ronald Cron5425a212020-08-04 14:58:35 +02001030 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001031 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1032 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1033 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1034 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001035
1036exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001037 /*
1038 * Key attributes may have been returned by psa_get_key_attributes()
1039 * thus reset them as required.
1040 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001041 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001042
1043 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001044 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045}
1046/* END_CASE */
1047
1048/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001049void check_key_policy( int type_arg, int bits_arg,
1050 int usage_arg, int alg_arg )
1051{
1052 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1053 usage_arg, usage_arg, alg_arg, alg_arg );
1054 goto exit;
1055}
1056/* END_CASE */
1057
1058/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001059void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001060{
1061 /* Test each valid way of initializing the object, except for `= {0}`, as
1062 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1063 * though it's OK by the C standard. We could test for this, but we'd need
1064 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001065 psa_key_attributes_t func = psa_key_attributes_init( );
1066 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1067 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001068
1069 memset( &zero, 0, sizeof( zero ) );
1070
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001071 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1072 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1073 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001074
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001075 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1076 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1077 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1078
1079 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1080 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1081 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1082
1083 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1084 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1085 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1086
1087 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1088 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1089 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001090}
1091/* END_CASE */
1092
1093/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001094void mac_key_policy( int policy_usage,
1095 int policy_alg,
1096 int key_type,
1097 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001098 int exercise_alg,
1099 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001100{
Ronald Cron5425a212020-08-04 14:58:35 +02001101 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001102 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001103 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001104 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001105 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001106 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001107
Gilles Peskine8817f612018-12-18 00:18:46 +01001108 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001109
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001110 psa_set_key_usage_flags( &attributes, policy_usage );
1111 psa_set_key_algorithm( &attributes, policy_alg );
1112 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001113
Gilles Peskine049c7532019-05-15 20:22:09 +02001114 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001115 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001116
Ronald Cron5425a212020-08-04 14:58:35 +02001117 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001118 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001119 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001120 else
1121 TEST_EQUAL( status, expected_status );
1122
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001123 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001124
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001125 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001126 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001127 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001128 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001129 else
1130 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001131
1132exit:
1133 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001134 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001135 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001136}
1137/* END_CASE */
1138
1139/* BEGIN_CASE */
1140void cipher_key_policy( int policy_usage,
1141 int policy_alg,
1142 int key_type,
1143 data_t *key_data,
1144 int exercise_alg )
1145{
Ronald Cron5425a212020-08-04 14:58:35 +02001146 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001148 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001149 psa_status_t status;
1150
Gilles Peskine8817f612018-12-18 00:18:46 +01001151 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001152
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001153 psa_set_key_usage_flags( &attributes, policy_usage );
1154 psa_set_key_algorithm( &attributes, policy_alg );
1155 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001156
Gilles Peskine049c7532019-05-15 20:22:09 +02001157 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001158 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159
Ronald Cron5425a212020-08-04 14:58:35 +02001160 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001161 if( policy_alg == exercise_alg &&
1162 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001163 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001165 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001166 psa_cipher_abort( &operation );
1167
Ronald Cron5425a212020-08-04 14:58:35 +02001168 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169 if( policy_alg == exercise_alg &&
1170 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001171 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001173 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001174
1175exit:
1176 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001177 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001178 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001179}
1180/* END_CASE */
1181
1182/* BEGIN_CASE */
1183void aead_key_policy( int policy_usage,
1184 int policy_alg,
1185 int key_type,
1186 data_t *key_data,
1187 int nonce_length_arg,
1188 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001189 int exercise_alg,
1190 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191{
Ronald Cron5425a212020-08-04 14:58:35 +02001192 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001193 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001195 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001196 unsigned char nonce[16] = {0};
1197 size_t nonce_length = nonce_length_arg;
1198 unsigned char tag[16];
1199 size_t tag_length = tag_length_arg;
1200 size_t output_length;
1201
1202 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1203 TEST_ASSERT( tag_length <= sizeof( tag ) );
1204
Gilles Peskine8817f612018-12-18 00:18:46 +01001205 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001206
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001207 psa_set_key_usage_flags( &attributes, policy_usage );
1208 psa_set_key_algorithm( &attributes, policy_alg );
1209 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210
Gilles Peskine049c7532019-05-15 20:22:09 +02001211 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001212 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213
Ronald Cron5425a212020-08-04 14:58:35 +02001214 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001215 nonce, nonce_length,
1216 NULL, 0,
1217 NULL, 0,
1218 tag, tag_length,
1219 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001220 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1221 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001223 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001224
1225 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001226 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001227 nonce, nonce_length,
1228 NULL, 0,
1229 tag, tag_length,
1230 NULL, 0,
1231 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001232 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1233 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1234 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001235 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001236 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001237 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001238
1239exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001240 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001241 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001242}
1243/* END_CASE */
1244
1245/* BEGIN_CASE */
1246void asymmetric_encryption_key_policy( int policy_usage,
1247 int policy_alg,
1248 int key_type,
1249 data_t *key_data,
1250 int exercise_alg )
1251{
Ronald Cron5425a212020-08-04 14:58:35 +02001252 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001254 psa_status_t status;
1255 size_t key_bits;
1256 size_t buffer_length;
1257 unsigned char *buffer = NULL;
1258 size_t output_length;
1259
Gilles Peskine8817f612018-12-18 00:18:46 +01001260 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001261
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001262 psa_set_key_usage_flags( &attributes, policy_usage );
1263 psa_set_key_algorithm( &attributes, policy_alg );
1264 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001265
Gilles Peskine049c7532019-05-15 20:22:09 +02001266 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001267 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001268
Ronald Cron5425a212020-08-04 14:58:35 +02001269 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001270 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001271 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1272 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001273 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001274
Ronald Cron5425a212020-08-04 14:58:35 +02001275 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001276 NULL, 0,
1277 NULL, 0,
1278 buffer, buffer_length,
1279 &output_length );
1280 if( policy_alg == exercise_alg &&
1281 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001282 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001283 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001284 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001285
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001286 if( buffer_length != 0 )
1287 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001288 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001289 buffer, buffer_length,
1290 NULL, 0,
1291 buffer, buffer_length,
1292 &output_length );
1293 if( policy_alg == exercise_alg &&
1294 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001295 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001297 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001298
1299exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001300 /*
1301 * Key attributes may have been returned by psa_get_key_attributes()
1302 * thus reset them as required.
1303 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001304 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001305
1306 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001307 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001308 mbedtls_free( buffer );
1309}
1310/* END_CASE */
1311
1312/* BEGIN_CASE */
1313void asymmetric_signature_key_policy( int policy_usage,
1314 int policy_alg,
1315 int key_type,
1316 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001317 int exercise_alg,
1318 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001319{
Ronald Cron5425a212020-08-04 14:58:35 +02001320 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001321 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001322 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001323 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1324 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1325 * compatible with the policy and `payload_length_arg` is supposed to be
1326 * a valid input length to sign. If `payload_length_arg <= 0`,
1327 * `exercise_alg` is supposed to be forbidden by the policy. */
1328 int compatible_alg = payload_length_arg > 0;
1329 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001330 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001331 size_t signature_length;
1332
Gilles Peskine8817f612018-12-18 00:18:46 +01001333 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001334
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001335 psa_set_key_usage_flags( &attributes, policy_usage );
1336 psa_set_key_algorithm( &attributes, policy_alg );
1337 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001338
Gilles Peskine049c7532019-05-15 20:22:09 +02001339 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001340 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001341
Ronald Cron5425a212020-08-04 14:58:35 +02001342 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001343 payload, payload_length,
1344 signature, sizeof( signature ),
1345 &signature_length );
1346 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001347 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001348 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001349 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001350
1351 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001352 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001353 payload, payload_length,
1354 signature, sizeof( signature ) );
1355 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001356 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001357 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001358 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001359
1360exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001361 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001362 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001363}
1364/* END_CASE */
1365
Janos Follathba3fab92019-06-11 14:50:16 +01001366/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001367void derive_key_policy( int policy_usage,
1368 int policy_alg,
1369 int key_type,
1370 data_t *key_data,
1371 int exercise_alg )
1372{
Ronald Cron5425a212020-08-04 14:58:35 +02001373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001375 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001376 psa_status_t status;
1377
Gilles Peskine8817f612018-12-18 00:18:46 +01001378 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001379
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001380 psa_set_key_usage_flags( &attributes, policy_usage );
1381 psa_set_key_algorithm( &attributes, policy_alg );
1382 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001383
Gilles Peskine049c7532019-05-15 20:22:09 +02001384 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001385 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001386
Janos Follathba3fab92019-06-11 14:50:16 +01001387 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1388
1389 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1390 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001391 {
Janos Follathba3fab92019-06-11 14:50:16 +01001392 PSA_ASSERT( psa_key_derivation_input_bytes(
1393 &operation,
1394 PSA_KEY_DERIVATION_INPUT_SEED,
1395 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001396 }
Janos Follathba3fab92019-06-11 14:50:16 +01001397
1398 status = psa_key_derivation_input_key( &operation,
1399 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001400 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001401
Gilles Peskineea0fb492018-07-12 17:17:20 +02001402 if( policy_alg == exercise_alg &&
1403 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001404 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001405 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001406 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001407
1408exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001409 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001410 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001411 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001412}
1413/* END_CASE */
1414
1415/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001416void agreement_key_policy( int policy_usage,
1417 int policy_alg,
1418 int key_type_arg,
1419 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001420 int exercise_alg,
1421 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001422{
Ronald Cron5425a212020-08-04 14:58:35 +02001423 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001424 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001425 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001426 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001427 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001428 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001429
Gilles Peskine8817f612018-12-18 00:18:46 +01001430 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001431
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001432 psa_set_key_usage_flags( &attributes, policy_usage );
1433 psa_set_key_algorithm( &attributes, policy_alg );
1434 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001435
Gilles Peskine049c7532019-05-15 20:22:09 +02001436 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001437 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001438
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001439 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001440 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441
Steven Cooremance48e852020-10-05 16:02:45 +02001442 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001443
1444exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001445 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001446 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001447 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001448}
1449/* END_CASE */
1450
1451/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001452void key_policy_alg2( int key_type_arg, data_t *key_data,
1453 int usage_arg, int alg_arg, int alg2_arg )
1454{
Ronald Cron5425a212020-08-04 14:58:35 +02001455 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001456 psa_key_type_t key_type = key_type_arg;
1457 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1458 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1459 psa_key_usage_t usage = usage_arg;
1460 psa_algorithm_t alg = alg_arg;
1461 psa_algorithm_t alg2 = alg2_arg;
1462
1463 PSA_ASSERT( psa_crypto_init( ) );
1464
1465 psa_set_key_usage_flags( &attributes, usage );
1466 psa_set_key_algorithm( &attributes, alg );
1467 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1468 psa_set_key_type( &attributes, key_type );
1469 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001470 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001471
Ronald Cron5425a212020-08-04 14:58:35 +02001472 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001473 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1474 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1475 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1476
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001477 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001478 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001479 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001480 goto exit;
1481
1482exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001483 /*
1484 * Key attributes may have been returned by psa_get_key_attributes()
1485 * thus reset them as required.
1486 */
1487 psa_reset_key_attributes( &got_attributes );
1488
Ronald Cron5425a212020-08-04 14:58:35 +02001489 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001490 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001491}
1492/* END_CASE */
1493
1494/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001495void raw_agreement_key_policy( int policy_usage,
1496 int policy_alg,
1497 int key_type_arg,
1498 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001499 int exercise_alg,
1500 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001501{
Ronald Cron5425a212020-08-04 14:58:35 +02001502 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001503 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001504 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001505 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001506 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001507 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001508
1509 PSA_ASSERT( psa_crypto_init( ) );
1510
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001511 psa_set_key_usage_flags( &attributes, policy_usage );
1512 psa_set_key_algorithm( &attributes, policy_alg );
1513 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001514
Gilles Peskine049c7532019-05-15 20:22:09 +02001515 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001516 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001518 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001519
Steven Cooremance48e852020-10-05 16:02:45 +02001520 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001521
1522exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001523 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001524 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001525 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001526}
1527/* END_CASE */
1528
1529/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001530void copy_success( int source_usage_arg,
1531 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001532 int type_arg, data_t *material,
1533 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001534 int target_usage_arg,
1535 int target_alg_arg, int target_alg2_arg,
1536 int expected_usage_arg,
1537 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001538{
Gilles Peskineca25db92019-04-19 11:43:08 +02001539 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1540 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001541 psa_key_usage_t expected_usage = expected_usage_arg;
1542 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001543 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001544 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1545 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001546 uint8_t *export_buffer = NULL;
1547
Gilles Peskine57ab7212019-01-28 13:03:09 +01001548 PSA_ASSERT( psa_crypto_init( ) );
1549
Gilles Peskineca25db92019-04-19 11:43:08 +02001550 /* Prepare the source key. */
1551 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1552 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001553 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001554 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001555 PSA_ASSERT( psa_import_key( &source_attributes,
1556 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001557 &source_key ) );
1558 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001559
Gilles Peskineca25db92019-04-19 11:43:08 +02001560 /* Prepare the target attributes. */
1561 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001562 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001563 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001564 /* Set volatile lifetime to reset the key identifier to 0. */
1565 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1566 }
1567
Gilles Peskineca25db92019-04-19 11:43:08 +02001568 if( target_usage_arg != -1 )
1569 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1570 if( target_alg_arg != -1 )
1571 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001572 if( target_alg2_arg != -1 )
1573 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001574
1575 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001576 PSA_ASSERT( psa_copy_key( source_key,
1577 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001578
1579 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001580 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001581
1582 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001583 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001584 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1585 psa_get_key_type( &target_attributes ) );
1586 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1587 psa_get_key_bits( &target_attributes ) );
1588 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1589 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001590 TEST_EQUAL( expected_alg2,
1591 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001592 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1593 {
1594 size_t length;
1595 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001596 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001597 material->len, &length ) );
1598 ASSERT_COMPARE( material->x, material->len,
1599 export_buffer, length );
1600 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001601
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001602 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001603 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001604 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001605 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001606
Ronald Cron5425a212020-08-04 14:58:35 +02001607 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001608
1609exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001610 /*
1611 * Source and target key attributes may have been returned by
1612 * psa_get_key_attributes() thus reset them as required.
1613 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001614 psa_reset_key_attributes( &source_attributes );
1615 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001616
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001617 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001618 mbedtls_free( export_buffer );
1619}
1620/* END_CASE */
1621
1622/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001623void copy_fail( int source_usage_arg,
1624 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001625 int type_arg, data_t *material,
1626 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001627 int target_usage_arg,
1628 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001629 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001630 int expected_status_arg )
1631{
1632 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1633 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001634 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1635 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001636 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001637
1638 PSA_ASSERT( psa_crypto_init( ) );
1639
1640 /* Prepare the source key. */
1641 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1642 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001643 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001644 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001645 PSA_ASSERT( psa_import_key( &source_attributes,
1646 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001647 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001648
1649 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001650 psa_set_key_id( &target_attributes, key_id );
1651 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001652 psa_set_key_type( &target_attributes, target_type_arg );
1653 psa_set_key_bits( &target_attributes, target_bits_arg );
1654 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1655 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001656 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001657
1658 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001659 TEST_EQUAL( psa_copy_key( source_key,
1660 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001661 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001662
Ronald Cron5425a212020-08-04 14:58:35 +02001663 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001664
Gilles Peskine4a644642019-05-03 17:14:08 +02001665exit:
1666 psa_reset_key_attributes( &source_attributes );
1667 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001668 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001669}
1670/* END_CASE */
1671
1672/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001673void hash_operation_init( )
1674{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001675 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001676 /* Test each valid way of initializing the object, except for `= {0}`, as
1677 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1678 * though it's OK by the C standard. We could test for this, but we'd need
1679 * to supress the Clang warning for the test. */
1680 psa_hash_operation_t func = psa_hash_operation_init( );
1681 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1682 psa_hash_operation_t zero;
1683
1684 memset( &zero, 0, sizeof( zero ) );
1685
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001686 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001687 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1688 PSA_ERROR_BAD_STATE );
1689 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1690 PSA_ERROR_BAD_STATE );
1691 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1692 PSA_ERROR_BAD_STATE );
1693
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001694 /* A default hash operation should be abortable without error. */
1695 PSA_ASSERT( psa_hash_abort( &func ) );
1696 PSA_ASSERT( psa_hash_abort( &init ) );
1697 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001698}
1699/* END_CASE */
1700
1701/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001702void hash_setup( int alg_arg,
1703 int expected_status_arg )
1704{
1705 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001706 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001707 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001708 psa_status_t status;
1709
Gilles Peskine8817f612018-12-18 00:18:46 +01001710 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001711
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001712 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001713 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001714
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001715 /* Whether setup succeeded or failed, abort must succeed. */
1716 PSA_ASSERT( psa_hash_abort( &operation ) );
1717
1718 /* If setup failed, reproduce the failure, so as to
1719 * test the resulting state of the operation object. */
1720 if( status != PSA_SUCCESS )
1721 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1722
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001723 /* Now the operation object should be reusable. */
1724#if defined(KNOWN_SUPPORTED_HASH_ALG)
1725 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1726 PSA_ASSERT( psa_hash_abort( &operation ) );
1727#endif
1728
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001729exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001730 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001731}
1732/* END_CASE */
1733
1734/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001735void hash_compute_fail( int alg_arg, data_t *input,
1736 int output_size_arg, int expected_status_arg )
1737{
1738 psa_algorithm_t alg = alg_arg;
1739 uint8_t *output = NULL;
1740 size_t output_size = output_size_arg;
1741 size_t output_length = INVALID_EXPORT_LENGTH;
1742 psa_status_t expected_status = expected_status_arg;
1743 psa_status_t status;
1744
1745 ASSERT_ALLOC( output, output_size );
1746
1747 PSA_ASSERT( psa_crypto_init( ) );
1748
1749 status = psa_hash_compute( alg, input->x, input->len,
1750 output, output_size, &output_length );
1751 TEST_EQUAL( status, expected_status );
1752 TEST_ASSERT( output_length <= output_size );
1753
1754exit:
1755 mbedtls_free( output );
1756 PSA_DONE( );
1757}
1758/* END_CASE */
1759
1760/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001761void hash_compare_fail( int alg_arg, data_t *input,
1762 data_t *reference_hash,
1763 int expected_status_arg )
1764{
1765 psa_algorithm_t alg = alg_arg;
1766 psa_status_t expected_status = expected_status_arg;
1767 psa_status_t status;
1768
1769 PSA_ASSERT( psa_crypto_init( ) );
1770
1771 status = psa_hash_compare( alg, input->x, input->len,
1772 reference_hash->x, reference_hash->len );
1773 TEST_EQUAL( status, expected_status );
1774
1775exit:
1776 PSA_DONE( );
1777}
1778/* END_CASE */
1779
1780/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001781void hash_compute_compare( int alg_arg, data_t *input,
1782 data_t *expected_output )
1783{
1784 psa_algorithm_t alg = alg_arg;
1785 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1786 size_t output_length = INVALID_EXPORT_LENGTH;
1787 size_t i;
1788
1789 PSA_ASSERT( psa_crypto_init( ) );
1790
1791 /* Compute with tight buffer */
1792 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001793 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001794 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001795 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001796 ASSERT_COMPARE( output, output_length,
1797 expected_output->x, expected_output->len );
1798
1799 /* Compute with larger buffer */
1800 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1801 output, sizeof( output ),
1802 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001803 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001804 ASSERT_COMPARE( output, output_length,
1805 expected_output->x, expected_output->len );
1806
1807 /* Compare with correct hash */
1808 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1809 output, output_length ) );
1810
1811 /* Compare with trailing garbage */
1812 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1813 output, output_length + 1 ),
1814 PSA_ERROR_INVALID_SIGNATURE );
1815
1816 /* Compare with truncated hash */
1817 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1818 output, output_length - 1 ),
1819 PSA_ERROR_INVALID_SIGNATURE );
1820
1821 /* Compare with corrupted value */
1822 for( i = 0; i < output_length; i++ )
1823 {
Chris Jones9634bb12021-01-20 15:56:42 +00001824 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001825 output[i] ^= 1;
1826 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1827 output, output_length ),
1828 PSA_ERROR_INVALID_SIGNATURE );
1829 output[i] ^= 1;
1830 }
1831
1832exit:
1833 PSA_DONE( );
1834}
1835/* END_CASE */
1836
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001837/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001838void hash_bad_order( )
1839{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001840 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001841 unsigned char input[] = "";
1842 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001843 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001844 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1845 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1846 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001847 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001848 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001849 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001850
Gilles Peskine8817f612018-12-18 00:18:46 +01001851 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001852
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001853 /* Call setup twice in a row. */
1854 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1855 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1856 PSA_ERROR_BAD_STATE );
1857 PSA_ASSERT( psa_hash_abort( &operation ) );
1858
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001859 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001860 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001861 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001862 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001863
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001864 /* Call update after finish. */
1865 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1866 PSA_ASSERT( psa_hash_finish( &operation,
1867 hash, sizeof( hash ), &hash_len ) );
1868 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001869 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001870 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001871
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001872 /* Call verify without calling setup beforehand. */
1873 TEST_EQUAL( psa_hash_verify( &operation,
1874 valid_hash, sizeof( valid_hash ) ),
1875 PSA_ERROR_BAD_STATE );
1876 PSA_ASSERT( psa_hash_abort( &operation ) );
1877
1878 /* Call verify after finish. */
1879 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1880 PSA_ASSERT( psa_hash_finish( &operation,
1881 hash, sizeof( hash ), &hash_len ) );
1882 TEST_EQUAL( psa_hash_verify( &operation,
1883 valid_hash, sizeof( valid_hash ) ),
1884 PSA_ERROR_BAD_STATE );
1885 PSA_ASSERT( psa_hash_abort( &operation ) );
1886
1887 /* Call verify twice in a row. */
1888 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1889 PSA_ASSERT( psa_hash_verify( &operation,
1890 valid_hash, sizeof( valid_hash ) ) );
1891 TEST_EQUAL( psa_hash_verify( &operation,
1892 valid_hash, sizeof( valid_hash ) ),
1893 PSA_ERROR_BAD_STATE );
1894 PSA_ASSERT( psa_hash_abort( &operation ) );
1895
1896 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001897 TEST_EQUAL( psa_hash_finish( &operation,
1898 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001899 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001900 PSA_ASSERT( psa_hash_abort( &operation ) );
1901
1902 /* Call finish twice in a row. */
1903 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1904 PSA_ASSERT( psa_hash_finish( &operation,
1905 hash, sizeof( hash ), &hash_len ) );
1906 TEST_EQUAL( psa_hash_finish( &operation,
1907 hash, sizeof( hash ), &hash_len ),
1908 PSA_ERROR_BAD_STATE );
1909 PSA_ASSERT( psa_hash_abort( &operation ) );
1910
1911 /* Call finish after calling verify. */
1912 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1913 PSA_ASSERT( psa_hash_verify( &operation,
1914 valid_hash, sizeof( valid_hash ) ) );
1915 TEST_EQUAL( psa_hash_finish( &operation,
1916 hash, sizeof( hash ), &hash_len ),
1917 PSA_ERROR_BAD_STATE );
1918 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001919
1920exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001921 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001922}
1923/* END_CASE */
1924
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001925/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001926void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001927{
1928 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001929 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1930 * appended to it */
1931 unsigned char hash[] = {
1932 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1933 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1934 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001935 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001936 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001937
Gilles Peskine8817f612018-12-18 00:18:46 +01001938 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001939
itayzafrir27e69452018-11-01 14:26:34 +02001940 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001941 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001942 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001943 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001944
itayzafrir27e69452018-11-01 14:26:34 +02001945 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001946 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001947 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001948 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001949
itayzafrir27e69452018-11-01 14:26:34 +02001950 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001951 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001952 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001953 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001954
itayzafrirec93d302018-10-18 18:01:10 +03001955exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001956 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001957}
1958/* END_CASE */
1959
Ronald Cronee414c72021-03-18 18:50:08 +01001960/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001961void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001962{
1963 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001964 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001965 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001966 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001967 size_t hash_len;
1968
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001970
itayzafrir58028322018-10-25 10:22:01 +03001971 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001972 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001973 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001974 hash, expected_size - 1, &hash_len ),
1975 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001976
1977exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001978 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001979}
1980/* END_CASE */
1981
Ronald Cronee414c72021-03-18 18:50:08 +01001982/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001983void hash_clone_source_state( )
1984{
1985 psa_algorithm_t alg = PSA_ALG_SHA_256;
1986 unsigned char hash[PSA_HASH_MAX_SIZE];
1987 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1988 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1989 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1990 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1991 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1992 size_t hash_len;
1993
1994 PSA_ASSERT( psa_crypto_init( ) );
1995 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1996
1997 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1998 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1999 PSA_ASSERT( psa_hash_finish( &op_finished,
2000 hash, sizeof( hash ), &hash_len ) );
2001 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2002 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2003
2004 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2005 PSA_ERROR_BAD_STATE );
2006
2007 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2008 PSA_ASSERT( psa_hash_finish( &op_init,
2009 hash, sizeof( hash ), &hash_len ) );
2010 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2011 PSA_ASSERT( psa_hash_finish( &op_finished,
2012 hash, sizeof( hash ), &hash_len ) );
2013 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2014 PSA_ASSERT( psa_hash_finish( &op_aborted,
2015 hash, sizeof( hash ), &hash_len ) );
2016
2017exit:
2018 psa_hash_abort( &op_source );
2019 psa_hash_abort( &op_init );
2020 psa_hash_abort( &op_setup );
2021 psa_hash_abort( &op_finished );
2022 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002023 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002024}
2025/* END_CASE */
2026
Ronald Cronee414c72021-03-18 18:50:08 +01002027/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002028void hash_clone_target_state( )
2029{
2030 psa_algorithm_t alg = PSA_ALG_SHA_256;
2031 unsigned char hash[PSA_HASH_MAX_SIZE];
2032 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2033 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2034 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2035 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2036 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2037 size_t hash_len;
2038
2039 PSA_ASSERT( psa_crypto_init( ) );
2040
2041 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2042 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2043 PSA_ASSERT( psa_hash_finish( &op_finished,
2044 hash, sizeof( hash ), &hash_len ) );
2045 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2046 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2047
2048 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2049 PSA_ASSERT( psa_hash_finish( &op_target,
2050 hash, sizeof( hash ), &hash_len ) );
2051
2052 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2053 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2054 PSA_ERROR_BAD_STATE );
2055 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2056 PSA_ERROR_BAD_STATE );
2057
2058exit:
2059 psa_hash_abort( &op_target );
2060 psa_hash_abort( &op_init );
2061 psa_hash_abort( &op_setup );
2062 psa_hash_abort( &op_finished );
2063 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002064 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002065}
2066/* END_CASE */
2067
itayzafrir58028322018-10-25 10:22:01 +03002068/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002069void mac_operation_init( )
2070{
Jaeden Amero252ef282019-02-15 14:05:35 +00002071 const uint8_t input[1] = { 0 };
2072
Jaeden Amero769ce272019-01-04 11:48:03 +00002073 /* Test each valid way of initializing the object, except for `= {0}`, as
2074 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2075 * though it's OK by the C standard. We could test for this, but we'd need
2076 * to supress the Clang warning for the test. */
2077 psa_mac_operation_t func = psa_mac_operation_init( );
2078 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2079 psa_mac_operation_t zero;
2080
2081 memset( &zero, 0, sizeof( zero ) );
2082
Jaeden Amero252ef282019-02-15 14:05:35 +00002083 /* A freshly-initialized MAC operation should not be usable. */
2084 TEST_EQUAL( psa_mac_update( &func,
2085 input, sizeof( input ) ),
2086 PSA_ERROR_BAD_STATE );
2087 TEST_EQUAL( psa_mac_update( &init,
2088 input, sizeof( input ) ),
2089 PSA_ERROR_BAD_STATE );
2090 TEST_EQUAL( psa_mac_update( &zero,
2091 input, sizeof( input ) ),
2092 PSA_ERROR_BAD_STATE );
2093
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002094 /* A default MAC operation should be abortable without error. */
2095 PSA_ASSERT( psa_mac_abort( &func ) );
2096 PSA_ASSERT( psa_mac_abort( &init ) );
2097 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002098}
2099/* END_CASE */
2100
2101/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002102void mac_setup( int key_type_arg,
2103 data_t *key,
2104 int alg_arg,
2105 int expected_status_arg )
2106{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002107 psa_key_type_t key_type = key_type_arg;
2108 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002109 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002110 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002111 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2112#if defined(KNOWN_SUPPORTED_MAC_ALG)
2113 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2114#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002115
Gilles Peskine8817f612018-12-18 00:18:46 +01002116 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002117
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002118 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2119 &operation, &status ) )
2120 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002121 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002122
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002123 /* The operation object should be reusable. */
2124#if defined(KNOWN_SUPPORTED_MAC_ALG)
2125 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2126 smoke_test_key_data,
2127 sizeof( smoke_test_key_data ),
2128 KNOWN_SUPPORTED_MAC_ALG,
2129 &operation, &status ) )
2130 goto exit;
2131 TEST_EQUAL( status, PSA_SUCCESS );
2132#endif
2133
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002135 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002136}
2137/* END_CASE */
2138
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002139/* 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 +00002140void mac_bad_order( )
2141{
Ronald Cron5425a212020-08-04 14:58:35 +02002142 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002143 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2144 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002145 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002146 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2147 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2148 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002150 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2151 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2152 size_t sign_mac_length = 0;
2153 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2154 const uint8_t verify_mac[] = {
2155 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2156 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2157 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2158
2159 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002160 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002161 psa_set_key_algorithm( &attributes, alg );
2162 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002163
Ronald Cron5425a212020-08-04 14:58:35 +02002164 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2165 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002166
Jaeden Amero252ef282019-02-15 14:05:35 +00002167 /* Call update without calling setup beforehand. */
2168 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2169 PSA_ERROR_BAD_STATE );
2170 PSA_ASSERT( psa_mac_abort( &operation ) );
2171
2172 /* Call sign finish without calling setup beforehand. */
2173 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2174 &sign_mac_length),
2175 PSA_ERROR_BAD_STATE );
2176 PSA_ASSERT( psa_mac_abort( &operation ) );
2177
2178 /* Call verify finish without calling setup beforehand. */
2179 TEST_EQUAL( psa_mac_verify_finish( &operation,
2180 verify_mac, sizeof( verify_mac ) ),
2181 PSA_ERROR_BAD_STATE );
2182 PSA_ASSERT( psa_mac_abort( &operation ) );
2183
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002184 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002185 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2186 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002187 PSA_ERROR_BAD_STATE );
2188 PSA_ASSERT( psa_mac_abort( &operation ) );
2189
Jaeden Amero252ef282019-02-15 14:05:35 +00002190 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002191 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002192 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2193 PSA_ASSERT( psa_mac_sign_finish( &operation,
2194 sign_mac, sizeof( sign_mac ),
2195 &sign_mac_length ) );
2196 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2197 PSA_ERROR_BAD_STATE );
2198 PSA_ASSERT( psa_mac_abort( &operation ) );
2199
2200 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002201 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002202 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2203 PSA_ASSERT( psa_mac_verify_finish( &operation,
2204 verify_mac, sizeof( verify_mac ) ) );
2205 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2206 PSA_ERROR_BAD_STATE );
2207 PSA_ASSERT( psa_mac_abort( &operation ) );
2208
2209 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002210 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002211 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2212 PSA_ASSERT( psa_mac_sign_finish( &operation,
2213 sign_mac, sizeof( sign_mac ),
2214 &sign_mac_length ) );
2215 TEST_EQUAL( psa_mac_sign_finish( &operation,
2216 sign_mac, sizeof( sign_mac ),
2217 &sign_mac_length ),
2218 PSA_ERROR_BAD_STATE );
2219 PSA_ASSERT( psa_mac_abort( &operation ) );
2220
2221 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002222 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002223 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2224 PSA_ASSERT( psa_mac_verify_finish( &operation,
2225 verify_mac, sizeof( verify_mac ) ) );
2226 TEST_EQUAL( psa_mac_verify_finish( &operation,
2227 verify_mac, sizeof( verify_mac ) ),
2228 PSA_ERROR_BAD_STATE );
2229 PSA_ASSERT( psa_mac_abort( &operation ) );
2230
2231 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002232 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002233 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2234 TEST_EQUAL( psa_mac_verify_finish( &operation,
2235 verify_mac, sizeof( verify_mac ) ),
2236 PSA_ERROR_BAD_STATE );
2237 PSA_ASSERT( psa_mac_abort( &operation ) );
2238
2239 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002240 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002241 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2242 TEST_EQUAL( psa_mac_sign_finish( &operation,
2243 sign_mac, sizeof( sign_mac ),
2244 &sign_mac_length ),
2245 PSA_ERROR_BAD_STATE );
2246 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002247
Ronald Cron5425a212020-08-04 14:58:35 +02002248 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002249
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002250exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002251 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002252}
2253/* END_CASE */
2254
2255/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002256void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002257 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002258 int alg_arg,
2259 data_t *input,
2260 data_t *expected_mac )
2261{
Ronald Cron5425a212020-08-04 14:58:35 +02002262 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002263 psa_key_type_t key_type = key_type_arg;
2264 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002265 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002266 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002267 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002268 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002269 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002270 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002271 const size_t output_sizes_to_test[] = {
2272 0,
2273 1,
2274 expected_mac->len - 1,
2275 expected_mac->len,
2276 expected_mac->len + 1,
2277 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002278
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002279 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002280 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002281 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002282
Gilles Peskine8817f612018-12-18 00:18:46 +01002283 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002284
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002285 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002286 psa_set_key_algorithm( &attributes, alg );
2287 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002288
Ronald Cron5425a212020-08-04 14:58:35 +02002289 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2290 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002291
Gilles Peskine8b356b52020-08-25 23:44:59 +02002292 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2293 {
2294 const size_t output_size = output_sizes_to_test[i];
2295 psa_status_t expected_status =
2296 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2297 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002298
Chris Jones9634bb12021-01-20 15:56:42 +00002299 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002300 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002301
Gilles Peskine8b356b52020-08-25 23:44:59 +02002302 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002303 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002304 PSA_ASSERT( psa_mac_update( &operation,
2305 input->x, input->len ) );
2306 TEST_EQUAL( psa_mac_sign_finish( &operation,
2307 actual_mac, output_size,
2308 &mac_length ),
2309 expected_status );
2310 PSA_ASSERT( psa_mac_abort( &operation ) );
2311
2312 if( expected_status == PSA_SUCCESS )
2313 {
2314 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2315 actual_mac, mac_length );
2316 }
2317 mbedtls_free( actual_mac );
2318 actual_mac = NULL;
2319 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002320
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002321exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002322 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002323 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002324 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002325 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002326}
2327/* END_CASE */
2328
2329/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002330void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002331 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002332 int alg_arg,
2333 data_t *input,
2334 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002335{
Ronald Cron5425a212020-08-04 14:58:35 +02002336 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002337 psa_key_type_t key_type = key_type_arg;
2338 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002339 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002341 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002342
Gilles Peskine69c12672018-06-28 00:07:19 +02002343 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2344
Gilles Peskine8817f612018-12-18 00:18:46 +01002345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002346
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002348 psa_set_key_algorithm( &attributes, alg );
2349 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002350
Ronald Cron5425a212020-08-04 14:58:35 +02002351 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2352 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002353
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002354 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002355 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002356 PSA_ASSERT( psa_mac_update( &operation,
2357 input->x, input->len ) );
2358 PSA_ASSERT( psa_mac_verify_finish( &operation,
2359 expected_mac->x,
2360 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002361
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002362 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002363 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002364 PSA_ASSERT( psa_mac_update( &operation,
2365 input->x, input->len ) );
2366 TEST_EQUAL( psa_mac_verify_finish( &operation,
2367 expected_mac->x,
2368 expected_mac->len - 1 ),
2369 PSA_ERROR_INVALID_SIGNATURE );
2370
2371 /* Test a MAC that's too long. */
2372 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2373 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002374 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002375 PSA_ASSERT( psa_mac_update( &operation,
2376 input->x, input->len ) );
2377 TEST_EQUAL( psa_mac_verify_finish( &operation,
2378 perturbed_mac,
2379 expected_mac->len + 1 ),
2380 PSA_ERROR_INVALID_SIGNATURE );
2381
2382 /* Test changing one byte. */
2383 for( size_t i = 0; i < expected_mac->len; i++ )
2384 {
Chris Jones9634bb12021-01-20 15:56:42 +00002385 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002386 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002387 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002388 PSA_ASSERT( psa_mac_update( &operation,
2389 input->x, input->len ) );
2390 TEST_EQUAL( psa_mac_verify_finish( &operation,
2391 perturbed_mac,
2392 expected_mac->len ),
2393 PSA_ERROR_INVALID_SIGNATURE );
2394 perturbed_mac[i] ^= 1;
2395 }
2396
Gilles Peskine8c9def32018-02-08 10:02:12 +01002397exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002398 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002399 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002400 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002401 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002402}
2403/* END_CASE */
2404
2405/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002406void cipher_operation_init( )
2407{
Jaeden Ameroab439972019-02-15 14:12:05 +00002408 const uint8_t input[1] = { 0 };
2409 unsigned char output[1] = { 0 };
2410 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002411 /* Test each valid way of initializing the object, except for `= {0}`, as
2412 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2413 * though it's OK by the C standard. We could test for this, but we'd need
2414 * to supress the Clang warning for the test. */
2415 psa_cipher_operation_t func = psa_cipher_operation_init( );
2416 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2417 psa_cipher_operation_t zero;
2418
2419 memset( &zero, 0, sizeof( zero ) );
2420
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 /* A freshly-initialized cipher operation should not be usable. */
2422 TEST_EQUAL( psa_cipher_update( &func,
2423 input, sizeof( input ),
2424 output, sizeof( output ),
2425 &output_length ),
2426 PSA_ERROR_BAD_STATE );
2427 TEST_EQUAL( psa_cipher_update( &init,
2428 input, sizeof( input ),
2429 output, sizeof( output ),
2430 &output_length ),
2431 PSA_ERROR_BAD_STATE );
2432 TEST_EQUAL( psa_cipher_update( &zero,
2433 input, sizeof( input ),
2434 output, sizeof( output ),
2435 &output_length ),
2436 PSA_ERROR_BAD_STATE );
2437
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002438 /* A default cipher operation should be abortable without error. */
2439 PSA_ASSERT( psa_cipher_abort( &func ) );
2440 PSA_ASSERT( psa_cipher_abort( &init ) );
2441 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002442}
2443/* END_CASE */
2444
2445/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002446void cipher_setup( int key_type_arg,
2447 data_t *key,
2448 int alg_arg,
2449 int expected_status_arg )
2450{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002451 psa_key_type_t key_type = key_type_arg;
2452 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002453 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002454 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002455 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002456#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002457 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2458#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002459
Gilles Peskine8817f612018-12-18 00:18:46 +01002460 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002461
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002462 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2463 &operation, &status ) )
2464 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002465 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002466
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002467 /* The operation object should be reusable. */
2468#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2469 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2470 smoke_test_key_data,
2471 sizeof( smoke_test_key_data ),
2472 KNOWN_SUPPORTED_CIPHER_ALG,
2473 &operation, &status ) )
2474 goto exit;
2475 TEST_EQUAL( status, PSA_SUCCESS );
2476#endif
2477
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002478exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002479 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002480 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002481}
2482/* END_CASE */
2483
Ronald Cronee414c72021-03-18 18:50:08 +01002484/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002485void cipher_bad_order( )
2486{
Ronald Cron5425a212020-08-04 14:58:35 +02002487 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002488 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2489 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002491 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002492 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002493 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002494 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2495 0xaa, 0xaa, 0xaa, 0xaa };
2496 const uint8_t text[] = {
2497 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2498 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002499 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002500 size_t length = 0;
2501
2502 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002503 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2504 psa_set_key_algorithm( &attributes, alg );
2505 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002506 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2507 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002508
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002509 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002510 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2511 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002512 PSA_ERROR_BAD_STATE );
2513 PSA_ASSERT( psa_cipher_abort( &operation ) );
2514
2515 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002516 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2517 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002518 PSA_ERROR_BAD_STATE );
2519 PSA_ASSERT( psa_cipher_abort( &operation ) );
2520
Jaeden Ameroab439972019-02-15 14:12:05 +00002521 /* Generate an IV without calling setup beforehand. */
2522 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2523 buffer, sizeof( buffer ),
2524 &length ),
2525 PSA_ERROR_BAD_STATE );
2526 PSA_ASSERT( psa_cipher_abort( &operation ) );
2527
2528 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002529 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002530 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2531 buffer, sizeof( buffer ),
2532 &length ) );
2533 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2534 buffer, sizeof( buffer ),
2535 &length ),
2536 PSA_ERROR_BAD_STATE );
2537 PSA_ASSERT( psa_cipher_abort( &operation ) );
2538
2539 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002540 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002541 PSA_ASSERT( psa_cipher_set_iv( &operation,
2542 iv, sizeof( iv ) ) );
2543 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2544 buffer, sizeof( buffer ),
2545 &length ),
2546 PSA_ERROR_BAD_STATE );
2547 PSA_ASSERT( psa_cipher_abort( &operation ) );
2548
2549 /* Set an IV without calling setup beforehand. */
2550 TEST_EQUAL( psa_cipher_set_iv( &operation,
2551 iv, sizeof( iv ) ),
2552 PSA_ERROR_BAD_STATE );
2553 PSA_ASSERT( psa_cipher_abort( &operation ) );
2554
2555 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002556 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002557 PSA_ASSERT( psa_cipher_set_iv( &operation,
2558 iv, sizeof( iv ) ) );
2559 TEST_EQUAL( psa_cipher_set_iv( &operation,
2560 iv, sizeof( iv ) ),
2561 PSA_ERROR_BAD_STATE );
2562 PSA_ASSERT( psa_cipher_abort( &operation ) );
2563
2564 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002565 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002566 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2567 buffer, sizeof( buffer ),
2568 &length ) );
2569 TEST_EQUAL( psa_cipher_set_iv( &operation,
2570 iv, sizeof( iv ) ),
2571 PSA_ERROR_BAD_STATE );
2572 PSA_ASSERT( psa_cipher_abort( &operation ) );
2573
2574 /* Call update without calling setup beforehand. */
2575 TEST_EQUAL( psa_cipher_update( &operation,
2576 text, sizeof( text ),
2577 buffer, sizeof( buffer ),
2578 &length ),
2579 PSA_ERROR_BAD_STATE );
2580 PSA_ASSERT( psa_cipher_abort( &operation ) );
2581
2582 /* Call update without an IV where an IV is required. */
2583 TEST_EQUAL( psa_cipher_update( &operation,
2584 text, sizeof( text ),
2585 buffer, sizeof( buffer ),
2586 &length ),
2587 PSA_ERROR_BAD_STATE );
2588 PSA_ASSERT( psa_cipher_abort( &operation ) );
2589
2590 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002591 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002592 PSA_ASSERT( psa_cipher_set_iv( &operation,
2593 iv, sizeof( iv ) ) );
2594 PSA_ASSERT( psa_cipher_finish( &operation,
2595 buffer, sizeof( buffer ), &length ) );
2596 TEST_EQUAL( psa_cipher_update( &operation,
2597 text, sizeof( text ),
2598 buffer, sizeof( buffer ),
2599 &length ),
2600 PSA_ERROR_BAD_STATE );
2601 PSA_ASSERT( psa_cipher_abort( &operation ) );
2602
2603 /* Call finish without calling setup beforehand. */
2604 TEST_EQUAL( psa_cipher_finish( &operation,
2605 buffer, sizeof( buffer ), &length ),
2606 PSA_ERROR_BAD_STATE );
2607 PSA_ASSERT( psa_cipher_abort( &operation ) );
2608
2609 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002610 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002611 /* Not calling update means we are encrypting an empty buffer, which is OK
2612 * for cipher modes with padding. */
2613 TEST_EQUAL( psa_cipher_finish( &operation,
2614 buffer, sizeof( buffer ), &length ),
2615 PSA_ERROR_BAD_STATE );
2616 PSA_ASSERT( psa_cipher_abort( &operation ) );
2617
2618 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002619 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002620 PSA_ASSERT( psa_cipher_set_iv( &operation,
2621 iv, sizeof( iv ) ) );
2622 PSA_ASSERT( psa_cipher_finish( &operation,
2623 buffer, sizeof( buffer ), &length ) );
2624 TEST_EQUAL( psa_cipher_finish( &operation,
2625 buffer, sizeof( buffer ), &length ),
2626 PSA_ERROR_BAD_STATE );
2627 PSA_ASSERT( psa_cipher_abort( &operation ) );
2628
Ronald Cron5425a212020-08-04 14:58:35 +02002629 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002630
Jaeden Ameroab439972019-02-15 14:12:05 +00002631exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002632 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002633 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002634}
2635/* END_CASE */
2636
2637/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002638void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002639 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002640 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002641 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642{
Ronald Cron5425a212020-08-04 14:58:35 +02002643 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002644 psa_status_t status;
2645 psa_key_type_t key_type = key_type_arg;
2646 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002647 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002648 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002649 size_t output_buffer_size = 0;
2650 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002651 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002652 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002653 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654
Gilles Peskine8817f612018-12-18 00:18:46 +01002655 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002656
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002657 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2658 psa_set_key_algorithm( &attributes, alg );
2659 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002660
Ronald Cron5425a212020-08-04 14:58:35 +02002661 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2662 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663
Ronald Cron5425a212020-08-04 14:58:35 +02002664 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002665
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002666 if( iv->len > 0 )
2667 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002668 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002669 }
2670
gabor-mezei-armceface22021-01-21 12:26:17 +01002671 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2672 TEST_ASSERT( output_buffer_size <=
2673 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002674 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675
Gilles Peskine8817f612018-12-18 00:18:46 +01002676 PSA_ASSERT( psa_cipher_update( &operation,
2677 input->x, input->len,
2678 output, output_buffer_size,
2679 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002680 TEST_ASSERT( function_output_length <=
2681 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2682 TEST_ASSERT( function_output_length <=
2683 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002684 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002685
Gilles Peskine50e586b2018-06-08 14:28:46 +02002686 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002687 ( output_buffer_size == 0 ? NULL :
2688 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002689 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002690 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002691 TEST_ASSERT( function_output_length <=
2692 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2693 TEST_ASSERT( function_output_length <=
2694 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002695 total_output_length += function_output_length;
2696
Gilles Peskinefe11b722018-12-18 00:24:04 +01002697 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002698 if( expected_status == PSA_SUCCESS )
2699 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002700 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002701 ASSERT_COMPARE( expected_output->x, expected_output->len,
2702 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002704
Gilles Peskine50e586b2018-06-08 14:28:46 +02002705exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002706 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002707 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002708 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002709 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710}
2711/* END_CASE */
2712
2713/* BEGIN_CASE */
2714void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002715 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002716 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002717 int first_part_size_arg,
2718 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002719 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720{
Ronald Cron5425a212020-08-04 14:58:35 +02002721 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002722 psa_key_type_t key_type = key_type_arg;
2723 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002724 size_t first_part_size = first_part_size_arg;
2725 size_t output1_length = output1_length_arg;
2726 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002727 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002728 size_t output_buffer_size = 0;
2729 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002730 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002731 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002732 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002733
Gilles Peskine8817f612018-12-18 00:18:46 +01002734 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002735
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002736 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2737 psa_set_key_algorithm( &attributes, alg );
2738 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002739
Ronald Cron5425a212020-08-04 14:58:35 +02002740 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2741 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002742
Ronald Cron5425a212020-08-04 14:58:35 +02002743 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002744
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002745 if( iv->len > 0 )
2746 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002747 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002748 }
2749
gabor-mezei-armceface22021-01-21 12:26:17 +01002750 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2751 TEST_ASSERT( output_buffer_size <=
2752 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002753 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002754
Gilles Peskinee0866522019-02-19 19:44:00 +01002755 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002756 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2757 output, output_buffer_size,
2758 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002759 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002760 TEST_ASSERT( function_output_length <=
2761 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2762 TEST_ASSERT( function_output_length <=
2763 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002764 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002765
Gilles Peskine8817f612018-12-18 00:18:46 +01002766 PSA_ASSERT( psa_cipher_update( &operation,
2767 input->x + first_part_size,
2768 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002769 ( output_buffer_size == 0 ? NULL :
2770 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002771 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002772 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002773 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002774 TEST_ASSERT( function_output_length <=
2775 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2776 alg,
2777 input->len - first_part_size ) );
2778 TEST_ASSERT( function_output_length <=
2779 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002780 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002781
Gilles Peskine8817f612018-12-18 00:18:46 +01002782 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002783 ( output_buffer_size == 0 ? NULL :
2784 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002785 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002786 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002787 TEST_ASSERT( function_output_length <=
2788 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2789 TEST_ASSERT( function_output_length <=
2790 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002791 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002792 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002793
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002794 ASSERT_COMPARE( expected_output->x, expected_output->len,
2795 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002796
2797exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002798 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002799 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002800 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002801 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002802}
2803/* END_CASE */
2804
2805/* BEGIN_CASE */
2806void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002807 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002808 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002809 int first_part_size_arg,
2810 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002811 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812{
Ronald Cron5425a212020-08-04 14:58:35 +02002813 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002814 psa_key_type_t key_type = key_type_arg;
2815 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002816 size_t first_part_size = first_part_size_arg;
2817 size_t output1_length = output1_length_arg;
2818 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002819 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002820 size_t output_buffer_size = 0;
2821 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002822 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002823 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002825
Gilles Peskine8817f612018-12-18 00:18:46 +01002826 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002827
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002828 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2829 psa_set_key_algorithm( &attributes, alg );
2830 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002831
Ronald Cron5425a212020-08-04 14:58:35 +02002832 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2833 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834
Ronald Cron5425a212020-08-04 14:58:35 +02002835 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002836
Steven Cooreman177deba2020-09-07 17:14:14 +02002837 if( iv->len > 0 )
2838 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002839 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002840 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002841
gabor-mezei-armceface22021-01-21 12:26:17 +01002842 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2843 TEST_ASSERT( output_buffer_size <=
2844 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002845 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002846
Gilles Peskinee0866522019-02-19 19:44:00 +01002847 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002848 PSA_ASSERT( psa_cipher_update( &operation,
2849 input->x, first_part_size,
2850 output, output_buffer_size,
2851 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002852 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002853 TEST_ASSERT( function_output_length <=
2854 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2855 TEST_ASSERT( function_output_length <=
2856 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002857 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002858
Gilles Peskine8817f612018-12-18 00:18:46 +01002859 PSA_ASSERT( psa_cipher_update( &operation,
2860 input->x + first_part_size,
2861 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002862 ( output_buffer_size == 0 ? NULL :
2863 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002864 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002865 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002866 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002867 TEST_ASSERT( function_output_length <=
2868 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2869 alg,
2870 input->len - first_part_size ) );
2871 TEST_ASSERT( function_output_length <=
2872 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002873 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002874
Gilles Peskine8817f612018-12-18 00:18:46 +01002875 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002876 ( output_buffer_size == 0 ? NULL :
2877 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002878 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002879 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002880 TEST_ASSERT( function_output_length <=
2881 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2882 TEST_ASSERT( function_output_length <=
2883 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002884 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002885 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002886
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002887 ASSERT_COMPARE( expected_output->x, expected_output->len,
2888 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889
2890exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002891 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002892 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002893 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002894 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002895}
2896/* END_CASE */
2897
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898/* BEGIN_CASE */
2899void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002900 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002901 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002902 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903{
Ronald Cron5425a212020-08-04 14:58:35 +02002904 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002905 psa_status_t status;
2906 psa_key_type_t key_type = key_type_arg;
2907 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002908 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002909 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002910 size_t output_buffer_size = 0;
2911 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002912 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002913 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915
Gilles Peskine8817f612018-12-18 00:18:46 +01002916 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002917
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002918 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2919 psa_set_key_algorithm( &attributes, alg );
2920 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002921
Ronald Cron5425a212020-08-04 14:58:35 +02002922 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2923 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924
Ronald Cron5425a212020-08-04 14:58:35 +02002925 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926
Steven Cooreman177deba2020-09-07 17:14:14 +02002927 if( iv->len > 0 )
2928 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002929 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002930 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002931
gabor-mezei-armceface22021-01-21 12:26:17 +01002932 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2933 TEST_ASSERT( output_buffer_size <=
2934 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002935 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002936
Gilles Peskine8817f612018-12-18 00:18:46 +01002937 PSA_ASSERT( psa_cipher_update( &operation,
2938 input->x, input->len,
2939 output, output_buffer_size,
2940 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002941 TEST_ASSERT( function_output_length <=
2942 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2943 TEST_ASSERT( function_output_length <=
2944 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002945 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002946
Gilles Peskine50e586b2018-06-08 14:28:46 +02002947 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002948 ( output_buffer_size == 0 ? NULL :
2949 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002950 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002951 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002952 TEST_ASSERT( function_output_length <=
2953 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2954 TEST_ASSERT( function_output_length <=
2955 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002956 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002957 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002958
2959 if( expected_status == PSA_SUCCESS )
2960 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002961 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002962 ASSERT_COMPARE( expected_output->x, expected_output->len,
2963 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002964 }
2965
Gilles Peskine50e586b2018-06-08 14:28:46 +02002966exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002967 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002968 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002969 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002970 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002971}
2972/* END_CASE */
2973
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974/* BEGIN_CASE */
2975void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002976 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002977 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002978{
Ronald Cron5425a212020-08-04 14:58:35 +02002979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002980 psa_key_type_t key_type = key_type_arg;
2981 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002982 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002983 size_t iv_size = 16;
2984 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002985 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002986 size_t output1_size = 0;
2987 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002988 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002989 size_t output2_size = 0;
2990 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002991 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002992 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2993 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002995
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002997
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002998 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2999 psa_set_key_algorithm( &attributes, alg );
3000 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003001
Ronald Cron5425a212020-08-04 14:58:35 +02003002 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3003 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003004
Ronald Cron5425a212020-08-04 14:58:35 +02003005 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3006 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003007
Steven Cooreman177deba2020-09-07 17:14:14 +02003008 if( alg != PSA_ALG_ECB_NO_PADDING )
3009 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003010 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3011 iv, iv_size,
3012 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003013 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003014 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3015 TEST_ASSERT( output1_size <=
3016 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003017 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003018
Gilles Peskine8817f612018-12-18 00:18:46 +01003019 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3020 output1, output1_size,
3021 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003022 TEST_ASSERT( output1_length <=
3023 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3024 TEST_ASSERT( output1_length <=
3025 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3026
Gilles Peskine8817f612018-12-18 00:18:46 +01003027 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003028 output1 + output1_length,
3029 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003030 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003031 TEST_ASSERT( function_output_length <=
3032 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3033 TEST_ASSERT( function_output_length <=
3034 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003035
Gilles Peskine048b7f02018-06-08 14:20:49 +02003036 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003037
Gilles Peskine8817f612018-12-18 00:18:46 +01003038 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003039
3040 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003041 TEST_ASSERT( output2_size <=
3042 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3043 TEST_ASSERT( output2_size <=
3044 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003045 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003046
Steven Cooreman177deba2020-09-07 17:14:14 +02003047 if( iv_length > 0 )
3048 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003049 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3050 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003051 }
3052
Gilles Peskine8817f612018-12-18 00:18:46 +01003053 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3054 output2, output2_size,
3055 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003056 TEST_ASSERT( output2_length <=
3057 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3058 TEST_ASSERT( output2_length <=
3059 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3060
Gilles Peskine048b7f02018-06-08 14:20:49 +02003061 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003062 PSA_ASSERT( psa_cipher_finish( &operation2,
3063 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003064 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003065 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003066 TEST_ASSERT( function_output_length <=
3067 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3068 TEST_ASSERT( function_output_length <=
3069 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003070
Gilles Peskine048b7f02018-06-08 14:20:49 +02003071 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003072
Gilles Peskine8817f612018-12-18 00:18:46 +01003073 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003074
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003075 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003076
3077exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003078 psa_cipher_abort( &operation1 );
3079 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003080 mbedtls_free( output1 );
3081 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003082 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003083 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003084}
3085/* END_CASE */
3086
3087/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003088void cipher_verify_output_multipart( int alg_arg,
3089 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003090 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003091 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003092 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003093{
Ronald Cron5425a212020-08-04 14:58:35 +02003094 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003095 psa_key_type_t key_type = key_type_arg;
3096 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003097 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003098 unsigned char iv[16] = {0};
3099 size_t iv_size = 16;
3100 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003101 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003102 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003103 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003104 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003105 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003106 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003107 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003108 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3109 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003111
Gilles Peskine8817f612018-12-18 00:18:46 +01003112 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003113
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003114 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3115 psa_set_key_algorithm( &attributes, alg );
3116 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003117
Ronald Cron5425a212020-08-04 14:58:35 +02003118 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3119 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003120
Ronald Cron5425a212020-08-04 14:58:35 +02003121 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3122 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003123
Steven Cooreman177deba2020-09-07 17:14:14 +02003124 if( alg != PSA_ALG_ECB_NO_PADDING )
3125 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003126 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3127 iv, iv_size,
3128 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003129 }
3130
gabor-mezei-armceface22021-01-21 12:26:17 +01003131 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3132 TEST_ASSERT( output1_buffer_size <=
3133 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003134 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003135
Gilles Peskinee0866522019-02-19 19:44:00 +01003136 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003137
Gilles Peskine8817f612018-12-18 00:18:46 +01003138 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3139 output1, output1_buffer_size,
3140 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003141 TEST_ASSERT( function_output_length <=
3142 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3143 TEST_ASSERT( function_output_length <=
3144 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003145 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003146
Gilles Peskine8817f612018-12-18 00:18:46 +01003147 PSA_ASSERT( psa_cipher_update( &operation1,
3148 input->x + first_part_size,
3149 input->len - first_part_size,
3150 output1, output1_buffer_size,
3151 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003152 TEST_ASSERT( function_output_length <=
3153 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3154 alg,
3155 input->len - first_part_size ) );
3156 TEST_ASSERT( function_output_length <=
3157 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003158 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003159
Gilles Peskine8817f612018-12-18 00:18:46 +01003160 PSA_ASSERT( psa_cipher_finish( &operation1,
3161 output1 + output1_length,
3162 output1_buffer_size - output1_length,
3163 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003164 TEST_ASSERT( function_output_length <=
3165 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3166 TEST_ASSERT( function_output_length <=
3167 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003168 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003169
Gilles Peskine8817f612018-12-18 00:18:46 +01003170 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003171
Gilles Peskine048b7f02018-06-08 14:20:49 +02003172 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003173 TEST_ASSERT( output2_buffer_size <=
3174 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3175 TEST_ASSERT( output2_buffer_size <=
3176 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003177 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003178
Steven Cooreman177deba2020-09-07 17:14:14 +02003179 if( iv_length > 0 )
3180 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003181 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3182 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003183 }
Moran Pekerded84402018-06-06 16:36:50 +03003184
Gilles Peskine8817f612018-12-18 00:18:46 +01003185 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3186 output2, output2_buffer_size,
3187 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003188 TEST_ASSERT( function_output_length <=
3189 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3190 TEST_ASSERT( function_output_length <=
3191 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003192 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003193
Gilles Peskine8817f612018-12-18 00:18:46 +01003194 PSA_ASSERT( psa_cipher_update( &operation2,
3195 output1 + first_part_size,
3196 output1_length - first_part_size,
3197 output2, output2_buffer_size,
3198 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003199 TEST_ASSERT( function_output_length <=
3200 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3201 alg,
3202 output1_length - first_part_size ) );
3203 TEST_ASSERT( function_output_length <=
3204 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003205 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003206
Gilles Peskine8817f612018-12-18 00:18:46 +01003207 PSA_ASSERT( psa_cipher_finish( &operation2,
3208 output2 + output2_length,
3209 output2_buffer_size - output2_length,
3210 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003211 TEST_ASSERT( function_output_length <=
3212 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3213 TEST_ASSERT( function_output_length <=
3214 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003215 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003216
Gilles Peskine8817f612018-12-18 00:18:46 +01003217 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003218
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003219 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003220
3221exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003222 psa_cipher_abort( &operation1 );
3223 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003224 mbedtls_free( output1 );
3225 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003226 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003227 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003228}
3229/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003230
Gilles Peskine20035e32018-02-03 22:44:14 +01003231/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003232void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003233 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003234 data_t *nonce,
3235 data_t *additional_data,
3236 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003237 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003238{
Ronald Cron5425a212020-08-04 14:58:35 +02003239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003240 psa_key_type_t key_type = key_type_arg;
3241 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003242 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003243 unsigned char *output_data = NULL;
3244 size_t output_size = 0;
3245 size_t output_length = 0;
3246 unsigned char *output_data2 = NULL;
3247 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003248 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003249 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251
Gilles Peskine8817f612018-12-18 00:18:46 +01003252 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003253
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003254 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3255 psa_set_key_algorithm( &attributes, alg );
3256 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003257
Gilles Peskine049c7532019-05-15 20:22:09 +02003258 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003259 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003260 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3261 key_bits = psa_get_key_bits( &attributes );
3262
3263 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3264 alg );
3265 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3266 * should be exact. */
3267 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3268 expected_result != PSA_ERROR_NOT_SUPPORTED )
3269 {
3270 TEST_EQUAL( output_size,
3271 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3272 TEST_ASSERT( output_size <=
3273 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3274 }
3275 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003276
Steven Cooremanf49478b2021-02-15 15:19:25 +01003277 status = psa_aead_encrypt( key, alg,
3278 nonce->x, nonce->len,
3279 additional_data->x,
3280 additional_data->len,
3281 input_data->x, input_data->len,
3282 output_data, output_size,
3283 &output_length );
3284
3285 /* If the operation is not supported, just skip and not fail in case the
3286 * encryption involves a common limitation of cryptography hardwares and
3287 * an alternative implementation. */
3288 if( status == PSA_ERROR_NOT_SUPPORTED )
3289 {
3290 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3291 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3292 }
3293
3294 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003295
3296 if( PSA_SUCCESS == expected_result )
3297 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003298 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003299
Gilles Peskine003a4a92019-05-14 16:09:40 +02003300 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3301 * should be exact. */
3302 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003303 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003304
gabor-mezei-armceface22021-01-21 12:26:17 +01003305 TEST_ASSERT( input_data->len <=
3306 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3307
Ronald Cron5425a212020-08-04 14:58:35 +02003308 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003309 nonce->x, nonce->len,
3310 additional_data->x,
3311 additional_data->len,
3312 output_data, output_length,
3313 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003314 &output_length2 ),
3315 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003316
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003317 ASSERT_COMPARE( input_data->x, input_data->len,
3318 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003319 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003320
Gilles Peskinea1cac842018-06-11 19:33:02 +02003321exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003322 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003323 mbedtls_free( output_data );
3324 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003325 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003326}
3327/* END_CASE */
3328
3329/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003330void aead_encrypt( int key_type_arg, data_t *key_data,
3331 int alg_arg,
3332 data_t *nonce,
3333 data_t *additional_data,
3334 data_t *input_data,
3335 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336{
Ronald Cron5425a212020-08-04 14:58:35 +02003337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003338 psa_key_type_t key_type = key_type_arg;
3339 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003340 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341 unsigned char *output_data = NULL;
3342 size_t output_size = 0;
3343 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003345 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003346
Gilles Peskine8817f612018-12-18 00:18:46 +01003347 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003348
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003349 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3350 psa_set_key_algorithm( &attributes, alg );
3351 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003352
Gilles Peskine049c7532019-05-15 20:22:09 +02003353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003354 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003355 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3356 key_bits = psa_get_key_bits( &attributes );
3357
3358 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3359 alg );
3360 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3361 * should be exact. */
3362 TEST_EQUAL( output_size,
3363 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3364 TEST_ASSERT( output_size <=
3365 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3366 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003367
Steven Cooremand588ea12021-01-11 19:36:04 +01003368 status = psa_aead_encrypt( key, alg,
3369 nonce->x, nonce->len,
3370 additional_data->x, additional_data->len,
3371 input_data->x, input_data->len,
3372 output_data, output_size,
3373 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003374
Ronald Cron28a45ed2021-02-09 20:35:42 +01003375 /* If the operation is not supported, just skip and not fail in case the
3376 * encryption involves a common limitation of cryptography hardwares and
3377 * an alternative implementation. */
3378 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003379 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003380 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3381 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003382 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003383
3384 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003385 ASSERT_COMPARE( expected_result->x, expected_result->len,
3386 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003387
Gilles Peskinea1cac842018-06-11 19:33:02 +02003388exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003389 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003391 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003392}
3393/* END_CASE */
3394
3395/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003396void aead_decrypt( int key_type_arg, data_t *key_data,
3397 int alg_arg,
3398 data_t *nonce,
3399 data_t *additional_data,
3400 data_t *input_data,
3401 data_t *expected_data,
3402 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403{
Ronald Cron5425a212020-08-04 14:58:35 +02003404 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003405 psa_key_type_t key_type = key_type_arg;
3406 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003407 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003408 unsigned char *output_data = NULL;
3409 size_t output_size = 0;
3410 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003412 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003413 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414
Gilles Peskine8817f612018-12-18 00:18:46 +01003415 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003416
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003417 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3418 psa_set_key_algorithm( &attributes, alg );
3419 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420
Gilles Peskine049c7532019-05-15 20:22:09 +02003421 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003422 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003423 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3424 key_bits = psa_get_key_bits( &attributes );
3425
3426 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3427 alg );
3428 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3429 expected_result != PSA_ERROR_NOT_SUPPORTED )
3430 {
3431 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3432 * should be exact. */
3433 TEST_EQUAL( output_size,
3434 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3435 TEST_ASSERT( output_size <=
3436 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3437 }
3438 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003439
Steven Cooremand588ea12021-01-11 19:36:04 +01003440 status = psa_aead_decrypt( key, alg,
3441 nonce->x, nonce->len,
3442 additional_data->x,
3443 additional_data->len,
3444 input_data->x, input_data->len,
3445 output_data, output_size,
3446 &output_length );
3447
Ronald Cron28a45ed2021-02-09 20:35:42 +01003448 /* If the operation is not supported, just skip and not fail in case the
3449 * decryption involves a common limitation of cryptography hardwares and
3450 * an alternative implementation. */
3451 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003452 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003453 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3454 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003455 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003456
3457 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003458
Gilles Peskine2d277862018-06-18 15:41:12 +02003459 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003460 ASSERT_COMPARE( expected_data->x, expected_data->len,
3461 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003462
Gilles Peskinea1cac842018-06-11 19:33:02 +02003463exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003464 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003466 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003467}
3468/* END_CASE */
3469
3470/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003471void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3472 int alg_arg,
3473 data_t *nonce,
3474 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003475 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003476 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003477 int do_test_data_chunked,
3478 int do_set_lengths,
3479 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003480{
Paul Elliottd3f82412021-06-16 16:52:21 +01003481 size_t ad_part_len = 0;
3482 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003483 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003484
Paul Elliotte64deda2021-09-09 14:07:23 +01003485 /* Ensure that either one part of the test or the other is done, i.e this
3486 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003487 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3488
3489 /* Temporary whilst we have algorithms that cannot support chunking */
3490 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003491 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003492 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3493 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003494 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003495 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003496
Paul Elliott33746aa2021-09-15 16:40:40 +01003497 if( do_set_lengths )
3498 {
3499 if( ad_part_len & 0x01 )
3500 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3501 else
3502 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3503 }
3504
Paul Elliott329d5382021-07-22 17:10:45 +01003505 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003506 if( !aead_multipart_internal_func( key_type_arg, key_data,
3507 alg_arg, nonce,
3508 additional_data,
3509 ad_part_len,
3510 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003511 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003512 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003513 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003514 break;
3515
3516 /* length(0) part, length(ad_part_len) part, length(0) part... */
3517 mbedtls_test_set_step( 1000 + ad_part_len );
3518
3519 if( !aead_multipart_internal_func( key_type_arg, key_data,
3520 alg_arg, nonce,
3521 additional_data,
3522 ad_part_len,
3523 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003524 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003525 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003526 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003527 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003528 }
3529 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003530
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003531 /* Temporary whilst we have algorithms that cannot support chunking */
3532 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003533 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003534 for( data_part_len = 1; data_part_len <= input_data->len;
3535 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003536 {
Paul Elliott329d5382021-07-22 17:10:45 +01003537 /* Split data into length(data_part_len) parts. */
3538 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003539
Paul Elliott33746aa2021-09-15 16:40:40 +01003540 if( do_set_lengths )
3541 {
3542 if( data_part_len & 0x01 )
3543 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3544 else
3545 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3546 }
3547
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003548 if( !aead_multipart_internal_func( key_type_arg, key_data,
3549 alg_arg, nonce,
3550 additional_data, -1,
3551 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003552 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003553 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003554 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003555 break;
3556
3557 /* length(0) part, length(data_part_len) part, length(0) part... */
3558 mbedtls_test_set_step( 3000 + data_part_len );
3559
3560 if( !aead_multipart_internal_func( key_type_arg, key_data,
3561 alg_arg, nonce,
3562 additional_data, -1,
3563 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003564 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003565 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003566 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003567 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003568 }
3569 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003570
Paul Elliott0023e0a2021-04-27 10:06:22 +01003571
Paul Elliott8fc45162021-06-23 16:06:01 +01003572 /* Goto is required to silence warnings about unused labels, as we
3573 * don't actually do any test assertions in this function. */
3574 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003575}
3576/* END_CASE */
3577
3578/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003579void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3580 int alg_arg,
3581 data_t *nonce,
3582 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003583 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003584 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003585 int do_test_data_chunked,
3586 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003587 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003588{
Paul Elliottd3f82412021-06-16 16:52:21 +01003589 size_t ad_part_len = 0;
3590 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003591 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003592
Paul Elliotte64deda2021-09-09 14:07:23 +01003593 /* Ensure that either one part of the test or the other is done, i.e this
3594 * test does something. */
3595 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3596
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003597 /* Temporary whilst we have algorithms that cannot support chunking */
3598 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003599 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003600 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3601 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003602 {
Paul Elliott329d5382021-07-22 17:10:45 +01003603 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003604 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003605
Paul Elliott33746aa2021-09-15 16:40:40 +01003606 if( do_set_lengths )
3607 {
3608 if( ad_part_len & 0x01 )
3609 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3610 else
3611 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3612 }
3613
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003614 if( !aead_multipart_internal_func( key_type_arg, key_data,
3615 alg_arg, nonce,
3616 additional_data,
3617 ad_part_len,
3618 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003619 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003620 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003621 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003622 break;
3623
3624 /* length(0) part, length(ad_part_len) part, length(0) part... */
3625 mbedtls_test_set_step( 1000 + ad_part_len );
3626
3627 if( !aead_multipart_internal_func( key_type_arg, key_data,
3628 alg_arg, nonce,
3629 additional_data,
3630 ad_part_len,
3631 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003632 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003633 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003634 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003635 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003636 }
3637 }
3638
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003639 /* Temporary whilst we have algorithms that cannot support chunking */
3640 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003641 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003642 for( data_part_len = 1; data_part_len <= input_data->len;
3643 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003644 {
Paul Elliott329d5382021-07-22 17:10:45 +01003645 /* Split data into length(data_part_len) parts. */
3646 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003647
Paul Elliott33746aa2021-09-15 16:40:40 +01003648 if( do_set_lengths )
3649 {
3650 if( data_part_len & 0x01 )
3651 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3652 else
3653 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3654 }
3655
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003656 if( !aead_multipart_internal_func( key_type_arg, key_data,
3657 alg_arg, nonce,
3658 additional_data, -1,
3659 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003660 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003661 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003662 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003663 break;
3664
3665 /* length(0) part, length(data_part_len) part, length(0) part... */
3666 mbedtls_test_set_step( 3000 + data_part_len );
3667
3668 if( !aead_multipart_internal_func( key_type_arg, key_data,
3669 alg_arg, nonce,
3670 additional_data, -1,
3671 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003672 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003673 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003674 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003675 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003676 }
3677 }
3678
Paul Elliott8fc45162021-06-23 16:06:01 +01003679 /* Goto is required to silence warnings about unused labels, as we
3680 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003681 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003682}
3683/* END_CASE */
3684
3685/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003686void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3687 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003688 int nonce_length,
3689 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003690 data_t *additional_data,
3691 data_t *input_data,
3692 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003693{
3694
3695 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3696 psa_key_type_t key_type = key_type_arg;
3697 psa_algorithm_t alg = alg_arg;
3698 psa_aead_operation_t operation;
3699 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3701 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003702 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003703 size_t actual_nonce_length = 0;
3704 size_t expected_nonce_length = expected_nonce_length_arg;
3705 unsigned char *output = NULL;
3706 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003707 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003708 size_t ciphertext_size = 0;
3709 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003710 size_t tag_length = 0;
3711 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003712
3713 PSA_ASSERT( psa_crypto_init( ) );
3714
3715 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3716 psa_set_key_algorithm( & attributes, alg );
3717 psa_set_key_type( & attributes, key_type );
3718
3719 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3720 &key ) );
3721
3722 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3723
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003724 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3725
Paul Elliottf1277632021-08-24 18:11:37 +01003726 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003727
Paul Elliottf1277632021-08-24 18:11:37 +01003728 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003729
Paul Elliottf1277632021-08-24 18:11:37 +01003730 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003731
Paul Elliottf1277632021-08-24 18:11:37 +01003732 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003733
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003734 operation = psa_aead_operation_init( );
3735
3736 status = psa_aead_encrypt_setup( &operation, key, alg );
3737
3738 /* If the operation is not supported, just skip and not fail in case the
3739 * encryption involves a common limitation of cryptography hardwares and
3740 * an alternative implementation. */
3741 if( status == PSA_ERROR_NOT_SUPPORTED )
3742 {
3743 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003744 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003745 }
3746
3747 PSA_ASSERT( status );
3748
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003749 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003750 nonce_length,
3751 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003752
Paul Elliott693bf312021-07-23 17:40:41 +01003753 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003754
Paul Elliottf1277632021-08-24 18:11:37 +01003755 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003756
Paul Elliottf1277632021-08-24 18:11:37 +01003757 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003758
Paul Elliott693bf312021-07-23 17:40:41 +01003759 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003760 {
3761
3762 /* Ensure we can still complete operation. */
3763
3764 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3765 additional_data->len ) );
3766
3767 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003768 output, output_size,
3769 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003770
Paul Elliottf1277632021-08-24 18:11:37 +01003771 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3772 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003773 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3774 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003775
3776exit:
3777 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003778 mbedtls_free( output );
3779 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003780 psa_aead_abort( &operation );
3781 PSA_DONE( );
3782}
3783/* END_CASE */
3784
3785/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003786void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3787 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003788 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003789 data_t *additional_data,
3790 data_t *input_data,
3791 int expected_status_arg )
3792{
3793
3794 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3795 psa_key_type_t key_type = key_type_arg;
3796 psa_algorithm_t alg = alg_arg;
3797 psa_aead_operation_t operation;
3798 uint8_t *nonce_buffer = NULL;
3799 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3800 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3801 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003802 unsigned char *output = NULL;
3803 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003804 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003805 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003806 size_t ciphertext_size = 0;
3807 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003808 size_t tag_length = 0;
3809 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003810 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003811
3812 PSA_ASSERT( psa_crypto_init( ) );
3813
3814 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3815 psa_set_key_algorithm( &attributes, alg );
3816 psa_set_key_type( &attributes, key_type );
3817
3818 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3819 &key ) );
3820
3821 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3822
3823 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3824
Paul Elliott6f0e7202021-08-25 12:57:18 +01003825 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003826
Paul Elliott6f0e7202021-08-25 12:57:18 +01003827 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003828
Paul Elliott6f0e7202021-08-25 12:57:18 +01003829 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003830
Paul Elliott6f0e7202021-08-25 12:57:18 +01003831 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003832
3833 operation = psa_aead_operation_init( );
3834
3835 status = psa_aead_encrypt_setup( &operation, key, alg );
3836
3837 /* If the operation is not supported, just skip and not fail in case the
3838 * encryption involves a common limitation of cryptography hardwares and
3839 * an alternative implementation. */
3840 if( status == PSA_ERROR_NOT_SUPPORTED )
3841 {
3842 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003843 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003844 }
3845
3846 PSA_ASSERT( status );
3847
Paul Elliott4023ffd2021-09-10 16:21:22 +01003848 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3849 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003850 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003851 /* Arbitrary size buffer, to test zero length valid buffer. */
3852 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003853 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003854 }
3855 else
3856 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003857 /* If length is zero, then this will return NULL. */
3858 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003859 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003860
Paul Elliott4023ffd2021-09-10 16:21:22 +01003861 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003862 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003863 for( index = 0; index < nonce_length - 1; ++index )
3864 {
3865 nonce_buffer[index] = 'a' + index;
3866 }
Paul Elliott66696b52021-08-16 18:42:41 +01003867 }
Paul Elliott863864a2021-07-23 17:28:31 +01003868 }
3869
Paul Elliott6f0e7202021-08-25 12:57:18 +01003870 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003871
Paul Elliott693bf312021-07-23 17:40:41 +01003872 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003873
3874 if( expected_status == PSA_SUCCESS )
3875 {
3876 /* Ensure we can still complete operation. */
3877
3878 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3879 additional_data->len ) );
3880
3881 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003882 output, output_size,
3883 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003884
Paul Elliott6f0e7202021-08-25 12:57:18 +01003885 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3886 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003887 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3888 }
3889
3890exit:
3891 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003892 mbedtls_free( output );
3893 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003894 mbedtls_free( nonce_buffer );
3895 psa_aead_abort( &operation );
3896 PSA_DONE( );
3897}
3898/* END_CASE */
3899
3900/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003901void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3902 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003903 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003904 data_t *nonce,
3905 data_t *additional_data,
3906 data_t *input_data,
3907 int expected_status_arg )
3908{
3909
3910 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3911 psa_key_type_t key_type = key_type_arg;
3912 psa_algorithm_t alg = alg_arg;
3913 psa_aead_operation_t operation;
3914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3915 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3916 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003917 unsigned char *output = NULL;
3918 unsigned char *ciphertext = NULL;
3919 size_t output_size = output_size_arg;
3920 size_t ciphertext_size = 0;
3921 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003922 size_t tag_length = 0;
3923 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3924
3925 PSA_ASSERT( psa_crypto_init( ) );
3926
3927 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3928 psa_set_key_algorithm( &attributes, alg );
3929 psa_set_key_type( &attributes, key_type );
3930
3931 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3932 &key ) );
3933
3934 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3935
Paul Elliottc6d11d02021-09-01 12:04:23 +01003936 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003937
Paul Elliottc6d11d02021-09-01 12:04:23 +01003938 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003939
Paul Elliottc6d11d02021-09-01 12:04:23 +01003940 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003941
3942 operation = psa_aead_operation_init( );
3943
3944 status = psa_aead_encrypt_setup( &operation, key, alg );
3945
3946 /* If the operation is not supported, just skip and not fail in case the
3947 * encryption involves a common limitation of cryptography hardwares and
3948 * an alternative implementation. */
3949 if( status == PSA_ERROR_NOT_SUPPORTED )
3950 {
3951 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3952 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3953 }
3954
3955 PSA_ASSERT( status );
3956
3957 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3958
3959 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3960 additional_data->len ) );
3961
3962 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003963 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003964
3965 TEST_EQUAL( status, expected_status );
3966
3967 if( expected_status == PSA_SUCCESS )
3968 {
3969 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003970 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3971 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003972 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3973 }
3974
3975exit:
3976 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003977 mbedtls_free( output );
3978 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003979 psa_aead_abort( &operation );
3980 PSA_DONE( );
3981}
3982/* END_CASE */
3983
Paul Elliott91b021e2021-07-23 18:52:31 +01003984/* BEGIN_CASE */
3985void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3986 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003987 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003988 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003989 data_t *nonce,
3990 data_t *additional_data,
3991 data_t *input_data,
3992 int expected_status_arg )
3993{
3994
3995 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3996 psa_key_type_t key_type = key_type_arg;
3997 psa_algorithm_t alg = alg_arg;
3998 psa_aead_operation_t operation;
3999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4000 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4001 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004002 unsigned char *ciphertext = NULL;
4003 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004004 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004005 size_t ciphertext_size = 0;
4006 size_t ciphertext_length = 0;
4007 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004008 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004009 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004010
4011 PSA_ASSERT( psa_crypto_init( ) );
4012
4013 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4014 psa_set_key_algorithm( &attributes, alg );
4015 psa_set_key_type( &attributes, key_type );
4016
4017 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4018 &key ) );
4019
4020 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4021
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004022 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004023
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004024 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004025
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004026 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004027
Paul Elliott719c1322021-09-13 18:27:22 +01004028 ASSERT_ALLOC( tag_buffer, tag_size );
4029
Paul Elliott91b021e2021-07-23 18:52:31 +01004030 operation = psa_aead_operation_init( );
4031
4032 status = psa_aead_encrypt_setup( &operation, key, alg );
4033
4034 /* If the operation is not supported, just skip and not fail in case the
4035 * encryption involves a common limitation of cryptography hardwares and
4036 * an alternative implementation. */
4037 if( status == PSA_ERROR_NOT_SUPPORTED )
4038 {
4039 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4040 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4041 }
4042
4043 PSA_ASSERT( status );
4044
4045 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4046
4047 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4048 additional_data->len ) );
4049
4050 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004051 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004052
4053 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004054 status = psa_aead_finish( &operation, finish_ciphertext,
4055 finish_ciphertext_size,
4056 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004057 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004058
4059 TEST_EQUAL( status, expected_status );
4060
4061exit:
4062 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004063 mbedtls_free( ciphertext );
4064 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004065 psa_aead_abort( &operation );
4066 PSA_DONE( );
4067}
4068/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004069
4070/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004071void aead_multipart_verify( int key_type_arg, data_t *key_data,
4072 int alg_arg,
4073 data_t *nonce,
4074 data_t *additional_data,
4075 data_t *input_data,
4076 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004077 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004078 int expected_status_arg )
4079{
4080 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4081 psa_key_type_t key_type = key_type_arg;
4082 psa_algorithm_t alg = alg_arg;
4083 psa_aead_operation_t operation;
4084 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4085 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4086 psa_status_t expected_status = expected_status_arg;
4087 unsigned char *plaintext = NULL;
4088 unsigned char *finish_plaintext = NULL;
4089 size_t plaintext_size = 0;
4090 size_t plaintext_length = 0;
4091 size_t verify_plaintext_size = 0;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004092 tagusage_method tag_usage = tag_usage_arg;
4093 unsigned char *tag_buffer = NULL;
4094 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004095
4096 PSA_ASSERT( psa_crypto_init( ) );
4097
4098 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4099 psa_set_key_algorithm( &attributes, alg );
4100 psa_set_key_type( &attributes, key_type );
4101
4102 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4103 &key ) );
4104
4105 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4106
4107 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4108 input_data->len );
4109
4110 ASSERT_ALLOC( plaintext, plaintext_size );
4111
4112 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4113
4114 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4115
4116 operation = psa_aead_operation_init( );
4117
4118 status = psa_aead_decrypt_setup( &operation, key, alg );
4119
4120 /* If the operation is not supported, just skip and not fail in case the
4121 * encryption involves a common limitation of cryptography hardwares and
4122 * an alternative implementation. */
4123 if( status == PSA_ERROR_NOT_SUPPORTED )
4124 {
4125 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4126 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4127 }
4128
4129 PSA_ASSERT( status );
4130
4131 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4132
4133 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4134 additional_data->len ) );
4135
4136 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4137 input_data->len,
4138 plaintext, plaintext_size,
4139 &plaintext_length ) );
4140
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004141 if( tag_usage == USE_GIVEN_TAG )
4142 {
4143 tag_buffer = tag->x;
4144 tag_size = tag->len;
4145 }
4146
Paul Elliott9961a662021-09-17 19:19:02 +01004147 status = psa_aead_verify( &operation, finish_plaintext,
4148 verify_plaintext_size,
4149 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004150 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004151
4152 TEST_EQUAL( status, expected_status );
4153
4154exit:
4155 psa_destroy_key( key );
4156 mbedtls_free( plaintext );
4157 mbedtls_free( finish_plaintext );
4158 psa_aead_abort( &operation );
4159 PSA_DONE( );
4160}
4161/* END_CASE */
4162
Paul Elliott9961a662021-09-17 19:19:02 +01004163/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004164void aead_multipart_setup( int key_type_arg, data_t *key_data,
4165 int alg_arg, int expected_status_arg )
4166{
4167 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4168 psa_key_type_t key_type = key_type_arg;
4169 psa_algorithm_t alg = alg_arg;
4170 psa_aead_operation_t operation;
4171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4172 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4173 psa_status_t expected_status = expected_status_arg;
4174
4175 PSA_ASSERT( psa_crypto_init( ) );
4176
4177 psa_set_key_usage_flags( &attributes,
4178 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4179 psa_set_key_algorithm( &attributes, alg );
4180 psa_set_key_type( &attributes, key_type );
4181
4182 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4183 &key ) );
4184
4185 mbedtls_test_set_step( 0 );
4186
4187 status = psa_aead_encrypt_setup( &operation, key, alg );
4188
4189 TEST_EQUAL( status, expected_status );
4190
4191 psa_aead_abort( &operation );
4192
4193 operation = psa_aead_operation_init( );
4194
4195 mbedtls_test_set_step( 1 );
4196
4197 status = psa_aead_decrypt_setup( &operation, key, alg );
4198
4199 TEST_EQUAL(status, expected_status );
4200
4201exit:
4202 psa_destroy_key( key );
4203 psa_aead_abort( &operation );
4204 PSA_DONE( );
4205}
4206/* END_CASE */
4207
4208/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004209void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4210 int alg_arg,
4211 data_t *nonce,
4212 data_t *additional_data,
4213 data_t *input_data )
4214{
4215 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4216 psa_key_type_t key_type = key_type_arg;
4217 psa_algorithm_t alg = alg_arg;
4218 psa_aead_operation_t operation;
4219 unsigned char *output_data = NULL;
4220 unsigned char *final_data = NULL;
4221 size_t output_size = 0;
4222 size_t finish_output_size = 0;
4223 size_t output_length = 0;
4224 size_t key_bits = 0;
4225 size_t tag_length = 0;
4226 size_t tag_size = 0;
4227 size_t nonce_length = 0;
4228 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4229 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4230 size_t output_part_length = 0;
4231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4232
4233 PSA_ASSERT( psa_crypto_init( ) );
4234
4235 psa_set_key_usage_flags( & attributes,
4236 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4237 psa_set_key_algorithm( & attributes, alg );
4238 psa_set_key_type( & attributes, key_type );
4239
4240 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4241 &key ) );
4242
4243 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4244 key_bits = psa_get_key_bits( &attributes );
4245
4246 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4247
4248 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4249
4250 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4251
4252 ASSERT_ALLOC( output_data, output_size );
4253
4254 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4255
4256 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4257
4258 ASSERT_ALLOC( final_data, finish_output_size );
4259
4260 /* Test all operations error without calling setup first. */
4261
4262 operation = psa_aead_operation_init( );
4263
4264 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4265 PSA_ERROR_BAD_STATE );
4266
4267 psa_aead_abort( &operation );
4268
4269 operation = psa_aead_operation_init( );
4270
4271 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4272 PSA_AEAD_NONCE_MAX_SIZE,
4273 &nonce_length ),
4274 PSA_ERROR_BAD_STATE );
4275
4276 psa_aead_abort( &operation );
4277
Paul Elliott481be342021-07-16 17:38:47 +01004278 /* ------------------------------------------------------- */
4279
Paul Elliottc23a9a02021-06-21 18:32:46 +01004280 operation = psa_aead_operation_init( );
4281
4282 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4283 input_data->len ),
4284 PSA_ERROR_BAD_STATE );
4285
4286 psa_aead_abort( &operation );
4287
Paul Elliott481be342021-07-16 17:38:47 +01004288 /* ------------------------------------------------------- */
4289
Paul Elliottc23a9a02021-06-21 18:32:46 +01004290 operation = psa_aead_operation_init( );
4291
4292 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4293 additional_data->len ),
4294 PSA_ERROR_BAD_STATE );
4295
4296 psa_aead_abort( &operation );
4297
Paul Elliott481be342021-07-16 17:38:47 +01004298 /* ------------------------------------------------------- */
4299
Paul Elliottc23a9a02021-06-21 18:32:46 +01004300 operation = psa_aead_operation_init( );
4301
4302 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4303 input_data->len, output_data,
4304 output_size, &output_length ),
4305 PSA_ERROR_BAD_STATE );
4306
4307 psa_aead_abort( &operation );
4308
Paul Elliott481be342021-07-16 17:38:47 +01004309 /* ------------------------------------------------------- */
4310
Paul Elliottc23a9a02021-06-21 18:32:46 +01004311 operation = psa_aead_operation_init( );
4312
4313 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4314 finish_output_size,
4315 &output_part_length,
4316 tag_buffer, tag_length,
4317 &tag_size ),
4318 PSA_ERROR_BAD_STATE );
4319
4320 psa_aead_abort( &operation );
4321
Paul Elliott481be342021-07-16 17:38:47 +01004322 /* ------------------------------------------------------- */
4323
Paul Elliottc23a9a02021-06-21 18:32:46 +01004324 operation = psa_aead_operation_init( );
4325
4326 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4327 finish_output_size,
4328 &output_part_length,
4329 tag_buffer,
4330 tag_length ),
4331 PSA_ERROR_BAD_STATE );
4332
4333 psa_aead_abort( &operation );
4334
4335 /* Test for double setups. */
4336
4337 operation = psa_aead_operation_init( );
4338
4339 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4340
4341 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4342 PSA_ERROR_BAD_STATE );
4343
4344 psa_aead_abort( &operation );
4345
Paul Elliott481be342021-07-16 17:38:47 +01004346 /* ------------------------------------------------------- */
4347
Paul Elliottc23a9a02021-06-21 18:32:46 +01004348 operation = psa_aead_operation_init( );
4349
4350 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4351
4352 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4353 PSA_ERROR_BAD_STATE );
4354
4355 psa_aead_abort( &operation );
4356
Paul Elliott374a2be2021-07-16 17:53:40 +01004357 /* ------------------------------------------------------- */
4358
4359 operation = psa_aead_operation_init( );
4360
4361 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4362
4363 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4364 PSA_ERROR_BAD_STATE );
4365
4366 psa_aead_abort( &operation );
4367
4368 /* ------------------------------------------------------- */
4369
4370 operation = psa_aead_operation_init( );
4371
4372 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4373
4374 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4375 PSA_ERROR_BAD_STATE );
4376
4377 psa_aead_abort( &operation );
4378
Paul Elliottc23a9a02021-06-21 18:32:46 +01004379 /* Test for not setting a nonce. */
4380
4381 operation = psa_aead_operation_init( );
4382
4383 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4384
4385 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4386 additional_data->len ),
4387 PSA_ERROR_BAD_STATE );
4388
4389 psa_aead_abort( &operation );
4390
Paul Elliott7f628422021-09-01 12:08:29 +01004391 /* ------------------------------------------------------- */
4392
4393 operation = psa_aead_operation_init( );
4394
4395 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4396
4397 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4398 input_data->len, output_data,
4399 output_size, &output_length ),
4400 PSA_ERROR_BAD_STATE );
4401
4402 psa_aead_abort( &operation );
4403
Paul Elliottc23a9a02021-06-21 18:32:46 +01004404 /* Test for double setting nonce. */
4405
4406 operation = psa_aead_operation_init( );
4407
4408 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4409
4410 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4411
4412 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4413 PSA_ERROR_BAD_STATE );
4414
4415 psa_aead_abort( &operation );
4416
Paul Elliott374a2be2021-07-16 17:53:40 +01004417 /* Test for double generating nonce. */
4418
4419 operation = psa_aead_operation_init( );
4420
4421 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4422
4423 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4424 PSA_AEAD_NONCE_MAX_SIZE,
4425 &nonce_length ) );
4426
4427 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4428 PSA_AEAD_NONCE_MAX_SIZE,
4429 &nonce_length ),
4430 PSA_ERROR_BAD_STATE );
4431
4432
4433 psa_aead_abort( &operation );
4434
4435 /* Test for generate nonce then set and vice versa */
4436
4437 operation = psa_aead_operation_init( );
4438
4439 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4440
4441 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4442 PSA_AEAD_NONCE_MAX_SIZE,
4443 &nonce_length ) );
4444
4445 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4446 PSA_ERROR_BAD_STATE );
4447
4448 psa_aead_abort( &operation );
4449
4450 /* ------------------------------------------------------- */
4451
4452 operation = psa_aead_operation_init( );
4453
4454 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4455
4456 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4457
4458 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4459 PSA_AEAD_NONCE_MAX_SIZE,
4460 &nonce_length ),
4461 PSA_ERROR_BAD_STATE );
4462
4463 psa_aead_abort( &operation );
4464
Paul Elliott7220cae2021-06-22 17:25:57 +01004465 /* Test for generating nonce in decrypt setup. */
4466
4467 operation = psa_aead_operation_init( );
4468
4469 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4470
4471 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4472 PSA_AEAD_NONCE_MAX_SIZE,
4473 &nonce_length ),
4474 PSA_ERROR_BAD_STATE );
4475
4476 psa_aead_abort( &operation );
4477
Paul Elliottc23a9a02021-06-21 18:32:46 +01004478 /* Test for setting lengths twice. */
4479
4480 operation = psa_aead_operation_init( );
4481
4482 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4483
4484 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4485
4486 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4487 input_data->len ) );
4488
4489 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4490 input_data->len ),
4491 PSA_ERROR_BAD_STATE );
4492
4493 psa_aead_abort( &operation );
4494
4495 /* Test for setting lengths after already starting data. */
4496
4497 operation = psa_aead_operation_init( );
4498
4499 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4500
4501 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4502
Paul Elliottf94bd992021-09-19 18:15:59 +01004503 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4504 additional_data->len ) );
4505
4506 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4507 input_data->len ),
4508 PSA_ERROR_BAD_STATE );
4509
4510 psa_aead_abort( &operation );
4511
4512 /* ------------------------------------------------------- */
4513
4514 operation = psa_aead_operation_init( );
4515
4516 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4517
4518 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4519
Paul Elliottc23a9a02021-06-21 18:32:46 +01004520 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4521 input_data->len, output_data,
4522 output_size, &output_length ) );
4523
4524 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4525 input_data->len ),
4526 PSA_ERROR_BAD_STATE );
4527
4528 psa_aead_abort( &operation );
4529
Paul Elliott243080c2021-07-21 19:01:17 +01004530 /* Test for not sending any additional data or data after setting non zero
4531 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004532
4533 operation = psa_aead_operation_init( );
4534
4535 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4536
4537 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4538
4539 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4540 input_data->len ) );
4541
4542 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4543 finish_output_size,
4544 &output_part_length,
4545 tag_buffer, tag_length,
4546 &tag_size ),
4547 PSA_ERROR_INVALID_ARGUMENT );
4548
4549 psa_aead_abort( &operation );
4550
Paul Elliott243080c2021-07-21 19:01:17 +01004551 /* Test for not sending any additional data or data after setting non-zero
4552 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004553
4554 operation = psa_aead_operation_init( );
4555
4556 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4557
4558 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4559
4560 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4561 input_data->len ) );
4562
4563 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4564 finish_output_size,
4565 &output_part_length,
4566 tag_buffer,
4567 tag_length ),
4568 PSA_ERROR_INVALID_ARGUMENT );
4569
4570 psa_aead_abort( &operation );
4571
Paul Elliott243080c2021-07-21 19:01:17 +01004572 /* Test for not sending any additional data after setting a non-zero length
4573 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004574
4575 operation = psa_aead_operation_init( );
4576
4577 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4578
4579 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4580
4581 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4582 input_data->len ) );
4583
4584 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4585 input_data->len, output_data,
4586 output_size, &output_length ),
4587 PSA_ERROR_INVALID_ARGUMENT );
4588
4589 psa_aead_abort( &operation );
4590
Paul Elliottf94bd992021-09-19 18:15:59 +01004591 /* Test for not sending any data after setting a non-zero length for it.*/
4592
4593 operation = psa_aead_operation_init( );
4594
4595 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4596
4597 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4598
4599 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4600 input_data->len ) );
4601
4602 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4603 additional_data->len ) );
4604
4605 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4606 finish_output_size,
4607 &output_part_length,
4608 tag_buffer, tag_length,
4609 &tag_size ),
4610 PSA_ERROR_INVALID_ARGUMENT );
4611
4612 psa_aead_abort( &operation );
4613
Paul Elliottb0450fe2021-09-01 15:06:26 +01004614 /* Test for sending too much additional data after setting lengths. */
4615
4616 operation = psa_aead_operation_init( );
4617
4618 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4619
4620 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4621
4622 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4623
4624
4625 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4626 additional_data->len ),
4627 PSA_ERROR_INVALID_ARGUMENT );
4628
4629 psa_aead_abort( &operation );
4630
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004631 operation = psa_aead_operation_init( );
4632
4633 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4634
4635 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4636
4637 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4638 input_data->len ) );
4639
4640 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4641 additional_data->len ) );
4642
4643 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4644 1 ),
4645 PSA_ERROR_INVALID_ARGUMENT );
4646
4647 psa_aead_abort( &operation );
4648
Paul Elliottb0450fe2021-09-01 15:06:26 +01004649 /* Test for sending too much data after setting lengths. */
4650
4651 operation = psa_aead_operation_init( );
4652
4653 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4654
4655 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4656
4657 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4658
4659 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4660 input_data->len, output_data,
4661 output_size, &output_length ),
4662 PSA_ERROR_INVALID_ARGUMENT );
4663
4664 psa_aead_abort( &operation );
4665
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004666 operation = psa_aead_operation_init( );
4667
4668 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4669
4670 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4671
4672 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4673 input_data->len ) );
4674
4675 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4676 additional_data->len ) );
4677
4678 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4679 input_data->len, output_data,
4680 output_size, &output_length ) );
4681
4682 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4683 1, output_data,
4684 output_size, &output_length ),
4685 PSA_ERROR_INVALID_ARGUMENT );
4686
4687 psa_aead_abort( &operation );
4688
Paul Elliottc23a9a02021-06-21 18:32:46 +01004689 /* Test sending additional data after data. */
4690
4691 operation = psa_aead_operation_init( );
4692
4693 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4694
4695 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4696
4697 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4698 input_data->len, output_data,
4699 output_size, &output_length ) );
4700
4701 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4702 additional_data->len ),
4703 PSA_ERROR_BAD_STATE );
4704
4705 psa_aead_abort( &operation );
4706
Paul Elliott534d0b42021-06-22 19:15:20 +01004707 /* Test calling finish on decryption. */
4708
4709 operation = psa_aead_operation_init( );
4710
4711 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4712
4713 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4714
4715 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4716 finish_output_size,
4717 &output_part_length,
4718 tag_buffer, tag_length,
4719 &tag_size ),
4720 PSA_ERROR_BAD_STATE );
4721
4722 psa_aead_abort( &operation );
4723
4724 /* Test calling verify on encryption. */
4725
4726 operation = psa_aead_operation_init( );
4727
4728 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4729
4730 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4731
4732 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4733 finish_output_size,
4734 &output_part_length,
4735 tag_buffer,
4736 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004737 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004738
4739 psa_aead_abort( &operation );
4740
4741
Paul Elliottc23a9a02021-06-21 18:32:46 +01004742exit:
4743 psa_destroy_key( key );
4744 psa_aead_abort( &operation );
4745 mbedtls_free( output_data );
4746 mbedtls_free( final_data );
4747 PSA_DONE( );
4748}
4749/* END_CASE */
4750
4751/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004752void signature_size( int type_arg,
4753 int bits,
4754 int alg_arg,
4755 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004756{
4757 psa_key_type_t type = type_arg;
4758 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004759 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004760
Gilles Peskinefe11b722018-12-18 00:24:04 +01004761 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004762
Gilles Peskinee59236f2018-01-27 23:32:46 +01004763exit:
4764 ;
4765}
4766/* END_CASE */
4767
4768/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004769void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4770 int alg_arg, data_t *input_data,
4771 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004772{
Ronald Cron5425a212020-08-04 14:58:35 +02004773 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004774 psa_key_type_t key_type = key_type_arg;
4775 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004776 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004777 unsigned char *signature = NULL;
4778 size_t signature_size;
4779 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004780 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004781
Gilles Peskine8817f612018-12-18 00:18:46 +01004782 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004783
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004784 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004785 psa_set_key_algorithm( &attributes, alg );
4786 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004787
Gilles Peskine049c7532019-05-15 20:22:09 +02004788 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004789 &key ) );
4790 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004791 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004792
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004793 /* Allocate a buffer which has the size advertized by the
4794 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004795 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004796 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004797 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004798 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004799 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004800
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004801 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004802 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004803 input_data->x, input_data->len,
4804 signature, signature_size,
4805 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004806 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004807 ASSERT_COMPARE( output_data->x, output_data->len,
4808 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004809
4810exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004811 /*
4812 * Key attributes may have been returned by psa_get_key_attributes()
4813 * thus reset them as required.
4814 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004815 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004816
Ronald Cron5425a212020-08-04 14:58:35 +02004817 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004818 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004819 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004820}
4821/* END_CASE */
4822
4823/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004824void sign_hash_fail( int key_type_arg, data_t *key_data,
4825 int alg_arg, data_t *input_data,
4826 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004827{
Ronald Cron5425a212020-08-04 14:58:35 +02004828 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004829 psa_key_type_t key_type = key_type_arg;
4830 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004831 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004832 psa_status_t actual_status;
4833 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004834 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004835 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004837
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004838 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004839
Gilles Peskine8817f612018-12-18 00:18:46 +01004840 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004841
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004842 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004843 psa_set_key_algorithm( &attributes, alg );
4844 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004845
Gilles Peskine049c7532019-05-15 20:22:09 +02004846 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004847 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004848
Ronald Cron5425a212020-08-04 14:58:35 +02004849 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004850 input_data->x, input_data->len,
4851 signature, signature_size,
4852 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004853 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004854 /* The value of *signature_length is unspecified on error, but
4855 * whatever it is, it should be less than signature_size, so that
4856 * if the caller tries to read *signature_length bytes without
4857 * checking the error code then they don't overflow a buffer. */
4858 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004859
4860exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004861 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004862 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004863 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004864 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004865}
4866/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004867
4868/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004869void sign_verify_hash( int key_type_arg, data_t *key_data,
4870 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004871{
Ronald Cron5425a212020-08-04 14:58:35 +02004872 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004873 psa_key_type_t key_type = key_type_arg;
4874 psa_algorithm_t alg = alg_arg;
4875 size_t key_bits;
4876 unsigned char *signature = NULL;
4877 size_t signature_size;
4878 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004880
Gilles Peskine8817f612018-12-18 00:18:46 +01004881 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004882
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004884 psa_set_key_algorithm( &attributes, alg );
4885 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004886
Gilles Peskine049c7532019-05-15 20:22:09 +02004887 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004888 &key ) );
4889 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004890 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004891
4892 /* Allocate a buffer which has the size advertized by the
4893 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004894 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004895 key_bits, alg );
4896 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004897 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004898 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004899
4900 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004901 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004902 input_data->x, input_data->len,
4903 signature, signature_size,
4904 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004905 /* Check that the signature length looks sensible. */
4906 TEST_ASSERT( signature_length <= signature_size );
4907 TEST_ASSERT( signature_length > 0 );
4908
4909 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004910 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004911 input_data->x, input_data->len,
4912 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004913
4914 if( input_data->len != 0 )
4915 {
4916 /* Flip a bit in the input and verify that the signature is now
4917 * detected as invalid. Flip a bit at the beginning, not at the end,
4918 * because ECDSA may ignore the last few bits of the input. */
4919 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004920 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004921 input_data->x, input_data->len,
4922 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004923 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004924 }
4925
4926exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004927 /*
4928 * Key attributes may have been returned by psa_get_key_attributes()
4929 * thus reset them as required.
4930 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004931 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004932
Ronald Cron5425a212020-08-04 14:58:35 +02004933 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004934 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004935 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004936}
4937/* END_CASE */
4938
4939/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004940void verify_hash( int key_type_arg, data_t *key_data,
4941 int alg_arg, data_t *hash_data,
4942 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004943{
Ronald Cron5425a212020-08-04 14:58:35 +02004944 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004945 psa_key_type_t key_type = key_type_arg;
4946 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004947 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004948
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004949 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004950
Gilles Peskine8817f612018-12-18 00:18:46 +01004951 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004952
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004953 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004954 psa_set_key_algorithm( &attributes, alg );
4955 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004956
Gilles Peskine049c7532019-05-15 20:22:09 +02004957 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004958 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004959
Ronald Cron5425a212020-08-04 14:58:35 +02004960 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004961 hash_data->x, hash_data->len,
4962 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004963
itayzafrir5c753392018-05-08 11:18:38 +03004964exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004965 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004966 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004967 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004968}
4969/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004970
4971/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004972void verify_hash_fail( int key_type_arg, data_t *key_data,
4973 int alg_arg, data_t *hash_data,
4974 data_t *signature_data,
4975 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004976{
Ronald Cron5425a212020-08-04 14:58:35 +02004977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004978 psa_key_type_t key_type = key_type_arg;
4979 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004980 psa_status_t actual_status;
4981 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004983
Gilles Peskine8817f612018-12-18 00:18:46 +01004984 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004985
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004986 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004987 psa_set_key_algorithm( &attributes, alg );
4988 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004989
Gilles Peskine049c7532019-05-15 20:22:09 +02004990 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004991 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004992
Ronald Cron5425a212020-08-04 14:58:35 +02004993 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004994 hash_data->x, hash_data->len,
4995 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004996 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004997
4998exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004999 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005000 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005001 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005002}
5003/* END_CASE */
5004
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005005/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005006void sign_message_deterministic( int key_type_arg,
5007 data_t *key_data,
5008 int alg_arg,
5009 data_t *input_data,
5010 data_t *output_data )
5011{
5012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5013 psa_key_type_t key_type = key_type_arg;
5014 psa_algorithm_t alg = alg_arg;
5015 size_t key_bits;
5016 unsigned char *signature = NULL;
5017 size_t signature_size;
5018 size_t signature_length = 0xdeadbeef;
5019 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5020
5021 PSA_ASSERT( psa_crypto_init( ) );
5022
5023 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5024 psa_set_key_algorithm( &attributes, alg );
5025 psa_set_key_type( &attributes, key_type );
5026
5027 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5028 &key ) );
5029 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5030 key_bits = psa_get_key_bits( &attributes );
5031
5032 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5033 TEST_ASSERT( signature_size != 0 );
5034 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5035 ASSERT_ALLOC( signature, signature_size );
5036
5037 PSA_ASSERT( psa_sign_message( key, alg,
5038 input_data->x, input_data->len,
5039 signature, signature_size,
5040 &signature_length ) );
5041
5042 ASSERT_COMPARE( output_data->x, output_data->len,
5043 signature, signature_length );
5044
5045exit:
5046 psa_reset_key_attributes( &attributes );
5047
5048 psa_destroy_key( key );
5049 mbedtls_free( signature );
5050 PSA_DONE( );
5051
5052}
5053/* END_CASE */
5054
5055/* BEGIN_CASE */
5056void sign_message_fail( int key_type_arg,
5057 data_t *key_data,
5058 int alg_arg,
5059 data_t *input_data,
5060 int signature_size_arg,
5061 int expected_status_arg )
5062{
5063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5064 psa_key_type_t key_type = key_type_arg;
5065 psa_algorithm_t alg = alg_arg;
5066 size_t signature_size = signature_size_arg;
5067 psa_status_t actual_status;
5068 psa_status_t expected_status = expected_status_arg;
5069 unsigned char *signature = NULL;
5070 size_t signature_length = 0xdeadbeef;
5071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5072
5073 ASSERT_ALLOC( signature, signature_size );
5074
5075 PSA_ASSERT( psa_crypto_init( ) );
5076
5077 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5078 psa_set_key_algorithm( &attributes, alg );
5079 psa_set_key_type( &attributes, key_type );
5080
5081 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5082 &key ) );
5083
5084 actual_status = psa_sign_message( key, alg,
5085 input_data->x, input_data->len,
5086 signature, signature_size,
5087 &signature_length );
5088 TEST_EQUAL( actual_status, expected_status );
5089 /* The value of *signature_length is unspecified on error, but
5090 * whatever it is, it should be less than signature_size, so that
5091 * if the caller tries to read *signature_length bytes without
5092 * checking the error code then they don't overflow a buffer. */
5093 TEST_ASSERT( signature_length <= signature_size );
5094
5095exit:
5096 psa_reset_key_attributes( &attributes );
5097 psa_destroy_key( key );
5098 mbedtls_free( signature );
5099 PSA_DONE( );
5100}
5101/* END_CASE */
5102
5103/* BEGIN_CASE */
5104void sign_verify_message( int key_type_arg,
5105 data_t *key_data,
5106 int alg_arg,
5107 data_t *input_data )
5108{
5109 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5110 psa_key_type_t key_type = key_type_arg;
5111 psa_algorithm_t alg = alg_arg;
5112 size_t key_bits;
5113 unsigned char *signature = NULL;
5114 size_t signature_size;
5115 size_t signature_length = 0xdeadbeef;
5116 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5117
5118 PSA_ASSERT( psa_crypto_init( ) );
5119
5120 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5121 PSA_KEY_USAGE_VERIFY_MESSAGE );
5122 psa_set_key_algorithm( &attributes, alg );
5123 psa_set_key_type( &attributes, key_type );
5124
5125 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5126 &key ) );
5127 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5128 key_bits = psa_get_key_bits( &attributes );
5129
5130 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5131 TEST_ASSERT( signature_size != 0 );
5132 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5133 ASSERT_ALLOC( signature, signature_size );
5134
5135 PSA_ASSERT( psa_sign_message( key, alg,
5136 input_data->x, input_data->len,
5137 signature, signature_size,
5138 &signature_length ) );
5139 TEST_ASSERT( signature_length <= signature_size );
5140 TEST_ASSERT( signature_length > 0 );
5141
5142 PSA_ASSERT( psa_verify_message( key, alg,
5143 input_data->x, input_data->len,
5144 signature, signature_length ) );
5145
5146 if( input_data->len != 0 )
5147 {
5148 /* Flip a bit in the input and verify that the signature is now
5149 * detected as invalid. Flip a bit at the beginning, not at the end,
5150 * because ECDSA may ignore the last few bits of the input. */
5151 input_data->x[0] ^= 1;
5152 TEST_EQUAL( psa_verify_message( key, alg,
5153 input_data->x, input_data->len,
5154 signature, signature_length ),
5155 PSA_ERROR_INVALID_SIGNATURE );
5156 }
5157
5158exit:
5159 psa_reset_key_attributes( &attributes );
5160
5161 psa_destroy_key( key );
5162 mbedtls_free( signature );
5163 PSA_DONE( );
5164}
5165/* END_CASE */
5166
5167/* BEGIN_CASE */
5168void verify_message( int key_type_arg,
5169 data_t *key_data,
5170 int alg_arg,
5171 data_t *input_data,
5172 data_t *signature_data )
5173{
5174 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5175 psa_key_type_t key_type = key_type_arg;
5176 psa_algorithm_t alg = alg_arg;
5177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5178
5179 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5180
5181 PSA_ASSERT( psa_crypto_init( ) );
5182
5183 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5184 psa_set_key_algorithm( &attributes, alg );
5185 psa_set_key_type( &attributes, key_type );
5186
5187 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5188 &key ) );
5189
5190 PSA_ASSERT( psa_verify_message( key, alg,
5191 input_data->x, input_data->len,
5192 signature_data->x, signature_data->len ) );
5193
5194exit:
5195 psa_reset_key_attributes( &attributes );
5196 psa_destroy_key( key );
5197 PSA_DONE( );
5198}
5199/* END_CASE */
5200
5201/* BEGIN_CASE */
5202void verify_message_fail( int key_type_arg,
5203 data_t *key_data,
5204 int alg_arg,
5205 data_t *hash_data,
5206 data_t *signature_data,
5207 int expected_status_arg )
5208{
5209 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5210 psa_key_type_t key_type = key_type_arg;
5211 psa_algorithm_t alg = alg_arg;
5212 psa_status_t actual_status;
5213 psa_status_t expected_status = expected_status_arg;
5214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5215
5216 PSA_ASSERT( psa_crypto_init( ) );
5217
5218 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5219 psa_set_key_algorithm( &attributes, alg );
5220 psa_set_key_type( &attributes, key_type );
5221
5222 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5223 &key ) );
5224
5225 actual_status = psa_verify_message( key, alg,
5226 hash_data->x, hash_data->len,
5227 signature_data->x,
5228 signature_data->len );
5229 TEST_EQUAL( actual_status, expected_status );
5230
5231exit:
5232 psa_reset_key_attributes( &attributes );
5233 psa_destroy_key( key );
5234 PSA_DONE( );
5235}
5236/* END_CASE */
5237
5238/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005239void asymmetric_encrypt( int key_type_arg,
5240 data_t *key_data,
5241 int alg_arg,
5242 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005243 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005244 int expected_output_length_arg,
5245 int expected_status_arg )
5246{
Ronald Cron5425a212020-08-04 14:58:35 +02005247 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005248 psa_key_type_t key_type = key_type_arg;
5249 psa_algorithm_t alg = alg_arg;
5250 size_t expected_output_length = expected_output_length_arg;
5251 size_t key_bits;
5252 unsigned char *output = NULL;
5253 size_t output_size;
5254 size_t output_length = ~0;
5255 psa_status_t actual_status;
5256 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005258
Gilles Peskine8817f612018-12-18 00:18:46 +01005259 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005260
Gilles Peskine656896e2018-06-29 19:12:28 +02005261 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005262 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5263 psa_set_key_algorithm( &attributes, alg );
5264 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005265 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005266 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005267
5268 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005269 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005270 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005271
Gilles Peskine656896e2018-06-29 19:12:28 +02005272 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005273 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005274 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005275
5276 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005277 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005278 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005279 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005280 output, output_size,
5281 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005282 TEST_EQUAL( actual_status, expected_status );
5283 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005284
Gilles Peskine68428122018-06-30 18:42:41 +02005285 /* If the label is empty, the test framework puts a non-null pointer
5286 * in label->x. Test that a null pointer works as well. */
5287 if( label->len == 0 )
5288 {
5289 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005290 if( output_size != 0 )
5291 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005292 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005293 input_data->x, input_data->len,
5294 NULL, label->len,
5295 output, output_size,
5296 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005297 TEST_EQUAL( actual_status, expected_status );
5298 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005299 }
5300
Gilles Peskine656896e2018-06-29 19:12:28 +02005301exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005302 /*
5303 * Key attributes may have been returned by psa_get_key_attributes()
5304 * thus reset them as required.
5305 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005306 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005307
Ronald Cron5425a212020-08-04 14:58:35 +02005308 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005309 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005310 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005311}
5312/* END_CASE */
5313
5314/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005315void asymmetric_encrypt_decrypt( int key_type_arg,
5316 data_t *key_data,
5317 int alg_arg,
5318 data_t *input_data,
5319 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005320{
Ronald Cron5425a212020-08-04 14:58:35 +02005321 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005322 psa_key_type_t key_type = key_type_arg;
5323 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005324 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005325 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005326 size_t output_size;
5327 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005328 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005329 size_t output2_size;
5330 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005332
Gilles Peskine8817f612018-12-18 00:18:46 +01005333 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005334
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005335 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5336 psa_set_key_algorithm( &attributes, alg );
5337 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005338
Gilles Peskine049c7532019-05-15 20:22:09 +02005339 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005340 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005341
5342 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005343 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005344 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005345
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005346 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005347 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005348 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005349
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005350 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005351 TEST_ASSERT( output2_size <=
5352 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5353 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005354 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005355
Gilles Peskineeebd7382018-06-08 18:11:54 +02005356 /* We test encryption by checking that encrypt-then-decrypt gives back
5357 * the original plaintext because of the non-optional random
5358 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005359 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005360 input_data->x, input_data->len,
5361 label->x, label->len,
5362 output, output_size,
5363 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005364 /* We don't know what ciphertext length to expect, but check that
5365 * it looks sensible. */
5366 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005367
Ronald Cron5425a212020-08-04 14:58:35 +02005368 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005369 output, output_length,
5370 label->x, label->len,
5371 output2, output2_size,
5372 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005373 ASSERT_COMPARE( input_data->x, input_data->len,
5374 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005375
5376exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005377 /*
5378 * Key attributes may have been returned by psa_get_key_attributes()
5379 * thus reset them as required.
5380 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005381 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005382
Ronald Cron5425a212020-08-04 14:58:35 +02005383 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005384 mbedtls_free( output );
5385 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005386 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005387}
5388/* END_CASE */
5389
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005390/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005391void asymmetric_decrypt( int key_type_arg,
5392 data_t *key_data,
5393 int alg_arg,
5394 data_t *input_data,
5395 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005396 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005397{
Ronald Cron5425a212020-08-04 14:58:35 +02005398 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005399 psa_key_type_t key_type = key_type_arg;
5400 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005401 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005402 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005403 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005404 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005406
Gilles Peskine8817f612018-12-18 00:18:46 +01005407 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005408
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005409 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5410 psa_set_key_algorithm( &attributes, alg );
5411 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005412
Gilles Peskine049c7532019-05-15 20:22:09 +02005413 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005414 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005415
gabor-mezei-armceface22021-01-21 12:26:17 +01005416 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5417 key_bits = psa_get_key_bits( &attributes );
5418
5419 /* Determine the maximum ciphertext length */
5420 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5421 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5422 ASSERT_ALLOC( output, output_size );
5423
Ronald Cron5425a212020-08-04 14:58:35 +02005424 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005425 input_data->x, input_data->len,
5426 label->x, label->len,
5427 output,
5428 output_size,
5429 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005430 ASSERT_COMPARE( expected_data->x, expected_data->len,
5431 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005432
Gilles Peskine68428122018-06-30 18:42:41 +02005433 /* If the label is empty, the test framework puts a non-null pointer
5434 * in label->x. Test that a null pointer works as well. */
5435 if( label->len == 0 )
5436 {
5437 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005438 if( output_size != 0 )
5439 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005440 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005441 input_data->x, input_data->len,
5442 NULL, label->len,
5443 output,
5444 output_size,
5445 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005446 ASSERT_COMPARE( expected_data->x, expected_data->len,
5447 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005448 }
5449
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005450exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005451 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005452 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005453 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005454 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005455}
5456/* END_CASE */
5457
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005458/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005459void asymmetric_decrypt_fail( int key_type_arg,
5460 data_t *key_data,
5461 int alg_arg,
5462 data_t *input_data,
5463 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005464 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005465 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005466{
Ronald Cron5425a212020-08-04 14:58:35 +02005467 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005468 psa_key_type_t key_type = key_type_arg;
5469 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005470 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005471 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005472 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005473 psa_status_t actual_status;
5474 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005475 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005476
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005477 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005478
Gilles Peskine8817f612018-12-18 00:18:46 +01005479 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005480
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005481 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5482 psa_set_key_algorithm( &attributes, alg );
5483 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005484
Gilles Peskine049c7532019-05-15 20:22:09 +02005485 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005486 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005487
Ronald Cron5425a212020-08-04 14:58:35 +02005488 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005489 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005490 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005491 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005492 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005493 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005494 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005495
Gilles Peskine68428122018-06-30 18:42:41 +02005496 /* If the label is empty, the test framework puts a non-null pointer
5497 * in label->x. Test that a null pointer works as well. */
5498 if( label->len == 0 )
5499 {
5500 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005501 if( output_size != 0 )
5502 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005503 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005504 input_data->x, input_data->len,
5505 NULL, label->len,
5506 output, output_size,
5507 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005508 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005509 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005510 }
5511
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005512exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005513 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005514 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005515 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005516 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005517}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005518/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005519
5520/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005521void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005522{
5523 /* Test each valid way of initializing the object, except for `= {0}`, as
5524 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5525 * though it's OK by the C standard. We could test for this, but we'd need
5526 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005527 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005528 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5529 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5530 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005531
5532 memset( &zero, 0, sizeof( zero ) );
5533
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005534 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005535 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005536 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005537 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005538 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005539 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005540 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005541
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005542 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005543 PSA_ASSERT( psa_key_derivation_abort(&func) );
5544 PSA_ASSERT( psa_key_derivation_abort(&init) );
5545 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005546}
5547/* END_CASE */
5548
Janos Follath16de4a42019-06-13 16:32:24 +01005549/* BEGIN_CASE */
5550void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005551{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005552 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005553 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005554 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005555
Gilles Peskine8817f612018-12-18 00:18:46 +01005556 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005557
Janos Follath16de4a42019-06-13 16:32:24 +01005558 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005559 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005560
5561exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005562 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005563 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005564}
5565/* END_CASE */
5566
Janos Follathaf3c2a02019-06-12 12:34:34 +01005567/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005568void derive_set_capacity( int alg_arg, int capacity_arg,
5569 int expected_status_arg )
5570{
5571 psa_algorithm_t alg = alg_arg;
5572 size_t capacity = capacity_arg;
5573 psa_status_t expected_status = expected_status_arg;
5574 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5575
5576 PSA_ASSERT( psa_crypto_init( ) );
5577
5578 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5579
5580 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5581 expected_status );
5582
5583exit:
5584 psa_key_derivation_abort( &operation );
5585 PSA_DONE( );
5586}
5587/* END_CASE */
5588
5589/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005590void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005591 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005592 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005593 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005594 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005595 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005596 int expected_status_arg3,
5597 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005598{
5599 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005600 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5601 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005602 psa_status_t expected_statuses[] = {expected_status_arg1,
5603 expected_status_arg2,
5604 expected_status_arg3};
5605 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005606 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5607 MBEDTLS_SVC_KEY_ID_INIT,
5608 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005609 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5611 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005612 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005613 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005614 psa_status_t expected_output_status = expected_output_status_arg;
5615 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005616
5617 PSA_ASSERT( psa_crypto_init( ) );
5618
5619 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5620 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005621
5622 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5623
5624 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5625 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005626 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005627 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005628 psa_set_key_type( &attributes, key_types[i] );
5629 PSA_ASSERT( psa_import_key( &attributes,
5630 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005631 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005632 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5633 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5634 {
5635 // When taking a private key as secret input, use key agreement
5636 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005637 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5638 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005639 expected_statuses[i] );
5640 }
5641 else
5642 {
5643 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005644 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005645 expected_statuses[i] );
5646 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005647 }
5648 else
5649 {
5650 TEST_EQUAL( psa_key_derivation_input_bytes(
5651 &operation, steps[i],
5652 inputs[i]->x, inputs[i]->len ),
5653 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005654 }
5655 }
5656
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005657 if( output_key_type != PSA_KEY_TYPE_NONE )
5658 {
5659 psa_reset_key_attributes( &attributes );
5660 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5661 psa_set_key_bits( &attributes, 8 );
5662 actual_output_status =
5663 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005664 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005665 }
5666 else
5667 {
5668 uint8_t buffer[1];
5669 actual_output_status =
5670 psa_key_derivation_output_bytes( &operation,
5671 buffer, sizeof( buffer ) );
5672 }
5673 TEST_EQUAL( actual_output_status, expected_output_status );
5674
Janos Follathaf3c2a02019-06-12 12:34:34 +01005675exit:
5676 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005677 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5678 psa_destroy_key( keys[i] );
5679 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005680 PSA_DONE( );
5681}
5682/* END_CASE */
5683
Janos Follathd958bb72019-07-03 15:02:16 +01005684/* BEGIN_CASE */
5685void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005686{
Janos Follathd958bb72019-07-03 15:02:16 +01005687 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005689 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005690 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005691 unsigned char input1[] = "Input 1";
5692 size_t input1_length = sizeof( input1 );
5693 unsigned char input2[] = "Input 2";
5694 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005695 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005696 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005697 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5698 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5699 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005701
Gilles Peskine8817f612018-12-18 00:18:46 +01005702 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005703
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005704 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5705 psa_set_key_algorithm( &attributes, alg );
5706 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005707
Gilles Peskine73676cb2019-05-15 20:15:10 +02005708 PSA_ASSERT( psa_import_key( &attributes,
5709 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005710 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005711
5712 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005713 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5714 input1, input1_length,
5715 input2, input2_length,
5716 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005717 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005718
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005719 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005720 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005721 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005722
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005724
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005725 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005726 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005727
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005728exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005729 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005730 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005731 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005732}
5733/* END_CASE */
5734
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005735/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005736void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005737{
5738 uint8_t output_buffer[16];
5739 size_t buffer_size = 16;
5740 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005741 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005742
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005743 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5744 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005745 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005746
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005747 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005748 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005749
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005750 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005751
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005752 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5753 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005754 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005755
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005756 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005757 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005758
5759exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005760 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005761}
5762/* END_CASE */
5763
5764/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005765void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005766 int step1_arg, data_t *input1,
5767 int step2_arg, data_t *input2,
5768 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005769 int requested_capacity_arg,
5770 data_t *expected_output1,
5771 data_t *expected_output2 )
5772{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005773 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005774 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5775 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005776 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5777 MBEDTLS_SVC_KEY_ID_INIT,
5778 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005779 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005780 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005781 uint8_t *expected_outputs[2] =
5782 {expected_output1->x, expected_output2->x};
5783 size_t output_sizes[2] =
5784 {expected_output1->len, expected_output2->len};
5785 size_t output_buffer_size = 0;
5786 uint8_t *output_buffer = NULL;
5787 size_t expected_capacity;
5788 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005790 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005791 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005792
5793 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5794 {
5795 if( output_sizes[i] > output_buffer_size )
5796 output_buffer_size = output_sizes[i];
5797 if( output_sizes[i] == 0 )
5798 expected_outputs[i] = NULL;
5799 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005800 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005801 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005802
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005803 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5804 psa_set_key_algorithm( &attributes, alg );
5805 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005806
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005807 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005808 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5809 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5810 requested_capacity ) );
5811 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005812 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005813 switch( steps[i] )
5814 {
5815 case 0:
5816 break;
5817 case PSA_KEY_DERIVATION_INPUT_SECRET:
5818 PSA_ASSERT( psa_import_key( &attributes,
5819 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005820 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005821
5822 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5823 {
5824 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5825 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5826 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5827 }
5828
Gilles Peskine1468da72019-05-29 17:35:49 +02005829 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005830 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005831 break;
5832 default:
5833 PSA_ASSERT( psa_key_derivation_input_bytes(
5834 &operation, steps[i],
5835 inputs[i]->x, inputs[i]->len ) );
5836 break;
5837 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005838 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005839
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005840 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005841 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005842 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005843 expected_capacity = requested_capacity;
5844
5845 /* Expansion phase. */
5846 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5847 {
5848 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005849 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005850 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005851 if( expected_capacity == 0 && output_sizes[i] == 0 )
5852 {
5853 /* Reading 0 bytes when 0 bytes are available can go either way. */
5854 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005855 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005856 continue;
5857 }
5858 else if( expected_capacity == 0 ||
5859 output_sizes[i] > expected_capacity )
5860 {
5861 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005862 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005863 expected_capacity = 0;
5864 continue;
5865 }
5866 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005867 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005868 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005869 ASSERT_COMPARE( output_buffer, output_sizes[i],
5870 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005871 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005872 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005873 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005874 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005875 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005876 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005877 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005878
5879exit:
5880 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005881 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005882 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5883 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005884 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005885}
5886/* END_CASE */
5887
5888/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005889void derive_full( int alg_arg,
5890 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005891 data_t *input1,
5892 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005893 int requested_capacity_arg )
5894{
Ronald Cron5425a212020-08-04 14:58:35 +02005895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005896 psa_algorithm_t alg = alg_arg;
5897 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005898 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005899 unsigned char output_buffer[16];
5900 size_t expected_capacity = requested_capacity;
5901 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005902 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005903
Gilles Peskine8817f612018-12-18 00:18:46 +01005904 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005905
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005906 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5907 psa_set_key_algorithm( &attributes, alg );
5908 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005909
Gilles Peskine049c7532019-05-15 20:22:09 +02005910 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005911 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005912
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005913 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5914 input1->x, input1->len,
5915 input2->x, input2->len,
5916 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005917 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005918
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005919 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005920 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005921 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005922
5923 /* Expansion phase. */
5924 while( current_capacity > 0 )
5925 {
5926 size_t read_size = sizeof( output_buffer );
5927 if( read_size > current_capacity )
5928 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005929 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005930 output_buffer,
5931 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005932 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005933 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005934 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005935 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005936 }
5937
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005938 /* Check that the operation refuses to go over capacity. */
5939 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005940 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005941
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005942 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005943
5944exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005945 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005946 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005947 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005948}
5949/* END_CASE */
5950
Janos Follathe60c9052019-07-03 13:51:30 +01005951/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005952void derive_key_exercise( int alg_arg,
5953 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005954 data_t *input1,
5955 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005956 int derived_type_arg,
5957 int derived_bits_arg,
5958 int derived_usage_arg,
5959 int derived_alg_arg )
5960{
Ronald Cron5425a212020-08-04 14:58:35 +02005961 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5962 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005963 psa_algorithm_t alg = alg_arg;
5964 psa_key_type_t derived_type = derived_type_arg;
5965 size_t derived_bits = derived_bits_arg;
5966 psa_key_usage_t derived_usage = derived_usage_arg;
5967 psa_algorithm_t derived_alg = derived_alg_arg;
5968 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005969 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005970 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005971 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005972
Gilles Peskine8817f612018-12-18 00:18:46 +01005973 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005974
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005975 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5976 psa_set_key_algorithm( &attributes, alg );
5977 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005978 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005979 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005980
5981 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005982 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5983 input1->x, input1->len,
5984 input2->x, input2->len,
5985 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005986 goto exit;
5987
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005988 psa_set_key_usage_flags( &attributes, derived_usage );
5989 psa_set_key_algorithm( &attributes, derived_alg );
5990 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005991 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005992 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005993 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005994
5995 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005996 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005997 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5998 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005999
6000 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006001 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006002 goto exit;
6003
6004exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006005 /*
6006 * Key attributes may have been returned by psa_get_key_attributes()
6007 * thus reset them as required.
6008 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006009 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006010
6011 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006012 psa_destroy_key( base_key );
6013 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006014 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006015}
6016/* END_CASE */
6017
Janos Follath42fd8882019-07-03 14:17:09 +01006018/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006019void derive_key_export( int alg_arg,
6020 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006021 data_t *input1,
6022 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006023 int bytes1_arg,
6024 int bytes2_arg )
6025{
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 Peskine0386fba2018-07-12 17:29:22 +02006028 psa_algorithm_t alg = alg_arg;
6029 size_t bytes1 = bytes1_arg;
6030 size_t bytes2 = bytes2_arg;
6031 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006032 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006033 uint8_t *output_buffer = NULL;
6034 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006035 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6036 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006037 size_t length;
6038
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006039 ASSERT_ALLOC( output_buffer, capacity );
6040 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006041 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006042
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006043 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6044 psa_set_key_algorithm( &base_attributes, alg );
6045 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006046 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006047 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006048
6049 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006050 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6051 input1->x, input1->len,
6052 input2->x, input2->len,
6053 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006054 goto exit;
6055
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006056 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006057 output_buffer,
6058 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006059 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006060
6061 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006062 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6063 input1->x, input1->len,
6064 input2->x, input2->len,
6065 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006066 goto exit;
6067
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006068 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6069 psa_set_key_algorithm( &derived_attributes, 0 );
6070 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006071 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006072 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006073 &derived_key ) );
6074 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006075 export_buffer, bytes1,
6076 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006077 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006078 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006079 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006080 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006081 &derived_key ) );
6082 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006083 export_buffer + bytes1, bytes2,
6084 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006085 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006086
6087 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006088 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6089 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006090
6091exit:
6092 mbedtls_free( output_buffer );
6093 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006094 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006095 psa_destroy_key( base_key );
6096 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006097 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006098}
6099/* END_CASE */
6100
6101/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006102void derive_key( int alg_arg,
6103 data_t *key_data, data_t *input1, data_t *input2,
6104 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006105 int expected_status_arg,
6106 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006107{
Ronald Cron5425a212020-08-04 14:58:35 +02006108 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6109 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006110 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006111 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006112 size_t bits = bits_arg;
6113 psa_status_t expected_status = expected_status_arg;
6114 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6115 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6116 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6117
6118 PSA_ASSERT( psa_crypto_init( ) );
6119
6120 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6121 psa_set_key_algorithm( &base_attributes, alg );
6122 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6123 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006124 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006125
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006126 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6127 input1->x, input1->len,
6128 input2->x, input2->len,
6129 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006130 goto exit;
6131
6132 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6133 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006134 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006135 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006136
6137 psa_status_t status =
6138 psa_key_derivation_output_key( &derived_attributes,
6139 &operation,
6140 &derived_key );
6141 if( is_large_output > 0 )
6142 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6143 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006144
6145exit:
6146 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006147 psa_destroy_key( base_key );
6148 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006149 PSA_DONE( );
6150}
6151/* END_CASE */
6152
6153/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006154void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006155 int our_key_type_arg, int our_key_alg_arg,
6156 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006157 int expected_status_arg )
6158{
Ronald Cron5425a212020-08-04 14:58:35 +02006159 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006160 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006161 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006162 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006163 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006165 psa_status_t expected_status = expected_status_arg;
6166 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006167
Gilles Peskine8817f612018-12-18 00:18:46 +01006168 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006169
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006170 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006171 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006172 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006173 PSA_ASSERT( psa_import_key( &attributes,
6174 our_key_data->x, our_key_data->len,
6175 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006176
Gilles Peskine77f40d82019-04-11 21:27:06 +02006177 /* The tests currently include inputs that should fail at either step.
6178 * Test cases that fail at the setup step should be changed to call
6179 * key_derivation_setup instead, and this function should be renamed
6180 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006181 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006182 if( status == PSA_SUCCESS )
6183 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006184 TEST_EQUAL( psa_key_derivation_key_agreement(
6185 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6186 our_key,
6187 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006188 expected_status );
6189 }
6190 else
6191 {
6192 TEST_ASSERT( status == expected_status );
6193 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006194
6195exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006196 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006197 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006198 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006199}
6200/* END_CASE */
6201
6202/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006203void raw_key_agreement( int alg_arg,
6204 int our_key_type_arg, data_t *our_key_data,
6205 data_t *peer_key_data,
6206 data_t *expected_output )
6207{
Ronald Cron5425a212020-08-04 14:58:35 +02006208 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006209 psa_algorithm_t alg = alg_arg;
6210 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006212 unsigned char *output = NULL;
6213 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006214 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006215
6216 ASSERT_ALLOC( output, expected_output->len );
6217 PSA_ASSERT( psa_crypto_init( ) );
6218
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6220 psa_set_key_algorithm( &attributes, alg );
6221 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006222 PSA_ASSERT( psa_import_key( &attributes,
6223 our_key_data->x, our_key_data->len,
6224 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006225
gabor-mezei-armceface22021-01-21 12:26:17 +01006226 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6227 key_bits = psa_get_key_bits( &attributes );
6228
Gilles Peskinebe697d82019-05-16 18:00:41 +02006229 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6230 peer_key_data->x, peer_key_data->len,
6231 output, expected_output->len,
6232 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006233 ASSERT_COMPARE( output, output_length,
6234 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006235 TEST_ASSERT( output_length <=
6236 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6237 TEST_ASSERT( output_length <=
6238 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006239
6240exit:
6241 mbedtls_free( output );
6242 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006243 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006244}
6245/* END_CASE */
6246
6247/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006248void key_agreement_capacity( int alg_arg,
6249 int our_key_type_arg, data_t *our_key_data,
6250 data_t *peer_key_data,
6251 int expected_capacity_arg )
6252{
Ronald Cron5425a212020-08-04 14:58:35 +02006253 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006254 psa_algorithm_t alg = alg_arg;
6255 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006256 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006258 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006259 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006260
Gilles Peskine8817f612018-12-18 00:18:46 +01006261 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006262
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006263 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6264 psa_set_key_algorithm( &attributes, alg );
6265 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006266 PSA_ASSERT( psa_import_key( &attributes,
6267 our_key_data->x, our_key_data->len,
6268 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006269
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006270 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006271 PSA_ASSERT( psa_key_derivation_key_agreement(
6272 &operation,
6273 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6274 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006275 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6276 {
6277 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006278 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006279 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006280 NULL, 0 ) );
6281 }
Gilles Peskine59685592018-09-18 12:11:34 +02006282
Gilles Peskinebf491972018-10-25 22:36:12 +02006283 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006284 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006285 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006286 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006287
Gilles Peskinebf491972018-10-25 22:36:12 +02006288 /* Test the actual capacity by reading the output. */
6289 while( actual_capacity > sizeof( output ) )
6290 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006291 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006292 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006293 actual_capacity -= sizeof( output );
6294 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006295 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006296 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006297 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006298 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006299
Gilles Peskine59685592018-09-18 12:11:34 +02006300exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006301 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006302 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006303 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006304}
6305/* END_CASE */
6306
6307/* BEGIN_CASE */
6308void key_agreement_output( int alg_arg,
6309 int our_key_type_arg, data_t *our_key_data,
6310 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006311 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006312{
Ronald Cron5425a212020-08-04 14:58:35 +02006313 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006314 psa_algorithm_t alg = alg_arg;
6315 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006316 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006318 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006319
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006320 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6321 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006322
Gilles Peskine8817f612018-12-18 00:18:46 +01006323 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006324
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006325 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6326 psa_set_key_algorithm( &attributes, alg );
6327 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006328 PSA_ASSERT( psa_import_key( &attributes,
6329 our_key_data->x, our_key_data->len,
6330 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006331
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006332 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006333 PSA_ASSERT( psa_key_derivation_key_agreement(
6334 &operation,
6335 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6336 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006337 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6338 {
6339 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006340 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006341 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006342 NULL, 0 ) );
6343 }
Gilles Peskine59685592018-09-18 12:11:34 +02006344
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006345 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006346 actual_output,
6347 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006348 ASSERT_COMPARE( actual_output, expected_output1->len,
6349 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006350 if( expected_output2->len != 0 )
6351 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006352 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006353 actual_output,
6354 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006355 ASSERT_COMPARE( actual_output, expected_output2->len,
6356 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006357 }
Gilles Peskine59685592018-09-18 12:11:34 +02006358
6359exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006360 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006361 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006362 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006363 mbedtls_free( actual_output );
6364}
6365/* END_CASE */
6366
6367/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006368void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006369{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006370 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006371 unsigned char *output = NULL;
6372 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006373 size_t i;
6374 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006375
Simon Butcher49f8e312020-03-03 15:51:50 +00006376 TEST_ASSERT( bytes_arg >= 0 );
6377
Gilles Peskine91892022021-02-08 19:50:26 +01006378 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006379 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006380
Gilles Peskine8817f612018-12-18 00:18:46 +01006381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006382
Gilles Peskinea50d7392018-06-21 10:22:13 +02006383 /* Run several times, to ensure that every output byte will be
6384 * nonzero at least once with overwhelming probability
6385 * (2^(-8*number_of_runs)). */
6386 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006387 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006388 if( bytes != 0 )
6389 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006390 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006391
Gilles Peskinea50d7392018-06-21 10:22:13 +02006392 for( i = 0; i < bytes; i++ )
6393 {
6394 if( output[i] != 0 )
6395 ++changed[i];
6396 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006397 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006398
6399 /* Check that every byte was changed to nonzero at least once. This
6400 * validates that psa_generate_random is overwriting every byte of
6401 * the output buffer. */
6402 for( i = 0; i < bytes; i++ )
6403 {
6404 TEST_ASSERT( changed[i] != 0 );
6405 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006406
6407exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006408 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006409 mbedtls_free( output );
6410 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006411}
6412/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006413
6414/* BEGIN_CASE */
6415void generate_key( int type_arg,
6416 int bits_arg,
6417 int usage_arg,
6418 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006419 int expected_status_arg,
6420 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006421{
Ronald Cron5425a212020-08-04 14:58:35 +02006422 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006423 psa_key_type_t type = type_arg;
6424 psa_key_usage_t usage = usage_arg;
6425 size_t bits = bits_arg;
6426 psa_algorithm_t alg = alg_arg;
6427 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006428 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006429 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006430
Gilles Peskine8817f612018-12-18 00:18:46 +01006431 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006432
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006433 psa_set_key_usage_flags( &attributes, usage );
6434 psa_set_key_algorithm( &attributes, alg );
6435 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006436 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006437
6438 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006439 psa_status_t status = psa_generate_key( &attributes, &key );
6440
6441 if( is_large_key > 0 )
6442 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6443 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006444 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006445 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006446
6447 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006448 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006449 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6450 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006451
Gilles Peskine818ca122018-06-20 18:16:48 +02006452 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006453 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006454 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006455
6456exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006457 /*
6458 * Key attributes may have been returned by psa_get_key_attributes()
6459 * thus reset them as required.
6460 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006461 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006462
Ronald Cron5425a212020-08-04 14:58:35 +02006463 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006464 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006465}
6466/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006467
Ronald Cronee414c72021-03-18 18:50:08 +01006468/* 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 +02006469void generate_key_rsa( int bits_arg,
6470 data_t *e_arg,
6471 int expected_status_arg )
6472{
Ronald Cron5425a212020-08-04 14:58:35 +02006473 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006474 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006475 size_t bits = bits_arg;
6476 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6477 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6478 psa_status_t expected_status = expected_status_arg;
6479 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6480 uint8_t *exported = NULL;
6481 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006482 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006483 size_t exported_length = SIZE_MAX;
6484 uint8_t *e_read_buffer = NULL;
6485 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006486 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006487 size_t e_read_length = SIZE_MAX;
6488
6489 if( e_arg->len == 0 ||
6490 ( e_arg->len == 3 &&
6491 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6492 {
6493 is_default_public_exponent = 1;
6494 e_read_size = 0;
6495 }
6496 ASSERT_ALLOC( e_read_buffer, e_read_size );
6497 ASSERT_ALLOC( exported, exported_size );
6498
6499 PSA_ASSERT( psa_crypto_init( ) );
6500
6501 psa_set_key_usage_flags( &attributes, usage );
6502 psa_set_key_algorithm( &attributes, alg );
6503 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6504 e_arg->x, e_arg->len ) );
6505 psa_set_key_bits( &attributes, bits );
6506
6507 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006508 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006509 if( expected_status != PSA_SUCCESS )
6510 goto exit;
6511
6512 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006513 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006514 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6515 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6516 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6517 e_read_buffer, e_read_size,
6518 &e_read_length ) );
6519 if( is_default_public_exponent )
6520 TEST_EQUAL( e_read_length, 0 );
6521 else
6522 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6523
6524 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006525 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006526 goto exit;
6527
6528 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006529 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006530 exported, exported_size,
6531 &exported_length ) );
6532 {
6533 uint8_t *p = exported;
6534 uint8_t *end = exported + exported_length;
6535 size_t len;
6536 /* RSAPublicKey ::= SEQUENCE {
6537 * modulus INTEGER, -- n
6538 * publicExponent INTEGER } -- e
6539 */
6540 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006541 MBEDTLS_ASN1_SEQUENCE |
6542 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006543 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006544 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6545 MBEDTLS_ASN1_INTEGER ) );
6546 if( len >= 1 && p[0] == 0 )
6547 {
6548 ++p;
6549 --len;
6550 }
6551 if( e_arg->len == 0 )
6552 {
6553 TEST_EQUAL( len, 3 );
6554 TEST_EQUAL( p[0], 1 );
6555 TEST_EQUAL( p[1], 0 );
6556 TEST_EQUAL( p[2], 1 );
6557 }
6558 else
6559 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6560 }
6561
6562exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006563 /*
6564 * Key attributes may have been returned by psa_get_key_attributes() or
6565 * set by psa_set_key_domain_parameters() thus reset them as required.
6566 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006567 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006568
Ronald Cron5425a212020-08-04 14:58:35 +02006569 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006570 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006571 mbedtls_free( e_read_buffer );
6572 mbedtls_free( exported );
6573}
6574/* END_CASE */
6575
Darryl Greend49a4992018-06-18 17:27:26 +01006576/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006577void persistent_key_load_key_from_storage( data_t *data,
6578 int type_arg, int bits_arg,
6579 int usage_flags_arg, int alg_arg,
6580 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006581{
Ronald Cron71016a92020-08-28 19:01:50 +02006582 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006584 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6585 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006586 psa_key_type_t type = type_arg;
6587 size_t bits = bits_arg;
6588 psa_key_usage_t usage_flags = usage_flags_arg;
6589 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006590 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006591 unsigned char *first_export = NULL;
6592 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006593 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006594 size_t first_exported_length;
6595 size_t second_exported_length;
6596
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006597 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6598 {
6599 ASSERT_ALLOC( first_export, export_size );
6600 ASSERT_ALLOC( second_export, export_size );
6601 }
Darryl Greend49a4992018-06-18 17:27:26 +01006602
Gilles Peskine8817f612018-12-18 00:18:46 +01006603 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006604
Gilles Peskinec87af662019-05-15 16:12:22 +02006605 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006606 psa_set_key_usage_flags( &attributes, usage_flags );
6607 psa_set_key_algorithm( &attributes, alg );
6608 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006609 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006610
Darryl Green0c6575a2018-11-07 16:05:30 +00006611 switch( generation_method )
6612 {
6613 case IMPORT_KEY:
6614 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006615 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006616 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006617 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006618
Darryl Green0c6575a2018-11-07 16:05:30 +00006619 case GENERATE_KEY:
6620 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006621 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006622 break;
6623
6624 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006625#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006626 {
6627 /* Create base key */
6628 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6629 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6630 psa_set_key_usage_flags( &base_attributes,
6631 PSA_KEY_USAGE_DERIVE );
6632 psa_set_key_algorithm( &base_attributes, derive_alg );
6633 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006634 PSA_ASSERT( psa_import_key( &base_attributes,
6635 data->x, data->len,
6636 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006637 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006638 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006639 PSA_ASSERT( psa_key_derivation_input_key(
6640 &operation,
6641 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006642 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006643 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006644 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006645 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6646 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006647 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006648 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006649 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006650 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006651 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006652#else
6653 TEST_ASSUME( ! "KDF not supported in this configuration" );
6654#endif
6655 break;
6656
6657 default:
6658 TEST_ASSERT( ! "generation_method not implemented in test" );
6659 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006660 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006661 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006662
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006663 /* Export the key if permitted by the key policy. */
6664 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6665 {
Ronald Cron5425a212020-08-04 14:58:35 +02006666 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006667 first_export, export_size,
6668 &first_exported_length ) );
6669 if( generation_method == IMPORT_KEY )
6670 ASSERT_COMPARE( data->x, data->len,
6671 first_export, first_exported_length );
6672 }
Darryl Greend49a4992018-06-18 17:27:26 +01006673
6674 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006675 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006676 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006677 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006678
Darryl Greend49a4992018-06-18 17:27:26 +01006679 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006680 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006681 TEST_ASSERT( mbedtls_svc_key_id_equal(
6682 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006683 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6684 PSA_KEY_LIFETIME_PERSISTENT );
6685 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6686 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6687 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6688 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006689
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006690 /* Export the key again if permitted by the key policy. */
6691 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006692 {
Ronald Cron5425a212020-08-04 14:58:35 +02006693 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006694 second_export, export_size,
6695 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006696 ASSERT_COMPARE( first_export, first_exported_length,
6697 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006698 }
6699
6700 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006701 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006702 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006703
6704exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006705 /*
6706 * Key attributes may have been returned by psa_get_key_attributes()
6707 * thus reset them as required.
6708 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006709 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006710
Darryl Greend49a4992018-06-18 17:27:26 +01006711 mbedtls_free( first_export );
6712 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006713 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006714 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006715 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006716 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006717}
6718/* END_CASE */