blob: accd4b3612a6c27962753a72cc4fe055f07e51b8 [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 {
Mircea Udrea657ff4f2022-01-31 13:51:56 +0100510 memcpy( ( output_data + output_length ), part_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 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
Neil Armstrong4766f992022-02-28 16:23:59 +0100592/*!
593 * \brief Internal Function for MAC multipart tests.
594 * \param key_type_arg Type of key passed in
595 * \param key_data The encryption / decryption key data
596 * \param alg_arg The type of algorithm used
597 * \param input_data Data to encrypt / decrypt
598 * \param data_part_len_arg If not -1, the length of chunks to feed
599 * the data in to be encrypted / decrypted. If
600 * -1, no chunking
601 * \param expected_output Expected output
602 * \param is_verify If non-zero this is an verify operation.
603 * \param do_zero_parts If non-zero, interleave zero length chunks
604 * with normal length chunks.
605 * \return int Zero on failure, non-zero on success.
606 */
607static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
608 int alg_arg,
609 data_t *input_data,
610 int data_part_len_arg,
611 data_t *expected_output,
612 int is_verify,
613 int do_zero_parts )
614{
615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
616 psa_key_type_t key_type = key_type_arg;
617 psa_algorithm_t alg = alg_arg;
618 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
619 unsigned char mac[PSA_MAC_MAX_SIZE];
620 size_t part_offset = 0;
621 size_t part_length = 0;
622 size_t data_part_len = 0;
623 size_t mac_len = 0;
624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
625 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
626
627 int test_ok = 0;
628 size_t part_count = 0;
629
Neil Armstrongfd4c2592022-03-07 10:11:11 +0100630 PSA_INIT( );
Neil Armstrong4766f992022-02-28 16:23:59 +0100631
632 if( is_verify )
633 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
634 else
635 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
636
637 psa_set_key_algorithm( &attributes, alg );
638 psa_set_key_type( &attributes, key_type );
639
640 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
641 &key ) );
642
643 if( is_verify )
644 status = psa_mac_verify_setup( &operation, key, alg );
645 else
646 status = psa_mac_sign_setup( &operation, key, alg );
647
648 PSA_ASSERT( status );
649
650 if( data_part_len_arg != -1 )
651 {
652 /* Pass data in parts */
653 data_part_len = ( size_t ) data_part_len_arg;
654
655 for( part_offset = 0, part_count = 0;
656 part_offset < input_data->len;
657 part_offset += part_length, part_count++ )
658 {
659 if( do_zero_parts && ( part_count & 0x01 ) )
660 {
661 part_length = 0;
662 }
663 else if( ( input_data->len - part_offset ) < data_part_len )
664 {
665 part_length = ( input_data->len - part_offset );
666 }
667 else
668 {
669 part_length = data_part_len;
670 }
671
672 PSA_ASSERT( psa_mac_update( &operation,
673 ( input_data->x + part_offset ),
674 part_length ) );
675 }
676 }
677 else
678 {
679 /* Pass all data in one go. */
680 PSA_ASSERT( psa_mac_update( &operation, input_data->x,
681 input_data->len ) );
682 }
683
684 if( is_verify )
685 {
686 PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
687 expected_output->len ) );
688 }
689 else
690 {
691 PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
692 PSA_MAC_MAX_SIZE, &mac_len ) );
693
694 ASSERT_COMPARE( expected_output->x, expected_output->len,
695 mac, mac_len );
696 }
697
698 test_ok = 1;
699
700exit:
701 psa_destroy_key( key );
702 psa_mac_abort( &operation );
703 PSA_DONE( );
704
705 return( test_ok );
706}
707
Gilles Peskinee59236f2018-01-27 23:32:46 +0100708/* END_HEADER */
709
710/* BEGIN_DEPENDENCIES
711 * depends_on:MBEDTLS_PSA_CRYPTO_C
712 * END_DEPENDENCIES
713 */
714
715/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200716void static_checks( )
717{
718 size_t max_truncated_mac_size =
719 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
720
721 /* Check that the length for a truncated MAC always fits in the algorithm
722 * encoding. The shifted mask is the maximum truncated value. The
723 * untruncated algorithm may be one byte larger. */
724 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
725}
726/* END_CASE */
727
728/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200729void import_with_policy( int type_arg,
730 int usage_arg, int alg_arg,
731 int expected_status_arg )
732{
733 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
734 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200735 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200736 psa_key_type_t type = type_arg;
737 psa_key_usage_t usage = usage_arg;
738 psa_algorithm_t alg = alg_arg;
739 psa_status_t expected_status = expected_status_arg;
740 const uint8_t key_material[16] = {0};
741 psa_status_t status;
742
743 PSA_ASSERT( psa_crypto_init( ) );
744
745 psa_set_key_type( &attributes, type );
746 psa_set_key_usage_flags( &attributes, usage );
747 psa_set_key_algorithm( &attributes, alg );
748
749 status = psa_import_key( &attributes,
750 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200751 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200752 TEST_EQUAL( status, expected_status );
753 if( status != PSA_SUCCESS )
754 goto exit;
755
Ronald Cron5425a212020-08-04 14:58:35 +0200756 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200757 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200758 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200759 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200760 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200761 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200762
Ronald Cron5425a212020-08-04 14:58:35 +0200763 PSA_ASSERT( psa_destroy_key( key ) );
764 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200765
766exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100767 /*
768 * Key attributes may have been returned by psa_get_key_attributes()
769 * thus reset them as required.
770 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200771 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100772
773 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200774 PSA_DONE( );
775}
776/* END_CASE */
777
778/* BEGIN_CASE */
779void import_with_data( data_t *data, int type_arg,
780 int attr_bits_arg,
781 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200782{
783 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
784 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200786 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200787 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200788 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790
Gilles Peskine8817f612018-12-18 00:18:46 +0100791 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100792
Gilles Peskine4747d192019-04-17 15:05:45 +0200793 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200794 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200795
Ronald Cron5425a212020-08-04 14:58:35 +0200796 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100797 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200798 if( status != PSA_SUCCESS )
799 goto exit;
800
Ronald Cron5425a212020-08-04 14:58:35 +0200801 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200802 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200803 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200804 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200805 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200806
Ronald Cron5425a212020-08-04 14:58:35 +0200807 PSA_ASSERT( psa_destroy_key( key ) );
808 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100809
810exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100811 /*
812 * Key attributes may have been returned by psa_get_key_attributes()
813 * thus reset them as required.
814 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200815 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100816
817 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200818 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819}
820/* END_CASE */
821
822/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200823void import_large_key( int type_arg, int byte_size_arg,
824 int expected_status_arg )
825{
826 psa_key_type_t type = type_arg;
827 size_t byte_size = byte_size_arg;
828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
829 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200831 psa_status_t status;
832 uint8_t *buffer = NULL;
833 size_t buffer_size = byte_size + 1;
834 size_t n;
835
Steven Cooreman69967ce2021-01-18 18:01:08 +0100836 /* Skip the test case if the target running the test cannot
837 * accomodate large keys due to heap size constraints */
838 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200839 memset( buffer, 'K', byte_size );
840
841 PSA_ASSERT( psa_crypto_init( ) );
842
843 /* Try importing the key */
844 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
845 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200846 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100847 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200848 TEST_EQUAL( status, expected_status );
849
850 if( status == PSA_SUCCESS )
851 {
Ronald Cron5425a212020-08-04 14:58:35 +0200852 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200853 TEST_EQUAL( psa_get_key_type( &attributes ), type );
854 TEST_EQUAL( psa_get_key_bits( &attributes ),
855 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200856 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200857 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200858 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200859 for( n = 0; n < byte_size; n++ )
860 TEST_EQUAL( buffer[n], 'K' );
861 for( n = byte_size; n < buffer_size; n++ )
862 TEST_EQUAL( buffer[n], 0 );
863 }
864
865exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100866 /*
867 * Key attributes may have been returned by psa_get_key_attributes()
868 * thus reset them as required.
869 */
870 psa_reset_key_attributes( &attributes );
871
Ronald Cron5425a212020-08-04 14:58:35 +0200872 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200873 PSA_DONE( );
874 mbedtls_free( buffer );
875}
876/* END_CASE */
877
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100878/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200879void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
880{
Ronald Cron5425a212020-08-04 14:58:35 +0200881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200882 size_t bits = bits_arg;
883 psa_status_t expected_status = expected_status_arg;
884 psa_status_t status;
885 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200886 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200887 size_t buffer_size = /* Slight overapproximations */
888 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200889 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200890 unsigned char *p;
891 int ret;
892 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200894
Gilles Peskine8817f612018-12-18 00:18:46 +0100895 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200896 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200897
898 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
899 bits, keypair ) ) >= 0 );
900 length = ret;
901
902 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200903 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200904 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100905 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200906
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200907 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200908 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200909
910exit:
911 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200912 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200913}
914/* END_CASE */
915
916/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300917void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300918 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200919 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530920 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100921 int expected_bits,
922 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200923 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100924 int canonical_input )
925{
Ronald Cron5425a212020-08-04 14:58:35 +0200926 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100927 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200928 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200929 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100930 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530931 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100932 unsigned char *exported = NULL;
933 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100934 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100935 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100936 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200938 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939
Moran Pekercb088e72018-07-17 17:36:59 +0300940 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200941 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100942 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200943 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100944 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945
Archana4d7ae1d2021-07-07 02:50:22 +0530946 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200947 psa_set_key_usage_flags( &attributes, usage_arg );
948 psa_set_key_algorithm( &attributes, alg );
949 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700950
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100951 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200952 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953
954 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200955 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200956 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
957 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200958 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100959
960 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200961 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100962 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100963
964 /* The exported length must be set by psa_export_key() to a value between 0
965 * and export_size. On errors, the exported length must be 0. */
966 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
967 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
968 TEST_ASSERT( exported_length <= export_size );
969
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200970 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200971 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100972 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200973 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100974 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200976 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977
Gilles Peskineea38a922021-02-13 00:05:16 +0100978 /* Run sanity checks on the exported key. For non-canonical inputs,
979 * this validates the canonical representations. For canonical inputs,
980 * this doesn't directly validate the implementation, but it still helps
981 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530982 if( !psa_key_lifetime_is_external( lifetime ) )
983 {
984 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
985 goto exit;
986 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200987
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200989 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990 else
991 {
Ronald Cron5425a212020-08-04 14:58:35 +0200992 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200993 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200994 &key2 ) );
995 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100996 reexported,
997 export_size,
998 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200999 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301000 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001001 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 }
gabor-mezei-armceface22021-01-21 12:26:17 +01001003 TEST_ASSERT( exported_length <=
Archana4d7ae1d2021-07-07 02:50:22 +05301004 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301005 psa_get_key_bits( &got_attributes ) ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01001006 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007
1008destroy:
1009 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001010 PSA_ASSERT( psa_destroy_key( key ) );
1011 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012
1013exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001014 /*
1015 * Key attributes may have been returned by psa_get_key_attributes()
1016 * thus reset them as required.
1017 */
1018 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301019 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001020 mbedtls_free( exported );
1021 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001022 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023}
1024/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001025
Moran Pekerf709f4a2018-06-06 17:26:04 +03001026/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001027void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001028 int type_arg,
1029 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301030 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001031 int export_size_delta,
1032 int expected_export_status_arg,
1033 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001034{
Ronald Cron5425a212020-08-04 14:58:35 +02001035 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001036 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001037 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001038 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001039 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301040 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001041 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001042 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001043 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001044 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001045
Gilles Peskine8817f612018-12-18 00:18:46 +01001046 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001047
Archana4d7ae1d2021-07-07 02:50:22 +05301048 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001049 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1050 psa_set_key_algorithm( &attributes, alg );
1051 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001052
1053 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001054 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001055
Gilles Peskine49c25912018-10-29 15:15:31 +01001056 /* Export the public key */
1057 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001058 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001059 exported, export_size,
1060 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001061 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001062 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001063 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001064 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001065 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001066 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001067 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001068 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001069 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01001070 TEST_ASSERT( expected_public_key->len <=
1071 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1072 TEST_ASSERT( expected_public_key->len <=
1073 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001074 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1075 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001076 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001077exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001078 /*
1079 * Key attributes may have been returned by psa_get_key_attributes()
1080 * thus reset them as required.
1081 */
1082 psa_reset_key_attributes( &attributes );
1083
itayzafrir3e02b3b2018-06-12 17:06:52 +03001084 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001085 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001086 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001087}
1088/* END_CASE */
1089
Gilles Peskine20035e32018-02-03 22:44:14 +01001090/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001091void import_and_exercise_key( data_t *data,
1092 int type_arg,
1093 int bits_arg,
1094 int alg_arg )
1095{
Ronald Cron5425a212020-08-04 14:58:35 +02001096 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001097 psa_key_type_t type = type_arg;
1098 size_t bits = bits_arg;
1099 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001100 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001102 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001103
Gilles Peskine8817f612018-12-18 00:18:46 +01001104 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001105
Gilles Peskine4747d192019-04-17 15:05:45 +02001106 psa_set_key_usage_flags( &attributes, usage );
1107 psa_set_key_algorithm( &attributes, alg );
1108 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001109
1110 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001111 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001112
1113 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001114 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001115 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1116 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001117
1118 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001119 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001120 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001121
Ronald Cron5425a212020-08-04 14:58:35 +02001122 PSA_ASSERT( psa_destroy_key( key ) );
1123 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001124
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001125exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001126 /*
1127 * Key attributes may have been returned by psa_get_key_attributes()
1128 * thus reset them as required.
1129 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001130 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001131
1132 psa_reset_key_attributes( &attributes );
1133 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001134 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001135}
1136/* END_CASE */
1137
1138/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001139void effective_key_attributes( int type_arg, int expected_type_arg,
1140 int bits_arg, int expected_bits_arg,
1141 int usage_arg, int expected_usage_arg,
1142 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001143{
Ronald Cron5425a212020-08-04 14:58:35 +02001144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001145 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001146 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001147 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001148 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001149 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001150 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001151 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001152 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001154
Gilles Peskine8817f612018-12-18 00:18:46 +01001155 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001156
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001157 psa_set_key_usage_flags( &attributes, usage );
1158 psa_set_key_algorithm( &attributes, alg );
1159 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001160 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001161
Ronald Cron5425a212020-08-04 14:58:35 +02001162 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001163 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001164
Ronald Cron5425a212020-08-04 14:58:35 +02001165 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001166 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1167 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1168 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1169 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001170
1171exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001172 /*
1173 * Key attributes may have been returned by psa_get_key_attributes()
1174 * thus reset them as required.
1175 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001176 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001177
1178 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001179 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001180}
1181/* END_CASE */
1182
1183/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001184void check_key_policy( int type_arg, int bits_arg,
1185 int usage_arg, int alg_arg )
1186{
1187 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001188 usage_arg,
1189 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001190 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001191 goto exit;
1192}
1193/* END_CASE */
1194
1195/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001196void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001197{
1198 /* Test each valid way of initializing the object, except for `= {0}`, as
1199 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1200 * though it's OK by the C standard. We could test for this, but we'd need
1201 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001202 psa_key_attributes_t func = psa_key_attributes_init( );
1203 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1204 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001205
1206 memset( &zero, 0, sizeof( zero ) );
1207
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001208 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1209 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1210 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001211
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001212 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1213 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1214 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1215
1216 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1217 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1218 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1219
1220 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1221 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1222 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1223
1224 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1225 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1226 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001227}
1228/* END_CASE */
1229
1230/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001231void mac_key_policy( int policy_usage_arg,
1232 int policy_alg_arg,
1233 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001234 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001235 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001236 int expected_status_sign_arg,
1237 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001238{
Ronald Cron5425a212020-08-04 14:58:35 +02001239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001241 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001242 psa_key_type_t key_type = key_type_arg;
1243 psa_algorithm_t policy_alg = policy_alg_arg;
1244 psa_algorithm_t exercise_alg = exercise_alg_arg;
1245 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001246 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001247 psa_status_t expected_status_sign = expected_status_sign_arg;
1248 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001249 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001250
Gilles Peskine8817f612018-12-18 00:18:46 +01001251 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001252
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001253 psa_set_key_usage_flags( &attributes, policy_usage );
1254 psa_set_key_algorithm( &attributes, policy_alg );
1255 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001256
Gilles Peskine049c7532019-05-15 20:22:09 +02001257 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001258 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001259
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001260 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1261 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001262
Ronald Cron5425a212020-08-04 14:58:35 +02001263 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001264 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001265
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001266 /* Calculate the MAC, one-shot case. */
1267 uint8_t input[128] = {0};
1268 size_t mac_len;
1269 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1270 input, 128,
1271 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1272 expected_status_sign );
1273
Neil Armstrong3af9b972022-02-07 12:20:21 +01001274 /* Calculate the MAC, multi-part case. */
1275 PSA_ASSERT( psa_mac_abort( &operation ) );
1276 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1277 if( status == PSA_SUCCESS )
1278 {
1279 status = psa_mac_update( &operation, input, 128 );
1280 if( status == PSA_SUCCESS )
1281 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1282 &mac_len ),
1283 expected_status_sign );
1284 else
1285 TEST_EQUAL( status, expected_status_sign );
1286 }
1287 else
1288 {
1289 TEST_EQUAL( status, expected_status_sign );
1290 }
1291 PSA_ASSERT( psa_mac_abort( &operation ) );
1292
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001293 /* Verify correct MAC, one-shot case. */
1294 status = psa_mac_verify( key, exercise_alg, input, 128,
1295 mac, mac_len );
1296
1297 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1298 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001299 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001300 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001301
Neil Armstrong3af9b972022-02-07 12:20:21 +01001302 /* Verify correct MAC, multi-part case. */
1303 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1304 if( status == PSA_SUCCESS )
1305 {
1306 status = psa_mac_update( &operation, input, 128 );
1307 if( status == PSA_SUCCESS )
1308 {
1309 status = psa_mac_verify_finish( &operation, mac, mac_len );
1310 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1311 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1312 else
1313 TEST_EQUAL( status, expected_status_verify );
1314 }
1315 else
1316 {
1317 TEST_EQUAL( status, expected_status_verify );
1318 }
1319 }
1320 else
1321 {
1322 TEST_EQUAL( status, expected_status_verify );
1323 }
1324
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001325 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001326
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001327 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001328 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001329 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001330
1331exit:
1332 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001333 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001334 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001335}
1336/* END_CASE */
1337
1338/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001339void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001340 int policy_alg,
1341 int key_type,
1342 data_t *key_data,
1343 int exercise_alg )
1344{
Ronald Cron5425a212020-08-04 14:58:35 +02001345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001347 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001348 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001349 size_t output_buffer_size = 0;
1350 size_t input_buffer_size = 0;
1351 size_t output_length = 0;
1352 uint8_t *output = NULL;
1353 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354 psa_status_t status;
1355
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001356 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1357 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1358 input_buffer_size );
1359
1360 ASSERT_ALLOC( input, input_buffer_size );
1361 ASSERT_ALLOC( output, output_buffer_size );
1362
Gilles Peskine8817f612018-12-18 00:18:46 +01001363 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001365 psa_set_key_usage_flags( &attributes, policy_usage );
1366 psa_set_key_algorithm( &attributes, policy_alg );
1367 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001368
Gilles Peskine049c7532019-05-15 20:22:09 +02001369 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001370 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001371
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001372 /* Check if no key usage flag implication is done */
1373 TEST_EQUAL( policy_usage,
1374 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001375
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001376 /* Encrypt check, one-shot */
1377 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1378 output, output_buffer_size,
1379 &output_length);
1380 if( policy_alg == exercise_alg &&
1381 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1382 PSA_ASSERT( status );
1383 else
1384 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1385
1386 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001387 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001388 if( policy_alg == exercise_alg &&
1389 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001391 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001392 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001393 psa_cipher_abort( &operation );
1394
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001395 /* Decrypt check, one-shot */
1396 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1397 input, input_buffer_size,
1398 &output_length);
1399 if( policy_alg == exercise_alg &&
1400 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1401 PSA_ASSERT( status );
1402 else
1403 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1404
1405 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001406 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001407 if( policy_alg == exercise_alg &&
1408 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001409 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001410 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001411 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001412
1413exit:
1414 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001415 mbedtls_free( input );
1416 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001417 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001418 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001419}
1420/* END_CASE */
1421
1422/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001423void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001424 int policy_alg,
1425 int key_type,
1426 data_t *key_data,
1427 int nonce_length_arg,
1428 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001429 int exercise_alg,
1430 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001431{
Ronald Cron5425a212020-08-04 14:58:35 +02001432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001434 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001435 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001436 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001437 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001438 unsigned char nonce[16] = {0};
1439 size_t nonce_length = nonce_length_arg;
1440 unsigned char tag[16];
1441 size_t tag_length = tag_length_arg;
1442 size_t output_length;
1443
1444 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1445 TEST_ASSERT( tag_length <= sizeof( tag ) );
1446
Gilles Peskine8817f612018-12-18 00:18:46 +01001447 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001448
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001449 psa_set_key_usage_flags( &attributes, policy_usage );
1450 psa_set_key_algorithm( &attributes, policy_alg );
1451 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001452
Gilles Peskine049c7532019-05-15 20:22:09 +02001453 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001454 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001455
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001456 /* Check if no key usage implication is done */
1457 TEST_EQUAL( policy_usage,
1458 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001459
Neil Armstrong752d8112022-02-07 14:51:11 +01001460 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02001461 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001462 nonce, nonce_length,
1463 NULL, 0,
1464 NULL, 0,
1465 tag, tag_length,
1466 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001467 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1468 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001469 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001470 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001471
Neil Armstrong752d8112022-02-07 14:51:11 +01001472 /* Encrypt check, multi-part */
1473 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
1474 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1475 TEST_EQUAL( status, expected_status );
1476 else
1477 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1478
1479 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001480 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001481 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001482 nonce, nonce_length,
1483 NULL, 0,
1484 tag, tag_length,
1485 NULL, 0,
1486 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001487 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1488 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1489 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001490 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001491 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001492 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001493
Neil Armstrong752d8112022-02-07 14:51:11 +01001494 /* Decrypt check, multi-part */
1495 PSA_ASSERT( psa_aead_abort( &operation ) );
1496 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
1497 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1498 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1499 else
1500 TEST_EQUAL( status, expected_status );
1501
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001502exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01001503 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001504 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001505 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001506}
1507/* END_CASE */
1508
1509/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001510void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001511 int policy_alg,
1512 int key_type,
1513 data_t *key_data,
1514 int exercise_alg )
1515{
Ronald Cron5425a212020-08-04 14:58:35 +02001516 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001517 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001518 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001519 psa_status_t status;
1520 size_t key_bits;
1521 size_t buffer_length;
1522 unsigned char *buffer = NULL;
1523 size_t output_length;
1524
Gilles Peskine8817f612018-12-18 00:18:46 +01001525 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001526
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001527 psa_set_key_usage_flags( &attributes, policy_usage );
1528 psa_set_key_algorithm( &attributes, policy_alg );
1529 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001530
Gilles Peskine049c7532019-05-15 20:22:09 +02001531 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001532 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001533
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001534 /* Check if no key usage implication is done */
1535 TEST_EQUAL( policy_usage,
1536 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001537
Ronald Cron5425a212020-08-04 14:58:35 +02001538 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001539 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001540 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1541 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001542 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001543
Ronald Cron5425a212020-08-04 14:58:35 +02001544 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001545 NULL, 0,
1546 NULL, 0,
1547 buffer, buffer_length,
1548 &output_length );
1549 if( policy_alg == exercise_alg &&
1550 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001551 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001552 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001553 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001554
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001555 if( buffer_length != 0 )
1556 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001557 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001558 buffer, buffer_length,
1559 NULL, 0,
1560 buffer, buffer_length,
1561 &output_length );
1562 if( policy_alg == exercise_alg &&
1563 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001564 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001565 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001566 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001567
1568exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001569 /*
1570 * Key attributes may have been returned by psa_get_key_attributes()
1571 * thus reset them as required.
1572 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001573 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001574
1575 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001576 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 mbedtls_free( buffer );
1578}
1579/* END_CASE */
1580
1581/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001582void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001583 int policy_alg,
1584 int key_type,
1585 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001586 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001587 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001588 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001589{
Ronald Cron5425a212020-08-04 14:58:35 +02001590 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001592 psa_key_usage_t policy_usage = policy_usage_arg;
1593 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001594 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001595 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1596 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1597 * compatible with the policy and `payload_length_arg` is supposed to be
1598 * a valid input length to sign. If `payload_length_arg <= 0`,
1599 * `exercise_alg` is supposed to be forbidden by the policy. */
1600 int compatible_alg = payload_length_arg > 0;
1601 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001602 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 size_t signature_length;
1604
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001605 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001606 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001607 TEST_EQUAL( expected_usage,
1608 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001609
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001611
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001612 psa_set_key_usage_flags( &attributes, policy_usage );
1613 psa_set_key_algorithm( &attributes, policy_alg );
1614 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001615
Gilles Peskine049c7532019-05-15 20:22:09 +02001616 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001617 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001618
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001619 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1620
Ronald Cron5425a212020-08-04 14:58:35 +02001621 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001622 payload, payload_length,
1623 signature, sizeof( signature ),
1624 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001625 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001626 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001627 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001628 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001629
1630 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001631 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001632 payload, payload_length,
1633 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001634 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001635 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001636 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001637 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001638
Gilles Peskinef7b41372021-09-22 16:15:05 +02001639 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001640 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001641 {
1642 status = psa_sign_message( key, exercise_alg,
1643 payload, payload_length,
1644 signature, sizeof( signature ),
1645 &signature_length );
1646 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1647 PSA_ASSERT( status );
1648 else
1649 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1650
1651 memset( signature, 0, sizeof( signature ) );
1652 status = psa_verify_message( key, exercise_alg,
1653 payload, payload_length,
1654 signature, sizeof( signature ) );
1655 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1656 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1657 else
1658 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1659 }
1660
Gilles Peskined5b33222018-06-18 22:20:03 +02001661exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001662 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001663 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001664}
1665/* END_CASE */
1666
Janos Follathba3fab92019-06-11 14:50:16 +01001667/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001668void derive_key_policy( int policy_usage,
1669 int policy_alg,
1670 int key_type,
1671 data_t *key_data,
1672 int exercise_alg )
1673{
Ronald Cron5425a212020-08-04 14:58:35 +02001674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001676 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001677 psa_status_t status;
1678
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001680
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001681 psa_set_key_usage_flags( &attributes, policy_usage );
1682 psa_set_key_algorithm( &attributes, policy_alg );
1683 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001684
Gilles Peskine049c7532019-05-15 20:22:09 +02001685 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001686 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001687
Janos Follathba3fab92019-06-11 14:50:16 +01001688 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1689
1690 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1691 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001692 {
Janos Follathba3fab92019-06-11 14:50:16 +01001693 PSA_ASSERT( psa_key_derivation_input_bytes(
1694 &operation,
1695 PSA_KEY_DERIVATION_INPUT_SEED,
1696 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001697 }
Janos Follathba3fab92019-06-11 14:50:16 +01001698
1699 status = psa_key_derivation_input_key( &operation,
1700 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001701 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001702
Gilles Peskineea0fb492018-07-12 17:17:20 +02001703 if( policy_alg == exercise_alg &&
1704 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001706 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001707 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001708
1709exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001710 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001711 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001712 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001713}
1714/* END_CASE */
1715
1716/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001717void agreement_key_policy( int policy_usage,
1718 int policy_alg,
1719 int key_type_arg,
1720 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001721 int exercise_alg,
1722 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001723{
Ronald Cron5425a212020-08-04 14:58:35 +02001724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001726 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001727 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001728 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001729 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001730
Gilles Peskine8817f612018-12-18 00:18:46 +01001731 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001732
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001733 psa_set_key_usage_flags( &attributes, policy_usage );
1734 psa_set_key_algorithm( &attributes, policy_alg );
1735 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001736
Gilles Peskine049c7532019-05-15 20:22:09 +02001737 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001738 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001739
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001740 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001741 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001742
Steven Cooremance48e852020-10-05 16:02:45 +02001743 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001744
1745exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001746 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001747 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001748 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001749}
1750/* END_CASE */
1751
1752/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001753void key_policy_alg2( int key_type_arg, data_t *key_data,
1754 int usage_arg, int alg_arg, int alg2_arg )
1755{
Ronald Cron5425a212020-08-04 14:58:35 +02001756 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001757 psa_key_type_t key_type = key_type_arg;
1758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1759 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1760 psa_key_usage_t usage = usage_arg;
1761 psa_algorithm_t alg = alg_arg;
1762 psa_algorithm_t alg2 = alg2_arg;
1763
1764 PSA_ASSERT( psa_crypto_init( ) );
1765
1766 psa_set_key_usage_flags( &attributes, usage );
1767 psa_set_key_algorithm( &attributes, alg );
1768 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1769 psa_set_key_type( &attributes, key_type );
1770 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001771 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001772
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001773 /* Update the usage flags to obtain implicit usage flags */
1774 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001775 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001776 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1777 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1778 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1779
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001780 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001781 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001782 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001783 goto exit;
1784
1785exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001786 /*
1787 * Key attributes may have been returned by psa_get_key_attributes()
1788 * thus reset them as required.
1789 */
1790 psa_reset_key_attributes( &got_attributes );
1791
Ronald Cron5425a212020-08-04 14:58:35 +02001792 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001793 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001794}
1795/* END_CASE */
1796
1797/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001798void raw_agreement_key_policy( int policy_usage,
1799 int policy_alg,
1800 int key_type_arg,
1801 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001802 int exercise_alg,
1803 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001804{
Ronald Cron5425a212020-08-04 14:58:35 +02001805 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001807 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001808 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001809 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001810 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001811
1812 PSA_ASSERT( psa_crypto_init( ) );
1813
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001814 psa_set_key_usage_flags( &attributes, policy_usage );
1815 psa_set_key_algorithm( &attributes, policy_alg );
1816 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001817
Gilles Peskine049c7532019-05-15 20:22:09 +02001818 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001819 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001820
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001821 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001822
Steven Cooremance48e852020-10-05 16:02:45 +02001823 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001824
1825exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001826 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001827 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001828 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001829}
1830/* END_CASE */
1831
1832/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001833void copy_success( int source_usage_arg,
1834 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301835 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001836 int type_arg, data_t *material,
1837 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001838 int target_usage_arg,
1839 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301840 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001841 int expected_usage_arg,
1842 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001843{
Gilles Peskineca25db92019-04-19 11:43:08 +02001844 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1845 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001846 psa_key_usage_t expected_usage = expected_usage_arg;
1847 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001848 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301849 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1850 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001851 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1852 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001853 uint8_t *export_buffer = NULL;
1854
Gilles Peskine57ab7212019-01-28 13:03:09 +01001855 PSA_ASSERT( psa_crypto_init( ) );
1856
Gilles Peskineca25db92019-04-19 11:43:08 +02001857 /* Prepare the source key. */
1858 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1859 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001860 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001861 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301862 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001863 PSA_ASSERT( psa_import_key( &source_attributes,
1864 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001865 &source_key ) );
1866 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001867
Gilles Peskineca25db92019-04-19 11:43:08 +02001868 /* Prepare the target attributes. */
1869 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001870 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001871 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001872 }
Archana8a180362021-07-05 02:18:48 +05301873 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001874
Gilles Peskineca25db92019-04-19 11:43:08 +02001875 if( target_usage_arg != -1 )
1876 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1877 if( target_alg_arg != -1 )
1878 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001879 if( target_alg2_arg != -1 )
1880 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001881
Archana8a180362021-07-05 02:18:48 +05301882
Gilles Peskine57ab7212019-01-28 13:03:09 +01001883 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001884 PSA_ASSERT( psa_copy_key( source_key,
1885 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001886
1887 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001888 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001889
1890 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001891 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001892 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1893 psa_get_key_type( &target_attributes ) );
1894 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1895 psa_get_key_bits( &target_attributes ) );
1896 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1897 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001898 TEST_EQUAL( expected_alg2,
1899 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001900 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1901 {
1902 size_t length;
1903 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001904 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001905 material->len, &length ) );
1906 ASSERT_COMPARE( material->x, material->len,
1907 export_buffer, length );
1908 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001909
Archana8a180362021-07-05 02:18:48 +05301910 if( !psa_key_lifetime_is_external( target_lifetime ) )
1911 {
1912 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1913 goto exit;
1914 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1915 goto exit;
1916 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001917
Ronald Cron5425a212020-08-04 14:58:35 +02001918 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001919
1920exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001921 /*
1922 * Source and target key attributes may have been returned by
1923 * psa_get_key_attributes() thus reset them as required.
1924 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001925 psa_reset_key_attributes( &source_attributes );
1926 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001927
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001928 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001929 mbedtls_free( export_buffer );
1930}
1931/* END_CASE */
1932
1933/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001934void copy_fail( int source_usage_arg,
1935 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301936 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001937 int type_arg, data_t *material,
1938 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001939 int target_usage_arg,
1940 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001941 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001942 int expected_status_arg )
1943{
1944 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1945 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001946 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1947 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001948 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001949
1950 PSA_ASSERT( psa_crypto_init( ) );
1951
1952 /* Prepare the source key. */
1953 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1954 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001955 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001956 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301957 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001958 PSA_ASSERT( psa_import_key( &source_attributes,
1959 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001960 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001961
1962 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001963 psa_set_key_id( &target_attributes, key_id );
1964 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001965 psa_set_key_type( &target_attributes, target_type_arg );
1966 psa_set_key_bits( &target_attributes, target_bits_arg );
1967 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1968 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001969 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001970
1971 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001972 TEST_EQUAL( psa_copy_key( source_key,
1973 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001974 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001975
Ronald Cron5425a212020-08-04 14:58:35 +02001976 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001977
Gilles Peskine4a644642019-05-03 17:14:08 +02001978exit:
1979 psa_reset_key_attributes( &source_attributes );
1980 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001981 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001982}
1983/* END_CASE */
1984
1985/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001986void hash_operation_init( )
1987{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001988 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001989 /* Test each valid way of initializing the object, except for `= {0}`, as
1990 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1991 * though it's OK by the C standard. We could test for this, but we'd need
1992 * to supress the Clang warning for the test. */
1993 psa_hash_operation_t func = psa_hash_operation_init( );
1994 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1995 psa_hash_operation_t zero;
1996
1997 memset( &zero, 0, sizeof( zero ) );
1998
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001999 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002000 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2001 PSA_ERROR_BAD_STATE );
2002 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2003 PSA_ERROR_BAD_STATE );
2004 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2005 PSA_ERROR_BAD_STATE );
2006
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002007 /* A default hash operation should be abortable without error. */
2008 PSA_ASSERT( psa_hash_abort( &func ) );
2009 PSA_ASSERT( psa_hash_abort( &init ) );
2010 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002011}
2012/* END_CASE */
2013
2014/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002015void hash_setup( int alg_arg,
2016 int expected_status_arg )
2017{
2018 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002019 uint8_t *output = NULL;
2020 size_t output_size = 0;
2021 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002022 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002023 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002024 psa_status_t status;
2025
Gilles Peskine8817f612018-12-18 00:18:46 +01002026 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002027
Neil Armstrongedb20862022-02-07 15:47:44 +01002028 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002029 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002030 ASSERT_ALLOC( output, output_size );
2031
2032 status = psa_hash_compute( alg, NULL, 0,
2033 output, output_size, &output_length );
2034 TEST_EQUAL( status, expected_status );
2035
2036 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002037 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002038 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002039
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002040 /* Whether setup succeeded or failed, abort must succeed. */
2041 PSA_ASSERT( psa_hash_abort( &operation ) );
2042
2043 /* If setup failed, reproduce the failure, so as to
2044 * test the resulting state of the operation object. */
2045 if( status != PSA_SUCCESS )
2046 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2047
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002048 /* Now the operation object should be reusable. */
2049#if defined(KNOWN_SUPPORTED_HASH_ALG)
2050 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2051 PSA_ASSERT( psa_hash_abort( &operation ) );
2052#endif
2053
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002054exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002055 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002056 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002061void hash_compute_fail( int alg_arg, data_t *input,
2062 int output_size_arg, int expected_status_arg )
2063{
2064 psa_algorithm_t alg = alg_arg;
2065 uint8_t *output = NULL;
2066 size_t output_size = output_size_arg;
2067 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002068 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002069 psa_status_t expected_status = expected_status_arg;
2070 psa_status_t status;
2071
2072 ASSERT_ALLOC( output, output_size );
2073
2074 PSA_ASSERT( psa_crypto_init( ) );
2075
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002076 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002077 status = psa_hash_compute( alg, input->x, input->len,
2078 output, output_size, &output_length );
2079 TEST_EQUAL( status, expected_status );
2080 TEST_ASSERT( output_length <= output_size );
2081
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002082 /* Hash Compute, multi-part */
2083 status = psa_hash_setup( &operation, alg );
2084 if( status == PSA_SUCCESS )
2085 {
2086 status = psa_hash_update( &operation, input->x, input->len );
2087 if( status == PSA_SUCCESS )
2088 {
2089 status = psa_hash_finish( &operation, output, output_size,
2090 &output_length );
2091 if( status == PSA_SUCCESS )
2092 TEST_ASSERT( output_length <= output_size );
2093 else
2094 TEST_EQUAL( status, expected_status );
2095 }
2096 else
2097 {
2098 TEST_EQUAL( status, expected_status );
2099 }
2100 }
2101 else
2102 {
2103 TEST_EQUAL( status, expected_status );
2104 }
2105
Gilles Peskine0a749c82019-11-28 19:33:58 +01002106exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002107 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002108 mbedtls_free( output );
2109 PSA_DONE( );
2110}
2111/* END_CASE */
2112
2113/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002114void hash_compare_fail( int alg_arg, data_t *input,
2115 data_t *reference_hash,
2116 int expected_status_arg )
2117{
2118 psa_algorithm_t alg = alg_arg;
2119 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002120 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002121 psa_status_t status;
2122
2123 PSA_ASSERT( psa_crypto_init( ) );
2124
Neil Armstrong55a1be12022-02-07 11:23:20 +01002125 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002126 status = psa_hash_compare( alg, input->x, input->len,
2127 reference_hash->x, reference_hash->len );
2128 TEST_EQUAL( status, expected_status );
2129
Neil Armstrong55a1be12022-02-07 11:23:20 +01002130 /* Hash Compare, multi-part */
2131 status = psa_hash_setup( &operation, alg );
2132 if( status == PSA_SUCCESS )
2133 {
2134 status = psa_hash_update( &operation, input->x, input->len );
2135 if( status == PSA_SUCCESS )
2136 {
2137 status = psa_hash_verify( &operation, reference_hash->x,
2138 reference_hash->len );
2139 TEST_EQUAL( status, expected_status );
2140 }
2141 else
2142 {
2143 TEST_EQUAL( status, expected_status );
2144 }
2145 }
2146 else
2147 {
2148 TEST_EQUAL( status, expected_status );
2149 }
2150
Gilles Peskine88e08462020-01-28 20:43:00 +01002151exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002152 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002153 PSA_DONE( );
2154}
2155/* END_CASE */
2156
2157/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002158void hash_compute_compare( int alg_arg, data_t *input,
2159 data_t *expected_output )
2160{
2161 psa_algorithm_t alg = alg_arg;
2162 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2163 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002164 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002165 size_t i;
2166
2167 PSA_ASSERT( psa_crypto_init( ) );
2168
Neil Armstrongca30a002022-02-07 11:40:23 +01002169 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002170 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002171 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002172 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002173 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002174 ASSERT_COMPARE( output, output_length,
2175 expected_output->x, expected_output->len );
2176
Neil Armstrongca30a002022-02-07 11:40:23 +01002177 /* Compute with tight buffer, multi-part */
2178 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2179 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2180 PSA_ASSERT( psa_hash_finish( &operation, output,
2181 PSA_HASH_LENGTH( alg ),
2182 &output_length ) );
2183 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2184 ASSERT_COMPARE( output, output_length,
2185 expected_output->x, expected_output->len );
2186
2187 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002188 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2189 output, sizeof( output ),
2190 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002191 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002192 ASSERT_COMPARE( output, output_length,
2193 expected_output->x, expected_output->len );
2194
Neil Armstrongca30a002022-02-07 11:40:23 +01002195 /* Compute with larger buffer, multi-part */
2196 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2197 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2198 PSA_ASSERT( psa_hash_finish( &operation, output,
2199 sizeof( output ), &output_length ) );
2200 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2201 ASSERT_COMPARE( output, output_length,
2202 expected_output->x, expected_output->len );
2203
2204 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002205 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2206 output, output_length ) );
2207
Neil Armstrongca30a002022-02-07 11:40:23 +01002208 /* Compare with correct hash, multi-part */
2209 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2210 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2211 PSA_ASSERT( psa_hash_verify( &operation, output,
2212 output_length ) );
2213
2214 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002215 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2216 output, output_length + 1 ),
2217 PSA_ERROR_INVALID_SIGNATURE );
2218
Neil Armstrongca30a002022-02-07 11:40:23 +01002219 /* Compare with trailing garbage, multi-part */
2220 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2221 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2222 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2223 PSA_ERROR_INVALID_SIGNATURE );
2224
2225 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002226 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2227 output, output_length - 1 ),
2228 PSA_ERROR_INVALID_SIGNATURE );
2229
Neil Armstrongca30a002022-02-07 11:40:23 +01002230 /* Compare with truncated hash, multi-part */
2231 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2232 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2233 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2234 PSA_ERROR_INVALID_SIGNATURE );
2235
Gilles Peskine0a749c82019-11-28 19:33:58 +01002236 /* Compare with corrupted value */
2237 for( i = 0; i < output_length; i++ )
2238 {
Chris Jones9634bb12021-01-20 15:56:42 +00002239 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002240 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002241
2242 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002243 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2244 output, output_length ),
2245 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002246
2247 /* Multi-Part */
2248 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2249 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2250 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2251 PSA_ERROR_INVALID_SIGNATURE );
2252
Gilles Peskine0a749c82019-11-28 19:33:58 +01002253 output[i] ^= 1;
2254 }
2255
2256exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002257 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002258 PSA_DONE( );
2259}
2260/* END_CASE */
2261
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002262/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002263void hash_bad_order( )
2264{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002265 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002266 unsigned char input[] = "";
2267 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002268 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002269 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2270 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2271 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002272 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002273 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002274 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002275
Gilles Peskine8817f612018-12-18 00:18:46 +01002276 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002277
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002278 /* Call setup twice in a row. */
2279 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002280 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002281 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2282 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002283 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002284 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002285 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002286
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002287 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002288 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002289 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002290 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002291
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002292 /* Check that update calls abort on error. */
2293 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002294 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002295 ASSERT_OPERATION_IS_ACTIVE( operation );
2296 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2297 PSA_ERROR_BAD_STATE );
2298 ASSERT_OPERATION_IS_INACTIVE( operation );
2299 PSA_ASSERT( psa_hash_abort( &operation ) );
2300 ASSERT_OPERATION_IS_INACTIVE( operation );
2301
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002302 /* Call update after finish. */
2303 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2304 PSA_ASSERT( psa_hash_finish( &operation,
2305 hash, sizeof( hash ), &hash_len ) );
2306 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002307 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002308 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002309
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002310 /* Call verify without calling setup beforehand. */
2311 TEST_EQUAL( psa_hash_verify( &operation,
2312 valid_hash, sizeof( valid_hash ) ),
2313 PSA_ERROR_BAD_STATE );
2314 PSA_ASSERT( psa_hash_abort( &operation ) );
2315
2316 /* Call verify after finish. */
2317 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2318 PSA_ASSERT( psa_hash_finish( &operation,
2319 hash, sizeof( hash ), &hash_len ) );
2320 TEST_EQUAL( psa_hash_verify( &operation,
2321 valid_hash, sizeof( valid_hash ) ),
2322 PSA_ERROR_BAD_STATE );
2323 PSA_ASSERT( psa_hash_abort( &operation ) );
2324
2325 /* Call verify twice in a row. */
2326 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002327 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002328 PSA_ASSERT( psa_hash_verify( &operation,
2329 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002330 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002331 TEST_EQUAL( psa_hash_verify( &operation,
2332 valid_hash, sizeof( valid_hash ) ),
2333 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002334 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002335 PSA_ASSERT( psa_hash_abort( &operation ) );
2336
2337 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002338 TEST_EQUAL( psa_hash_finish( &operation,
2339 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002340 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002341 PSA_ASSERT( psa_hash_abort( &operation ) );
2342
2343 /* Call finish twice in a row. */
2344 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2345 PSA_ASSERT( psa_hash_finish( &operation,
2346 hash, sizeof( hash ), &hash_len ) );
2347 TEST_EQUAL( psa_hash_finish( &operation,
2348 hash, sizeof( hash ), &hash_len ),
2349 PSA_ERROR_BAD_STATE );
2350 PSA_ASSERT( psa_hash_abort( &operation ) );
2351
2352 /* Call finish after calling verify. */
2353 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2354 PSA_ASSERT( psa_hash_verify( &operation,
2355 valid_hash, sizeof( valid_hash ) ) );
2356 TEST_EQUAL( psa_hash_finish( &operation,
2357 hash, sizeof( hash ), &hash_len ),
2358 PSA_ERROR_BAD_STATE );
2359 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002360
2361exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002362 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002363}
2364/* END_CASE */
2365
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002366/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002367void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002368{
2369 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002370 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2371 * appended to it */
2372 unsigned char hash[] = {
2373 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2374 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2375 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002376 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002377 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002378
Gilles Peskine8817f612018-12-18 00:18:46 +01002379 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002380
itayzafrir27e69452018-11-01 14:26:34 +02002381 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002382 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002383 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002384 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002385 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002386 ASSERT_OPERATION_IS_INACTIVE( operation );
2387 PSA_ASSERT( psa_hash_abort( &operation ) );
2388 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002389
itayzafrir27e69452018-11-01 14:26:34 +02002390 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002391 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002392 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002393 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002394
itayzafrir27e69452018-11-01 14:26:34 +02002395 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002396 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002397 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002398 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002399
itayzafrirec93d302018-10-18 18:01:10 +03002400exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002401 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002402}
2403/* END_CASE */
2404
Ronald Cronee414c72021-03-18 18:50:08 +01002405/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002406void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002407{
2408 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002409 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002410 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002411 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002412 size_t hash_len;
2413
Gilles Peskine8817f612018-12-18 00:18:46 +01002414 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002415
itayzafrir58028322018-10-25 10:22:01 +03002416 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002417 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002418 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002419 hash, expected_size - 1, &hash_len ),
2420 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002421
2422exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002423 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002424}
2425/* END_CASE */
2426
Ronald Cronee414c72021-03-18 18:50:08 +01002427/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002428void hash_clone_source_state( )
2429{
2430 psa_algorithm_t alg = PSA_ALG_SHA_256;
2431 unsigned char hash[PSA_HASH_MAX_SIZE];
2432 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2433 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2434 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2435 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2436 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2437 size_t hash_len;
2438
2439 PSA_ASSERT( psa_crypto_init( ) );
2440 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2441
2442 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2443 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2444 PSA_ASSERT( psa_hash_finish( &op_finished,
2445 hash, sizeof( hash ), &hash_len ) );
2446 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2447 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2448
2449 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2450 PSA_ERROR_BAD_STATE );
2451
2452 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2453 PSA_ASSERT( psa_hash_finish( &op_init,
2454 hash, sizeof( hash ), &hash_len ) );
2455 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2456 PSA_ASSERT( psa_hash_finish( &op_finished,
2457 hash, sizeof( hash ), &hash_len ) );
2458 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2459 PSA_ASSERT( psa_hash_finish( &op_aborted,
2460 hash, sizeof( hash ), &hash_len ) );
2461
2462exit:
2463 psa_hash_abort( &op_source );
2464 psa_hash_abort( &op_init );
2465 psa_hash_abort( &op_setup );
2466 psa_hash_abort( &op_finished );
2467 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002468 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002469}
2470/* END_CASE */
2471
Ronald Cronee414c72021-03-18 18:50:08 +01002472/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002473void hash_clone_target_state( )
2474{
2475 psa_algorithm_t alg = PSA_ALG_SHA_256;
2476 unsigned char hash[PSA_HASH_MAX_SIZE];
2477 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2478 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2479 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2480 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2481 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2482 size_t hash_len;
2483
2484 PSA_ASSERT( psa_crypto_init( ) );
2485
2486 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2487 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2488 PSA_ASSERT( psa_hash_finish( &op_finished,
2489 hash, sizeof( hash ), &hash_len ) );
2490 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2491 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2492
2493 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2494 PSA_ASSERT( psa_hash_finish( &op_target,
2495 hash, sizeof( hash ), &hash_len ) );
2496
2497 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2498 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2499 PSA_ERROR_BAD_STATE );
2500 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2501 PSA_ERROR_BAD_STATE );
2502
2503exit:
2504 psa_hash_abort( &op_target );
2505 psa_hash_abort( &op_init );
2506 psa_hash_abort( &op_setup );
2507 psa_hash_abort( &op_finished );
2508 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002509 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002510}
2511/* END_CASE */
2512
itayzafrir58028322018-10-25 10:22:01 +03002513/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002514void mac_operation_init( )
2515{
Jaeden Amero252ef282019-02-15 14:05:35 +00002516 const uint8_t input[1] = { 0 };
2517
Jaeden Amero769ce272019-01-04 11:48:03 +00002518 /* Test each valid way of initializing the object, except for `= {0}`, as
2519 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2520 * though it's OK by the C standard. We could test for this, but we'd need
2521 * to supress the Clang warning for the test. */
2522 psa_mac_operation_t func = psa_mac_operation_init( );
2523 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2524 psa_mac_operation_t zero;
2525
2526 memset( &zero, 0, sizeof( zero ) );
2527
Jaeden Amero252ef282019-02-15 14:05:35 +00002528 /* A freshly-initialized MAC operation should not be usable. */
2529 TEST_EQUAL( psa_mac_update( &func,
2530 input, sizeof( input ) ),
2531 PSA_ERROR_BAD_STATE );
2532 TEST_EQUAL( psa_mac_update( &init,
2533 input, sizeof( input ) ),
2534 PSA_ERROR_BAD_STATE );
2535 TEST_EQUAL( psa_mac_update( &zero,
2536 input, sizeof( input ) ),
2537 PSA_ERROR_BAD_STATE );
2538
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002539 /* A default MAC operation should be abortable without error. */
2540 PSA_ASSERT( psa_mac_abort( &func ) );
2541 PSA_ASSERT( psa_mac_abort( &init ) );
2542 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002543}
2544/* END_CASE */
2545
2546/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002547void mac_setup( int key_type_arg,
2548 data_t *key,
2549 int alg_arg,
2550 int expected_status_arg )
2551{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002552 psa_key_type_t key_type = key_type_arg;
2553 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002554 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002555 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002556 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2557#if defined(KNOWN_SUPPORTED_MAC_ALG)
2558 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2559#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002560
Gilles Peskine8817f612018-12-18 00:18:46 +01002561 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002562
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002563 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2564 &operation, &status ) )
2565 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002566 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002567
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002568 /* The operation object should be reusable. */
2569#if defined(KNOWN_SUPPORTED_MAC_ALG)
2570 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2571 smoke_test_key_data,
2572 sizeof( smoke_test_key_data ),
2573 KNOWN_SUPPORTED_MAC_ALG,
2574 &operation, &status ) )
2575 goto exit;
2576 TEST_EQUAL( status, PSA_SUCCESS );
2577#endif
2578
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002579exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002580 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002581}
2582/* END_CASE */
2583
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002584/* 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 +00002585void mac_bad_order( )
2586{
Ronald Cron5425a212020-08-04 14:58:35 +02002587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002588 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2589 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002590 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002591 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2592 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2593 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002594 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002595 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2596 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2597 size_t sign_mac_length = 0;
2598 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2599 const uint8_t verify_mac[] = {
2600 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2601 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2602 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2603
2604 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002605 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002606 psa_set_key_algorithm( &attributes, alg );
2607 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002608
Ronald Cron5425a212020-08-04 14:58:35 +02002609 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2610 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002611
Jaeden Amero252ef282019-02-15 14:05:35 +00002612 /* Call update without calling setup beforehand. */
2613 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2614 PSA_ERROR_BAD_STATE );
2615 PSA_ASSERT( psa_mac_abort( &operation ) );
2616
2617 /* Call sign finish without calling setup beforehand. */
2618 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2619 &sign_mac_length),
2620 PSA_ERROR_BAD_STATE );
2621 PSA_ASSERT( psa_mac_abort( &operation ) );
2622
2623 /* Call verify finish without calling setup beforehand. */
2624 TEST_EQUAL( psa_mac_verify_finish( &operation,
2625 verify_mac, sizeof( verify_mac ) ),
2626 PSA_ERROR_BAD_STATE );
2627 PSA_ASSERT( psa_mac_abort( &operation ) );
2628
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002629 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002630 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002631 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002632 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002633 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002634 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002635 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002636 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002637
Jaeden Amero252ef282019-02-15 14:05:35 +00002638 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002639 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002640 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2641 PSA_ASSERT( psa_mac_sign_finish( &operation,
2642 sign_mac, sizeof( sign_mac ),
2643 &sign_mac_length ) );
2644 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2645 PSA_ERROR_BAD_STATE );
2646 PSA_ASSERT( psa_mac_abort( &operation ) );
2647
2648 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002649 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002650 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2651 PSA_ASSERT( psa_mac_verify_finish( &operation,
2652 verify_mac, sizeof( verify_mac ) ) );
2653 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2654 PSA_ERROR_BAD_STATE );
2655 PSA_ASSERT( psa_mac_abort( &operation ) );
2656
2657 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002658 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002659 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2660 PSA_ASSERT( psa_mac_sign_finish( &operation,
2661 sign_mac, sizeof( sign_mac ),
2662 &sign_mac_length ) );
2663 TEST_EQUAL( psa_mac_sign_finish( &operation,
2664 sign_mac, sizeof( sign_mac ),
2665 &sign_mac_length ),
2666 PSA_ERROR_BAD_STATE );
2667 PSA_ASSERT( psa_mac_abort( &operation ) );
2668
2669 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002670 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002671 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2672 PSA_ASSERT( psa_mac_verify_finish( &operation,
2673 verify_mac, sizeof( verify_mac ) ) );
2674 TEST_EQUAL( psa_mac_verify_finish( &operation,
2675 verify_mac, sizeof( verify_mac ) ),
2676 PSA_ERROR_BAD_STATE );
2677 PSA_ASSERT( psa_mac_abort( &operation ) );
2678
2679 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002680 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002681 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002682 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002683 TEST_EQUAL( psa_mac_verify_finish( &operation,
2684 verify_mac, sizeof( verify_mac ) ),
2685 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002686 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002687 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002688 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002689
2690 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002691 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002692 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002693 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002694 TEST_EQUAL( psa_mac_sign_finish( &operation,
2695 sign_mac, sizeof( sign_mac ),
2696 &sign_mac_length ),
2697 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002698 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002699 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002700 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002701
Ronald Cron5425a212020-08-04 14:58:35 +02002702 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002703
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002704exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002705 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002706}
2707/* END_CASE */
2708
2709/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01002710void mac_sign_verify_multi( int key_type_arg,
2711 data_t *key_data,
2712 int alg_arg,
2713 data_t *input,
2714 int is_verify,
2715 data_t *expected_mac )
2716{
2717 size_t data_part_len = 0;
2718
2719 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
2720 {
2721 /* Split data into length(data_part_len) parts. */
2722 mbedtls_test_set_step( 2000 + data_part_len );
2723
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01002724 if( mac_multipart_internal_func( key_type_arg, key_data,
2725 alg_arg,
2726 input, data_part_len,
2727 expected_mac,
2728 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01002729 break;
2730
2731 /* length(0) part, length(data_part_len) part, length(0) part... */
2732 mbedtls_test_set_step( 3000 + data_part_len );
2733
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01002734 if( mac_multipart_internal_func( key_type_arg, key_data,
2735 alg_arg,
2736 input, data_part_len,
2737 expected_mac,
2738 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01002739 break;
2740 }
2741
2742 /* Goto is required to silence warnings about unused labels, as we
2743 * don't actually do any test assertions in this function. */
2744 goto exit;
2745}
2746/* END_CASE */
2747
2748/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002749void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002750 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002751 int alg_arg,
2752 data_t *input,
2753 data_t *expected_mac )
2754{
Ronald Cron5425a212020-08-04 14:58:35 +02002755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002756 psa_key_type_t key_type = key_type_arg;
2757 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002758 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002760 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002761 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002762 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002763 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002764 const size_t output_sizes_to_test[] = {
2765 0,
2766 1,
2767 expected_mac->len - 1,
2768 expected_mac->len,
2769 expected_mac->len + 1,
2770 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002771
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002772 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002773 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002774 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002775
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002777
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002779 psa_set_key_algorithm( &attributes, alg );
2780 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002781
Ronald Cron5425a212020-08-04 14:58:35 +02002782 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2783 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002784
Gilles Peskine8b356b52020-08-25 23:44:59 +02002785 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2786 {
2787 const size_t output_size = output_sizes_to_test[i];
2788 psa_status_t expected_status =
2789 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2790 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002791
Chris Jones9634bb12021-01-20 15:56:42 +00002792 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002793 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002794
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002795 /* Calculate the MAC, one-shot case. */
2796 TEST_EQUAL( psa_mac_compute( key, alg,
2797 input->x, input->len,
2798 actual_mac, output_size, &mac_length ),
2799 expected_status );
2800 if( expected_status == PSA_SUCCESS )
2801 {
2802 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2803 actual_mac, mac_length );
2804 }
2805
2806 if( output_size > 0 )
2807 memset( actual_mac, 0, output_size );
2808
2809 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002810 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002811 PSA_ASSERT( psa_mac_update( &operation,
2812 input->x, input->len ) );
2813 TEST_EQUAL( psa_mac_sign_finish( &operation,
2814 actual_mac, output_size,
2815 &mac_length ),
2816 expected_status );
2817 PSA_ASSERT( psa_mac_abort( &operation ) );
2818
2819 if( expected_status == PSA_SUCCESS )
2820 {
2821 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2822 actual_mac, mac_length );
2823 }
2824 mbedtls_free( actual_mac );
2825 actual_mac = NULL;
2826 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002827
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002828exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002829 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002830 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002831 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002832 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002833}
2834/* END_CASE */
2835
2836/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002837void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002838 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002839 int alg_arg,
2840 data_t *input,
2841 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002842{
Ronald Cron5425a212020-08-04 14:58:35 +02002843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844 psa_key_type_t key_type = key_type_arg;
2845 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002846 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002848 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002849
Gilles Peskine69c12672018-06-28 00:07:19 +02002850 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2851
Gilles Peskine8817f612018-12-18 00:18:46 +01002852 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002854 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002855 psa_set_key_algorithm( &attributes, alg );
2856 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002857
Ronald Cron5425a212020-08-04 14:58:35 +02002858 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2859 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002860
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002861 /* Verify correct MAC, one-shot case. */
2862 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2863 expected_mac->x, expected_mac->len ) );
2864
2865 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002866 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002867 PSA_ASSERT( psa_mac_update( &operation,
2868 input->x, input->len ) );
2869 PSA_ASSERT( psa_mac_verify_finish( &operation,
2870 expected_mac->x,
2871 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002873 /* Test a MAC that's too short, one-shot case. */
2874 TEST_EQUAL( psa_mac_verify( key, alg,
2875 input->x, input->len,
2876 expected_mac->x,
2877 expected_mac->len - 1 ),
2878 PSA_ERROR_INVALID_SIGNATURE );
2879
2880 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002881 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002882 PSA_ASSERT( psa_mac_update( &operation,
2883 input->x, input->len ) );
2884 TEST_EQUAL( psa_mac_verify_finish( &operation,
2885 expected_mac->x,
2886 expected_mac->len - 1 ),
2887 PSA_ERROR_INVALID_SIGNATURE );
2888
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002889 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002890 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2891 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002892 TEST_EQUAL( psa_mac_verify( key, alg,
2893 input->x, input->len,
2894 perturbed_mac, expected_mac->len + 1 ),
2895 PSA_ERROR_INVALID_SIGNATURE );
2896
2897 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002898 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002899 PSA_ASSERT( psa_mac_update( &operation,
2900 input->x, input->len ) );
2901 TEST_EQUAL( psa_mac_verify_finish( &operation,
2902 perturbed_mac,
2903 expected_mac->len + 1 ),
2904 PSA_ERROR_INVALID_SIGNATURE );
2905
2906 /* Test changing one byte. */
2907 for( size_t i = 0; i < expected_mac->len; i++ )
2908 {
Chris Jones9634bb12021-01-20 15:56:42 +00002909 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002910 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002911
2912 TEST_EQUAL( psa_mac_verify( key, alg,
2913 input->x, input->len,
2914 perturbed_mac, expected_mac->len ),
2915 PSA_ERROR_INVALID_SIGNATURE );
2916
Ronald Cron5425a212020-08-04 14:58:35 +02002917 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002918 PSA_ASSERT( psa_mac_update( &operation,
2919 input->x, input->len ) );
2920 TEST_EQUAL( psa_mac_verify_finish( &operation,
2921 perturbed_mac,
2922 expected_mac->len ),
2923 PSA_ERROR_INVALID_SIGNATURE );
2924 perturbed_mac[i] ^= 1;
2925 }
2926
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002928 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002929 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002930 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002931 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002932}
2933/* END_CASE */
2934
2935/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002936void cipher_operation_init( )
2937{
Jaeden Ameroab439972019-02-15 14:12:05 +00002938 const uint8_t input[1] = { 0 };
2939 unsigned char output[1] = { 0 };
2940 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002941 /* Test each valid way of initializing the object, except for `= {0}`, as
2942 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2943 * though it's OK by the C standard. We could test for this, but we'd need
2944 * to supress the Clang warning for the test. */
2945 psa_cipher_operation_t func = psa_cipher_operation_init( );
2946 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2947 psa_cipher_operation_t zero;
2948
2949 memset( &zero, 0, sizeof( zero ) );
2950
Jaeden Ameroab439972019-02-15 14:12:05 +00002951 /* A freshly-initialized cipher operation should not be usable. */
2952 TEST_EQUAL( psa_cipher_update( &func,
2953 input, sizeof( input ),
2954 output, sizeof( output ),
2955 &output_length ),
2956 PSA_ERROR_BAD_STATE );
2957 TEST_EQUAL( psa_cipher_update( &init,
2958 input, sizeof( input ),
2959 output, sizeof( output ),
2960 &output_length ),
2961 PSA_ERROR_BAD_STATE );
2962 TEST_EQUAL( psa_cipher_update( &zero,
2963 input, sizeof( input ),
2964 output, sizeof( output ),
2965 &output_length ),
2966 PSA_ERROR_BAD_STATE );
2967
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002968 /* A default cipher operation should be abortable without error. */
2969 PSA_ASSERT( psa_cipher_abort( &func ) );
2970 PSA_ASSERT( psa_cipher_abort( &init ) );
2971 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002972}
2973/* END_CASE */
2974
2975/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002976void cipher_setup( int key_type_arg,
2977 data_t *key,
2978 int alg_arg,
2979 int expected_status_arg )
2980{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002981 psa_key_type_t key_type = key_type_arg;
2982 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002983 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002984 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002985 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002986#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002987 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2988#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002989
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002991
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002992 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2993 &operation, &status ) )
2994 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002995 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002996
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002997 /* The operation object should be reusable. */
2998#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2999 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3000 smoke_test_key_data,
3001 sizeof( smoke_test_key_data ),
3002 KNOWN_SUPPORTED_CIPHER_ALG,
3003 &operation, &status ) )
3004 goto exit;
3005 TEST_EQUAL( status, PSA_SUCCESS );
3006#endif
3007
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003008exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003009 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003010 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003011}
3012/* END_CASE */
3013
Ronald Cronee414c72021-03-18 18:50:08 +01003014/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003015void cipher_bad_order( )
3016{
Ronald Cron5425a212020-08-04 14:58:35 +02003017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003018 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3019 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003021 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003022 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003023 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003024 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3025 0xaa, 0xaa, 0xaa, 0xaa };
3026 const uint8_t text[] = {
3027 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3028 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003029 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003030 size_t length = 0;
3031
3032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3034 psa_set_key_algorithm( &attributes, alg );
3035 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003036 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3037 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003038
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003039 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003040 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003041 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003042 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003043 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003044 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003045 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003046 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003047
3048 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003049 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003050 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003051 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003052 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003053 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003054 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003055 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003056
Jaeden Ameroab439972019-02-15 14:12:05 +00003057 /* Generate an IV without calling setup beforehand. */
3058 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3059 buffer, sizeof( buffer ),
3060 &length ),
3061 PSA_ERROR_BAD_STATE );
3062 PSA_ASSERT( psa_cipher_abort( &operation ) );
3063
3064 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003065 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003066 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3067 buffer, sizeof( buffer ),
3068 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003069 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003070 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3071 buffer, sizeof( buffer ),
3072 &length ),
3073 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003074 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003075 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003076 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003077
3078 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003079 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003080 PSA_ASSERT( psa_cipher_set_iv( &operation,
3081 iv, sizeof( iv ) ) );
3082 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3083 buffer, sizeof( buffer ),
3084 &length ),
3085 PSA_ERROR_BAD_STATE );
3086 PSA_ASSERT( psa_cipher_abort( &operation ) );
3087
3088 /* Set an IV without calling setup beforehand. */
3089 TEST_EQUAL( psa_cipher_set_iv( &operation,
3090 iv, sizeof( iv ) ),
3091 PSA_ERROR_BAD_STATE );
3092 PSA_ASSERT( psa_cipher_abort( &operation ) );
3093
3094 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003095 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003096 PSA_ASSERT( psa_cipher_set_iv( &operation,
3097 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003098 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003099 TEST_EQUAL( psa_cipher_set_iv( &operation,
3100 iv, sizeof( iv ) ),
3101 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003102 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003103 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003104 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003105
3106 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003107 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003108 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3109 buffer, sizeof( buffer ),
3110 &length ) );
3111 TEST_EQUAL( psa_cipher_set_iv( &operation,
3112 iv, sizeof( iv ) ),
3113 PSA_ERROR_BAD_STATE );
3114 PSA_ASSERT( psa_cipher_abort( &operation ) );
3115
3116 /* Call update without calling setup beforehand. */
3117 TEST_EQUAL( psa_cipher_update( &operation,
3118 text, sizeof( text ),
3119 buffer, sizeof( buffer ),
3120 &length ),
3121 PSA_ERROR_BAD_STATE );
3122 PSA_ASSERT( psa_cipher_abort( &operation ) );
3123
3124 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003125 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003126 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003127 TEST_EQUAL( psa_cipher_update( &operation,
3128 text, sizeof( text ),
3129 buffer, sizeof( buffer ),
3130 &length ),
3131 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003132 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003133 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003134 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003135
3136 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003137 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003138 PSA_ASSERT( psa_cipher_set_iv( &operation,
3139 iv, sizeof( iv ) ) );
3140 PSA_ASSERT( psa_cipher_finish( &operation,
3141 buffer, sizeof( buffer ), &length ) );
3142 TEST_EQUAL( psa_cipher_update( &operation,
3143 text, sizeof( text ),
3144 buffer, sizeof( buffer ),
3145 &length ),
3146 PSA_ERROR_BAD_STATE );
3147 PSA_ASSERT( psa_cipher_abort( &operation ) );
3148
3149 /* Call finish without calling setup beforehand. */
3150 TEST_EQUAL( psa_cipher_finish( &operation,
3151 buffer, sizeof( buffer ), &length ),
3152 PSA_ERROR_BAD_STATE );
3153 PSA_ASSERT( psa_cipher_abort( &operation ) );
3154
3155 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003156 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003157 /* Not calling update means we are encrypting an empty buffer, which is OK
3158 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003159 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003160 TEST_EQUAL( psa_cipher_finish( &operation,
3161 buffer, sizeof( buffer ), &length ),
3162 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003163 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003164 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003165 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003166
3167 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003168 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003169 PSA_ASSERT( psa_cipher_set_iv( &operation,
3170 iv, sizeof( iv ) ) );
3171 PSA_ASSERT( psa_cipher_finish( &operation,
3172 buffer, sizeof( buffer ), &length ) );
3173 TEST_EQUAL( psa_cipher_finish( &operation,
3174 buffer, sizeof( buffer ), &length ),
3175 PSA_ERROR_BAD_STATE );
3176 PSA_ASSERT( psa_cipher_abort( &operation ) );
3177
Ronald Cron5425a212020-08-04 14:58:35 +02003178 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003179
Jaeden Ameroab439972019-02-15 14:12:05 +00003180exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003181 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003182 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003183}
3184/* END_CASE */
3185
3186/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003187void cipher_encrypt_fail( int alg_arg,
3188 int key_type_arg,
3189 data_t *key_data,
3190 data_t *input,
3191 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003192{
Ronald Cron5425a212020-08-04 14:58:35 +02003193 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003194 psa_status_t status;
3195 psa_key_type_t key_type = key_type_arg;
3196 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003197 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003198 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3199 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3200 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003201 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003202 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003203 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003204 size_t function_output_length;
3205 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3207
3208 if ( PSA_ERROR_BAD_STATE != expected_status )
3209 {
3210 PSA_ASSERT( psa_crypto_init( ) );
3211
3212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3213 psa_set_key_algorithm( &attributes, alg );
3214 psa_set_key_type( &attributes, key_type );
3215
3216 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3217 input->len );
3218 ASSERT_ALLOC( output, output_buffer_size );
3219
3220 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3221 &key ) );
3222 }
3223
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003224 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003225 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3226 output_buffer_size, &output_length );
3227
3228 TEST_EQUAL( status, expected_status );
3229
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003230 /* Encrypt, multi-part */
3231 status = psa_cipher_encrypt_setup( &operation, key, alg );
3232 if( status == PSA_SUCCESS )
3233 {
3234 if( alg != PSA_ALG_ECB_NO_PADDING )
3235 {
3236 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3237 iv, iv_size,
3238 &iv_length ) );
3239 }
3240
3241 status = psa_cipher_update( &operation, input->x, input->len,
3242 output, output_buffer_size,
3243 &function_output_length );
3244 if( status == PSA_SUCCESS )
3245 {
3246 output_length += function_output_length;
3247
3248 status = psa_cipher_finish( &operation, output + output_length,
3249 output_buffer_size - output_length,
3250 &function_output_length );
3251
3252 TEST_EQUAL( status, expected_status );
3253 }
3254 else
3255 {
3256 TEST_EQUAL( status, expected_status );
3257 }
3258 }
3259 else
3260 {
3261 TEST_EQUAL( status, expected_status );
3262 }
3263
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003264exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003265 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003266 mbedtls_free( output );
3267 psa_destroy_key( key );
3268 PSA_DONE( );
3269}
3270/* END_CASE */
3271
3272/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003273void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3274 data_t *input, int iv_length,
3275 int expected_result )
3276{
3277 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3278 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3280 size_t output_buffer_size = 0;
3281 unsigned char *output = NULL;
3282
3283 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3284 ASSERT_ALLOC( output, output_buffer_size );
3285
3286 PSA_ASSERT( psa_crypto_init( ) );
3287
3288 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3289 psa_set_key_algorithm( &attributes, alg );
3290 psa_set_key_type( &attributes, key_type );
3291
3292 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3293 &key ) );
3294 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3295 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3296 iv_length ) );
3297
3298exit:
3299 psa_cipher_abort( &operation );
3300 mbedtls_free( output );
3301 psa_destroy_key( key );
3302 PSA_DONE( );
3303}
3304/* END_CASE */
3305
3306/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003307void cipher_encrypt_alg_without_iv( int alg_arg,
3308 int key_type_arg,
3309 data_t *key_data,
3310 data_t *input,
3311 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003312{
3313 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3314 psa_key_type_t key_type = key_type_arg;
3315 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003316 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3317 uint8_t iv[1] = { 0x5a };
3318 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003319 unsigned char *output = NULL;
3320 size_t output_buffer_size = 0;
3321 size_t output_length = 0;
3322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3323
3324 PSA_ASSERT( psa_crypto_init( ) );
3325
3326 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3327 psa_set_key_algorithm( &attributes, alg );
3328 psa_set_key_type( &attributes, key_type );
3329
3330 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3331 ASSERT_ALLOC( output, output_buffer_size );
3332
3333 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3334 &key ) );
3335
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003336 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3337 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3338 PSA_ERROR_BAD_STATE );
3339 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3340 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
3341 &iv_length ),
3342 PSA_ERROR_BAD_STATE );
3343
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003344 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003345 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
3346 output_buffer_size, &output_length ) );
3347 TEST_ASSERT( output_length <=
3348 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3349 TEST_ASSERT( output_length <=
3350 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3351
3352 ASSERT_COMPARE( expected_output->x, expected_output->len,
3353 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003354
3355 /* Encrypt, multi-part */
3356 PSA_ASSERT( psa_cipher_abort( &operation ) );
3357 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3358
3359 PSA_ASSERT( psa_cipher_update( &operation, input->x, input->len,
3360 output, output_buffer_size,
3361 &output_length) );
3362 TEST_ASSERT( output_length <=
3363 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3364 TEST_ASSERT( output_length <=
3365 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3366
3367 ASSERT_COMPARE( expected_output->x, expected_output->len,
3368 output, output_length );
3369
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003370exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003371 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003372 mbedtls_free( output );
3373 psa_destroy_key( key );
3374 PSA_DONE( );
3375}
3376/* END_CASE */
3377
3378/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003379void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3380{
3381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3382 psa_algorithm_t alg = alg_arg;
3383 psa_key_type_t key_type = key_type_arg;
3384 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3385 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3386 psa_status_t status;
3387
3388 PSA_ASSERT( psa_crypto_init( ) );
3389
3390 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3391 psa_set_key_algorithm( &attributes, alg );
3392 psa_set_key_type( &attributes, key_type );
3393
3394 /* Usage of either of these two size macros would cause divide by zero
3395 * with incorrect key types previously. Input length should be irrelevant
3396 * here. */
3397 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3398 0 );
3399 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3400
3401
3402 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3403 &key ) );
3404
3405 /* Should fail due to invalid alg type (to support invalid key type).
3406 * Encrypt or decrypt will end up in the same place. */
3407 status = psa_cipher_encrypt_setup( &operation, key, alg );
3408
3409 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3410
3411exit:
3412 psa_cipher_abort( &operation );
3413 psa_destroy_key( key );
3414 PSA_DONE( );
3415}
3416/* END_CASE */
3417
3418/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003419void cipher_encrypt_validation( int alg_arg,
3420 int key_type_arg,
3421 data_t *key_data,
3422 data_t *input )
3423{
3424 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3425 psa_key_type_t key_type = key_type_arg;
3426 psa_algorithm_t alg = alg_arg;
3427 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3428 unsigned char *output1 = NULL;
3429 size_t output1_buffer_size = 0;
3430 size_t output1_length = 0;
3431 unsigned char *output2 = NULL;
3432 size_t output2_buffer_size = 0;
3433 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003434 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003435 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003437
Gilles Peskine8817f612018-12-18 00:18:46 +01003438 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003439
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003440 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3441 psa_set_key_algorithm( &attributes, alg );
3442 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003443
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003444 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3445 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3446 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3447 ASSERT_ALLOC( output1, output1_buffer_size );
3448 ASSERT_ALLOC( output2, output2_buffer_size );
3449
Ronald Cron5425a212020-08-04 14:58:35 +02003450 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3451 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003452
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003453 /* The one-shot cipher encryption uses generated iv so validating
3454 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003455 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3456 output1_buffer_size, &output1_length ) );
3457 TEST_ASSERT( output1_length <=
3458 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3459 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003460 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003461
3462 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3463 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003464
Gilles Peskine8817f612018-12-18 00:18:46 +01003465 PSA_ASSERT( psa_cipher_update( &operation,
3466 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003467 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003468 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003469 TEST_ASSERT( function_output_length <=
3470 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3471 TEST_ASSERT( function_output_length <=
3472 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003473 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003474
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003475 PSA_ASSERT( psa_cipher_finish( &operation,
3476 output2 + output2_length,
3477 output2_buffer_size - output2_length,
3478 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003479 TEST_ASSERT( function_output_length <=
3480 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3481 TEST_ASSERT( function_output_length <=
3482 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003483 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003484
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003485 PSA_ASSERT( psa_cipher_abort( &operation ) );
3486 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3487 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003488
Gilles Peskine50e586b2018-06-08 14:28:46 +02003489exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003490 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003491 mbedtls_free( output1 );
3492 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003493 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003494 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003495}
3496/* END_CASE */
3497
3498/* BEGIN_CASE */
3499void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003500 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003501 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003502 int first_part_size_arg,
3503 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003504 data_t *expected_output,
3505 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003506{
Ronald Cron5425a212020-08-04 14:58:35 +02003507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003508 psa_key_type_t key_type = key_type_arg;
3509 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003510 psa_status_t status;
3511 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003512 size_t first_part_size = first_part_size_arg;
3513 size_t output1_length = output1_length_arg;
3514 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003515 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003516 size_t output_buffer_size = 0;
3517 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003518 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003519 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003521
Gilles Peskine8817f612018-12-18 00:18:46 +01003522 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003523
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003524 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3525 psa_set_key_algorithm( &attributes, alg );
3526 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003527
Ronald Cron5425a212020-08-04 14:58:35 +02003528 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3529 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003530
Ronald Cron5425a212020-08-04 14:58:35 +02003531 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003532
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003533 if( iv->len > 0 )
3534 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003535 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003536 }
3537
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003538 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3539 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003540 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003541
Gilles Peskinee0866522019-02-19 19:44:00 +01003542 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003543 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3544 output, output_buffer_size,
3545 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003546 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003547 TEST_ASSERT( function_output_length <=
3548 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3549 TEST_ASSERT( function_output_length <=
3550 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003551 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003552
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003553 if( first_part_size < input->len )
3554 {
3555 PSA_ASSERT( psa_cipher_update( &operation,
3556 input->x + first_part_size,
3557 input->len - first_part_size,
3558 ( output_buffer_size == 0 ? NULL :
3559 output + total_output_length ),
3560 output_buffer_size - total_output_length,
3561 &function_output_length ) );
3562 TEST_ASSERT( function_output_length == output2_length );
3563 TEST_ASSERT( function_output_length <=
3564 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3565 alg,
3566 input->len - first_part_size ) );
3567 TEST_ASSERT( function_output_length <=
3568 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3569 total_output_length += function_output_length;
3570 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003571
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003572 status = psa_cipher_finish( &operation,
3573 ( output_buffer_size == 0 ? NULL :
3574 output + total_output_length ),
3575 output_buffer_size - total_output_length,
3576 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003577 TEST_ASSERT( function_output_length <=
3578 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3579 TEST_ASSERT( function_output_length <=
3580 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003581 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003582 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003583
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003584 if( expected_status == PSA_SUCCESS )
3585 {
3586 PSA_ASSERT( psa_cipher_abort( &operation ) );
3587
3588 ASSERT_COMPARE( expected_output->x, expected_output->len,
3589 output, total_output_length );
3590 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003591
3592exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003593 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003594 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003595 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003596 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003597}
3598/* END_CASE */
3599
3600/* BEGIN_CASE */
3601void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003602 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003603 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003604 int first_part_size_arg,
3605 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003606 data_t *expected_output,
3607 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003608{
Ronald Cron5425a212020-08-04 14:58:35 +02003609 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003610 psa_key_type_t key_type = key_type_arg;
3611 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003612 psa_status_t status;
3613 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003614 size_t first_part_size = first_part_size_arg;
3615 size_t output1_length = output1_length_arg;
3616 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003617 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003618 size_t output_buffer_size = 0;
3619 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003620 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003621 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003622 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003623
Gilles Peskine8817f612018-12-18 00:18:46 +01003624 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003625
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003626 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3627 psa_set_key_algorithm( &attributes, alg );
3628 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003629
Ronald Cron5425a212020-08-04 14:58:35 +02003630 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3631 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003632
Ronald Cron5425a212020-08-04 14:58:35 +02003633 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003634
Steven Cooreman177deba2020-09-07 17:14:14 +02003635 if( iv->len > 0 )
3636 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003637 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003638 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003639
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003640 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3641 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003642 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003643
Gilles Peskinee0866522019-02-19 19:44:00 +01003644 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003645 PSA_ASSERT( psa_cipher_update( &operation,
3646 input->x, first_part_size,
3647 output, output_buffer_size,
3648 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003649 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003650 TEST_ASSERT( function_output_length <=
3651 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3652 TEST_ASSERT( function_output_length <=
3653 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003654 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003655
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003656 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003657 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003658 PSA_ASSERT( psa_cipher_update( &operation,
3659 input->x + first_part_size,
3660 input->len - first_part_size,
3661 ( output_buffer_size == 0 ? NULL :
3662 output + total_output_length ),
3663 output_buffer_size - total_output_length,
3664 &function_output_length ) );
3665 TEST_ASSERT( function_output_length == output2_length );
3666 TEST_ASSERT( function_output_length <=
3667 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3668 alg,
3669 input->len - first_part_size ) );
3670 TEST_ASSERT( function_output_length <=
3671 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3672 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003673 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003674
Gilles Peskine50e586b2018-06-08 14:28:46 +02003675 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003676 ( output_buffer_size == 0 ? NULL :
3677 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003678 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003679 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003680 TEST_ASSERT( function_output_length <=
3681 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3682 TEST_ASSERT( function_output_length <=
3683 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003684 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003685 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003686
3687 if( expected_status == PSA_SUCCESS )
3688 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003689 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003690
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003691 ASSERT_COMPARE( expected_output->x, expected_output->len,
3692 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003693 }
3694
Gilles Peskine50e586b2018-06-08 14:28:46 +02003695exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003696 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003697 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003698 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003699 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003700}
3701/* END_CASE */
3702
Gilles Peskine50e586b2018-06-08 14:28:46 +02003703/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003704void cipher_decrypt_fail( int alg_arg,
3705 int key_type_arg,
3706 data_t *key_data,
3707 data_t *iv,
3708 data_t *input_arg,
3709 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003710{
3711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3712 psa_status_t status;
3713 psa_key_type_t key_type = key_type_arg;
3714 psa_algorithm_t alg = alg_arg;
3715 psa_status_t expected_status = expected_status_arg;
3716 unsigned char *input = NULL;
3717 size_t input_buffer_size = 0;
3718 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01003719 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003720 size_t output_buffer_size = 0;
3721 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01003722 size_t function_output_length;
3723 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3725
3726 if ( PSA_ERROR_BAD_STATE != expected_status )
3727 {
3728 PSA_ASSERT( psa_crypto_init( ) );
3729
3730 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3731 psa_set_key_algorithm( &attributes, alg );
3732 psa_set_key_type( &attributes, key_type );
3733
3734 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3735 &key ) );
3736 }
3737
3738 /* Allocate input buffer and copy the iv and the plaintext */
3739 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3740 if ( input_buffer_size > 0 )
3741 {
3742 ASSERT_ALLOC( input, input_buffer_size );
3743 memcpy( input, iv->x, iv->len );
3744 memcpy( input + iv->len, input_arg->x, input_arg->len );
3745 }
3746
3747 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3748 ASSERT_ALLOC( output, output_buffer_size );
3749
Neil Armstrong66a479f2022-02-07 15:41:19 +01003750 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003751 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3752 output_buffer_size, &output_length );
3753 TEST_EQUAL( status, expected_status );
3754
Neil Armstrong66a479f2022-02-07 15:41:19 +01003755 /* Decrypt, multi-part */
3756 status = psa_cipher_decrypt_setup( &operation, key, alg );
3757 if( status == PSA_SUCCESS )
3758 {
3759 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
3760 input_arg->len ) +
3761 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3762 ASSERT_ALLOC( output_multi, output_buffer_size );
3763
3764 if( iv->len > 0 )
3765 {
3766 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
3767
3768 if( status != PSA_SUCCESS )
3769 TEST_EQUAL( status, expected_status );
3770 }
3771
3772 if( status == PSA_SUCCESS )
3773 {
3774 status = psa_cipher_update( &operation,
3775 input_arg->x, input_arg->len,
3776 output_multi, output_buffer_size,
3777 &function_output_length );
3778 if( status == PSA_SUCCESS )
3779 {
3780 output_length = function_output_length;
3781
3782 status = psa_cipher_finish( &operation,
3783 output_multi + output_length,
3784 output_buffer_size - output_length,
3785 &function_output_length );
3786
3787 TEST_EQUAL( status, expected_status );
3788 }
3789 else
3790 {
3791 TEST_EQUAL( status, expected_status );
3792 }
3793 }
3794 else
3795 {
3796 TEST_EQUAL( status, expected_status );
3797 }
3798 }
3799 else
3800 {
3801 TEST_EQUAL( status, expected_status );
3802 }
3803
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003804exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01003805 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003806 mbedtls_free( input );
3807 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01003808 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003809 psa_destroy_key( key );
3810 PSA_DONE( );
3811}
3812/* END_CASE */
3813
3814/* BEGIN_CASE */
3815void cipher_decrypt( int alg_arg,
3816 int key_type_arg,
3817 data_t *key_data,
3818 data_t *iv,
3819 data_t *input_arg,
3820 data_t *expected_output )
3821{
3822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3823 psa_key_type_t key_type = key_type_arg;
3824 psa_algorithm_t alg = alg_arg;
3825 unsigned char *input = NULL;
3826 size_t input_buffer_size = 0;
3827 unsigned char *output = NULL;
3828 size_t output_buffer_size = 0;
3829 size_t output_length = 0;
3830 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3831
3832 PSA_ASSERT( psa_crypto_init( ) );
3833
3834 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3835 psa_set_key_algorithm( &attributes, alg );
3836 psa_set_key_type( &attributes, key_type );
3837
3838 /* Allocate input buffer and copy the iv and the plaintext */
3839 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3840 if ( input_buffer_size > 0 )
3841 {
3842 ASSERT_ALLOC( input, input_buffer_size );
3843 memcpy( input, iv->x, iv->len );
3844 memcpy( input + iv->len, input_arg->x, input_arg->len );
3845 }
3846
3847 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3848 ASSERT_ALLOC( output, output_buffer_size );
3849
3850 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3851 &key ) );
3852
3853 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3854 output_buffer_size, &output_length ) );
3855 TEST_ASSERT( output_length <=
3856 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3857 TEST_ASSERT( output_length <=
3858 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3859
3860 ASSERT_COMPARE( expected_output->x, expected_output->len,
3861 output, output_length );
3862exit:
3863 mbedtls_free( input );
3864 mbedtls_free( output );
3865 psa_destroy_key( key );
3866 PSA_DONE( );
3867}
3868/* END_CASE */
3869
3870/* BEGIN_CASE */
3871void cipher_verify_output( int alg_arg,
3872 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003873 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003874 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003875{
Ronald Cron5425a212020-08-04 14:58:35 +02003876 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003877 psa_key_type_t key_type = key_type_arg;
3878 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003879 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003880 size_t output1_size = 0;
3881 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003882 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003883 size_t output2_size = 0;
3884 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003886
Gilles Peskine8817f612018-12-18 00:18:46 +01003887 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003888
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003889 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3890 psa_set_key_algorithm( &attributes, alg );
3891 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003892
Ronald Cron5425a212020-08-04 14:58:35 +02003893 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3894 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003895 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003896 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003897
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003898 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3899 output1, output1_size,
3900 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003901 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003902 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003903 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003904 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003905
3906 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003907 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003908
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003909 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3910 output2, output2_size,
3911 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003912 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003913 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003914 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003915 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003916
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003917 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003918
3919exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003920 mbedtls_free( output1 );
3921 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003922 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003923 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003924}
3925/* END_CASE */
3926
3927/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003928void cipher_verify_output_multipart( int alg_arg,
3929 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003930 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003931 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003932 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003933{
Ronald Cron5425a212020-08-04 14:58:35 +02003934 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003935 psa_key_type_t key_type = key_type_arg;
3936 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003937 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003938 unsigned char iv[16] = {0};
3939 size_t iv_size = 16;
3940 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003941 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003942 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003943 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003944 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003945 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003946 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003947 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003948 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3949 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003950 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003951
Gilles Peskine8817f612018-12-18 00:18:46 +01003952 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003953
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003954 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3955 psa_set_key_algorithm( &attributes, alg );
3956 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003957
Ronald Cron5425a212020-08-04 14:58:35 +02003958 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3959 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003960
Ronald Cron5425a212020-08-04 14:58:35 +02003961 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3962 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003963
Steven Cooreman177deba2020-09-07 17:14:14 +02003964 if( alg != PSA_ALG_ECB_NO_PADDING )
3965 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003966 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3967 iv, iv_size,
3968 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003969 }
3970
gabor-mezei-armceface22021-01-21 12:26:17 +01003971 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3972 TEST_ASSERT( output1_buffer_size <=
3973 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003974 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003975
Gilles Peskinee0866522019-02-19 19:44:00 +01003976 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003977
Gilles Peskine8817f612018-12-18 00:18:46 +01003978 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3979 output1, output1_buffer_size,
3980 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003981 TEST_ASSERT( function_output_length <=
3982 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3983 TEST_ASSERT( function_output_length <=
3984 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003985 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003986
Gilles Peskine8817f612018-12-18 00:18:46 +01003987 PSA_ASSERT( psa_cipher_update( &operation1,
3988 input->x + first_part_size,
3989 input->len - first_part_size,
3990 output1, output1_buffer_size,
3991 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003992 TEST_ASSERT( function_output_length <=
3993 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3994 alg,
3995 input->len - first_part_size ) );
3996 TEST_ASSERT( function_output_length <=
3997 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003998 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003999
Gilles Peskine8817f612018-12-18 00:18:46 +01004000 PSA_ASSERT( psa_cipher_finish( &operation1,
4001 output1 + output1_length,
4002 output1_buffer_size - output1_length,
4003 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004004 TEST_ASSERT( function_output_length <=
4005 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4006 TEST_ASSERT( function_output_length <=
4007 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004008 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004009
Gilles Peskine8817f612018-12-18 00:18:46 +01004010 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004011
Gilles Peskine048b7f02018-06-08 14:20:49 +02004012 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004013 TEST_ASSERT( output2_buffer_size <=
4014 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4015 TEST_ASSERT( output2_buffer_size <=
4016 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004017 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004018
Steven Cooreman177deba2020-09-07 17:14:14 +02004019 if( iv_length > 0 )
4020 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004021 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4022 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004023 }
Moran Pekerded84402018-06-06 16:36:50 +03004024
Gilles Peskine8817f612018-12-18 00:18:46 +01004025 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4026 output2, output2_buffer_size,
4027 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004028 TEST_ASSERT( function_output_length <=
4029 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4030 TEST_ASSERT( function_output_length <=
4031 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004032 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004033
Gilles Peskine8817f612018-12-18 00:18:46 +01004034 PSA_ASSERT( psa_cipher_update( &operation2,
4035 output1 + first_part_size,
4036 output1_length - first_part_size,
4037 output2, output2_buffer_size,
4038 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004039 TEST_ASSERT( function_output_length <=
4040 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4041 alg,
4042 output1_length - first_part_size ) );
4043 TEST_ASSERT( function_output_length <=
4044 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004045 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004046
Gilles Peskine8817f612018-12-18 00:18:46 +01004047 PSA_ASSERT( psa_cipher_finish( &operation2,
4048 output2 + output2_length,
4049 output2_buffer_size - output2_length,
4050 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004051 TEST_ASSERT( function_output_length <=
4052 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4053 TEST_ASSERT( function_output_length <=
4054 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004055 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004056
Gilles Peskine8817f612018-12-18 00:18:46 +01004057 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004058
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004059 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004060
4061exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004062 psa_cipher_abort( &operation1 );
4063 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004064 mbedtls_free( output1 );
4065 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004066 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004067 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004068}
4069/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004070
Gilles Peskine20035e32018-02-03 22:44:14 +01004071/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004072void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004073 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004074 data_t *nonce,
4075 data_t *additional_data,
4076 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004077 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004078{
Ronald Cron5425a212020-08-04 14:58:35 +02004079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004080 psa_key_type_t key_type = key_type_arg;
4081 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004082 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004083 unsigned char *output_data = NULL;
4084 size_t output_size = 0;
4085 size_t output_length = 0;
4086 unsigned char *output_data2 = NULL;
4087 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004088 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004089 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004091
Gilles Peskine8817f612018-12-18 00:18:46 +01004092 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004093
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004094 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4095 psa_set_key_algorithm( &attributes, alg );
4096 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004097
Gilles Peskine049c7532019-05-15 20:22:09 +02004098 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004099 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004100 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4101 key_bits = psa_get_key_bits( &attributes );
4102
4103 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4104 alg );
4105 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4106 * should be exact. */
4107 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4108 expected_result != PSA_ERROR_NOT_SUPPORTED )
4109 {
4110 TEST_EQUAL( output_size,
4111 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4112 TEST_ASSERT( output_size <=
4113 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4114 }
4115 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004116
Steven Cooremanf49478b2021-02-15 15:19:25 +01004117 status = psa_aead_encrypt( key, alg,
4118 nonce->x, nonce->len,
4119 additional_data->x,
4120 additional_data->len,
4121 input_data->x, input_data->len,
4122 output_data, output_size,
4123 &output_length );
4124
4125 /* If the operation is not supported, just skip and not fail in case the
4126 * encryption involves a common limitation of cryptography hardwares and
4127 * an alternative implementation. */
4128 if( status == PSA_ERROR_NOT_SUPPORTED )
4129 {
4130 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4131 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4132 }
4133
4134 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004135
4136 if( PSA_SUCCESS == expected_result )
4137 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004138 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004139
Gilles Peskine003a4a92019-05-14 16:09:40 +02004140 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4141 * should be exact. */
4142 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004143 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004144
gabor-mezei-armceface22021-01-21 12:26:17 +01004145 TEST_ASSERT( input_data->len <=
4146 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
4147
Ronald Cron5425a212020-08-04 14:58:35 +02004148 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004149 nonce->x, nonce->len,
4150 additional_data->x,
4151 additional_data->len,
4152 output_data, output_length,
4153 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004154 &output_length2 ),
4155 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004156
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004157 ASSERT_COMPARE( input_data->x, input_data->len,
4158 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004159 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004160
Gilles Peskinea1cac842018-06-11 19:33:02 +02004161exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004162 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004163 mbedtls_free( output_data );
4164 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004165 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004166}
4167/* END_CASE */
4168
4169/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004170void aead_encrypt( int key_type_arg, data_t *key_data,
4171 int alg_arg,
4172 data_t *nonce,
4173 data_t *additional_data,
4174 data_t *input_data,
4175 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004176{
Ronald Cron5425a212020-08-04 14:58:35 +02004177 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004178 psa_key_type_t key_type = key_type_arg;
4179 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004180 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004181 unsigned char *output_data = NULL;
4182 size_t output_size = 0;
4183 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004185 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004186
Gilles Peskine8817f612018-12-18 00:18:46 +01004187 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004188
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004189 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4190 psa_set_key_algorithm( &attributes, alg );
4191 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004192
Gilles Peskine049c7532019-05-15 20:22:09 +02004193 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004194 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004195 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4196 key_bits = psa_get_key_bits( &attributes );
4197
4198 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4199 alg );
4200 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4201 * should be exact. */
4202 TEST_EQUAL( output_size,
4203 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4204 TEST_ASSERT( output_size <=
4205 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4206 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004207
Steven Cooremand588ea12021-01-11 19:36:04 +01004208 status = psa_aead_encrypt( key, alg,
4209 nonce->x, nonce->len,
4210 additional_data->x, additional_data->len,
4211 input_data->x, input_data->len,
4212 output_data, output_size,
4213 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004214
Ronald Cron28a45ed2021-02-09 20:35:42 +01004215 /* If the operation is not supported, just skip and not fail in case the
4216 * encryption involves a common limitation of cryptography hardwares and
4217 * an alternative implementation. */
4218 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004219 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004220 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4221 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004222 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004223
4224 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004225 ASSERT_COMPARE( expected_result->x, expected_result->len,
4226 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004227
Gilles Peskinea1cac842018-06-11 19:33:02 +02004228exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004229 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004230 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004231 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004232}
4233/* END_CASE */
4234
4235/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004236void aead_decrypt( int key_type_arg, data_t *key_data,
4237 int alg_arg,
4238 data_t *nonce,
4239 data_t *additional_data,
4240 data_t *input_data,
4241 data_t *expected_data,
4242 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004243{
Ronald Cron5425a212020-08-04 14:58:35 +02004244 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004245 psa_key_type_t key_type = key_type_arg;
4246 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004247 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004248 unsigned char *output_data = NULL;
4249 size_t output_size = 0;
4250 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004252 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004253 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004254
Gilles Peskine8817f612018-12-18 00:18:46 +01004255 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004256
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4258 psa_set_key_algorithm( &attributes, alg );
4259 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004260
Gilles Peskine049c7532019-05-15 20:22:09 +02004261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004262 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004263 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4264 key_bits = psa_get_key_bits( &attributes );
4265
4266 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4267 alg );
4268 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4269 expected_result != PSA_ERROR_NOT_SUPPORTED )
4270 {
4271 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4272 * should be exact. */
4273 TEST_EQUAL( output_size,
4274 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4275 TEST_ASSERT( output_size <=
4276 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4277 }
4278 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004279
Steven Cooremand588ea12021-01-11 19:36:04 +01004280 status = psa_aead_decrypt( key, alg,
4281 nonce->x, nonce->len,
4282 additional_data->x,
4283 additional_data->len,
4284 input_data->x, input_data->len,
4285 output_data, output_size,
4286 &output_length );
4287
Ronald Cron28a45ed2021-02-09 20:35:42 +01004288 /* If the operation is not supported, just skip and not fail in case the
4289 * decryption involves a common limitation of cryptography hardwares and
4290 * an alternative implementation. */
4291 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004292 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004293 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4294 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004295 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004296
4297 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004298
Gilles Peskine2d277862018-06-18 15:41:12 +02004299 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004300 ASSERT_COMPARE( expected_data->x, expected_data->len,
4301 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004302
Gilles Peskinea1cac842018-06-11 19:33:02 +02004303exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004304 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004305 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004306 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004307}
4308/* END_CASE */
4309
4310/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004311void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4312 int alg_arg,
4313 data_t *nonce,
4314 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004315 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004316 int do_set_lengths,
4317 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004318{
Paul Elliottd3f82412021-06-16 16:52:21 +01004319 size_t ad_part_len = 0;
4320 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004321 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004322
Paul Elliott32f46ba2021-09-23 18:24:36 +01004323 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004324 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004325 mbedtls_test_set_step( ad_part_len );
4326
4327 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004328 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004329 if( ad_part_len & 0x01 )
4330 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4331 else
4332 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004333 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004334
4335 /* Split ad into length(ad_part_len) parts. */
4336 if( !aead_multipart_internal_func( key_type_arg, key_data,
4337 alg_arg, nonce,
4338 additional_data,
4339 ad_part_len,
4340 input_data, -1,
4341 set_lengths_method,
4342 expected_output,
4343 1, 0 ) )
4344 break;
4345
4346 /* length(0) part, length(ad_part_len) part, length(0) part... */
4347 mbedtls_test_set_step( 1000 + ad_part_len );
4348
4349 if( !aead_multipart_internal_func( key_type_arg, key_data,
4350 alg_arg, nonce,
4351 additional_data,
4352 ad_part_len,
4353 input_data, -1,
4354 set_lengths_method,
4355 expected_output,
4356 1, 1 ) )
4357 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004358 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004359
Paul Elliott32f46ba2021-09-23 18:24:36 +01004360 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004361 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004362 /* Split data into length(data_part_len) parts. */
4363 mbedtls_test_set_step( 2000 + data_part_len );
4364
4365 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004366 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004367 if( data_part_len & 0x01 )
4368 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4369 else
4370 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004371 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004372
Paul Elliott32f46ba2021-09-23 18:24:36 +01004373 if( !aead_multipart_internal_func( key_type_arg, key_data,
4374 alg_arg, nonce,
4375 additional_data, -1,
4376 input_data, data_part_len,
4377 set_lengths_method,
4378 expected_output,
4379 1, 0 ) )
4380 break;
4381
4382 /* length(0) part, length(data_part_len) part, length(0) part... */
4383 mbedtls_test_set_step( 3000 + data_part_len );
4384
4385 if( !aead_multipart_internal_func( key_type_arg, key_data,
4386 alg_arg, nonce,
4387 additional_data, -1,
4388 input_data, data_part_len,
4389 set_lengths_method,
4390 expected_output,
4391 1, 1 ) )
4392 break;
4393 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004394
Paul Elliott8fc45162021-06-23 16:06:01 +01004395 /* Goto is required to silence warnings about unused labels, as we
4396 * don't actually do any test assertions in this function. */
4397 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004398}
4399/* END_CASE */
4400
4401/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004402void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4403 int alg_arg,
4404 data_t *nonce,
4405 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004406 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004407 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004408 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004409{
Paul Elliottd3f82412021-06-16 16:52:21 +01004410 size_t ad_part_len = 0;
4411 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004412 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004413
Paul Elliott32f46ba2021-09-23 18:24:36 +01004414 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004415 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004416 /* Split ad into length(ad_part_len) parts. */
4417 mbedtls_test_set_step( ad_part_len );
4418
4419 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004420 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004421 if( ad_part_len & 0x01 )
4422 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4423 else
4424 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004425 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004426
4427 if( !aead_multipart_internal_func( key_type_arg, key_data,
4428 alg_arg, nonce,
4429 additional_data,
4430 ad_part_len,
4431 input_data, -1,
4432 set_lengths_method,
4433 expected_output,
4434 0, 0 ) )
4435 break;
4436
4437 /* length(0) part, length(ad_part_len) part, length(0) part... */
4438 mbedtls_test_set_step( 1000 + ad_part_len );
4439
4440 if( !aead_multipart_internal_func( key_type_arg, key_data,
4441 alg_arg, nonce,
4442 additional_data,
4443 ad_part_len,
4444 input_data, -1,
4445 set_lengths_method,
4446 expected_output,
4447 0, 1 ) )
4448 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004449 }
4450
Paul Elliott32f46ba2021-09-23 18:24:36 +01004451 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004452 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004453 /* Split data into length(data_part_len) parts. */
4454 mbedtls_test_set_step( 2000 + data_part_len );
4455
4456 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004457 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004458 if( data_part_len & 0x01 )
4459 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4460 else
4461 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004462 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004463
4464 if( !aead_multipart_internal_func( key_type_arg, key_data,
4465 alg_arg, nonce,
4466 additional_data, -1,
4467 input_data, data_part_len,
4468 set_lengths_method,
4469 expected_output,
4470 0, 0 ) )
4471 break;
4472
4473 /* length(0) part, length(data_part_len) part, length(0) part... */
4474 mbedtls_test_set_step( 3000 + data_part_len );
4475
4476 if( !aead_multipart_internal_func( key_type_arg, key_data,
4477 alg_arg, nonce,
4478 additional_data, -1,
4479 input_data, data_part_len,
4480 set_lengths_method,
4481 expected_output,
4482 0, 1 ) )
4483 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004484 }
4485
Paul Elliott8fc45162021-06-23 16:06:01 +01004486 /* Goto is required to silence warnings about unused labels, as we
4487 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004488 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004489}
4490/* END_CASE */
4491
4492/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004493void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4494 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004495 int nonce_length,
4496 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004497 data_t *additional_data,
4498 data_t *input_data,
4499 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004500{
4501
4502 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4503 psa_key_type_t key_type = key_type_arg;
4504 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004505 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004506 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4508 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004509 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004510 size_t actual_nonce_length = 0;
4511 size_t expected_nonce_length = expected_nonce_length_arg;
4512 unsigned char *output = NULL;
4513 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004514 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004515 size_t ciphertext_size = 0;
4516 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004517 size_t tag_length = 0;
4518 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004519
4520 PSA_ASSERT( psa_crypto_init( ) );
4521
4522 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4523 psa_set_key_algorithm( & attributes, alg );
4524 psa_set_key_type( & attributes, key_type );
4525
4526 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4527 &key ) );
4528
4529 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4530
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004531 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4532
Paul Elliottf1277632021-08-24 18:11:37 +01004533 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004534
Paul Elliottf1277632021-08-24 18:11:37 +01004535 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004536
Paul Elliottf1277632021-08-24 18:11:37 +01004537 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004538
Paul Elliottf1277632021-08-24 18:11:37 +01004539 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004540
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004541 status = psa_aead_encrypt_setup( &operation, key, alg );
4542
4543 /* If the operation is not supported, just skip and not fail in case the
4544 * encryption involves a common limitation of cryptography hardwares and
4545 * an alternative implementation. */
4546 if( status == PSA_ERROR_NOT_SUPPORTED )
4547 {
4548 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004549 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004550 }
4551
4552 PSA_ASSERT( status );
4553
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004554 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004555 nonce_length,
4556 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004557
Paul Elliott693bf312021-07-23 17:40:41 +01004558 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004559
Paul Elliottf1277632021-08-24 18:11:37 +01004560 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004561
Paul Elliott88ecbe12021-09-22 17:23:03 +01004562 if( expected_status == PSA_SUCCESS )
4563 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4564 alg ) );
4565
Paul Elliottd79c5c52021-10-06 21:49:41 +01004566 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004567
Paul Elliott693bf312021-07-23 17:40:41 +01004568 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004569 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004570 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004571 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4572 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004573
4574 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4575 additional_data->len ) );
4576
4577 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004578 output, output_size,
4579 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004580
Paul Elliottf1277632021-08-24 18:11:37 +01004581 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4582 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004583 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4584 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004585
4586exit:
4587 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004588 mbedtls_free( output );
4589 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004590 psa_aead_abort( &operation );
4591 PSA_DONE( );
4592}
4593/* END_CASE */
4594
4595/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004596void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4597 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004598 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004599 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004600 data_t *additional_data,
4601 data_t *input_data,
4602 int expected_status_arg )
4603{
4604
4605 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4606 psa_key_type_t key_type = key_type_arg;
4607 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004608 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004609 uint8_t *nonce_buffer = NULL;
4610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4611 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4612 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004613 unsigned char *output = NULL;
4614 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004615 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004616 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004617 size_t ciphertext_size = 0;
4618 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004619 size_t tag_length = 0;
4620 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004621 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004622 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004623
4624 PSA_ASSERT( psa_crypto_init( ) );
4625
4626 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4627 psa_set_key_algorithm( &attributes, alg );
4628 psa_set_key_type( &attributes, key_type );
4629
4630 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4631 &key ) );
4632
4633 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4634
4635 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4636
Paul Elliott6f0e7202021-08-25 12:57:18 +01004637 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004638
Paul Elliott6f0e7202021-08-25 12:57:18 +01004639 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004640
Paul Elliott6f0e7202021-08-25 12:57:18 +01004641 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004642
Paul Elliott6f0e7202021-08-25 12:57:18 +01004643 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004644
Paul Elliott863864a2021-07-23 17:28:31 +01004645 status = psa_aead_encrypt_setup( &operation, key, alg );
4646
4647 /* If the operation is not supported, just skip and not fail in case the
4648 * encryption involves a common limitation of cryptography hardwares and
4649 * an alternative implementation. */
4650 if( status == PSA_ERROR_NOT_SUPPORTED )
4651 {
4652 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004653 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004654 }
4655
4656 PSA_ASSERT( status );
4657
Paul Elliott4023ffd2021-09-10 16:21:22 +01004658 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4659 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004660 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004661 /* Arbitrary size buffer, to test zero length valid buffer. */
4662 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004663 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004664 }
4665 else
4666 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004667 /* If length is zero, then this will return NULL. */
4668 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004669 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004670
Paul Elliott4023ffd2021-09-10 16:21:22 +01004671 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004672 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004673 for( index = 0; index < nonce_length - 1; ++index )
4674 {
4675 nonce_buffer[index] = 'a' + index;
4676 }
Paul Elliott66696b52021-08-16 18:42:41 +01004677 }
Paul Elliott863864a2021-07-23 17:28:31 +01004678 }
4679
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004680 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4681 {
4682 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4683 input_data->len ) );
4684 }
4685
Paul Elliott6f0e7202021-08-25 12:57:18 +01004686 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004687
Paul Elliott693bf312021-07-23 17:40:41 +01004688 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004689
4690 if( expected_status == PSA_SUCCESS )
4691 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004692 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4693 {
4694 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4695 input_data->len ) );
4696 }
4697 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4698 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004699
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004700 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4701 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4702 additional_data->len ),
4703 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004704
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004705 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004706 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004707 &ciphertext_length ),
4708 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004709
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004710 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004711 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004712 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4713 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004714 }
4715
4716exit:
4717 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004718 mbedtls_free( output );
4719 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004720 mbedtls_free( nonce_buffer );
4721 psa_aead_abort( &operation );
4722 PSA_DONE( );
4723}
4724/* END_CASE */
4725
4726/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004727void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4728 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004729 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004730 data_t *nonce,
4731 data_t *additional_data,
4732 data_t *input_data,
4733 int expected_status_arg )
4734{
4735
4736 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4737 psa_key_type_t key_type = key_type_arg;
4738 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004739 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004740 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4741 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4742 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004743 unsigned char *output = NULL;
4744 unsigned char *ciphertext = NULL;
4745 size_t output_size = output_size_arg;
4746 size_t ciphertext_size = 0;
4747 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004748 size_t tag_length = 0;
4749 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4750
4751 PSA_ASSERT( psa_crypto_init( ) );
4752
4753 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4754 psa_set_key_algorithm( &attributes, alg );
4755 psa_set_key_type( &attributes, key_type );
4756
4757 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4758 &key ) );
4759
4760 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4761
Paul Elliottc6d11d02021-09-01 12:04:23 +01004762 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004763
Paul Elliottc6d11d02021-09-01 12:04:23 +01004764 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004765
Paul Elliottc6d11d02021-09-01 12:04:23 +01004766 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004767
Paul Elliott43fbda62021-07-23 18:30:59 +01004768 status = psa_aead_encrypt_setup( &operation, key, alg );
4769
4770 /* If the operation is not supported, just skip and not fail in case the
4771 * encryption involves a common limitation of cryptography hardwares and
4772 * an alternative implementation. */
4773 if( status == PSA_ERROR_NOT_SUPPORTED )
4774 {
4775 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4776 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4777 }
4778
4779 PSA_ASSERT( status );
4780
Paul Elliott47b9a142021-10-07 15:04:57 +01004781 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4782 input_data->len ) );
4783
Paul Elliott43fbda62021-07-23 18:30:59 +01004784 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4785
4786 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4787 additional_data->len ) );
4788
4789 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004790 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004791
4792 TEST_EQUAL( status, expected_status );
4793
4794 if( expected_status == PSA_SUCCESS )
4795 {
4796 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004797 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4798 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004799 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4800 }
4801
4802exit:
4803 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004804 mbedtls_free( output );
4805 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004806 psa_aead_abort( &operation );
4807 PSA_DONE( );
4808}
4809/* END_CASE */
4810
Paul Elliott91b021e2021-07-23 18:52:31 +01004811/* BEGIN_CASE */
4812void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4813 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004814 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004815 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004816 data_t *nonce,
4817 data_t *additional_data,
4818 data_t *input_data,
4819 int expected_status_arg )
4820{
4821
4822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4823 psa_key_type_t key_type = key_type_arg;
4824 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004825 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004826 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4827 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4828 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004829 unsigned char *ciphertext = NULL;
4830 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004831 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004832 size_t ciphertext_size = 0;
4833 size_t ciphertext_length = 0;
4834 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004835 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004836 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004837
4838 PSA_ASSERT( psa_crypto_init( ) );
4839
4840 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4841 psa_set_key_algorithm( &attributes, alg );
4842 psa_set_key_type( &attributes, key_type );
4843
4844 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4845 &key ) );
4846
4847 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4848
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004849 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004850
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004851 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004852
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004853 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004854
Paul Elliott719c1322021-09-13 18:27:22 +01004855 ASSERT_ALLOC( tag_buffer, tag_size );
4856
Paul Elliott91b021e2021-07-23 18:52:31 +01004857 status = psa_aead_encrypt_setup( &operation, key, alg );
4858
4859 /* If the operation is not supported, just skip and not fail in case the
4860 * encryption involves a common limitation of cryptography hardwares and
4861 * an alternative implementation. */
4862 if( status == PSA_ERROR_NOT_SUPPORTED )
4863 {
4864 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4865 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4866 }
4867
4868 PSA_ASSERT( status );
4869
4870 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4871
Paul Elliott76bda482021-10-07 17:07:23 +01004872 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4873 input_data->len ) );
4874
Paul Elliott91b021e2021-07-23 18:52:31 +01004875 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4876 additional_data->len ) );
4877
4878 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004879 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004880
4881 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004882 status = psa_aead_finish( &operation, finish_ciphertext,
4883 finish_ciphertext_size,
4884 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004885 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004886
4887 TEST_EQUAL( status, expected_status );
4888
4889exit:
4890 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004891 mbedtls_free( ciphertext );
4892 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004893 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004894 psa_aead_abort( &operation );
4895 PSA_DONE( );
4896}
4897/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004898
4899/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004900void aead_multipart_verify( int key_type_arg, data_t *key_data,
4901 int alg_arg,
4902 data_t *nonce,
4903 data_t *additional_data,
4904 data_t *input_data,
4905 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004906 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004907 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004908 int expected_status_arg )
4909{
4910 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4911 psa_key_type_t key_type = key_type_arg;
4912 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004913 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4915 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4916 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004917 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004918 unsigned char *plaintext = NULL;
4919 unsigned char *finish_plaintext = NULL;
4920 size_t plaintext_size = 0;
4921 size_t plaintext_length = 0;
4922 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004923 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004924 unsigned char *tag_buffer = NULL;
4925 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004926
4927 PSA_ASSERT( psa_crypto_init( ) );
4928
4929 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4930 psa_set_key_algorithm( &attributes, alg );
4931 psa_set_key_type( &attributes, key_type );
4932
4933 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4934 &key ) );
4935
4936 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4937
4938 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4939 input_data->len );
4940
4941 ASSERT_ALLOC( plaintext, plaintext_size );
4942
4943 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4944
4945 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4946
Paul Elliott9961a662021-09-17 19:19:02 +01004947 status = psa_aead_decrypt_setup( &operation, key, alg );
4948
4949 /* If the operation is not supported, just skip and not fail in case the
4950 * encryption involves a common limitation of cryptography hardwares and
4951 * an alternative implementation. */
4952 if( status == PSA_ERROR_NOT_SUPPORTED )
4953 {
4954 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4955 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4956 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004957 TEST_EQUAL( status, expected_setup_status );
4958
4959 if( status != PSA_SUCCESS )
4960 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004961
4962 PSA_ASSERT( status );
4963
4964 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4965
Paul Elliottfec6f372021-10-06 17:15:02 +01004966 status = psa_aead_set_lengths( &operation, additional_data->len,
4967 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004968 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004969
Paul Elliott9961a662021-09-17 19:19:02 +01004970 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4971 additional_data->len ) );
4972
4973 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4974 input_data->len,
4975 plaintext, plaintext_size,
4976 &plaintext_length ) );
4977
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004978 if( tag_usage == USE_GIVEN_TAG )
4979 {
4980 tag_buffer = tag->x;
4981 tag_size = tag->len;
4982 }
4983
Paul Elliott9961a662021-09-17 19:19:02 +01004984 status = psa_aead_verify( &operation, finish_plaintext,
4985 verify_plaintext_size,
4986 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004987 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004988
4989 TEST_EQUAL( status, expected_status );
4990
4991exit:
4992 psa_destroy_key( key );
4993 mbedtls_free( plaintext );
4994 mbedtls_free( finish_plaintext );
4995 psa_aead_abort( &operation );
4996 PSA_DONE( );
4997}
4998/* END_CASE */
4999
Paul Elliott9961a662021-09-17 19:19:02 +01005000/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005001void aead_multipart_setup( int key_type_arg, data_t *key_data,
5002 int alg_arg, int expected_status_arg )
5003{
5004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5005 psa_key_type_t key_type = key_type_arg;
5006 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005007 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005008 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5009 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5010 psa_status_t expected_status = expected_status_arg;
5011
5012 PSA_ASSERT( psa_crypto_init( ) );
5013
5014 psa_set_key_usage_flags( &attributes,
5015 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5016 psa_set_key_algorithm( &attributes, alg );
5017 psa_set_key_type( &attributes, key_type );
5018
5019 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5020 &key ) );
5021
Paul Elliott5221ef62021-09-19 17:33:03 +01005022 status = psa_aead_encrypt_setup( &operation, key, alg );
5023
5024 TEST_EQUAL( status, expected_status );
5025
5026 psa_aead_abort( &operation );
5027
Paul Elliott5221ef62021-09-19 17:33:03 +01005028 status = psa_aead_decrypt_setup( &operation, key, alg );
5029
5030 TEST_EQUAL(status, expected_status );
5031
5032exit:
5033 psa_destroy_key( key );
5034 psa_aead_abort( &operation );
5035 PSA_DONE( );
5036}
5037/* END_CASE */
5038
5039/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005040void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5041 int alg_arg,
5042 data_t *nonce,
5043 data_t *additional_data,
5044 data_t *input_data )
5045{
5046 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5047 psa_key_type_t key_type = key_type_arg;
5048 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005049 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005050 unsigned char *output_data = NULL;
5051 unsigned char *final_data = NULL;
5052 size_t output_size = 0;
5053 size_t finish_output_size = 0;
5054 size_t output_length = 0;
5055 size_t key_bits = 0;
5056 size_t tag_length = 0;
5057 size_t tag_size = 0;
5058 size_t nonce_length = 0;
5059 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5060 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5061 size_t output_part_length = 0;
5062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5063
5064 PSA_ASSERT( psa_crypto_init( ) );
5065
5066 psa_set_key_usage_flags( & attributes,
5067 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5068 psa_set_key_algorithm( & attributes, alg );
5069 psa_set_key_type( & attributes, key_type );
5070
5071 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5072 &key ) );
5073
5074 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5075 key_bits = psa_get_key_bits( &attributes );
5076
5077 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5078
5079 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
5080
5081 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5082
5083 ASSERT_ALLOC( output_data, output_size );
5084
5085 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5086
5087 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
5088
5089 ASSERT_ALLOC( final_data, finish_output_size );
5090
5091 /* Test all operations error without calling setup first. */
5092
Paul Elliottc23a9a02021-06-21 18:32:46 +01005093 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5094 PSA_ERROR_BAD_STATE );
5095
5096 psa_aead_abort( &operation );
5097
Paul Elliottc23a9a02021-06-21 18:32:46 +01005098 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5099 PSA_AEAD_NONCE_MAX_SIZE,
5100 &nonce_length ),
5101 PSA_ERROR_BAD_STATE );
5102
5103 psa_aead_abort( &operation );
5104
Paul Elliott481be342021-07-16 17:38:47 +01005105 /* ------------------------------------------------------- */
5106
Paul Elliottc23a9a02021-06-21 18:32:46 +01005107 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5108 input_data->len ),
5109 PSA_ERROR_BAD_STATE );
5110
5111 psa_aead_abort( &operation );
5112
Paul Elliott481be342021-07-16 17:38:47 +01005113 /* ------------------------------------------------------- */
5114
Paul Elliottc23a9a02021-06-21 18:32:46 +01005115 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5116 additional_data->len ),
5117 PSA_ERROR_BAD_STATE );
5118
5119 psa_aead_abort( &operation );
5120
Paul Elliott481be342021-07-16 17:38:47 +01005121 /* ------------------------------------------------------- */
5122
Paul Elliottc23a9a02021-06-21 18:32:46 +01005123 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5124 input_data->len, output_data,
5125 output_size, &output_length ),
5126 PSA_ERROR_BAD_STATE );
5127
5128 psa_aead_abort( &operation );
5129
Paul Elliott481be342021-07-16 17:38:47 +01005130 /* ------------------------------------------------------- */
5131
Paul Elliottc23a9a02021-06-21 18:32:46 +01005132 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5133 finish_output_size,
5134 &output_part_length,
5135 tag_buffer, tag_length,
5136 &tag_size ),
5137 PSA_ERROR_BAD_STATE );
5138
5139 psa_aead_abort( &operation );
5140
Paul Elliott481be342021-07-16 17:38:47 +01005141 /* ------------------------------------------------------- */
5142
Paul Elliottc23a9a02021-06-21 18:32:46 +01005143 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5144 finish_output_size,
5145 &output_part_length,
5146 tag_buffer,
5147 tag_length ),
5148 PSA_ERROR_BAD_STATE );
5149
5150 psa_aead_abort( &operation );
5151
5152 /* Test for double setups. */
5153
Paul Elliottc23a9a02021-06-21 18:32:46 +01005154 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5155
5156 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5157 PSA_ERROR_BAD_STATE );
5158
5159 psa_aead_abort( &operation );
5160
Paul Elliott481be342021-07-16 17:38:47 +01005161 /* ------------------------------------------------------- */
5162
Paul Elliottc23a9a02021-06-21 18:32:46 +01005163 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5164
5165 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5166 PSA_ERROR_BAD_STATE );
5167
5168 psa_aead_abort( &operation );
5169
Paul Elliott374a2be2021-07-16 17:53:40 +01005170 /* ------------------------------------------------------- */
5171
Paul Elliott374a2be2021-07-16 17:53:40 +01005172 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5173
5174 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5175 PSA_ERROR_BAD_STATE );
5176
5177 psa_aead_abort( &operation );
5178
5179 /* ------------------------------------------------------- */
5180
Paul Elliott374a2be2021-07-16 17:53:40 +01005181 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5182
5183 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5184 PSA_ERROR_BAD_STATE );
5185
5186 psa_aead_abort( &operation );
5187
Paul Elliottc23a9a02021-06-21 18:32:46 +01005188 /* Test for not setting a nonce. */
5189
Paul Elliottc23a9a02021-06-21 18:32:46 +01005190 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5191
5192 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5193 additional_data->len ),
5194 PSA_ERROR_BAD_STATE );
5195
5196 psa_aead_abort( &operation );
5197
Paul Elliott7f628422021-09-01 12:08:29 +01005198 /* ------------------------------------------------------- */
5199
Paul Elliott7f628422021-09-01 12:08:29 +01005200 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5201
5202 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5203 input_data->len, output_data,
5204 output_size, &output_length ),
5205 PSA_ERROR_BAD_STATE );
5206
5207 psa_aead_abort( &operation );
5208
Paul Elliottbdc2c682021-09-21 18:37:10 +01005209 /* ------------------------------------------------------- */
5210
Paul Elliottbdc2c682021-09-21 18:37:10 +01005211 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5212
5213 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5214 finish_output_size,
5215 &output_part_length,
5216 tag_buffer, tag_length,
5217 &tag_size ),
5218 PSA_ERROR_BAD_STATE );
5219
5220 psa_aead_abort( &operation );
5221
5222 /* ------------------------------------------------------- */
5223
Paul Elliottbdc2c682021-09-21 18:37:10 +01005224 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5225
5226 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5227 finish_output_size,
5228 &output_part_length,
5229 tag_buffer,
5230 tag_length ),
5231 PSA_ERROR_BAD_STATE );
5232
5233 psa_aead_abort( &operation );
5234
Paul Elliottc23a9a02021-06-21 18:32:46 +01005235 /* Test for double setting nonce. */
5236
Paul Elliottc23a9a02021-06-21 18:32:46 +01005237 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5238
5239 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5240
5241 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5242 PSA_ERROR_BAD_STATE );
5243
5244 psa_aead_abort( &operation );
5245
Paul Elliott374a2be2021-07-16 17:53:40 +01005246 /* Test for double generating nonce. */
5247
Paul Elliott374a2be2021-07-16 17:53:40 +01005248 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5249
5250 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5251 PSA_AEAD_NONCE_MAX_SIZE,
5252 &nonce_length ) );
5253
5254 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5255 PSA_AEAD_NONCE_MAX_SIZE,
5256 &nonce_length ),
5257 PSA_ERROR_BAD_STATE );
5258
5259
5260 psa_aead_abort( &operation );
5261
5262 /* Test for generate nonce then set and vice versa */
5263
Paul Elliott374a2be2021-07-16 17:53:40 +01005264 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5265
5266 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5267 PSA_AEAD_NONCE_MAX_SIZE,
5268 &nonce_length ) );
5269
5270 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5271 PSA_ERROR_BAD_STATE );
5272
5273 psa_aead_abort( &operation );
5274
Andrzej Kurekad837522021-12-15 15:28:49 +01005275 /* Test for generating nonce after calling set lengths */
5276
5277 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5278
5279 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5280 input_data->len ) );
5281
5282 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5283 PSA_AEAD_NONCE_MAX_SIZE,
5284 &nonce_length ) );
5285
5286 psa_aead_abort( &operation );
5287
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005288 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005289
5290 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5291
5292 if( operation.alg == PSA_ALG_CCM )
5293 {
5294 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5295 input_data->len ),
5296 PSA_ERROR_INVALID_ARGUMENT );
5297 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5298 PSA_AEAD_NONCE_MAX_SIZE,
5299 &nonce_length ),
5300 PSA_ERROR_BAD_STATE );
5301 }
5302 else
5303 {
5304 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5305 input_data->len ) );
5306 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5307 PSA_AEAD_NONCE_MAX_SIZE,
5308 &nonce_length ) );
5309 }
5310
5311 psa_aead_abort( &operation );
5312
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005313 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005314#if SIZE_MAX > UINT32_MAX
5315 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5316
5317 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5318 {
5319 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5320 input_data->len ),
5321 PSA_ERROR_INVALID_ARGUMENT );
5322 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5323 PSA_AEAD_NONCE_MAX_SIZE,
5324 &nonce_length ),
5325 PSA_ERROR_BAD_STATE );
5326 }
5327 else
5328 {
5329 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5330 input_data->len ) );
5331 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5332 PSA_AEAD_NONCE_MAX_SIZE,
5333 &nonce_length ) );
5334 }
5335
5336 psa_aead_abort( &operation );
5337#endif
5338
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005339 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005340
5341 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5342
5343 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5344 PSA_AEAD_NONCE_MAX_SIZE,
5345 &nonce_length ) );
5346
5347 if( operation.alg == PSA_ALG_CCM )
5348 {
5349 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5350 input_data->len ),
5351 PSA_ERROR_INVALID_ARGUMENT );
5352 }
5353 else
5354 {
5355 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5356 input_data->len ) );
5357 }
5358
5359 psa_aead_abort( &operation );
5360
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005361 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005362 /* Test for setting nonce after calling set lengths */
5363
5364 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5365
5366 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5367 input_data->len ) );
5368
5369 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5370
5371 psa_aead_abort( &operation );
5372
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005373 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005374
5375 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5376
5377 if( operation.alg == PSA_ALG_CCM )
5378 {
5379 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5380 input_data->len ),
5381 PSA_ERROR_INVALID_ARGUMENT );
5382 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5383 PSA_ERROR_BAD_STATE );
5384 }
5385 else
5386 {
5387 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5388 input_data->len ) );
5389 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5390 }
5391
5392 psa_aead_abort( &operation );
5393
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005394 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005395#if SIZE_MAX > UINT32_MAX
5396 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5397
5398 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5399 {
5400 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5401 input_data->len ),
5402 PSA_ERROR_INVALID_ARGUMENT );
5403 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5404 PSA_ERROR_BAD_STATE );
5405 }
5406 else
5407 {
5408 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5409 input_data->len ) );
5410 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5411 }
5412
5413 psa_aead_abort( &operation );
5414#endif
5415
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005416 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005417
5418 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5419
5420 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5421
5422 if( operation.alg == PSA_ALG_CCM )
5423 {
5424 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5425 input_data->len ),
5426 PSA_ERROR_INVALID_ARGUMENT );
5427 }
5428 else
5429 {
5430 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5431 input_data->len ) );
5432 }
5433
5434 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005435
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005436 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005437#if SIZE_MAX > UINT32_MAX
5438 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5439
5440 if( operation.alg == PSA_ALG_GCM )
5441 {
5442 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5443 SIZE_MAX ),
5444 PSA_ERROR_INVALID_ARGUMENT );
5445 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5446 PSA_ERROR_BAD_STATE );
5447 }
5448 else if ( operation.alg != PSA_ALG_CCM )
5449 {
5450 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5451 SIZE_MAX ) );
5452 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5453 }
5454
5455 psa_aead_abort( &operation );
5456
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005457 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005458 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5459
5460 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5461
5462 if( operation.alg == PSA_ALG_GCM )
5463 {
5464 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5465 SIZE_MAX ),
5466 PSA_ERROR_INVALID_ARGUMENT );
5467 }
5468 else if ( operation.alg != PSA_ALG_CCM )
5469 {
5470 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5471 SIZE_MAX ) );
5472 }
5473
5474 psa_aead_abort( &operation );
5475#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005476
5477 /* ------------------------------------------------------- */
5478
Paul Elliott374a2be2021-07-16 17:53:40 +01005479 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5480
5481 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5482
5483 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5484 PSA_AEAD_NONCE_MAX_SIZE,
5485 &nonce_length ),
5486 PSA_ERROR_BAD_STATE );
5487
5488 psa_aead_abort( &operation );
5489
Paul Elliott7220cae2021-06-22 17:25:57 +01005490 /* Test for generating nonce in decrypt setup. */
5491
Paul Elliott7220cae2021-06-22 17:25:57 +01005492 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5493
5494 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5495 PSA_AEAD_NONCE_MAX_SIZE,
5496 &nonce_length ),
5497 PSA_ERROR_BAD_STATE );
5498
5499 psa_aead_abort( &operation );
5500
Paul Elliottc23a9a02021-06-21 18:32:46 +01005501 /* Test for setting lengths twice. */
5502
Paul Elliottc23a9a02021-06-21 18:32:46 +01005503 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5504
5505 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5506
5507 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5508 input_data->len ) );
5509
5510 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5511 input_data->len ),
5512 PSA_ERROR_BAD_STATE );
5513
5514 psa_aead_abort( &operation );
5515
Andrzej Kurekad837522021-12-15 15:28:49 +01005516 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005517
Paul Elliottc23a9a02021-06-21 18:32:46 +01005518 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5519
5520 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5521
Andrzej Kurekad837522021-12-15 15:28:49 +01005522 if( operation.alg == PSA_ALG_CCM )
5523 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005524
Andrzej Kurekad837522021-12-15 15:28:49 +01005525 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5526 additional_data->len ),
5527 PSA_ERROR_BAD_STATE );
5528 }
5529 else
5530 {
5531 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5532 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005533
Andrzej Kurekad837522021-12-15 15:28:49 +01005534 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5535 input_data->len ),
5536 PSA_ERROR_BAD_STATE );
5537 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005538 psa_aead_abort( &operation );
5539
5540 /* ------------------------------------------------------- */
5541
Paul Elliottf94bd992021-09-19 18:15:59 +01005542 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5543
5544 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5545
Andrzej Kurekad837522021-12-15 15:28:49 +01005546 if( operation.alg == PSA_ALG_CCM )
5547 {
5548 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5549 input_data->len, output_data,
5550 output_size, &output_length ),
5551 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005552
Andrzej Kurekad837522021-12-15 15:28:49 +01005553 }
5554 else
5555 {
5556 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5557 input_data->len, output_data,
5558 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005559
Andrzej Kurekad837522021-12-15 15:28:49 +01005560 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5561 input_data->len ),
5562 PSA_ERROR_BAD_STATE );
5563 }
5564 psa_aead_abort( &operation );
5565
5566 /* ------------------------------------------------------- */
5567
5568 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5569
5570 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5571
5572 if( operation.alg == PSA_ALG_CCM )
5573 {
5574 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5575 finish_output_size,
5576 &output_part_length,
5577 tag_buffer, tag_length,
5578 &tag_size ) );
5579 }
5580 else
5581 {
5582 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5583 finish_output_size,
5584 &output_part_length,
5585 tag_buffer, tag_length,
5586 &tag_size ) );
5587
5588 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5589 input_data->len ),
5590 PSA_ERROR_BAD_STATE );
5591 }
5592 psa_aead_abort( &operation );
5593
5594 /* Test for setting lengths after generating nonce + already starting data. */
5595
5596 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5597
5598 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5599 PSA_AEAD_NONCE_MAX_SIZE,
5600 &nonce_length ) );
5601 if( operation.alg == PSA_ALG_CCM )
5602 {
5603
5604 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5605 additional_data->len ),
5606 PSA_ERROR_BAD_STATE );
5607 }
5608 else
5609 {
5610 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5611 additional_data->len ) );
5612
5613 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5614 input_data->len ),
5615 PSA_ERROR_BAD_STATE );
5616 }
5617 psa_aead_abort( &operation );
5618
5619 /* ------------------------------------------------------- */
5620
5621 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5622
5623 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5624 PSA_AEAD_NONCE_MAX_SIZE,
5625 &nonce_length ) );
5626 if( operation.alg == PSA_ALG_CCM )
5627 {
5628 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5629 input_data->len, output_data,
5630 output_size, &output_length ),
5631 PSA_ERROR_BAD_STATE );
5632
5633 }
5634 else
5635 {
5636 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5637 input_data->len, output_data,
5638 output_size, &output_length ) );
5639
5640 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5641 input_data->len ),
5642 PSA_ERROR_BAD_STATE );
5643 }
5644 psa_aead_abort( &operation );
5645
5646 /* ------------------------------------------------------- */
5647
5648 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5649
5650 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5651 PSA_AEAD_NONCE_MAX_SIZE,
5652 &nonce_length ) );
5653 if( operation.alg == PSA_ALG_CCM )
5654 {
5655 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5656 finish_output_size,
5657 &output_part_length,
5658 tag_buffer, tag_length,
5659 &tag_size ) );
5660 }
5661 else
5662 {
5663 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5664 finish_output_size,
5665 &output_part_length,
5666 tag_buffer, tag_length,
5667 &tag_size ) );
5668
5669 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5670 input_data->len ),
5671 PSA_ERROR_BAD_STATE );
5672 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005673 psa_aead_abort( &operation );
5674
Paul Elliott243080c2021-07-21 19:01:17 +01005675 /* Test for not sending any additional data or data after setting non zero
5676 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005677
Paul Elliottc23a9a02021-06-21 18:32:46 +01005678 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5679
5680 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5681
5682 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5683 input_data->len ) );
5684
5685 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5686 finish_output_size,
5687 &output_part_length,
5688 tag_buffer, tag_length,
5689 &tag_size ),
5690 PSA_ERROR_INVALID_ARGUMENT );
5691
5692 psa_aead_abort( &operation );
5693
Paul Elliott243080c2021-07-21 19:01:17 +01005694 /* Test for not sending any additional data or data after setting non-zero
5695 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005696
Paul Elliottc23a9a02021-06-21 18:32:46 +01005697 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5698
5699 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5700
5701 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5702 input_data->len ) );
5703
5704 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5705 finish_output_size,
5706 &output_part_length,
5707 tag_buffer,
5708 tag_length ),
5709 PSA_ERROR_INVALID_ARGUMENT );
5710
5711 psa_aead_abort( &operation );
5712
Paul Elliott243080c2021-07-21 19:01:17 +01005713 /* Test for not sending any additional data after setting a non-zero length
5714 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005715
Paul Elliottc23a9a02021-06-21 18:32:46 +01005716 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5717
5718 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5719
5720 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5721 input_data->len ) );
5722
5723 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5724 input_data->len, output_data,
5725 output_size, &output_length ),
5726 PSA_ERROR_INVALID_ARGUMENT );
5727
5728 psa_aead_abort( &operation );
5729
Paul Elliottf94bd992021-09-19 18:15:59 +01005730 /* Test for not sending any data after setting a non-zero length for it.*/
5731
Paul Elliottf94bd992021-09-19 18:15:59 +01005732 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5733
5734 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5735
5736 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5737 input_data->len ) );
5738
5739 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5740 additional_data->len ) );
5741
5742 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5743 finish_output_size,
5744 &output_part_length,
5745 tag_buffer, tag_length,
5746 &tag_size ),
5747 PSA_ERROR_INVALID_ARGUMENT );
5748
5749 psa_aead_abort( &operation );
5750
Paul Elliottb0450fe2021-09-01 15:06:26 +01005751 /* Test for sending too much additional data after setting lengths. */
5752
Paul Elliottb0450fe2021-09-01 15:06:26 +01005753 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5754
5755 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5756
5757 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5758
5759
5760 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5761 additional_data->len ),
5762 PSA_ERROR_INVALID_ARGUMENT );
5763
5764 psa_aead_abort( &operation );
5765
Paul Elliotta2a09b02021-09-22 14:56:40 +01005766 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005767
5768 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5769
5770 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5771
5772 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5773 input_data->len ) );
5774
5775 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5776 additional_data->len ) );
5777
5778 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5779 1 ),
5780 PSA_ERROR_INVALID_ARGUMENT );
5781
5782 psa_aead_abort( &operation );
5783
Paul Elliottb0450fe2021-09-01 15:06:26 +01005784 /* Test for sending too much data after setting lengths. */
5785
Paul Elliottb0450fe2021-09-01 15:06:26 +01005786 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5787
5788 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5789
5790 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5791
5792 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5793 input_data->len, output_data,
5794 output_size, &output_length ),
5795 PSA_ERROR_INVALID_ARGUMENT );
5796
5797 psa_aead_abort( &operation );
5798
Paul Elliotta2a09b02021-09-22 14:56:40 +01005799 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005800
5801 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5802
5803 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5804
5805 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5806 input_data->len ) );
5807
5808 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5809 additional_data->len ) );
5810
5811 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5812 input_data->len, output_data,
5813 output_size, &output_length ) );
5814
5815 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5816 1, output_data,
5817 output_size, &output_length ),
5818 PSA_ERROR_INVALID_ARGUMENT );
5819
5820 psa_aead_abort( &operation );
5821
Paul Elliottc23a9a02021-06-21 18:32:46 +01005822 /* Test sending additional data after data. */
5823
Paul Elliottc23a9a02021-06-21 18:32:46 +01005824 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5825
5826 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5827
Andrzej Kurekad837522021-12-15 15:28:49 +01005828 if( operation.alg != PSA_ALG_CCM )
5829 {
5830 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5831 input_data->len, output_data,
5832 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005833
Andrzej Kurekad837522021-12-15 15:28:49 +01005834 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5835 additional_data->len ),
5836 PSA_ERROR_BAD_STATE );
5837 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005838 psa_aead_abort( &operation );
5839
Paul Elliott534d0b42021-06-22 19:15:20 +01005840 /* Test calling finish on decryption. */
5841
Paul Elliott534d0b42021-06-22 19:15:20 +01005842 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5843
5844 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5845
5846 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5847 finish_output_size,
5848 &output_part_length,
5849 tag_buffer, tag_length,
5850 &tag_size ),
5851 PSA_ERROR_BAD_STATE );
5852
5853 psa_aead_abort( &operation );
5854
5855 /* Test calling verify on encryption. */
5856
Paul Elliott534d0b42021-06-22 19:15:20 +01005857 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5858
5859 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5860
5861 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5862 finish_output_size,
5863 &output_part_length,
5864 tag_buffer,
5865 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005866 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005867
5868 psa_aead_abort( &operation );
5869
5870
Paul Elliottc23a9a02021-06-21 18:32:46 +01005871exit:
5872 psa_destroy_key( key );
5873 psa_aead_abort( &operation );
5874 mbedtls_free( output_data );
5875 mbedtls_free( final_data );
5876 PSA_DONE( );
5877}
5878/* END_CASE */
5879
5880/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005881void signature_size( int type_arg,
5882 int bits,
5883 int alg_arg,
5884 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005885{
5886 psa_key_type_t type = type_arg;
5887 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005888 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005889
Gilles Peskinefe11b722018-12-18 00:24:04 +01005890 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005891
Gilles Peskinee59236f2018-01-27 23:32:46 +01005892exit:
5893 ;
5894}
5895/* END_CASE */
5896
5897/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005898void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5899 int alg_arg, data_t *input_data,
5900 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005901{
Ronald Cron5425a212020-08-04 14:58:35 +02005902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005903 psa_key_type_t key_type = key_type_arg;
5904 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005905 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005906 unsigned char *signature = NULL;
5907 size_t signature_size;
5908 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005910
Gilles Peskine8817f612018-12-18 00:18:46 +01005911 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005912
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005913 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005914 psa_set_key_algorithm( &attributes, alg );
5915 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005916
Gilles Peskine049c7532019-05-15 20:22:09 +02005917 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005918 &key ) );
5919 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005920 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005921
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005922 /* Allocate a buffer which has the size advertized by the
5923 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005924 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005925 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005926 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005927 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005928 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005929
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005930 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005931 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005932 input_data->x, input_data->len,
5933 signature, signature_size,
5934 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005935 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005936 ASSERT_COMPARE( output_data->x, output_data->len,
5937 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005938
5939exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005940 /*
5941 * Key attributes may have been returned by psa_get_key_attributes()
5942 * thus reset them as required.
5943 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005944 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005945
Ronald Cron5425a212020-08-04 14:58:35 +02005946 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005947 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005948 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005949}
5950/* END_CASE */
5951
5952/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005953void sign_hash_fail( int key_type_arg, data_t *key_data,
5954 int alg_arg, data_t *input_data,
5955 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005956{
Ronald Cron5425a212020-08-04 14:58:35 +02005957 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005958 psa_key_type_t key_type = key_type_arg;
5959 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005960 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005961 psa_status_t actual_status;
5962 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005963 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005964 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005965 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005966
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005967 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005968
Gilles Peskine8817f612018-12-18 00:18:46 +01005969 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005970
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005971 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005972 psa_set_key_algorithm( &attributes, alg );
5973 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005974
Gilles Peskine049c7532019-05-15 20:22:09 +02005975 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005976 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005977
Ronald Cron5425a212020-08-04 14:58:35 +02005978 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005979 input_data->x, input_data->len,
5980 signature, signature_size,
5981 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005982 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005983 /* The value of *signature_length is unspecified on error, but
5984 * whatever it is, it should be less than signature_size, so that
5985 * if the caller tries to read *signature_length bytes without
5986 * checking the error code then they don't overflow a buffer. */
5987 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005988
5989exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005990 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005991 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005992 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005993 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005994}
5995/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005996
5997/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005998void sign_verify_hash( int key_type_arg, data_t *key_data,
5999 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006000{
Ronald Cron5425a212020-08-04 14:58:35 +02006001 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006002 psa_key_type_t key_type = key_type_arg;
6003 psa_algorithm_t alg = alg_arg;
6004 size_t key_bits;
6005 unsigned char *signature = NULL;
6006 size_t signature_size;
6007 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006008 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006009
Gilles Peskine8817f612018-12-18 00:18:46 +01006010 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006011
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006012 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006013 psa_set_key_algorithm( &attributes, alg );
6014 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006015
Gilles Peskine049c7532019-05-15 20:22:09 +02006016 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006017 &key ) );
6018 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006019 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006020
6021 /* Allocate a buffer which has the size advertized by the
6022 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006023 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006024 key_bits, alg );
6025 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006026 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006027 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006028
6029 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006030 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006031 input_data->x, input_data->len,
6032 signature, signature_size,
6033 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006034 /* Check that the signature length looks sensible. */
6035 TEST_ASSERT( signature_length <= signature_size );
6036 TEST_ASSERT( signature_length > 0 );
6037
6038 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006039 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006040 input_data->x, input_data->len,
6041 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006042
6043 if( input_data->len != 0 )
6044 {
6045 /* Flip a bit in the input and verify that the signature is now
6046 * detected as invalid. Flip a bit at the beginning, not at the end,
6047 * because ECDSA may ignore the last few bits of the input. */
6048 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006049 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006050 input_data->x, input_data->len,
6051 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006052 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006053 }
6054
6055exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006056 /*
6057 * Key attributes may have been returned by psa_get_key_attributes()
6058 * thus reset them as required.
6059 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006060 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006061
Ronald Cron5425a212020-08-04 14:58:35 +02006062 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006063 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006064 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006065}
6066/* END_CASE */
6067
6068/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006069void verify_hash( int key_type_arg, data_t *key_data,
6070 int alg_arg, data_t *hash_data,
6071 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006072{
Ronald Cron5425a212020-08-04 14:58:35 +02006073 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006074 psa_key_type_t key_type = key_type_arg;
6075 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006076 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006077
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006078 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006079
Gilles Peskine8817f612018-12-18 00:18:46 +01006080 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006081
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006082 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006083 psa_set_key_algorithm( &attributes, alg );
6084 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006085
Gilles Peskine049c7532019-05-15 20:22:09 +02006086 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006087 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006088
Ronald Cron5425a212020-08-04 14:58:35 +02006089 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006090 hash_data->x, hash_data->len,
6091 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006092
itayzafrir5c753392018-05-08 11:18:38 +03006093exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006094 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006095 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006096 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006097}
6098/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006099
6100/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006101void verify_hash_fail( int key_type_arg, data_t *key_data,
6102 int alg_arg, data_t *hash_data,
6103 data_t *signature_data,
6104 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006105{
Ronald Cron5425a212020-08-04 14:58:35 +02006106 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006107 psa_key_type_t key_type = key_type_arg;
6108 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006109 psa_status_t actual_status;
6110 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006112
Gilles Peskine8817f612018-12-18 00:18:46 +01006113 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006114
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006115 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006116 psa_set_key_algorithm( &attributes, alg );
6117 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006118
Gilles Peskine049c7532019-05-15 20:22:09 +02006119 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006120 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006121
Ronald Cron5425a212020-08-04 14:58:35 +02006122 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006123 hash_data->x, hash_data->len,
6124 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006125 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006126
6127exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006128 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006129 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006130 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006131}
6132/* END_CASE */
6133
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006134/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006135void sign_message_deterministic( int key_type_arg,
6136 data_t *key_data,
6137 int alg_arg,
6138 data_t *input_data,
6139 data_t *output_data )
6140{
6141 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6142 psa_key_type_t key_type = key_type_arg;
6143 psa_algorithm_t alg = alg_arg;
6144 size_t key_bits;
6145 unsigned char *signature = NULL;
6146 size_t signature_size;
6147 size_t signature_length = 0xdeadbeef;
6148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6149
6150 PSA_ASSERT( psa_crypto_init( ) );
6151
6152 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6153 psa_set_key_algorithm( &attributes, alg );
6154 psa_set_key_type( &attributes, key_type );
6155
6156 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6157 &key ) );
6158 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6159 key_bits = psa_get_key_bits( &attributes );
6160
6161 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6162 TEST_ASSERT( signature_size != 0 );
6163 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
6164 ASSERT_ALLOC( signature, signature_size );
6165
6166 PSA_ASSERT( psa_sign_message( key, alg,
6167 input_data->x, input_data->len,
6168 signature, signature_size,
6169 &signature_length ) );
6170
6171 ASSERT_COMPARE( output_data->x, output_data->len,
6172 signature, signature_length );
6173
6174exit:
6175 psa_reset_key_attributes( &attributes );
6176
6177 psa_destroy_key( key );
6178 mbedtls_free( signature );
6179 PSA_DONE( );
6180
6181}
6182/* END_CASE */
6183
6184/* BEGIN_CASE */
6185void sign_message_fail( int key_type_arg,
6186 data_t *key_data,
6187 int alg_arg,
6188 data_t *input_data,
6189 int signature_size_arg,
6190 int expected_status_arg )
6191{
6192 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6193 psa_key_type_t key_type = key_type_arg;
6194 psa_algorithm_t alg = alg_arg;
6195 size_t signature_size = signature_size_arg;
6196 psa_status_t actual_status;
6197 psa_status_t expected_status = expected_status_arg;
6198 unsigned char *signature = NULL;
6199 size_t signature_length = 0xdeadbeef;
6200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6201
6202 ASSERT_ALLOC( signature, signature_size );
6203
6204 PSA_ASSERT( psa_crypto_init( ) );
6205
6206 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6207 psa_set_key_algorithm( &attributes, alg );
6208 psa_set_key_type( &attributes, key_type );
6209
6210 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6211 &key ) );
6212
6213 actual_status = psa_sign_message( key, alg,
6214 input_data->x, input_data->len,
6215 signature, signature_size,
6216 &signature_length );
6217 TEST_EQUAL( actual_status, expected_status );
6218 /* The value of *signature_length is unspecified on error, but
6219 * whatever it is, it should be less than signature_size, so that
6220 * if the caller tries to read *signature_length bytes without
6221 * checking the error code then they don't overflow a buffer. */
6222 TEST_ASSERT( signature_length <= signature_size );
6223
6224exit:
6225 psa_reset_key_attributes( &attributes );
6226 psa_destroy_key( key );
6227 mbedtls_free( signature );
6228 PSA_DONE( );
6229}
6230/* END_CASE */
6231
6232/* BEGIN_CASE */
6233void sign_verify_message( int key_type_arg,
6234 data_t *key_data,
6235 int alg_arg,
6236 data_t *input_data )
6237{
6238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6239 psa_key_type_t key_type = key_type_arg;
6240 psa_algorithm_t alg = alg_arg;
6241 size_t key_bits;
6242 unsigned char *signature = NULL;
6243 size_t signature_size;
6244 size_t signature_length = 0xdeadbeef;
6245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6246
6247 PSA_ASSERT( psa_crypto_init( ) );
6248
6249 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6250 PSA_KEY_USAGE_VERIFY_MESSAGE );
6251 psa_set_key_algorithm( &attributes, alg );
6252 psa_set_key_type( &attributes, key_type );
6253
6254 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6255 &key ) );
6256 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6257 key_bits = psa_get_key_bits( &attributes );
6258
6259 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6260 TEST_ASSERT( signature_size != 0 );
6261 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
6262 ASSERT_ALLOC( signature, signature_size );
6263
6264 PSA_ASSERT( psa_sign_message( key, alg,
6265 input_data->x, input_data->len,
6266 signature, signature_size,
6267 &signature_length ) );
6268 TEST_ASSERT( signature_length <= signature_size );
6269 TEST_ASSERT( signature_length > 0 );
6270
6271 PSA_ASSERT( psa_verify_message( key, alg,
6272 input_data->x, input_data->len,
6273 signature, signature_length ) );
6274
6275 if( input_data->len != 0 )
6276 {
6277 /* Flip a bit in the input and verify that the signature is now
6278 * detected as invalid. Flip a bit at the beginning, not at the end,
6279 * because ECDSA may ignore the last few bits of the input. */
6280 input_data->x[0] ^= 1;
6281 TEST_EQUAL( psa_verify_message( key, alg,
6282 input_data->x, input_data->len,
6283 signature, signature_length ),
6284 PSA_ERROR_INVALID_SIGNATURE );
6285 }
6286
6287exit:
6288 psa_reset_key_attributes( &attributes );
6289
6290 psa_destroy_key( key );
6291 mbedtls_free( signature );
6292 PSA_DONE( );
6293}
6294/* END_CASE */
6295
6296/* BEGIN_CASE */
6297void verify_message( int key_type_arg,
6298 data_t *key_data,
6299 int alg_arg,
6300 data_t *input_data,
6301 data_t *signature_data )
6302{
6303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6304 psa_key_type_t key_type = key_type_arg;
6305 psa_algorithm_t alg = alg_arg;
6306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6307
6308 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
6309
6310 PSA_ASSERT( psa_crypto_init( ) );
6311
6312 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6313 psa_set_key_algorithm( &attributes, alg );
6314 psa_set_key_type( &attributes, key_type );
6315
6316 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6317 &key ) );
6318
6319 PSA_ASSERT( psa_verify_message( key, alg,
6320 input_data->x, input_data->len,
6321 signature_data->x, signature_data->len ) );
6322
6323exit:
6324 psa_reset_key_attributes( &attributes );
6325 psa_destroy_key( key );
6326 PSA_DONE( );
6327}
6328/* END_CASE */
6329
6330/* BEGIN_CASE */
6331void verify_message_fail( int key_type_arg,
6332 data_t *key_data,
6333 int alg_arg,
6334 data_t *hash_data,
6335 data_t *signature_data,
6336 int expected_status_arg )
6337{
6338 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6339 psa_key_type_t key_type = key_type_arg;
6340 psa_algorithm_t alg = alg_arg;
6341 psa_status_t actual_status;
6342 psa_status_t expected_status = expected_status_arg;
6343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6344
6345 PSA_ASSERT( psa_crypto_init( ) );
6346
6347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6348 psa_set_key_algorithm( &attributes, alg );
6349 psa_set_key_type( &attributes, key_type );
6350
6351 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6352 &key ) );
6353
6354 actual_status = psa_verify_message( key, alg,
6355 hash_data->x, hash_data->len,
6356 signature_data->x,
6357 signature_data->len );
6358 TEST_EQUAL( actual_status, expected_status );
6359
6360exit:
6361 psa_reset_key_attributes( &attributes );
6362 psa_destroy_key( key );
6363 PSA_DONE( );
6364}
6365/* END_CASE */
6366
6367/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006368void asymmetric_encrypt( int key_type_arg,
6369 data_t *key_data,
6370 int alg_arg,
6371 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006372 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006373 int expected_output_length_arg,
6374 int expected_status_arg )
6375{
Ronald Cron5425a212020-08-04 14:58:35 +02006376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006377 psa_key_type_t key_type = key_type_arg;
6378 psa_algorithm_t alg = alg_arg;
6379 size_t expected_output_length = expected_output_length_arg;
6380 size_t key_bits;
6381 unsigned char *output = NULL;
6382 size_t output_size;
6383 size_t output_length = ~0;
6384 psa_status_t actual_status;
6385 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006386 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006387
Gilles Peskine8817f612018-12-18 00:18:46 +01006388 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006389
Gilles Peskine656896e2018-06-29 19:12:28 +02006390 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006391 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6392 psa_set_key_algorithm( &attributes, alg );
6393 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006394 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006395 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006396
6397 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006398 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006399 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006400
Gilles Peskine656896e2018-06-29 19:12:28 +02006401 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006402 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006403 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006404
6405 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006406 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006407 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006408 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006409 output, output_size,
6410 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006411 TEST_EQUAL( actual_status, expected_status );
6412 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006413
Gilles Peskine68428122018-06-30 18:42:41 +02006414 /* If the label is empty, the test framework puts a non-null pointer
6415 * in label->x. Test that a null pointer works as well. */
6416 if( label->len == 0 )
6417 {
6418 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006419 if( output_size != 0 )
6420 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006421 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006422 input_data->x, input_data->len,
6423 NULL, label->len,
6424 output, output_size,
6425 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006426 TEST_EQUAL( actual_status, expected_status );
6427 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006428 }
6429
Gilles Peskine656896e2018-06-29 19:12:28 +02006430exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006431 /*
6432 * Key attributes may have been returned by psa_get_key_attributes()
6433 * thus reset them as required.
6434 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006435 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006436
Ronald Cron5425a212020-08-04 14:58:35 +02006437 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006438 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006439 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006440}
6441/* END_CASE */
6442
6443/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006444void asymmetric_encrypt_decrypt( int key_type_arg,
6445 data_t *key_data,
6446 int alg_arg,
6447 data_t *input_data,
6448 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006449{
Ronald Cron5425a212020-08-04 14:58:35 +02006450 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006451 psa_key_type_t key_type = key_type_arg;
6452 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006453 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006454 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006455 size_t output_size;
6456 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006457 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006458 size_t output2_size;
6459 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006461
Gilles Peskine8817f612018-12-18 00:18:46 +01006462 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006463
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006464 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6465 psa_set_key_algorithm( &attributes, alg );
6466 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006467
Gilles Peskine049c7532019-05-15 20:22:09 +02006468 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006469 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006470
6471 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006472 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006473 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006474
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006475 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006476 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006477 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006478
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006479 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006480 TEST_ASSERT( output2_size <=
6481 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6482 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006483 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006484
Gilles Peskineeebd7382018-06-08 18:11:54 +02006485 /* We test encryption by checking that encrypt-then-decrypt gives back
6486 * the original plaintext because of the non-optional random
6487 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006488 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006489 input_data->x, input_data->len,
6490 label->x, label->len,
6491 output, output_size,
6492 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006493 /* We don't know what ciphertext length to expect, but check that
6494 * it looks sensible. */
6495 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006496
Ronald Cron5425a212020-08-04 14:58:35 +02006497 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006498 output, output_length,
6499 label->x, label->len,
6500 output2, output2_size,
6501 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006502 ASSERT_COMPARE( input_data->x, input_data->len,
6503 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006504
6505exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006506 /*
6507 * Key attributes may have been returned by psa_get_key_attributes()
6508 * thus reset them as required.
6509 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006510 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006511
Ronald Cron5425a212020-08-04 14:58:35 +02006512 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006513 mbedtls_free( output );
6514 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006515 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006516}
6517/* END_CASE */
6518
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006519/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006520void asymmetric_decrypt( int key_type_arg,
6521 data_t *key_data,
6522 int alg_arg,
6523 data_t *input_data,
6524 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006525 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006526{
Ronald Cron5425a212020-08-04 14:58:35 +02006527 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006528 psa_key_type_t key_type = key_type_arg;
6529 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006530 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006531 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006532 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006533 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006534 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006535
Gilles Peskine8817f612018-12-18 00:18:46 +01006536 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006537
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006538 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6539 psa_set_key_algorithm( &attributes, alg );
6540 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006541
Gilles Peskine049c7532019-05-15 20:22:09 +02006542 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006543 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006544
gabor-mezei-armceface22021-01-21 12:26:17 +01006545 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6546 key_bits = psa_get_key_bits( &attributes );
6547
6548 /* Determine the maximum ciphertext length */
6549 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6550 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6551 ASSERT_ALLOC( output, output_size );
6552
Ronald Cron5425a212020-08-04 14:58:35 +02006553 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006554 input_data->x, input_data->len,
6555 label->x, label->len,
6556 output,
6557 output_size,
6558 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006559 ASSERT_COMPARE( expected_data->x, expected_data->len,
6560 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006561
Gilles Peskine68428122018-06-30 18:42:41 +02006562 /* If the label is empty, the test framework puts a non-null pointer
6563 * in label->x. Test that a null pointer works as well. */
6564 if( label->len == 0 )
6565 {
6566 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006567 if( output_size != 0 )
6568 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006569 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006570 input_data->x, input_data->len,
6571 NULL, label->len,
6572 output,
6573 output_size,
6574 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006575 ASSERT_COMPARE( expected_data->x, expected_data->len,
6576 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006577 }
6578
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006579exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006580 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006581 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006582 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006583 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006584}
6585/* END_CASE */
6586
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006587/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006588void asymmetric_decrypt_fail( int key_type_arg,
6589 data_t *key_data,
6590 int alg_arg,
6591 data_t *input_data,
6592 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006593 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006594 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006595{
Ronald Cron5425a212020-08-04 14:58:35 +02006596 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006597 psa_key_type_t key_type = key_type_arg;
6598 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006599 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006600 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006601 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006602 psa_status_t actual_status;
6603 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006605
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006606 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006607
Gilles Peskine8817f612018-12-18 00:18:46 +01006608 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006609
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006610 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6611 psa_set_key_algorithm( &attributes, alg );
6612 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006613
Gilles Peskine049c7532019-05-15 20:22:09 +02006614 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006615 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006616
Ronald Cron5425a212020-08-04 14:58:35 +02006617 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006618 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006619 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006620 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006621 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006622 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006623 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006624
Gilles Peskine68428122018-06-30 18:42:41 +02006625 /* If the label is empty, the test framework puts a non-null pointer
6626 * in label->x. Test that a null pointer works as well. */
6627 if( label->len == 0 )
6628 {
6629 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006630 if( output_size != 0 )
6631 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006632 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006633 input_data->x, input_data->len,
6634 NULL, label->len,
6635 output, output_size,
6636 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006637 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006638 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006639 }
6640
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006641exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006642 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006643 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006644 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006645 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006646}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006647/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006648
6649/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006650void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006651{
6652 /* Test each valid way of initializing the object, except for `= {0}`, as
6653 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6654 * though it's OK by the C standard. We could test for this, but we'd need
6655 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006656 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006657 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6658 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6659 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006660
6661 memset( &zero, 0, sizeof( zero ) );
6662
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006663 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006664 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006665 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006666 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006667 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006668 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006669 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006670
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006671 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006672 PSA_ASSERT( psa_key_derivation_abort(&func) );
6673 PSA_ASSERT( psa_key_derivation_abort(&init) );
6674 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006675}
6676/* END_CASE */
6677
Janos Follath16de4a42019-06-13 16:32:24 +01006678/* BEGIN_CASE */
6679void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006680{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006681 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006682 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006683 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006684
Gilles Peskine8817f612018-12-18 00:18:46 +01006685 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006686
Janos Follath16de4a42019-06-13 16:32:24 +01006687 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006688 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006689
6690exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006691 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006692 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006693}
6694/* END_CASE */
6695
Janos Follathaf3c2a02019-06-12 12:34:34 +01006696/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006697void derive_set_capacity( int alg_arg, int capacity_arg,
6698 int expected_status_arg )
6699{
6700 psa_algorithm_t alg = alg_arg;
6701 size_t capacity = capacity_arg;
6702 psa_status_t expected_status = expected_status_arg;
6703 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6704
6705 PSA_ASSERT( psa_crypto_init( ) );
6706
6707 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6708
6709 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6710 expected_status );
6711
6712exit:
6713 psa_key_derivation_abort( &operation );
6714 PSA_DONE( );
6715}
6716/* END_CASE */
6717
6718/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006719void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006720 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006721 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006722 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006723 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006724 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006725 int expected_status_arg3,
6726 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006727{
6728 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006729 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6730 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006731 psa_status_t expected_statuses[] = {expected_status_arg1,
6732 expected_status_arg2,
6733 expected_status_arg3};
6734 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006735 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6736 MBEDTLS_SVC_KEY_ID_INIT,
6737 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006738 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6740 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006741 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006742 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006743 psa_status_t expected_output_status = expected_output_status_arg;
6744 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006745
6746 PSA_ASSERT( psa_crypto_init( ) );
6747
6748 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6749 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006750
6751 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6752
6753 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6754 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006755 mbedtls_test_set_step( i );
6756 if( steps[i] == 0 )
6757 {
6758 /* Skip this step */
6759 }
6760 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006761 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006762 psa_set_key_type( &attributes, key_types[i] );
6763 PSA_ASSERT( psa_import_key( &attributes,
6764 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006765 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006766 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6767 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6768 {
6769 // When taking a private key as secret input, use key agreement
6770 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006771 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6772 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006773 expected_statuses[i] );
6774 }
6775 else
6776 {
6777 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006778 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006779 expected_statuses[i] );
6780 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006781 }
6782 else
6783 {
6784 TEST_EQUAL( psa_key_derivation_input_bytes(
6785 &operation, steps[i],
6786 inputs[i]->x, inputs[i]->len ),
6787 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006788 }
6789 }
6790
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006791 if( output_key_type != PSA_KEY_TYPE_NONE )
6792 {
6793 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006794 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006795 psa_set_key_bits( &attributes, 8 );
6796 actual_output_status =
6797 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006798 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006799 }
6800 else
6801 {
6802 uint8_t buffer[1];
6803 actual_output_status =
6804 psa_key_derivation_output_bytes( &operation,
6805 buffer, sizeof( buffer ) );
6806 }
6807 TEST_EQUAL( actual_output_status, expected_output_status );
6808
Janos Follathaf3c2a02019-06-12 12:34:34 +01006809exit:
6810 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006811 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6812 psa_destroy_key( keys[i] );
6813 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006814 PSA_DONE( );
6815}
6816/* END_CASE */
6817
Janos Follathd958bb72019-07-03 15:02:16 +01006818/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006819void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006820{
Janos Follathd958bb72019-07-03 15:02:16 +01006821 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006823 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006824 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006825 unsigned char input1[] = "Input 1";
6826 size_t input1_length = sizeof( input1 );
6827 unsigned char input2[] = "Input 2";
6828 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006829 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006830 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006831 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6832 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6833 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006834 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006835
Gilles Peskine8817f612018-12-18 00:18:46 +01006836 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006837
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006838 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6839 psa_set_key_algorithm( &attributes, alg );
6840 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006841
Gilles Peskine73676cb2019-05-15 20:15:10 +02006842 PSA_ASSERT( psa_import_key( &attributes,
6843 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006844 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006845
6846 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006847 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6848 input1, input1_length,
6849 input2, input2_length,
6850 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006851 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006852
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006853 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006854 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006855 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006856
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006857 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006858
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006859 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006860 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006861
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006862exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006863 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006864 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006865 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006866}
6867/* END_CASE */
6868
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006869/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006870void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006871{
6872 uint8_t output_buffer[16];
6873 size_t buffer_size = 16;
6874 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006875 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006876
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006877 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6878 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006879 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006880
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006881 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006882 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006883
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006884 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006885
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006886 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6887 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006888 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006889
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006890 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006891 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006892
6893exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006894 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006895}
6896/* END_CASE */
6897
6898/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006899void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006900 int step1_arg, data_t *input1, int expected_status_arg1,
6901 int step2_arg, data_t *input2, int expected_status_arg2,
6902 int step3_arg, data_t *input3, int expected_status_arg3,
6903 int step4_arg, data_t *input4, int expected_status_arg4,
6904 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006905 int requested_capacity_arg,
6906 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006907 data_t *expected_output2,
6908 int other_key_input_type,
6909 int key_input_type,
6910 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006911{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006912 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02006913 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
6914 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006915 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
6916 MBEDTLS_SVC_KEY_ID_INIT,
6917 MBEDTLS_SVC_KEY_ID_INIT,
6918 MBEDTLS_SVC_KEY_ID_INIT};
6919 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
6920 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006921 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006922 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006923 uint8_t *expected_outputs[2] =
6924 {expected_output1->x, expected_output2->x};
6925 size_t output_sizes[2] =
6926 {expected_output1->len, expected_output2->len};
6927 size_t output_buffer_size = 0;
6928 uint8_t *output_buffer = NULL;
6929 size_t expected_capacity;
6930 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006931 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
6932 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
6933 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
6934 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006935 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006936 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006937
6938 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6939 {
6940 if( output_sizes[i] > output_buffer_size )
6941 output_buffer_size = output_sizes[i];
6942 if( output_sizes[i] == 0 )
6943 expected_outputs[i] = NULL;
6944 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006945 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006946 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006947
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006948 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006949 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6950 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6951 requested_capacity ) );
6952 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006953 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006954 switch( steps[i] )
6955 {
6956 case 0:
6957 break;
6958 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02006959 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01006960 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006961 case 0: // input bytes
6962 PSA_ASSERT( psa_key_derivation_input_bytes(
6963 &operation, steps[i],
6964 inputs[i]->x, inputs[i]->len ) );
6965 break;
6966 case 1: // input key
6967 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
6968 psa_set_key_algorithm( &attributes1, alg );
6969 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
6970
6971 PSA_ASSERT( psa_import_key( &attributes1,
6972 inputs[i]->x, inputs[i]->len,
6973 &keys[i] ) );
6974
Przemek Stekiel38647de2022-04-19 13:27:47 +02006975 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006976 {
6977 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
6978 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ) <=
6979 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6980 }
6981
Przemek Stekiel38647de2022-04-19 13:27:47 +02006982 PSA_ASSERT( psa_key_derivation_input_key( &operation,
6983 steps[i],
6984 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006985 break;
6986 default:
6987 TEST_ASSERT( ! "default case not supported" );
6988 break;
6989 }
6990 break;
6991 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02006992 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006993 {
6994 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02006995 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
6996 steps[i],
6997 inputs[i]->x,
6998 inputs[i]->len ),
6999 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007000 break;
7001 case 1: // input key
7002 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7003 psa_set_key_algorithm( &attributes2, alg );
7004 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7005
7006 // other secret of type RAW_DATA passed with input_key
Przemek Stekiel38647de2022-04-19 13:27:47 +02007007 if( statuses[i] == PSA_ERROR_INVALID_ARGUMENT )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007008 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7009
7010 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007011 inputs[i]->x, inputs[i]->len,
7012 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007013
Przemek Stekiel38647de2022-04-19 13:27:47 +02007014 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7015 steps[i],
7016 keys[i] ),
7017 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007018 break;
7019 case 2: // key agreement
7020 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7021 psa_set_key_algorithm( &attributes3, alg );
7022 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7023
7024 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007025 inputs[i]->x, inputs[i]->len,
7026 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007027
7028 TEST_EQUAL( psa_key_derivation_key_agreement(
7029 &operation,
7030 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7031 keys[i], key_agreement_peer_key->x,
7032 key_agreement_peer_key->len ), statuses[i] );
7033 break;
7034 case 3: // raw key agreement
7035 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7036 psa_set_key_algorithm( &attributes3, PSA_ALG_ECDH );
7037 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7038
7039 uint8_t key_agreement_output[32];
7040 size_t key_agreement_output_length;
7041
7042 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007043 inputs[i]->x, inputs[i]->len,
7044 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007045
7046 PSA_ASSERT( psa_raw_key_agreement(
7047 PSA_ALG_ECDH,
7048 keys[i], key_agreement_peer_key->x,
7049 key_agreement_peer_key->len, key_agreement_output,
7050 sizeof(key_agreement_output), &key_agreement_output_length ) );
7051
7052 TEST_ASSERT( key_agreement_output_length == 32 );
7053
7054 TEST_EQUAL( psa_key_derivation_input_bytes(
7055 &operation, steps[i],
7056 key_agreement_output, key_agreement_output_length ), statuses[i] );
7057 break;
7058 default:
7059 TEST_ASSERT( ! "default case not supported" );
7060 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007061 }
7062
Przemek Stekiel38647de2022-04-19 13:27:47 +02007063 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007064 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007065 break;
7066 default:
7067 PSA_ASSERT( psa_key_derivation_input_bytes(
7068 &operation, steps[i],
7069 inputs[i]->x, inputs[i]->len ) );
7070 break;
7071 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007072 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007073
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007074 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007075 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007076 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007077 expected_capacity = requested_capacity;
7078
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007079 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007080 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007081 /* Test that output key derivation is not permitted when secret is
7082 * passed using input bytes and other secret is passed using input key. */
7083 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
7084
7085 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7086 psa_set_key_algorithm( &attributes4, alg );
7087 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
7088 psa_set_key_bits( &attributes4, 48 );
7089
7090 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
7091 &derived_key ), PSA_ERROR_NOT_PERMITTED );
7092 }
7093 else // output bytes
7094 {
7095 /* Expansion phase. */
7096 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007097 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007098 /* Read some bytes. */
7099 status = psa_key_derivation_output_bytes( &operation,
7100 output_buffer, output_sizes[i] );
7101 if( expected_capacity == 0 && output_sizes[i] == 0 )
7102 {
7103 /* Reading 0 bytes when 0 bytes are available can go either way. */
7104 TEST_ASSERT( status == PSA_SUCCESS ||
7105 status == PSA_ERROR_INSUFFICIENT_DATA );
7106 continue;
7107 }
7108 else if( expected_capacity == 0 ||
7109 output_sizes[i] > expected_capacity )
7110 {
7111 /* Capacity exceeded. */
7112 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7113 expected_capacity = 0;
7114 continue;
7115 }
7116 /* Success. Check the read data. */
7117 PSA_ASSERT( status );
7118 if( output_sizes[i] != 0 )
7119 ASSERT_COMPARE( output_buffer, output_sizes[i],
7120 expected_outputs[i], output_sizes[i] );
7121 /* Check the operation status. */
7122 expected_capacity -= output_sizes[i];
7123 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7124 &current_capacity ) );
7125 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007126 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007127 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007128 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007129
7130exit:
7131 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007132 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007133 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7134 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007135 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007136}
7137/* END_CASE */
7138
7139/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007140void derive_full( int alg_arg,
7141 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007142 data_t *input1,
7143 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007144 int requested_capacity_arg )
7145{
Ronald Cron5425a212020-08-04 14:58:35 +02007146 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007147 psa_algorithm_t alg = alg_arg;
7148 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007149 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007150 unsigned char output_buffer[16];
7151 size_t expected_capacity = requested_capacity;
7152 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007154
Gilles Peskine8817f612018-12-18 00:18:46 +01007155 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007156
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7158 psa_set_key_algorithm( &attributes, alg );
7159 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007160
Gilles Peskine049c7532019-05-15 20:22:09 +02007161 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007162 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007163
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007164 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7165 input1->x, input1->len,
7166 input2->x, input2->len,
7167 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007168 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007169
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007170 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007171 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007172 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007173
7174 /* Expansion phase. */
7175 while( current_capacity > 0 )
7176 {
7177 size_t read_size = sizeof( output_buffer );
7178 if( read_size > current_capacity )
7179 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007180 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007181 output_buffer,
7182 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007183 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007184 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007185 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007186 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007187 }
7188
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007189 /* Check that the operation refuses to go over capacity. */
7190 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007191 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007192
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007193 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007194
7195exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007196 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007197 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007198 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007199}
7200/* END_CASE */
7201
Janos Follathe60c9052019-07-03 13:51:30 +01007202/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007203void derive_key_exercise( int alg_arg,
7204 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007205 data_t *input1,
7206 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007207 int derived_type_arg,
7208 int derived_bits_arg,
7209 int derived_usage_arg,
7210 int derived_alg_arg )
7211{
Ronald Cron5425a212020-08-04 14:58:35 +02007212 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7213 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007214 psa_algorithm_t alg = alg_arg;
7215 psa_key_type_t derived_type = derived_type_arg;
7216 size_t derived_bits = derived_bits_arg;
7217 psa_key_usage_t derived_usage = derived_usage_arg;
7218 psa_algorithm_t derived_alg = derived_alg_arg;
7219 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007220 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007222 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007223
Gilles Peskine8817f612018-12-18 00:18:46 +01007224 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007225
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007226 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7227 psa_set_key_algorithm( &attributes, alg );
7228 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007229 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007230 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007231
7232 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007233 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7234 input1->x, input1->len,
7235 input2->x, input2->len,
7236 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007237 goto exit;
7238
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007239 psa_set_key_usage_flags( &attributes, derived_usage );
7240 psa_set_key_algorithm( &attributes, derived_alg );
7241 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007242 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007243 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007244 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007245
7246 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007247 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007248 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7249 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007250
7251 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007252 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007253 goto exit;
7254
7255exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007256 /*
7257 * Key attributes may have been returned by psa_get_key_attributes()
7258 * thus reset them as required.
7259 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007260 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007261
7262 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007263 psa_destroy_key( base_key );
7264 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007265 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007266}
7267/* END_CASE */
7268
Janos Follath42fd8882019-07-03 14:17:09 +01007269/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007270void derive_key_export( int alg_arg,
7271 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007272 data_t *input1,
7273 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007274 int bytes1_arg,
7275 int bytes2_arg )
7276{
Ronald Cron5425a212020-08-04 14:58:35 +02007277 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7278 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007279 psa_algorithm_t alg = alg_arg;
7280 size_t bytes1 = bytes1_arg;
7281 size_t bytes2 = bytes2_arg;
7282 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007283 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007284 uint8_t *output_buffer = NULL;
7285 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007286 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7287 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007288 size_t length;
7289
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007290 ASSERT_ALLOC( output_buffer, capacity );
7291 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007292 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007293
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007294 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7295 psa_set_key_algorithm( &base_attributes, alg );
7296 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007297 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007298 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007299
7300 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007301 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7302 input1->x, input1->len,
7303 input2->x, input2->len,
7304 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007305 goto exit;
7306
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007307 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007308 output_buffer,
7309 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007310 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007311
7312 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007313 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7314 input1->x, input1->len,
7315 input2->x, input2->len,
7316 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007317 goto exit;
7318
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007319 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7320 psa_set_key_algorithm( &derived_attributes, 0 );
7321 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007322 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007323 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007324 &derived_key ) );
7325 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007326 export_buffer, bytes1,
7327 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007328 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007329 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007330 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007331 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007332 &derived_key ) );
7333 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007334 export_buffer + bytes1, bytes2,
7335 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007336 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007337
7338 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007339 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7340 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007341
7342exit:
7343 mbedtls_free( output_buffer );
7344 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007345 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007346 psa_destroy_key( base_key );
7347 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007348 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007349}
7350/* END_CASE */
7351
7352/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007353void derive_key_type( int alg_arg,
7354 data_t *key_data,
7355 data_t *input1,
7356 data_t *input2,
7357 int key_type_arg, int bits_arg,
7358 data_t *expected_export )
7359{
7360 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7361 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
7362 const psa_algorithm_t alg = alg_arg;
7363 const psa_key_type_t key_type = key_type_arg;
7364 const size_t bits = bits_arg;
7365 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7366 const size_t export_buffer_size =
7367 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
7368 uint8_t *export_buffer = NULL;
7369 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7370 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7371 size_t export_length;
7372
7373 ASSERT_ALLOC( export_buffer, export_buffer_size );
7374 PSA_ASSERT( psa_crypto_init( ) );
7375
7376 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7377 psa_set_key_algorithm( &base_attributes, alg );
7378 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7379 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
7380 &base_key ) );
7381
Przemek Stekielc85f0912022-03-08 11:37:54 +01007382 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007383 &operation, base_key, alg,
7384 input1->x, input1->len,
7385 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01007386 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007387 goto exit;
7388
7389 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7390 psa_set_key_algorithm( &derived_attributes, 0 );
7391 psa_set_key_type( &derived_attributes, key_type );
7392 psa_set_key_bits( &derived_attributes, bits );
7393 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
7394 &derived_key ) );
7395
7396 PSA_ASSERT( psa_export_key( derived_key,
7397 export_buffer, export_buffer_size,
7398 &export_length ) );
7399 ASSERT_COMPARE( export_buffer, export_length,
7400 expected_export->x, expected_export->len );
7401
7402exit:
7403 mbedtls_free( export_buffer );
7404 psa_key_derivation_abort( &operation );
7405 psa_destroy_key( base_key );
7406 psa_destroy_key( derived_key );
7407 PSA_DONE( );
7408}
7409/* END_CASE */
7410
7411/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007412void derive_key( int alg_arg,
7413 data_t *key_data, data_t *input1, data_t *input2,
7414 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007415 int expected_status_arg,
7416 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02007417{
Ronald Cron5425a212020-08-04 14:58:35 +02007418 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7419 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02007420 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007421 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02007422 size_t bits = bits_arg;
7423 psa_status_t expected_status = expected_status_arg;
7424 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7425 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7426 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7427
7428 PSA_ASSERT( psa_crypto_init( ) );
7429
7430 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7431 psa_set_key_algorithm( &base_attributes, alg );
7432 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7433 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007434 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02007435
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007436 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7437 input1->x, input1->len,
7438 input2->x, input2->len,
7439 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02007440 goto exit;
7441
7442 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7443 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007444 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02007445 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01007446
7447 psa_status_t status =
7448 psa_key_derivation_output_key( &derived_attributes,
7449 &operation,
7450 &derived_key );
7451 if( is_large_output > 0 )
7452 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7453 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02007454
7455exit:
7456 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007457 psa_destroy_key( base_key );
7458 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02007459 PSA_DONE( );
7460}
7461/* END_CASE */
7462
7463/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02007464void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02007465 int our_key_type_arg, int our_key_alg_arg,
7466 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02007467 int expected_status_arg )
7468{
Ronald Cron5425a212020-08-04 14:58:35 +02007469 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007470 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007471 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007472 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007473 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007474 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007475 psa_status_t expected_status = expected_status_arg;
7476 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007477
Gilles Peskine8817f612018-12-18 00:18:46 +01007478 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007479
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007481 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007482 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007483 PSA_ASSERT( psa_import_key( &attributes,
7484 our_key_data->x, our_key_data->len,
7485 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007486
Gilles Peskine77f40d82019-04-11 21:27:06 +02007487 /* The tests currently include inputs that should fail at either step.
7488 * Test cases that fail at the setup step should be changed to call
7489 * key_derivation_setup instead, and this function should be renamed
7490 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007491 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02007492 if( status == PSA_SUCCESS )
7493 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007494 TEST_EQUAL( psa_key_derivation_key_agreement(
7495 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
7496 our_key,
7497 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02007498 expected_status );
7499 }
7500 else
7501 {
7502 TEST_ASSERT( status == expected_status );
7503 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007504
7505exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007506 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007507 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007508 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007509}
7510/* END_CASE */
7511
7512/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007513void raw_key_agreement( int alg_arg,
7514 int our_key_type_arg, data_t *our_key_data,
7515 data_t *peer_key_data,
7516 data_t *expected_output )
7517{
Ronald Cron5425a212020-08-04 14:58:35 +02007518 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007519 psa_algorithm_t alg = alg_arg;
7520 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007522 unsigned char *output = NULL;
7523 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007524 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007525
7526 ASSERT_ALLOC( output, expected_output->len );
7527 PSA_ASSERT( psa_crypto_init( ) );
7528
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007529 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7530 psa_set_key_algorithm( &attributes, alg );
7531 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007532 PSA_ASSERT( psa_import_key( &attributes,
7533 our_key_data->x, our_key_data->len,
7534 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007535
gabor-mezei-armceface22021-01-21 12:26:17 +01007536 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7537 key_bits = psa_get_key_bits( &attributes );
7538
Gilles Peskinebe697d82019-05-16 18:00:41 +02007539 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7540 peer_key_data->x, peer_key_data->len,
7541 output, expected_output->len,
7542 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007543 ASSERT_COMPARE( output, output_length,
7544 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01007545 TEST_ASSERT( output_length <=
7546 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7547 TEST_ASSERT( output_length <=
7548 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007549
7550exit:
7551 mbedtls_free( output );
7552 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007553 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007554}
7555/* END_CASE */
7556
7557/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007558void key_agreement_capacity( int alg_arg,
7559 int our_key_type_arg, data_t *our_key_data,
7560 data_t *peer_key_data,
7561 int expected_capacity_arg )
7562{
Ronald Cron5425a212020-08-04 14:58:35 +02007563 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007564 psa_algorithm_t alg = alg_arg;
7565 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007566 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007568 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007569 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007570
Gilles Peskine8817f612018-12-18 00:18:46 +01007571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007572
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007573 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7574 psa_set_key_algorithm( &attributes, alg );
7575 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007576 PSA_ASSERT( psa_import_key( &attributes,
7577 our_key_data->x, our_key_data->len,
7578 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007579
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007580 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007581 PSA_ASSERT( psa_key_derivation_key_agreement(
7582 &operation,
7583 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7584 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007585 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7586 {
7587 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007588 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007589 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007590 NULL, 0 ) );
7591 }
Gilles Peskine59685592018-09-18 12:11:34 +02007592
Gilles Peskinebf491972018-10-25 22:36:12 +02007593 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007594 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007595 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007596 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007597
Gilles Peskinebf491972018-10-25 22:36:12 +02007598 /* Test the actual capacity by reading the output. */
7599 while( actual_capacity > sizeof( output ) )
7600 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007601 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007602 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007603 actual_capacity -= sizeof( output );
7604 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007605 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007606 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007607 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007608 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007609
Gilles Peskine59685592018-09-18 12:11:34 +02007610exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007611 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007612 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007613 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007614}
7615/* END_CASE */
7616
7617/* BEGIN_CASE */
7618void key_agreement_output( int alg_arg,
7619 int our_key_type_arg, data_t *our_key_data,
7620 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007621 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007622{
Ronald Cron5425a212020-08-04 14:58:35 +02007623 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007624 psa_algorithm_t alg = alg_arg;
7625 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007626 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007627 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007628 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007629
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007630 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7631 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007632
Gilles Peskine8817f612018-12-18 00:18:46 +01007633 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007634
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007635 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7636 psa_set_key_algorithm( &attributes, alg );
7637 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007638 PSA_ASSERT( psa_import_key( &attributes,
7639 our_key_data->x, our_key_data->len,
7640 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007641
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007642 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007643 PSA_ASSERT( psa_key_derivation_key_agreement(
7644 &operation,
7645 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7646 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007647 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7648 {
7649 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007650 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007651 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007652 NULL, 0 ) );
7653 }
Gilles Peskine59685592018-09-18 12:11:34 +02007654
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007655 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007656 actual_output,
7657 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007658 ASSERT_COMPARE( actual_output, expected_output1->len,
7659 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007660 if( expected_output2->len != 0 )
7661 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007662 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007663 actual_output,
7664 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007665 ASSERT_COMPARE( actual_output, expected_output2->len,
7666 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007667 }
Gilles Peskine59685592018-09-18 12:11:34 +02007668
7669exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007670 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007671 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007672 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007673 mbedtls_free( actual_output );
7674}
7675/* END_CASE */
7676
7677/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007678void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007679{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007680 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007681 unsigned char *output = NULL;
7682 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007683 size_t i;
7684 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007685
Simon Butcher49f8e312020-03-03 15:51:50 +00007686 TEST_ASSERT( bytes_arg >= 0 );
7687
Gilles Peskine91892022021-02-08 19:50:26 +01007688 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007689 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007690
Gilles Peskine8817f612018-12-18 00:18:46 +01007691 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007692
Gilles Peskinea50d7392018-06-21 10:22:13 +02007693 /* Run several times, to ensure that every output byte will be
7694 * nonzero at least once with overwhelming probability
7695 * (2^(-8*number_of_runs)). */
7696 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007697 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007698 if( bytes != 0 )
7699 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007700 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007701
Gilles Peskinea50d7392018-06-21 10:22:13 +02007702 for( i = 0; i < bytes; i++ )
7703 {
7704 if( output[i] != 0 )
7705 ++changed[i];
7706 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007707 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007708
7709 /* Check that every byte was changed to nonzero at least once. This
7710 * validates that psa_generate_random is overwriting every byte of
7711 * the output buffer. */
7712 for( i = 0; i < bytes; i++ )
7713 {
7714 TEST_ASSERT( changed[i] != 0 );
7715 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007716
7717exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007718 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007719 mbedtls_free( output );
7720 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007721}
7722/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007723
7724/* BEGIN_CASE */
7725void generate_key( int type_arg,
7726 int bits_arg,
7727 int usage_arg,
7728 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007729 int expected_status_arg,
7730 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007731{
Ronald Cron5425a212020-08-04 14:58:35 +02007732 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007733 psa_key_type_t type = type_arg;
7734 psa_key_usage_t usage = usage_arg;
7735 size_t bits = bits_arg;
7736 psa_algorithm_t alg = alg_arg;
7737 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007738 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007739 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007740
Gilles Peskine8817f612018-12-18 00:18:46 +01007741 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007742
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007743 psa_set_key_usage_flags( &attributes, usage );
7744 psa_set_key_algorithm( &attributes, alg );
7745 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007746 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007747
7748 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007749 psa_status_t status = psa_generate_key( &attributes, &key );
7750
7751 if( is_large_key > 0 )
7752 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7753 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007754 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007755 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007756
7757 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007758 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007759 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7760 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007761
Gilles Peskine818ca122018-06-20 18:16:48 +02007762 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007763 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007764 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007765
7766exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007767 /*
7768 * Key attributes may have been returned by psa_get_key_attributes()
7769 * thus reset them as required.
7770 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007771 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007772
Ronald Cron5425a212020-08-04 14:58:35 +02007773 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007774 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007775}
7776/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007777
Ronald Cronee414c72021-03-18 18:50:08 +01007778/* 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 +02007779void generate_key_rsa( int bits_arg,
7780 data_t *e_arg,
7781 int expected_status_arg )
7782{
Ronald Cron5425a212020-08-04 14:58:35 +02007783 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007784 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007785 size_t bits = bits_arg;
7786 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7787 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7788 psa_status_t expected_status = expected_status_arg;
7789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7790 uint8_t *exported = NULL;
7791 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007792 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007793 size_t exported_length = SIZE_MAX;
7794 uint8_t *e_read_buffer = NULL;
7795 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007796 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007797 size_t e_read_length = SIZE_MAX;
7798
7799 if( e_arg->len == 0 ||
7800 ( e_arg->len == 3 &&
7801 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7802 {
7803 is_default_public_exponent = 1;
7804 e_read_size = 0;
7805 }
7806 ASSERT_ALLOC( e_read_buffer, e_read_size );
7807 ASSERT_ALLOC( exported, exported_size );
7808
7809 PSA_ASSERT( psa_crypto_init( ) );
7810
7811 psa_set_key_usage_flags( &attributes, usage );
7812 psa_set_key_algorithm( &attributes, alg );
7813 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7814 e_arg->x, e_arg->len ) );
7815 psa_set_key_bits( &attributes, bits );
7816
7817 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007818 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007819 if( expected_status != PSA_SUCCESS )
7820 goto exit;
7821
7822 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007823 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007824 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7825 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7826 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7827 e_read_buffer, e_read_size,
7828 &e_read_length ) );
7829 if( is_default_public_exponent )
7830 TEST_EQUAL( e_read_length, 0 );
7831 else
7832 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7833
7834 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007835 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007836 goto exit;
7837
7838 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007839 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007840 exported, exported_size,
7841 &exported_length ) );
7842 {
7843 uint8_t *p = exported;
7844 uint8_t *end = exported + exported_length;
7845 size_t len;
7846 /* RSAPublicKey ::= SEQUENCE {
7847 * modulus INTEGER, -- n
7848 * publicExponent INTEGER } -- e
7849 */
7850 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007851 MBEDTLS_ASN1_SEQUENCE |
7852 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007853 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007854 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7855 MBEDTLS_ASN1_INTEGER ) );
7856 if( len >= 1 && p[0] == 0 )
7857 {
7858 ++p;
7859 --len;
7860 }
7861 if( e_arg->len == 0 )
7862 {
7863 TEST_EQUAL( len, 3 );
7864 TEST_EQUAL( p[0], 1 );
7865 TEST_EQUAL( p[1], 0 );
7866 TEST_EQUAL( p[2], 1 );
7867 }
7868 else
7869 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7870 }
7871
7872exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007873 /*
7874 * Key attributes may have been returned by psa_get_key_attributes() or
7875 * set by psa_set_key_domain_parameters() thus reset them as required.
7876 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007877 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007878
Ronald Cron5425a212020-08-04 14:58:35 +02007879 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007880 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007881 mbedtls_free( e_read_buffer );
7882 mbedtls_free( exported );
7883}
7884/* END_CASE */
7885
Darryl Greend49a4992018-06-18 17:27:26 +01007886/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007887void persistent_key_load_key_from_storage( data_t *data,
7888 int type_arg, int bits_arg,
7889 int usage_flags_arg, int alg_arg,
7890 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007891{
Ronald Cron71016a92020-08-28 19:01:50 +02007892 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007894 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7895 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007896 psa_key_type_t type = type_arg;
7897 size_t bits = bits_arg;
7898 psa_key_usage_t usage_flags = usage_flags_arg;
7899 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007900 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007901 unsigned char *first_export = NULL;
7902 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007903 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007904 size_t first_exported_length;
7905 size_t second_exported_length;
7906
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007907 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7908 {
7909 ASSERT_ALLOC( first_export, export_size );
7910 ASSERT_ALLOC( second_export, export_size );
7911 }
Darryl Greend49a4992018-06-18 17:27:26 +01007912
Gilles Peskine8817f612018-12-18 00:18:46 +01007913 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007914
Gilles Peskinec87af662019-05-15 16:12:22 +02007915 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007916 psa_set_key_usage_flags( &attributes, usage_flags );
7917 psa_set_key_algorithm( &attributes, alg );
7918 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007919 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007920
Darryl Green0c6575a2018-11-07 16:05:30 +00007921 switch( generation_method )
7922 {
7923 case IMPORT_KEY:
7924 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007925 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007926 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007927 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007928
Darryl Green0c6575a2018-11-07 16:05:30 +00007929 case GENERATE_KEY:
7930 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007931 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007932 break;
7933
7934 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007935#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007936 {
7937 /* Create base key */
7938 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7939 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7940 psa_set_key_usage_flags( &base_attributes,
7941 PSA_KEY_USAGE_DERIVE );
7942 psa_set_key_algorithm( &base_attributes, derive_alg );
7943 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007944 PSA_ASSERT( psa_import_key( &base_attributes,
7945 data->x, data->len,
7946 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007947 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007948 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007949 PSA_ASSERT( psa_key_derivation_input_key(
7950 &operation,
7951 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007952 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007953 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007954 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007955 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7956 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007957 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007958 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007959 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007960 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007961 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007962#else
7963 TEST_ASSUME( ! "KDF not supported in this configuration" );
7964#endif
7965 break;
7966
7967 default:
7968 TEST_ASSERT( ! "generation_method not implemented in test" );
7969 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007970 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007971 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007972
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007973 /* Export the key if permitted by the key policy. */
7974 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7975 {
Ronald Cron5425a212020-08-04 14:58:35 +02007976 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007977 first_export, export_size,
7978 &first_exported_length ) );
7979 if( generation_method == IMPORT_KEY )
7980 ASSERT_COMPARE( data->x, data->len,
7981 first_export, first_exported_length );
7982 }
Darryl Greend49a4992018-06-18 17:27:26 +01007983
7984 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007985 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007986 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007987 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007988
Darryl Greend49a4992018-06-18 17:27:26 +01007989 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007990 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007991 TEST_ASSERT( mbedtls_svc_key_id_equal(
7992 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007993 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7994 PSA_KEY_LIFETIME_PERSISTENT );
7995 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7996 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007997 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007998 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007999 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008000
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008001 /* Export the key again if permitted by the key policy. */
8002 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008003 {
Ronald Cron5425a212020-08-04 14:58:35 +02008004 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008005 second_export, export_size,
8006 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008007 ASSERT_COMPARE( first_export, first_exported_length,
8008 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008009 }
8010
8011 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008012 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008013 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008014
8015exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008016 /*
8017 * Key attributes may have been returned by psa_get_key_attributes()
8018 * thus reset them as required.
8019 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008020 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008021
Darryl Greend49a4992018-06-18 17:27:26 +01008022 mbedtls_free( first_export );
8023 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008024 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008025 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008026 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008027 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008028}
8029/* END_CASE */