blob: e31b6b21ccb1168e9b3b788ff6a2cf987eae51d2 [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;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001318 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001319 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001320 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001321 unsigned char nonce[16] = {0};
1322 size_t nonce_length = nonce_length_arg;
1323 unsigned char tag[16];
1324 size_t tag_length = tag_length_arg;
1325 size_t output_length;
1326
1327 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1328 TEST_ASSERT( tag_length <= sizeof( tag ) );
1329
Gilles Peskine8817f612018-12-18 00:18:46 +01001330 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001331
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001332 psa_set_key_usage_flags( &attributes, policy_usage );
1333 psa_set_key_algorithm( &attributes, policy_alg );
1334 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001335
Gilles Peskine049c7532019-05-15 20:22:09 +02001336 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001337 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001338
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001339 /* Check if no key usage implication is done */
1340 TEST_EQUAL( policy_usage,
1341 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001342
Ronald Cron5425a212020-08-04 14:58:35 +02001343 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001344 nonce, nonce_length,
1345 NULL, 0,
1346 NULL, 0,
1347 tag, tag_length,
1348 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001349 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1350 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001351 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001352 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001353
1354 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001355 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001356 nonce, nonce_length,
1357 NULL, 0,
1358 tag, tag_length,
1359 NULL, 0,
1360 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001361 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1362 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1363 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001364 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001365 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001366 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001367
1368exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001369 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001370 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001371}
1372/* END_CASE */
1373
1374/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001375void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001376 int policy_alg,
1377 int key_type,
1378 data_t *key_data,
1379 int exercise_alg )
1380{
Ronald Cron5425a212020-08-04 14:58:35 +02001381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001383 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001384 psa_status_t status;
1385 size_t key_bits;
1386 size_t buffer_length;
1387 unsigned char *buffer = NULL;
1388 size_t output_length;
1389
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001391
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001392 psa_set_key_usage_flags( &attributes, policy_usage );
1393 psa_set_key_algorithm( &attributes, policy_alg );
1394 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001395
Gilles Peskine049c7532019-05-15 20:22:09 +02001396 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001397 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001398
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001399 /* Check if no key usage implication is done */
1400 TEST_EQUAL( policy_usage,
1401 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001402
Ronald Cron5425a212020-08-04 14:58:35 +02001403 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001404 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001405 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1406 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001407 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001408
Ronald Cron5425a212020-08-04 14:58:35 +02001409 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001410 NULL, 0,
1411 NULL, 0,
1412 buffer, buffer_length,
1413 &output_length );
1414 if( policy_alg == exercise_alg &&
1415 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001416 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001417 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001418 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001419
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001420 if( buffer_length != 0 )
1421 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001422 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001423 buffer, buffer_length,
1424 NULL, 0,
1425 buffer, buffer_length,
1426 &output_length );
1427 if( policy_alg == exercise_alg &&
1428 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001429 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001430 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001431 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001432
1433exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001434 /*
1435 * Key attributes may have been returned by psa_get_key_attributes()
1436 * thus reset them as required.
1437 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001438 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001439
1440 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001441 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001442 mbedtls_free( buffer );
1443}
1444/* END_CASE */
1445
1446/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001447void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001448 int policy_alg,
1449 int key_type,
1450 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001451 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001452 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001453 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001454{
Ronald Cron5425a212020-08-04 14:58:35 +02001455 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001456 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001457 psa_key_usage_t policy_usage = policy_usage_arg;
1458 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001459 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001460 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1461 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1462 * compatible with the policy and `payload_length_arg` is supposed to be
1463 * a valid input length to sign. If `payload_length_arg <= 0`,
1464 * `exercise_alg` is supposed to be forbidden by the policy. */
1465 int compatible_alg = payload_length_arg > 0;
1466 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001467 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001468 size_t signature_length;
1469
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001470 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001471 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001472 TEST_EQUAL( expected_usage,
1473 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001474
Gilles Peskine8817f612018-12-18 00:18:46 +01001475 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001477 psa_set_key_usage_flags( &attributes, policy_usage );
1478 psa_set_key_algorithm( &attributes, policy_alg );
1479 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001480
Gilles Peskine049c7532019-05-15 20:22:09 +02001481 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001482 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001483
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001484 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1485
Ronald Cron5425a212020-08-04 14:58:35 +02001486 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001487 payload, payload_length,
1488 signature, sizeof( signature ),
1489 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001490 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001491 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001492 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001493 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001494
1495 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001496 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001497 payload, payload_length,
1498 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001499 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001500 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001501 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001502 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001503
Gilles Peskinef7b41372021-09-22 16:15:05 +02001504 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001505 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001506 {
1507 status = psa_sign_message( key, exercise_alg,
1508 payload, payload_length,
1509 signature, sizeof( signature ),
1510 &signature_length );
1511 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1512 PSA_ASSERT( status );
1513 else
1514 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1515
1516 memset( signature, 0, sizeof( signature ) );
1517 status = psa_verify_message( key, exercise_alg,
1518 payload, payload_length,
1519 signature, sizeof( signature ) );
1520 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1521 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1522 else
1523 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1524 }
1525
Gilles Peskined5b33222018-06-18 22:20:03 +02001526exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001527 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001528 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001529}
1530/* END_CASE */
1531
Janos Follathba3fab92019-06-11 14:50:16 +01001532/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001533void derive_key_policy( int policy_usage,
1534 int policy_alg,
1535 int key_type,
1536 data_t *key_data,
1537 int exercise_alg )
1538{
Ronald Cron5425a212020-08-04 14:58:35 +02001539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001541 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001542 psa_status_t status;
1543
Gilles Peskine8817f612018-12-18 00:18:46 +01001544 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001545
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001546 psa_set_key_usage_flags( &attributes, policy_usage );
1547 psa_set_key_algorithm( &attributes, policy_alg );
1548 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001549
Gilles Peskine049c7532019-05-15 20:22:09 +02001550 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001551 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001552
Janos Follathba3fab92019-06-11 14:50:16 +01001553 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1554
1555 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1556 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001557 {
Janos Follathba3fab92019-06-11 14:50:16 +01001558 PSA_ASSERT( psa_key_derivation_input_bytes(
1559 &operation,
1560 PSA_KEY_DERIVATION_INPUT_SEED,
1561 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001562 }
Janos Follathba3fab92019-06-11 14:50:16 +01001563
1564 status = psa_key_derivation_input_key( &operation,
1565 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001566 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001567
Gilles Peskineea0fb492018-07-12 17:17:20 +02001568 if( policy_alg == exercise_alg &&
1569 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001570 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001571 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001572 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001573
1574exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001575 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001576 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001577 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001578}
1579/* END_CASE */
1580
1581/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001582void agreement_key_policy( int policy_usage,
1583 int policy_alg,
1584 int key_type_arg,
1585 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001586 int exercise_alg,
1587 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001588{
Ronald Cron5425a212020-08-04 14:58:35 +02001589 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001590 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001591 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001592 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001593 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001594 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001595
Gilles Peskine8817f612018-12-18 00:18:46 +01001596 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001597
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001598 psa_set_key_usage_flags( &attributes, policy_usage );
1599 psa_set_key_algorithm( &attributes, policy_alg );
1600 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001601
Gilles Peskine049c7532019-05-15 20:22:09 +02001602 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001603 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001604
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001605 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001606 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001607
Steven Cooremance48e852020-10-05 16:02:45 +02001608 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001609
1610exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001611 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001612 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001613 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001614}
1615/* END_CASE */
1616
1617/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001618void key_policy_alg2( int key_type_arg, data_t *key_data,
1619 int usage_arg, int alg_arg, int alg2_arg )
1620{
Ronald Cron5425a212020-08-04 14:58:35 +02001621 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001622 psa_key_type_t key_type = key_type_arg;
1623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1624 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1625 psa_key_usage_t usage = usage_arg;
1626 psa_algorithm_t alg = alg_arg;
1627 psa_algorithm_t alg2 = alg2_arg;
1628
1629 PSA_ASSERT( psa_crypto_init( ) );
1630
1631 psa_set_key_usage_flags( &attributes, usage );
1632 psa_set_key_algorithm( &attributes, alg );
1633 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1634 psa_set_key_type( &attributes, key_type );
1635 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001636 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001637
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001638 /* Update the usage flags to obtain implicit usage flags */
1639 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001640 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001641 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1642 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1643 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1644
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001645 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001646 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001647 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001648 goto exit;
1649
1650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001651 /*
1652 * Key attributes may have been returned by psa_get_key_attributes()
1653 * thus reset them as required.
1654 */
1655 psa_reset_key_attributes( &got_attributes );
1656
Ronald Cron5425a212020-08-04 14:58:35 +02001657 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001658 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001659}
1660/* END_CASE */
1661
1662/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001663void raw_agreement_key_policy( int policy_usage,
1664 int policy_alg,
1665 int key_type_arg,
1666 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001667 int exercise_alg,
1668 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001669{
Ronald Cron5425a212020-08-04 14:58:35 +02001670 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001672 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001673 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001674 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001675 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001676
1677 PSA_ASSERT( psa_crypto_init( ) );
1678
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001679 psa_set_key_usage_flags( &attributes, policy_usage );
1680 psa_set_key_algorithm( &attributes, policy_alg );
1681 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001682
Gilles Peskine049c7532019-05-15 20:22:09 +02001683 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001684 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001685
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001686 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001687
Steven Cooremance48e852020-10-05 16:02:45 +02001688 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001689
1690exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001691 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001692 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001693 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001694}
1695/* END_CASE */
1696
1697/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001698void copy_success( int source_usage_arg,
1699 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301700 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001701 int type_arg, data_t *material,
1702 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001703 int target_usage_arg,
1704 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301705 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001706 int expected_usage_arg,
1707 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001708{
Gilles Peskineca25db92019-04-19 11:43:08 +02001709 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1710 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001711 psa_key_usage_t expected_usage = expected_usage_arg;
1712 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001713 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301714 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1715 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001716 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1717 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001718 uint8_t *export_buffer = NULL;
1719
Gilles Peskine57ab7212019-01-28 13:03:09 +01001720 PSA_ASSERT( psa_crypto_init( ) );
1721
Gilles Peskineca25db92019-04-19 11:43:08 +02001722 /* Prepare the source key. */
1723 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1724 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001725 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001726 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301727 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001728 PSA_ASSERT( psa_import_key( &source_attributes,
1729 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001730 &source_key ) );
1731 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001732
Gilles Peskineca25db92019-04-19 11:43:08 +02001733 /* Prepare the target attributes. */
1734 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001735 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001736 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001737 }
Archana8a180362021-07-05 02:18:48 +05301738 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001739
Gilles Peskineca25db92019-04-19 11:43:08 +02001740 if( target_usage_arg != -1 )
1741 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1742 if( target_alg_arg != -1 )
1743 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001744 if( target_alg2_arg != -1 )
1745 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001746
Archana8a180362021-07-05 02:18:48 +05301747
Gilles Peskine57ab7212019-01-28 13:03:09 +01001748 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001749 PSA_ASSERT( psa_copy_key( source_key,
1750 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001751
1752 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001753 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001754
1755 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001756 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001757 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1758 psa_get_key_type( &target_attributes ) );
1759 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1760 psa_get_key_bits( &target_attributes ) );
1761 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1762 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001763 TEST_EQUAL( expected_alg2,
1764 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001765 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1766 {
1767 size_t length;
1768 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001769 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001770 material->len, &length ) );
1771 ASSERT_COMPARE( material->x, material->len,
1772 export_buffer, length );
1773 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001774
Archana8a180362021-07-05 02:18:48 +05301775 if( !psa_key_lifetime_is_external( target_lifetime ) )
1776 {
1777 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1778 goto exit;
1779 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1780 goto exit;
1781 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001782
Ronald Cron5425a212020-08-04 14:58:35 +02001783 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001784
1785exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001786 /*
1787 * Source and target key attributes may have been returned by
1788 * psa_get_key_attributes() thus reset them as required.
1789 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001790 psa_reset_key_attributes( &source_attributes );
1791 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001792
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001793 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001794 mbedtls_free( export_buffer );
1795}
1796/* END_CASE */
1797
1798/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001799void copy_fail( int source_usage_arg,
1800 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301801 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001802 int type_arg, data_t *material,
1803 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001804 int target_usage_arg,
1805 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001806 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001807 int expected_status_arg )
1808{
1809 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1810 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001811 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1812 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001813 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001814
1815 PSA_ASSERT( psa_crypto_init( ) );
1816
1817 /* Prepare the source key. */
1818 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1819 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001820 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001821 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301822 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001823 PSA_ASSERT( psa_import_key( &source_attributes,
1824 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001825 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001826
1827 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001828 psa_set_key_id( &target_attributes, key_id );
1829 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001830 psa_set_key_type( &target_attributes, target_type_arg );
1831 psa_set_key_bits( &target_attributes, target_bits_arg );
1832 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1833 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001834 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001835
1836 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001837 TEST_EQUAL( psa_copy_key( source_key,
1838 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001839 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001840
Ronald Cron5425a212020-08-04 14:58:35 +02001841 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001842
Gilles Peskine4a644642019-05-03 17:14:08 +02001843exit:
1844 psa_reset_key_attributes( &source_attributes );
1845 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001846 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001847}
1848/* END_CASE */
1849
1850/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001851void hash_operation_init( )
1852{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001853 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001854 /* Test each valid way of initializing the object, except for `= {0}`, as
1855 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1856 * though it's OK by the C standard. We could test for this, but we'd need
1857 * to supress the Clang warning for the test. */
1858 psa_hash_operation_t func = psa_hash_operation_init( );
1859 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1860 psa_hash_operation_t zero;
1861
1862 memset( &zero, 0, sizeof( zero ) );
1863
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001864 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001865 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1866 PSA_ERROR_BAD_STATE );
1867 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1868 PSA_ERROR_BAD_STATE );
1869 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1870 PSA_ERROR_BAD_STATE );
1871
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001872 /* A default hash operation should be abortable without error. */
1873 PSA_ASSERT( psa_hash_abort( &func ) );
1874 PSA_ASSERT( psa_hash_abort( &init ) );
1875 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001876}
1877/* END_CASE */
1878
1879/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001880void hash_setup( int alg_arg,
1881 int expected_status_arg )
1882{
1883 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01001884 uint8_t *output = NULL;
1885 size_t output_size = 0;
1886 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001887 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001888 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001889 psa_status_t status;
1890
Gilles Peskine8817f612018-12-18 00:18:46 +01001891 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001892
Neil Armstrongedb20862022-02-07 15:47:44 +01001893 /* Hash Setup, one-shot */
1894 output_size = PSA_HASH_LENGTH(alg);
1895 ASSERT_ALLOC( output, output_size );
1896
1897 status = psa_hash_compute( alg, NULL, 0,
1898 output, output_size, &output_length );
1899 TEST_EQUAL( status, expected_status );
1900
1901 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001902 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001903 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001904
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001905 /* Whether setup succeeded or failed, abort must succeed. */
1906 PSA_ASSERT( psa_hash_abort( &operation ) );
1907
1908 /* If setup failed, reproduce the failure, so as to
1909 * test the resulting state of the operation object. */
1910 if( status != PSA_SUCCESS )
1911 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1912
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001913 /* Now the operation object should be reusable. */
1914#if defined(KNOWN_SUPPORTED_HASH_ALG)
1915 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1916 PSA_ASSERT( psa_hash_abort( &operation ) );
1917#endif
1918
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001919exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01001920 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001921 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001922}
1923/* END_CASE */
1924
1925/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001926void hash_compute_fail( int alg_arg, data_t *input,
1927 int output_size_arg, int expected_status_arg )
1928{
1929 psa_algorithm_t alg = alg_arg;
1930 uint8_t *output = NULL;
1931 size_t output_size = output_size_arg;
1932 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001933 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01001934 psa_status_t expected_status = expected_status_arg;
1935 psa_status_t status;
1936
1937 ASSERT_ALLOC( output, output_size );
1938
1939 PSA_ASSERT( psa_crypto_init( ) );
1940
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001941 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001942 status = psa_hash_compute( alg, input->x, input->len,
1943 output, output_size, &output_length );
1944 TEST_EQUAL( status, expected_status );
1945 TEST_ASSERT( output_length <= output_size );
1946
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001947 /* Hash Compute, multi-part */
1948 status = psa_hash_setup( &operation, alg );
1949 if( status == PSA_SUCCESS )
1950 {
1951 status = psa_hash_update( &operation, input->x, input->len );
1952 if( status == PSA_SUCCESS )
1953 {
1954 status = psa_hash_finish( &operation, output, output_size,
1955 &output_length );
1956 if( status == PSA_SUCCESS )
1957 TEST_ASSERT( output_length <= output_size );
1958 else
1959 TEST_EQUAL( status, expected_status );
1960 }
1961 else
1962 {
1963 TEST_EQUAL( status, expected_status );
1964 }
1965 }
1966 else
1967 {
1968 TEST_EQUAL( status, expected_status );
1969 }
1970
Gilles Peskine0a749c82019-11-28 19:33:58 +01001971exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001972 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001973 mbedtls_free( output );
1974 PSA_DONE( );
1975}
1976/* END_CASE */
1977
1978/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001979void hash_compare_fail( int alg_arg, data_t *input,
1980 data_t *reference_hash,
1981 int expected_status_arg )
1982{
1983 psa_algorithm_t alg = alg_arg;
1984 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01001985 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01001986 psa_status_t status;
1987
1988 PSA_ASSERT( psa_crypto_init( ) );
1989
Neil Armstrong55a1be12022-02-07 11:23:20 +01001990 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01001991 status = psa_hash_compare( alg, input->x, input->len,
1992 reference_hash->x, reference_hash->len );
1993 TEST_EQUAL( status, expected_status );
1994
Neil Armstrong55a1be12022-02-07 11:23:20 +01001995 /* Hash Compare, multi-part */
1996 status = psa_hash_setup( &operation, alg );
1997 if( status == PSA_SUCCESS )
1998 {
1999 status = psa_hash_update( &operation, input->x, input->len );
2000 if( status == PSA_SUCCESS )
2001 {
2002 status = psa_hash_verify( &operation, reference_hash->x,
2003 reference_hash->len );
2004 TEST_EQUAL( status, expected_status );
2005 }
2006 else
2007 {
2008 TEST_EQUAL( status, expected_status );
2009 }
2010 }
2011 else
2012 {
2013 TEST_EQUAL( status, expected_status );
2014 }
2015
Gilles Peskine88e08462020-01-28 20:43:00 +01002016exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002017 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002018 PSA_DONE( );
2019}
2020/* END_CASE */
2021
2022/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002023void hash_compute_compare( int alg_arg, data_t *input,
2024 data_t *expected_output )
2025{
2026 psa_algorithm_t alg = alg_arg;
2027 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2028 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002029 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002030 size_t i;
2031
2032 PSA_ASSERT( psa_crypto_init( ) );
2033
Neil Armstrongca30a002022-02-07 11:40:23 +01002034 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002035 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002036 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002037 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002038 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002039 ASSERT_COMPARE( output, output_length,
2040 expected_output->x, expected_output->len );
2041
Neil Armstrongca30a002022-02-07 11:40:23 +01002042 /* Compute with tight buffer, multi-part */
2043 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2044 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2045 PSA_ASSERT( psa_hash_finish( &operation, output,
2046 PSA_HASH_LENGTH( alg ),
2047 &output_length ) );
2048 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2049 ASSERT_COMPARE( output, output_length,
2050 expected_output->x, expected_output->len );
2051
2052 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002053 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2054 output, sizeof( output ),
2055 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002056 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002057 ASSERT_COMPARE( output, output_length,
2058 expected_output->x, expected_output->len );
2059
Neil Armstrongca30a002022-02-07 11:40:23 +01002060 /* Compute with larger buffer, multi-part */
2061 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2062 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2063 PSA_ASSERT( psa_hash_finish( &operation, output,
2064 sizeof( output ), &output_length ) );
2065 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2066 ASSERT_COMPARE( output, output_length,
2067 expected_output->x, expected_output->len );
2068
2069 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002070 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2071 output, output_length ) );
2072
Neil Armstrongca30a002022-02-07 11:40:23 +01002073 /* Compare with correct hash, multi-part */
2074 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2075 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2076 PSA_ASSERT( psa_hash_verify( &operation, output,
2077 output_length ) );
2078
2079 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002080 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2081 output, output_length + 1 ),
2082 PSA_ERROR_INVALID_SIGNATURE );
2083
Neil Armstrongca30a002022-02-07 11:40:23 +01002084 /* Compare with trailing garbage, multi-part */
2085 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2086 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2087 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2088 PSA_ERROR_INVALID_SIGNATURE );
2089
2090 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002091 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2092 output, output_length - 1 ),
2093 PSA_ERROR_INVALID_SIGNATURE );
2094
Neil Armstrongca30a002022-02-07 11:40:23 +01002095 /* Compare with truncated hash, multi-part */
2096 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2097 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2098 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2099 PSA_ERROR_INVALID_SIGNATURE );
2100
Gilles Peskine0a749c82019-11-28 19:33:58 +01002101 /* Compare with corrupted value */
2102 for( i = 0; i < output_length; i++ )
2103 {
Chris Jones9634bb12021-01-20 15:56:42 +00002104 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002105 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002106
2107 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002108 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2109 output, output_length ),
2110 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002111
2112 /* Multi-Part */
2113 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2114 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2115 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2116 PSA_ERROR_INVALID_SIGNATURE );
2117
Gilles Peskine0a749c82019-11-28 19:33:58 +01002118 output[i] ^= 1;
2119 }
2120
2121exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002122 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002123 PSA_DONE( );
2124}
2125/* END_CASE */
2126
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002127/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002128void hash_bad_order( )
2129{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002130 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002131 unsigned char input[] = "";
2132 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002133 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002134 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2135 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2136 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002137 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002138 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002139 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002140
Gilles Peskine8817f612018-12-18 00:18:46 +01002141 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002142
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002143 /* Call setup twice in a row. */
2144 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002145 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002146 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2147 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002148 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002149 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002150 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002151
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002152 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002153 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002154 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002155 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002156
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002157 /* Check that update calls abort on error. */
2158 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002159 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002160 ASSERT_OPERATION_IS_ACTIVE( operation );
2161 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2162 PSA_ERROR_BAD_STATE );
2163 ASSERT_OPERATION_IS_INACTIVE( operation );
2164 PSA_ASSERT( psa_hash_abort( &operation ) );
2165 ASSERT_OPERATION_IS_INACTIVE( operation );
2166
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002167 /* Call update after finish. */
2168 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2169 PSA_ASSERT( psa_hash_finish( &operation,
2170 hash, sizeof( hash ), &hash_len ) );
2171 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002172 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002173 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002174
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002175 /* Call verify without calling setup beforehand. */
2176 TEST_EQUAL( psa_hash_verify( &operation,
2177 valid_hash, sizeof( valid_hash ) ),
2178 PSA_ERROR_BAD_STATE );
2179 PSA_ASSERT( psa_hash_abort( &operation ) );
2180
2181 /* Call verify after finish. */
2182 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2183 PSA_ASSERT( psa_hash_finish( &operation,
2184 hash, sizeof( hash ), &hash_len ) );
2185 TEST_EQUAL( psa_hash_verify( &operation,
2186 valid_hash, sizeof( valid_hash ) ),
2187 PSA_ERROR_BAD_STATE );
2188 PSA_ASSERT( psa_hash_abort( &operation ) );
2189
2190 /* Call verify twice in a row. */
2191 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002192 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002193 PSA_ASSERT( psa_hash_verify( &operation,
2194 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002195 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002196 TEST_EQUAL( psa_hash_verify( &operation,
2197 valid_hash, sizeof( valid_hash ) ),
2198 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002199 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002200 PSA_ASSERT( psa_hash_abort( &operation ) );
2201
2202 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002203 TEST_EQUAL( psa_hash_finish( &operation,
2204 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002205 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002206 PSA_ASSERT( psa_hash_abort( &operation ) );
2207
2208 /* Call finish twice in a row. */
2209 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2210 PSA_ASSERT( psa_hash_finish( &operation,
2211 hash, sizeof( hash ), &hash_len ) );
2212 TEST_EQUAL( psa_hash_finish( &operation,
2213 hash, sizeof( hash ), &hash_len ),
2214 PSA_ERROR_BAD_STATE );
2215 PSA_ASSERT( psa_hash_abort( &operation ) );
2216
2217 /* Call finish after calling verify. */
2218 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2219 PSA_ASSERT( psa_hash_verify( &operation,
2220 valid_hash, sizeof( valid_hash ) ) );
2221 TEST_EQUAL( psa_hash_finish( &operation,
2222 hash, sizeof( hash ), &hash_len ),
2223 PSA_ERROR_BAD_STATE );
2224 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002225
2226exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002227 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002228}
2229/* END_CASE */
2230
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002231/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002232void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002233{
2234 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002235 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2236 * appended to it */
2237 unsigned char hash[] = {
2238 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2239 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2240 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002241 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002242 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002243
Gilles Peskine8817f612018-12-18 00:18:46 +01002244 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002245
itayzafrir27e69452018-11-01 14:26:34 +02002246 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002247 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002248 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002249 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002250 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002251 ASSERT_OPERATION_IS_INACTIVE( operation );
2252 PSA_ASSERT( psa_hash_abort( &operation ) );
2253 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002254
itayzafrir27e69452018-11-01 14:26:34 +02002255 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002256 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002257 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002258 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002259
itayzafrir27e69452018-11-01 14:26:34 +02002260 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002261 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002262 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002263 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002264
itayzafrirec93d302018-10-18 18:01:10 +03002265exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002266 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002267}
2268/* END_CASE */
2269
Ronald Cronee414c72021-03-18 18:50:08 +01002270/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002271void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002272{
2273 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002274 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002275 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002276 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002277 size_t hash_len;
2278
Gilles Peskine8817f612018-12-18 00:18:46 +01002279 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002280
itayzafrir58028322018-10-25 10:22:01 +03002281 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002282 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002283 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002284 hash, expected_size - 1, &hash_len ),
2285 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002286
2287exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002288 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002289}
2290/* END_CASE */
2291
Ronald Cronee414c72021-03-18 18:50:08 +01002292/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002293void hash_clone_source_state( )
2294{
2295 psa_algorithm_t alg = PSA_ALG_SHA_256;
2296 unsigned char hash[PSA_HASH_MAX_SIZE];
2297 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2298 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2299 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2300 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2301 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2302 size_t hash_len;
2303
2304 PSA_ASSERT( psa_crypto_init( ) );
2305 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2306
2307 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2308 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2309 PSA_ASSERT( psa_hash_finish( &op_finished,
2310 hash, sizeof( hash ), &hash_len ) );
2311 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2312 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2313
2314 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2315 PSA_ERROR_BAD_STATE );
2316
2317 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2318 PSA_ASSERT( psa_hash_finish( &op_init,
2319 hash, sizeof( hash ), &hash_len ) );
2320 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2321 PSA_ASSERT( psa_hash_finish( &op_finished,
2322 hash, sizeof( hash ), &hash_len ) );
2323 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2324 PSA_ASSERT( psa_hash_finish( &op_aborted,
2325 hash, sizeof( hash ), &hash_len ) );
2326
2327exit:
2328 psa_hash_abort( &op_source );
2329 psa_hash_abort( &op_init );
2330 psa_hash_abort( &op_setup );
2331 psa_hash_abort( &op_finished );
2332 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002333 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002334}
2335/* END_CASE */
2336
Ronald Cronee414c72021-03-18 18:50:08 +01002337/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002338void hash_clone_target_state( )
2339{
2340 psa_algorithm_t alg = PSA_ALG_SHA_256;
2341 unsigned char hash[PSA_HASH_MAX_SIZE];
2342 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2343 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2344 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2345 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2346 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2347 size_t hash_len;
2348
2349 PSA_ASSERT( psa_crypto_init( ) );
2350
2351 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2352 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2353 PSA_ASSERT( psa_hash_finish( &op_finished,
2354 hash, sizeof( hash ), &hash_len ) );
2355 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2356 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2357
2358 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2359 PSA_ASSERT( psa_hash_finish( &op_target,
2360 hash, sizeof( hash ), &hash_len ) );
2361
2362 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2363 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2364 PSA_ERROR_BAD_STATE );
2365 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2366 PSA_ERROR_BAD_STATE );
2367
2368exit:
2369 psa_hash_abort( &op_target );
2370 psa_hash_abort( &op_init );
2371 psa_hash_abort( &op_setup );
2372 psa_hash_abort( &op_finished );
2373 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002374 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002375}
2376/* END_CASE */
2377
itayzafrir58028322018-10-25 10:22:01 +03002378/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002379void mac_operation_init( )
2380{
Jaeden Amero252ef282019-02-15 14:05:35 +00002381 const uint8_t input[1] = { 0 };
2382
Jaeden Amero769ce272019-01-04 11:48:03 +00002383 /* Test each valid way of initializing the object, except for `= {0}`, as
2384 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2385 * though it's OK by the C standard. We could test for this, but we'd need
2386 * to supress the Clang warning for the test. */
2387 psa_mac_operation_t func = psa_mac_operation_init( );
2388 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2389 psa_mac_operation_t zero;
2390
2391 memset( &zero, 0, sizeof( zero ) );
2392
Jaeden Amero252ef282019-02-15 14:05:35 +00002393 /* A freshly-initialized MAC operation should not be usable. */
2394 TEST_EQUAL( psa_mac_update( &func,
2395 input, sizeof( input ) ),
2396 PSA_ERROR_BAD_STATE );
2397 TEST_EQUAL( psa_mac_update( &init,
2398 input, sizeof( input ) ),
2399 PSA_ERROR_BAD_STATE );
2400 TEST_EQUAL( psa_mac_update( &zero,
2401 input, sizeof( input ) ),
2402 PSA_ERROR_BAD_STATE );
2403
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002404 /* A default MAC operation should be abortable without error. */
2405 PSA_ASSERT( psa_mac_abort( &func ) );
2406 PSA_ASSERT( psa_mac_abort( &init ) );
2407 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002408}
2409/* END_CASE */
2410
2411/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002412void mac_setup( int key_type_arg,
2413 data_t *key,
2414 int alg_arg,
2415 int expected_status_arg )
2416{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002417 psa_key_type_t key_type = key_type_arg;
2418 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002419 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002420 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002421 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2422#if defined(KNOWN_SUPPORTED_MAC_ALG)
2423 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2424#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002425
Gilles Peskine8817f612018-12-18 00:18:46 +01002426 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002427
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002428 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2429 &operation, &status ) )
2430 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002431 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002432
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002433 /* The operation object should be reusable. */
2434#if defined(KNOWN_SUPPORTED_MAC_ALG)
2435 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2436 smoke_test_key_data,
2437 sizeof( smoke_test_key_data ),
2438 KNOWN_SUPPORTED_MAC_ALG,
2439 &operation, &status ) )
2440 goto exit;
2441 TEST_EQUAL( status, PSA_SUCCESS );
2442#endif
2443
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002445 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002446}
2447/* END_CASE */
2448
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002449/* 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 +00002450void mac_bad_order( )
2451{
Ronald Cron5425a212020-08-04 14:58:35 +02002452 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002453 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2454 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002455 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002456 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2457 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2458 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002459 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002460 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2461 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2462 size_t sign_mac_length = 0;
2463 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2464 const uint8_t verify_mac[] = {
2465 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2466 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2467 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2468
2469 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002470 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002471 psa_set_key_algorithm( &attributes, alg );
2472 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002473
Ronald Cron5425a212020-08-04 14:58:35 +02002474 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2475 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476
Jaeden Amero252ef282019-02-15 14:05:35 +00002477 /* Call update without calling setup beforehand. */
2478 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2479 PSA_ERROR_BAD_STATE );
2480 PSA_ASSERT( psa_mac_abort( &operation ) );
2481
2482 /* Call sign finish without calling setup beforehand. */
2483 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2484 &sign_mac_length),
2485 PSA_ERROR_BAD_STATE );
2486 PSA_ASSERT( psa_mac_abort( &operation ) );
2487
2488 /* Call verify finish without calling setup beforehand. */
2489 TEST_EQUAL( psa_mac_verify_finish( &operation,
2490 verify_mac, sizeof( verify_mac ) ),
2491 PSA_ERROR_BAD_STATE );
2492 PSA_ASSERT( psa_mac_abort( &operation ) );
2493
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002494 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002495 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002496 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002497 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002498 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002499 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002500 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002501 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002502
Jaeden Amero252ef282019-02-15 14:05:35 +00002503 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002504 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002505 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2506 PSA_ASSERT( psa_mac_sign_finish( &operation,
2507 sign_mac, sizeof( sign_mac ),
2508 &sign_mac_length ) );
2509 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2510 PSA_ERROR_BAD_STATE );
2511 PSA_ASSERT( psa_mac_abort( &operation ) );
2512
2513 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002514 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002515 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2516 PSA_ASSERT( psa_mac_verify_finish( &operation,
2517 verify_mac, sizeof( verify_mac ) ) );
2518 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2519 PSA_ERROR_BAD_STATE );
2520 PSA_ASSERT( psa_mac_abort( &operation ) );
2521
2522 /* Call sign finish twice in a row. */
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_sign_finish( &operation,
2529 sign_mac, sizeof( sign_mac ),
2530 &sign_mac_length ),
2531 PSA_ERROR_BAD_STATE );
2532 PSA_ASSERT( psa_mac_abort( &operation ) );
2533
2534 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002535 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002536 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2537 PSA_ASSERT( psa_mac_verify_finish( &operation,
2538 verify_mac, sizeof( verify_mac ) ) );
2539 TEST_EQUAL( psa_mac_verify_finish( &operation,
2540 verify_mac, sizeof( verify_mac ) ),
2541 PSA_ERROR_BAD_STATE );
2542 PSA_ASSERT( psa_mac_abort( &operation ) );
2543
2544 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002545 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002546 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002547 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002548 TEST_EQUAL( psa_mac_verify_finish( &operation,
2549 verify_mac, sizeof( verify_mac ) ),
2550 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002551 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002552 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002553 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002554
2555 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002556 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002557 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002558 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002559 TEST_EQUAL( psa_mac_sign_finish( &operation,
2560 sign_mac, sizeof( sign_mac ),
2561 &sign_mac_length ),
2562 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002563 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002564 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002565 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002566
Ronald Cron5425a212020-08-04 14:58:35 +02002567 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002568
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002569exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002570 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002571}
2572/* END_CASE */
2573
2574/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002575void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002576 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002577 int alg_arg,
2578 data_t *input,
2579 data_t *expected_mac )
2580{
Ronald Cron5425a212020-08-04 14:58:35 +02002581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002582 psa_key_type_t key_type = key_type_arg;
2583 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002584 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002586 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002587 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002588 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002589 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002590 const size_t output_sizes_to_test[] = {
2591 0,
2592 1,
2593 expected_mac->len - 1,
2594 expected_mac->len,
2595 expected_mac->len + 1,
2596 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002597
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002598 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002599 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002600 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002601
Gilles Peskine8817f612018-12-18 00:18:46 +01002602 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002603
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002604 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002605 psa_set_key_algorithm( &attributes, alg );
2606 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002607
Ronald Cron5425a212020-08-04 14:58:35 +02002608 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2609 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002610
Gilles Peskine8b356b52020-08-25 23:44:59 +02002611 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2612 {
2613 const size_t output_size = output_sizes_to_test[i];
2614 psa_status_t expected_status =
2615 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2616 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002617
Chris Jones9634bb12021-01-20 15:56:42 +00002618 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002619 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002620
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002621 /* Calculate the MAC, one-shot case. */
2622 TEST_EQUAL( psa_mac_compute( key, alg,
2623 input->x, input->len,
2624 actual_mac, output_size, &mac_length ),
2625 expected_status );
2626 if( expected_status == PSA_SUCCESS )
2627 {
2628 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2629 actual_mac, mac_length );
2630 }
2631
2632 if( output_size > 0 )
2633 memset( actual_mac, 0, output_size );
2634
2635 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002636 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002637 PSA_ASSERT( psa_mac_update( &operation,
2638 input->x, input->len ) );
2639 TEST_EQUAL( psa_mac_sign_finish( &operation,
2640 actual_mac, output_size,
2641 &mac_length ),
2642 expected_status );
2643 PSA_ASSERT( psa_mac_abort( &operation ) );
2644
2645 if( expected_status == PSA_SUCCESS )
2646 {
2647 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2648 actual_mac, mac_length );
2649 }
2650 mbedtls_free( actual_mac );
2651 actual_mac = NULL;
2652 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002653
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002654exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002655 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002656 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002657 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002658 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002659}
2660/* END_CASE */
2661
2662/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002663void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002664 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002665 int alg_arg,
2666 data_t *input,
2667 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002668{
Ronald Cron5425a212020-08-04 14:58:35 +02002669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670 psa_key_type_t key_type = key_type_arg;
2671 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002672 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002674 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675
Gilles Peskine69c12672018-06-28 00:07:19 +02002676 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2677
Gilles Peskine8817f612018-12-18 00:18:46 +01002678 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002680 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002681 psa_set_key_algorithm( &attributes, alg );
2682 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002683
Ronald Cron5425a212020-08-04 14:58:35 +02002684 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2685 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002686
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002687 /* Verify correct MAC, one-shot case. */
2688 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2689 expected_mac->x, expected_mac->len ) );
2690
2691 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002692 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002693 PSA_ASSERT( psa_mac_update( &operation,
2694 input->x, input->len ) );
2695 PSA_ASSERT( psa_mac_verify_finish( &operation,
2696 expected_mac->x,
2697 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002698
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002699 /* Test a MAC that's too short, one-shot case. */
2700 TEST_EQUAL( psa_mac_verify( key, alg,
2701 input->x, input->len,
2702 expected_mac->x,
2703 expected_mac->len - 1 ),
2704 PSA_ERROR_INVALID_SIGNATURE );
2705
2706 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002707 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002708 PSA_ASSERT( psa_mac_update( &operation,
2709 input->x, input->len ) );
2710 TEST_EQUAL( psa_mac_verify_finish( &operation,
2711 expected_mac->x,
2712 expected_mac->len - 1 ),
2713 PSA_ERROR_INVALID_SIGNATURE );
2714
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002715 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002716 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2717 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002718 TEST_EQUAL( psa_mac_verify( key, alg,
2719 input->x, input->len,
2720 perturbed_mac, expected_mac->len + 1 ),
2721 PSA_ERROR_INVALID_SIGNATURE );
2722
2723 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002724 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002725 PSA_ASSERT( psa_mac_update( &operation,
2726 input->x, input->len ) );
2727 TEST_EQUAL( psa_mac_verify_finish( &operation,
2728 perturbed_mac,
2729 expected_mac->len + 1 ),
2730 PSA_ERROR_INVALID_SIGNATURE );
2731
2732 /* Test changing one byte. */
2733 for( size_t i = 0; i < expected_mac->len; i++ )
2734 {
Chris Jones9634bb12021-01-20 15:56:42 +00002735 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002736 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002737
2738 TEST_EQUAL( psa_mac_verify( key, alg,
2739 input->x, input->len,
2740 perturbed_mac, expected_mac->len ),
2741 PSA_ERROR_INVALID_SIGNATURE );
2742
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 ),
2749 PSA_ERROR_INVALID_SIGNATURE );
2750 perturbed_mac[i] ^= 1;
2751 }
2752
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002754 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002755 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002756 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002757 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002758}
2759/* END_CASE */
2760
2761/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002762void cipher_operation_init( )
2763{
Jaeden Ameroab439972019-02-15 14:12:05 +00002764 const uint8_t input[1] = { 0 };
2765 unsigned char output[1] = { 0 };
2766 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002767 /* Test each valid way of initializing the object, except for `= {0}`, as
2768 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2769 * though it's OK by the C standard. We could test for this, but we'd need
2770 * to supress the Clang warning for the test. */
2771 psa_cipher_operation_t func = psa_cipher_operation_init( );
2772 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2773 psa_cipher_operation_t zero;
2774
2775 memset( &zero, 0, sizeof( zero ) );
2776
Jaeden Ameroab439972019-02-15 14:12:05 +00002777 /* A freshly-initialized cipher operation should not be usable. */
2778 TEST_EQUAL( psa_cipher_update( &func,
2779 input, sizeof( input ),
2780 output, sizeof( output ),
2781 &output_length ),
2782 PSA_ERROR_BAD_STATE );
2783 TEST_EQUAL( psa_cipher_update( &init,
2784 input, sizeof( input ),
2785 output, sizeof( output ),
2786 &output_length ),
2787 PSA_ERROR_BAD_STATE );
2788 TEST_EQUAL( psa_cipher_update( &zero,
2789 input, sizeof( input ),
2790 output, sizeof( output ),
2791 &output_length ),
2792 PSA_ERROR_BAD_STATE );
2793
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002794 /* A default cipher operation should be abortable without error. */
2795 PSA_ASSERT( psa_cipher_abort( &func ) );
2796 PSA_ASSERT( psa_cipher_abort( &init ) );
2797 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002798}
2799/* END_CASE */
2800
2801/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002802void cipher_setup( int key_type_arg,
2803 data_t *key,
2804 int alg_arg,
2805 int expected_status_arg )
2806{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002807 psa_key_type_t key_type = key_type_arg;
2808 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002809 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002810 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002811 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002812#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002813 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2814#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002815
Gilles Peskine8817f612018-12-18 00:18:46 +01002816 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002817
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002818 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2819 &operation, &status ) )
2820 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002821 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002822
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002823 /* The operation object should be reusable. */
2824#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2825 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2826 smoke_test_key_data,
2827 sizeof( smoke_test_key_data ),
2828 KNOWN_SUPPORTED_CIPHER_ALG,
2829 &operation, &status ) )
2830 goto exit;
2831 TEST_EQUAL( status, PSA_SUCCESS );
2832#endif
2833
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002834exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002835 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002836 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002837}
2838/* END_CASE */
2839
Ronald Cronee414c72021-03-18 18:50:08 +01002840/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002841void cipher_bad_order( )
2842{
Ronald Cron5425a212020-08-04 14:58:35 +02002843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002844 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2845 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002846 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002847 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002848 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002849 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002850 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2851 0xaa, 0xaa, 0xaa, 0xaa };
2852 const uint8_t text[] = {
2853 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2854 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002855 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002856 size_t length = 0;
2857
2858 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002859 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2860 psa_set_key_algorithm( &attributes, alg );
2861 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002862 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2863 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002864
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002865 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002866 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002867 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002868 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002869 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002870 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002871 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002872 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002873
2874 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002875 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002876 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002877 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002878 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002879 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002880 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002881 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002882
Jaeden Ameroab439972019-02-15 14:12:05 +00002883 /* Generate an IV without calling setup beforehand. */
2884 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2885 buffer, sizeof( buffer ),
2886 &length ),
2887 PSA_ERROR_BAD_STATE );
2888 PSA_ASSERT( psa_cipher_abort( &operation ) );
2889
2890 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002891 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002892 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2893 buffer, sizeof( buffer ),
2894 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002895 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002896 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2897 buffer, sizeof( buffer ),
2898 &length ),
2899 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002900 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002901 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002902 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002903
2904 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002905 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002906 PSA_ASSERT( psa_cipher_set_iv( &operation,
2907 iv, sizeof( iv ) ) );
2908 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2909 buffer, sizeof( buffer ),
2910 &length ),
2911 PSA_ERROR_BAD_STATE );
2912 PSA_ASSERT( psa_cipher_abort( &operation ) );
2913
2914 /* Set an IV without calling setup beforehand. */
2915 TEST_EQUAL( psa_cipher_set_iv( &operation,
2916 iv, sizeof( iv ) ),
2917 PSA_ERROR_BAD_STATE );
2918 PSA_ASSERT( psa_cipher_abort( &operation ) );
2919
2920 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002921 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002922 PSA_ASSERT( psa_cipher_set_iv( &operation,
2923 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002924 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002925 TEST_EQUAL( psa_cipher_set_iv( &operation,
2926 iv, sizeof( iv ) ),
2927 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002928 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002929 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002930 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002931
2932 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002933 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002934 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2935 buffer, sizeof( buffer ),
2936 &length ) );
2937 TEST_EQUAL( psa_cipher_set_iv( &operation,
2938 iv, sizeof( iv ) ),
2939 PSA_ERROR_BAD_STATE );
2940 PSA_ASSERT( psa_cipher_abort( &operation ) );
2941
2942 /* Call update without calling setup beforehand. */
2943 TEST_EQUAL( psa_cipher_update( &operation,
2944 text, sizeof( text ),
2945 buffer, sizeof( buffer ),
2946 &length ),
2947 PSA_ERROR_BAD_STATE );
2948 PSA_ASSERT( psa_cipher_abort( &operation ) );
2949
2950 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002951 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002952 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002953 TEST_EQUAL( psa_cipher_update( &operation,
2954 text, sizeof( text ),
2955 buffer, sizeof( buffer ),
2956 &length ),
2957 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002958 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002959 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002960 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002961
2962 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002963 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002964 PSA_ASSERT( psa_cipher_set_iv( &operation,
2965 iv, sizeof( iv ) ) );
2966 PSA_ASSERT( psa_cipher_finish( &operation,
2967 buffer, sizeof( buffer ), &length ) );
2968 TEST_EQUAL( psa_cipher_update( &operation,
2969 text, sizeof( text ),
2970 buffer, sizeof( buffer ),
2971 &length ),
2972 PSA_ERROR_BAD_STATE );
2973 PSA_ASSERT( psa_cipher_abort( &operation ) );
2974
2975 /* Call finish without calling setup beforehand. */
2976 TEST_EQUAL( psa_cipher_finish( &operation,
2977 buffer, sizeof( buffer ), &length ),
2978 PSA_ERROR_BAD_STATE );
2979 PSA_ASSERT( psa_cipher_abort( &operation ) );
2980
2981 /* Call finish without an IV where an IV is required. */
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 /* Not calling update means we are encrypting an empty buffer, which is OK
2984 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002985 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002986 TEST_EQUAL( psa_cipher_finish( &operation,
2987 buffer, sizeof( buffer ), &length ),
2988 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002989 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002990 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002991 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002992
2993 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002994 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002995 PSA_ASSERT( psa_cipher_set_iv( &operation,
2996 iv, sizeof( iv ) ) );
2997 PSA_ASSERT( psa_cipher_finish( &operation,
2998 buffer, sizeof( buffer ), &length ) );
2999 TEST_EQUAL( psa_cipher_finish( &operation,
3000 buffer, sizeof( buffer ), &length ),
3001 PSA_ERROR_BAD_STATE );
3002 PSA_ASSERT( psa_cipher_abort( &operation ) );
3003
Ronald Cron5425a212020-08-04 14:58:35 +02003004 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003005
Jaeden Ameroab439972019-02-15 14:12:05 +00003006exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003007 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003008 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003009}
3010/* END_CASE */
3011
3012/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003013void cipher_encrypt_fail( int alg_arg,
3014 int key_type_arg,
3015 data_t *key_data,
3016 data_t *input,
3017 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003018{
Ronald Cron5425a212020-08-04 14:58:35 +02003019 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003020 psa_status_t status;
3021 psa_key_type_t key_type = key_type_arg;
3022 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003023 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003024 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003025 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003026 size_t output_length = 0;
3027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3028
3029 if ( PSA_ERROR_BAD_STATE != expected_status )
3030 {
3031 PSA_ASSERT( psa_crypto_init( ) );
3032
3033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3034 psa_set_key_algorithm( &attributes, alg );
3035 psa_set_key_type( &attributes, key_type );
3036
3037 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3038 input->len );
3039 ASSERT_ALLOC( output, output_buffer_size );
3040
3041 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3042 &key ) );
3043 }
3044
3045 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3046 output_buffer_size, &output_length );
3047
3048 TEST_EQUAL( status, expected_status );
3049
3050exit:
3051 mbedtls_free( output );
3052 psa_destroy_key( key );
3053 PSA_DONE( );
3054}
3055/* END_CASE */
3056
3057/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003058void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3059 data_t *input, int iv_length,
3060 int expected_result )
3061{
3062 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3063 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3065 size_t output_buffer_size = 0;
3066 unsigned char *output = NULL;
3067
3068 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3069 ASSERT_ALLOC( output, output_buffer_size );
3070
3071 PSA_ASSERT( psa_crypto_init( ) );
3072
3073 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3074 psa_set_key_algorithm( &attributes, alg );
3075 psa_set_key_type( &attributes, key_type );
3076
3077 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3078 &key ) );
3079 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3080 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3081 iv_length ) );
3082
3083exit:
3084 psa_cipher_abort( &operation );
3085 mbedtls_free( output );
3086 psa_destroy_key( key );
3087 PSA_DONE( );
3088}
3089/* END_CASE */
3090
3091/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003092void cipher_encrypt_alg_without_iv( int alg_arg,
3093 int key_type_arg,
3094 data_t *key_data,
3095 data_t *input,
3096 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003097{
3098 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3099 psa_key_type_t key_type = key_type_arg;
3100 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003101 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3102 uint8_t iv[1] = { 0x5a };
3103 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003104 unsigned char *output = NULL;
3105 size_t output_buffer_size = 0;
3106 size_t output_length = 0;
3107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3108
3109 PSA_ASSERT( psa_crypto_init( ) );
3110
3111 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3112 psa_set_key_algorithm( &attributes, alg );
3113 psa_set_key_type( &attributes, key_type );
3114
3115 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3116 ASSERT_ALLOC( output, output_buffer_size );
3117
3118 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3119 &key ) );
3120
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003121 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3122 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3123 PSA_ERROR_BAD_STATE );
3124 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3125 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
3126 &iv_length ),
3127 PSA_ERROR_BAD_STATE );
3128
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003129 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
3130 output_buffer_size, &output_length ) );
3131 TEST_ASSERT( output_length <=
3132 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3133 TEST_ASSERT( output_length <=
3134 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3135
3136 ASSERT_COMPARE( expected_output->x, expected_output->len,
3137 output, output_length );
3138exit:
3139 mbedtls_free( output );
3140 psa_destroy_key( key );
3141 PSA_DONE( );
3142}
3143/* END_CASE */
3144
3145/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003146void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3147{
3148 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3149 psa_algorithm_t alg = alg_arg;
3150 psa_key_type_t key_type = key_type_arg;
3151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3152 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3153 psa_status_t status;
3154
3155 PSA_ASSERT( psa_crypto_init( ) );
3156
3157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3158 psa_set_key_algorithm( &attributes, alg );
3159 psa_set_key_type( &attributes, key_type );
3160
3161 /* Usage of either of these two size macros would cause divide by zero
3162 * with incorrect key types previously. Input length should be irrelevant
3163 * here. */
3164 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3165 0 );
3166 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3167
3168
3169 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3170 &key ) );
3171
3172 /* Should fail due to invalid alg type (to support invalid key type).
3173 * Encrypt or decrypt will end up in the same place. */
3174 status = psa_cipher_encrypt_setup( &operation, key, alg );
3175
3176 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3177
3178exit:
3179 psa_cipher_abort( &operation );
3180 psa_destroy_key( key );
3181 PSA_DONE( );
3182}
3183/* END_CASE */
3184
3185/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003186void cipher_encrypt_validation( int alg_arg,
3187 int key_type_arg,
3188 data_t *key_data,
3189 data_t *input )
3190{
3191 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3192 psa_key_type_t key_type = key_type_arg;
3193 psa_algorithm_t alg = alg_arg;
3194 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3195 unsigned char *output1 = NULL;
3196 size_t output1_buffer_size = 0;
3197 size_t output1_length = 0;
3198 unsigned char *output2 = NULL;
3199 size_t output2_buffer_size = 0;
3200 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003201 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003202 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003204
Gilles Peskine8817f612018-12-18 00:18:46 +01003205 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003206
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3208 psa_set_key_algorithm( &attributes, alg );
3209 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003210
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003211 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3212 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3213 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3214 ASSERT_ALLOC( output1, output1_buffer_size );
3215 ASSERT_ALLOC( output2, output2_buffer_size );
3216
Ronald Cron5425a212020-08-04 14:58:35 +02003217 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3218 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003219
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003220 /* The one-shot cipher encryption uses generated iv so validating
3221 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003222 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3223 output1_buffer_size, &output1_length ) );
3224 TEST_ASSERT( output1_length <=
3225 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3226 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003227 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003228
3229 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3230 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003231
Gilles Peskine8817f612018-12-18 00:18:46 +01003232 PSA_ASSERT( psa_cipher_update( &operation,
3233 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003234 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003235 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003236 TEST_ASSERT( function_output_length <=
3237 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3238 TEST_ASSERT( function_output_length <=
3239 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003240 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003241
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003242 PSA_ASSERT( psa_cipher_finish( &operation,
3243 output2 + output2_length,
3244 output2_buffer_size - output2_length,
3245 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003246 TEST_ASSERT( function_output_length <=
3247 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3248 TEST_ASSERT( function_output_length <=
3249 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003250 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003251
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003252 PSA_ASSERT( psa_cipher_abort( &operation ) );
3253 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3254 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003255
Gilles Peskine50e586b2018-06-08 14:28:46 +02003256exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003257 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003258 mbedtls_free( output1 );
3259 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003260 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003261 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003262}
3263/* END_CASE */
3264
3265/* BEGIN_CASE */
3266void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003267 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003268 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003269 int first_part_size_arg,
3270 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003271 data_t *expected_output,
3272 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003273{
Ronald Cron5425a212020-08-04 14:58:35 +02003274 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003275 psa_key_type_t key_type = key_type_arg;
3276 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003277 psa_status_t status;
3278 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003279 size_t first_part_size = first_part_size_arg;
3280 size_t output1_length = output1_length_arg;
3281 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003282 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003283 size_t output_buffer_size = 0;
3284 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003285 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003286 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003287 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003288
Gilles Peskine8817f612018-12-18 00:18:46 +01003289 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003290
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003291 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3292 psa_set_key_algorithm( &attributes, alg );
3293 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003294
Ronald Cron5425a212020-08-04 14:58:35 +02003295 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3296 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003297
Ronald Cron5425a212020-08-04 14:58:35 +02003298 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003299
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003300 if( iv->len > 0 )
3301 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003302 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003303 }
3304
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003305 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3306 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003307 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003308
Gilles Peskinee0866522019-02-19 19:44:00 +01003309 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003310 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3311 output, output_buffer_size,
3312 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003313 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003314 TEST_ASSERT( function_output_length <=
3315 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3316 TEST_ASSERT( function_output_length <=
3317 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003318 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003319
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003320 if( first_part_size < input->len )
3321 {
3322 PSA_ASSERT( psa_cipher_update( &operation,
3323 input->x + first_part_size,
3324 input->len - first_part_size,
3325 ( output_buffer_size == 0 ? NULL :
3326 output + total_output_length ),
3327 output_buffer_size - total_output_length,
3328 &function_output_length ) );
3329 TEST_ASSERT( function_output_length == output2_length );
3330 TEST_ASSERT( function_output_length <=
3331 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3332 alg,
3333 input->len - first_part_size ) );
3334 TEST_ASSERT( function_output_length <=
3335 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3336 total_output_length += function_output_length;
3337 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003338
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003339 status = psa_cipher_finish( &operation,
3340 ( output_buffer_size == 0 ? NULL :
3341 output + total_output_length ),
3342 output_buffer_size - total_output_length,
3343 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003344 TEST_ASSERT( function_output_length <=
3345 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3346 TEST_ASSERT( function_output_length <=
3347 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003348 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003349 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003350
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003351 if( expected_status == PSA_SUCCESS )
3352 {
3353 PSA_ASSERT( psa_cipher_abort( &operation ) );
3354
3355 ASSERT_COMPARE( expected_output->x, expected_output->len,
3356 output, total_output_length );
3357 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003358
3359exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003360 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003361 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003362 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003363 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003364}
3365/* END_CASE */
3366
3367/* BEGIN_CASE */
3368void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003369 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003370 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003371 int first_part_size_arg,
3372 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003373 data_t *expected_output,
3374 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003375{
Ronald Cron5425a212020-08-04 14:58:35 +02003376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003377 psa_key_type_t key_type = key_type_arg;
3378 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003379 psa_status_t status;
3380 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003381 size_t first_part_size = first_part_size_arg;
3382 size_t output1_length = output1_length_arg;
3383 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003384 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003385 size_t output_buffer_size = 0;
3386 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003387 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003388 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003390
Gilles Peskine8817f612018-12-18 00:18:46 +01003391 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003392
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003393 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3394 psa_set_key_algorithm( &attributes, alg );
3395 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003396
Ronald Cron5425a212020-08-04 14:58:35 +02003397 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3398 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003399
Ronald Cron5425a212020-08-04 14:58:35 +02003400 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003401
Steven Cooreman177deba2020-09-07 17:14:14 +02003402 if( iv->len > 0 )
3403 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003404 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003405 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003406
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003407 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3408 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003409 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003410
Gilles Peskinee0866522019-02-19 19:44:00 +01003411 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003412 PSA_ASSERT( psa_cipher_update( &operation,
3413 input->x, first_part_size,
3414 output, output_buffer_size,
3415 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003416 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003417 TEST_ASSERT( function_output_length <=
3418 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3419 TEST_ASSERT( function_output_length <=
3420 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003421 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003422
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003423 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003424 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003425 PSA_ASSERT( psa_cipher_update( &operation,
3426 input->x + first_part_size,
3427 input->len - first_part_size,
3428 ( output_buffer_size == 0 ? NULL :
3429 output + total_output_length ),
3430 output_buffer_size - total_output_length,
3431 &function_output_length ) );
3432 TEST_ASSERT( function_output_length == output2_length );
3433 TEST_ASSERT( function_output_length <=
3434 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3435 alg,
3436 input->len - first_part_size ) );
3437 TEST_ASSERT( function_output_length <=
3438 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3439 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003440 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003441
Gilles Peskine50e586b2018-06-08 14:28:46 +02003442 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003443 ( output_buffer_size == 0 ? NULL :
3444 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003445 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003446 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003447 TEST_ASSERT( function_output_length <=
3448 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3449 TEST_ASSERT( function_output_length <=
3450 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003451 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003452 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003453
3454 if( expected_status == PSA_SUCCESS )
3455 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003456 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003457
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003458 ASSERT_COMPARE( expected_output->x, expected_output->len,
3459 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003460 }
3461
Gilles Peskine50e586b2018-06-08 14:28:46 +02003462exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003463 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003464 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003465 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003466 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003467}
3468/* END_CASE */
3469
Gilles Peskine50e586b2018-06-08 14:28:46 +02003470/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003471void cipher_decrypt_fail( int alg_arg,
3472 int key_type_arg,
3473 data_t *key_data,
3474 data_t *iv,
3475 data_t *input_arg,
3476 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003477{
3478 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3479 psa_status_t status;
3480 psa_key_type_t key_type = key_type_arg;
3481 psa_algorithm_t alg = alg_arg;
3482 psa_status_t expected_status = expected_status_arg;
3483 unsigned char *input = NULL;
3484 size_t input_buffer_size = 0;
3485 unsigned char *output = NULL;
3486 size_t output_buffer_size = 0;
3487 size_t output_length = 0;
3488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3489
3490 if ( PSA_ERROR_BAD_STATE != expected_status )
3491 {
3492 PSA_ASSERT( psa_crypto_init( ) );
3493
3494 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3495 psa_set_key_algorithm( &attributes, alg );
3496 psa_set_key_type( &attributes, key_type );
3497
3498 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3499 &key ) );
3500 }
3501
3502 /* Allocate input buffer and copy the iv and the plaintext */
3503 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3504 if ( input_buffer_size > 0 )
3505 {
3506 ASSERT_ALLOC( input, input_buffer_size );
3507 memcpy( input, iv->x, iv->len );
3508 memcpy( input + iv->len, input_arg->x, input_arg->len );
3509 }
3510
3511 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3512 ASSERT_ALLOC( output, output_buffer_size );
3513
3514 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3515 output_buffer_size, &output_length );
3516 TEST_EQUAL( status, expected_status );
3517
3518exit:
3519 mbedtls_free( input );
3520 mbedtls_free( output );
3521 psa_destroy_key( key );
3522 PSA_DONE( );
3523}
3524/* END_CASE */
3525
3526/* BEGIN_CASE */
3527void cipher_decrypt( int alg_arg,
3528 int key_type_arg,
3529 data_t *key_data,
3530 data_t *iv,
3531 data_t *input_arg,
3532 data_t *expected_output )
3533{
3534 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3535 psa_key_type_t key_type = key_type_arg;
3536 psa_algorithm_t alg = alg_arg;
3537 unsigned char *input = NULL;
3538 size_t input_buffer_size = 0;
3539 unsigned char *output = NULL;
3540 size_t output_buffer_size = 0;
3541 size_t output_length = 0;
3542 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3543
3544 PSA_ASSERT( psa_crypto_init( ) );
3545
3546 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3547 psa_set_key_algorithm( &attributes, alg );
3548 psa_set_key_type( &attributes, key_type );
3549
3550 /* Allocate input buffer and copy the iv and the plaintext */
3551 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3552 if ( input_buffer_size > 0 )
3553 {
3554 ASSERT_ALLOC( input, input_buffer_size );
3555 memcpy( input, iv->x, iv->len );
3556 memcpy( input + iv->len, input_arg->x, input_arg->len );
3557 }
3558
3559 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3560 ASSERT_ALLOC( output, output_buffer_size );
3561
3562 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3563 &key ) );
3564
3565 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3566 output_buffer_size, &output_length ) );
3567 TEST_ASSERT( output_length <=
3568 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3569 TEST_ASSERT( output_length <=
3570 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3571
3572 ASSERT_COMPARE( expected_output->x, expected_output->len,
3573 output, output_length );
3574exit:
3575 mbedtls_free( input );
3576 mbedtls_free( output );
3577 psa_destroy_key( key );
3578 PSA_DONE( );
3579}
3580/* END_CASE */
3581
3582/* BEGIN_CASE */
3583void cipher_verify_output( int alg_arg,
3584 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003585 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003586 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003587{
Ronald Cron5425a212020-08-04 14:58:35 +02003588 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003589 psa_key_type_t key_type = key_type_arg;
3590 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003591 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003592 size_t output1_size = 0;
3593 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003594 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003595 size_t output2_size = 0;
3596 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003597 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003598
Gilles Peskine8817f612018-12-18 00:18:46 +01003599 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003600
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003601 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3602 psa_set_key_algorithm( &attributes, alg );
3603 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003604
Ronald Cron5425a212020-08-04 14:58:35 +02003605 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3606 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003607 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003608 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003609
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003610 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3611 output1, output1_size,
3612 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003613 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003614 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003615 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003616 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003617
3618 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003619 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003620
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003621 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3622 output2, output2_size,
3623 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003624 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003625 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003626 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003627 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003628
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003629 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003630
3631exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003632 mbedtls_free( output1 );
3633 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003634 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003635 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003636}
3637/* END_CASE */
3638
3639/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003640void cipher_verify_output_multipart( int alg_arg,
3641 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003642 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003643 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003644 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003645{
Ronald Cron5425a212020-08-04 14:58:35 +02003646 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003647 psa_key_type_t key_type = key_type_arg;
3648 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003649 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003650 unsigned char iv[16] = {0};
3651 size_t iv_size = 16;
3652 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003653 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003654 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003655 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003656 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003657 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003658 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003659 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003660 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3661 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003662 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003663
Gilles Peskine8817f612018-12-18 00:18:46 +01003664 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003665
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003666 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3667 psa_set_key_algorithm( &attributes, alg );
3668 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003669
Ronald Cron5425a212020-08-04 14:58:35 +02003670 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3671 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003672
Ronald Cron5425a212020-08-04 14:58:35 +02003673 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3674 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003675
Steven Cooreman177deba2020-09-07 17:14:14 +02003676 if( alg != PSA_ALG_ECB_NO_PADDING )
3677 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003678 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3679 iv, iv_size,
3680 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003681 }
3682
gabor-mezei-armceface22021-01-21 12:26:17 +01003683 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3684 TEST_ASSERT( output1_buffer_size <=
3685 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003686 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003687
Gilles Peskinee0866522019-02-19 19:44:00 +01003688 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003689
Gilles Peskine8817f612018-12-18 00:18:46 +01003690 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3691 output1, output1_buffer_size,
3692 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003693 TEST_ASSERT( function_output_length <=
3694 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3695 TEST_ASSERT( function_output_length <=
3696 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003697 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003698
Gilles Peskine8817f612018-12-18 00:18:46 +01003699 PSA_ASSERT( psa_cipher_update( &operation1,
3700 input->x + first_part_size,
3701 input->len - first_part_size,
3702 output1, output1_buffer_size,
3703 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003704 TEST_ASSERT( function_output_length <=
3705 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3706 alg,
3707 input->len - first_part_size ) );
3708 TEST_ASSERT( function_output_length <=
3709 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003710 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003711
Gilles Peskine8817f612018-12-18 00:18:46 +01003712 PSA_ASSERT( psa_cipher_finish( &operation1,
3713 output1 + output1_length,
3714 output1_buffer_size - output1_length,
3715 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003716 TEST_ASSERT( function_output_length <=
3717 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3718 TEST_ASSERT( function_output_length <=
3719 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003720 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003721
Gilles Peskine8817f612018-12-18 00:18:46 +01003722 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003723
Gilles Peskine048b7f02018-06-08 14:20:49 +02003724 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003725 TEST_ASSERT( output2_buffer_size <=
3726 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3727 TEST_ASSERT( output2_buffer_size <=
3728 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003729 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003730
Steven Cooreman177deba2020-09-07 17:14:14 +02003731 if( iv_length > 0 )
3732 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003733 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3734 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003735 }
Moran Pekerded84402018-06-06 16:36:50 +03003736
Gilles Peskine8817f612018-12-18 00:18:46 +01003737 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3738 output2, output2_buffer_size,
3739 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003740 TEST_ASSERT( function_output_length <=
3741 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3742 TEST_ASSERT( function_output_length <=
3743 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003744 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003745
Gilles Peskine8817f612018-12-18 00:18:46 +01003746 PSA_ASSERT( psa_cipher_update( &operation2,
3747 output1 + first_part_size,
3748 output1_length - first_part_size,
3749 output2, output2_buffer_size,
3750 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003751 TEST_ASSERT( function_output_length <=
3752 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3753 alg,
3754 output1_length - first_part_size ) );
3755 TEST_ASSERT( function_output_length <=
3756 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003757 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003758
Gilles Peskine8817f612018-12-18 00:18:46 +01003759 PSA_ASSERT( psa_cipher_finish( &operation2,
3760 output2 + output2_length,
3761 output2_buffer_size - output2_length,
3762 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003763 TEST_ASSERT( function_output_length <=
3764 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3765 TEST_ASSERT( function_output_length <=
3766 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003767 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003768
Gilles Peskine8817f612018-12-18 00:18:46 +01003769 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003770
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003771 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003772
3773exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003774 psa_cipher_abort( &operation1 );
3775 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003776 mbedtls_free( output1 );
3777 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003778 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003779 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003780}
3781/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003782
Gilles Peskine20035e32018-02-03 22:44:14 +01003783/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003784void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003785 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003786 data_t *nonce,
3787 data_t *additional_data,
3788 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003789 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003790{
Ronald Cron5425a212020-08-04 14:58:35 +02003791 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003792 psa_key_type_t key_type = key_type_arg;
3793 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003794 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003795 unsigned char *output_data = NULL;
3796 size_t output_size = 0;
3797 size_t output_length = 0;
3798 unsigned char *output_data2 = NULL;
3799 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003800 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003801 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003803
Gilles Peskine8817f612018-12-18 00:18:46 +01003804 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003805
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003806 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3807 psa_set_key_algorithm( &attributes, alg );
3808 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003809
Gilles Peskine049c7532019-05-15 20:22:09 +02003810 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003811 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003812 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3813 key_bits = psa_get_key_bits( &attributes );
3814
3815 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3816 alg );
3817 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3818 * should be exact. */
3819 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3820 expected_result != PSA_ERROR_NOT_SUPPORTED )
3821 {
3822 TEST_EQUAL( output_size,
3823 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3824 TEST_ASSERT( output_size <=
3825 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3826 }
3827 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003828
Steven Cooremanf49478b2021-02-15 15:19:25 +01003829 status = psa_aead_encrypt( key, alg,
3830 nonce->x, nonce->len,
3831 additional_data->x,
3832 additional_data->len,
3833 input_data->x, input_data->len,
3834 output_data, output_size,
3835 &output_length );
3836
3837 /* If the operation is not supported, just skip and not fail in case the
3838 * encryption involves a common limitation of cryptography hardwares and
3839 * an alternative implementation. */
3840 if( status == PSA_ERROR_NOT_SUPPORTED )
3841 {
3842 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3843 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3844 }
3845
3846 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003847
3848 if( PSA_SUCCESS == expected_result )
3849 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003850 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003851
Gilles Peskine003a4a92019-05-14 16:09:40 +02003852 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3853 * should be exact. */
3854 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003855 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003856
gabor-mezei-armceface22021-01-21 12:26:17 +01003857 TEST_ASSERT( input_data->len <=
3858 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3859
Ronald Cron5425a212020-08-04 14:58:35 +02003860 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003861 nonce->x, nonce->len,
3862 additional_data->x,
3863 additional_data->len,
3864 output_data, output_length,
3865 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003866 &output_length2 ),
3867 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003868
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003869 ASSERT_COMPARE( input_data->x, input_data->len,
3870 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003871 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003872
Gilles Peskinea1cac842018-06-11 19:33:02 +02003873exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003874 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003875 mbedtls_free( output_data );
3876 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003877 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003878}
3879/* END_CASE */
3880
3881/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003882void aead_encrypt( int key_type_arg, data_t *key_data,
3883 int alg_arg,
3884 data_t *nonce,
3885 data_t *additional_data,
3886 data_t *input_data,
3887 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003888{
Ronald Cron5425a212020-08-04 14:58:35 +02003889 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003890 psa_key_type_t key_type = key_type_arg;
3891 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003892 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003893 unsigned char *output_data = NULL;
3894 size_t output_size = 0;
3895 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003897 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003898
Gilles Peskine8817f612018-12-18 00:18:46 +01003899 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003900
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003901 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3902 psa_set_key_algorithm( &attributes, alg );
3903 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003904
Gilles Peskine049c7532019-05-15 20:22:09 +02003905 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003906 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003907 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3908 key_bits = psa_get_key_bits( &attributes );
3909
3910 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3911 alg );
3912 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3913 * should be exact. */
3914 TEST_EQUAL( output_size,
3915 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3916 TEST_ASSERT( output_size <=
3917 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3918 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003919
Steven Cooremand588ea12021-01-11 19:36:04 +01003920 status = psa_aead_encrypt( key, alg,
3921 nonce->x, nonce->len,
3922 additional_data->x, additional_data->len,
3923 input_data->x, input_data->len,
3924 output_data, output_size,
3925 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003926
Ronald Cron28a45ed2021-02-09 20:35:42 +01003927 /* If the operation is not supported, just skip and not fail in case the
3928 * encryption involves a common limitation of cryptography hardwares and
3929 * an alternative implementation. */
3930 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003931 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003932 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3933 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003934 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003935
3936 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003937 ASSERT_COMPARE( expected_result->x, expected_result->len,
3938 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003939
Gilles Peskinea1cac842018-06-11 19:33:02 +02003940exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003941 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003942 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003943 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003944}
3945/* END_CASE */
3946
3947/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003948void aead_decrypt( int key_type_arg, data_t *key_data,
3949 int alg_arg,
3950 data_t *nonce,
3951 data_t *additional_data,
3952 data_t *input_data,
3953 data_t *expected_data,
3954 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003955{
Ronald Cron5425a212020-08-04 14:58:35 +02003956 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003957 psa_key_type_t key_type = key_type_arg;
3958 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003959 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003960 unsigned char *output_data = NULL;
3961 size_t output_size = 0;
3962 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003963 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003964 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003965 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003966
Gilles Peskine8817f612018-12-18 00:18:46 +01003967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003968
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003969 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3970 psa_set_key_algorithm( &attributes, alg );
3971 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003972
Gilles Peskine049c7532019-05-15 20:22:09 +02003973 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003974 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003975 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3976 key_bits = psa_get_key_bits( &attributes );
3977
3978 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3979 alg );
3980 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3981 expected_result != PSA_ERROR_NOT_SUPPORTED )
3982 {
3983 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3984 * should be exact. */
3985 TEST_EQUAL( output_size,
3986 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3987 TEST_ASSERT( output_size <=
3988 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3989 }
3990 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003991
Steven Cooremand588ea12021-01-11 19:36:04 +01003992 status = psa_aead_decrypt( key, alg,
3993 nonce->x, nonce->len,
3994 additional_data->x,
3995 additional_data->len,
3996 input_data->x, input_data->len,
3997 output_data, output_size,
3998 &output_length );
3999
Ronald Cron28a45ed2021-02-09 20:35:42 +01004000 /* If the operation is not supported, just skip and not fail in case the
4001 * decryption involves a common limitation of cryptography hardwares and
4002 * an alternative implementation. */
4003 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004004 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004005 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4006 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004007 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004008
4009 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004010
Gilles Peskine2d277862018-06-18 15:41:12 +02004011 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004012 ASSERT_COMPARE( expected_data->x, expected_data->len,
4013 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004014
Gilles Peskinea1cac842018-06-11 19:33:02 +02004015exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004016 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004017 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004018 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004019}
4020/* END_CASE */
4021
4022/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004023void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4024 int alg_arg,
4025 data_t *nonce,
4026 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004027 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004028 int do_set_lengths,
4029 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004030{
Paul Elliottd3f82412021-06-16 16:52:21 +01004031 size_t ad_part_len = 0;
4032 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004033 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004034
Paul Elliott32f46ba2021-09-23 18:24:36 +01004035 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004036 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004037 mbedtls_test_set_step( ad_part_len );
4038
4039 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004040 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004041 if( ad_part_len & 0x01 )
4042 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4043 else
4044 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004045 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004046
4047 /* Split ad into length(ad_part_len) parts. */
4048 if( !aead_multipart_internal_func( key_type_arg, key_data,
4049 alg_arg, nonce,
4050 additional_data,
4051 ad_part_len,
4052 input_data, -1,
4053 set_lengths_method,
4054 expected_output,
4055 1, 0 ) )
4056 break;
4057
4058 /* length(0) part, length(ad_part_len) part, length(0) part... */
4059 mbedtls_test_set_step( 1000 + ad_part_len );
4060
4061 if( !aead_multipart_internal_func( key_type_arg, key_data,
4062 alg_arg, nonce,
4063 additional_data,
4064 ad_part_len,
4065 input_data, -1,
4066 set_lengths_method,
4067 expected_output,
4068 1, 1 ) )
4069 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004070 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004071
Paul Elliott32f46ba2021-09-23 18:24:36 +01004072 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004073 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004074 /* Split data into length(data_part_len) parts. */
4075 mbedtls_test_set_step( 2000 + data_part_len );
4076
4077 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004078 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004079 if( data_part_len & 0x01 )
4080 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4081 else
4082 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004083 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004084
Paul Elliott32f46ba2021-09-23 18:24:36 +01004085 if( !aead_multipart_internal_func( key_type_arg, key_data,
4086 alg_arg, nonce,
4087 additional_data, -1,
4088 input_data, data_part_len,
4089 set_lengths_method,
4090 expected_output,
4091 1, 0 ) )
4092 break;
4093
4094 /* length(0) part, length(data_part_len) part, length(0) part... */
4095 mbedtls_test_set_step( 3000 + data_part_len );
4096
4097 if( !aead_multipart_internal_func( key_type_arg, key_data,
4098 alg_arg, nonce,
4099 additional_data, -1,
4100 input_data, data_part_len,
4101 set_lengths_method,
4102 expected_output,
4103 1, 1 ) )
4104 break;
4105 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004106
Paul Elliott8fc45162021-06-23 16:06:01 +01004107 /* Goto is required to silence warnings about unused labels, as we
4108 * don't actually do any test assertions in this function. */
4109 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004110}
4111/* END_CASE */
4112
4113/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004114void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4115 int alg_arg,
4116 data_t *nonce,
4117 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004118 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004119 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004120 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004121{
Paul Elliottd3f82412021-06-16 16:52:21 +01004122 size_t ad_part_len = 0;
4123 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004124 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004125
Paul Elliott32f46ba2021-09-23 18:24:36 +01004126 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004127 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004128 /* Split ad into length(ad_part_len) parts. */
4129 mbedtls_test_set_step( ad_part_len );
4130
4131 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004132 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004133 if( ad_part_len & 0x01 )
4134 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4135 else
4136 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004137 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004138
4139 if( !aead_multipart_internal_func( key_type_arg, key_data,
4140 alg_arg, nonce,
4141 additional_data,
4142 ad_part_len,
4143 input_data, -1,
4144 set_lengths_method,
4145 expected_output,
4146 0, 0 ) )
4147 break;
4148
4149 /* length(0) part, length(ad_part_len) part, length(0) part... */
4150 mbedtls_test_set_step( 1000 + ad_part_len );
4151
4152 if( !aead_multipart_internal_func( key_type_arg, key_data,
4153 alg_arg, nonce,
4154 additional_data,
4155 ad_part_len,
4156 input_data, -1,
4157 set_lengths_method,
4158 expected_output,
4159 0, 1 ) )
4160 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004161 }
4162
Paul Elliott32f46ba2021-09-23 18:24:36 +01004163 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004164 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004165 /* Split data into length(data_part_len) parts. */
4166 mbedtls_test_set_step( 2000 + data_part_len );
4167
4168 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004169 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004170 if( data_part_len & 0x01 )
4171 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4172 else
4173 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004174 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004175
4176 if( !aead_multipart_internal_func( key_type_arg, key_data,
4177 alg_arg, nonce,
4178 additional_data, -1,
4179 input_data, data_part_len,
4180 set_lengths_method,
4181 expected_output,
4182 0, 0 ) )
4183 break;
4184
4185 /* length(0) part, length(data_part_len) part, length(0) part... */
4186 mbedtls_test_set_step( 3000 + data_part_len );
4187
4188 if( !aead_multipart_internal_func( key_type_arg, key_data,
4189 alg_arg, nonce,
4190 additional_data, -1,
4191 input_data, data_part_len,
4192 set_lengths_method,
4193 expected_output,
4194 0, 1 ) )
4195 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004196 }
4197
Paul Elliott8fc45162021-06-23 16:06:01 +01004198 /* Goto is required to silence warnings about unused labels, as we
4199 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004200 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004201}
4202/* END_CASE */
4203
4204/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004205void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4206 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004207 int nonce_length,
4208 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004209 data_t *additional_data,
4210 data_t *input_data,
4211 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004212{
4213
4214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4215 psa_key_type_t key_type = key_type_arg;
4216 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004217 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004218 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4220 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004221 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004222 size_t actual_nonce_length = 0;
4223 size_t expected_nonce_length = expected_nonce_length_arg;
4224 unsigned char *output = NULL;
4225 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004226 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004227 size_t ciphertext_size = 0;
4228 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004229 size_t tag_length = 0;
4230 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004231
4232 PSA_ASSERT( psa_crypto_init( ) );
4233
4234 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4235 psa_set_key_algorithm( & attributes, alg );
4236 psa_set_key_type( & attributes, key_type );
4237
4238 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4239 &key ) );
4240
4241 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4242
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004243 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4244
Paul Elliottf1277632021-08-24 18:11:37 +01004245 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004246
Paul Elliottf1277632021-08-24 18:11:37 +01004247 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004248
Paul Elliottf1277632021-08-24 18:11:37 +01004249 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004250
Paul Elliottf1277632021-08-24 18:11:37 +01004251 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004252
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004253 status = psa_aead_encrypt_setup( &operation, key, alg );
4254
4255 /* If the operation is not supported, just skip and not fail in case the
4256 * encryption involves a common limitation of cryptography hardwares and
4257 * an alternative implementation. */
4258 if( status == PSA_ERROR_NOT_SUPPORTED )
4259 {
4260 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004261 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004262 }
4263
4264 PSA_ASSERT( status );
4265
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004266 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004267 nonce_length,
4268 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004269
Paul Elliott693bf312021-07-23 17:40:41 +01004270 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004271
Paul Elliottf1277632021-08-24 18:11:37 +01004272 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004273
Paul Elliott88ecbe12021-09-22 17:23:03 +01004274 if( expected_status == PSA_SUCCESS )
4275 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4276 alg ) );
4277
Paul Elliottd79c5c52021-10-06 21:49:41 +01004278 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004279
Paul Elliott693bf312021-07-23 17:40:41 +01004280 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004281 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004282 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004283 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4284 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004285
4286 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4287 additional_data->len ) );
4288
4289 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004290 output, output_size,
4291 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004292
Paul Elliottf1277632021-08-24 18:11:37 +01004293 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4294 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004295 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4296 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004297
4298exit:
4299 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004300 mbedtls_free( output );
4301 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004302 psa_aead_abort( &operation );
4303 PSA_DONE( );
4304}
4305/* END_CASE */
4306
4307/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004308void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4309 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004310 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004311 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004312 data_t *additional_data,
4313 data_t *input_data,
4314 int expected_status_arg )
4315{
4316
4317 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4318 psa_key_type_t key_type = key_type_arg;
4319 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004320 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004321 uint8_t *nonce_buffer = NULL;
4322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4323 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4324 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004325 unsigned char *output = NULL;
4326 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004327 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004328 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004329 size_t ciphertext_size = 0;
4330 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004331 size_t tag_length = 0;
4332 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004333 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004334 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004335
4336 PSA_ASSERT( psa_crypto_init( ) );
4337
4338 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4339 psa_set_key_algorithm( &attributes, alg );
4340 psa_set_key_type( &attributes, key_type );
4341
4342 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4343 &key ) );
4344
4345 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4346
4347 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4348
Paul Elliott6f0e7202021-08-25 12:57:18 +01004349 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004350
Paul Elliott6f0e7202021-08-25 12:57:18 +01004351 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004352
Paul Elliott6f0e7202021-08-25 12:57:18 +01004353 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004354
Paul Elliott6f0e7202021-08-25 12:57:18 +01004355 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004356
Paul Elliott863864a2021-07-23 17:28:31 +01004357 status = psa_aead_encrypt_setup( &operation, key, alg );
4358
4359 /* If the operation is not supported, just skip and not fail in case the
4360 * encryption involves a common limitation of cryptography hardwares and
4361 * an alternative implementation. */
4362 if( status == PSA_ERROR_NOT_SUPPORTED )
4363 {
4364 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004365 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004366 }
4367
4368 PSA_ASSERT( status );
4369
Paul Elliott4023ffd2021-09-10 16:21:22 +01004370 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4371 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004372 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004373 /* Arbitrary size buffer, to test zero length valid buffer. */
4374 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004375 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004376 }
4377 else
4378 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004379 /* If length is zero, then this will return NULL. */
4380 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004381 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004382
Paul Elliott4023ffd2021-09-10 16:21:22 +01004383 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004384 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004385 for( index = 0; index < nonce_length - 1; ++index )
4386 {
4387 nonce_buffer[index] = 'a' + index;
4388 }
Paul Elliott66696b52021-08-16 18:42:41 +01004389 }
Paul Elliott863864a2021-07-23 17:28:31 +01004390 }
4391
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004392 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4393 {
4394 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4395 input_data->len ) );
4396 }
4397
Paul Elliott6f0e7202021-08-25 12:57:18 +01004398 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004399
Paul Elliott693bf312021-07-23 17:40:41 +01004400 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004401
4402 if( expected_status == PSA_SUCCESS )
4403 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004404 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4405 {
4406 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4407 input_data->len ) );
4408 }
4409 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4410 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004411
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004412 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4413 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4414 additional_data->len ),
4415 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004416
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004417 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004418 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004419 &ciphertext_length ),
4420 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004421
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004422 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004423 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004424 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4425 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004426 }
4427
4428exit:
4429 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004430 mbedtls_free( output );
4431 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004432 mbedtls_free( nonce_buffer );
4433 psa_aead_abort( &operation );
4434 PSA_DONE( );
4435}
4436/* END_CASE */
4437
4438/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004439void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4440 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004441 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004442 data_t *nonce,
4443 data_t *additional_data,
4444 data_t *input_data,
4445 int expected_status_arg )
4446{
4447
4448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4449 psa_key_type_t key_type = key_type_arg;
4450 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004451 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004452 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4453 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4454 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004455 unsigned char *output = NULL;
4456 unsigned char *ciphertext = NULL;
4457 size_t output_size = output_size_arg;
4458 size_t ciphertext_size = 0;
4459 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004460 size_t tag_length = 0;
4461 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4462
4463 PSA_ASSERT( psa_crypto_init( ) );
4464
4465 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4466 psa_set_key_algorithm( &attributes, alg );
4467 psa_set_key_type( &attributes, key_type );
4468
4469 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4470 &key ) );
4471
4472 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4473
Paul Elliottc6d11d02021-09-01 12:04:23 +01004474 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004475
Paul Elliottc6d11d02021-09-01 12:04:23 +01004476 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004477
Paul Elliottc6d11d02021-09-01 12:04:23 +01004478 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004479
Paul Elliott43fbda62021-07-23 18:30:59 +01004480 status = psa_aead_encrypt_setup( &operation, key, alg );
4481
4482 /* If the operation is not supported, just skip and not fail in case the
4483 * encryption involves a common limitation of cryptography hardwares and
4484 * an alternative implementation. */
4485 if( status == PSA_ERROR_NOT_SUPPORTED )
4486 {
4487 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4488 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4489 }
4490
4491 PSA_ASSERT( status );
4492
Paul Elliott47b9a142021-10-07 15:04:57 +01004493 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4494 input_data->len ) );
4495
Paul Elliott43fbda62021-07-23 18:30:59 +01004496 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4497
4498 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4499 additional_data->len ) );
4500
4501 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004502 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004503
4504 TEST_EQUAL( status, expected_status );
4505
4506 if( expected_status == PSA_SUCCESS )
4507 {
4508 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004509 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4510 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004511 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4512 }
4513
4514exit:
4515 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004516 mbedtls_free( output );
4517 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004518 psa_aead_abort( &operation );
4519 PSA_DONE( );
4520}
4521/* END_CASE */
4522
Paul Elliott91b021e2021-07-23 18:52:31 +01004523/* BEGIN_CASE */
4524void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4525 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004526 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004527 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004528 data_t *nonce,
4529 data_t *additional_data,
4530 data_t *input_data,
4531 int expected_status_arg )
4532{
4533
4534 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4535 psa_key_type_t key_type = key_type_arg;
4536 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004537 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4539 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4540 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004541 unsigned char *ciphertext = NULL;
4542 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004543 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004544 size_t ciphertext_size = 0;
4545 size_t ciphertext_length = 0;
4546 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004547 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004548 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004549
4550 PSA_ASSERT( psa_crypto_init( ) );
4551
4552 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4553 psa_set_key_algorithm( &attributes, alg );
4554 psa_set_key_type( &attributes, key_type );
4555
4556 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4557 &key ) );
4558
4559 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4560
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004561 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004562
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004563 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004564
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004565 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004566
Paul Elliott719c1322021-09-13 18:27:22 +01004567 ASSERT_ALLOC( tag_buffer, tag_size );
4568
Paul Elliott91b021e2021-07-23 18:52:31 +01004569 status = psa_aead_encrypt_setup( &operation, key, alg );
4570
4571 /* If the operation is not supported, just skip and not fail in case the
4572 * encryption involves a common limitation of cryptography hardwares and
4573 * an alternative implementation. */
4574 if( status == PSA_ERROR_NOT_SUPPORTED )
4575 {
4576 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4577 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4578 }
4579
4580 PSA_ASSERT( status );
4581
4582 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4583
Paul Elliott76bda482021-10-07 17:07:23 +01004584 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4585 input_data->len ) );
4586
Paul Elliott91b021e2021-07-23 18:52:31 +01004587 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4588 additional_data->len ) );
4589
4590 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004591 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004592
4593 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004594 status = psa_aead_finish( &operation, finish_ciphertext,
4595 finish_ciphertext_size,
4596 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004597 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004598
4599 TEST_EQUAL( status, expected_status );
4600
4601exit:
4602 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004603 mbedtls_free( ciphertext );
4604 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004605 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004606 psa_aead_abort( &operation );
4607 PSA_DONE( );
4608}
4609/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004610
4611/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004612void aead_multipart_verify( int key_type_arg, data_t *key_data,
4613 int alg_arg,
4614 data_t *nonce,
4615 data_t *additional_data,
4616 data_t *input_data,
4617 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004618 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004619 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004620 int expected_status_arg )
4621{
4622 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4623 psa_key_type_t key_type = key_type_arg;
4624 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004625 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4627 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4628 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004629 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004630 unsigned char *plaintext = NULL;
4631 unsigned char *finish_plaintext = NULL;
4632 size_t plaintext_size = 0;
4633 size_t plaintext_length = 0;
4634 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004635 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004636 unsigned char *tag_buffer = NULL;
4637 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004638
4639 PSA_ASSERT( psa_crypto_init( ) );
4640
4641 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4642 psa_set_key_algorithm( &attributes, alg );
4643 psa_set_key_type( &attributes, key_type );
4644
4645 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4646 &key ) );
4647
4648 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4649
4650 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4651 input_data->len );
4652
4653 ASSERT_ALLOC( plaintext, plaintext_size );
4654
4655 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4656
4657 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4658
Paul Elliott9961a662021-09-17 19:19:02 +01004659 status = psa_aead_decrypt_setup( &operation, key, alg );
4660
4661 /* If the operation is not supported, just skip and not fail in case the
4662 * encryption involves a common limitation of cryptography hardwares and
4663 * an alternative implementation. */
4664 if( status == PSA_ERROR_NOT_SUPPORTED )
4665 {
4666 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4667 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4668 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004669 TEST_EQUAL( status, expected_setup_status );
4670
4671 if( status != PSA_SUCCESS )
4672 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004673
4674 PSA_ASSERT( status );
4675
4676 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4677
Paul Elliottfec6f372021-10-06 17:15:02 +01004678 status = psa_aead_set_lengths( &operation, additional_data->len,
4679 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004680 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004681
Paul Elliott9961a662021-09-17 19:19:02 +01004682 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4683 additional_data->len ) );
4684
4685 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4686 input_data->len,
4687 plaintext, plaintext_size,
4688 &plaintext_length ) );
4689
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004690 if( tag_usage == USE_GIVEN_TAG )
4691 {
4692 tag_buffer = tag->x;
4693 tag_size = tag->len;
4694 }
4695
Paul Elliott9961a662021-09-17 19:19:02 +01004696 status = psa_aead_verify( &operation, finish_plaintext,
4697 verify_plaintext_size,
4698 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004699 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004700
4701 TEST_EQUAL( status, expected_status );
4702
4703exit:
4704 psa_destroy_key( key );
4705 mbedtls_free( plaintext );
4706 mbedtls_free( finish_plaintext );
4707 psa_aead_abort( &operation );
4708 PSA_DONE( );
4709}
4710/* END_CASE */
4711
Paul Elliott9961a662021-09-17 19:19:02 +01004712/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004713void aead_multipart_setup( int key_type_arg, data_t *key_data,
4714 int alg_arg, int expected_status_arg )
4715{
4716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4717 psa_key_type_t key_type = key_type_arg;
4718 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004719 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4721 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4722 psa_status_t expected_status = expected_status_arg;
4723
4724 PSA_ASSERT( psa_crypto_init( ) );
4725
4726 psa_set_key_usage_flags( &attributes,
4727 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4728 psa_set_key_algorithm( &attributes, alg );
4729 psa_set_key_type( &attributes, key_type );
4730
4731 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4732 &key ) );
4733
Paul Elliott5221ef62021-09-19 17:33:03 +01004734 status = psa_aead_encrypt_setup( &operation, key, alg );
4735
4736 TEST_EQUAL( status, expected_status );
4737
4738 psa_aead_abort( &operation );
4739
Paul Elliott5221ef62021-09-19 17:33:03 +01004740 status = psa_aead_decrypt_setup( &operation, key, alg );
4741
4742 TEST_EQUAL(status, expected_status );
4743
4744exit:
4745 psa_destroy_key( key );
4746 psa_aead_abort( &operation );
4747 PSA_DONE( );
4748}
4749/* END_CASE */
4750
4751/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004752void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4753 int alg_arg,
4754 data_t *nonce,
4755 data_t *additional_data,
4756 data_t *input_data )
4757{
4758 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4759 psa_key_type_t key_type = key_type_arg;
4760 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004761 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004762 unsigned char *output_data = NULL;
4763 unsigned char *final_data = NULL;
4764 size_t output_size = 0;
4765 size_t finish_output_size = 0;
4766 size_t output_length = 0;
4767 size_t key_bits = 0;
4768 size_t tag_length = 0;
4769 size_t tag_size = 0;
4770 size_t nonce_length = 0;
4771 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4772 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4773 size_t output_part_length = 0;
4774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4775
4776 PSA_ASSERT( psa_crypto_init( ) );
4777
4778 psa_set_key_usage_flags( & attributes,
4779 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4780 psa_set_key_algorithm( & attributes, alg );
4781 psa_set_key_type( & attributes, key_type );
4782
4783 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4784 &key ) );
4785
4786 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4787 key_bits = psa_get_key_bits( &attributes );
4788
4789 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4790
4791 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4792
4793 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4794
4795 ASSERT_ALLOC( output_data, output_size );
4796
4797 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4798
4799 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4800
4801 ASSERT_ALLOC( final_data, finish_output_size );
4802
4803 /* Test all operations error without calling setup first. */
4804
Paul Elliottc23a9a02021-06-21 18:32:46 +01004805 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4806 PSA_ERROR_BAD_STATE );
4807
4808 psa_aead_abort( &operation );
4809
Paul Elliottc23a9a02021-06-21 18:32:46 +01004810 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4811 PSA_AEAD_NONCE_MAX_SIZE,
4812 &nonce_length ),
4813 PSA_ERROR_BAD_STATE );
4814
4815 psa_aead_abort( &operation );
4816
Paul Elliott481be342021-07-16 17:38:47 +01004817 /* ------------------------------------------------------- */
4818
Paul Elliottc23a9a02021-06-21 18:32:46 +01004819 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4820 input_data->len ),
4821 PSA_ERROR_BAD_STATE );
4822
4823 psa_aead_abort( &operation );
4824
Paul Elliott481be342021-07-16 17:38:47 +01004825 /* ------------------------------------------------------- */
4826
Paul Elliottc23a9a02021-06-21 18:32:46 +01004827 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4828 additional_data->len ),
4829 PSA_ERROR_BAD_STATE );
4830
4831 psa_aead_abort( &operation );
4832
Paul Elliott481be342021-07-16 17:38:47 +01004833 /* ------------------------------------------------------- */
4834
Paul Elliottc23a9a02021-06-21 18:32:46 +01004835 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4836 input_data->len, output_data,
4837 output_size, &output_length ),
4838 PSA_ERROR_BAD_STATE );
4839
4840 psa_aead_abort( &operation );
4841
Paul Elliott481be342021-07-16 17:38:47 +01004842 /* ------------------------------------------------------- */
4843
Paul Elliottc23a9a02021-06-21 18:32:46 +01004844 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4845 finish_output_size,
4846 &output_part_length,
4847 tag_buffer, tag_length,
4848 &tag_size ),
4849 PSA_ERROR_BAD_STATE );
4850
4851 psa_aead_abort( &operation );
4852
Paul Elliott481be342021-07-16 17:38:47 +01004853 /* ------------------------------------------------------- */
4854
Paul Elliottc23a9a02021-06-21 18:32:46 +01004855 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4856 finish_output_size,
4857 &output_part_length,
4858 tag_buffer,
4859 tag_length ),
4860 PSA_ERROR_BAD_STATE );
4861
4862 psa_aead_abort( &operation );
4863
4864 /* Test for double setups. */
4865
Paul Elliottc23a9a02021-06-21 18:32:46 +01004866 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4867
4868 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4869 PSA_ERROR_BAD_STATE );
4870
4871 psa_aead_abort( &operation );
4872
Paul Elliott481be342021-07-16 17:38:47 +01004873 /* ------------------------------------------------------- */
4874
Paul Elliottc23a9a02021-06-21 18:32:46 +01004875 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4876
4877 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4878 PSA_ERROR_BAD_STATE );
4879
4880 psa_aead_abort( &operation );
4881
Paul Elliott374a2be2021-07-16 17:53:40 +01004882 /* ------------------------------------------------------- */
4883
Paul Elliott374a2be2021-07-16 17:53:40 +01004884 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4885
4886 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4887 PSA_ERROR_BAD_STATE );
4888
4889 psa_aead_abort( &operation );
4890
4891 /* ------------------------------------------------------- */
4892
Paul Elliott374a2be2021-07-16 17:53:40 +01004893 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4894
4895 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4896 PSA_ERROR_BAD_STATE );
4897
4898 psa_aead_abort( &operation );
4899
Paul Elliottc23a9a02021-06-21 18:32:46 +01004900 /* Test for not setting a nonce. */
4901
Paul Elliottc23a9a02021-06-21 18:32:46 +01004902 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4903
4904 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4905 additional_data->len ),
4906 PSA_ERROR_BAD_STATE );
4907
4908 psa_aead_abort( &operation );
4909
Paul Elliott7f628422021-09-01 12:08:29 +01004910 /* ------------------------------------------------------- */
4911
Paul Elliott7f628422021-09-01 12:08:29 +01004912 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4913
4914 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4915 input_data->len, output_data,
4916 output_size, &output_length ),
4917 PSA_ERROR_BAD_STATE );
4918
4919 psa_aead_abort( &operation );
4920
Paul Elliottbdc2c682021-09-21 18:37:10 +01004921 /* ------------------------------------------------------- */
4922
Paul Elliottbdc2c682021-09-21 18:37:10 +01004923 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4924
4925 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4926 finish_output_size,
4927 &output_part_length,
4928 tag_buffer, tag_length,
4929 &tag_size ),
4930 PSA_ERROR_BAD_STATE );
4931
4932 psa_aead_abort( &operation );
4933
4934 /* ------------------------------------------------------- */
4935
Paul Elliottbdc2c682021-09-21 18:37:10 +01004936 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4937
4938 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4939 finish_output_size,
4940 &output_part_length,
4941 tag_buffer,
4942 tag_length ),
4943 PSA_ERROR_BAD_STATE );
4944
4945 psa_aead_abort( &operation );
4946
Paul Elliottc23a9a02021-06-21 18:32:46 +01004947 /* Test for double setting nonce. */
4948
Paul Elliottc23a9a02021-06-21 18:32:46 +01004949 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4950
4951 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4952
4953 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4954 PSA_ERROR_BAD_STATE );
4955
4956 psa_aead_abort( &operation );
4957
Paul Elliott374a2be2021-07-16 17:53:40 +01004958 /* Test for double generating nonce. */
4959
Paul Elliott374a2be2021-07-16 17:53:40 +01004960 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4961
4962 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4963 PSA_AEAD_NONCE_MAX_SIZE,
4964 &nonce_length ) );
4965
4966 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4967 PSA_AEAD_NONCE_MAX_SIZE,
4968 &nonce_length ),
4969 PSA_ERROR_BAD_STATE );
4970
4971
4972 psa_aead_abort( &operation );
4973
4974 /* Test for generate nonce then set and vice versa */
4975
Paul Elliott374a2be2021-07-16 17:53:40 +01004976 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4977
4978 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4979 PSA_AEAD_NONCE_MAX_SIZE,
4980 &nonce_length ) );
4981
4982 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4983 PSA_ERROR_BAD_STATE );
4984
4985 psa_aead_abort( &operation );
4986
Andrzej Kurekad837522021-12-15 15:28:49 +01004987 /* Test for generating nonce after calling set lengths */
4988
4989 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4990
4991 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4992 input_data->len ) );
4993
4994 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4995 PSA_AEAD_NONCE_MAX_SIZE,
4996 &nonce_length ) );
4997
4998 psa_aead_abort( &operation );
4999
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005000 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005001
5002 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5003
5004 if( operation.alg == PSA_ALG_CCM )
5005 {
5006 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5007 input_data->len ),
5008 PSA_ERROR_INVALID_ARGUMENT );
5009 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5010 PSA_AEAD_NONCE_MAX_SIZE,
5011 &nonce_length ),
5012 PSA_ERROR_BAD_STATE );
5013 }
5014 else
5015 {
5016 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5017 input_data->len ) );
5018 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5019 PSA_AEAD_NONCE_MAX_SIZE,
5020 &nonce_length ) );
5021 }
5022
5023 psa_aead_abort( &operation );
5024
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005025 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005026#if SIZE_MAX > UINT32_MAX
5027 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5028
5029 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5030 {
5031 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5032 input_data->len ),
5033 PSA_ERROR_INVALID_ARGUMENT );
5034 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5035 PSA_AEAD_NONCE_MAX_SIZE,
5036 &nonce_length ),
5037 PSA_ERROR_BAD_STATE );
5038 }
5039 else
5040 {
5041 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5042 input_data->len ) );
5043 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5044 PSA_AEAD_NONCE_MAX_SIZE,
5045 &nonce_length ) );
5046 }
5047
5048 psa_aead_abort( &operation );
5049#endif
5050
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005051 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005052
5053 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5054
5055 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5056 PSA_AEAD_NONCE_MAX_SIZE,
5057 &nonce_length ) );
5058
5059 if( operation.alg == PSA_ALG_CCM )
5060 {
5061 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5062 input_data->len ),
5063 PSA_ERROR_INVALID_ARGUMENT );
5064 }
5065 else
5066 {
5067 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5068 input_data->len ) );
5069 }
5070
5071 psa_aead_abort( &operation );
5072
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005073 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005074 /* Test for setting nonce after calling set lengths */
5075
5076 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5077
5078 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5079 input_data->len ) );
5080
5081 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5082
5083 psa_aead_abort( &operation );
5084
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005085 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005086
5087 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5088
5089 if( operation.alg == PSA_ALG_CCM )
5090 {
5091 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5092 input_data->len ),
5093 PSA_ERROR_INVALID_ARGUMENT );
5094 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5095 PSA_ERROR_BAD_STATE );
5096 }
5097 else
5098 {
5099 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5100 input_data->len ) );
5101 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5102 }
5103
5104 psa_aead_abort( &operation );
5105
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005106 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005107#if SIZE_MAX > UINT32_MAX
5108 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5109
5110 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5111 {
5112 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5113 input_data->len ),
5114 PSA_ERROR_INVALID_ARGUMENT );
5115 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5116 PSA_ERROR_BAD_STATE );
5117 }
5118 else
5119 {
5120 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5121 input_data->len ) );
5122 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5123 }
5124
5125 psa_aead_abort( &operation );
5126#endif
5127
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005128 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005129
5130 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5131
5132 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5133
5134 if( operation.alg == PSA_ALG_CCM )
5135 {
5136 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5137 input_data->len ),
5138 PSA_ERROR_INVALID_ARGUMENT );
5139 }
5140 else
5141 {
5142 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5143 input_data->len ) );
5144 }
5145
5146 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005147
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005148 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005149#if SIZE_MAX > UINT32_MAX
5150 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5151
5152 if( operation.alg == PSA_ALG_GCM )
5153 {
5154 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5155 SIZE_MAX ),
5156 PSA_ERROR_INVALID_ARGUMENT );
5157 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5158 PSA_ERROR_BAD_STATE );
5159 }
5160 else if ( operation.alg != PSA_ALG_CCM )
5161 {
5162 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5163 SIZE_MAX ) );
5164 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5165 }
5166
5167 psa_aead_abort( &operation );
5168
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005169 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005170 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5171
5172 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5173
5174 if( operation.alg == PSA_ALG_GCM )
5175 {
5176 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5177 SIZE_MAX ),
5178 PSA_ERROR_INVALID_ARGUMENT );
5179 }
5180 else if ( operation.alg != PSA_ALG_CCM )
5181 {
5182 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5183 SIZE_MAX ) );
5184 }
5185
5186 psa_aead_abort( &operation );
5187#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005188
5189 /* ------------------------------------------------------- */
5190
Paul Elliott374a2be2021-07-16 17:53:40 +01005191 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5192
5193 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5194
5195 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5196 PSA_AEAD_NONCE_MAX_SIZE,
5197 &nonce_length ),
5198 PSA_ERROR_BAD_STATE );
5199
5200 psa_aead_abort( &operation );
5201
Paul Elliott7220cae2021-06-22 17:25:57 +01005202 /* Test for generating nonce in decrypt setup. */
5203
Paul Elliott7220cae2021-06-22 17:25:57 +01005204 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5205
5206 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5207 PSA_AEAD_NONCE_MAX_SIZE,
5208 &nonce_length ),
5209 PSA_ERROR_BAD_STATE );
5210
5211 psa_aead_abort( &operation );
5212
Paul Elliottc23a9a02021-06-21 18:32:46 +01005213 /* Test for setting lengths twice. */
5214
Paul Elliottc23a9a02021-06-21 18:32:46 +01005215 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5216
5217 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5218
5219 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5220 input_data->len ) );
5221
5222 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5223 input_data->len ),
5224 PSA_ERROR_BAD_STATE );
5225
5226 psa_aead_abort( &operation );
5227
Andrzej Kurekad837522021-12-15 15:28:49 +01005228 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005229
Paul Elliottc23a9a02021-06-21 18:32:46 +01005230 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5231
5232 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5233
Andrzej Kurekad837522021-12-15 15:28:49 +01005234 if( operation.alg == PSA_ALG_CCM )
5235 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005236
Andrzej Kurekad837522021-12-15 15:28:49 +01005237 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5238 additional_data->len ),
5239 PSA_ERROR_BAD_STATE );
5240 }
5241 else
5242 {
5243 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5244 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005245
Andrzej Kurekad837522021-12-15 15:28:49 +01005246 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5247 input_data->len ),
5248 PSA_ERROR_BAD_STATE );
5249 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005250 psa_aead_abort( &operation );
5251
5252 /* ------------------------------------------------------- */
5253
Paul Elliottf94bd992021-09-19 18:15:59 +01005254 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5255
5256 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5257
Andrzej Kurekad837522021-12-15 15:28:49 +01005258 if( operation.alg == PSA_ALG_CCM )
5259 {
5260 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5261 input_data->len, output_data,
5262 output_size, &output_length ),
5263 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005264
Andrzej Kurekad837522021-12-15 15:28:49 +01005265 }
5266 else
5267 {
5268 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5269 input_data->len, output_data,
5270 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005271
Andrzej Kurekad837522021-12-15 15:28:49 +01005272 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5273 input_data->len ),
5274 PSA_ERROR_BAD_STATE );
5275 }
5276 psa_aead_abort( &operation );
5277
5278 /* ------------------------------------------------------- */
5279
5280 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5281
5282 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5283
5284 if( operation.alg == PSA_ALG_CCM )
5285 {
5286 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5287 finish_output_size,
5288 &output_part_length,
5289 tag_buffer, tag_length,
5290 &tag_size ) );
5291 }
5292 else
5293 {
5294 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5295 finish_output_size,
5296 &output_part_length,
5297 tag_buffer, tag_length,
5298 &tag_size ) );
5299
5300 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5301 input_data->len ),
5302 PSA_ERROR_BAD_STATE );
5303 }
5304 psa_aead_abort( &operation );
5305
5306 /* Test for setting lengths after generating nonce + already starting data. */
5307
5308 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5309
5310 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5311 PSA_AEAD_NONCE_MAX_SIZE,
5312 &nonce_length ) );
5313 if( operation.alg == PSA_ALG_CCM )
5314 {
5315
5316 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5317 additional_data->len ),
5318 PSA_ERROR_BAD_STATE );
5319 }
5320 else
5321 {
5322 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5323 additional_data->len ) );
5324
5325 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5326 input_data->len ),
5327 PSA_ERROR_BAD_STATE );
5328 }
5329 psa_aead_abort( &operation );
5330
5331 /* ------------------------------------------------------- */
5332
5333 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5334
5335 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5336 PSA_AEAD_NONCE_MAX_SIZE,
5337 &nonce_length ) );
5338 if( operation.alg == PSA_ALG_CCM )
5339 {
5340 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5341 input_data->len, output_data,
5342 output_size, &output_length ),
5343 PSA_ERROR_BAD_STATE );
5344
5345 }
5346 else
5347 {
5348 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5349 input_data->len, output_data,
5350 output_size, &output_length ) );
5351
5352 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5353 input_data->len ),
5354 PSA_ERROR_BAD_STATE );
5355 }
5356 psa_aead_abort( &operation );
5357
5358 /* ------------------------------------------------------- */
5359
5360 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5361
5362 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5363 PSA_AEAD_NONCE_MAX_SIZE,
5364 &nonce_length ) );
5365 if( operation.alg == PSA_ALG_CCM )
5366 {
5367 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5368 finish_output_size,
5369 &output_part_length,
5370 tag_buffer, tag_length,
5371 &tag_size ) );
5372 }
5373 else
5374 {
5375 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5376 finish_output_size,
5377 &output_part_length,
5378 tag_buffer, tag_length,
5379 &tag_size ) );
5380
5381 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5382 input_data->len ),
5383 PSA_ERROR_BAD_STATE );
5384 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005385 psa_aead_abort( &operation );
5386
Paul Elliott243080c2021-07-21 19:01:17 +01005387 /* Test for not sending any additional data or data after setting non zero
5388 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005389
Paul Elliottc23a9a02021-06-21 18:32:46 +01005390 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5391
5392 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5393
5394 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5395 input_data->len ) );
5396
5397 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5398 finish_output_size,
5399 &output_part_length,
5400 tag_buffer, tag_length,
5401 &tag_size ),
5402 PSA_ERROR_INVALID_ARGUMENT );
5403
5404 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. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005408
Paul Elliottc23a9a02021-06-21 18:32:46 +01005409 PSA_ASSERT( psa_aead_decrypt_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_verify( &operation, final_data,
5417 finish_output_size,
5418 &output_part_length,
5419 tag_buffer,
5420 tag_length ),
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 after setting a non-zero length
5426 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005427
Paul Elliottc23a9a02021-06-21 18:32:46 +01005428 PSA_ASSERT( psa_aead_encrypt_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_update( &operation, input_data->x,
5436 input_data->len, output_data,
5437 output_size, &output_length ),
5438 PSA_ERROR_INVALID_ARGUMENT );
5439
5440 psa_aead_abort( &operation );
5441
Paul Elliottf94bd992021-09-19 18:15:59 +01005442 /* Test for not sending any data after setting a non-zero length for it.*/
5443
Paul Elliottf94bd992021-09-19 18:15:59 +01005444 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5445
5446 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5447
5448 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5449 input_data->len ) );
5450
5451 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5452 additional_data->len ) );
5453
5454 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5455 finish_output_size,
5456 &output_part_length,
5457 tag_buffer, tag_length,
5458 &tag_size ),
5459 PSA_ERROR_INVALID_ARGUMENT );
5460
5461 psa_aead_abort( &operation );
5462
Paul Elliottb0450fe2021-09-01 15:06:26 +01005463 /* Test for sending too much additional data after setting lengths. */
5464
Paul Elliottb0450fe2021-09-01 15:06:26 +01005465 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5466
5467 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5468
5469 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5470
5471
5472 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5473 additional_data->len ),
5474 PSA_ERROR_INVALID_ARGUMENT );
5475
5476 psa_aead_abort( &operation );
5477
Paul Elliotta2a09b02021-09-22 14:56:40 +01005478 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005479
5480 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5481
5482 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5483
5484 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5485 input_data->len ) );
5486
5487 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5488 additional_data->len ) );
5489
5490 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5491 1 ),
5492 PSA_ERROR_INVALID_ARGUMENT );
5493
5494 psa_aead_abort( &operation );
5495
Paul Elliottb0450fe2021-09-01 15:06:26 +01005496 /* Test for sending too much data after setting lengths. */
5497
Paul Elliottb0450fe2021-09-01 15:06:26 +01005498 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5499
5500 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5501
5502 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5503
5504 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5505 input_data->len, output_data,
5506 output_size, &output_length ),
5507 PSA_ERROR_INVALID_ARGUMENT );
5508
5509 psa_aead_abort( &operation );
5510
Paul Elliotta2a09b02021-09-22 14:56:40 +01005511 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005512
5513 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5514
5515 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5516
5517 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5518 input_data->len ) );
5519
5520 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5521 additional_data->len ) );
5522
5523 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5524 input_data->len, output_data,
5525 output_size, &output_length ) );
5526
5527 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5528 1, output_data,
5529 output_size, &output_length ),
5530 PSA_ERROR_INVALID_ARGUMENT );
5531
5532 psa_aead_abort( &operation );
5533
Paul Elliottc23a9a02021-06-21 18:32:46 +01005534 /* Test sending additional data after data. */
5535
Paul Elliottc23a9a02021-06-21 18:32:46 +01005536 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5537
5538 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5539
Andrzej Kurekad837522021-12-15 15:28:49 +01005540 if( operation.alg != PSA_ALG_CCM )
5541 {
5542 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5543 input_data->len, output_data,
5544 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005545
Andrzej Kurekad837522021-12-15 15:28:49 +01005546 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5547 additional_data->len ),
5548 PSA_ERROR_BAD_STATE );
5549 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005550 psa_aead_abort( &operation );
5551
Paul Elliott534d0b42021-06-22 19:15:20 +01005552 /* Test calling finish on decryption. */
5553
Paul Elliott534d0b42021-06-22 19:15:20 +01005554 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5555
5556 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5557
5558 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5559 finish_output_size,
5560 &output_part_length,
5561 tag_buffer, tag_length,
5562 &tag_size ),
5563 PSA_ERROR_BAD_STATE );
5564
5565 psa_aead_abort( &operation );
5566
5567 /* Test calling verify on encryption. */
5568
Paul Elliott534d0b42021-06-22 19:15:20 +01005569 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5570
5571 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5572
5573 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5574 finish_output_size,
5575 &output_part_length,
5576 tag_buffer,
5577 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005578 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005579
5580 psa_aead_abort( &operation );
5581
5582
Paul Elliottc23a9a02021-06-21 18:32:46 +01005583exit:
5584 psa_destroy_key( key );
5585 psa_aead_abort( &operation );
5586 mbedtls_free( output_data );
5587 mbedtls_free( final_data );
5588 PSA_DONE( );
5589}
5590/* END_CASE */
5591
5592/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005593void signature_size( int type_arg,
5594 int bits,
5595 int alg_arg,
5596 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005597{
5598 psa_key_type_t type = type_arg;
5599 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005600 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005601
Gilles Peskinefe11b722018-12-18 00:24:04 +01005602 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005603
Gilles Peskinee59236f2018-01-27 23:32:46 +01005604exit:
5605 ;
5606}
5607/* END_CASE */
5608
5609/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005610void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5611 int alg_arg, data_t *input_data,
5612 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005613{
Ronald Cron5425a212020-08-04 14:58:35 +02005614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005615 psa_key_type_t key_type = key_type_arg;
5616 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005617 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005618 unsigned char *signature = NULL;
5619 size_t signature_size;
5620 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005622
Gilles Peskine8817f612018-12-18 00:18:46 +01005623 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005624
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005625 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005626 psa_set_key_algorithm( &attributes, alg );
5627 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005628
Gilles Peskine049c7532019-05-15 20:22:09 +02005629 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005630 &key ) );
5631 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005632 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005633
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005634 /* Allocate a buffer which has the size advertized by the
5635 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005636 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005637 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005638 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005639 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005640 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005641
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005642 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005643 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005644 input_data->x, input_data->len,
5645 signature, signature_size,
5646 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005647 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005648 ASSERT_COMPARE( output_data->x, output_data->len,
5649 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005650
5651exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005652 /*
5653 * Key attributes may have been returned by psa_get_key_attributes()
5654 * thus reset them as required.
5655 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005656 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005657
Ronald Cron5425a212020-08-04 14:58:35 +02005658 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005659 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005660 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005661}
5662/* END_CASE */
5663
5664/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005665void sign_hash_fail( int key_type_arg, data_t *key_data,
5666 int alg_arg, data_t *input_data,
5667 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005668{
Ronald Cron5425a212020-08-04 14:58:35 +02005669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005670 psa_key_type_t key_type = key_type_arg;
5671 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005672 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005673 psa_status_t actual_status;
5674 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005675 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005676 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005678
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005679 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005680
Gilles Peskine8817f612018-12-18 00:18:46 +01005681 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005682
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005683 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005684 psa_set_key_algorithm( &attributes, alg );
5685 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005686
Gilles Peskine049c7532019-05-15 20:22:09 +02005687 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005688 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005689
Ronald Cron5425a212020-08-04 14:58:35 +02005690 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005691 input_data->x, input_data->len,
5692 signature, signature_size,
5693 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005694 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005695 /* The value of *signature_length is unspecified on error, but
5696 * whatever it is, it should be less than signature_size, so that
5697 * if the caller tries to read *signature_length bytes without
5698 * checking the error code then they don't overflow a buffer. */
5699 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005700
5701exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005702 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005703 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005704 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005705 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005706}
5707/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005708
5709/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005710void sign_verify_hash( int key_type_arg, data_t *key_data,
5711 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005712{
Ronald Cron5425a212020-08-04 14:58:35 +02005713 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005714 psa_key_type_t key_type = key_type_arg;
5715 psa_algorithm_t alg = alg_arg;
5716 size_t key_bits;
5717 unsigned char *signature = NULL;
5718 size_t signature_size;
5719 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005721
Gilles Peskine8817f612018-12-18 00:18:46 +01005722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005723
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005724 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005725 psa_set_key_algorithm( &attributes, alg );
5726 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005727
Gilles Peskine049c7532019-05-15 20:22:09 +02005728 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005729 &key ) );
5730 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005731 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005732
5733 /* Allocate a buffer which has the size advertized by the
5734 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005735 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005736 key_bits, alg );
5737 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005738 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005739 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005740
5741 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005742 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005743 input_data->x, input_data->len,
5744 signature, signature_size,
5745 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005746 /* Check that the signature length looks sensible. */
5747 TEST_ASSERT( signature_length <= signature_size );
5748 TEST_ASSERT( signature_length > 0 );
5749
5750 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005751 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005752 input_data->x, input_data->len,
5753 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005754
5755 if( input_data->len != 0 )
5756 {
5757 /* Flip a bit in the input and verify that the signature is now
5758 * detected as invalid. Flip a bit at the beginning, not at the end,
5759 * because ECDSA may ignore the last few bits of the input. */
5760 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005761 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005762 input_data->x, input_data->len,
5763 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005764 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005765 }
5766
5767exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005768 /*
5769 * Key attributes may have been returned by psa_get_key_attributes()
5770 * thus reset them as required.
5771 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005772 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005773
Ronald Cron5425a212020-08-04 14:58:35 +02005774 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005775 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005776 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005777}
5778/* END_CASE */
5779
5780/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005781void verify_hash( int key_type_arg, data_t *key_data,
5782 int alg_arg, data_t *hash_data,
5783 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005784{
Ronald Cron5425a212020-08-04 14:58:35 +02005785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005786 psa_key_type_t key_type = key_type_arg;
5787 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005788 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005789
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005790 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005791
Gilles Peskine8817f612018-12-18 00:18:46 +01005792 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005793
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005794 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005795 psa_set_key_algorithm( &attributes, alg );
5796 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005797
Gilles Peskine049c7532019-05-15 20:22:09 +02005798 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005799 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005800
Ronald Cron5425a212020-08-04 14:58:35 +02005801 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005802 hash_data->x, hash_data->len,
5803 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005804
itayzafrir5c753392018-05-08 11:18:38 +03005805exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005806 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005807 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005808 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005809}
5810/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005811
5812/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005813void verify_hash_fail( int key_type_arg, data_t *key_data,
5814 int alg_arg, data_t *hash_data,
5815 data_t *signature_data,
5816 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005817{
Ronald Cron5425a212020-08-04 14:58:35 +02005818 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005819 psa_key_type_t key_type = key_type_arg;
5820 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005821 psa_status_t actual_status;
5822 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005823 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005824
Gilles Peskine8817f612018-12-18 00:18:46 +01005825 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005826
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005827 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005828 psa_set_key_algorithm( &attributes, alg );
5829 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005830
Gilles Peskine049c7532019-05-15 20:22:09 +02005831 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005832 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005833
Ronald Cron5425a212020-08-04 14:58:35 +02005834 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005835 hash_data->x, hash_data->len,
5836 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005837 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005838
5839exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005840 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005841 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005842 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005843}
5844/* END_CASE */
5845
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005846/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005847void sign_message_deterministic( int key_type_arg,
5848 data_t *key_data,
5849 int alg_arg,
5850 data_t *input_data,
5851 data_t *output_data )
5852{
5853 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5854 psa_key_type_t key_type = key_type_arg;
5855 psa_algorithm_t alg = alg_arg;
5856 size_t key_bits;
5857 unsigned char *signature = NULL;
5858 size_t signature_size;
5859 size_t signature_length = 0xdeadbeef;
5860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5861
5862 PSA_ASSERT( psa_crypto_init( ) );
5863
5864 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5865 psa_set_key_algorithm( &attributes, alg );
5866 psa_set_key_type( &attributes, key_type );
5867
5868 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5869 &key ) );
5870 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5871 key_bits = psa_get_key_bits( &attributes );
5872
5873 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5874 TEST_ASSERT( signature_size != 0 );
5875 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5876 ASSERT_ALLOC( signature, signature_size );
5877
5878 PSA_ASSERT( psa_sign_message( key, alg,
5879 input_data->x, input_data->len,
5880 signature, signature_size,
5881 &signature_length ) );
5882
5883 ASSERT_COMPARE( output_data->x, output_data->len,
5884 signature, signature_length );
5885
5886exit:
5887 psa_reset_key_attributes( &attributes );
5888
5889 psa_destroy_key( key );
5890 mbedtls_free( signature );
5891 PSA_DONE( );
5892
5893}
5894/* END_CASE */
5895
5896/* BEGIN_CASE */
5897void sign_message_fail( int key_type_arg,
5898 data_t *key_data,
5899 int alg_arg,
5900 data_t *input_data,
5901 int signature_size_arg,
5902 int expected_status_arg )
5903{
5904 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5905 psa_key_type_t key_type = key_type_arg;
5906 psa_algorithm_t alg = alg_arg;
5907 size_t signature_size = signature_size_arg;
5908 psa_status_t actual_status;
5909 psa_status_t expected_status = expected_status_arg;
5910 unsigned char *signature = NULL;
5911 size_t signature_length = 0xdeadbeef;
5912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5913
5914 ASSERT_ALLOC( signature, signature_size );
5915
5916 PSA_ASSERT( psa_crypto_init( ) );
5917
5918 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5919 psa_set_key_algorithm( &attributes, alg );
5920 psa_set_key_type( &attributes, key_type );
5921
5922 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5923 &key ) );
5924
5925 actual_status = psa_sign_message( key, alg,
5926 input_data->x, input_data->len,
5927 signature, signature_size,
5928 &signature_length );
5929 TEST_EQUAL( actual_status, expected_status );
5930 /* The value of *signature_length is unspecified on error, but
5931 * whatever it is, it should be less than signature_size, so that
5932 * if the caller tries to read *signature_length bytes without
5933 * checking the error code then they don't overflow a buffer. */
5934 TEST_ASSERT( signature_length <= signature_size );
5935
5936exit:
5937 psa_reset_key_attributes( &attributes );
5938 psa_destroy_key( key );
5939 mbedtls_free( signature );
5940 PSA_DONE( );
5941}
5942/* END_CASE */
5943
5944/* BEGIN_CASE */
5945void sign_verify_message( int key_type_arg,
5946 data_t *key_data,
5947 int alg_arg,
5948 data_t *input_data )
5949{
5950 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5951 psa_key_type_t key_type = key_type_arg;
5952 psa_algorithm_t alg = alg_arg;
5953 size_t key_bits;
5954 unsigned char *signature = NULL;
5955 size_t signature_size;
5956 size_t signature_length = 0xdeadbeef;
5957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5958
5959 PSA_ASSERT( psa_crypto_init( ) );
5960
5961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5962 PSA_KEY_USAGE_VERIFY_MESSAGE );
5963 psa_set_key_algorithm( &attributes, alg );
5964 psa_set_key_type( &attributes, key_type );
5965
5966 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5967 &key ) );
5968 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5969 key_bits = psa_get_key_bits( &attributes );
5970
5971 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5972 TEST_ASSERT( signature_size != 0 );
5973 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5974 ASSERT_ALLOC( signature, signature_size );
5975
5976 PSA_ASSERT( psa_sign_message( key, alg,
5977 input_data->x, input_data->len,
5978 signature, signature_size,
5979 &signature_length ) );
5980 TEST_ASSERT( signature_length <= signature_size );
5981 TEST_ASSERT( signature_length > 0 );
5982
5983 PSA_ASSERT( psa_verify_message( key, alg,
5984 input_data->x, input_data->len,
5985 signature, signature_length ) );
5986
5987 if( input_data->len != 0 )
5988 {
5989 /* Flip a bit in the input and verify that the signature is now
5990 * detected as invalid. Flip a bit at the beginning, not at the end,
5991 * because ECDSA may ignore the last few bits of the input. */
5992 input_data->x[0] ^= 1;
5993 TEST_EQUAL( psa_verify_message( key, alg,
5994 input_data->x, input_data->len,
5995 signature, signature_length ),
5996 PSA_ERROR_INVALID_SIGNATURE );
5997 }
5998
5999exit:
6000 psa_reset_key_attributes( &attributes );
6001
6002 psa_destroy_key( key );
6003 mbedtls_free( signature );
6004 PSA_DONE( );
6005}
6006/* END_CASE */
6007
6008/* BEGIN_CASE */
6009void verify_message( int key_type_arg,
6010 data_t *key_data,
6011 int alg_arg,
6012 data_t *input_data,
6013 data_t *signature_data )
6014{
6015 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6016 psa_key_type_t key_type = key_type_arg;
6017 psa_algorithm_t alg = alg_arg;
6018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6019
6020 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
6021
6022 PSA_ASSERT( psa_crypto_init( ) );
6023
6024 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6025 psa_set_key_algorithm( &attributes, alg );
6026 psa_set_key_type( &attributes, key_type );
6027
6028 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6029 &key ) );
6030
6031 PSA_ASSERT( psa_verify_message( key, alg,
6032 input_data->x, input_data->len,
6033 signature_data->x, signature_data->len ) );
6034
6035exit:
6036 psa_reset_key_attributes( &attributes );
6037 psa_destroy_key( key );
6038 PSA_DONE( );
6039}
6040/* END_CASE */
6041
6042/* BEGIN_CASE */
6043void verify_message_fail( int key_type_arg,
6044 data_t *key_data,
6045 int alg_arg,
6046 data_t *hash_data,
6047 data_t *signature_data,
6048 int expected_status_arg )
6049{
6050 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6051 psa_key_type_t key_type = key_type_arg;
6052 psa_algorithm_t alg = alg_arg;
6053 psa_status_t actual_status;
6054 psa_status_t expected_status = expected_status_arg;
6055 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6056
6057 PSA_ASSERT( psa_crypto_init( ) );
6058
6059 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6060 psa_set_key_algorithm( &attributes, alg );
6061 psa_set_key_type( &attributes, key_type );
6062
6063 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6064 &key ) );
6065
6066 actual_status = psa_verify_message( key, alg,
6067 hash_data->x, hash_data->len,
6068 signature_data->x,
6069 signature_data->len );
6070 TEST_EQUAL( actual_status, expected_status );
6071
6072exit:
6073 psa_reset_key_attributes( &attributes );
6074 psa_destroy_key( key );
6075 PSA_DONE( );
6076}
6077/* END_CASE */
6078
6079/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006080void asymmetric_encrypt( int key_type_arg,
6081 data_t *key_data,
6082 int alg_arg,
6083 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006084 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006085 int expected_output_length_arg,
6086 int expected_status_arg )
6087{
Ronald Cron5425a212020-08-04 14:58:35 +02006088 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006089 psa_key_type_t key_type = key_type_arg;
6090 psa_algorithm_t alg = alg_arg;
6091 size_t expected_output_length = expected_output_length_arg;
6092 size_t key_bits;
6093 unsigned char *output = NULL;
6094 size_t output_size;
6095 size_t output_length = ~0;
6096 psa_status_t actual_status;
6097 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006098 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006099
Gilles Peskine8817f612018-12-18 00:18:46 +01006100 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006101
Gilles Peskine656896e2018-06-29 19:12:28 +02006102 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006103 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6104 psa_set_key_algorithm( &attributes, alg );
6105 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006106 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006107 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006108
6109 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006110 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006111 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006112
Gilles Peskine656896e2018-06-29 19:12:28 +02006113 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006114 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006115 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006116
6117 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006118 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006119 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006120 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006121 output, output_size,
6122 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006123 TEST_EQUAL( actual_status, expected_status );
6124 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006125
Gilles Peskine68428122018-06-30 18:42:41 +02006126 /* If the label is empty, the test framework puts a non-null pointer
6127 * in label->x. Test that a null pointer works as well. */
6128 if( label->len == 0 )
6129 {
6130 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006131 if( output_size != 0 )
6132 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006133 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006134 input_data->x, input_data->len,
6135 NULL, label->len,
6136 output, output_size,
6137 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006138 TEST_EQUAL( actual_status, expected_status );
6139 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006140 }
6141
Gilles Peskine656896e2018-06-29 19:12:28 +02006142exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006143 /*
6144 * Key attributes may have been returned by psa_get_key_attributes()
6145 * thus reset them as required.
6146 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006147 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006148
Ronald Cron5425a212020-08-04 14:58:35 +02006149 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006150 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006151 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006152}
6153/* END_CASE */
6154
6155/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006156void asymmetric_encrypt_decrypt( int key_type_arg,
6157 data_t *key_data,
6158 int alg_arg,
6159 data_t *input_data,
6160 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006161{
Ronald Cron5425a212020-08-04 14:58:35 +02006162 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006163 psa_key_type_t key_type = key_type_arg;
6164 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006165 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006166 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006167 size_t output_size;
6168 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006169 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006170 size_t output2_size;
6171 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006173
Gilles Peskine8817f612018-12-18 00:18:46 +01006174 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006175
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006176 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6177 psa_set_key_algorithm( &attributes, alg );
6178 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006179
Gilles Peskine049c7532019-05-15 20:22:09 +02006180 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006181 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006182
6183 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006184 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006185 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006186
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006187 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006188 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006189 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006190
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006191 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006192 TEST_ASSERT( output2_size <=
6193 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6194 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006195 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006196
Gilles Peskineeebd7382018-06-08 18:11:54 +02006197 /* We test encryption by checking that encrypt-then-decrypt gives back
6198 * the original plaintext because of the non-optional random
6199 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006200 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006201 input_data->x, input_data->len,
6202 label->x, label->len,
6203 output, output_size,
6204 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006205 /* We don't know what ciphertext length to expect, but check that
6206 * it looks sensible. */
6207 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006208
Ronald Cron5425a212020-08-04 14:58:35 +02006209 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006210 output, output_length,
6211 label->x, label->len,
6212 output2, output2_size,
6213 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006214 ASSERT_COMPARE( input_data->x, input_data->len,
6215 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006216
6217exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006218 /*
6219 * Key attributes may have been returned by psa_get_key_attributes()
6220 * thus reset them as required.
6221 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006222 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006223
Ronald Cron5425a212020-08-04 14:58:35 +02006224 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006225 mbedtls_free( output );
6226 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006227 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006228}
6229/* END_CASE */
6230
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006231/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006232void asymmetric_decrypt( int key_type_arg,
6233 data_t *key_data,
6234 int alg_arg,
6235 data_t *input_data,
6236 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006237 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006238{
Ronald Cron5425a212020-08-04 14:58:35 +02006239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006240 psa_key_type_t key_type = key_type_arg;
6241 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006242 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006243 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006244 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006245 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006247
Gilles Peskine8817f612018-12-18 00:18:46 +01006248 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006249
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6251 psa_set_key_algorithm( &attributes, alg );
6252 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006253
Gilles Peskine049c7532019-05-15 20:22:09 +02006254 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006255 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006256
gabor-mezei-armceface22021-01-21 12:26:17 +01006257 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6258 key_bits = psa_get_key_bits( &attributes );
6259
6260 /* Determine the maximum ciphertext length */
6261 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6262 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6263 ASSERT_ALLOC( output, output_size );
6264
Ronald Cron5425a212020-08-04 14:58:35 +02006265 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006266 input_data->x, input_data->len,
6267 label->x, label->len,
6268 output,
6269 output_size,
6270 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006271 ASSERT_COMPARE( expected_data->x, expected_data->len,
6272 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006273
Gilles Peskine68428122018-06-30 18:42:41 +02006274 /* If the label is empty, the test framework puts a non-null pointer
6275 * in label->x. Test that a null pointer works as well. */
6276 if( label->len == 0 )
6277 {
6278 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006279 if( output_size != 0 )
6280 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006281 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006282 input_data->x, input_data->len,
6283 NULL, label->len,
6284 output,
6285 output_size,
6286 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006287 ASSERT_COMPARE( expected_data->x, expected_data->len,
6288 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006289 }
6290
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006291exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006292 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006293 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006294 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006295 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006296}
6297/* END_CASE */
6298
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006299/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006300void asymmetric_decrypt_fail( int key_type_arg,
6301 data_t *key_data,
6302 int alg_arg,
6303 data_t *input_data,
6304 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006305 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006306 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006307{
Ronald Cron5425a212020-08-04 14:58:35 +02006308 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006309 psa_key_type_t key_type = key_type_arg;
6310 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006311 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006312 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006313 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006314 psa_status_t actual_status;
6315 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006317
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006318 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006319
Gilles Peskine8817f612018-12-18 00:18:46 +01006320 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006321
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006322 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6323 psa_set_key_algorithm( &attributes, alg );
6324 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006325
Gilles Peskine049c7532019-05-15 20:22:09 +02006326 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006327 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006328
Ronald Cron5425a212020-08-04 14:58:35 +02006329 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006330 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006331 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006332 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006333 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006334 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006335 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006336
Gilles Peskine68428122018-06-30 18:42:41 +02006337 /* If the label is empty, the test framework puts a non-null pointer
6338 * in label->x. Test that a null pointer works as well. */
6339 if( label->len == 0 )
6340 {
6341 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006342 if( output_size != 0 )
6343 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006344 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006345 input_data->x, input_data->len,
6346 NULL, label->len,
6347 output, output_size,
6348 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006349 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006350 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006351 }
6352
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006353exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006354 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006355 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006356 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006357 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006358}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006359/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006360
6361/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006362void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006363{
6364 /* Test each valid way of initializing the object, except for `= {0}`, as
6365 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6366 * though it's OK by the C standard. We could test for this, but we'd need
6367 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006368 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006369 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6370 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6371 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006372
6373 memset( &zero, 0, sizeof( zero ) );
6374
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006375 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006376 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006377 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006378 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006379 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006380 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006381 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006382
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006383 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006384 PSA_ASSERT( psa_key_derivation_abort(&func) );
6385 PSA_ASSERT( psa_key_derivation_abort(&init) );
6386 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006387}
6388/* END_CASE */
6389
Janos Follath16de4a42019-06-13 16:32:24 +01006390/* BEGIN_CASE */
6391void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006392{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006393 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006394 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006395 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006396
Gilles Peskine8817f612018-12-18 00:18:46 +01006397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006398
Janos Follath16de4a42019-06-13 16:32:24 +01006399 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006400 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006401
6402exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006403 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006404 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006405}
6406/* END_CASE */
6407
Janos Follathaf3c2a02019-06-12 12:34:34 +01006408/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006409void derive_set_capacity( int alg_arg, int capacity_arg,
6410 int expected_status_arg )
6411{
6412 psa_algorithm_t alg = alg_arg;
6413 size_t capacity = capacity_arg;
6414 psa_status_t expected_status = expected_status_arg;
6415 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6416
6417 PSA_ASSERT( psa_crypto_init( ) );
6418
6419 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6420
6421 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6422 expected_status );
6423
6424exit:
6425 psa_key_derivation_abort( &operation );
6426 PSA_DONE( );
6427}
6428/* END_CASE */
6429
6430/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006431void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006432 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006433 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006434 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006435 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006436 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006437 int expected_status_arg3,
6438 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006439{
6440 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006441 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6442 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006443 psa_status_t expected_statuses[] = {expected_status_arg1,
6444 expected_status_arg2,
6445 expected_status_arg3};
6446 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006447 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6448 MBEDTLS_SVC_KEY_ID_INIT,
6449 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006450 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6452 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006453 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006454 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006455 psa_status_t expected_output_status = expected_output_status_arg;
6456 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006457
6458 PSA_ASSERT( psa_crypto_init( ) );
6459
6460 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6461 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006462
6463 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6464
6465 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6466 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006467 mbedtls_test_set_step( i );
6468 if( steps[i] == 0 )
6469 {
6470 /* Skip this step */
6471 }
6472 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006473 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006474 psa_set_key_type( &attributes, key_types[i] );
6475 PSA_ASSERT( psa_import_key( &attributes,
6476 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006477 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006478 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6479 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6480 {
6481 // When taking a private key as secret input, use key agreement
6482 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006483 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6484 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006485 expected_statuses[i] );
6486 }
6487 else
6488 {
6489 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006490 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006491 expected_statuses[i] );
6492 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006493 }
6494 else
6495 {
6496 TEST_EQUAL( psa_key_derivation_input_bytes(
6497 &operation, steps[i],
6498 inputs[i]->x, inputs[i]->len ),
6499 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006500 }
6501 }
6502
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006503 if( output_key_type != PSA_KEY_TYPE_NONE )
6504 {
6505 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006506 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006507 psa_set_key_bits( &attributes, 8 );
6508 actual_output_status =
6509 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006510 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006511 }
6512 else
6513 {
6514 uint8_t buffer[1];
6515 actual_output_status =
6516 psa_key_derivation_output_bytes( &operation,
6517 buffer, sizeof( buffer ) );
6518 }
6519 TEST_EQUAL( actual_output_status, expected_output_status );
6520
Janos Follathaf3c2a02019-06-12 12:34:34 +01006521exit:
6522 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006523 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6524 psa_destroy_key( keys[i] );
6525 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006526 PSA_DONE( );
6527}
6528/* END_CASE */
6529
Janos Follathd958bb72019-07-03 15:02:16 +01006530/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006531void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006532{
Janos Follathd958bb72019-07-03 15:02:16 +01006533 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006534 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006535 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006536 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006537 unsigned char input1[] = "Input 1";
6538 size_t input1_length = sizeof( input1 );
6539 unsigned char input2[] = "Input 2";
6540 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006541 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006542 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006543 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6544 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6545 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006546 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006547
Gilles Peskine8817f612018-12-18 00:18:46 +01006548 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006549
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006550 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6551 psa_set_key_algorithm( &attributes, alg );
6552 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006553
Gilles Peskine73676cb2019-05-15 20:15:10 +02006554 PSA_ASSERT( psa_import_key( &attributes,
6555 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006556 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006557
6558 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006559 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6560 input1, input1_length,
6561 input2, input2_length,
6562 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006563 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006564
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006565 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006566 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006567 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006568
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006569 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006570
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006571 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006572 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006573
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006574exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006575 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006576 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006577 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006578}
6579/* END_CASE */
6580
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006581/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006582void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006583{
6584 uint8_t output_buffer[16];
6585 size_t buffer_size = 16;
6586 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006587 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006588
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006589 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6590 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006591 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006592
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006593 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006594 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006595
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006596 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006597
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006598 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6599 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006600 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006601
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006602 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006603 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006604
6605exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006606 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006607}
6608/* END_CASE */
6609
6610/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006611void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006612 int step1_arg, data_t *input1,
6613 int step2_arg, data_t *input2,
6614 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006615 int requested_capacity_arg,
6616 data_t *expected_output1,
6617 data_t *expected_output2 )
6618{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006619 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006620 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6621 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006622 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6623 MBEDTLS_SVC_KEY_ID_INIT,
6624 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006625 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006626 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006627 uint8_t *expected_outputs[2] =
6628 {expected_output1->x, expected_output2->x};
6629 size_t output_sizes[2] =
6630 {expected_output1->len, expected_output2->len};
6631 size_t output_buffer_size = 0;
6632 uint8_t *output_buffer = NULL;
6633 size_t expected_capacity;
6634 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006636 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006637 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006638
6639 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6640 {
6641 if( output_sizes[i] > output_buffer_size )
6642 output_buffer_size = output_sizes[i];
6643 if( output_sizes[i] == 0 )
6644 expected_outputs[i] = NULL;
6645 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006646 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006647 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006648
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006649 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6650 psa_set_key_algorithm( &attributes, alg );
6651 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006652
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006653 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006654 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6655 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6656 requested_capacity ) );
6657 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006658 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006659 switch( steps[i] )
6660 {
6661 case 0:
6662 break;
6663 case PSA_KEY_DERIVATION_INPUT_SECRET:
6664 PSA_ASSERT( psa_import_key( &attributes,
6665 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006666 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006667
6668 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6669 {
6670 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6671 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6672 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6673 }
6674
Gilles Peskine1468da72019-05-29 17:35:49 +02006675 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006676 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006677 break;
6678 default:
6679 PSA_ASSERT( psa_key_derivation_input_bytes(
6680 &operation, steps[i],
6681 inputs[i]->x, inputs[i]->len ) );
6682 break;
6683 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006684 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006685
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006686 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006687 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006688 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006689 expected_capacity = requested_capacity;
6690
6691 /* Expansion phase. */
6692 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6693 {
6694 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006695 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006696 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006697 if( expected_capacity == 0 && output_sizes[i] == 0 )
6698 {
6699 /* Reading 0 bytes when 0 bytes are available can go either way. */
6700 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006701 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006702 continue;
6703 }
6704 else if( expected_capacity == 0 ||
6705 output_sizes[i] > expected_capacity )
6706 {
6707 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006708 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006709 expected_capacity = 0;
6710 continue;
6711 }
6712 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006713 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006714 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006715 ASSERT_COMPARE( output_buffer, output_sizes[i],
6716 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006717 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006718 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006719 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006720 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006721 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006722 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006723 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006724
6725exit:
6726 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006727 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006728 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6729 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006730 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006731}
6732/* END_CASE */
6733
6734/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006735void derive_full( int alg_arg,
6736 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006737 data_t *input1,
6738 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006739 int requested_capacity_arg )
6740{
Ronald Cron5425a212020-08-04 14:58:35 +02006741 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006742 psa_algorithm_t alg = alg_arg;
6743 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006744 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006745 unsigned char output_buffer[16];
6746 size_t expected_capacity = requested_capacity;
6747 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006749
Gilles Peskine8817f612018-12-18 00:18:46 +01006750 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006751
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006752 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6753 psa_set_key_algorithm( &attributes, alg );
6754 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006755
Gilles Peskine049c7532019-05-15 20:22:09 +02006756 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006757 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006758
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006759 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6760 input1->x, input1->len,
6761 input2->x, input2->len,
6762 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006763 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006764
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006765 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006766 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006767 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006768
6769 /* Expansion phase. */
6770 while( current_capacity > 0 )
6771 {
6772 size_t read_size = sizeof( output_buffer );
6773 if( read_size > current_capacity )
6774 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006775 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006776 output_buffer,
6777 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006778 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006779 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006780 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006781 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006782 }
6783
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006784 /* Check that the operation refuses to go over capacity. */
6785 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006786 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006787
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006788 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006789
6790exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006791 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006792 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006793 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006794}
6795/* END_CASE */
6796
Janos Follathe60c9052019-07-03 13:51:30 +01006797/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006798void derive_key_exercise( int alg_arg,
6799 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006800 data_t *input1,
6801 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006802 int derived_type_arg,
6803 int derived_bits_arg,
6804 int derived_usage_arg,
6805 int derived_alg_arg )
6806{
Ronald Cron5425a212020-08-04 14:58:35 +02006807 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6808 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006809 psa_algorithm_t alg = alg_arg;
6810 psa_key_type_t derived_type = derived_type_arg;
6811 size_t derived_bits = derived_bits_arg;
6812 psa_key_usage_t derived_usage = derived_usage_arg;
6813 psa_algorithm_t derived_alg = derived_alg_arg;
6814 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006815 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006817 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006818
Gilles Peskine8817f612018-12-18 00:18:46 +01006819 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006820
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006821 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6822 psa_set_key_algorithm( &attributes, alg );
6823 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006824 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006825 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006826
6827 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006828 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6829 input1->x, input1->len,
6830 input2->x, input2->len,
6831 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006832 goto exit;
6833
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006834 psa_set_key_usage_flags( &attributes, derived_usage );
6835 psa_set_key_algorithm( &attributes, derived_alg );
6836 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006837 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006838 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006839 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006840
6841 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006842 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006843 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6844 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006845
6846 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006847 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006848 goto exit;
6849
6850exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006851 /*
6852 * Key attributes may have been returned by psa_get_key_attributes()
6853 * thus reset them as required.
6854 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006855 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006856
6857 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006858 psa_destroy_key( base_key );
6859 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006860 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006861}
6862/* END_CASE */
6863
Janos Follath42fd8882019-07-03 14:17:09 +01006864/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006865void derive_key_export( int alg_arg,
6866 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006867 data_t *input1,
6868 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006869 int bytes1_arg,
6870 int bytes2_arg )
6871{
Ronald Cron5425a212020-08-04 14:58:35 +02006872 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6873 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006874 psa_algorithm_t alg = alg_arg;
6875 size_t bytes1 = bytes1_arg;
6876 size_t bytes2 = bytes2_arg;
6877 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006878 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006879 uint8_t *output_buffer = NULL;
6880 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006881 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6882 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006883 size_t length;
6884
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006885 ASSERT_ALLOC( output_buffer, capacity );
6886 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006887 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006888
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006889 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6890 psa_set_key_algorithm( &base_attributes, alg );
6891 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006892 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006893 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006894
6895 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006896 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6897 input1->x, input1->len,
6898 input2->x, input2->len,
6899 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006900 goto exit;
6901
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006902 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006903 output_buffer,
6904 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006905 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006906
6907 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006908 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6909 input1->x, input1->len,
6910 input2->x, input2->len,
6911 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006912 goto exit;
6913
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006914 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6915 psa_set_key_algorithm( &derived_attributes, 0 );
6916 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006917 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006918 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006919 &derived_key ) );
6920 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006921 export_buffer, bytes1,
6922 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006923 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006924 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006925 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006926 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006927 &derived_key ) );
6928 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006929 export_buffer + bytes1, bytes2,
6930 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006931 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006932
6933 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006934 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6935 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006936
6937exit:
6938 mbedtls_free( output_buffer );
6939 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006940 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006941 psa_destroy_key( base_key );
6942 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006943 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006944}
6945/* END_CASE */
6946
6947/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006948void derive_key( int alg_arg,
6949 data_t *key_data, data_t *input1, data_t *input2,
6950 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006951 int expected_status_arg,
6952 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006953{
Ronald Cron5425a212020-08-04 14:58:35 +02006954 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6955 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006956 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006957 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006958 size_t bits = bits_arg;
6959 psa_status_t expected_status = expected_status_arg;
6960 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6961 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6962 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6963
6964 PSA_ASSERT( psa_crypto_init( ) );
6965
6966 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6967 psa_set_key_algorithm( &base_attributes, alg );
6968 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6969 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006970 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006971
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006972 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6973 input1->x, input1->len,
6974 input2->x, input2->len,
6975 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006976 goto exit;
6977
6978 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6979 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006980 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006981 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006982
6983 psa_status_t status =
6984 psa_key_derivation_output_key( &derived_attributes,
6985 &operation,
6986 &derived_key );
6987 if( is_large_output > 0 )
6988 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6989 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006990
6991exit:
6992 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006993 psa_destroy_key( base_key );
6994 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006995 PSA_DONE( );
6996}
6997/* END_CASE */
6998
6999/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02007000void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02007001 int our_key_type_arg, int our_key_alg_arg,
7002 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02007003 int expected_status_arg )
7004{
Ronald Cron5425a212020-08-04 14:58:35 +02007005 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007006 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007007 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007008 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007009 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007011 psa_status_t expected_status = expected_status_arg;
7012 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007013
Gilles Peskine8817f612018-12-18 00:18:46 +01007014 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007015
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007016 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007017 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007018 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007019 PSA_ASSERT( psa_import_key( &attributes,
7020 our_key_data->x, our_key_data->len,
7021 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007022
Gilles Peskine77f40d82019-04-11 21:27:06 +02007023 /* The tests currently include inputs that should fail at either step.
7024 * Test cases that fail at the setup step should be changed to call
7025 * key_derivation_setup instead, and this function should be renamed
7026 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007027 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02007028 if( status == PSA_SUCCESS )
7029 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007030 TEST_EQUAL( psa_key_derivation_key_agreement(
7031 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
7032 our_key,
7033 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02007034 expected_status );
7035 }
7036 else
7037 {
7038 TEST_ASSERT( status == expected_status );
7039 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007040
7041exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007042 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007043 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007044 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007045}
7046/* END_CASE */
7047
7048/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007049void raw_key_agreement( int alg_arg,
7050 int our_key_type_arg, data_t *our_key_data,
7051 data_t *peer_key_data,
7052 data_t *expected_output )
7053{
Ronald Cron5425a212020-08-04 14:58:35 +02007054 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007055 psa_algorithm_t alg = alg_arg;
7056 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007057 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007058 unsigned char *output = NULL;
7059 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007060 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007061
7062 ASSERT_ALLOC( output, expected_output->len );
7063 PSA_ASSERT( psa_crypto_init( ) );
7064
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007065 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7066 psa_set_key_algorithm( &attributes, alg );
7067 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007068 PSA_ASSERT( psa_import_key( &attributes,
7069 our_key_data->x, our_key_data->len,
7070 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007071
gabor-mezei-armceface22021-01-21 12:26:17 +01007072 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7073 key_bits = psa_get_key_bits( &attributes );
7074
Gilles Peskinebe697d82019-05-16 18:00:41 +02007075 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7076 peer_key_data->x, peer_key_data->len,
7077 output, expected_output->len,
7078 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007079 ASSERT_COMPARE( output, output_length,
7080 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01007081 TEST_ASSERT( output_length <=
7082 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7083 TEST_ASSERT( output_length <=
7084 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007085
7086exit:
7087 mbedtls_free( output );
7088 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007089 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007090}
7091/* END_CASE */
7092
7093/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007094void key_agreement_capacity( int alg_arg,
7095 int our_key_type_arg, data_t *our_key_data,
7096 data_t *peer_key_data,
7097 int expected_capacity_arg )
7098{
Ronald Cron5425a212020-08-04 14:58:35 +02007099 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007100 psa_algorithm_t alg = alg_arg;
7101 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007102 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007103 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007104 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007105 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007106
Gilles Peskine8817f612018-12-18 00:18:46 +01007107 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007108
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007109 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7110 psa_set_key_algorithm( &attributes, alg );
7111 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007112 PSA_ASSERT( psa_import_key( &attributes,
7113 our_key_data->x, our_key_data->len,
7114 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007115
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007116 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007117 PSA_ASSERT( psa_key_derivation_key_agreement(
7118 &operation,
7119 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7120 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007121 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7122 {
7123 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007124 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007125 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007126 NULL, 0 ) );
7127 }
Gilles Peskine59685592018-09-18 12:11:34 +02007128
Gilles Peskinebf491972018-10-25 22:36:12 +02007129 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007130 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007131 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007132 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007133
Gilles Peskinebf491972018-10-25 22:36:12 +02007134 /* Test the actual capacity by reading the output. */
7135 while( actual_capacity > sizeof( output ) )
7136 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007137 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007138 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007139 actual_capacity -= sizeof( output );
7140 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007141 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007142 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007143 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007144 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007145
Gilles Peskine59685592018-09-18 12:11:34 +02007146exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007147 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007148 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007149 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007150}
7151/* END_CASE */
7152
7153/* BEGIN_CASE */
7154void key_agreement_output( int alg_arg,
7155 int our_key_type_arg, data_t *our_key_data,
7156 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007157 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007158{
Ronald Cron5425a212020-08-04 14:58:35 +02007159 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007160 psa_algorithm_t alg = alg_arg;
7161 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007162 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007164 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007165
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007166 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7167 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007168
Gilles Peskine8817f612018-12-18 00:18:46 +01007169 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007170
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7172 psa_set_key_algorithm( &attributes, alg );
7173 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007174 PSA_ASSERT( psa_import_key( &attributes,
7175 our_key_data->x, our_key_data->len,
7176 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007177
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007178 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007179 PSA_ASSERT( psa_key_derivation_key_agreement(
7180 &operation,
7181 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7182 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007183 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7184 {
7185 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007186 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007187 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007188 NULL, 0 ) );
7189 }
Gilles Peskine59685592018-09-18 12:11:34 +02007190
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007191 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007192 actual_output,
7193 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007194 ASSERT_COMPARE( actual_output, expected_output1->len,
7195 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007196 if( expected_output2->len != 0 )
7197 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007198 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007199 actual_output,
7200 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007201 ASSERT_COMPARE( actual_output, expected_output2->len,
7202 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007203 }
Gilles Peskine59685592018-09-18 12:11:34 +02007204
7205exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007206 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007207 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007208 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007209 mbedtls_free( actual_output );
7210}
7211/* END_CASE */
7212
7213/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007214void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007215{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007216 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007217 unsigned char *output = NULL;
7218 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007219 size_t i;
7220 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007221
Simon Butcher49f8e312020-03-03 15:51:50 +00007222 TEST_ASSERT( bytes_arg >= 0 );
7223
Gilles Peskine91892022021-02-08 19:50:26 +01007224 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007225 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007226
Gilles Peskine8817f612018-12-18 00:18:46 +01007227 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007228
Gilles Peskinea50d7392018-06-21 10:22:13 +02007229 /* Run several times, to ensure that every output byte will be
7230 * nonzero at least once with overwhelming probability
7231 * (2^(-8*number_of_runs)). */
7232 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007233 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007234 if( bytes != 0 )
7235 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007236 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007237
Gilles Peskinea50d7392018-06-21 10:22:13 +02007238 for( i = 0; i < bytes; i++ )
7239 {
7240 if( output[i] != 0 )
7241 ++changed[i];
7242 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007243 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007244
7245 /* Check that every byte was changed to nonzero at least once. This
7246 * validates that psa_generate_random is overwriting every byte of
7247 * the output buffer. */
7248 for( i = 0; i < bytes; i++ )
7249 {
7250 TEST_ASSERT( changed[i] != 0 );
7251 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007252
7253exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007254 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007255 mbedtls_free( output );
7256 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007257}
7258/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007259
7260/* BEGIN_CASE */
7261void generate_key( int type_arg,
7262 int bits_arg,
7263 int usage_arg,
7264 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007265 int expected_status_arg,
7266 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007267{
Ronald Cron5425a212020-08-04 14:58:35 +02007268 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007269 psa_key_type_t type = type_arg;
7270 psa_key_usage_t usage = usage_arg;
7271 size_t bits = bits_arg;
7272 psa_algorithm_t alg = alg_arg;
7273 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007274 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007275 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007276
Gilles Peskine8817f612018-12-18 00:18:46 +01007277 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007278
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007279 psa_set_key_usage_flags( &attributes, usage );
7280 psa_set_key_algorithm( &attributes, alg );
7281 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007282 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007283
7284 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007285 psa_status_t status = psa_generate_key( &attributes, &key );
7286
7287 if( is_large_key > 0 )
7288 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7289 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007290 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007291 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007292
7293 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007294 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007295 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7296 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007297
Gilles Peskine818ca122018-06-20 18:16:48 +02007298 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007299 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007300 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007301
7302exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007303 /*
7304 * Key attributes may have been returned by psa_get_key_attributes()
7305 * thus reset them as required.
7306 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007307 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007308
Ronald Cron5425a212020-08-04 14:58:35 +02007309 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007310 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007311}
7312/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007313
Ronald Cronee414c72021-03-18 18:50:08 +01007314/* 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 +02007315void generate_key_rsa( int bits_arg,
7316 data_t *e_arg,
7317 int expected_status_arg )
7318{
Ronald Cron5425a212020-08-04 14:58:35 +02007319 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007320 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007321 size_t bits = bits_arg;
7322 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7323 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7324 psa_status_t expected_status = expected_status_arg;
7325 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7326 uint8_t *exported = NULL;
7327 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007328 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007329 size_t exported_length = SIZE_MAX;
7330 uint8_t *e_read_buffer = NULL;
7331 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007332 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007333 size_t e_read_length = SIZE_MAX;
7334
7335 if( e_arg->len == 0 ||
7336 ( e_arg->len == 3 &&
7337 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7338 {
7339 is_default_public_exponent = 1;
7340 e_read_size = 0;
7341 }
7342 ASSERT_ALLOC( e_read_buffer, e_read_size );
7343 ASSERT_ALLOC( exported, exported_size );
7344
7345 PSA_ASSERT( psa_crypto_init( ) );
7346
7347 psa_set_key_usage_flags( &attributes, usage );
7348 psa_set_key_algorithm( &attributes, alg );
7349 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7350 e_arg->x, e_arg->len ) );
7351 psa_set_key_bits( &attributes, bits );
7352
7353 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007354 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007355 if( expected_status != PSA_SUCCESS )
7356 goto exit;
7357
7358 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007359 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007360 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7361 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7362 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7363 e_read_buffer, e_read_size,
7364 &e_read_length ) );
7365 if( is_default_public_exponent )
7366 TEST_EQUAL( e_read_length, 0 );
7367 else
7368 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7369
7370 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007371 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007372 goto exit;
7373
7374 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007375 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007376 exported, exported_size,
7377 &exported_length ) );
7378 {
7379 uint8_t *p = exported;
7380 uint8_t *end = exported + exported_length;
7381 size_t len;
7382 /* RSAPublicKey ::= SEQUENCE {
7383 * modulus INTEGER, -- n
7384 * publicExponent INTEGER } -- e
7385 */
7386 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007387 MBEDTLS_ASN1_SEQUENCE |
7388 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007389 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007390 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7391 MBEDTLS_ASN1_INTEGER ) );
7392 if( len >= 1 && p[0] == 0 )
7393 {
7394 ++p;
7395 --len;
7396 }
7397 if( e_arg->len == 0 )
7398 {
7399 TEST_EQUAL( len, 3 );
7400 TEST_EQUAL( p[0], 1 );
7401 TEST_EQUAL( p[1], 0 );
7402 TEST_EQUAL( p[2], 1 );
7403 }
7404 else
7405 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7406 }
7407
7408exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007409 /*
7410 * Key attributes may have been returned by psa_get_key_attributes() or
7411 * set by psa_set_key_domain_parameters() thus reset them as required.
7412 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007413 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007414
Ronald Cron5425a212020-08-04 14:58:35 +02007415 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007416 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007417 mbedtls_free( e_read_buffer );
7418 mbedtls_free( exported );
7419}
7420/* END_CASE */
7421
Darryl Greend49a4992018-06-18 17:27:26 +01007422/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007423void persistent_key_load_key_from_storage( data_t *data,
7424 int type_arg, int bits_arg,
7425 int usage_flags_arg, int alg_arg,
7426 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007427{
Ronald Cron71016a92020-08-28 19:01:50 +02007428 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007429 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7431 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007432 psa_key_type_t type = type_arg;
7433 size_t bits = bits_arg;
7434 psa_key_usage_t usage_flags = usage_flags_arg;
7435 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007436 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007437 unsigned char *first_export = NULL;
7438 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007439 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007440 size_t first_exported_length;
7441 size_t second_exported_length;
7442
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007443 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7444 {
7445 ASSERT_ALLOC( first_export, export_size );
7446 ASSERT_ALLOC( second_export, export_size );
7447 }
Darryl Greend49a4992018-06-18 17:27:26 +01007448
Gilles Peskine8817f612018-12-18 00:18:46 +01007449 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007450
Gilles Peskinec87af662019-05-15 16:12:22 +02007451 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007452 psa_set_key_usage_flags( &attributes, usage_flags );
7453 psa_set_key_algorithm( &attributes, alg );
7454 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007455 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007456
Darryl Green0c6575a2018-11-07 16:05:30 +00007457 switch( generation_method )
7458 {
7459 case IMPORT_KEY:
7460 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007461 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007462 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007463 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007464
Darryl Green0c6575a2018-11-07 16:05:30 +00007465 case GENERATE_KEY:
7466 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007467 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007468 break;
7469
7470 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007471#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007472 {
7473 /* Create base key */
7474 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7475 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7476 psa_set_key_usage_flags( &base_attributes,
7477 PSA_KEY_USAGE_DERIVE );
7478 psa_set_key_algorithm( &base_attributes, derive_alg );
7479 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007480 PSA_ASSERT( psa_import_key( &base_attributes,
7481 data->x, data->len,
7482 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007483 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007484 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007485 PSA_ASSERT( psa_key_derivation_input_key(
7486 &operation,
7487 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007488 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007489 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007490 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007491 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7492 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007493 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007494 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007495 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007496 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007497 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007498#else
7499 TEST_ASSUME( ! "KDF not supported in this configuration" );
7500#endif
7501 break;
7502
7503 default:
7504 TEST_ASSERT( ! "generation_method not implemented in test" );
7505 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007506 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007507 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007508
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007509 /* Export the key if permitted by the key policy. */
7510 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7511 {
Ronald Cron5425a212020-08-04 14:58:35 +02007512 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007513 first_export, export_size,
7514 &first_exported_length ) );
7515 if( generation_method == IMPORT_KEY )
7516 ASSERT_COMPARE( data->x, data->len,
7517 first_export, first_exported_length );
7518 }
Darryl Greend49a4992018-06-18 17:27:26 +01007519
7520 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007521 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007522 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007523 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007524
Darryl Greend49a4992018-06-18 17:27:26 +01007525 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007526 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007527 TEST_ASSERT( mbedtls_svc_key_id_equal(
7528 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007529 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7530 PSA_KEY_LIFETIME_PERSISTENT );
7531 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7532 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007533 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007534 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007535 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007536
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007537 /* Export the key again if permitted by the key policy. */
7538 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007539 {
Ronald Cron5425a212020-08-04 14:58:35 +02007540 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007541 second_export, export_size,
7542 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007543 ASSERT_COMPARE( first_export, first_exported_length,
7544 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007545 }
7546
7547 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007548 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007549 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007550
7551exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007552 /*
7553 * Key attributes may have been returned by psa_get_key_attributes()
7554 * thus reset them as required.
7555 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007556 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007557
Darryl Greend49a4992018-06-18 17:27:26 +01007558 mbedtls_free( first_export );
7559 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007560 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007561 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007562 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007563 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007564}
7565/* END_CASE */