blob: d05a2de44e67d40b53484075c587d7bb932b38a3 [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"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010024
Gilles Peskine4023c012021-05-27 13:21:20 +020025/* If this comes up, it's a bug in the test code or in the test data. */
26#define UNUSED 0xdeadbeef
27
Dave Rodgman647791d2021-06-23 12:49:59 +010028/* Assert that an operation is (not) active.
29 * This serves as a proxy for checking if the operation is aborted. */
30#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
31#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010032
33/** An invalid export length that will never be set by psa_export_key(). */
34static const size_t INVALID_EXPORT_LENGTH = ~0U;
35
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036/** Test if a buffer contains a constant byte value.
37 *
38 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 *
40 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042 * \param size Size of the buffer in bytes.
43 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 * \return 1 if the buffer is all-bits-zero.
45 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048{
49 size_t i;
50 for( i = 0; i < size; i++ )
51 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020052 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020055 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020056}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010057#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
59static int asn1_write_10x( unsigned char **p,
60 unsigned char *start,
61 size_t bits,
62 unsigned char x )
63{
64 int ret;
65 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020066 if( bits == 0 )
67 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
68 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020069 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030070 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020071 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
72 *p -= len;
73 ( *p )[len-1] = x;
74 if( bits % 8 == 0 )
75 ( *p )[1] |= 1;
76 else
77 ( *p )[0] |= 1 << ( bits % 8 );
78 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
80 MBEDTLS_ASN1_INTEGER ) );
81 return( len );
82}
83
84static int construct_fake_rsa_key( unsigned char *buffer,
85 size_t buffer_size,
86 unsigned char **p,
87 size_t bits,
88 int keypair )
89{
90 size_t half_bits = ( bits + 1 ) / 2;
91 int ret;
92 int len = 0;
93 /* Construct something that looks like a DER encoding of
94 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
95 * RSAPrivateKey ::= SEQUENCE {
96 * version Version,
97 * modulus INTEGER, -- n
98 * publicExponent INTEGER, -- e
99 * privateExponent INTEGER, -- d
100 * prime1 INTEGER, -- p
101 * prime2 INTEGER, -- q
102 * exponent1 INTEGER, -- d mod (p-1)
103 * exponent2 INTEGER, -- d mod (q-1)
104 * coefficient INTEGER, -- (inverse of q) mod p
105 * otherPrimeInfos OtherPrimeInfos OPTIONAL
106 * }
107 * Or, for a public key, the same structure with only
108 * version, modulus and publicExponent.
109 */
110 *p = buffer + buffer_size;
111 if( keypair )
112 {
113 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
116 asn1_write_10x( p, buffer, half_bits, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
118 asn1_write_10x( p, buffer, half_bits, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* q */
120 asn1_write_10x( p, buffer, half_bits, 1 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x( p, buffer, half_bits, 3 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* d */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 }
126 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
127 asn1_write_10x( p, buffer, 17, 1 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, /* n */
129 asn1_write_10x( p, buffer, bits, 1 ) );
130 if( keypair )
131 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
132 mbedtls_asn1_write_int( p, buffer, 0 ) );
133 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
134 {
135 const unsigned char tag =
136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
137 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
138 }
139 return( len );
140}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100141#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100143int exercise_mac_setup( psa_key_type_t key_type,
144 const unsigned char *key_bytes,
145 size_t key_length,
146 psa_algorithm_t alg,
147 psa_mac_operation_t *operation,
148 psa_status_t *status )
149{
Ronald Cron5425a212020-08-04 14:58:35 +0200150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200154 psa_set_key_algorithm( &attributes, alg );
155 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200156 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157
Ronald Cron5425a212020-08-04 14:58:35 +0200158 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100159 /* Whether setup succeeded or failed, abort must succeed. */
160 PSA_ASSERT( psa_mac_abort( operation ) );
161 /* If setup failed, reproduce the failure, so that the caller can
162 * test the resulting state of the operation object. */
163 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 {
Ronald Cron5425a212020-08-04 14:58:35 +0200165 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 }
167
Ronald Cron5425a212020-08-04 14:58:35 +0200168 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100169 return( 1 );
170
171exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200172 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173 return( 0 );
174}
175
176int exercise_cipher_setup( psa_key_type_t key_type,
177 const unsigned char *key_bytes,
178 size_t key_length,
179 psa_algorithm_t alg,
180 psa_cipher_operation_t *operation,
181 psa_status_t *status )
182{
Ronald Cron5425a212020-08-04 14:58:35 +0200183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
187 psa_set_key_algorithm( &attributes, alg );
188 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200189 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190
Ronald Cron5425a212020-08-04 14:58:35 +0200191 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* Whether setup succeeded or failed, abort must succeed. */
193 PSA_ASSERT( psa_cipher_abort( operation ) );
194 /* If setup failed, reproduce the failure, so that the caller can
195 * test the resulting state of the operation object. */
196 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 {
Ronald Cron5425a212020-08-04 14:58:35 +0200198 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100199 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 }
201
Ronald Cron5425a212020-08-04 14:58:35 +0200202 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203 return( 1 );
204
205exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200206 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207 return( 0 );
208}
209
Ronald Cron5425a212020-08-04 14:58:35 +0200210static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211{
212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 uint8_t buffer[1];
215 size_t length;
216 int ok = 0;
217
Ronald Cronecfb2372020-07-23 17:13:42 +0200218 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
220 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
221 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200224 TEST_EQUAL(
225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
226 TEST_EQUAL(
227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200228 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200229 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
233
Ronald Cron5425a212020-08-04 14:58:35 +0200234 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000235 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200236 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000238 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 ok = 1;
241
242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243 /*
244 * Key attributes may have been returned by psa_get_key_attributes()
245 * thus reset them as required.
246 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200247 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100248
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200249 return( ok );
250}
251
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252/* Assert that a key isn't reported as having a slot number. */
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
255 do \
256 { \
257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
258 TEST_EQUAL( psa_get_key_slot_number( \
259 attributes, \
260 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
261 PSA_ERROR_INVALID_ARGUMENT ); \
262 } \
263 while( 0 )
264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
265#define ASSERT_NO_SLOT_NUMBER( attributes ) \
266 ( (void) 0 )
267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100269/* An overapproximation of the amount of storage needed for a key of the
270 * given type and with the given content. The API doesn't make it easy
271 * to find a good value for the size. The current implementation doesn't
272 * care about the value anyway. */
273#define KEY_BITS_FROM_DATA( type, data ) \
274 ( data )->len
275
Darryl Green0c6575a2018-11-07 16:05:30 +0000276typedef enum {
277 IMPORT_KEY = 0,
278 GENERATE_KEY = 1,
279 DERIVE_KEY = 2
280} generate_method;
281
Paul Elliott33746aa2021-09-15 16:40:40 +0100282typedef enum
283{
284 DO_NOT_SET_LENGTHS = 0,
285 SET_LENGTHS_BEFORE_NONCE = 1,
286 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100287} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100288
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100289typedef enum
290{
291 USE_NULL_TAG = 0,
292 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100293} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100294
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100295/*!
296 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100297 * \param key_type_arg Type of key passed in
298 * \param key_data The encryption / decryption key data
299 * \param alg_arg The type of algorithm used
300 * \param nonce Nonce data
301 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100302 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 * feed additional data in to be encrypted /
304 * decrypted. If -1, no chunking.
305 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100306 * \param data_part_len_arg If not -1, the length of chunks to feed
307 * the data in to be encrypted / decrypted. If
308 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100309 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100310 * expected here, this controls whether or not
311 * to set lengths, and in what order with
312 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100315 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100316 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 */
319static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
320 int alg_arg,
321 data_t *nonce,
322 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100323 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100325 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100326 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100328 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100329 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100330{
331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
332 psa_key_type_t key_type = key_type_arg;
333 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100334 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100335 unsigned char *output_data = NULL;
336 unsigned char *part_data = NULL;
337 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100339 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 size_t output_size = 0;
341 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 size_t output_length = 0;
343 size_t key_bits = 0;
344 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100345 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100346 size_t part_length = 0;
347 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100349 size_t ad_part_len = 0;
350 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100351 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100356 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100357
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 PSA_ASSERT( psa_crypto_init( ) );
359
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 if( is_encrypt )
361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
362 else
363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
364
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 psa_set_key_algorithm( &attributes, alg );
366 psa_set_key_type( &attributes, key_type );
367
368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
369 &key ) );
370
371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
372 key_bits = psa_get_key_bits( &attributes );
373
374 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 if( is_encrypt )
377 {
378 /* Tag gets written at end of buffer. */
379 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
380 ( input_data->len +
381 tag_length ) );
382 data_true_size = input_data->len;
383 }
384 else
385 {
386 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
387 ( input_data->len -
388 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 /* Do not want to attempt to decrypt tag. */
391 data_true_size = input_data->len - tag_length;
392 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 ASSERT_ALLOC( output_data, output_size );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396 if( is_encrypt )
397 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100398 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
399 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 }
401 else
402 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100403 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
404 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100405 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100407 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100408
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100409 if( is_encrypt )
410 status = psa_aead_encrypt_setup( &operation, key, alg );
411 else
412 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
414 /* If the operation is not supported, just skip and not fail in case the
415 * encryption involves a common limitation of cryptography hardwares and
416 * an alternative implementation. */
417 if( status == PSA_ERROR_NOT_SUPPORTED )
418 {
419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
421 }
422
423 PSA_ASSERT( status );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 if( set_lengths_method == DO_NOT_SET_LENGTHS )
426 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
427 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100428 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100429 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
430 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
432 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100433 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100434 {
435 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
436
Paul Elliott33746aa2021-09-15 16:40:40 +0100437 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
438 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100441 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
443 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100444 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100446 for( part_offset = 0, part_count = 0;
447 part_offset < additional_data->len;
448 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100450 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 {
Paul Elliott329d5382021-07-22 17:10:45 +0100452 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100453 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100454 else if( additional_data->len - part_offset < ad_part_len )
455 {
456 part_length = additional_data->len - part_offset;
457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 else
459 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100460 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
462
463 PSA_ASSERT( psa_aead_update_ad( &operation,
464 additional_data->x + part_offset,
465 part_length ) );
466
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 }
468 }
469 else
470 {
471 /* Pass additional data in one go. */
472 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
473 additional_data->len ) );
474 }
475
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100476 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
478 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100479 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100481 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
483 ASSERT_ALLOC( part_data, part_data_size );
484
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100485 for( part_offset = 0, part_count = 0;
486 part_offset < data_true_size;
487 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100489 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 {
Paul Elliott329d5382021-07-22 17:10:45 +0100491 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100493 else if( ( data_true_size - part_offset ) < data_part_len )
494 {
495 part_length = ( data_true_size - part_offset );
496 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 else
498 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
502 PSA_ASSERT( psa_aead_update( &operation,
503 ( input_data->x + part_offset ),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length ) );
507
508 if( output_data && output_part_length )
509 {
510 memcpy( ( output_data + part_offset ), part_data,
511 output_part_length );
512 }
513
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 output_length += output_part_length;
515 }
516 }
517 else
518 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 output_size, &output_length ) );
523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( is_encrypt )
526 PSA_ASSERT( psa_aead_finish( &operation, final_data,
527 final_output_size,
528 &output_part_length,
529 tag_buffer, tag_length,
530 &tag_size ) );
531 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100532 {
Paul Elliott9961a662021-09-17 19:19:02 +0100533 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 final_output_size,
535 &output_part_length,
536 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100537 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100540 if( output_data && output_part_length )
541 memcpy( ( output_data + output_length ), final_data,
542 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
549 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551 TEST_EQUAL( tag_length, tag_size );
552
553 if( output_data && tag_length )
554 memcpy( ( output_data + output_length ), tag_buffer,
555 tag_length );
556
557 output_length += tag_length;
558
559 TEST_EQUAL( output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_ASSERT( output_length <=
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
570 TEST_ASSERT( output_length <=
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Gilles Peskinee59236f2018-01-27 23:32:46 +0100592/* END_HEADER */
593
594/* BEGIN_DEPENDENCIES
595 * depends_on:MBEDTLS_PSA_CRYPTO_C
596 * END_DEPENDENCIES
597 */
598
599/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200600void static_checks( )
601{
602 size_t max_truncated_mac_size =
603 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
604
605 /* Check that the length for a truncated MAC always fits in the algorithm
606 * encoding. The shifted mask is the maximum truncated value. The
607 * untruncated algorithm may be one byte larger. */
608 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
609}
610/* END_CASE */
611
612/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200613void import_with_policy( int type_arg,
614 int usage_arg, int alg_arg,
615 int expected_status_arg )
616{
617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
618 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200620 psa_key_type_t type = type_arg;
621 psa_key_usage_t usage = usage_arg;
622 psa_algorithm_t alg = alg_arg;
623 psa_status_t expected_status = expected_status_arg;
624 const uint8_t key_material[16] = {0};
625 psa_status_t status;
626
627 PSA_ASSERT( psa_crypto_init( ) );
628
629 psa_set_key_type( &attributes, type );
630 psa_set_key_usage_flags( &attributes, usage );
631 psa_set_key_algorithm( &attributes, alg );
632
633 status = psa_import_key( &attributes,
634 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200635 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636 TEST_EQUAL( status, expected_status );
637 if( status != PSA_SUCCESS )
638 goto exit;
639
Ronald Cron5425a212020-08-04 14:58:35 +0200640 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200642 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200643 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200644 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200645 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200646
Ronald Cron5425a212020-08-04 14:58:35 +0200647 PSA_ASSERT( psa_destroy_key( key ) );
648 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200649
650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100651 /*
652 * Key attributes may have been returned by psa_get_key_attributes()
653 * thus reset them as required.
654 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200655 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100656
657 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200658 PSA_DONE( );
659}
660/* END_CASE */
661
662/* BEGIN_CASE */
663void import_with_data( data_t *data, int type_arg,
664 int attr_bits_arg,
665 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200666{
667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
668 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200670 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200671 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200672 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
Gilles Peskine4747d192019-04-17 15:05:45 +0200677 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200678 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200679
Ronald Cron5425a212020-08-04 14:58:35 +0200680 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100681 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200682 if( status != PSA_SUCCESS )
683 goto exit;
684
Ronald Cron5425a212020-08-04 14:58:35 +0200685 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200686 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200687 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200688 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200689 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200690
Ronald Cron5425a212020-08-04 14:58:35 +0200691 PSA_ASSERT( psa_destroy_key( key ) );
692 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693
694exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100695 /*
696 * Key attributes may have been returned by psa_get_key_attributes()
697 * thus reset them as required.
698 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200699 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100700
701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200702 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703}
704/* END_CASE */
705
706/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200707void import_large_key( int type_arg, int byte_size_arg,
708 int expected_status_arg )
709{
710 psa_key_type_t type = type_arg;
711 size_t byte_size = byte_size_arg;
712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
713 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200715 psa_status_t status;
716 uint8_t *buffer = NULL;
717 size_t buffer_size = byte_size + 1;
718 size_t n;
719
Steven Cooreman69967ce2021-01-18 18:01:08 +0100720 /* Skip the test case if the target running the test cannot
721 * accomodate large keys due to heap size constraints */
722 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200723 memset( buffer, 'K', byte_size );
724
725 PSA_ASSERT( psa_crypto_init( ) );
726
727 /* Try importing the key */
728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
729 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200730 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100731 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200732 TEST_EQUAL( status, expected_status );
733
734 if( status == PSA_SUCCESS )
735 {
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200737 TEST_EQUAL( psa_get_key_type( &attributes ), type );
738 TEST_EQUAL( psa_get_key_bits( &attributes ),
739 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200740 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200742 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200743 for( n = 0; n < byte_size; n++ )
744 TEST_EQUAL( buffer[n], 'K' );
745 for( n = byte_size; n < buffer_size; n++ )
746 TEST_EQUAL( buffer[n], 0 );
747 }
748
749exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100750 /*
751 * Key attributes may have been returned by psa_get_key_attributes()
752 * thus reset them as required.
753 */
754 psa_reset_key_attributes( &attributes );
755
Ronald Cron5425a212020-08-04 14:58:35 +0200756 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200757 PSA_DONE( );
758 mbedtls_free( buffer );
759}
760/* END_CASE */
761
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100762/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
764{
Ronald Cron5425a212020-08-04 14:58:35 +0200765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200766 size_t bits = bits_arg;
767 psa_status_t expected_status = expected_status_arg;
768 psa_status_t status;
769 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200770 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200771 size_t buffer_size = /* Slight overapproximations */
772 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200773 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200774 unsigned char *p;
775 int ret;
776 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778
Gilles Peskine8817f612018-12-18 00:18:46 +0100779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200780 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200781
782 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
783 bits, keypair ) ) >= 0 );
784 length = ret;
785
786 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200787 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200788 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100789 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200790
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200791 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200792 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200793
794exit:
795 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200796 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200797}
798/* END_CASE */
799
800/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300801void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300802 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200803 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530804 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 int expected_bits,
806 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200807 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 int canonical_input )
809{
Ronald Cron5425a212020-08-04 14:58:35 +0200810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200812 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200813 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530815 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 unsigned char *exported = NULL;
817 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100819 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200822 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823
Moran Pekercb088e72018-07-17 17:36:59 +0300824 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200825 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200827 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829
Archana4d7ae1d2021-07-07 02:50:22 +0530830 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200831 psa_set_key_usage_flags( &attributes, usage_arg );
832 psa_set_key_algorithm( &attributes, alg );
833 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700834
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200836 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837
838 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200839 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200840 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
841 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200842 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843
844 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200845 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100846 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100847
848 /* The exported length must be set by psa_export_key() to a value between 0
849 * and export_size. On errors, the exported length must be 0. */
850 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
851 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
852 TEST_ASSERT( exported_length <= export_size );
853
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200854 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200855 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100856 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200857 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200860 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861
Gilles Peskineea38a922021-02-13 00:05:16 +0100862 /* Run sanity checks on the exported key. For non-canonical inputs,
863 * this validates the canonical representations. For canonical inputs,
864 * this doesn't directly validate the implementation, but it still helps
865 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530866 if( !psa_key_lifetime_is_external( lifetime ) )
867 {
868 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
869 goto exit;
870 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200873 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 else
875 {
Ronald Cron5425a212020-08-04 14:58:35 +0200876 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200877 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200878 &key2 ) );
879 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100880 reexported,
881 export_size,
882 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200883 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +0530884 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100887 TEST_ASSERT( exported_length <=
Archana4d7ae1d2021-07-07 02:50:22 +0530888 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +0530889 psa_get_key_bits( &got_attributes ) ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100890 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891
892destroy:
893 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200894 PSA_ASSERT( psa_destroy_key( key ) );
895 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896
897exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100898 /*
899 * Key attributes may have been returned by psa_get_key_attributes()
900 * thus reset them as required.
901 */
902 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +0530903 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300904 mbedtls_free( exported );
905 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200906 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907}
908/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100909
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300911void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200912 int type_arg,
913 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530914 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 int export_size_delta,
916 int expected_export_status_arg,
917 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918{
Ronald Cron5425a212020-08-04 14:58:35 +0200919 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300920 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200921 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200922 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300923 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530924 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300925 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100926 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100927 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929
Gilles Peskine8817f612018-12-18 00:18:46 +0100930 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300931
Archana4d7ae1d2021-07-07 02:50:22 +0530932 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
934 psa_set_key_algorithm( &attributes, alg );
935 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300936
937 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200938 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300939
Gilles Peskine49c25912018-10-29 15:15:31 +0100940 /* Export the public key */
941 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200942 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200943 exported, export_size,
944 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100946 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100947 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200948 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100949 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200950 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200951 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100952 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100953 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100954 TEST_ASSERT( expected_public_key->len <=
955 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
956 TEST_ASSERT( expected_public_key->len <=
957 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100958 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
959 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100960 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300961exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100962 /*
963 * Key attributes may have been returned by psa_get_key_attributes()
964 * thus reset them as required.
965 */
966 psa_reset_key_attributes( &attributes );
967
itayzafrir3e02b3b2018-06-12 17:06:52 +0300968 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200969 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200970 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300971}
972/* END_CASE */
973
Gilles Peskine20035e32018-02-03 22:44:14 +0100974/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975void import_and_exercise_key( data_t *data,
976 int type_arg,
977 int bits_arg,
978 int alg_arg )
979{
Ronald Cron5425a212020-08-04 14:58:35 +0200980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200981 psa_key_type_t type = type_arg;
982 size_t bits = bits_arg;
983 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100984 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200986 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200987
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
Gilles Peskine4747d192019-04-17 15:05:45 +0200990 psa_set_key_usage_flags( &attributes, usage );
991 psa_set_key_algorithm( &attributes, alg );
992 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993
994 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200995 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200996
997 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200998 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200999 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1000 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001001
1002 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001003 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001004 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001005
Ronald Cron5425a212020-08-04 14:58:35 +02001006 PSA_ASSERT( psa_destroy_key( key ) );
1007 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001008
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001009exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001010 /*
1011 * Key attributes may have been returned by psa_get_key_attributes()
1012 * thus reset them as required.
1013 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001014 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001015
1016 psa_reset_key_attributes( &attributes );
1017 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001018 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001019}
1020/* END_CASE */
1021
1022/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001023void effective_key_attributes( int type_arg, int expected_type_arg,
1024 int bits_arg, int expected_bits_arg,
1025 int usage_arg, int expected_usage_arg,
1026 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001027{
Ronald Cron5425a212020-08-04 14:58:35 +02001028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001029 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001030 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001031 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001032 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001033 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001034 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001035 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001036 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
Gilles Peskine8817f612018-12-18 00:18:46 +01001039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001040
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001041 psa_set_key_usage_flags( &attributes, usage );
1042 psa_set_key_algorithm( &attributes, alg );
1043 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001044 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001047 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048
Ronald Cron5425a212020-08-04 14:58:35 +02001049 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001050 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1051 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1052 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1053 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001054
1055exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001056 /*
1057 * Key attributes may have been returned by psa_get_key_attributes()
1058 * thus reset them as required.
1059 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001060 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001061
1062 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001063 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001064}
1065/* END_CASE */
1066
1067/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001068void check_key_policy( int type_arg, int bits_arg,
1069 int usage_arg, int alg_arg )
1070{
1071 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001072 usage_arg,
1073 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001074 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001075 goto exit;
1076}
1077/* END_CASE */
1078
1079/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001080void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001081{
1082 /* Test each valid way of initializing the object, except for `= {0}`, as
1083 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1084 * though it's OK by the C standard. We could test for this, but we'd need
1085 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001086 psa_key_attributes_t func = psa_key_attributes_init( );
1087 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1088 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001089
1090 memset( &zero, 0, sizeof( zero ) );
1091
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001092 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1093 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1094 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001096 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1097 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1098 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1099
1100 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1101 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1102 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1103
1104 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1105 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1106 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1107
1108 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1109 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1110 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001111}
1112/* END_CASE */
1113
1114/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001115void mac_key_policy( int policy_usage_arg,
1116 int policy_alg_arg,
1117 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001119 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001120 int expected_status_sign_arg,
1121 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001126 psa_key_type_t key_type = key_type_arg;
1127 psa_algorithm_t policy_alg = policy_alg_arg;
1128 psa_algorithm_t exercise_alg = exercise_alg_arg;
1129 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001130 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001131 psa_status_t expected_status_sign = expected_status_sign_arg;
1132 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001134
Gilles Peskine8817f612018-12-18 00:18:46 +01001135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001137 psa_set_key_usage_flags( &attributes, policy_usage );
1138 psa_set_key_algorithm( &attributes, policy_alg );
1139 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001140
Gilles Peskine049c7532019-05-15 20:22:09 +02001141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001142 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001143
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001144 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1145 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001146
Ronald Cron5425a212020-08-04 14:58:35 +02001147 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001148 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001149
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001150 /* Calculate the MAC, one-shot case. */
1151 uint8_t input[128] = {0};
1152 size_t mac_len;
1153 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1154 input, 128,
1155 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1156 expected_status_sign );
1157
Neil Armstrong3af9b972022-02-07 12:20:21 +01001158 /* Calculate the MAC, multi-part case. */
1159 PSA_ASSERT( psa_mac_abort( &operation ) );
1160 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1161 if( status == PSA_SUCCESS )
1162 {
1163 status = psa_mac_update( &operation, input, 128 );
1164 if( status == PSA_SUCCESS )
1165 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1166 &mac_len ),
1167 expected_status_sign );
1168 else
1169 TEST_EQUAL( status, expected_status_sign );
1170 }
1171 else
1172 {
1173 TEST_EQUAL( status, expected_status_sign );
1174 }
1175 PSA_ASSERT( psa_mac_abort( &operation ) );
1176
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001177 /* Verify correct MAC, one-shot case. */
1178 status = psa_mac_verify( key, exercise_alg, input, 128,
1179 mac, mac_len );
1180
1181 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1182 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001183 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001184 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001185
Neil Armstrong3af9b972022-02-07 12:20:21 +01001186 /* Verify correct MAC, multi-part case. */
1187 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1188 if( status == PSA_SUCCESS )
1189 {
1190 status = psa_mac_update( &operation, input, 128 );
1191 if( status == PSA_SUCCESS )
1192 {
1193 status = psa_mac_verify_finish( &operation, mac, mac_len );
1194 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1195 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1196 else
1197 TEST_EQUAL( status, expected_status_verify );
1198 }
1199 else
1200 {
1201 TEST_EQUAL( status, expected_status_verify );
1202 }
1203 }
1204 else
1205 {
1206 TEST_EQUAL( status, expected_status_verify );
1207 }
1208
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001210
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001212 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001213 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214
1215exit:
1216 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001218 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219}
1220/* END_CASE */
1221
1222/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001223void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001224 int policy_alg,
1225 int key_type,
1226 data_t *key_data,
1227 int exercise_alg )
1228{
Ronald Cron5425a212020-08-04 14:58:35 +02001229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001231 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001232 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001233 size_t output_buffer_size = 0;
1234 size_t input_buffer_size = 0;
1235 size_t output_length = 0;
1236 uint8_t *output = NULL;
1237 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001238 psa_status_t status;
1239
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001240 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1241 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1242 input_buffer_size );
1243
1244 ASSERT_ALLOC( input, input_buffer_size );
1245 ASSERT_ALLOC( output, output_buffer_size );
1246
Gilles Peskine8817f612018-12-18 00:18:46 +01001247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001248
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001249 psa_set_key_usage_flags( &attributes, policy_usage );
1250 psa_set_key_algorithm( &attributes, policy_alg );
1251 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252
Gilles Peskine049c7532019-05-15 20:22:09 +02001253 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001254 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001255
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001256 /* Check if no key usage flag implication is done */
1257 TEST_EQUAL( policy_usage,
1258 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001259
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001260 /* Encrypt check, one-shot */
1261 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1262 output, output_buffer_size,
1263 &output_length);
1264 if( policy_alg == exercise_alg &&
1265 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1266 PSA_ASSERT( status );
1267 else
1268 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1269
1270 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001271 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001272 if( policy_alg == exercise_alg &&
1273 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001274 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001275 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001276 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277 psa_cipher_abort( &operation );
1278
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001279 /* Decrypt check, one-shot */
1280 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1281 input, input_buffer_size,
1282 &output_length);
1283 if( policy_alg == exercise_alg &&
1284 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1285 PSA_ASSERT( status );
1286 else
1287 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1288
1289 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001290 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001291 if( policy_alg == exercise_alg &&
1292 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001293 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001294 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001295 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296
1297exit:
1298 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001299 mbedtls_free( input );
1300 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001301 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001302 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001303}
1304/* END_CASE */
1305
1306/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001307void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001308 int policy_alg,
1309 int key_type,
1310 data_t *key_data,
1311 int nonce_length_arg,
1312 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001313 int exercise_alg,
1314 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001315{
Ronald Cron5425a212020-08-04 14:58:35 +02001316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001318 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001319 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001321 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001322 unsigned char nonce[16] = {0};
1323 size_t nonce_length = nonce_length_arg;
1324 unsigned char tag[16];
1325 size_t tag_length = tag_length_arg;
1326 size_t output_length;
1327
1328 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1329 TEST_ASSERT( tag_length <= sizeof( tag ) );
1330
Gilles Peskine8817f612018-12-18 00:18:46 +01001331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001333 psa_set_key_usage_flags( &attributes, policy_usage );
1334 psa_set_key_algorithm( &attributes, policy_alg );
1335 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001336
Gilles Peskine049c7532019-05-15 20:22:09 +02001337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001338 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001340 /* Check if no key usage implication is done */
1341 TEST_EQUAL( policy_usage,
1342 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001343
Neil Armstrong752d8112022-02-07 14:51:11 +01001344 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02001345 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346 nonce, nonce_length,
1347 NULL, 0,
1348 NULL, 0,
1349 tag, tag_length,
1350 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001351 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1352 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001353 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001354 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001355
Neil Armstrong752d8112022-02-07 14:51:11 +01001356 /* Encrypt check, multi-part */
1357 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
1358 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1359 TEST_EQUAL( status, expected_status );
1360 else
1361 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1362
1363 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001365 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001366 nonce, nonce_length,
1367 NULL, 0,
1368 tag, tag_length,
1369 NULL, 0,
1370 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001371 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1372 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1373 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001374 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001375 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001376 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001377
Neil Armstrong752d8112022-02-07 14:51:11 +01001378 /* Decrypt check, multi-part */
1379 PSA_ASSERT( psa_aead_abort( &operation ) );
1380 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
1381 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1382 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1383 else
1384 TEST_EQUAL( status, expected_status );
1385
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001386exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01001387 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001388 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001389 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001390}
1391/* END_CASE */
1392
1393/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001394void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001395 int policy_alg,
1396 int key_type,
1397 data_t *key_data,
1398 int exercise_alg )
1399{
Ronald Cron5425a212020-08-04 14:58:35 +02001400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001402 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001403 psa_status_t status;
1404 size_t key_bits;
1405 size_t buffer_length;
1406 unsigned char *buffer = NULL;
1407 size_t output_length;
1408
Gilles Peskine8817f612018-12-18 00:18:46 +01001409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001410
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001411 psa_set_key_usage_flags( &attributes, policy_usage );
1412 psa_set_key_algorithm( &attributes, policy_alg );
1413 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001414
Gilles Peskine049c7532019-05-15 20:22:09 +02001415 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001416 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001417
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001418 /* Check if no key usage implication is done */
1419 TEST_EQUAL( policy_usage,
1420 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001421
Ronald Cron5425a212020-08-04 14:58:35 +02001422 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001423 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001424 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1425 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001426 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001427
Ronald Cron5425a212020-08-04 14:58:35 +02001428 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001429 NULL, 0,
1430 NULL, 0,
1431 buffer, buffer_length,
1432 &output_length );
1433 if( policy_alg == exercise_alg &&
1434 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001435 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001436 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001437 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001438
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001439 if( buffer_length != 0 )
1440 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001441 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001442 buffer, buffer_length,
1443 NULL, 0,
1444 buffer, buffer_length,
1445 &output_length );
1446 if( policy_alg == exercise_alg &&
1447 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001448 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001449 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001450 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001451
1452exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001453 /*
1454 * Key attributes may have been returned by psa_get_key_attributes()
1455 * thus reset them as required.
1456 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001457 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001458
1459 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001460 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001461 mbedtls_free( buffer );
1462}
1463/* END_CASE */
1464
1465/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001466void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 int policy_alg,
1468 int key_type,
1469 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001470 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001471 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001472 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001473{
Ronald Cron5425a212020-08-04 14:58:35 +02001474 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001475 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001476 psa_key_usage_t policy_usage = policy_usage_arg;
1477 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001478 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001479 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1480 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1481 * compatible with the policy and `payload_length_arg` is supposed to be
1482 * a valid input length to sign. If `payload_length_arg <= 0`,
1483 * `exercise_alg` is supposed to be forbidden by the policy. */
1484 int compatible_alg = payload_length_arg > 0;
1485 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001486 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001487 size_t signature_length;
1488
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001489 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001490 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001491 TEST_EQUAL( expected_usage,
1492 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001493
Gilles Peskine8817f612018-12-18 00:18:46 +01001494 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001495
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001496 psa_set_key_usage_flags( &attributes, policy_usage );
1497 psa_set_key_algorithm( &attributes, policy_alg );
1498 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001499
Gilles Peskine049c7532019-05-15 20:22:09 +02001500 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001501 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001502
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001503 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1504
Ronald Cron5425a212020-08-04 14:58:35 +02001505 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001506 payload, payload_length,
1507 signature, sizeof( signature ),
1508 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001509 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001510 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001511 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001512 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001513
1514 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001515 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001516 payload, payload_length,
1517 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001518 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001519 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001520 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001521 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001522
Gilles Peskinef7b41372021-09-22 16:15:05 +02001523 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001524 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001525 {
1526 status = psa_sign_message( key, exercise_alg,
1527 payload, payload_length,
1528 signature, sizeof( signature ),
1529 &signature_length );
1530 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1531 PSA_ASSERT( status );
1532 else
1533 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1534
1535 memset( signature, 0, sizeof( signature ) );
1536 status = psa_verify_message( key, exercise_alg,
1537 payload, payload_length,
1538 signature, sizeof( signature ) );
1539 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1540 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1541 else
1542 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1543 }
1544
Gilles Peskined5b33222018-06-18 22:20:03 +02001545exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001546 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001547 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001548}
1549/* END_CASE */
1550
Janos Follathba3fab92019-06-11 14:50:16 +01001551/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001552void derive_key_policy( int policy_usage,
1553 int policy_alg,
1554 int key_type,
1555 data_t *key_data,
1556 int exercise_alg )
1557{
Ronald Cron5425a212020-08-04 14:58:35 +02001558 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001559 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001560 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001561 psa_status_t status;
1562
Gilles Peskine8817f612018-12-18 00:18:46 +01001563 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001564
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001565 psa_set_key_usage_flags( &attributes, policy_usage );
1566 psa_set_key_algorithm( &attributes, policy_alg );
1567 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001568
Gilles Peskine049c7532019-05-15 20:22:09 +02001569 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001570 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001571
Janos Follathba3fab92019-06-11 14:50:16 +01001572 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1573
1574 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1575 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001576 {
Janos Follathba3fab92019-06-11 14:50:16 +01001577 PSA_ASSERT( psa_key_derivation_input_bytes(
1578 &operation,
1579 PSA_KEY_DERIVATION_INPUT_SEED,
1580 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001581 }
Janos Follathba3fab92019-06-11 14:50:16 +01001582
1583 status = psa_key_derivation_input_key( &operation,
1584 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001585 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001586
Gilles Peskineea0fb492018-07-12 17:17:20 +02001587 if( policy_alg == exercise_alg &&
1588 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001589 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001590 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001591 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001592
1593exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001594 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001595 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001596 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001597}
1598/* END_CASE */
1599
1600/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001601void agreement_key_policy( int policy_usage,
1602 int policy_alg,
1603 int key_type_arg,
1604 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001605 int exercise_alg,
1606 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001607{
Ronald Cron5425a212020-08-04 14:58:35 +02001608 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001610 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001611 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001612 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001613 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001614
Gilles Peskine8817f612018-12-18 00:18:46 +01001615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001616
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001617 psa_set_key_usage_flags( &attributes, policy_usage );
1618 psa_set_key_algorithm( &attributes, policy_alg );
1619 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001620
Gilles Peskine049c7532019-05-15 20:22:09 +02001621 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001622 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001623
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001624 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001625 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001626
Steven Cooremance48e852020-10-05 16:02:45 +02001627 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001628
1629exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001630 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001631 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001632 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001633}
1634/* END_CASE */
1635
1636/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001637void key_policy_alg2( int key_type_arg, data_t *key_data,
1638 int usage_arg, int alg_arg, int alg2_arg )
1639{
Ronald Cron5425a212020-08-04 14:58:35 +02001640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001641 psa_key_type_t key_type = key_type_arg;
1642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1643 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1644 psa_key_usage_t usage = usage_arg;
1645 psa_algorithm_t alg = alg_arg;
1646 psa_algorithm_t alg2 = alg2_arg;
1647
1648 PSA_ASSERT( psa_crypto_init( ) );
1649
1650 psa_set_key_usage_flags( &attributes, usage );
1651 psa_set_key_algorithm( &attributes, alg );
1652 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1653 psa_set_key_type( &attributes, key_type );
1654 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001655 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001656
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001657 /* Update the usage flags to obtain implicit usage flags */
1658 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001659 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001660 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1661 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1662 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1663
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001664 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001665 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001666 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001667 goto exit;
1668
1669exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001670 /*
1671 * Key attributes may have been returned by psa_get_key_attributes()
1672 * thus reset them as required.
1673 */
1674 psa_reset_key_attributes( &got_attributes );
1675
Ronald Cron5425a212020-08-04 14:58:35 +02001676 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001677 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001678}
1679/* END_CASE */
1680
1681/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001682void raw_agreement_key_policy( int policy_usage,
1683 int policy_alg,
1684 int key_type_arg,
1685 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001686 int exercise_alg,
1687 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001688{
Ronald Cron5425a212020-08-04 14:58:35 +02001689 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001690 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001691 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001692 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001693 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001694 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001695
1696 PSA_ASSERT( psa_crypto_init( ) );
1697
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001698 psa_set_key_usage_flags( &attributes, policy_usage );
1699 psa_set_key_algorithm( &attributes, policy_alg );
1700 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001701
Gilles Peskine049c7532019-05-15 20:22:09 +02001702 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001703 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001704
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001705 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001706
Steven Cooremance48e852020-10-05 16:02:45 +02001707 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001708
1709exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001710 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001711 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001712 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001713}
1714/* END_CASE */
1715
1716/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001717void copy_success( int source_usage_arg,
1718 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301719 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001720 int type_arg, data_t *material,
1721 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001722 int target_usage_arg,
1723 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301724 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001725 int expected_usage_arg,
1726 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001727{
Gilles Peskineca25db92019-04-19 11:43:08 +02001728 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1729 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001730 psa_key_usage_t expected_usage = expected_usage_arg;
1731 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001732 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301733 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1734 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001735 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1736 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001737 uint8_t *export_buffer = NULL;
1738
Gilles Peskine57ab7212019-01-28 13:03:09 +01001739 PSA_ASSERT( psa_crypto_init( ) );
1740
Gilles Peskineca25db92019-04-19 11:43:08 +02001741 /* Prepare the source key. */
1742 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1743 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001744 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001745 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301746 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001747 PSA_ASSERT( psa_import_key( &source_attributes,
1748 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001749 &source_key ) );
1750 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001751
Gilles Peskineca25db92019-04-19 11:43:08 +02001752 /* Prepare the target attributes. */
1753 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001754 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001755 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001756 }
Archana8a180362021-07-05 02:18:48 +05301757 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001758
Gilles Peskineca25db92019-04-19 11:43:08 +02001759 if( target_usage_arg != -1 )
1760 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1761 if( target_alg_arg != -1 )
1762 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001763 if( target_alg2_arg != -1 )
1764 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001765
Archana8a180362021-07-05 02:18:48 +05301766
Gilles Peskine57ab7212019-01-28 13:03:09 +01001767 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001768 PSA_ASSERT( psa_copy_key( source_key,
1769 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001770
1771 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001772 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001773
1774 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001775 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001776 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1777 psa_get_key_type( &target_attributes ) );
1778 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1779 psa_get_key_bits( &target_attributes ) );
1780 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1781 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001782 TEST_EQUAL( expected_alg2,
1783 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001784 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1785 {
1786 size_t length;
1787 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001788 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001789 material->len, &length ) );
1790 ASSERT_COMPARE( material->x, material->len,
1791 export_buffer, length );
1792 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001793
Archana8a180362021-07-05 02:18:48 +05301794 if( !psa_key_lifetime_is_external( target_lifetime ) )
1795 {
1796 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1797 goto exit;
1798 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1799 goto exit;
1800 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001801
Ronald Cron5425a212020-08-04 14:58:35 +02001802 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001803
1804exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001805 /*
1806 * Source and target key attributes may have been returned by
1807 * psa_get_key_attributes() thus reset them as required.
1808 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001809 psa_reset_key_attributes( &source_attributes );
1810 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001811
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001812 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001813 mbedtls_free( export_buffer );
1814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001818void copy_fail( int source_usage_arg,
1819 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301820 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001821 int type_arg, data_t *material,
1822 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001823 int target_usage_arg,
1824 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001825 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001826 int expected_status_arg )
1827{
1828 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1829 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001830 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1831 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001832 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001833
1834 PSA_ASSERT( psa_crypto_init( ) );
1835
1836 /* Prepare the source key. */
1837 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1838 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001839 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001840 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301841 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001842 PSA_ASSERT( psa_import_key( &source_attributes,
1843 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001844 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001845
1846 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001847 psa_set_key_id( &target_attributes, key_id );
1848 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001849 psa_set_key_type( &target_attributes, target_type_arg );
1850 psa_set_key_bits( &target_attributes, target_bits_arg );
1851 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1852 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001853 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001854
1855 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001856 TEST_EQUAL( psa_copy_key( source_key,
1857 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001858 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001859
Ronald Cron5425a212020-08-04 14:58:35 +02001860 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001861
Gilles Peskine4a644642019-05-03 17:14:08 +02001862exit:
1863 psa_reset_key_attributes( &source_attributes );
1864 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001865 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001866}
1867/* END_CASE */
1868
1869/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001870void hash_operation_init( )
1871{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001872 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001873 /* Test each valid way of initializing the object, except for `= {0}`, as
1874 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1875 * though it's OK by the C standard. We could test for this, but we'd need
1876 * to supress the Clang warning for the test. */
1877 psa_hash_operation_t func = psa_hash_operation_init( );
1878 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1879 psa_hash_operation_t zero;
1880
1881 memset( &zero, 0, sizeof( zero ) );
1882
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001883 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001884 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1885 PSA_ERROR_BAD_STATE );
1886 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1887 PSA_ERROR_BAD_STATE );
1888 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1889 PSA_ERROR_BAD_STATE );
1890
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001891 /* A default hash operation should be abortable without error. */
1892 PSA_ASSERT( psa_hash_abort( &func ) );
1893 PSA_ASSERT( psa_hash_abort( &init ) );
1894 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001895}
1896/* END_CASE */
1897
1898/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001899void hash_setup( int alg_arg,
1900 int expected_status_arg )
1901{
1902 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01001903 uint8_t *output = NULL;
1904 size_t output_size = 0;
1905 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001906 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001907 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001908 psa_status_t status;
1909
Gilles Peskine8817f612018-12-18 00:18:46 +01001910 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001911
Neil Armstrongedb20862022-02-07 15:47:44 +01001912 /* Hash Setup, one-shot */
1913 output_size = PSA_HASH_LENGTH(alg);
1914 ASSERT_ALLOC( output, output_size );
1915
1916 status = psa_hash_compute( alg, NULL, 0,
1917 output, output_size, &output_length );
1918 TEST_EQUAL( status, expected_status );
1919
1920 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001921 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001922 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001923
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001924 /* Whether setup succeeded or failed, abort must succeed. */
1925 PSA_ASSERT( psa_hash_abort( &operation ) );
1926
1927 /* If setup failed, reproduce the failure, so as to
1928 * test the resulting state of the operation object. */
1929 if( status != PSA_SUCCESS )
1930 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1931
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001932 /* Now the operation object should be reusable. */
1933#if defined(KNOWN_SUPPORTED_HASH_ALG)
1934 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1935 PSA_ASSERT( psa_hash_abort( &operation ) );
1936#endif
1937
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001938exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01001939 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001940 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001941}
1942/* END_CASE */
1943
1944/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001945void hash_compute_fail( int alg_arg, data_t *input,
1946 int output_size_arg, int expected_status_arg )
1947{
1948 psa_algorithm_t alg = alg_arg;
1949 uint8_t *output = NULL;
1950 size_t output_size = output_size_arg;
1951 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001952 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01001953 psa_status_t expected_status = expected_status_arg;
1954 psa_status_t status;
1955
1956 ASSERT_ALLOC( output, output_size );
1957
1958 PSA_ASSERT( psa_crypto_init( ) );
1959
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001960 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001961 status = psa_hash_compute( alg, input->x, input->len,
1962 output, output_size, &output_length );
1963 TEST_EQUAL( status, expected_status );
1964 TEST_ASSERT( output_length <= output_size );
1965
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001966 /* Hash Compute, multi-part */
1967 status = psa_hash_setup( &operation, alg );
1968 if( status == PSA_SUCCESS )
1969 {
1970 status = psa_hash_update( &operation, input->x, input->len );
1971 if( status == PSA_SUCCESS )
1972 {
1973 status = psa_hash_finish( &operation, output, output_size,
1974 &output_length );
1975 if( status == PSA_SUCCESS )
1976 TEST_ASSERT( output_length <= output_size );
1977 else
1978 TEST_EQUAL( status, expected_status );
1979 }
1980 else
1981 {
1982 TEST_EQUAL( status, expected_status );
1983 }
1984 }
1985 else
1986 {
1987 TEST_EQUAL( status, expected_status );
1988 }
1989
Gilles Peskine0a749c82019-11-28 19:33:58 +01001990exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001991 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001992 mbedtls_free( output );
1993 PSA_DONE( );
1994}
1995/* END_CASE */
1996
1997/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001998void hash_compare_fail( int alg_arg, data_t *input,
1999 data_t *reference_hash,
2000 int expected_status_arg )
2001{
2002 psa_algorithm_t alg = alg_arg;
2003 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002004 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002005 psa_status_t status;
2006
2007 PSA_ASSERT( psa_crypto_init( ) );
2008
Neil Armstrong55a1be12022-02-07 11:23:20 +01002009 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002010 status = psa_hash_compare( alg, input->x, input->len,
2011 reference_hash->x, reference_hash->len );
2012 TEST_EQUAL( status, expected_status );
2013
Neil Armstrong55a1be12022-02-07 11:23:20 +01002014 /* Hash Compare, multi-part */
2015 status = psa_hash_setup( &operation, alg );
2016 if( status == PSA_SUCCESS )
2017 {
2018 status = psa_hash_update( &operation, input->x, input->len );
2019 if( status == PSA_SUCCESS )
2020 {
2021 status = psa_hash_verify( &operation, reference_hash->x,
2022 reference_hash->len );
2023 TEST_EQUAL( status, expected_status );
2024 }
2025 else
2026 {
2027 TEST_EQUAL( status, expected_status );
2028 }
2029 }
2030 else
2031 {
2032 TEST_EQUAL( status, expected_status );
2033 }
2034
Gilles Peskine88e08462020-01-28 20:43:00 +01002035exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002036 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002037 PSA_DONE( );
2038}
2039/* END_CASE */
2040
2041/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002042void hash_compute_compare( int alg_arg, data_t *input,
2043 data_t *expected_output )
2044{
2045 psa_algorithm_t alg = alg_arg;
2046 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2047 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002048 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002049 size_t i;
2050
2051 PSA_ASSERT( psa_crypto_init( ) );
2052
Neil Armstrongca30a002022-02-07 11:40:23 +01002053 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002054 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002055 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002056 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002057 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002058 ASSERT_COMPARE( output, output_length,
2059 expected_output->x, expected_output->len );
2060
Neil Armstrongca30a002022-02-07 11:40:23 +01002061 /* Compute with tight buffer, multi-part */
2062 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2063 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2064 PSA_ASSERT( psa_hash_finish( &operation, output,
2065 PSA_HASH_LENGTH( alg ),
2066 &output_length ) );
2067 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2068 ASSERT_COMPARE( output, output_length,
2069 expected_output->x, expected_output->len );
2070
2071 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002072 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2073 output, sizeof( output ),
2074 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002075 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002076 ASSERT_COMPARE( output, output_length,
2077 expected_output->x, expected_output->len );
2078
Neil Armstrongca30a002022-02-07 11:40:23 +01002079 /* Compute with larger buffer, multi-part */
2080 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2081 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2082 PSA_ASSERT( psa_hash_finish( &operation, output,
2083 sizeof( output ), &output_length ) );
2084 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2085 ASSERT_COMPARE( output, output_length,
2086 expected_output->x, expected_output->len );
2087
2088 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002089 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2090 output, output_length ) );
2091
Neil Armstrongca30a002022-02-07 11:40:23 +01002092 /* Compare with correct hash, multi-part */
2093 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2094 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2095 PSA_ASSERT( psa_hash_verify( &operation, output,
2096 output_length ) );
2097
2098 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002099 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2100 output, output_length + 1 ),
2101 PSA_ERROR_INVALID_SIGNATURE );
2102
Neil Armstrongca30a002022-02-07 11:40:23 +01002103 /* Compare with trailing garbage, multi-part */
2104 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2105 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2106 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2107 PSA_ERROR_INVALID_SIGNATURE );
2108
2109 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002110 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2111 output, output_length - 1 ),
2112 PSA_ERROR_INVALID_SIGNATURE );
2113
Neil Armstrongca30a002022-02-07 11:40:23 +01002114 /* Compare with truncated hash, multi-part */
2115 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2116 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2117 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2118 PSA_ERROR_INVALID_SIGNATURE );
2119
Gilles Peskine0a749c82019-11-28 19:33:58 +01002120 /* Compare with corrupted value */
2121 for( i = 0; i < output_length; i++ )
2122 {
Chris Jones9634bb12021-01-20 15:56:42 +00002123 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002124 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002125
2126 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002127 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2128 output, output_length ),
2129 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002130
2131 /* Multi-Part */
2132 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2133 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2134 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2135 PSA_ERROR_INVALID_SIGNATURE );
2136
Gilles Peskine0a749c82019-11-28 19:33:58 +01002137 output[i] ^= 1;
2138 }
2139
2140exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002141 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002142 PSA_DONE( );
2143}
2144/* END_CASE */
2145
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002146/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002147void hash_bad_order( )
2148{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002149 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002150 unsigned char input[] = "";
2151 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002152 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002153 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2154 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2155 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002156 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002157 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002158 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002159
Gilles Peskine8817f612018-12-18 00:18:46 +01002160 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002161
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002162 /* Call setup twice in a row. */
2163 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002164 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002165 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2166 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002167 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002168 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002169 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002170
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002171 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002172 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002173 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002174 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002175
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002176 /* Check that update calls abort on error. */
2177 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002178 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002179 ASSERT_OPERATION_IS_ACTIVE( operation );
2180 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2181 PSA_ERROR_BAD_STATE );
2182 ASSERT_OPERATION_IS_INACTIVE( operation );
2183 PSA_ASSERT( psa_hash_abort( &operation ) );
2184 ASSERT_OPERATION_IS_INACTIVE( operation );
2185
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002186 /* Call update after finish. */
2187 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2188 PSA_ASSERT( psa_hash_finish( &operation,
2189 hash, sizeof( hash ), &hash_len ) );
2190 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002191 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002192 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002193
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002194 /* Call verify without calling setup beforehand. */
2195 TEST_EQUAL( psa_hash_verify( &operation,
2196 valid_hash, sizeof( valid_hash ) ),
2197 PSA_ERROR_BAD_STATE );
2198 PSA_ASSERT( psa_hash_abort( &operation ) );
2199
2200 /* Call verify after finish. */
2201 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2202 PSA_ASSERT( psa_hash_finish( &operation,
2203 hash, sizeof( hash ), &hash_len ) );
2204 TEST_EQUAL( psa_hash_verify( &operation,
2205 valid_hash, sizeof( valid_hash ) ),
2206 PSA_ERROR_BAD_STATE );
2207 PSA_ASSERT( psa_hash_abort( &operation ) );
2208
2209 /* Call verify twice in a row. */
2210 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002211 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002212 PSA_ASSERT( psa_hash_verify( &operation,
2213 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002214 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002215 TEST_EQUAL( psa_hash_verify( &operation,
2216 valid_hash, sizeof( valid_hash ) ),
2217 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002218 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002219 PSA_ASSERT( psa_hash_abort( &operation ) );
2220
2221 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002222 TEST_EQUAL( psa_hash_finish( &operation,
2223 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002224 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002225 PSA_ASSERT( psa_hash_abort( &operation ) );
2226
2227 /* Call finish twice in a row. */
2228 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2229 PSA_ASSERT( psa_hash_finish( &operation,
2230 hash, sizeof( hash ), &hash_len ) );
2231 TEST_EQUAL( psa_hash_finish( &operation,
2232 hash, sizeof( hash ), &hash_len ),
2233 PSA_ERROR_BAD_STATE );
2234 PSA_ASSERT( psa_hash_abort( &operation ) );
2235
2236 /* Call finish after calling verify. */
2237 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2238 PSA_ASSERT( psa_hash_verify( &operation,
2239 valid_hash, sizeof( valid_hash ) ) );
2240 TEST_EQUAL( psa_hash_finish( &operation,
2241 hash, sizeof( hash ), &hash_len ),
2242 PSA_ERROR_BAD_STATE );
2243 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002244
2245exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002246 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002247}
2248/* END_CASE */
2249
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002250/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002251void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002252{
2253 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002254 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2255 * appended to it */
2256 unsigned char hash[] = {
2257 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2258 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2259 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002260 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002261 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002262
Gilles Peskine8817f612018-12-18 00:18:46 +01002263 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002264
itayzafrir27e69452018-11-01 14:26:34 +02002265 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002266 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002267 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002268 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002269 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002270 ASSERT_OPERATION_IS_INACTIVE( operation );
2271 PSA_ASSERT( psa_hash_abort( &operation ) );
2272 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002273
itayzafrir27e69452018-11-01 14:26:34 +02002274 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002275 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002276 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002277 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002278
itayzafrir27e69452018-11-01 14:26:34 +02002279 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002280 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002281 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002282 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002283
itayzafrirec93d302018-10-18 18:01:10 +03002284exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002285 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002286}
2287/* END_CASE */
2288
Ronald Cronee414c72021-03-18 18:50:08 +01002289/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002290void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002291{
2292 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002293 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002294 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002295 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002296 size_t hash_len;
2297
Gilles Peskine8817f612018-12-18 00:18:46 +01002298 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002299
itayzafrir58028322018-10-25 10:22:01 +03002300 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002301 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002302 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002303 hash, expected_size - 1, &hash_len ),
2304 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002305
2306exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002307 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002308}
2309/* END_CASE */
2310
Ronald Cronee414c72021-03-18 18:50:08 +01002311/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002312void hash_clone_source_state( )
2313{
2314 psa_algorithm_t alg = PSA_ALG_SHA_256;
2315 unsigned char hash[PSA_HASH_MAX_SIZE];
2316 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2317 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2318 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2319 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2320 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2321 size_t hash_len;
2322
2323 PSA_ASSERT( psa_crypto_init( ) );
2324 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2325
2326 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2327 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2328 PSA_ASSERT( psa_hash_finish( &op_finished,
2329 hash, sizeof( hash ), &hash_len ) );
2330 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2331 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2332
2333 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2334 PSA_ERROR_BAD_STATE );
2335
2336 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2337 PSA_ASSERT( psa_hash_finish( &op_init,
2338 hash, sizeof( hash ), &hash_len ) );
2339 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2340 PSA_ASSERT( psa_hash_finish( &op_finished,
2341 hash, sizeof( hash ), &hash_len ) );
2342 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2343 PSA_ASSERT( psa_hash_finish( &op_aborted,
2344 hash, sizeof( hash ), &hash_len ) );
2345
2346exit:
2347 psa_hash_abort( &op_source );
2348 psa_hash_abort( &op_init );
2349 psa_hash_abort( &op_setup );
2350 psa_hash_abort( &op_finished );
2351 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002352 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002353}
2354/* END_CASE */
2355
Ronald Cronee414c72021-03-18 18:50:08 +01002356/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002357void hash_clone_target_state( )
2358{
2359 psa_algorithm_t alg = PSA_ALG_SHA_256;
2360 unsigned char hash[PSA_HASH_MAX_SIZE];
2361 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2362 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2363 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2364 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2365 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2366 size_t hash_len;
2367
2368 PSA_ASSERT( psa_crypto_init( ) );
2369
2370 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2371 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2372 PSA_ASSERT( psa_hash_finish( &op_finished,
2373 hash, sizeof( hash ), &hash_len ) );
2374 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2375 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2376
2377 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2378 PSA_ASSERT( psa_hash_finish( &op_target,
2379 hash, sizeof( hash ), &hash_len ) );
2380
2381 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2382 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2383 PSA_ERROR_BAD_STATE );
2384 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2385 PSA_ERROR_BAD_STATE );
2386
2387exit:
2388 psa_hash_abort( &op_target );
2389 psa_hash_abort( &op_init );
2390 psa_hash_abort( &op_setup );
2391 psa_hash_abort( &op_finished );
2392 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002393 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002394}
2395/* END_CASE */
2396
itayzafrir58028322018-10-25 10:22:01 +03002397/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002398void mac_operation_init( )
2399{
Jaeden Amero252ef282019-02-15 14:05:35 +00002400 const uint8_t input[1] = { 0 };
2401
Jaeden Amero769ce272019-01-04 11:48:03 +00002402 /* Test each valid way of initializing the object, except for `= {0}`, as
2403 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2404 * though it's OK by the C standard. We could test for this, but we'd need
2405 * to supress the Clang warning for the test. */
2406 psa_mac_operation_t func = psa_mac_operation_init( );
2407 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2408 psa_mac_operation_t zero;
2409
2410 memset( &zero, 0, sizeof( zero ) );
2411
Jaeden Amero252ef282019-02-15 14:05:35 +00002412 /* A freshly-initialized MAC operation should not be usable. */
2413 TEST_EQUAL( psa_mac_update( &func,
2414 input, sizeof( input ) ),
2415 PSA_ERROR_BAD_STATE );
2416 TEST_EQUAL( psa_mac_update( &init,
2417 input, sizeof( input ) ),
2418 PSA_ERROR_BAD_STATE );
2419 TEST_EQUAL( psa_mac_update( &zero,
2420 input, sizeof( input ) ),
2421 PSA_ERROR_BAD_STATE );
2422
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002423 /* A default MAC operation should be abortable without error. */
2424 PSA_ASSERT( psa_mac_abort( &func ) );
2425 PSA_ASSERT( psa_mac_abort( &init ) );
2426 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002427}
2428/* END_CASE */
2429
2430/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002431void mac_setup( int key_type_arg,
2432 data_t *key,
2433 int alg_arg,
2434 int expected_status_arg )
2435{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002436 psa_key_type_t key_type = key_type_arg;
2437 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002438 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002439 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002440 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2441#if defined(KNOWN_SUPPORTED_MAC_ALG)
2442 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2443#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444
Gilles Peskine8817f612018-12-18 00:18:46 +01002445 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002446
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002447 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2448 &operation, &status ) )
2449 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002450 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002451
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002452 /* The operation object should be reusable. */
2453#if defined(KNOWN_SUPPORTED_MAC_ALG)
2454 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2455 smoke_test_key_data,
2456 sizeof( smoke_test_key_data ),
2457 KNOWN_SUPPORTED_MAC_ALG,
2458 &operation, &status ) )
2459 goto exit;
2460 TEST_EQUAL( status, PSA_SUCCESS );
2461#endif
2462
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002463exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002464 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002465}
2466/* END_CASE */
2467
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002468/* 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 +00002469void mac_bad_order( )
2470{
Ronald Cron5425a212020-08-04 14:58:35 +02002471 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002472 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2473 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002474 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002475 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2476 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2477 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002479 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2480 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2481 size_t sign_mac_length = 0;
2482 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2483 const uint8_t verify_mac[] = {
2484 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2485 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2486 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2487
2488 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002489 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002490 psa_set_key_algorithm( &attributes, alg );
2491 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002492
Ronald Cron5425a212020-08-04 14:58:35 +02002493 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2494 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002495
Jaeden Amero252ef282019-02-15 14:05:35 +00002496 /* Call update without calling setup beforehand. */
2497 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2498 PSA_ERROR_BAD_STATE );
2499 PSA_ASSERT( psa_mac_abort( &operation ) );
2500
2501 /* Call sign finish without calling setup beforehand. */
2502 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2503 &sign_mac_length),
2504 PSA_ERROR_BAD_STATE );
2505 PSA_ASSERT( psa_mac_abort( &operation ) );
2506
2507 /* Call verify finish without calling setup beforehand. */
2508 TEST_EQUAL( psa_mac_verify_finish( &operation,
2509 verify_mac, sizeof( verify_mac ) ),
2510 PSA_ERROR_BAD_STATE );
2511 PSA_ASSERT( psa_mac_abort( &operation ) );
2512
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002513 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002514 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002515 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002516 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002517 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002518 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002519 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002520 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002521
Jaeden Amero252ef282019-02-15 14:05:35 +00002522 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002523 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002524 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2525 PSA_ASSERT( psa_mac_sign_finish( &operation,
2526 sign_mac, sizeof( sign_mac ),
2527 &sign_mac_length ) );
2528 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2529 PSA_ERROR_BAD_STATE );
2530 PSA_ASSERT( psa_mac_abort( &operation ) );
2531
2532 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002533 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002534 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2535 PSA_ASSERT( psa_mac_verify_finish( &operation,
2536 verify_mac, sizeof( verify_mac ) ) );
2537 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2538 PSA_ERROR_BAD_STATE );
2539 PSA_ASSERT( psa_mac_abort( &operation ) );
2540
2541 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002542 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002543 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2544 PSA_ASSERT( psa_mac_sign_finish( &operation,
2545 sign_mac, sizeof( sign_mac ),
2546 &sign_mac_length ) );
2547 TEST_EQUAL( psa_mac_sign_finish( &operation,
2548 sign_mac, sizeof( sign_mac ),
2549 &sign_mac_length ),
2550 PSA_ERROR_BAD_STATE );
2551 PSA_ASSERT( psa_mac_abort( &operation ) );
2552
2553 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002554 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002555 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2556 PSA_ASSERT( psa_mac_verify_finish( &operation,
2557 verify_mac, sizeof( verify_mac ) ) );
2558 TEST_EQUAL( psa_mac_verify_finish( &operation,
2559 verify_mac, sizeof( verify_mac ) ),
2560 PSA_ERROR_BAD_STATE );
2561 PSA_ASSERT( psa_mac_abort( &operation ) );
2562
2563 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002564 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002565 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002566 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002567 TEST_EQUAL( psa_mac_verify_finish( &operation,
2568 verify_mac, sizeof( verify_mac ) ),
2569 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002570 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002571 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002572 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002573
2574 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002575 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002576 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002577 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002578 TEST_EQUAL( psa_mac_sign_finish( &operation,
2579 sign_mac, sizeof( sign_mac ),
2580 &sign_mac_length ),
2581 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002582 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002583 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002584 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002585
Ronald Cron5425a212020-08-04 14:58:35 +02002586 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002587
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002588exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002589 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002590}
2591/* END_CASE */
2592
2593/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002594void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002595 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002596 int alg_arg,
2597 data_t *input,
2598 data_t *expected_mac )
2599{
Ronald Cron5425a212020-08-04 14:58:35 +02002600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002601 psa_key_type_t key_type = key_type_arg;
2602 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002603 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002605 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002606 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002607 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002608 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002609 const size_t output_sizes_to_test[] = {
2610 0,
2611 1,
2612 expected_mac->len - 1,
2613 expected_mac->len,
2614 expected_mac->len + 1,
2615 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002616
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002617 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002618 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002619 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002620
Gilles Peskine8817f612018-12-18 00:18:46 +01002621 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002622
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002623 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002624 psa_set_key_algorithm( &attributes, alg );
2625 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002626
Ronald Cron5425a212020-08-04 14:58:35 +02002627 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2628 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002629
Gilles Peskine8b356b52020-08-25 23:44:59 +02002630 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2631 {
2632 const size_t output_size = output_sizes_to_test[i];
2633 psa_status_t expected_status =
2634 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2635 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002636
Chris Jones9634bb12021-01-20 15:56:42 +00002637 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002638 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002639
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002640 /* Calculate the MAC, one-shot case. */
2641 TEST_EQUAL( psa_mac_compute( key, alg,
2642 input->x, input->len,
2643 actual_mac, output_size, &mac_length ),
2644 expected_status );
2645 if( expected_status == PSA_SUCCESS )
2646 {
2647 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2648 actual_mac, mac_length );
2649 }
2650
2651 if( output_size > 0 )
2652 memset( actual_mac, 0, output_size );
2653
2654 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002655 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002656 PSA_ASSERT( psa_mac_update( &operation,
2657 input->x, input->len ) );
2658 TEST_EQUAL( psa_mac_sign_finish( &operation,
2659 actual_mac, output_size,
2660 &mac_length ),
2661 expected_status );
2662 PSA_ASSERT( psa_mac_abort( &operation ) );
2663
2664 if( expected_status == PSA_SUCCESS )
2665 {
2666 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2667 actual_mac, mac_length );
2668 }
2669 mbedtls_free( actual_mac );
2670 actual_mac = NULL;
2671 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002672
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002673exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002674 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002675 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002676 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002677 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002678}
2679/* END_CASE */
2680
2681/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002682void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002683 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002684 int alg_arg,
2685 data_t *input,
2686 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687{
Ronald Cron5425a212020-08-04 14:58:35 +02002688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689 psa_key_type_t key_type = key_type_arg;
2690 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002691 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002693 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694
Gilles Peskine69c12672018-06-28 00:07:19 +02002695 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2696
Gilles Peskine8817f612018-12-18 00:18:46 +01002697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002698
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002699 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002700 psa_set_key_algorithm( &attributes, alg );
2701 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002702
Ronald Cron5425a212020-08-04 14:58:35 +02002703 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2704 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002705
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002706 /* Verify correct MAC, one-shot case. */
2707 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2708 expected_mac->x, expected_mac->len ) );
2709
2710 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002711 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_mac_update( &operation,
2713 input->x, input->len ) );
2714 PSA_ASSERT( psa_mac_verify_finish( &operation,
2715 expected_mac->x,
2716 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002717
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002718 /* Test a MAC that's too short, one-shot case. */
2719 TEST_EQUAL( psa_mac_verify( key, alg,
2720 input->x, input->len,
2721 expected_mac->x,
2722 expected_mac->len - 1 ),
2723 PSA_ERROR_INVALID_SIGNATURE );
2724
2725 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002726 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002727 PSA_ASSERT( psa_mac_update( &operation,
2728 input->x, input->len ) );
2729 TEST_EQUAL( psa_mac_verify_finish( &operation,
2730 expected_mac->x,
2731 expected_mac->len - 1 ),
2732 PSA_ERROR_INVALID_SIGNATURE );
2733
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002734 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002735 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2736 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002737 TEST_EQUAL( psa_mac_verify( key, alg,
2738 input->x, input->len,
2739 perturbed_mac, expected_mac->len + 1 ),
2740 PSA_ERROR_INVALID_SIGNATURE );
2741
2742 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002743 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002744 PSA_ASSERT( psa_mac_update( &operation,
2745 input->x, input->len ) );
2746 TEST_EQUAL( psa_mac_verify_finish( &operation,
2747 perturbed_mac,
2748 expected_mac->len + 1 ),
2749 PSA_ERROR_INVALID_SIGNATURE );
2750
2751 /* Test changing one byte. */
2752 for( size_t i = 0; i < expected_mac->len; i++ )
2753 {
Chris Jones9634bb12021-01-20 15:56:42 +00002754 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002755 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002756
2757 TEST_EQUAL( psa_mac_verify( key, alg,
2758 input->x, input->len,
2759 perturbed_mac, expected_mac->len ),
2760 PSA_ERROR_INVALID_SIGNATURE );
2761
Ronald Cron5425a212020-08-04 14:58:35 +02002762 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002763 PSA_ASSERT( psa_mac_update( &operation,
2764 input->x, input->len ) );
2765 TEST_EQUAL( psa_mac_verify_finish( &operation,
2766 perturbed_mac,
2767 expected_mac->len ),
2768 PSA_ERROR_INVALID_SIGNATURE );
2769 perturbed_mac[i] ^= 1;
2770 }
2771
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002773 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002774 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002775 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002776 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002777}
2778/* END_CASE */
2779
2780/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002781void cipher_operation_init( )
2782{
Jaeden Ameroab439972019-02-15 14:12:05 +00002783 const uint8_t input[1] = { 0 };
2784 unsigned char output[1] = { 0 };
2785 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002786 /* Test each valid way of initializing the object, except for `= {0}`, as
2787 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2788 * though it's OK by the C standard. We could test for this, but we'd need
2789 * to supress the Clang warning for the test. */
2790 psa_cipher_operation_t func = psa_cipher_operation_init( );
2791 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2792 psa_cipher_operation_t zero;
2793
2794 memset( &zero, 0, sizeof( zero ) );
2795
Jaeden Ameroab439972019-02-15 14:12:05 +00002796 /* A freshly-initialized cipher operation should not be usable. */
2797 TEST_EQUAL( psa_cipher_update( &func,
2798 input, sizeof( input ),
2799 output, sizeof( output ),
2800 &output_length ),
2801 PSA_ERROR_BAD_STATE );
2802 TEST_EQUAL( psa_cipher_update( &init,
2803 input, sizeof( input ),
2804 output, sizeof( output ),
2805 &output_length ),
2806 PSA_ERROR_BAD_STATE );
2807 TEST_EQUAL( psa_cipher_update( &zero,
2808 input, sizeof( input ),
2809 output, sizeof( output ),
2810 &output_length ),
2811 PSA_ERROR_BAD_STATE );
2812
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002813 /* A default cipher operation should be abortable without error. */
2814 PSA_ASSERT( psa_cipher_abort( &func ) );
2815 PSA_ASSERT( psa_cipher_abort( &init ) );
2816 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002817}
2818/* END_CASE */
2819
2820/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002821void cipher_setup( int key_type_arg,
2822 data_t *key,
2823 int alg_arg,
2824 int expected_status_arg )
2825{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002826 psa_key_type_t key_type = key_type_arg;
2827 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002828 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002829 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002830 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002831#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002832 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2833#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002834
Gilles Peskine8817f612018-12-18 00:18:46 +01002835 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002836
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002837 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2838 &operation, &status ) )
2839 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002840 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002841
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002842 /* The operation object should be reusable. */
2843#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2844 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2845 smoke_test_key_data,
2846 sizeof( smoke_test_key_data ),
2847 KNOWN_SUPPORTED_CIPHER_ALG,
2848 &operation, &status ) )
2849 goto exit;
2850 TEST_EQUAL( status, PSA_SUCCESS );
2851#endif
2852
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002853exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002854 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002855 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002856}
2857/* END_CASE */
2858
Ronald Cronee414c72021-03-18 18:50:08 +01002859/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002860void cipher_bad_order( )
2861{
Ronald Cron5425a212020-08-04 14:58:35 +02002862 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002863 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2864 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002866 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002867 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002868 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002869 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2870 0xaa, 0xaa, 0xaa, 0xaa };
2871 const uint8_t text[] = {
2872 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2873 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002874 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002875 size_t length = 0;
2876
2877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002878 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2879 psa_set_key_algorithm( &attributes, alg );
2880 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002881 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2882 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002883
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002884 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002885 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002886 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002887 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002888 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002889 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002890 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002891 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002892
2893 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002894 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002895 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002896 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002897 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002898 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002899 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002900 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002901
Jaeden Ameroab439972019-02-15 14:12:05 +00002902 /* Generate an IV without calling setup beforehand. */
2903 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2904 buffer, sizeof( buffer ),
2905 &length ),
2906 PSA_ERROR_BAD_STATE );
2907 PSA_ASSERT( psa_cipher_abort( &operation ) );
2908
2909 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002910 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002911 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2912 buffer, sizeof( buffer ),
2913 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002914 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002915 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2916 buffer, sizeof( buffer ),
2917 &length ),
2918 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002919 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002920 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002921 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002922
2923 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002924 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002925 PSA_ASSERT( psa_cipher_set_iv( &operation,
2926 iv, sizeof( iv ) ) );
2927 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2928 buffer, sizeof( buffer ),
2929 &length ),
2930 PSA_ERROR_BAD_STATE );
2931 PSA_ASSERT( psa_cipher_abort( &operation ) );
2932
2933 /* Set an IV without calling setup beforehand. */
2934 TEST_EQUAL( psa_cipher_set_iv( &operation,
2935 iv, sizeof( iv ) ),
2936 PSA_ERROR_BAD_STATE );
2937 PSA_ASSERT( psa_cipher_abort( &operation ) );
2938
2939 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002940 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002941 PSA_ASSERT( psa_cipher_set_iv( &operation,
2942 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002943 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002944 TEST_EQUAL( psa_cipher_set_iv( &operation,
2945 iv, sizeof( iv ) ),
2946 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002947 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002948 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002949 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002950
2951 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002952 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002953 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2954 buffer, sizeof( buffer ),
2955 &length ) );
2956 TEST_EQUAL( psa_cipher_set_iv( &operation,
2957 iv, sizeof( iv ) ),
2958 PSA_ERROR_BAD_STATE );
2959 PSA_ASSERT( psa_cipher_abort( &operation ) );
2960
2961 /* Call update without calling setup beforehand. */
2962 TEST_EQUAL( psa_cipher_update( &operation,
2963 text, sizeof( text ),
2964 buffer, sizeof( buffer ),
2965 &length ),
2966 PSA_ERROR_BAD_STATE );
2967 PSA_ASSERT( psa_cipher_abort( &operation ) );
2968
2969 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002970 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002971 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002972 TEST_EQUAL( psa_cipher_update( &operation,
2973 text, sizeof( text ),
2974 buffer, sizeof( buffer ),
2975 &length ),
2976 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002977 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002978 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002979 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002980
2981 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002982 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002983 PSA_ASSERT( psa_cipher_set_iv( &operation,
2984 iv, sizeof( iv ) ) );
2985 PSA_ASSERT( psa_cipher_finish( &operation,
2986 buffer, sizeof( buffer ), &length ) );
2987 TEST_EQUAL( psa_cipher_update( &operation,
2988 text, sizeof( text ),
2989 buffer, sizeof( buffer ),
2990 &length ),
2991 PSA_ERROR_BAD_STATE );
2992 PSA_ASSERT( psa_cipher_abort( &operation ) );
2993
2994 /* Call finish without calling setup beforehand. */
2995 TEST_EQUAL( psa_cipher_finish( &operation,
2996 buffer, sizeof( buffer ), &length ),
2997 PSA_ERROR_BAD_STATE );
2998 PSA_ASSERT( psa_cipher_abort( &operation ) );
2999
3000 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003001 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003002 /* Not calling update means we are encrypting an empty buffer, which is OK
3003 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003004 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003005 TEST_EQUAL( psa_cipher_finish( &operation,
3006 buffer, sizeof( buffer ), &length ),
3007 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003008 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003009 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003010 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003011
3012 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003013 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003014 PSA_ASSERT( psa_cipher_set_iv( &operation,
3015 iv, sizeof( iv ) ) );
3016 PSA_ASSERT( psa_cipher_finish( &operation,
3017 buffer, sizeof( buffer ), &length ) );
3018 TEST_EQUAL( psa_cipher_finish( &operation,
3019 buffer, sizeof( buffer ), &length ),
3020 PSA_ERROR_BAD_STATE );
3021 PSA_ASSERT( psa_cipher_abort( &operation ) );
3022
Ronald Cron5425a212020-08-04 14:58:35 +02003023 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003024
Jaeden Ameroab439972019-02-15 14:12:05 +00003025exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003026 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003027 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003028}
3029/* END_CASE */
3030
3031/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003032void cipher_encrypt_fail( int alg_arg,
3033 int key_type_arg,
3034 data_t *key_data,
3035 data_t *input,
3036 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003037{
Ronald Cron5425a212020-08-04 14:58:35 +02003038 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003039 psa_status_t status;
3040 psa_key_type_t key_type = key_type_arg;
3041 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003042 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003043 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003044 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003045 size_t output_length = 0;
3046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3047
3048 if ( PSA_ERROR_BAD_STATE != expected_status )
3049 {
3050 PSA_ASSERT( psa_crypto_init( ) );
3051
3052 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3053 psa_set_key_algorithm( &attributes, alg );
3054 psa_set_key_type( &attributes, key_type );
3055
3056 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3057 input->len );
3058 ASSERT_ALLOC( output, output_buffer_size );
3059
3060 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3061 &key ) );
3062 }
3063
3064 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3065 output_buffer_size, &output_length );
3066
3067 TEST_EQUAL( status, expected_status );
3068
3069exit:
3070 mbedtls_free( output );
3071 psa_destroy_key( key );
3072 PSA_DONE( );
3073}
3074/* END_CASE */
3075
3076/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003077void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3078 data_t *input, int iv_length,
3079 int expected_result )
3080{
3081 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3082 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3084 size_t output_buffer_size = 0;
3085 unsigned char *output = NULL;
3086
3087 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3088 ASSERT_ALLOC( output, output_buffer_size );
3089
3090 PSA_ASSERT( psa_crypto_init( ) );
3091
3092 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3093 psa_set_key_algorithm( &attributes, alg );
3094 psa_set_key_type( &attributes, key_type );
3095
3096 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3097 &key ) );
3098 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3099 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3100 iv_length ) );
3101
3102exit:
3103 psa_cipher_abort( &operation );
3104 mbedtls_free( output );
3105 psa_destroy_key( key );
3106 PSA_DONE( );
3107}
3108/* END_CASE */
3109
3110/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003111void cipher_encrypt_alg_without_iv( int alg_arg,
3112 int key_type_arg,
3113 data_t *key_data,
3114 data_t *input,
3115 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003116{
3117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3118 psa_key_type_t key_type = key_type_arg;
3119 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003120 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3121 uint8_t iv[1] = { 0x5a };
3122 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003123 unsigned char *output = NULL;
3124 size_t output_buffer_size = 0;
3125 size_t output_length = 0;
3126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3127
3128 PSA_ASSERT( psa_crypto_init( ) );
3129
3130 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3131 psa_set_key_algorithm( &attributes, alg );
3132 psa_set_key_type( &attributes, key_type );
3133
3134 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3135 ASSERT_ALLOC( output, output_buffer_size );
3136
3137 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3138 &key ) );
3139
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003140 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3141 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3142 PSA_ERROR_BAD_STATE );
3143 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3144 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
3145 &iv_length ),
3146 PSA_ERROR_BAD_STATE );
3147
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003148 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
3149 output_buffer_size, &output_length ) );
3150 TEST_ASSERT( output_length <=
3151 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3152 TEST_ASSERT( output_length <=
3153 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3154
3155 ASSERT_COMPARE( expected_output->x, expected_output->len,
3156 output, output_length );
3157exit:
3158 mbedtls_free( output );
3159 psa_destroy_key( key );
3160 PSA_DONE( );
3161}
3162/* END_CASE */
3163
3164/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003165void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3166{
3167 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3168 psa_algorithm_t alg = alg_arg;
3169 psa_key_type_t key_type = key_type_arg;
3170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3171 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3172 psa_status_t status;
3173
3174 PSA_ASSERT( psa_crypto_init( ) );
3175
3176 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3177 psa_set_key_algorithm( &attributes, alg );
3178 psa_set_key_type( &attributes, key_type );
3179
3180 /* Usage of either of these two size macros would cause divide by zero
3181 * with incorrect key types previously. Input length should be irrelevant
3182 * here. */
3183 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3184 0 );
3185 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3186
3187
3188 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3189 &key ) );
3190
3191 /* Should fail due to invalid alg type (to support invalid key type).
3192 * Encrypt or decrypt will end up in the same place. */
3193 status = psa_cipher_encrypt_setup( &operation, key, alg );
3194
3195 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3196
3197exit:
3198 psa_cipher_abort( &operation );
3199 psa_destroy_key( key );
3200 PSA_DONE( );
3201}
3202/* END_CASE */
3203
3204/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003205void cipher_encrypt_validation( int alg_arg,
3206 int key_type_arg,
3207 data_t *key_data,
3208 data_t *input )
3209{
3210 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3211 psa_key_type_t key_type = key_type_arg;
3212 psa_algorithm_t alg = alg_arg;
3213 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3214 unsigned char *output1 = NULL;
3215 size_t output1_buffer_size = 0;
3216 size_t output1_length = 0;
3217 unsigned char *output2 = NULL;
3218 size_t output2_buffer_size = 0;
3219 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003220 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003221 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003222 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003223
Gilles Peskine8817f612018-12-18 00:18:46 +01003224 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003225
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003226 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3227 psa_set_key_algorithm( &attributes, alg );
3228 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003229
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003230 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3231 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3232 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3233 ASSERT_ALLOC( output1, output1_buffer_size );
3234 ASSERT_ALLOC( output2, output2_buffer_size );
3235
Ronald Cron5425a212020-08-04 14:58:35 +02003236 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3237 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003238
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003239 /* The one-shot cipher encryption uses generated iv so validating
3240 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003241 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3242 output1_buffer_size, &output1_length ) );
3243 TEST_ASSERT( output1_length <=
3244 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3245 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003246 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003247
3248 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3249 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003250
Gilles Peskine8817f612018-12-18 00:18:46 +01003251 PSA_ASSERT( psa_cipher_update( &operation,
3252 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003253 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003254 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003255 TEST_ASSERT( function_output_length <=
3256 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3257 TEST_ASSERT( function_output_length <=
3258 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003259 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003260
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003261 PSA_ASSERT( psa_cipher_finish( &operation,
3262 output2 + output2_length,
3263 output2_buffer_size - output2_length,
3264 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003265 TEST_ASSERT( function_output_length <=
3266 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3267 TEST_ASSERT( function_output_length <=
3268 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003269 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003270
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003271 PSA_ASSERT( psa_cipher_abort( &operation ) );
3272 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3273 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003274
Gilles Peskine50e586b2018-06-08 14:28:46 +02003275exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003276 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003277 mbedtls_free( output1 );
3278 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003279 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003280 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003281}
3282/* END_CASE */
3283
3284/* BEGIN_CASE */
3285void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003286 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003287 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003288 int first_part_size_arg,
3289 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003290 data_t *expected_output,
3291 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003292{
Ronald Cron5425a212020-08-04 14:58:35 +02003293 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003294 psa_key_type_t key_type = key_type_arg;
3295 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003296 psa_status_t status;
3297 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003298 size_t first_part_size = first_part_size_arg;
3299 size_t output1_length = output1_length_arg;
3300 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003301 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003302 size_t output_buffer_size = 0;
3303 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003304 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003305 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003307
Gilles Peskine8817f612018-12-18 00:18:46 +01003308 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003309
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003310 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3311 psa_set_key_algorithm( &attributes, alg );
3312 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003313
Ronald Cron5425a212020-08-04 14:58:35 +02003314 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3315 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003316
Ronald Cron5425a212020-08-04 14:58:35 +02003317 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003318
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003319 if( iv->len > 0 )
3320 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003321 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003322 }
3323
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003324 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3325 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003326 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003327
Gilles Peskinee0866522019-02-19 19:44:00 +01003328 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003329 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3330 output, output_buffer_size,
3331 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003332 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003333 TEST_ASSERT( function_output_length <=
3334 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3335 TEST_ASSERT( function_output_length <=
3336 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003337 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003338
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003339 if( first_part_size < input->len )
3340 {
3341 PSA_ASSERT( psa_cipher_update( &operation,
3342 input->x + first_part_size,
3343 input->len - first_part_size,
3344 ( output_buffer_size == 0 ? NULL :
3345 output + total_output_length ),
3346 output_buffer_size - total_output_length,
3347 &function_output_length ) );
3348 TEST_ASSERT( function_output_length == output2_length );
3349 TEST_ASSERT( function_output_length <=
3350 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3351 alg,
3352 input->len - first_part_size ) );
3353 TEST_ASSERT( function_output_length <=
3354 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3355 total_output_length += function_output_length;
3356 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003357
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003358 status = psa_cipher_finish( &operation,
3359 ( output_buffer_size == 0 ? NULL :
3360 output + total_output_length ),
3361 output_buffer_size - total_output_length,
3362 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003363 TEST_ASSERT( function_output_length <=
3364 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3365 TEST_ASSERT( function_output_length <=
3366 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003367 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003368 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003369
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003370 if( expected_status == PSA_SUCCESS )
3371 {
3372 PSA_ASSERT( psa_cipher_abort( &operation ) );
3373
3374 ASSERT_COMPARE( expected_output->x, expected_output->len,
3375 output, total_output_length );
3376 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003377
3378exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003379 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003380 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003381 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003382 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003383}
3384/* END_CASE */
3385
3386/* BEGIN_CASE */
3387void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003388 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003389 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003390 int first_part_size_arg,
3391 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003392 data_t *expected_output,
3393 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003394{
Ronald Cron5425a212020-08-04 14:58:35 +02003395 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003396 psa_key_type_t key_type = key_type_arg;
3397 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003398 psa_status_t status;
3399 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003400 size_t first_part_size = first_part_size_arg;
3401 size_t output1_length = output1_length_arg;
3402 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003403 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003404 size_t output_buffer_size = 0;
3405 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003406 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003407 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003409
Gilles Peskine8817f612018-12-18 00:18:46 +01003410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003411
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3413 psa_set_key_algorithm( &attributes, alg );
3414 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003415
Ronald Cron5425a212020-08-04 14:58:35 +02003416 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3417 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003418
Ronald Cron5425a212020-08-04 14:58:35 +02003419 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003420
Steven Cooreman177deba2020-09-07 17:14:14 +02003421 if( iv->len > 0 )
3422 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003423 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003424 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003425
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003426 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3427 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003428 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003429
Gilles Peskinee0866522019-02-19 19:44:00 +01003430 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003431 PSA_ASSERT( psa_cipher_update( &operation,
3432 input->x, first_part_size,
3433 output, output_buffer_size,
3434 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003435 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003436 TEST_ASSERT( function_output_length <=
3437 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3438 TEST_ASSERT( function_output_length <=
3439 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003440 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003441
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003442 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003443 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003444 PSA_ASSERT( psa_cipher_update( &operation,
3445 input->x + first_part_size,
3446 input->len - first_part_size,
3447 ( output_buffer_size == 0 ? NULL :
3448 output + total_output_length ),
3449 output_buffer_size - total_output_length,
3450 &function_output_length ) );
3451 TEST_ASSERT( function_output_length == output2_length );
3452 TEST_ASSERT( function_output_length <=
3453 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3454 alg,
3455 input->len - first_part_size ) );
3456 TEST_ASSERT( function_output_length <=
3457 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3458 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003459 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003460
Gilles Peskine50e586b2018-06-08 14:28:46 +02003461 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003462 ( output_buffer_size == 0 ? NULL :
3463 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003464 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003465 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003466 TEST_ASSERT( function_output_length <=
3467 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3468 TEST_ASSERT( function_output_length <=
3469 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003470 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003471 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003472
3473 if( expected_status == PSA_SUCCESS )
3474 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003475 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003476
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003477 ASSERT_COMPARE( expected_output->x, expected_output->len,
3478 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003479 }
3480
Gilles Peskine50e586b2018-06-08 14:28:46 +02003481exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003482 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003483 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003484 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003485 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003486}
3487/* END_CASE */
3488
Gilles Peskine50e586b2018-06-08 14:28:46 +02003489/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003490void cipher_decrypt_fail( int alg_arg,
3491 int key_type_arg,
3492 data_t *key_data,
3493 data_t *iv,
3494 data_t *input_arg,
3495 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003496{
3497 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3498 psa_status_t status;
3499 psa_key_type_t key_type = key_type_arg;
3500 psa_algorithm_t alg = alg_arg;
3501 psa_status_t expected_status = expected_status_arg;
3502 unsigned char *input = NULL;
3503 size_t input_buffer_size = 0;
3504 unsigned char *output = NULL;
3505 size_t output_buffer_size = 0;
3506 size_t output_length = 0;
3507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3508
3509 if ( PSA_ERROR_BAD_STATE != expected_status )
3510 {
3511 PSA_ASSERT( psa_crypto_init( ) );
3512
3513 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3514 psa_set_key_algorithm( &attributes, alg );
3515 psa_set_key_type( &attributes, key_type );
3516
3517 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3518 &key ) );
3519 }
3520
3521 /* Allocate input buffer and copy the iv and the plaintext */
3522 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3523 if ( input_buffer_size > 0 )
3524 {
3525 ASSERT_ALLOC( input, input_buffer_size );
3526 memcpy( input, iv->x, iv->len );
3527 memcpy( input + iv->len, input_arg->x, input_arg->len );
3528 }
3529
3530 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3531 ASSERT_ALLOC( output, output_buffer_size );
3532
3533 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3534 output_buffer_size, &output_length );
3535 TEST_EQUAL( status, expected_status );
3536
3537exit:
3538 mbedtls_free( input );
3539 mbedtls_free( output );
3540 psa_destroy_key( key );
3541 PSA_DONE( );
3542}
3543/* END_CASE */
3544
3545/* BEGIN_CASE */
3546void cipher_decrypt( int alg_arg,
3547 int key_type_arg,
3548 data_t *key_data,
3549 data_t *iv,
3550 data_t *input_arg,
3551 data_t *expected_output )
3552{
3553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3554 psa_key_type_t key_type = key_type_arg;
3555 psa_algorithm_t alg = alg_arg;
3556 unsigned char *input = NULL;
3557 size_t input_buffer_size = 0;
3558 unsigned char *output = NULL;
3559 size_t output_buffer_size = 0;
3560 size_t output_length = 0;
3561 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3562
3563 PSA_ASSERT( psa_crypto_init( ) );
3564
3565 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3566 psa_set_key_algorithm( &attributes, alg );
3567 psa_set_key_type( &attributes, key_type );
3568
3569 /* Allocate input buffer and copy the iv and the plaintext */
3570 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3571 if ( input_buffer_size > 0 )
3572 {
3573 ASSERT_ALLOC( input, input_buffer_size );
3574 memcpy( input, iv->x, iv->len );
3575 memcpy( input + iv->len, input_arg->x, input_arg->len );
3576 }
3577
3578 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3579 ASSERT_ALLOC( output, output_buffer_size );
3580
3581 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3582 &key ) );
3583
3584 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3585 output_buffer_size, &output_length ) );
3586 TEST_ASSERT( output_length <=
3587 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3588 TEST_ASSERT( output_length <=
3589 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3590
3591 ASSERT_COMPARE( expected_output->x, expected_output->len,
3592 output, output_length );
3593exit:
3594 mbedtls_free( input );
3595 mbedtls_free( output );
3596 psa_destroy_key( key );
3597 PSA_DONE( );
3598}
3599/* END_CASE */
3600
3601/* BEGIN_CASE */
3602void cipher_verify_output( int alg_arg,
3603 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003604 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003605 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003606{
Ronald Cron5425a212020-08-04 14:58:35 +02003607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003608 psa_key_type_t key_type = key_type_arg;
3609 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003610 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003611 size_t output1_size = 0;
3612 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003613 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003614 size_t output2_size = 0;
3615 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003617
Gilles Peskine8817f612018-12-18 00:18:46 +01003618 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003619
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003620 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3621 psa_set_key_algorithm( &attributes, alg );
3622 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003623
Ronald Cron5425a212020-08-04 14:58:35 +02003624 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3625 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003626 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003627 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003628
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003629 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3630 output1, output1_size,
3631 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003632 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003633 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003634 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003635 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003636
3637 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003638 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003639
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003640 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3641 output2, output2_size,
3642 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003643 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003644 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003645 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003646 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003647
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003648 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003649
3650exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003651 mbedtls_free( output1 );
3652 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003653 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003654 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003655}
3656/* END_CASE */
3657
3658/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003659void cipher_verify_output_multipart( int alg_arg,
3660 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003661 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003662 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003663 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003664{
Ronald Cron5425a212020-08-04 14:58:35 +02003665 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003666 psa_key_type_t key_type = key_type_arg;
3667 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003668 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003669 unsigned char iv[16] = {0};
3670 size_t iv_size = 16;
3671 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003672 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003673 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003674 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003675 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003676 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003677 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003678 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003679 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3680 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003682
Gilles Peskine8817f612018-12-18 00:18:46 +01003683 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003684
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003685 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3686 psa_set_key_algorithm( &attributes, alg );
3687 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003688
Ronald Cron5425a212020-08-04 14:58:35 +02003689 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3690 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003691
Ronald Cron5425a212020-08-04 14:58:35 +02003692 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3693 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003694
Steven Cooreman177deba2020-09-07 17:14:14 +02003695 if( alg != PSA_ALG_ECB_NO_PADDING )
3696 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003697 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3698 iv, iv_size,
3699 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003700 }
3701
gabor-mezei-armceface22021-01-21 12:26:17 +01003702 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3703 TEST_ASSERT( output1_buffer_size <=
3704 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003705 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003706
Gilles Peskinee0866522019-02-19 19:44:00 +01003707 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003708
Gilles Peskine8817f612018-12-18 00:18:46 +01003709 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3710 output1, output1_buffer_size,
3711 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003712 TEST_ASSERT( function_output_length <=
3713 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3714 TEST_ASSERT( function_output_length <=
3715 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003716 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003717
Gilles Peskine8817f612018-12-18 00:18:46 +01003718 PSA_ASSERT( psa_cipher_update( &operation1,
3719 input->x + first_part_size,
3720 input->len - first_part_size,
3721 output1, output1_buffer_size,
3722 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003723 TEST_ASSERT( function_output_length <=
3724 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3725 alg,
3726 input->len - first_part_size ) );
3727 TEST_ASSERT( function_output_length <=
3728 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003729 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003730
Gilles Peskine8817f612018-12-18 00:18:46 +01003731 PSA_ASSERT( psa_cipher_finish( &operation1,
3732 output1 + output1_length,
3733 output1_buffer_size - output1_length,
3734 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003735 TEST_ASSERT( function_output_length <=
3736 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3737 TEST_ASSERT( function_output_length <=
3738 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003739 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003740
Gilles Peskine8817f612018-12-18 00:18:46 +01003741 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003742
Gilles Peskine048b7f02018-06-08 14:20:49 +02003743 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003744 TEST_ASSERT( output2_buffer_size <=
3745 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3746 TEST_ASSERT( output2_buffer_size <=
3747 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003748 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003749
Steven Cooreman177deba2020-09-07 17:14:14 +02003750 if( iv_length > 0 )
3751 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003752 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3753 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003754 }
Moran Pekerded84402018-06-06 16:36:50 +03003755
Gilles Peskine8817f612018-12-18 00:18:46 +01003756 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3757 output2, output2_buffer_size,
3758 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003759 TEST_ASSERT( function_output_length <=
3760 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3761 TEST_ASSERT( function_output_length <=
3762 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003763 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003764
Gilles Peskine8817f612018-12-18 00:18:46 +01003765 PSA_ASSERT( psa_cipher_update( &operation2,
3766 output1 + first_part_size,
3767 output1_length - first_part_size,
3768 output2, output2_buffer_size,
3769 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003770 TEST_ASSERT( function_output_length <=
3771 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3772 alg,
3773 output1_length - first_part_size ) );
3774 TEST_ASSERT( function_output_length <=
3775 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003776 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003777
Gilles Peskine8817f612018-12-18 00:18:46 +01003778 PSA_ASSERT( psa_cipher_finish( &operation2,
3779 output2 + output2_length,
3780 output2_buffer_size - output2_length,
3781 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003782 TEST_ASSERT( function_output_length <=
3783 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3784 TEST_ASSERT( function_output_length <=
3785 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003786 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003787
Gilles Peskine8817f612018-12-18 00:18:46 +01003788 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003789
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003790 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003791
3792exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003793 psa_cipher_abort( &operation1 );
3794 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003795 mbedtls_free( output1 );
3796 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003797 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003798 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003799}
3800/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003801
Gilles Peskine20035e32018-02-03 22:44:14 +01003802/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003803void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003804 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003805 data_t *nonce,
3806 data_t *additional_data,
3807 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003808 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003809{
Ronald Cron5425a212020-08-04 14:58:35 +02003810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003811 psa_key_type_t key_type = key_type_arg;
3812 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003813 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003814 unsigned char *output_data = NULL;
3815 size_t output_size = 0;
3816 size_t output_length = 0;
3817 unsigned char *output_data2 = NULL;
3818 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003819 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003820 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003822
Gilles Peskine8817f612018-12-18 00:18:46 +01003823 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003824
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003825 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3826 psa_set_key_algorithm( &attributes, alg );
3827 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003828
Gilles Peskine049c7532019-05-15 20:22:09 +02003829 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003830 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003831 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3832 key_bits = psa_get_key_bits( &attributes );
3833
3834 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3835 alg );
3836 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3837 * should be exact. */
3838 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3839 expected_result != PSA_ERROR_NOT_SUPPORTED )
3840 {
3841 TEST_EQUAL( output_size,
3842 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3843 TEST_ASSERT( output_size <=
3844 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3845 }
3846 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003847
Steven Cooremanf49478b2021-02-15 15:19:25 +01003848 status = psa_aead_encrypt( key, alg,
3849 nonce->x, nonce->len,
3850 additional_data->x,
3851 additional_data->len,
3852 input_data->x, input_data->len,
3853 output_data, output_size,
3854 &output_length );
3855
3856 /* If the operation is not supported, just skip and not fail in case the
3857 * encryption involves a common limitation of cryptography hardwares and
3858 * an alternative implementation. */
3859 if( status == PSA_ERROR_NOT_SUPPORTED )
3860 {
3861 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3862 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3863 }
3864
3865 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003866
3867 if( PSA_SUCCESS == expected_result )
3868 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003869 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003870
Gilles Peskine003a4a92019-05-14 16:09:40 +02003871 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3872 * should be exact. */
3873 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003874 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003875
gabor-mezei-armceface22021-01-21 12:26:17 +01003876 TEST_ASSERT( input_data->len <=
3877 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3878
Ronald Cron5425a212020-08-04 14:58:35 +02003879 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003880 nonce->x, nonce->len,
3881 additional_data->x,
3882 additional_data->len,
3883 output_data, output_length,
3884 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003885 &output_length2 ),
3886 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003887
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003888 ASSERT_COMPARE( input_data->x, input_data->len,
3889 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003890 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003891
Gilles Peskinea1cac842018-06-11 19:33:02 +02003892exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003893 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003894 mbedtls_free( output_data );
3895 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003896 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003897}
3898/* END_CASE */
3899
3900/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003901void aead_encrypt( int key_type_arg, data_t *key_data,
3902 int alg_arg,
3903 data_t *nonce,
3904 data_t *additional_data,
3905 data_t *input_data,
3906 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003907{
Ronald Cron5425a212020-08-04 14:58:35 +02003908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003909 psa_key_type_t key_type = key_type_arg;
3910 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003911 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003912 unsigned char *output_data = NULL;
3913 size_t output_size = 0;
3914 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003916 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003917
Gilles Peskine8817f612018-12-18 00:18:46 +01003918 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003919
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003920 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3921 psa_set_key_algorithm( &attributes, alg );
3922 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003923
Gilles Peskine049c7532019-05-15 20:22:09 +02003924 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003925 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003926 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3927 key_bits = psa_get_key_bits( &attributes );
3928
3929 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3930 alg );
3931 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3932 * should be exact. */
3933 TEST_EQUAL( output_size,
3934 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3935 TEST_ASSERT( output_size <=
3936 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3937 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003938
Steven Cooremand588ea12021-01-11 19:36:04 +01003939 status = psa_aead_encrypt( key, alg,
3940 nonce->x, nonce->len,
3941 additional_data->x, additional_data->len,
3942 input_data->x, input_data->len,
3943 output_data, output_size,
3944 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003945
Ronald Cron28a45ed2021-02-09 20:35:42 +01003946 /* 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 )
Steven Cooremand588ea12021-01-11 19:36:04 +01003950 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003951 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 );
Steven Cooremand588ea12021-01-11 19:36:04 +01003953 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003954
3955 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003956 ASSERT_COMPARE( expected_result->x, expected_result->len,
3957 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003958
Gilles Peskinea1cac842018-06-11 19:33:02 +02003959exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003960 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003961 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003962 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003963}
3964/* END_CASE */
3965
3966/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003967void aead_decrypt( int key_type_arg, data_t *key_data,
3968 int alg_arg,
3969 data_t *nonce,
3970 data_t *additional_data,
3971 data_t *input_data,
3972 data_t *expected_data,
3973 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003974{
Ronald Cron5425a212020-08-04 14:58:35 +02003975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003976 psa_key_type_t key_type = key_type_arg;
3977 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003978 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003979 unsigned char *output_data = NULL;
3980 size_t output_size = 0;
3981 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003983 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003984 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003985
Gilles Peskine8817f612018-12-18 00:18:46 +01003986 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003987
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003988 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3989 psa_set_key_algorithm( &attributes, alg );
3990 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003991
Gilles Peskine049c7532019-05-15 20:22:09 +02003992 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003993 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003994 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3995 key_bits = psa_get_key_bits( &attributes );
3996
3997 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3998 alg );
3999 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4000 expected_result != PSA_ERROR_NOT_SUPPORTED )
4001 {
4002 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4003 * should be exact. */
4004 TEST_EQUAL( output_size,
4005 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4006 TEST_ASSERT( output_size <=
4007 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4008 }
4009 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004010
Steven Cooremand588ea12021-01-11 19:36:04 +01004011 status = psa_aead_decrypt( key, alg,
4012 nonce->x, nonce->len,
4013 additional_data->x,
4014 additional_data->len,
4015 input_data->x, input_data->len,
4016 output_data, output_size,
4017 &output_length );
4018
Ronald Cron28a45ed2021-02-09 20:35:42 +01004019 /* If the operation is not supported, just skip and not fail in case the
4020 * decryption involves a common limitation of cryptography hardwares and
4021 * an alternative implementation. */
4022 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004023 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004024 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4025 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004026 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004027
4028 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004029
Gilles Peskine2d277862018-06-18 15:41:12 +02004030 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004031 ASSERT_COMPARE( expected_data->x, expected_data->len,
4032 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004033
Gilles Peskinea1cac842018-06-11 19:33:02 +02004034exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004035 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004036 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004037 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004038}
4039/* END_CASE */
4040
4041/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004042void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4043 int alg_arg,
4044 data_t *nonce,
4045 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004046 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004047 int do_set_lengths,
4048 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004049{
Paul Elliottd3f82412021-06-16 16:52:21 +01004050 size_t ad_part_len = 0;
4051 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004052 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004053
Paul Elliott32f46ba2021-09-23 18:24:36 +01004054 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004055 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004056 mbedtls_test_set_step( ad_part_len );
4057
4058 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004059 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004060 if( ad_part_len & 0x01 )
4061 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4062 else
4063 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004064 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004065
4066 /* Split ad into length(ad_part_len) parts. */
4067 if( !aead_multipart_internal_func( key_type_arg, key_data,
4068 alg_arg, nonce,
4069 additional_data,
4070 ad_part_len,
4071 input_data, -1,
4072 set_lengths_method,
4073 expected_output,
4074 1, 0 ) )
4075 break;
4076
4077 /* length(0) part, length(ad_part_len) part, length(0) part... */
4078 mbedtls_test_set_step( 1000 + ad_part_len );
4079
4080 if( !aead_multipart_internal_func( key_type_arg, key_data,
4081 alg_arg, nonce,
4082 additional_data,
4083 ad_part_len,
4084 input_data, -1,
4085 set_lengths_method,
4086 expected_output,
4087 1, 1 ) )
4088 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004089 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004090
Paul Elliott32f46ba2021-09-23 18:24:36 +01004091 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004092 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004093 /* Split data into length(data_part_len) parts. */
4094 mbedtls_test_set_step( 2000 + data_part_len );
4095
4096 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004097 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004098 if( data_part_len & 0x01 )
4099 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4100 else
4101 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004102 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004103
Paul Elliott32f46ba2021-09-23 18:24:36 +01004104 if( !aead_multipart_internal_func( key_type_arg, key_data,
4105 alg_arg, nonce,
4106 additional_data, -1,
4107 input_data, data_part_len,
4108 set_lengths_method,
4109 expected_output,
4110 1, 0 ) )
4111 break;
4112
4113 /* length(0) part, length(data_part_len) part, length(0) part... */
4114 mbedtls_test_set_step( 3000 + data_part_len );
4115
4116 if( !aead_multipart_internal_func( key_type_arg, key_data,
4117 alg_arg, nonce,
4118 additional_data, -1,
4119 input_data, data_part_len,
4120 set_lengths_method,
4121 expected_output,
4122 1, 1 ) )
4123 break;
4124 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004125
Paul Elliott8fc45162021-06-23 16:06:01 +01004126 /* Goto is required to silence warnings about unused labels, as we
4127 * don't actually do any test assertions in this function. */
4128 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004129}
4130/* END_CASE */
4131
4132/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004133void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4134 int alg_arg,
4135 data_t *nonce,
4136 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004137 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004138 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004139 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004140{
Paul Elliottd3f82412021-06-16 16:52:21 +01004141 size_t ad_part_len = 0;
4142 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004143 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004144
Paul Elliott32f46ba2021-09-23 18:24:36 +01004145 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004146 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004147 /* Split ad into length(ad_part_len) parts. */
4148 mbedtls_test_set_step( ad_part_len );
4149
4150 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004151 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004152 if( ad_part_len & 0x01 )
4153 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4154 else
4155 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004156 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004157
4158 if( !aead_multipart_internal_func( key_type_arg, key_data,
4159 alg_arg, nonce,
4160 additional_data,
4161 ad_part_len,
4162 input_data, -1,
4163 set_lengths_method,
4164 expected_output,
4165 0, 0 ) )
4166 break;
4167
4168 /* length(0) part, length(ad_part_len) part, length(0) part... */
4169 mbedtls_test_set_step( 1000 + ad_part_len );
4170
4171 if( !aead_multipart_internal_func( key_type_arg, key_data,
4172 alg_arg, nonce,
4173 additional_data,
4174 ad_part_len,
4175 input_data, -1,
4176 set_lengths_method,
4177 expected_output,
4178 0, 1 ) )
4179 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004180 }
4181
Paul Elliott32f46ba2021-09-23 18:24:36 +01004182 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004183 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004184 /* Split data into length(data_part_len) parts. */
4185 mbedtls_test_set_step( 2000 + data_part_len );
4186
4187 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004188 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004189 if( data_part_len & 0x01 )
4190 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4191 else
4192 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004193 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004194
4195 if( !aead_multipart_internal_func( key_type_arg, key_data,
4196 alg_arg, nonce,
4197 additional_data, -1,
4198 input_data, data_part_len,
4199 set_lengths_method,
4200 expected_output,
4201 0, 0 ) )
4202 break;
4203
4204 /* length(0) part, length(data_part_len) part, length(0) part... */
4205 mbedtls_test_set_step( 3000 + data_part_len );
4206
4207 if( !aead_multipart_internal_func( key_type_arg, key_data,
4208 alg_arg, nonce,
4209 additional_data, -1,
4210 input_data, data_part_len,
4211 set_lengths_method,
4212 expected_output,
4213 0, 1 ) )
4214 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004215 }
4216
Paul Elliott8fc45162021-06-23 16:06:01 +01004217 /* Goto is required to silence warnings about unused labels, as we
4218 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004219 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004220}
4221/* END_CASE */
4222
4223/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004224void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4225 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004226 int nonce_length,
4227 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004228 data_t *additional_data,
4229 data_t *input_data,
4230 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004231{
4232
4233 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4234 psa_key_type_t key_type = key_type_arg;
4235 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004236 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004237 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4239 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004240 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004241 size_t actual_nonce_length = 0;
4242 size_t expected_nonce_length = expected_nonce_length_arg;
4243 unsigned char *output = NULL;
4244 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004245 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004246 size_t ciphertext_size = 0;
4247 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004248 size_t tag_length = 0;
4249 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004250
4251 PSA_ASSERT( psa_crypto_init( ) );
4252
4253 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4254 psa_set_key_algorithm( & attributes, alg );
4255 psa_set_key_type( & attributes, key_type );
4256
4257 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4258 &key ) );
4259
4260 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4261
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004262 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4263
Paul Elliottf1277632021-08-24 18:11:37 +01004264 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004265
Paul Elliottf1277632021-08-24 18:11:37 +01004266 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004267
Paul Elliottf1277632021-08-24 18:11:37 +01004268 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004269
Paul Elliottf1277632021-08-24 18:11:37 +01004270 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004271
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004272 status = psa_aead_encrypt_setup( &operation, key, alg );
4273
4274 /* If the operation is not supported, just skip and not fail in case the
4275 * encryption involves a common limitation of cryptography hardwares and
4276 * an alternative implementation. */
4277 if( status == PSA_ERROR_NOT_SUPPORTED )
4278 {
4279 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004280 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004281 }
4282
4283 PSA_ASSERT( status );
4284
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004285 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004286 nonce_length,
4287 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004288
Paul Elliott693bf312021-07-23 17:40:41 +01004289 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004290
Paul Elliottf1277632021-08-24 18:11:37 +01004291 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004292
Paul Elliott88ecbe12021-09-22 17:23:03 +01004293 if( expected_status == PSA_SUCCESS )
4294 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4295 alg ) );
4296
Paul Elliottd79c5c52021-10-06 21:49:41 +01004297 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004298
Paul Elliott693bf312021-07-23 17:40:41 +01004299 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004300 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004301 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004302 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4303 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004304
4305 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4306 additional_data->len ) );
4307
4308 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004309 output, output_size,
4310 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004311
Paul Elliottf1277632021-08-24 18:11:37 +01004312 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4313 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004314 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4315 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004316
4317exit:
4318 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004319 mbedtls_free( output );
4320 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004321 psa_aead_abort( &operation );
4322 PSA_DONE( );
4323}
4324/* END_CASE */
4325
4326/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004327void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4328 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004329 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004330 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004331 data_t *additional_data,
4332 data_t *input_data,
4333 int expected_status_arg )
4334{
4335
4336 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4337 psa_key_type_t key_type = key_type_arg;
4338 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004339 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004340 uint8_t *nonce_buffer = NULL;
4341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4342 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4343 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004344 unsigned char *output = NULL;
4345 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004346 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004347 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004348 size_t ciphertext_size = 0;
4349 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004350 size_t tag_length = 0;
4351 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004352 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004353 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004354
4355 PSA_ASSERT( psa_crypto_init( ) );
4356
4357 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4358 psa_set_key_algorithm( &attributes, alg );
4359 psa_set_key_type( &attributes, key_type );
4360
4361 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4362 &key ) );
4363
4364 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4365
4366 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4367
Paul Elliott6f0e7202021-08-25 12:57:18 +01004368 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004369
Paul Elliott6f0e7202021-08-25 12:57:18 +01004370 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004371
Paul Elliott6f0e7202021-08-25 12:57:18 +01004372 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004373
Paul Elliott6f0e7202021-08-25 12:57:18 +01004374 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004375
Paul Elliott863864a2021-07-23 17:28:31 +01004376 status = psa_aead_encrypt_setup( &operation, key, alg );
4377
4378 /* If the operation is not supported, just skip and not fail in case the
4379 * encryption involves a common limitation of cryptography hardwares and
4380 * an alternative implementation. */
4381 if( status == PSA_ERROR_NOT_SUPPORTED )
4382 {
4383 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004384 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004385 }
4386
4387 PSA_ASSERT( status );
4388
Paul Elliott4023ffd2021-09-10 16:21:22 +01004389 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4390 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004391 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004392 /* Arbitrary size buffer, to test zero length valid buffer. */
4393 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004394 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004395 }
4396 else
4397 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004398 /* If length is zero, then this will return NULL. */
4399 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004400 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004401
Paul Elliott4023ffd2021-09-10 16:21:22 +01004402 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004403 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004404 for( index = 0; index < nonce_length - 1; ++index )
4405 {
4406 nonce_buffer[index] = 'a' + index;
4407 }
Paul Elliott66696b52021-08-16 18:42:41 +01004408 }
Paul Elliott863864a2021-07-23 17:28:31 +01004409 }
4410
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004411 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4412 {
4413 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4414 input_data->len ) );
4415 }
4416
Paul Elliott6f0e7202021-08-25 12:57:18 +01004417 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004418
Paul Elliott693bf312021-07-23 17:40:41 +01004419 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004420
4421 if( expected_status == PSA_SUCCESS )
4422 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004423 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4424 {
4425 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4426 input_data->len ) );
4427 }
4428 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4429 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004430
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004431 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4432 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4433 additional_data->len ),
4434 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004435
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004436 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004437 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004438 &ciphertext_length ),
4439 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004440
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004441 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004442 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004443 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4444 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004445 }
4446
4447exit:
4448 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004449 mbedtls_free( output );
4450 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004451 mbedtls_free( nonce_buffer );
4452 psa_aead_abort( &operation );
4453 PSA_DONE( );
4454}
4455/* END_CASE */
4456
4457/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004458void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4459 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004460 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004461 data_t *nonce,
4462 data_t *additional_data,
4463 data_t *input_data,
4464 int expected_status_arg )
4465{
4466
4467 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4468 psa_key_type_t key_type = key_type_arg;
4469 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004470 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4472 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4473 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004474 unsigned char *output = NULL;
4475 unsigned char *ciphertext = NULL;
4476 size_t output_size = output_size_arg;
4477 size_t ciphertext_size = 0;
4478 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004479 size_t tag_length = 0;
4480 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4481
4482 PSA_ASSERT( psa_crypto_init( ) );
4483
4484 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4485 psa_set_key_algorithm( &attributes, alg );
4486 psa_set_key_type( &attributes, key_type );
4487
4488 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4489 &key ) );
4490
4491 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4492
Paul Elliottc6d11d02021-09-01 12:04:23 +01004493 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004494
Paul Elliottc6d11d02021-09-01 12:04:23 +01004495 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004496
Paul Elliottc6d11d02021-09-01 12:04:23 +01004497 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004498
Paul Elliott43fbda62021-07-23 18:30:59 +01004499 status = psa_aead_encrypt_setup( &operation, key, alg );
4500
4501 /* If the operation is not supported, just skip and not fail in case the
4502 * encryption involves a common limitation of cryptography hardwares and
4503 * an alternative implementation. */
4504 if( status == PSA_ERROR_NOT_SUPPORTED )
4505 {
4506 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4507 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4508 }
4509
4510 PSA_ASSERT( status );
4511
Paul Elliott47b9a142021-10-07 15:04:57 +01004512 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4513 input_data->len ) );
4514
Paul Elliott43fbda62021-07-23 18:30:59 +01004515 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4516
4517 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4518 additional_data->len ) );
4519
4520 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004521 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004522
4523 TEST_EQUAL( status, expected_status );
4524
4525 if( expected_status == PSA_SUCCESS )
4526 {
4527 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004528 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4529 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004530 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4531 }
4532
4533exit:
4534 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004535 mbedtls_free( output );
4536 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004537 psa_aead_abort( &operation );
4538 PSA_DONE( );
4539}
4540/* END_CASE */
4541
Paul Elliott91b021e2021-07-23 18:52:31 +01004542/* BEGIN_CASE */
4543void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4544 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004545 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004546 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004547 data_t *nonce,
4548 data_t *additional_data,
4549 data_t *input_data,
4550 int expected_status_arg )
4551{
4552
4553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4554 psa_key_type_t key_type = key_type_arg;
4555 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004556 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4558 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4559 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004560 unsigned char *ciphertext = NULL;
4561 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004562 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004563 size_t ciphertext_size = 0;
4564 size_t ciphertext_length = 0;
4565 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004566 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004567 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004568
4569 PSA_ASSERT( psa_crypto_init( ) );
4570
4571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4572 psa_set_key_algorithm( &attributes, alg );
4573 psa_set_key_type( &attributes, key_type );
4574
4575 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4576 &key ) );
4577
4578 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4579
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004580 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004581
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004582 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004583
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004584 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004585
Paul Elliott719c1322021-09-13 18:27:22 +01004586 ASSERT_ALLOC( tag_buffer, tag_size );
4587
Paul Elliott91b021e2021-07-23 18:52:31 +01004588 status = psa_aead_encrypt_setup( &operation, key, alg );
4589
4590 /* If the operation is not supported, just skip and not fail in case the
4591 * encryption involves a common limitation of cryptography hardwares and
4592 * an alternative implementation. */
4593 if( status == PSA_ERROR_NOT_SUPPORTED )
4594 {
4595 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4596 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4597 }
4598
4599 PSA_ASSERT( status );
4600
4601 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4602
Paul Elliott76bda482021-10-07 17:07:23 +01004603 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4604 input_data->len ) );
4605
Paul Elliott91b021e2021-07-23 18:52:31 +01004606 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4607 additional_data->len ) );
4608
4609 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004610 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004611
4612 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004613 status = psa_aead_finish( &operation, finish_ciphertext,
4614 finish_ciphertext_size,
4615 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004616 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004617
4618 TEST_EQUAL( status, expected_status );
4619
4620exit:
4621 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004622 mbedtls_free( ciphertext );
4623 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004624 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004625 psa_aead_abort( &operation );
4626 PSA_DONE( );
4627}
4628/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004629
4630/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004631void aead_multipart_verify( int key_type_arg, data_t *key_data,
4632 int alg_arg,
4633 data_t *nonce,
4634 data_t *additional_data,
4635 data_t *input_data,
4636 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004637 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004638 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004639 int expected_status_arg )
4640{
4641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4642 psa_key_type_t key_type = key_type_arg;
4643 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004644 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4646 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4647 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004648 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004649 unsigned char *plaintext = NULL;
4650 unsigned char *finish_plaintext = NULL;
4651 size_t plaintext_size = 0;
4652 size_t plaintext_length = 0;
4653 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004654 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004655 unsigned char *tag_buffer = NULL;
4656 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004657
4658 PSA_ASSERT( psa_crypto_init( ) );
4659
4660 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4661 psa_set_key_algorithm( &attributes, alg );
4662 psa_set_key_type( &attributes, key_type );
4663
4664 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4665 &key ) );
4666
4667 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4668
4669 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4670 input_data->len );
4671
4672 ASSERT_ALLOC( plaintext, plaintext_size );
4673
4674 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4675
4676 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4677
Paul Elliott9961a662021-09-17 19:19:02 +01004678 status = psa_aead_decrypt_setup( &operation, key, alg );
4679
4680 /* If the operation is not supported, just skip and not fail in case the
4681 * encryption involves a common limitation of cryptography hardwares and
4682 * an alternative implementation. */
4683 if( status == PSA_ERROR_NOT_SUPPORTED )
4684 {
4685 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4686 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4687 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004688 TEST_EQUAL( status, expected_setup_status );
4689
4690 if( status != PSA_SUCCESS )
4691 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004692
4693 PSA_ASSERT( status );
4694
4695 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4696
Paul Elliottfec6f372021-10-06 17:15:02 +01004697 status = psa_aead_set_lengths( &operation, additional_data->len,
4698 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004699 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004700
Paul Elliott9961a662021-09-17 19:19:02 +01004701 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4702 additional_data->len ) );
4703
4704 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4705 input_data->len,
4706 plaintext, plaintext_size,
4707 &plaintext_length ) );
4708
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004709 if( tag_usage == USE_GIVEN_TAG )
4710 {
4711 tag_buffer = tag->x;
4712 tag_size = tag->len;
4713 }
4714
Paul Elliott9961a662021-09-17 19:19:02 +01004715 status = psa_aead_verify( &operation, finish_plaintext,
4716 verify_plaintext_size,
4717 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004718 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004719
4720 TEST_EQUAL( status, expected_status );
4721
4722exit:
4723 psa_destroy_key( key );
4724 mbedtls_free( plaintext );
4725 mbedtls_free( finish_plaintext );
4726 psa_aead_abort( &operation );
4727 PSA_DONE( );
4728}
4729/* END_CASE */
4730
Paul Elliott9961a662021-09-17 19:19:02 +01004731/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004732void aead_multipart_setup( int key_type_arg, data_t *key_data,
4733 int alg_arg, int expected_status_arg )
4734{
4735 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4736 psa_key_type_t key_type = key_type_arg;
4737 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004738 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4740 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4741 psa_status_t expected_status = expected_status_arg;
4742
4743 PSA_ASSERT( psa_crypto_init( ) );
4744
4745 psa_set_key_usage_flags( &attributes,
4746 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4747 psa_set_key_algorithm( &attributes, alg );
4748 psa_set_key_type( &attributes, key_type );
4749
4750 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4751 &key ) );
4752
Paul Elliott5221ef62021-09-19 17:33:03 +01004753 status = psa_aead_encrypt_setup( &operation, key, alg );
4754
4755 TEST_EQUAL( status, expected_status );
4756
4757 psa_aead_abort( &operation );
4758
Paul Elliott5221ef62021-09-19 17:33:03 +01004759 status = psa_aead_decrypt_setup( &operation, key, alg );
4760
4761 TEST_EQUAL(status, expected_status );
4762
4763exit:
4764 psa_destroy_key( key );
4765 psa_aead_abort( &operation );
4766 PSA_DONE( );
4767}
4768/* END_CASE */
4769
4770/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004771void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4772 int alg_arg,
4773 data_t *nonce,
4774 data_t *additional_data,
4775 data_t *input_data )
4776{
4777 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4778 psa_key_type_t key_type = key_type_arg;
4779 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004780 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004781 unsigned char *output_data = NULL;
4782 unsigned char *final_data = NULL;
4783 size_t output_size = 0;
4784 size_t finish_output_size = 0;
4785 size_t output_length = 0;
4786 size_t key_bits = 0;
4787 size_t tag_length = 0;
4788 size_t tag_size = 0;
4789 size_t nonce_length = 0;
4790 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4791 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4792 size_t output_part_length = 0;
4793 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4794
4795 PSA_ASSERT( psa_crypto_init( ) );
4796
4797 psa_set_key_usage_flags( & attributes,
4798 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4799 psa_set_key_algorithm( & attributes, alg );
4800 psa_set_key_type( & attributes, key_type );
4801
4802 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4803 &key ) );
4804
4805 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4806 key_bits = psa_get_key_bits( &attributes );
4807
4808 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4809
4810 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4811
4812 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4813
4814 ASSERT_ALLOC( output_data, output_size );
4815
4816 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4817
4818 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4819
4820 ASSERT_ALLOC( final_data, finish_output_size );
4821
4822 /* Test all operations error without calling setup first. */
4823
Paul Elliottc23a9a02021-06-21 18:32:46 +01004824 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4825 PSA_ERROR_BAD_STATE );
4826
4827 psa_aead_abort( &operation );
4828
Paul Elliottc23a9a02021-06-21 18:32:46 +01004829 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4830 PSA_AEAD_NONCE_MAX_SIZE,
4831 &nonce_length ),
4832 PSA_ERROR_BAD_STATE );
4833
4834 psa_aead_abort( &operation );
4835
Paul Elliott481be342021-07-16 17:38:47 +01004836 /* ------------------------------------------------------- */
4837
Paul Elliottc23a9a02021-06-21 18:32:46 +01004838 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4839 input_data->len ),
4840 PSA_ERROR_BAD_STATE );
4841
4842 psa_aead_abort( &operation );
4843
Paul Elliott481be342021-07-16 17:38:47 +01004844 /* ------------------------------------------------------- */
4845
Paul Elliottc23a9a02021-06-21 18:32:46 +01004846 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4847 additional_data->len ),
4848 PSA_ERROR_BAD_STATE );
4849
4850 psa_aead_abort( &operation );
4851
Paul Elliott481be342021-07-16 17:38:47 +01004852 /* ------------------------------------------------------- */
4853
Paul Elliottc23a9a02021-06-21 18:32:46 +01004854 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4855 input_data->len, output_data,
4856 output_size, &output_length ),
4857 PSA_ERROR_BAD_STATE );
4858
4859 psa_aead_abort( &operation );
4860
Paul Elliott481be342021-07-16 17:38:47 +01004861 /* ------------------------------------------------------- */
4862
Paul Elliottc23a9a02021-06-21 18:32:46 +01004863 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4864 finish_output_size,
4865 &output_part_length,
4866 tag_buffer, tag_length,
4867 &tag_size ),
4868 PSA_ERROR_BAD_STATE );
4869
4870 psa_aead_abort( &operation );
4871
Paul Elliott481be342021-07-16 17:38:47 +01004872 /* ------------------------------------------------------- */
4873
Paul Elliottc23a9a02021-06-21 18:32:46 +01004874 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4875 finish_output_size,
4876 &output_part_length,
4877 tag_buffer,
4878 tag_length ),
4879 PSA_ERROR_BAD_STATE );
4880
4881 psa_aead_abort( &operation );
4882
4883 /* Test for double setups. */
4884
Paul Elliottc23a9a02021-06-21 18:32:46 +01004885 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4886
4887 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4888 PSA_ERROR_BAD_STATE );
4889
4890 psa_aead_abort( &operation );
4891
Paul Elliott481be342021-07-16 17:38:47 +01004892 /* ------------------------------------------------------- */
4893
Paul Elliottc23a9a02021-06-21 18:32:46 +01004894 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4895
4896 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4897 PSA_ERROR_BAD_STATE );
4898
4899 psa_aead_abort( &operation );
4900
Paul Elliott374a2be2021-07-16 17:53:40 +01004901 /* ------------------------------------------------------- */
4902
Paul Elliott374a2be2021-07-16 17:53:40 +01004903 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4904
4905 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4906 PSA_ERROR_BAD_STATE );
4907
4908 psa_aead_abort( &operation );
4909
4910 /* ------------------------------------------------------- */
4911
Paul Elliott374a2be2021-07-16 17:53:40 +01004912 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4913
4914 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4915 PSA_ERROR_BAD_STATE );
4916
4917 psa_aead_abort( &operation );
4918
Paul Elliottc23a9a02021-06-21 18:32:46 +01004919 /* Test for not setting a nonce. */
4920
Paul Elliottc23a9a02021-06-21 18:32:46 +01004921 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4922
4923 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4924 additional_data->len ),
4925 PSA_ERROR_BAD_STATE );
4926
4927 psa_aead_abort( &operation );
4928
Paul Elliott7f628422021-09-01 12:08:29 +01004929 /* ------------------------------------------------------- */
4930
Paul Elliott7f628422021-09-01 12:08:29 +01004931 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4932
4933 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4934 input_data->len, output_data,
4935 output_size, &output_length ),
4936 PSA_ERROR_BAD_STATE );
4937
4938 psa_aead_abort( &operation );
4939
Paul Elliottbdc2c682021-09-21 18:37:10 +01004940 /* ------------------------------------------------------- */
4941
Paul Elliottbdc2c682021-09-21 18:37:10 +01004942 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4943
4944 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4945 finish_output_size,
4946 &output_part_length,
4947 tag_buffer, tag_length,
4948 &tag_size ),
4949 PSA_ERROR_BAD_STATE );
4950
4951 psa_aead_abort( &operation );
4952
4953 /* ------------------------------------------------------- */
4954
Paul Elliottbdc2c682021-09-21 18:37:10 +01004955 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4956
4957 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4958 finish_output_size,
4959 &output_part_length,
4960 tag_buffer,
4961 tag_length ),
4962 PSA_ERROR_BAD_STATE );
4963
4964 psa_aead_abort( &operation );
4965
Paul Elliottc23a9a02021-06-21 18:32:46 +01004966 /* Test for double setting nonce. */
4967
Paul Elliottc23a9a02021-06-21 18:32:46 +01004968 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4969
4970 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4971
4972 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4973 PSA_ERROR_BAD_STATE );
4974
4975 psa_aead_abort( &operation );
4976
Paul Elliott374a2be2021-07-16 17:53:40 +01004977 /* Test for double generating nonce. */
4978
Paul Elliott374a2be2021-07-16 17:53:40 +01004979 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4980
4981 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4982 PSA_AEAD_NONCE_MAX_SIZE,
4983 &nonce_length ) );
4984
4985 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4986 PSA_AEAD_NONCE_MAX_SIZE,
4987 &nonce_length ),
4988 PSA_ERROR_BAD_STATE );
4989
4990
4991 psa_aead_abort( &operation );
4992
4993 /* Test for generate nonce then set and vice versa */
4994
Paul Elliott374a2be2021-07-16 17:53:40 +01004995 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4996
4997 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4998 PSA_AEAD_NONCE_MAX_SIZE,
4999 &nonce_length ) );
5000
5001 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5002 PSA_ERROR_BAD_STATE );
5003
5004 psa_aead_abort( &operation );
5005
Andrzej Kurekad837522021-12-15 15:28:49 +01005006 /* Test for generating nonce after calling set lengths */
5007
5008 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5009
5010 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5011 input_data->len ) );
5012
5013 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5014 PSA_AEAD_NONCE_MAX_SIZE,
5015 &nonce_length ) );
5016
5017 psa_aead_abort( &operation );
5018
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005019 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005020
5021 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5022
5023 if( operation.alg == PSA_ALG_CCM )
5024 {
5025 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5026 input_data->len ),
5027 PSA_ERROR_INVALID_ARGUMENT );
5028 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5029 PSA_AEAD_NONCE_MAX_SIZE,
5030 &nonce_length ),
5031 PSA_ERROR_BAD_STATE );
5032 }
5033 else
5034 {
5035 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5036 input_data->len ) );
5037 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5038 PSA_AEAD_NONCE_MAX_SIZE,
5039 &nonce_length ) );
5040 }
5041
5042 psa_aead_abort( &operation );
5043
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005044 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005045#if SIZE_MAX > UINT32_MAX
5046 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5047
5048 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5049 {
5050 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5051 input_data->len ),
5052 PSA_ERROR_INVALID_ARGUMENT );
5053 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5054 PSA_AEAD_NONCE_MAX_SIZE,
5055 &nonce_length ),
5056 PSA_ERROR_BAD_STATE );
5057 }
5058 else
5059 {
5060 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5061 input_data->len ) );
5062 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5063 PSA_AEAD_NONCE_MAX_SIZE,
5064 &nonce_length ) );
5065 }
5066
5067 psa_aead_abort( &operation );
5068#endif
5069
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005070 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005071
5072 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5073
5074 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5075 PSA_AEAD_NONCE_MAX_SIZE,
5076 &nonce_length ) );
5077
5078 if( operation.alg == PSA_ALG_CCM )
5079 {
5080 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5081 input_data->len ),
5082 PSA_ERROR_INVALID_ARGUMENT );
5083 }
5084 else
5085 {
5086 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5087 input_data->len ) );
5088 }
5089
5090 psa_aead_abort( &operation );
5091
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005092 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005093 /* Test for setting nonce after calling set lengths */
5094
5095 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5096
5097 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5098 input_data->len ) );
5099
5100 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5101
5102 psa_aead_abort( &operation );
5103
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005104 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005105
5106 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5107
5108 if( operation.alg == PSA_ALG_CCM )
5109 {
5110 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5111 input_data->len ),
5112 PSA_ERROR_INVALID_ARGUMENT );
5113 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5114 PSA_ERROR_BAD_STATE );
5115 }
5116 else
5117 {
5118 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5119 input_data->len ) );
5120 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5121 }
5122
5123 psa_aead_abort( &operation );
5124
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005125 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005126#if SIZE_MAX > UINT32_MAX
5127 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5128
5129 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5130 {
5131 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5132 input_data->len ),
5133 PSA_ERROR_INVALID_ARGUMENT );
5134 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5135 PSA_ERROR_BAD_STATE );
5136 }
5137 else
5138 {
5139 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5140 input_data->len ) );
5141 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5142 }
5143
5144 psa_aead_abort( &operation );
5145#endif
5146
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005147 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005148
5149 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5150
5151 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5152
5153 if( operation.alg == PSA_ALG_CCM )
5154 {
5155 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5156 input_data->len ),
5157 PSA_ERROR_INVALID_ARGUMENT );
5158 }
5159 else
5160 {
5161 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5162 input_data->len ) );
5163 }
5164
5165 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005166
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005167 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005168#if SIZE_MAX > UINT32_MAX
5169 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5170
5171 if( operation.alg == PSA_ALG_GCM )
5172 {
5173 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5174 SIZE_MAX ),
5175 PSA_ERROR_INVALID_ARGUMENT );
5176 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5177 PSA_ERROR_BAD_STATE );
5178 }
5179 else if ( operation.alg != PSA_ALG_CCM )
5180 {
5181 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5182 SIZE_MAX ) );
5183 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5184 }
5185
5186 psa_aead_abort( &operation );
5187
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005188 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005189 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5190
5191 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5192
5193 if( operation.alg == PSA_ALG_GCM )
5194 {
5195 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5196 SIZE_MAX ),
5197 PSA_ERROR_INVALID_ARGUMENT );
5198 }
5199 else if ( operation.alg != PSA_ALG_CCM )
5200 {
5201 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5202 SIZE_MAX ) );
5203 }
5204
5205 psa_aead_abort( &operation );
5206#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005207
5208 /* ------------------------------------------------------- */
5209
Paul Elliott374a2be2021-07-16 17:53:40 +01005210 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5211
5212 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5213
5214 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5215 PSA_AEAD_NONCE_MAX_SIZE,
5216 &nonce_length ),
5217 PSA_ERROR_BAD_STATE );
5218
5219 psa_aead_abort( &operation );
5220
Paul Elliott7220cae2021-06-22 17:25:57 +01005221 /* Test for generating nonce in decrypt setup. */
5222
Paul Elliott7220cae2021-06-22 17:25:57 +01005223 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5224
5225 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5226 PSA_AEAD_NONCE_MAX_SIZE,
5227 &nonce_length ),
5228 PSA_ERROR_BAD_STATE );
5229
5230 psa_aead_abort( &operation );
5231
Paul Elliottc23a9a02021-06-21 18:32:46 +01005232 /* Test for setting lengths twice. */
5233
Paul Elliottc23a9a02021-06-21 18:32:46 +01005234 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5235
5236 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5237
5238 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5239 input_data->len ) );
5240
5241 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5242 input_data->len ),
5243 PSA_ERROR_BAD_STATE );
5244
5245 psa_aead_abort( &operation );
5246
Andrzej Kurekad837522021-12-15 15:28:49 +01005247 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005248
Paul Elliottc23a9a02021-06-21 18:32:46 +01005249 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5250
5251 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5252
Andrzej Kurekad837522021-12-15 15:28:49 +01005253 if( operation.alg == PSA_ALG_CCM )
5254 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005255
Andrzej Kurekad837522021-12-15 15:28:49 +01005256 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5257 additional_data->len ),
5258 PSA_ERROR_BAD_STATE );
5259 }
5260 else
5261 {
5262 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5263 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005264
Andrzej Kurekad837522021-12-15 15:28:49 +01005265 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5266 input_data->len ),
5267 PSA_ERROR_BAD_STATE );
5268 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005269 psa_aead_abort( &operation );
5270
5271 /* ------------------------------------------------------- */
5272
Paul Elliottf94bd992021-09-19 18:15:59 +01005273 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5274
5275 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5276
Andrzej Kurekad837522021-12-15 15:28:49 +01005277 if( operation.alg == PSA_ALG_CCM )
5278 {
5279 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5280 input_data->len, output_data,
5281 output_size, &output_length ),
5282 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005283
Andrzej Kurekad837522021-12-15 15:28:49 +01005284 }
5285 else
5286 {
5287 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5288 input_data->len, output_data,
5289 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005290
Andrzej Kurekad837522021-12-15 15:28:49 +01005291 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5292 input_data->len ),
5293 PSA_ERROR_BAD_STATE );
5294 }
5295 psa_aead_abort( &operation );
5296
5297 /* ------------------------------------------------------- */
5298
5299 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5300
5301 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5302
5303 if( operation.alg == PSA_ALG_CCM )
5304 {
5305 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5306 finish_output_size,
5307 &output_part_length,
5308 tag_buffer, tag_length,
5309 &tag_size ) );
5310 }
5311 else
5312 {
5313 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5314 finish_output_size,
5315 &output_part_length,
5316 tag_buffer, tag_length,
5317 &tag_size ) );
5318
5319 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5320 input_data->len ),
5321 PSA_ERROR_BAD_STATE );
5322 }
5323 psa_aead_abort( &operation );
5324
5325 /* Test for setting lengths after generating nonce + already starting data. */
5326
5327 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5328
5329 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5330 PSA_AEAD_NONCE_MAX_SIZE,
5331 &nonce_length ) );
5332 if( operation.alg == PSA_ALG_CCM )
5333 {
5334
5335 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5336 additional_data->len ),
5337 PSA_ERROR_BAD_STATE );
5338 }
5339 else
5340 {
5341 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5342 additional_data->len ) );
5343
5344 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5345 input_data->len ),
5346 PSA_ERROR_BAD_STATE );
5347 }
5348 psa_aead_abort( &operation );
5349
5350 /* ------------------------------------------------------- */
5351
5352 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5353
5354 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5355 PSA_AEAD_NONCE_MAX_SIZE,
5356 &nonce_length ) );
5357 if( operation.alg == PSA_ALG_CCM )
5358 {
5359 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5360 input_data->len, output_data,
5361 output_size, &output_length ),
5362 PSA_ERROR_BAD_STATE );
5363
5364 }
5365 else
5366 {
5367 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5368 input_data->len, output_data,
5369 output_size, &output_length ) );
5370
5371 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5372 input_data->len ),
5373 PSA_ERROR_BAD_STATE );
5374 }
5375 psa_aead_abort( &operation );
5376
5377 /* ------------------------------------------------------- */
5378
5379 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5380
5381 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5382 PSA_AEAD_NONCE_MAX_SIZE,
5383 &nonce_length ) );
5384 if( operation.alg == PSA_ALG_CCM )
5385 {
5386 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5387 finish_output_size,
5388 &output_part_length,
5389 tag_buffer, tag_length,
5390 &tag_size ) );
5391 }
5392 else
5393 {
5394 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5395 finish_output_size,
5396 &output_part_length,
5397 tag_buffer, tag_length,
5398 &tag_size ) );
5399
5400 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5401 input_data->len ),
5402 PSA_ERROR_BAD_STATE );
5403 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005404 psa_aead_abort( &operation );
5405
Paul Elliott243080c2021-07-21 19:01:17 +01005406 /* Test for not sending any additional data or data after setting non zero
5407 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005408
Paul Elliottc23a9a02021-06-21 18:32:46 +01005409 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5410
5411 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5412
5413 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5414 input_data->len ) );
5415
5416 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5417 finish_output_size,
5418 &output_part_length,
5419 tag_buffer, tag_length,
5420 &tag_size ),
5421 PSA_ERROR_INVALID_ARGUMENT );
5422
5423 psa_aead_abort( &operation );
5424
Paul Elliott243080c2021-07-21 19:01:17 +01005425 /* Test for not sending any additional data or data after setting non-zero
5426 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005427
Paul Elliottc23a9a02021-06-21 18:32:46 +01005428 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5429
5430 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5431
5432 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5433 input_data->len ) );
5434
5435 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5436 finish_output_size,
5437 &output_part_length,
5438 tag_buffer,
5439 tag_length ),
5440 PSA_ERROR_INVALID_ARGUMENT );
5441
5442 psa_aead_abort( &operation );
5443
Paul Elliott243080c2021-07-21 19:01:17 +01005444 /* Test for not sending any additional data after setting a non-zero length
5445 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005446
Paul Elliottc23a9a02021-06-21 18:32:46 +01005447 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5448
5449 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5450
5451 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5452 input_data->len ) );
5453
5454 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5455 input_data->len, output_data,
5456 output_size, &output_length ),
5457 PSA_ERROR_INVALID_ARGUMENT );
5458
5459 psa_aead_abort( &operation );
5460
Paul Elliottf94bd992021-09-19 18:15:59 +01005461 /* Test for not sending any data after setting a non-zero length for it.*/
5462
Paul Elliottf94bd992021-09-19 18:15:59 +01005463 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5464
5465 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5466
5467 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5468 input_data->len ) );
5469
5470 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5471 additional_data->len ) );
5472
5473 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5474 finish_output_size,
5475 &output_part_length,
5476 tag_buffer, tag_length,
5477 &tag_size ),
5478 PSA_ERROR_INVALID_ARGUMENT );
5479
5480 psa_aead_abort( &operation );
5481
Paul Elliottb0450fe2021-09-01 15:06:26 +01005482 /* Test for sending too much additional data after setting lengths. */
5483
Paul Elliottb0450fe2021-09-01 15:06:26 +01005484 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5485
5486 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5487
5488 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5489
5490
5491 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5492 additional_data->len ),
5493 PSA_ERROR_INVALID_ARGUMENT );
5494
5495 psa_aead_abort( &operation );
5496
Paul Elliotta2a09b02021-09-22 14:56:40 +01005497 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005498
5499 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5500
5501 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5502
5503 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5504 input_data->len ) );
5505
5506 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5507 additional_data->len ) );
5508
5509 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5510 1 ),
5511 PSA_ERROR_INVALID_ARGUMENT );
5512
5513 psa_aead_abort( &operation );
5514
Paul Elliottb0450fe2021-09-01 15:06:26 +01005515 /* Test for sending too much data after setting lengths. */
5516
Paul Elliottb0450fe2021-09-01 15:06:26 +01005517 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5518
5519 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5520
5521 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5522
5523 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5524 input_data->len, output_data,
5525 output_size, &output_length ),
5526 PSA_ERROR_INVALID_ARGUMENT );
5527
5528 psa_aead_abort( &operation );
5529
Paul Elliotta2a09b02021-09-22 14:56:40 +01005530 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005531
5532 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5533
5534 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5535
5536 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5537 input_data->len ) );
5538
5539 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5540 additional_data->len ) );
5541
5542 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5543 input_data->len, output_data,
5544 output_size, &output_length ) );
5545
5546 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5547 1, output_data,
5548 output_size, &output_length ),
5549 PSA_ERROR_INVALID_ARGUMENT );
5550
5551 psa_aead_abort( &operation );
5552
Paul Elliottc23a9a02021-06-21 18:32:46 +01005553 /* Test sending additional data after data. */
5554
Paul Elliottc23a9a02021-06-21 18:32:46 +01005555 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5556
5557 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5558
Andrzej Kurekad837522021-12-15 15:28:49 +01005559 if( operation.alg != PSA_ALG_CCM )
5560 {
5561 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5562 input_data->len, output_data,
5563 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005564
Andrzej Kurekad837522021-12-15 15:28:49 +01005565 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5566 additional_data->len ),
5567 PSA_ERROR_BAD_STATE );
5568 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005569 psa_aead_abort( &operation );
5570
Paul Elliott534d0b42021-06-22 19:15:20 +01005571 /* Test calling finish on decryption. */
5572
Paul Elliott534d0b42021-06-22 19:15:20 +01005573 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5574
5575 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5576
5577 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5578 finish_output_size,
5579 &output_part_length,
5580 tag_buffer, tag_length,
5581 &tag_size ),
5582 PSA_ERROR_BAD_STATE );
5583
5584 psa_aead_abort( &operation );
5585
5586 /* Test calling verify on encryption. */
5587
Paul Elliott534d0b42021-06-22 19:15:20 +01005588 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5589
5590 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5591
5592 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5593 finish_output_size,
5594 &output_part_length,
5595 tag_buffer,
5596 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005597 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005598
5599 psa_aead_abort( &operation );
5600
5601
Paul Elliottc23a9a02021-06-21 18:32:46 +01005602exit:
5603 psa_destroy_key( key );
5604 psa_aead_abort( &operation );
5605 mbedtls_free( output_data );
5606 mbedtls_free( final_data );
5607 PSA_DONE( );
5608}
5609/* END_CASE */
5610
5611/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005612void signature_size( int type_arg,
5613 int bits,
5614 int alg_arg,
5615 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005616{
5617 psa_key_type_t type = type_arg;
5618 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005619 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005620
Gilles Peskinefe11b722018-12-18 00:24:04 +01005621 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005622
Gilles Peskinee59236f2018-01-27 23:32:46 +01005623exit:
5624 ;
5625}
5626/* END_CASE */
5627
5628/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005629void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5630 int alg_arg, data_t *input_data,
5631 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005632{
Ronald Cron5425a212020-08-04 14:58:35 +02005633 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005634 psa_key_type_t key_type = key_type_arg;
5635 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005636 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005637 unsigned char *signature = NULL;
5638 size_t signature_size;
5639 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005640 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005641
Gilles Peskine8817f612018-12-18 00:18:46 +01005642 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005643
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005644 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005645 psa_set_key_algorithm( &attributes, alg );
5646 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005647
Gilles Peskine049c7532019-05-15 20:22:09 +02005648 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005649 &key ) );
5650 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005651 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005652
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005653 /* Allocate a buffer which has the size advertized by the
5654 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005655 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005656 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005657 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005658 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005659 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005660
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005661 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005662 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005663 input_data->x, input_data->len,
5664 signature, signature_size,
5665 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005666 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005667 ASSERT_COMPARE( output_data->x, output_data->len,
5668 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005669
5670exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005671 /*
5672 * Key attributes may have been returned by psa_get_key_attributes()
5673 * thus reset them as required.
5674 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005675 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005676
Ronald Cron5425a212020-08-04 14:58:35 +02005677 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005678 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005679 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005680}
5681/* END_CASE */
5682
5683/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005684void sign_hash_fail( int key_type_arg, data_t *key_data,
5685 int alg_arg, data_t *input_data,
5686 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005687{
Ronald Cron5425a212020-08-04 14:58:35 +02005688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005689 psa_key_type_t key_type = key_type_arg;
5690 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005691 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005692 psa_status_t actual_status;
5693 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005694 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005695 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005696 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005697
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005698 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005699
Gilles Peskine8817f612018-12-18 00:18:46 +01005700 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005701
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005702 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005703 psa_set_key_algorithm( &attributes, alg );
5704 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005705
Gilles Peskine049c7532019-05-15 20:22:09 +02005706 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005707 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005708
Ronald Cron5425a212020-08-04 14:58:35 +02005709 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005710 input_data->x, input_data->len,
5711 signature, signature_size,
5712 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005713 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005714 /* The value of *signature_length is unspecified on error, but
5715 * whatever it is, it should be less than signature_size, so that
5716 * if the caller tries to read *signature_length bytes without
5717 * checking the error code then they don't overflow a buffer. */
5718 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005719
5720exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005721 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005722 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005723 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005724 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005725}
5726/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005727
5728/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005729void sign_verify_hash( int key_type_arg, data_t *key_data,
5730 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005731{
Ronald Cron5425a212020-08-04 14:58:35 +02005732 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005733 psa_key_type_t key_type = key_type_arg;
5734 psa_algorithm_t alg = alg_arg;
5735 size_t key_bits;
5736 unsigned char *signature = NULL;
5737 size_t signature_size;
5738 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005740
Gilles Peskine8817f612018-12-18 00:18:46 +01005741 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005742
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005743 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005744 psa_set_key_algorithm( &attributes, alg );
5745 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005746
Gilles Peskine049c7532019-05-15 20:22:09 +02005747 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005748 &key ) );
5749 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005750 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005751
5752 /* Allocate a buffer which has the size advertized by the
5753 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005754 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005755 key_bits, alg );
5756 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005757 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005758 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005759
5760 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005761 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005762 input_data->x, input_data->len,
5763 signature, signature_size,
5764 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005765 /* Check that the signature length looks sensible. */
5766 TEST_ASSERT( signature_length <= signature_size );
5767 TEST_ASSERT( signature_length > 0 );
5768
5769 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005770 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005771 input_data->x, input_data->len,
5772 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005773
5774 if( input_data->len != 0 )
5775 {
5776 /* Flip a bit in the input and verify that the signature is now
5777 * detected as invalid. Flip a bit at the beginning, not at the end,
5778 * because ECDSA may ignore the last few bits of the input. */
5779 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005780 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005781 input_data->x, input_data->len,
5782 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005783 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005784 }
5785
5786exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005787 /*
5788 * Key attributes may have been returned by psa_get_key_attributes()
5789 * thus reset them as required.
5790 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005791 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005792
Ronald Cron5425a212020-08-04 14:58:35 +02005793 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005794 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005795 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005796}
5797/* END_CASE */
5798
5799/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005800void verify_hash( int key_type_arg, data_t *key_data,
5801 int alg_arg, data_t *hash_data,
5802 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005803{
Ronald Cron5425a212020-08-04 14:58:35 +02005804 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005805 psa_key_type_t key_type = key_type_arg;
5806 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005807 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005808
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005809 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005810
Gilles Peskine8817f612018-12-18 00:18:46 +01005811 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005812
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005813 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005814 psa_set_key_algorithm( &attributes, alg );
5815 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005816
Gilles Peskine049c7532019-05-15 20:22:09 +02005817 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005818 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005819
Ronald Cron5425a212020-08-04 14:58:35 +02005820 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005821 hash_data->x, hash_data->len,
5822 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005823
itayzafrir5c753392018-05-08 11:18:38 +03005824exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005825 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005826 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005827 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005828}
5829/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005830
5831/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005832void verify_hash_fail( int key_type_arg, data_t *key_data,
5833 int alg_arg, data_t *hash_data,
5834 data_t *signature_data,
5835 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005836{
Ronald Cron5425a212020-08-04 14:58:35 +02005837 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005838 psa_key_type_t key_type = key_type_arg;
5839 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005840 psa_status_t actual_status;
5841 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005842 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005843
Gilles Peskine8817f612018-12-18 00:18:46 +01005844 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005845
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005846 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005847 psa_set_key_algorithm( &attributes, alg );
5848 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005849
Gilles Peskine049c7532019-05-15 20:22:09 +02005850 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005851 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005852
Ronald Cron5425a212020-08-04 14:58:35 +02005853 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005854 hash_data->x, hash_data->len,
5855 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005856 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005857
5858exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005859 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005860 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005861 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005862}
5863/* END_CASE */
5864
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005865/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005866void sign_message_deterministic( int key_type_arg,
5867 data_t *key_data,
5868 int alg_arg,
5869 data_t *input_data,
5870 data_t *output_data )
5871{
5872 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5873 psa_key_type_t key_type = key_type_arg;
5874 psa_algorithm_t alg = alg_arg;
5875 size_t key_bits;
5876 unsigned char *signature = NULL;
5877 size_t signature_size;
5878 size_t signature_length = 0xdeadbeef;
5879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5880
5881 PSA_ASSERT( psa_crypto_init( ) );
5882
5883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5884 psa_set_key_algorithm( &attributes, alg );
5885 psa_set_key_type( &attributes, key_type );
5886
5887 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5888 &key ) );
5889 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5890 key_bits = psa_get_key_bits( &attributes );
5891
5892 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5893 TEST_ASSERT( signature_size != 0 );
5894 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5895 ASSERT_ALLOC( signature, signature_size );
5896
5897 PSA_ASSERT( psa_sign_message( key, alg,
5898 input_data->x, input_data->len,
5899 signature, signature_size,
5900 &signature_length ) );
5901
5902 ASSERT_COMPARE( output_data->x, output_data->len,
5903 signature, signature_length );
5904
5905exit:
5906 psa_reset_key_attributes( &attributes );
5907
5908 psa_destroy_key( key );
5909 mbedtls_free( signature );
5910 PSA_DONE( );
5911
5912}
5913/* END_CASE */
5914
5915/* BEGIN_CASE */
5916void sign_message_fail( int key_type_arg,
5917 data_t *key_data,
5918 int alg_arg,
5919 data_t *input_data,
5920 int signature_size_arg,
5921 int expected_status_arg )
5922{
5923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5924 psa_key_type_t key_type = key_type_arg;
5925 psa_algorithm_t alg = alg_arg;
5926 size_t signature_size = signature_size_arg;
5927 psa_status_t actual_status;
5928 psa_status_t expected_status = expected_status_arg;
5929 unsigned char *signature = NULL;
5930 size_t signature_length = 0xdeadbeef;
5931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5932
5933 ASSERT_ALLOC( signature, signature_size );
5934
5935 PSA_ASSERT( psa_crypto_init( ) );
5936
5937 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5938 psa_set_key_algorithm( &attributes, alg );
5939 psa_set_key_type( &attributes, key_type );
5940
5941 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5942 &key ) );
5943
5944 actual_status = psa_sign_message( key, alg,
5945 input_data->x, input_data->len,
5946 signature, signature_size,
5947 &signature_length );
5948 TEST_EQUAL( actual_status, expected_status );
5949 /* The value of *signature_length is unspecified on error, but
5950 * whatever it is, it should be less than signature_size, so that
5951 * if the caller tries to read *signature_length bytes without
5952 * checking the error code then they don't overflow a buffer. */
5953 TEST_ASSERT( signature_length <= signature_size );
5954
5955exit:
5956 psa_reset_key_attributes( &attributes );
5957 psa_destroy_key( key );
5958 mbedtls_free( signature );
5959 PSA_DONE( );
5960}
5961/* END_CASE */
5962
5963/* BEGIN_CASE */
5964void sign_verify_message( int key_type_arg,
5965 data_t *key_data,
5966 int alg_arg,
5967 data_t *input_data )
5968{
5969 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5970 psa_key_type_t key_type = key_type_arg;
5971 psa_algorithm_t alg = alg_arg;
5972 size_t key_bits;
5973 unsigned char *signature = NULL;
5974 size_t signature_size;
5975 size_t signature_length = 0xdeadbeef;
5976 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5977
5978 PSA_ASSERT( psa_crypto_init( ) );
5979
5980 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5981 PSA_KEY_USAGE_VERIFY_MESSAGE );
5982 psa_set_key_algorithm( &attributes, alg );
5983 psa_set_key_type( &attributes, key_type );
5984
5985 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5986 &key ) );
5987 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5988 key_bits = psa_get_key_bits( &attributes );
5989
5990 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5991 TEST_ASSERT( signature_size != 0 );
5992 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5993 ASSERT_ALLOC( signature, signature_size );
5994
5995 PSA_ASSERT( psa_sign_message( key, alg,
5996 input_data->x, input_data->len,
5997 signature, signature_size,
5998 &signature_length ) );
5999 TEST_ASSERT( signature_length <= signature_size );
6000 TEST_ASSERT( signature_length > 0 );
6001
6002 PSA_ASSERT( psa_verify_message( key, alg,
6003 input_data->x, input_data->len,
6004 signature, signature_length ) );
6005
6006 if( input_data->len != 0 )
6007 {
6008 /* Flip a bit in the input and verify that the signature is now
6009 * detected as invalid. Flip a bit at the beginning, not at the end,
6010 * because ECDSA may ignore the last few bits of the input. */
6011 input_data->x[0] ^= 1;
6012 TEST_EQUAL( psa_verify_message( key, alg,
6013 input_data->x, input_data->len,
6014 signature, signature_length ),
6015 PSA_ERROR_INVALID_SIGNATURE );
6016 }
6017
6018exit:
6019 psa_reset_key_attributes( &attributes );
6020
6021 psa_destroy_key( key );
6022 mbedtls_free( signature );
6023 PSA_DONE( );
6024}
6025/* END_CASE */
6026
6027/* BEGIN_CASE */
6028void verify_message( int key_type_arg,
6029 data_t *key_data,
6030 int alg_arg,
6031 data_t *input_data,
6032 data_t *signature_data )
6033{
6034 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6035 psa_key_type_t key_type = key_type_arg;
6036 psa_algorithm_t alg = alg_arg;
6037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6038
6039 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
6040
6041 PSA_ASSERT( psa_crypto_init( ) );
6042
6043 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6044 psa_set_key_algorithm( &attributes, alg );
6045 psa_set_key_type( &attributes, key_type );
6046
6047 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6048 &key ) );
6049
6050 PSA_ASSERT( psa_verify_message( key, alg,
6051 input_data->x, input_data->len,
6052 signature_data->x, signature_data->len ) );
6053
6054exit:
6055 psa_reset_key_attributes( &attributes );
6056 psa_destroy_key( key );
6057 PSA_DONE( );
6058}
6059/* END_CASE */
6060
6061/* BEGIN_CASE */
6062void verify_message_fail( int key_type_arg,
6063 data_t *key_data,
6064 int alg_arg,
6065 data_t *hash_data,
6066 data_t *signature_data,
6067 int expected_status_arg )
6068{
6069 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6070 psa_key_type_t key_type = key_type_arg;
6071 psa_algorithm_t alg = alg_arg;
6072 psa_status_t actual_status;
6073 psa_status_t expected_status = expected_status_arg;
6074 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6075
6076 PSA_ASSERT( psa_crypto_init( ) );
6077
6078 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6079 psa_set_key_algorithm( &attributes, alg );
6080 psa_set_key_type( &attributes, key_type );
6081
6082 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6083 &key ) );
6084
6085 actual_status = psa_verify_message( key, alg,
6086 hash_data->x, hash_data->len,
6087 signature_data->x,
6088 signature_data->len );
6089 TEST_EQUAL( actual_status, expected_status );
6090
6091exit:
6092 psa_reset_key_attributes( &attributes );
6093 psa_destroy_key( key );
6094 PSA_DONE( );
6095}
6096/* END_CASE */
6097
6098/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006099void asymmetric_encrypt( int key_type_arg,
6100 data_t *key_data,
6101 int alg_arg,
6102 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006103 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006104 int expected_output_length_arg,
6105 int expected_status_arg )
6106{
Ronald Cron5425a212020-08-04 14:58:35 +02006107 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006108 psa_key_type_t key_type = key_type_arg;
6109 psa_algorithm_t alg = alg_arg;
6110 size_t expected_output_length = expected_output_length_arg;
6111 size_t key_bits;
6112 unsigned char *output = NULL;
6113 size_t output_size;
6114 size_t output_length = ~0;
6115 psa_status_t actual_status;
6116 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006118
Gilles Peskine8817f612018-12-18 00:18:46 +01006119 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006120
Gilles Peskine656896e2018-06-29 19:12:28 +02006121 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006122 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6123 psa_set_key_algorithm( &attributes, alg );
6124 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006125 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006126 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006127
6128 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006129 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006130 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006131
Gilles Peskine656896e2018-06-29 19:12:28 +02006132 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006133 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006134 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006135
6136 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006137 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006138 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006139 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006140 output, output_size,
6141 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006142 TEST_EQUAL( actual_status, expected_status );
6143 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006144
Gilles Peskine68428122018-06-30 18:42:41 +02006145 /* If the label is empty, the test framework puts a non-null pointer
6146 * in label->x. Test that a null pointer works as well. */
6147 if( label->len == 0 )
6148 {
6149 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006150 if( output_size != 0 )
6151 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006152 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006153 input_data->x, input_data->len,
6154 NULL, label->len,
6155 output, output_size,
6156 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006157 TEST_EQUAL( actual_status, expected_status );
6158 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006159 }
6160
Gilles Peskine656896e2018-06-29 19:12:28 +02006161exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006162 /*
6163 * Key attributes may have been returned by psa_get_key_attributes()
6164 * thus reset them as required.
6165 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006166 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006167
Ronald Cron5425a212020-08-04 14:58:35 +02006168 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006169 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006170 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006171}
6172/* END_CASE */
6173
6174/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006175void asymmetric_encrypt_decrypt( int key_type_arg,
6176 data_t *key_data,
6177 int alg_arg,
6178 data_t *input_data,
6179 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006180{
Ronald Cron5425a212020-08-04 14:58:35 +02006181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006182 psa_key_type_t key_type = key_type_arg;
6183 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006184 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006185 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006186 size_t output_size;
6187 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006188 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006189 size_t output2_size;
6190 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006192
Gilles Peskine8817f612018-12-18 00:18:46 +01006193 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006194
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006195 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6196 psa_set_key_algorithm( &attributes, alg );
6197 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006198
Gilles Peskine049c7532019-05-15 20:22:09 +02006199 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006200 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006201
6202 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006203 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006204 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006205
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006206 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006207 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006208 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006209
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006210 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006211 TEST_ASSERT( output2_size <=
6212 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6213 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006214 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006215
Gilles Peskineeebd7382018-06-08 18:11:54 +02006216 /* We test encryption by checking that encrypt-then-decrypt gives back
6217 * the original plaintext because of the non-optional random
6218 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006219 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006220 input_data->x, input_data->len,
6221 label->x, label->len,
6222 output, output_size,
6223 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006224 /* We don't know what ciphertext length to expect, but check that
6225 * it looks sensible. */
6226 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006227
Ronald Cron5425a212020-08-04 14:58:35 +02006228 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006229 output, output_length,
6230 label->x, label->len,
6231 output2, output2_size,
6232 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006233 ASSERT_COMPARE( input_data->x, input_data->len,
6234 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006235
6236exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006237 /*
6238 * Key attributes may have been returned by psa_get_key_attributes()
6239 * thus reset them as required.
6240 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006241 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006242
Ronald Cron5425a212020-08-04 14:58:35 +02006243 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006244 mbedtls_free( output );
6245 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006246 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006247}
6248/* END_CASE */
6249
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006250/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006251void asymmetric_decrypt( int key_type_arg,
6252 data_t *key_data,
6253 int alg_arg,
6254 data_t *input_data,
6255 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006256 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006257{
Ronald Cron5425a212020-08-04 14:58:35 +02006258 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006259 psa_key_type_t key_type = key_type_arg;
6260 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006261 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006262 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006263 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006264 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006265 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006266
Gilles Peskine8817f612018-12-18 00:18:46 +01006267 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006268
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006269 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6270 psa_set_key_algorithm( &attributes, alg );
6271 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006272
Gilles Peskine049c7532019-05-15 20:22:09 +02006273 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006274 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006275
gabor-mezei-armceface22021-01-21 12:26:17 +01006276 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6277 key_bits = psa_get_key_bits( &attributes );
6278
6279 /* Determine the maximum ciphertext length */
6280 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6281 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6282 ASSERT_ALLOC( output, output_size );
6283
Ronald Cron5425a212020-08-04 14:58:35 +02006284 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006285 input_data->x, input_data->len,
6286 label->x, label->len,
6287 output,
6288 output_size,
6289 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006290 ASSERT_COMPARE( expected_data->x, expected_data->len,
6291 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006292
Gilles Peskine68428122018-06-30 18:42:41 +02006293 /* If the label is empty, the test framework puts a non-null pointer
6294 * in label->x. Test that a null pointer works as well. */
6295 if( label->len == 0 )
6296 {
6297 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006298 if( output_size != 0 )
6299 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006300 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006301 input_data->x, input_data->len,
6302 NULL, label->len,
6303 output,
6304 output_size,
6305 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006306 ASSERT_COMPARE( expected_data->x, expected_data->len,
6307 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006308 }
6309
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006310exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006311 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006312 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006313 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006314 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006315}
6316/* END_CASE */
6317
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006318/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006319void asymmetric_decrypt_fail( int key_type_arg,
6320 data_t *key_data,
6321 int alg_arg,
6322 data_t *input_data,
6323 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006324 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006325 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006326{
Ronald Cron5425a212020-08-04 14:58:35 +02006327 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006328 psa_key_type_t key_type = key_type_arg;
6329 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006330 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006331 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006332 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006333 psa_status_t actual_status;
6334 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006336
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006337 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006338
Gilles Peskine8817f612018-12-18 00:18:46 +01006339 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006340
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006341 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6342 psa_set_key_algorithm( &attributes, alg );
6343 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006344
Gilles Peskine049c7532019-05-15 20:22:09 +02006345 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006346 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006347
Ronald Cron5425a212020-08-04 14:58:35 +02006348 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006349 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006350 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006351 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006352 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006353 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006354 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006355
Gilles Peskine68428122018-06-30 18:42:41 +02006356 /* If the label is empty, the test framework puts a non-null pointer
6357 * in label->x. Test that a null pointer works as well. */
6358 if( label->len == 0 )
6359 {
6360 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006361 if( output_size != 0 )
6362 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006363 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006364 input_data->x, input_data->len,
6365 NULL, label->len,
6366 output, output_size,
6367 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006368 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006369 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006370 }
6371
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006372exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006373 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006374 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006375 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006376 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006377}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006378/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006379
6380/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006381void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006382{
6383 /* Test each valid way of initializing the object, except for `= {0}`, as
6384 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6385 * though it's OK by the C standard. We could test for this, but we'd need
6386 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006387 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006388 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6389 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6390 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006391
6392 memset( &zero, 0, sizeof( zero ) );
6393
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006394 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006395 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006396 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006397 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006398 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006399 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006400 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006401
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006402 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006403 PSA_ASSERT( psa_key_derivation_abort(&func) );
6404 PSA_ASSERT( psa_key_derivation_abort(&init) );
6405 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006406}
6407/* END_CASE */
6408
Janos Follath16de4a42019-06-13 16:32:24 +01006409/* BEGIN_CASE */
6410void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006411{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006412 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006413 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006414 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006415
Gilles Peskine8817f612018-12-18 00:18:46 +01006416 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006417
Janos Follath16de4a42019-06-13 16:32:24 +01006418 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006419 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006420
6421exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006422 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006423 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006424}
6425/* END_CASE */
6426
Janos Follathaf3c2a02019-06-12 12:34:34 +01006427/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006428void derive_set_capacity( int alg_arg, int capacity_arg,
6429 int expected_status_arg )
6430{
6431 psa_algorithm_t alg = alg_arg;
6432 size_t capacity = capacity_arg;
6433 psa_status_t expected_status = expected_status_arg;
6434 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6435
6436 PSA_ASSERT( psa_crypto_init( ) );
6437
6438 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6439
6440 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6441 expected_status );
6442
6443exit:
6444 psa_key_derivation_abort( &operation );
6445 PSA_DONE( );
6446}
6447/* END_CASE */
6448
6449/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006450void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006451 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006452 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006453 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006454 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006455 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006456 int expected_status_arg3,
6457 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006458{
6459 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006460 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6461 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006462 psa_status_t expected_statuses[] = {expected_status_arg1,
6463 expected_status_arg2,
6464 expected_status_arg3};
6465 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006466 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6467 MBEDTLS_SVC_KEY_ID_INIT,
6468 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006469 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6470 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6471 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006472 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006473 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006474 psa_status_t expected_output_status = expected_output_status_arg;
6475 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006476
6477 PSA_ASSERT( psa_crypto_init( ) );
6478
6479 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6480 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006481
6482 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6483
6484 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6485 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006486 mbedtls_test_set_step( i );
6487 if( steps[i] == 0 )
6488 {
6489 /* Skip this step */
6490 }
6491 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006492 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006493 psa_set_key_type( &attributes, key_types[i] );
6494 PSA_ASSERT( psa_import_key( &attributes,
6495 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006496 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006497 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6498 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6499 {
6500 // When taking a private key as secret input, use key agreement
6501 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006502 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6503 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006504 expected_statuses[i] );
6505 }
6506 else
6507 {
6508 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006509 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006510 expected_statuses[i] );
6511 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006512 }
6513 else
6514 {
6515 TEST_EQUAL( psa_key_derivation_input_bytes(
6516 &operation, steps[i],
6517 inputs[i]->x, inputs[i]->len ),
6518 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006519 }
6520 }
6521
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006522 if( output_key_type != PSA_KEY_TYPE_NONE )
6523 {
6524 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006525 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006526 psa_set_key_bits( &attributes, 8 );
6527 actual_output_status =
6528 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006529 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006530 }
6531 else
6532 {
6533 uint8_t buffer[1];
6534 actual_output_status =
6535 psa_key_derivation_output_bytes( &operation,
6536 buffer, sizeof( buffer ) );
6537 }
6538 TEST_EQUAL( actual_output_status, expected_output_status );
6539
Janos Follathaf3c2a02019-06-12 12:34:34 +01006540exit:
6541 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006542 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6543 psa_destroy_key( keys[i] );
6544 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006545 PSA_DONE( );
6546}
6547/* END_CASE */
6548
Janos Follathd958bb72019-07-03 15:02:16 +01006549/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006550void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006551{
Janos Follathd958bb72019-07-03 15:02:16 +01006552 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006554 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006555 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006556 unsigned char input1[] = "Input 1";
6557 size_t input1_length = sizeof( input1 );
6558 unsigned char input2[] = "Input 2";
6559 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006560 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006561 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006562 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6563 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6564 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006565 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006566
Gilles Peskine8817f612018-12-18 00:18:46 +01006567 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006568
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006569 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6570 psa_set_key_algorithm( &attributes, alg );
6571 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006572
Gilles Peskine73676cb2019-05-15 20:15:10 +02006573 PSA_ASSERT( psa_import_key( &attributes,
6574 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006575 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006576
6577 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006578 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6579 input1, input1_length,
6580 input2, input2_length,
6581 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006582 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006583
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006584 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006585 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006586 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006587
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006588 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006589
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006590 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006591 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006592
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006593exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006594 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006595 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006596 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006597}
6598/* END_CASE */
6599
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006600/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006601void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006602{
6603 uint8_t output_buffer[16];
6604 size_t buffer_size = 16;
6605 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006606 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006607
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006608 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6609 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006610 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006611
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006612 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006613 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006614
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006615 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006616
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006617 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6618 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006619 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006620
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006621 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006622 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006623
6624exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006625 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006626}
6627/* END_CASE */
6628
6629/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006630void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006631 int step1_arg, data_t *input1,
6632 int step2_arg, data_t *input2,
6633 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006634 int requested_capacity_arg,
6635 data_t *expected_output1,
6636 data_t *expected_output2 )
6637{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006638 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006639 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6640 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006641 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6642 MBEDTLS_SVC_KEY_ID_INIT,
6643 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006644 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006645 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006646 uint8_t *expected_outputs[2] =
6647 {expected_output1->x, expected_output2->x};
6648 size_t output_sizes[2] =
6649 {expected_output1->len, expected_output2->len};
6650 size_t output_buffer_size = 0;
6651 uint8_t *output_buffer = NULL;
6652 size_t expected_capacity;
6653 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006655 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006656 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006657
6658 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6659 {
6660 if( output_sizes[i] > output_buffer_size )
6661 output_buffer_size = output_sizes[i];
6662 if( output_sizes[i] == 0 )
6663 expected_outputs[i] = NULL;
6664 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006665 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006666 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006667
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006668 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6669 psa_set_key_algorithm( &attributes, alg );
6670 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006671
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006672 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006673 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6674 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6675 requested_capacity ) );
6676 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006677 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006678 switch( steps[i] )
6679 {
6680 case 0:
6681 break;
6682 case PSA_KEY_DERIVATION_INPUT_SECRET:
6683 PSA_ASSERT( psa_import_key( &attributes,
6684 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006685 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006686
6687 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6688 {
6689 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6690 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6691 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6692 }
6693
Gilles Peskine1468da72019-05-29 17:35:49 +02006694 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006695 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006696 break;
6697 default:
6698 PSA_ASSERT( psa_key_derivation_input_bytes(
6699 &operation, steps[i],
6700 inputs[i]->x, inputs[i]->len ) );
6701 break;
6702 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006703 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006704
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006705 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006706 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006707 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006708 expected_capacity = requested_capacity;
6709
6710 /* Expansion phase. */
6711 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6712 {
6713 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006714 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006715 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006716 if( expected_capacity == 0 && output_sizes[i] == 0 )
6717 {
6718 /* Reading 0 bytes when 0 bytes are available can go either way. */
6719 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006720 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006721 continue;
6722 }
6723 else if( expected_capacity == 0 ||
6724 output_sizes[i] > expected_capacity )
6725 {
6726 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006727 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006728 expected_capacity = 0;
6729 continue;
6730 }
6731 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006732 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006733 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006734 ASSERT_COMPARE( output_buffer, output_sizes[i],
6735 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006736 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006737 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006738 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006739 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006740 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006741 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006742 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006743
6744exit:
6745 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006746 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006747 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6748 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006749 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006750}
6751/* END_CASE */
6752
6753/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006754void derive_full( int alg_arg,
6755 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006756 data_t *input1,
6757 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006758 int requested_capacity_arg )
6759{
Ronald Cron5425a212020-08-04 14:58:35 +02006760 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006761 psa_algorithm_t alg = alg_arg;
6762 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006763 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006764 unsigned char output_buffer[16];
6765 size_t expected_capacity = requested_capacity;
6766 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006768
Gilles Peskine8817f612018-12-18 00:18:46 +01006769 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006770
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006771 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6772 psa_set_key_algorithm( &attributes, alg );
6773 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006774
Gilles Peskine049c7532019-05-15 20:22:09 +02006775 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006776 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006777
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006778 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6779 input1->x, input1->len,
6780 input2->x, input2->len,
6781 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006782 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006783
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006784 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006785 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006786 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006787
6788 /* Expansion phase. */
6789 while( current_capacity > 0 )
6790 {
6791 size_t read_size = sizeof( output_buffer );
6792 if( read_size > current_capacity )
6793 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006794 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006795 output_buffer,
6796 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006797 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006798 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006799 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006800 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006801 }
6802
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006803 /* Check that the operation refuses to go over capacity. */
6804 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006805 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006806
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006807 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006808
6809exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006810 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006811 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006812 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006813}
6814/* END_CASE */
6815
Janos Follathe60c9052019-07-03 13:51:30 +01006816/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006817void derive_key_exercise( int alg_arg,
6818 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006819 data_t *input1,
6820 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006821 int derived_type_arg,
6822 int derived_bits_arg,
6823 int derived_usage_arg,
6824 int derived_alg_arg )
6825{
Ronald Cron5425a212020-08-04 14:58:35 +02006826 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6827 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006828 psa_algorithm_t alg = alg_arg;
6829 psa_key_type_t derived_type = derived_type_arg;
6830 size_t derived_bits = derived_bits_arg;
6831 psa_key_usage_t derived_usage = derived_usage_arg;
6832 psa_algorithm_t derived_alg = derived_alg_arg;
6833 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006834 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006835 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006836 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006837
Gilles Peskine8817f612018-12-18 00:18:46 +01006838 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006839
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006840 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6841 psa_set_key_algorithm( &attributes, alg );
6842 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006843 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006844 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006845
6846 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006847 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6848 input1->x, input1->len,
6849 input2->x, input2->len,
6850 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006851 goto exit;
6852
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006853 psa_set_key_usage_flags( &attributes, derived_usage );
6854 psa_set_key_algorithm( &attributes, derived_alg );
6855 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006856 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006857 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006858 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006859
6860 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006861 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006862 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6863 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006864
6865 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006866 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006867 goto exit;
6868
6869exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006870 /*
6871 * Key attributes may have been returned by psa_get_key_attributes()
6872 * thus reset them as required.
6873 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006874 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006875
6876 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006877 psa_destroy_key( base_key );
6878 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006879 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006880}
6881/* END_CASE */
6882
Janos Follath42fd8882019-07-03 14:17:09 +01006883/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006884void derive_key_export( int alg_arg,
6885 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006886 data_t *input1,
6887 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006888 int bytes1_arg,
6889 int bytes2_arg )
6890{
Ronald Cron5425a212020-08-04 14:58:35 +02006891 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6892 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006893 psa_algorithm_t alg = alg_arg;
6894 size_t bytes1 = bytes1_arg;
6895 size_t bytes2 = bytes2_arg;
6896 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006897 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006898 uint8_t *output_buffer = NULL;
6899 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006900 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6901 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006902 size_t length;
6903
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006904 ASSERT_ALLOC( output_buffer, capacity );
6905 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006906 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006907
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006908 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6909 psa_set_key_algorithm( &base_attributes, alg );
6910 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006911 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006912 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006913
6914 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006915 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6916 input1->x, input1->len,
6917 input2->x, input2->len,
6918 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006919 goto exit;
6920
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006921 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006922 output_buffer,
6923 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006924 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006925
6926 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006927 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6928 input1->x, input1->len,
6929 input2->x, input2->len,
6930 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006931 goto exit;
6932
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006933 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6934 psa_set_key_algorithm( &derived_attributes, 0 );
6935 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006936 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006937 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006938 &derived_key ) );
6939 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006940 export_buffer, bytes1,
6941 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006942 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006943 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006944 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006945 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006946 &derived_key ) );
6947 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006948 export_buffer + bytes1, bytes2,
6949 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006950 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006951
6952 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006953 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6954 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006955
6956exit:
6957 mbedtls_free( output_buffer );
6958 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006959 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006960 psa_destroy_key( base_key );
6961 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006962 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006963}
6964/* END_CASE */
6965
6966/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006967void derive_key( int alg_arg,
6968 data_t *key_data, data_t *input1, data_t *input2,
6969 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006970 int expected_status_arg,
6971 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006972{
Ronald Cron5425a212020-08-04 14:58:35 +02006973 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6974 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006975 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006976 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006977 size_t bits = bits_arg;
6978 psa_status_t expected_status = expected_status_arg;
6979 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6980 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6981 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6982
6983 PSA_ASSERT( psa_crypto_init( ) );
6984
6985 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6986 psa_set_key_algorithm( &base_attributes, alg );
6987 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6988 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006989 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006990
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006991 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6992 input1->x, input1->len,
6993 input2->x, input2->len,
6994 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006995 goto exit;
6996
6997 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6998 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006999 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02007000 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01007001
7002 psa_status_t status =
7003 psa_key_derivation_output_key( &derived_attributes,
7004 &operation,
7005 &derived_key );
7006 if( is_large_output > 0 )
7007 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7008 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02007009
7010exit:
7011 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007012 psa_destroy_key( base_key );
7013 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02007014 PSA_DONE( );
7015}
7016/* END_CASE */
7017
7018/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02007019void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02007020 int our_key_type_arg, int our_key_alg_arg,
7021 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02007022 int expected_status_arg )
7023{
Ronald Cron5425a212020-08-04 14:58:35 +02007024 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007025 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007026 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007027 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007028 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007030 psa_status_t expected_status = expected_status_arg;
7031 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007032
Gilles Peskine8817f612018-12-18 00:18:46 +01007033 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007034
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007035 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007036 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007037 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007038 PSA_ASSERT( psa_import_key( &attributes,
7039 our_key_data->x, our_key_data->len,
7040 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007041
Gilles Peskine77f40d82019-04-11 21:27:06 +02007042 /* The tests currently include inputs that should fail at either step.
7043 * Test cases that fail at the setup step should be changed to call
7044 * key_derivation_setup instead, and this function should be renamed
7045 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007046 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02007047 if( status == PSA_SUCCESS )
7048 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007049 TEST_EQUAL( psa_key_derivation_key_agreement(
7050 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
7051 our_key,
7052 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02007053 expected_status );
7054 }
7055 else
7056 {
7057 TEST_ASSERT( status == expected_status );
7058 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007059
7060exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007061 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007062 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007063 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007064}
7065/* END_CASE */
7066
7067/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007068void raw_key_agreement( int alg_arg,
7069 int our_key_type_arg, data_t *our_key_data,
7070 data_t *peer_key_data,
7071 data_t *expected_output )
7072{
Ronald Cron5425a212020-08-04 14:58:35 +02007073 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007074 psa_algorithm_t alg = alg_arg;
7075 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007076 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007077 unsigned char *output = NULL;
7078 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007079 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007080
7081 ASSERT_ALLOC( output, expected_output->len );
7082 PSA_ASSERT( psa_crypto_init( ) );
7083
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007084 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7085 psa_set_key_algorithm( &attributes, alg );
7086 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007087 PSA_ASSERT( psa_import_key( &attributes,
7088 our_key_data->x, our_key_data->len,
7089 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007090
gabor-mezei-armceface22021-01-21 12:26:17 +01007091 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7092 key_bits = psa_get_key_bits( &attributes );
7093
Gilles Peskinebe697d82019-05-16 18:00:41 +02007094 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7095 peer_key_data->x, peer_key_data->len,
7096 output, expected_output->len,
7097 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007098 ASSERT_COMPARE( output, output_length,
7099 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01007100 TEST_ASSERT( output_length <=
7101 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7102 TEST_ASSERT( output_length <=
7103 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007104
7105exit:
7106 mbedtls_free( output );
7107 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007108 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007109}
7110/* END_CASE */
7111
7112/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007113void key_agreement_capacity( int alg_arg,
7114 int our_key_type_arg, data_t *our_key_data,
7115 data_t *peer_key_data,
7116 int expected_capacity_arg )
7117{
Ronald Cron5425a212020-08-04 14:58:35 +02007118 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007119 psa_algorithm_t alg = alg_arg;
7120 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007121 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007123 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007124 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007125
Gilles Peskine8817f612018-12-18 00:18:46 +01007126 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007127
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007128 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7129 psa_set_key_algorithm( &attributes, alg );
7130 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007131 PSA_ASSERT( psa_import_key( &attributes,
7132 our_key_data->x, our_key_data->len,
7133 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007134
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007135 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007136 PSA_ASSERT( psa_key_derivation_key_agreement(
7137 &operation,
7138 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7139 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007140 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7141 {
7142 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007143 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007144 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007145 NULL, 0 ) );
7146 }
Gilles Peskine59685592018-09-18 12:11:34 +02007147
Gilles Peskinebf491972018-10-25 22:36:12 +02007148 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007149 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007150 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007151 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007152
Gilles Peskinebf491972018-10-25 22:36:12 +02007153 /* Test the actual capacity by reading the output. */
7154 while( actual_capacity > sizeof( output ) )
7155 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007156 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007157 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007158 actual_capacity -= sizeof( output );
7159 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007160 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007161 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007162 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007163 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007164
Gilles Peskine59685592018-09-18 12:11:34 +02007165exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007166 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007167 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007168 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007169}
7170/* END_CASE */
7171
7172/* BEGIN_CASE */
7173void key_agreement_output( int alg_arg,
7174 int our_key_type_arg, data_t *our_key_data,
7175 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007176 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007177{
Ronald Cron5425a212020-08-04 14:58:35 +02007178 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007179 psa_algorithm_t alg = alg_arg;
7180 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007181 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007183 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007184
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007185 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7186 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007187
Gilles Peskine8817f612018-12-18 00:18:46 +01007188 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007189
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007190 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7191 psa_set_key_algorithm( &attributes, alg );
7192 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007193 PSA_ASSERT( psa_import_key( &attributes,
7194 our_key_data->x, our_key_data->len,
7195 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007196
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007197 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007198 PSA_ASSERT( psa_key_derivation_key_agreement(
7199 &operation,
7200 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7201 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007202 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7203 {
7204 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007205 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007206 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007207 NULL, 0 ) );
7208 }
Gilles Peskine59685592018-09-18 12:11:34 +02007209
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007210 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007211 actual_output,
7212 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007213 ASSERT_COMPARE( actual_output, expected_output1->len,
7214 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007215 if( expected_output2->len != 0 )
7216 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007217 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007218 actual_output,
7219 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007220 ASSERT_COMPARE( actual_output, expected_output2->len,
7221 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007222 }
Gilles Peskine59685592018-09-18 12:11:34 +02007223
7224exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007225 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007226 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007227 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007228 mbedtls_free( actual_output );
7229}
7230/* END_CASE */
7231
7232/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007233void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007234{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007235 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007236 unsigned char *output = NULL;
7237 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007238 size_t i;
7239 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007240
Simon Butcher49f8e312020-03-03 15:51:50 +00007241 TEST_ASSERT( bytes_arg >= 0 );
7242
Gilles Peskine91892022021-02-08 19:50:26 +01007243 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007244 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007245
Gilles Peskine8817f612018-12-18 00:18:46 +01007246 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007247
Gilles Peskinea50d7392018-06-21 10:22:13 +02007248 /* Run several times, to ensure that every output byte will be
7249 * nonzero at least once with overwhelming probability
7250 * (2^(-8*number_of_runs)). */
7251 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007252 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007253 if( bytes != 0 )
7254 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007255 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007256
Gilles Peskinea50d7392018-06-21 10:22:13 +02007257 for( i = 0; i < bytes; i++ )
7258 {
7259 if( output[i] != 0 )
7260 ++changed[i];
7261 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007262 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007263
7264 /* Check that every byte was changed to nonzero at least once. This
7265 * validates that psa_generate_random is overwriting every byte of
7266 * the output buffer. */
7267 for( i = 0; i < bytes; i++ )
7268 {
7269 TEST_ASSERT( changed[i] != 0 );
7270 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007271
7272exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007273 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007274 mbedtls_free( output );
7275 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007276}
7277/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007278
7279/* BEGIN_CASE */
7280void generate_key( int type_arg,
7281 int bits_arg,
7282 int usage_arg,
7283 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007284 int expected_status_arg,
7285 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007286{
Ronald Cron5425a212020-08-04 14:58:35 +02007287 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007288 psa_key_type_t type = type_arg;
7289 psa_key_usage_t usage = usage_arg;
7290 size_t bits = bits_arg;
7291 psa_algorithm_t alg = alg_arg;
7292 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007294 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007295
Gilles Peskine8817f612018-12-18 00:18:46 +01007296 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007297
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007298 psa_set_key_usage_flags( &attributes, usage );
7299 psa_set_key_algorithm( &attributes, alg );
7300 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007301 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007302
7303 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007304 psa_status_t status = psa_generate_key( &attributes, &key );
7305
7306 if( is_large_key > 0 )
7307 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7308 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007309 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007310 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007311
7312 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007313 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007314 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7315 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007316
Gilles Peskine818ca122018-06-20 18:16:48 +02007317 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007318 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007319 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007320
7321exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007322 /*
7323 * Key attributes may have been returned by psa_get_key_attributes()
7324 * thus reset them as required.
7325 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007326 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007327
Ronald Cron5425a212020-08-04 14:58:35 +02007328 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007329 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007330}
7331/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007332
Ronald Cronee414c72021-03-18 18:50:08 +01007333/* 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 +02007334void generate_key_rsa( int bits_arg,
7335 data_t *e_arg,
7336 int expected_status_arg )
7337{
Ronald Cron5425a212020-08-04 14:58:35 +02007338 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007339 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007340 size_t bits = bits_arg;
7341 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7342 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7343 psa_status_t expected_status = expected_status_arg;
7344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7345 uint8_t *exported = NULL;
7346 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007347 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007348 size_t exported_length = SIZE_MAX;
7349 uint8_t *e_read_buffer = NULL;
7350 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007351 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007352 size_t e_read_length = SIZE_MAX;
7353
7354 if( e_arg->len == 0 ||
7355 ( e_arg->len == 3 &&
7356 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7357 {
7358 is_default_public_exponent = 1;
7359 e_read_size = 0;
7360 }
7361 ASSERT_ALLOC( e_read_buffer, e_read_size );
7362 ASSERT_ALLOC( exported, exported_size );
7363
7364 PSA_ASSERT( psa_crypto_init( ) );
7365
7366 psa_set_key_usage_flags( &attributes, usage );
7367 psa_set_key_algorithm( &attributes, alg );
7368 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7369 e_arg->x, e_arg->len ) );
7370 psa_set_key_bits( &attributes, bits );
7371
7372 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007373 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007374 if( expected_status != PSA_SUCCESS )
7375 goto exit;
7376
7377 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007378 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007379 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7380 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7381 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7382 e_read_buffer, e_read_size,
7383 &e_read_length ) );
7384 if( is_default_public_exponent )
7385 TEST_EQUAL( e_read_length, 0 );
7386 else
7387 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7388
7389 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007390 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007391 goto exit;
7392
7393 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007394 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007395 exported, exported_size,
7396 &exported_length ) );
7397 {
7398 uint8_t *p = exported;
7399 uint8_t *end = exported + exported_length;
7400 size_t len;
7401 /* RSAPublicKey ::= SEQUENCE {
7402 * modulus INTEGER, -- n
7403 * publicExponent INTEGER } -- e
7404 */
7405 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007406 MBEDTLS_ASN1_SEQUENCE |
7407 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007408 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007409 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7410 MBEDTLS_ASN1_INTEGER ) );
7411 if( len >= 1 && p[0] == 0 )
7412 {
7413 ++p;
7414 --len;
7415 }
7416 if( e_arg->len == 0 )
7417 {
7418 TEST_EQUAL( len, 3 );
7419 TEST_EQUAL( p[0], 1 );
7420 TEST_EQUAL( p[1], 0 );
7421 TEST_EQUAL( p[2], 1 );
7422 }
7423 else
7424 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7425 }
7426
7427exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007428 /*
7429 * Key attributes may have been returned by psa_get_key_attributes() or
7430 * set by psa_set_key_domain_parameters() thus reset them as required.
7431 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007432 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007433
Ronald Cron5425a212020-08-04 14:58:35 +02007434 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007435 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007436 mbedtls_free( e_read_buffer );
7437 mbedtls_free( exported );
7438}
7439/* END_CASE */
7440
Darryl Greend49a4992018-06-18 17:27:26 +01007441/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007442void persistent_key_load_key_from_storage( data_t *data,
7443 int type_arg, int bits_arg,
7444 int usage_flags_arg, int alg_arg,
7445 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007446{
Ronald Cron71016a92020-08-28 19:01:50 +02007447 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007449 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7450 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007451 psa_key_type_t type = type_arg;
7452 size_t bits = bits_arg;
7453 psa_key_usage_t usage_flags = usage_flags_arg;
7454 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007455 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007456 unsigned char *first_export = NULL;
7457 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007458 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007459 size_t first_exported_length;
7460 size_t second_exported_length;
7461
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007462 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7463 {
7464 ASSERT_ALLOC( first_export, export_size );
7465 ASSERT_ALLOC( second_export, export_size );
7466 }
Darryl Greend49a4992018-06-18 17:27:26 +01007467
Gilles Peskine8817f612018-12-18 00:18:46 +01007468 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007469
Gilles Peskinec87af662019-05-15 16:12:22 +02007470 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007471 psa_set_key_usage_flags( &attributes, usage_flags );
7472 psa_set_key_algorithm( &attributes, alg );
7473 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007474 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007475
Darryl Green0c6575a2018-11-07 16:05:30 +00007476 switch( generation_method )
7477 {
7478 case IMPORT_KEY:
7479 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007480 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007481 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007482 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007483
Darryl Green0c6575a2018-11-07 16:05:30 +00007484 case GENERATE_KEY:
7485 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007486 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007487 break;
7488
7489 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007490#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007491 {
7492 /* Create base key */
7493 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7494 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7495 psa_set_key_usage_flags( &base_attributes,
7496 PSA_KEY_USAGE_DERIVE );
7497 psa_set_key_algorithm( &base_attributes, derive_alg );
7498 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007499 PSA_ASSERT( psa_import_key( &base_attributes,
7500 data->x, data->len,
7501 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007502 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007503 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007504 PSA_ASSERT( psa_key_derivation_input_key(
7505 &operation,
7506 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007507 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007508 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007509 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007510 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7511 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007512 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007513 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007514 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007515 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007516 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007517#else
7518 TEST_ASSUME( ! "KDF not supported in this configuration" );
7519#endif
7520 break;
7521
7522 default:
7523 TEST_ASSERT( ! "generation_method not implemented in test" );
7524 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007525 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007526 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007527
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007528 /* Export the key if permitted by the key policy. */
7529 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7530 {
Ronald Cron5425a212020-08-04 14:58:35 +02007531 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007532 first_export, export_size,
7533 &first_exported_length ) );
7534 if( generation_method == IMPORT_KEY )
7535 ASSERT_COMPARE( data->x, data->len,
7536 first_export, first_exported_length );
7537 }
Darryl Greend49a4992018-06-18 17:27:26 +01007538
7539 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007540 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007541 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007542 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007543
Darryl Greend49a4992018-06-18 17:27:26 +01007544 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007545 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007546 TEST_ASSERT( mbedtls_svc_key_id_equal(
7547 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007548 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7549 PSA_KEY_LIFETIME_PERSISTENT );
7550 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7551 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007552 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007553 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007554 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007555
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007556 /* Export the key again if permitted by the key policy. */
7557 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007558 {
Ronald Cron5425a212020-08-04 14:58:35 +02007559 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007560 second_export, export_size,
7561 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007562 ASSERT_COMPARE( first_export, first_exported_length,
7563 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007564 }
7565
7566 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007567 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007568 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007569
7570exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007571 /*
7572 * Key attributes may have been returned by psa_get_key_attributes()
7573 * thus reset them as required.
7574 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007575 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007576
Darryl Greend49a4992018-06-18 17:27:26 +01007577 mbedtls_free( first_export );
7578 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007579 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007580 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007581 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007582 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007583}
7584/* END_CASE */