blob: f67230d01d2251e3ae98d2ac30336585e869e9fd [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 */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003307void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3308 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003309{
3310 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3311 psa_key_type_t key_type = key_type_arg;
3312 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003313 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3314 uint8_t iv[1] = { 0x5a };
3315 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003316 unsigned char *output = NULL;
3317 size_t output_buffer_size = 0;
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003318 size_t output_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3320
3321 PSA_ASSERT( psa_crypto_init( ) );
3322
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003323 /* Validate size macros */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003324 TEST_ASSERT( ciphertext->len <=
3325 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3326 TEST_ASSERT( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) <=
3327 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
3328 TEST_ASSERT( plaintext->len <=
3329 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3330 TEST_ASSERT( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) <=
3331 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
3332
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003333
3334 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003335 psa_set_key_usage_flags( &attributes,
3336 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003337 psa_set_key_algorithm( &attributes, alg );
3338 psa_set_key_type( &attributes, key_type );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003339 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3340 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003341 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3342 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003343 ASSERT_ALLOC( output, output_buffer_size );
3344
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003345 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003346 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3347 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3348 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003349 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3350 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3351 PSA_ERROR_BAD_STATE );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003352
3353 /* generate_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003354 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3355 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
3356 &iv_length ),
3357 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003358 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3359 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
3360 &iv_length ),
3361 PSA_ERROR_BAD_STATE );
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003362
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003363 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003364 output_length = ~0;
3365 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
3366 output, output_buffer_size,
3367 &output_length ) );
3368 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
3369 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003370
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003371 /* One-shot decryption */
3372 output_length = ~0;
3373 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
3374 output, output_buffer_size,
3375 &output_length ) );
3376 ASSERT_COMPARE( plaintext->x, plaintext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003377 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003378
3379 /* Encrypt, multi-part */
3380 PSA_ASSERT( psa_cipher_abort( &operation ) );
3381 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3382
3383 PSA_ASSERT( psa_cipher_update( &operation, input->x, input->len,
3384 output, output_buffer_size,
3385 &output_length) );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003386
3387 ASSERT_COMPARE( expected_output->x, expected_output->len,
3388 output, output_length );
3389
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003390exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003391 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003392 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003393 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003394 psa_destroy_key( key );
3395 PSA_DONE( );
3396}
3397/* END_CASE */
3398
3399/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003400void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3401{
3402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3403 psa_algorithm_t alg = alg_arg;
3404 psa_key_type_t key_type = key_type_arg;
3405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3406 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3407 psa_status_t status;
3408
3409 PSA_ASSERT( psa_crypto_init( ) );
3410
3411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3412 psa_set_key_algorithm( &attributes, alg );
3413 psa_set_key_type( &attributes, key_type );
3414
3415 /* Usage of either of these two size macros would cause divide by zero
3416 * with incorrect key types previously. Input length should be irrelevant
3417 * here. */
3418 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3419 0 );
3420 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3421
3422
3423 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3424 &key ) );
3425
3426 /* Should fail due to invalid alg type (to support invalid key type).
3427 * Encrypt or decrypt will end up in the same place. */
3428 status = psa_cipher_encrypt_setup( &operation, key, alg );
3429
3430 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3431
3432exit:
3433 psa_cipher_abort( &operation );
3434 psa_destroy_key( key );
3435 PSA_DONE( );
3436}
3437/* END_CASE */
3438
3439/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003440void cipher_encrypt_validation( int alg_arg,
3441 int key_type_arg,
3442 data_t *key_data,
3443 data_t *input )
3444{
3445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3446 psa_key_type_t key_type = key_type_arg;
3447 psa_algorithm_t alg = alg_arg;
3448 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3449 unsigned char *output1 = NULL;
3450 size_t output1_buffer_size = 0;
3451 size_t output1_length = 0;
3452 unsigned char *output2 = NULL;
3453 size_t output2_buffer_size = 0;
3454 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003455 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003456 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003457 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003458
Gilles Peskine8817f612018-12-18 00:18:46 +01003459 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003460
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003461 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3462 psa_set_key_algorithm( &attributes, alg );
3463 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003464
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003465 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3466 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3467 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3468 ASSERT_ALLOC( output1, output1_buffer_size );
3469 ASSERT_ALLOC( output2, output2_buffer_size );
3470
Ronald Cron5425a212020-08-04 14:58:35 +02003471 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3472 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003473
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003474 /* The one-shot cipher encryption uses generated iv so validating
3475 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003476 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3477 output1_buffer_size, &output1_length ) );
3478 TEST_ASSERT( output1_length <=
3479 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3480 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003481 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003482
3483 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3484 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003485
Gilles Peskine8817f612018-12-18 00:18:46 +01003486 PSA_ASSERT( psa_cipher_update( &operation,
3487 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003488 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003489 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003490 TEST_ASSERT( function_output_length <=
3491 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3492 TEST_ASSERT( function_output_length <=
3493 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003494 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003495
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003496 PSA_ASSERT( psa_cipher_finish( &operation,
3497 output2 + output2_length,
3498 output2_buffer_size - output2_length,
3499 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003500 TEST_ASSERT( function_output_length <=
3501 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3502 TEST_ASSERT( function_output_length <=
3503 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003504 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003505
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003506 PSA_ASSERT( psa_cipher_abort( &operation ) );
3507 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3508 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003509
Gilles Peskine50e586b2018-06-08 14:28:46 +02003510exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003511 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003512 mbedtls_free( output1 );
3513 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003514 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003515 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003516}
3517/* END_CASE */
3518
3519/* BEGIN_CASE */
3520void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003521 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003522 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003523 int first_part_size_arg,
3524 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003525 data_t *expected_output,
3526 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003527{
Ronald Cron5425a212020-08-04 14:58:35 +02003528 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003529 psa_key_type_t key_type = key_type_arg;
3530 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003531 psa_status_t status;
3532 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003533 size_t first_part_size = first_part_size_arg;
3534 size_t output1_length = output1_length_arg;
3535 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003536 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003537 size_t output_buffer_size = 0;
3538 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003539 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003540 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003541 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003542
Gilles Peskine8817f612018-12-18 00:18:46 +01003543 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003544
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003545 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3546 psa_set_key_algorithm( &attributes, alg );
3547 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003548
Ronald Cron5425a212020-08-04 14:58:35 +02003549 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3550 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003551
Ronald Cron5425a212020-08-04 14:58:35 +02003552 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003553
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003554 if( iv->len > 0 )
3555 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003556 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003557 }
3558
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003559 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3560 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003561 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003562
Gilles Peskinee0866522019-02-19 19:44:00 +01003563 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003564 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3565 output, output_buffer_size,
3566 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003567 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003568 TEST_ASSERT( function_output_length <=
3569 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3570 TEST_ASSERT( function_output_length <=
3571 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003572 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003573
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003574 if( first_part_size < input->len )
3575 {
3576 PSA_ASSERT( psa_cipher_update( &operation,
3577 input->x + first_part_size,
3578 input->len - first_part_size,
3579 ( output_buffer_size == 0 ? NULL :
3580 output + total_output_length ),
3581 output_buffer_size - total_output_length,
3582 &function_output_length ) );
3583 TEST_ASSERT( function_output_length == output2_length );
3584 TEST_ASSERT( function_output_length <=
3585 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3586 alg,
3587 input->len - first_part_size ) );
3588 TEST_ASSERT( function_output_length <=
3589 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3590 total_output_length += function_output_length;
3591 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003592
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003593 status = psa_cipher_finish( &operation,
3594 ( output_buffer_size == 0 ? NULL :
3595 output + total_output_length ),
3596 output_buffer_size - total_output_length,
3597 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003598 TEST_ASSERT( function_output_length <=
3599 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3600 TEST_ASSERT( function_output_length <=
3601 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003602 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003603 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003604
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003605 if( expected_status == PSA_SUCCESS )
3606 {
3607 PSA_ASSERT( psa_cipher_abort( &operation ) );
3608
3609 ASSERT_COMPARE( expected_output->x, expected_output->len,
3610 output, total_output_length );
3611 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003612
3613exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003614 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003615 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003616 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003617 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003618}
3619/* END_CASE */
3620
3621/* BEGIN_CASE */
3622void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003623 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003624 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003625 int first_part_size_arg,
3626 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003627 data_t *expected_output,
3628 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003629{
Ronald Cron5425a212020-08-04 14:58:35 +02003630 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003631 psa_key_type_t key_type = key_type_arg;
3632 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003633 psa_status_t status;
3634 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003635 size_t first_part_size = first_part_size_arg;
3636 size_t output1_length = output1_length_arg;
3637 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003638 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003639 size_t output_buffer_size = 0;
3640 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003641 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003642 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003643 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003644
Gilles Peskine8817f612018-12-18 00:18:46 +01003645 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003646
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003647 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3648 psa_set_key_algorithm( &attributes, alg );
3649 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003650
Ronald Cron5425a212020-08-04 14:58:35 +02003651 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3652 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003653
Ronald Cron5425a212020-08-04 14:58:35 +02003654 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003655
Steven Cooreman177deba2020-09-07 17:14:14 +02003656 if( iv->len > 0 )
3657 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003658 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003659 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003660
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003661 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3662 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003663 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003664
Gilles Peskinee0866522019-02-19 19:44:00 +01003665 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003666 PSA_ASSERT( psa_cipher_update( &operation,
3667 input->x, first_part_size,
3668 output, output_buffer_size,
3669 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003670 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003671 TEST_ASSERT( function_output_length <=
3672 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3673 TEST_ASSERT( function_output_length <=
3674 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003675 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003676
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003677 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003678 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003679 PSA_ASSERT( psa_cipher_update( &operation,
3680 input->x + first_part_size,
3681 input->len - first_part_size,
3682 ( output_buffer_size == 0 ? NULL :
3683 output + total_output_length ),
3684 output_buffer_size - total_output_length,
3685 &function_output_length ) );
3686 TEST_ASSERT( function_output_length == output2_length );
3687 TEST_ASSERT( function_output_length <=
3688 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3689 alg,
3690 input->len - first_part_size ) );
3691 TEST_ASSERT( function_output_length <=
3692 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3693 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003694 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003695
Gilles Peskine50e586b2018-06-08 14:28:46 +02003696 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003697 ( output_buffer_size == 0 ? NULL :
3698 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003699 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003700 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003701 TEST_ASSERT( function_output_length <=
3702 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3703 TEST_ASSERT( function_output_length <=
3704 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003705 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003706 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003707
3708 if( expected_status == PSA_SUCCESS )
3709 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003710 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003711
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003712 ASSERT_COMPARE( expected_output->x, expected_output->len,
3713 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003714 }
3715
Gilles Peskine50e586b2018-06-08 14:28:46 +02003716exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003717 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003718 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003719 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003720 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003721}
3722/* END_CASE */
3723
Gilles Peskine50e586b2018-06-08 14:28:46 +02003724/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003725void cipher_decrypt_fail( int alg_arg,
3726 int key_type_arg,
3727 data_t *key_data,
3728 data_t *iv,
3729 data_t *input_arg,
3730 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003731{
3732 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3733 psa_status_t status;
3734 psa_key_type_t key_type = key_type_arg;
3735 psa_algorithm_t alg = alg_arg;
3736 psa_status_t expected_status = expected_status_arg;
3737 unsigned char *input = NULL;
3738 size_t input_buffer_size = 0;
3739 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01003740 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003741 size_t output_buffer_size = 0;
3742 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01003743 size_t function_output_length;
3744 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3746
3747 if ( PSA_ERROR_BAD_STATE != expected_status )
3748 {
3749 PSA_ASSERT( psa_crypto_init( ) );
3750
3751 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3752 psa_set_key_algorithm( &attributes, alg );
3753 psa_set_key_type( &attributes, key_type );
3754
3755 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3756 &key ) );
3757 }
3758
3759 /* Allocate input buffer and copy the iv and the plaintext */
3760 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3761 if ( input_buffer_size > 0 )
3762 {
3763 ASSERT_ALLOC( input, input_buffer_size );
3764 memcpy( input, iv->x, iv->len );
3765 memcpy( input + iv->len, input_arg->x, input_arg->len );
3766 }
3767
3768 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3769 ASSERT_ALLOC( output, output_buffer_size );
3770
Neil Armstrong66a479f2022-02-07 15:41:19 +01003771 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003772 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3773 output_buffer_size, &output_length );
3774 TEST_EQUAL( status, expected_status );
3775
Neil Armstrong66a479f2022-02-07 15:41:19 +01003776 /* Decrypt, multi-part */
3777 status = psa_cipher_decrypt_setup( &operation, key, alg );
3778 if( status == PSA_SUCCESS )
3779 {
3780 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
3781 input_arg->len ) +
3782 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3783 ASSERT_ALLOC( output_multi, output_buffer_size );
3784
3785 if( iv->len > 0 )
3786 {
3787 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
3788
3789 if( status != PSA_SUCCESS )
3790 TEST_EQUAL( status, expected_status );
3791 }
3792
3793 if( status == PSA_SUCCESS )
3794 {
3795 status = psa_cipher_update( &operation,
3796 input_arg->x, input_arg->len,
3797 output_multi, output_buffer_size,
3798 &function_output_length );
3799 if( status == PSA_SUCCESS )
3800 {
3801 output_length = function_output_length;
3802
3803 status = psa_cipher_finish( &operation,
3804 output_multi + output_length,
3805 output_buffer_size - output_length,
3806 &function_output_length );
3807
3808 TEST_EQUAL( status, expected_status );
3809 }
3810 else
3811 {
3812 TEST_EQUAL( status, expected_status );
3813 }
3814 }
3815 else
3816 {
3817 TEST_EQUAL( status, expected_status );
3818 }
3819 }
3820 else
3821 {
3822 TEST_EQUAL( status, expected_status );
3823 }
3824
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003825exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01003826 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003827 mbedtls_free( input );
3828 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01003829 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003830 psa_destroy_key( key );
3831 PSA_DONE( );
3832}
3833/* END_CASE */
3834
3835/* BEGIN_CASE */
3836void cipher_decrypt( int alg_arg,
3837 int key_type_arg,
3838 data_t *key_data,
3839 data_t *iv,
3840 data_t *input_arg,
3841 data_t *expected_output )
3842{
3843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3844 psa_key_type_t key_type = key_type_arg;
3845 psa_algorithm_t alg = alg_arg;
3846 unsigned char *input = NULL;
3847 size_t input_buffer_size = 0;
3848 unsigned char *output = NULL;
3849 size_t output_buffer_size = 0;
3850 size_t output_length = 0;
3851 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3852
3853 PSA_ASSERT( psa_crypto_init( ) );
3854
3855 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3856 psa_set_key_algorithm( &attributes, alg );
3857 psa_set_key_type( &attributes, key_type );
3858
3859 /* Allocate input buffer and copy the iv and the plaintext */
3860 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3861 if ( input_buffer_size > 0 )
3862 {
3863 ASSERT_ALLOC( input, input_buffer_size );
3864 memcpy( input, iv->x, iv->len );
3865 memcpy( input + iv->len, input_arg->x, input_arg->len );
3866 }
3867
3868 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3869 ASSERT_ALLOC( output, output_buffer_size );
3870
3871 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3872 &key ) );
3873
3874 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3875 output_buffer_size, &output_length ) );
3876 TEST_ASSERT( output_length <=
3877 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3878 TEST_ASSERT( output_length <=
3879 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3880
3881 ASSERT_COMPARE( expected_output->x, expected_output->len,
3882 output, output_length );
3883exit:
3884 mbedtls_free( input );
3885 mbedtls_free( output );
3886 psa_destroy_key( key );
3887 PSA_DONE( );
3888}
3889/* END_CASE */
3890
3891/* BEGIN_CASE */
3892void cipher_verify_output( int alg_arg,
3893 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003894 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003895 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003896{
Ronald Cron5425a212020-08-04 14:58:35 +02003897 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003898 psa_key_type_t key_type = key_type_arg;
3899 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003900 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003901 size_t output1_size = 0;
3902 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003903 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003904 size_t output2_size = 0;
3905 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003906 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003907
Gilles Peskine8817f612018-12-18 00:18:46 +01003908 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003909
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003910 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3911 psa_set_key_algorithm( &attributes, alg );
3912 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003913
Ronald Cron5425a212020-08-04 14:58:35 +02003914 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3915 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003916 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003917 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003918
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003919 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3920 output1, output1_size,
3921 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003922 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003923 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003924 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003925 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003926
3927 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003928 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003929
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003930 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3931 output2, output2_size,
3932 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003933 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003934 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003935 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003936 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003937
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003938 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003939
3940exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003941 mbedtls_free( output1 );
3942 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003943 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003944 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003945}
3946/* END_CASE */
3947
3948/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003949void cipher_verify_output_multipart( int alg_arg,
3950 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003951 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003952 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003953 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003954{
Ronald Cron5425a212020-08-04 14:58:35 +02003955 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003956 psa_key_type_t key_type = key_type_arg;
3957 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003958 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003959 unsigned char iv[16] = {0};
3960 size_t iv_size = 16;
3961 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003962 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003963 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003964 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003965 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003966 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003967 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003968 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003969 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3970 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003971 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003972
Gilles Peskine8817f612018-12-18 00:18:46 +01003973 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003974
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003975 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3976 psa_set_key_algorithm( &attributes, alg );
3977 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003978
Ronald Cron5425a212020-08-04 14:58:35 +02003979 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3980 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003981
Ronald Cron5425a212020-08-04 14:58:35 +02003982 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3983 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003984
Steven Cooreman177deba2020-09-07 17:14:14 +02003985 if( alg != PSA_ALG_ECB_NO_PADDING )
3986 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003987 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3988 iv, iv_size,
3989 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003990 }
3991
gabor-mezei-armceface22021-01-21 12:26:17 +01003992 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3993 TEST_ASSERT( output1_buffer_size <=
3994 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003995 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003996
Gilles Peskinee0866522019-02-19 19:44:00 +01003997 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003998
Gilles Peskine8817f612018-12-18 00:18:46 +01003999 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4000 output1, output1_buffer_size,
4001 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004002 TEST_ASSERT( function_output_length <=
4003 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4004 TEST_ASSERT( function_output_length <=
4005 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004006 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004007
Gilles Peskine8817f612018-12-18 00:18:46 +01004008 PSA_ASSERT( psa_cipher_update( &operation1,
4009 input->x + first_part_size,
4010 input->len - first_part_size,
4011 output1, output1_buffer_size,
4012 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004013 TEST_ASSERT( function_output_length <=
4014 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4015 alg,
4016 input->len - first_part_size ) );
4017 TEST_ASSERT( function_output_length <=
4018 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004019 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004020
Gilles Peskine8817f612018-12-18 00:18:46 +01004021 PSA_ASSERT( psa_cipher_finish( &operation1,
4022 output1 + output1_length,
4023 output1_buffer_size - output1_length,
4024 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004025 TEST_ASSERT( function_output_length <=
4026 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4027 TEST_ASSERT( function_output_length <=
4028 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004029 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004030
Gilles Peskine8817f612018-12-18 00:18:46 +01004031 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004032
Gilles Peskine048b7f02018-06-08 14:20:49 +02004033 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004034 TEST_ASSERT( output2_buffer_size <=
4035 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4036 TEST_ASSERT( output2_buffer_size <=
4037 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004038 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004039
Steven Cooreman177deba2020-09-07 17:14:14 +02004040 if( iv_length > 0 )
4041 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004042 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4043 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004044 }
Moran Pekerded84402018-06-06 16:36:50 +03004045
Gilles Peskine8817f612018-12-18 00:18:46 +01004046 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4047 output2, output2_buffer_size,
4048 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004049 TEST_ASSERT( function_output_length <=
4050 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4051 TEST_ASSERT( function_output_length <=
4052 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004053 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004054
Gilles Peskine8817f612018-12-18 00:18:46 +01004055 PSA_ASSERT( psa_cipher_update( &operation2,
4056 output1 + first_part_size,
4057 output1_length - first_part_size,
4058 output2, output2_buffer_size,
4059 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004060 TEST_ASSERT( function_output_length <=
4061 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4062 alg,
4063 output1_length - first_part_size ) );
4064 TEST_ASSERT( function_output_length <=
4065 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004066 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004067
Gilles Peskine8817f612018-12-18 00:18:46 +01004068 PSA_ASSERT( psa_cipher_finish( &operation2,
4069 output2 + output2_length,
4070 output2_buffer_size - output2_length,
4071 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004072 TEST_ASSERT( function_output_length <=
4073 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4074 TEST_ASSERT( function_output_length <=
4075 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004076 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004077
Gilles Peskine8817f612018-12-18 00:18:46 +01004078 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004079
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004080 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004081
4082exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004083 psa_cipher_abort( &operation1 );
4084 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004085 mbedtls_free( output1 );
4086 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004087 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004088 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004089}
4090/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004091
Gilles Peskine20035e32018-02-03 22:44:14 +01004092/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004093void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004094 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004095 data_t *nonce,
4096 data_t *additional_data,
4097 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004098 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004099{
Ronald Cron5425a212020-08-04 14:58:35 +02004100 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004101 psa_key_type_t key_type = key_type_arg;
4102 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004103 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004104 unsigned char *output_data = NULL;
4105 size_t output_size = 0;
4106 size_t output_length = 0;
4107 unsigned char *output_data2 = NULL;
4108 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004109 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004110 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004112
Gilles Peskine8817f612018-12-18 00:18:46 +01004113 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004114
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004115 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4116 psa_set_key_algorithm( &attributes, alg );
4117 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004118
Gilles Peskine049c7532019-05-15 20:22:09 +02004119 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004120 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004121 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4122 key_bits = psa_get_key_bits( &attributes );
4123
4124 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4125 alg );
4126 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4127 * should be exact. */
4128 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4129 expected_result != PSA_ERROR_NOT_SUPPORTED )
4130 {
4131 TEST_EQUAL( output_size,
4132 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4133 TEST_ASSERT( output_size <=
4134 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4135 }
4136 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004137
Steven Cooremanf49478b2021-02-15 15:19:25 +01004138 status = psa_aead_encrypt( key, alg,
4139 nonce->x, nonce->len,
4140 additional_data->x,
4141 additional_data->len,
4142 input_data->x, input_data->len,
4143 output_data, output_size,
4144 &output_length );
4145
4146 /* If the operation is not supported, just skip and not fail in case the
4147 * encryption involves a common limitation of cryptography hardwares and
4148 * an alternative implementation. */
4149 if( status == PSA_ERROR_NOT_SUPPORTED )
4150 {
4151 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4152 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4153 }
4154
4155 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004156
4157 if( PSA_SUCCESS == expected_result )
4158 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004159 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004160
Gilles Peskine003a4a92019-05-14 16:09:40 +02004161 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4162 * should be exact. */
4163 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004164 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004165
gabor-mezei-armceface22021-01-21 12:26:17 +01004166 TEST_ASSERT( input_data->len <=
4167 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
4168
Ronald Cron5425a212020-08-04 14:58:35 +02004169 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004170 nonce->x, nonce->len,
4171 additional_data->x,
4172 additional_data->len,
4173 output_data, output_length,
4174 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004175 &output_length2 ),
4176 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004177
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004178 ASSERT_COMPARE( input_data->x, input_data->len,
4179 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004180 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004181
Gilles Peskinea1cac842018-06-11 19:33:02 +02004182exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004183 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004184 mbedtls_free( output_data );
4185 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004186 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004187}
4188/* END_CASE */
4189
4190/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004191void aead_encrypt( int key_type_arg, data_t *key_data,
4192 int alg_arg,
4193 data_t *nonce,
4194 data_t *additional_data,
4195 data_t *input_data,
4196 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004197{
Ronald Cron5425a212020-08-04 14:58:35 +02004198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004199 psa_key_type_t key_type = key_type_arg;
4200 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004201 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004202 unsigned char *output_data = NULL;
4203 size_t output_size = 0;
4204 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004206 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004207
Gilles Peskine8817f612018-12-18 00:18:46 +01004208 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004209
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004210 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4211 psa_set_key_algorithm( &attributes, alg );
4212 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004213
Gilles Peskine049c7532019-05-15 20:22:09 +02004214 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004215 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004216 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4217 key_bits = psa_get_key_bits( &attributes );
4218
4219 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4220 alg );
4221 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4222 * should be exact. */
4223 TEST_EQUAL( output_size,
4224 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4225 TEST_ASSERT( output_size <=
4226 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4227 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004228
Steven Cooremand588ea12021-01-11 19:36:04 +01004229 status = psa_aead_encrypt( key, alg,
4230 nonce->x, nonce->len,
4231 additional_data->x, additional_data->len,
4232 input_data->x, input_data->len,
4233 output_data, output_size,
4234 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004235
Ronald Cron28a45ed2021-02-09 20:35:42 +01004236 /* If the operation is not supported, just skip and not fail in case the
4237 * encryption involves a common limitation of cryptography hardwares and
4238 * an alternative implementation. */
4239 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004240 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004241 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4242 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004243 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004244
4245 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004246 ASSERT_COMPARE( expected_result->x, expected_result->len,
4247 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004248
Gilles Peskinea1cac842018-06-11 19:33:02 +02004249exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004250 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004251 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004252 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004253}
4254/* END_CASE */
4255
4256/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004257void aead_decrypt( int key_type_arg, data_t *key_data,
4258 int alg_arg,
4259 data_t *nonce,
4260 data_t *additional_data,
4261 data_t *input_data,
4262 data_t *expected_data,
4263 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004264{
Ronald Cron5425a212020-08-04 14:58:35 +02004265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004266 psa_key_type_t key_type = key_type_arg;
4267 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004268 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004269 unsigned char *output_data = NULL;
4270 size_t output_size = 0;
4271 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004273 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004274 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004275
Gilles Peskine8817f612018-12-18 00:18:46 +01004276 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004277
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004278 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4279 psa_set_key_algorithm( &attributes, alg );
4280 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004281
Gilles Peskine049c7532019-05-15 20:22:09 +02004282 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004283 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004284 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4285 key_bits = psa_get_key_bits( &attributes );
4286
4287 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4288 alg );
4289 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4290 expected_result != PSA_ERROR_NOT_SUPPORTED )
4291 {
4292 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4293 * should be exact. */
4294 TEST_EQUAL( output_size,
4295 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4296 TEST_ASSERT( output_size <=
4297 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4298 }
4299 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004300
Steven Cooremand588ea12021-01-11 19:36:04 +01004301 status = psa_aead_decrypt( key, alg,
4302 nonce->x, nonce->len,
4303 additional_data->x,
4304 additional_data->len,
4305 input_data->x, input_data->len,
4306 output_data, output_size,
4307 &output_length );
4308
Ronald Cron28a45ed2021-02-09 20:35:42 +01004309 /* If the operation is not supported, just skip and not fail in case the
4310 * decryption involves a common limitation of cryptography hardwares and
4311 * an alternative implementation. */
4312 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004313 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004314 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4315 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004316 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004317
4318 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004319
Gilles Peskine2d277862018-06-18 15:41:12 +02004320 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004321 ASSERT_COMPARE( expected_data->x, expected_data->len,
4322 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004323
Gilles Peskinea1cac842018-06-11 19:33:02 +02004324exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004325 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004326 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004327 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004328}
4329/* END_CASE */
4330
4331/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004332void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4333 int alg_arg,
4334 data_t *nonce,
4335 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004336 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004337 int do_set_lengths,
4338 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004339{
Paul Elliottd3f82412021-06-16 16:52:21 +01004340 size_t ad_part_len = 0;
4341 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004342 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004343
Paul Elliott32f46ba2021-09-23 18:24:36 +01004344 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004345 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004346 mbedtls_test_set_step( ad_part_len );
4347
4348 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004349 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004350 if( ad_part_len & 0x01 )
4351 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4352 else
4353 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004354 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004355
4356 /* Split ad into length(ad_part_len) parts. */
4357 if( !aead_multipart_internal_func( key_type_arg, key_data,
4358 alg_arg, nonce,
4359 additional_data,
4360 ad_part_len,
4361 input_data, -1,
4362 set_lengths_method,
4363 expected_output,
4364 1, 0 ) )
4365 break;
4366
4367 /* length(0) part, length(ad_part_len) part, length(0) part... */
4368 mbedtls_test_set_step( 1000 + ad_part_len );
4369
4370 if( !aead_multipart_internal_func( key_type_arg, key_data,
4371 alg_arg, nonce,
4372 additional_data,
4373 ad_part_len,
4374 input_data, -1,
4375 set_lengths_method,
4376 expected_output,
4377 1, 1 ) )
4378 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004379 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004380
Paul Elliott32f46ba2021-09-23 18:24:36 +01004381 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004382 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004383 /* Split data into length(data_part_len) parts. */
4384 mbedtls_test_set_step( 2000 + data_part_len );
4385
4386 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004387 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004388 if( data_part_len & 0x01 )
4389 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4390 else
4391 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004392 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004393
Paul Elliott32f46ba2021-09-23 18:24:36 +01004394 if( !aead_multipart_internal_func( key_type_arg, key_data,
4395 alg_arg, nonce,
4396 additional_data, -1,
4397 input_data, data_part_len,
4398 set_lengths_method,
4399 expected_output,
4400 1, 0 ) )
4401 break;
4402
4403 /* length(0) part, length(data_part_len) part, length(0) part... */
4404 mbedtls_test_set_step( 3000 + data_part_len );
4405
4406 if( !aead_multipart_internal_func( key_type_arg, key_data,
4407 alg_arg, nonce,
4408 additional_data, -1,
4409 input_data, data_part_len,
4410 set_lengths_method,
4411 expected_output,
4412 1, 1 ) )
4413 break;
4414 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004415
Paul Elliott8fc45162021-06-23 16:06:01 +01004416 /* Goto is required to silence warnings about unused labels, as we
4417 * don't actually do any test assertions in this function. */
4418 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004419}
4420/* END_CASE */
4421
4422/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004423void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4424 int alg_arg,
4425 data_t *nonce,
4426 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004427 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004428 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004429 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004430{
Paul Elliottd3f82412021-06-16 16:52:21 +01004431 size_t ad_part_len = 0;
4432 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004433 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004434
Paul Elliott32f46ba2021-09-23 18:24:36 +01004435 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004436 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004437 /* Split ad into length(ad_part_len) parts. */
4438 mbedtls_test_set_step( ad_part_len );
4439
4440 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004441 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004442 if( ad_part_len & 0x01 )
4443 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4444 else
4445 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004446 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004447
4448 if( !aead_multipart_internal_func( key_type_arg, key_data,
4449 alg_arg, nonce,
4450 additional_data,
4451 ad_part_len,
4452 input_data, -1,
4453 set_lengths_method,
4454 expected_output,
4455 0, 0 ) )
4456 break;
4457
4458 /* length(0) part, length(ad_part_len) part, length(0) part... */
4459 mbedtls_test_set_step( 1000 + ad_part_len );
4460
4461 if( !aead_multipart_internal_func( key_type_arg, key_data,
4462 alg_arg, nonce,
4463 additional_data,
4464 ad_part_len,
4465 input_data, -1,
4466 set_lengths_method,
4467 expected_output,
4468 0, 1 ) )
4469 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004470 }
4471
Paul Elliott32f46ba2021-09-23 18:24:36 +01004472 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004473 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004474 /* Split data into length(data_part_len) parts. */
4475 mbedtls_test_set_step( 2000 + data_part_len );
4476
4477 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004478 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004479 if( data_part_len & 0x01 )
4480 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4481 else
4482 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004483 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004484
4485 if( !aead_multipart_internal_func( key_type_arg, key_data,
4486 alg_arg, nonce,
4487 additional_data, -1,
4488 input_data, data_part_len,
4489 set_lengths_method,
4490 expected_output,
4491 0, 0 ) )
4492 break;
4493
4494 /* length(0) part, length(data_part_len) part, length(0) part... */
4495 mbedtls_test_set_step( 3000 + data_part_len );
4496
4497 if( !aead_multipart_internal_func( key_type_arg, key_data,
4498 alg_arg, nonce,
4499 additional_data, -1,
4500 input_data, data_part_len,
4501 set_lengths_method,
4502 expected_output,
4503 0, 1 ) )
4504 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004505 }
4506
Paul Elliott8fc45162021-06-23 16:06:01 +01004507 /* Goto is required to silence warnings about unused labels, as we
4508 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004509 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004510}
4511/* END_CASE */
4512
4513/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004514void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4515 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004516 int nonce_length,
4517 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004518 data_t *additional_data,
4519 data_t *input_data,
4520 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004521{
4522
4523 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4524 psa_key_type_t key_type = key_type_arg;
4525 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004526 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004527 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4529 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004530 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004531 size_t actual_nonce_length = 0;
4532 size_t expected_nonce_length = expected_nonce_length_arg;
4533 unsigned char *output = NULL;
4534 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004535 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004536 size_t ciphertext_size = 0;
4537 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004538 size_t tag_length = 0;
4539 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004540
4541 PSA_ASSERT( psa_crypto_init( ) );
4542
4543 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4544 psa_set_key_algorithm( & attributes, alg );
4545 psa_set_key_type( & attributes, key_type );
4546
4547 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4548 &key ) );
4549
4550 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4551
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004552 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4553
Paul Elliottf1277632021-08-24 18:11:37 +01004554 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004555
Paul Elliottf1277632021-08-24 18:11:37 +01004556 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004557
Paul Elliottf1277632021-08-24 18:11:37 +01004558 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004559
Paul Elliottf1277632021-08-24 18:11:37 +01004560 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004561
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004562 status = psa_aead_encrypt_setup( &operation, key, alg );
4563
4564 /* If the operation is not supported, just skip and not fail in case the
4565 * encryption involves a common limitation of cryptography hardwares and
4566 * an alternative implementation. */
4567 if( status == PSA_ERROR_NOT_SUPPORTED )
4568 {
4569 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004570 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004571 }
4572
4573 PSA_ASSERT( status );
4574
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004575 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004576 nonce_length,
4577 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004578
Paul Elliott693bf312021-07-23 17:40:41 +01004579 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004580
Paul Elliottf1277632021-08-24 18:11:37 +01004581 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004582
Paul Elliott88ecbe12021-09-22 17:23:03 +01004583 if( expected_status == PSA_SUCCESS )
4584 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4585 alg ) );
4586
Paul Elliottd79c5c52021-10-06 21:49:41 +01004587 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004588
Paul Elliott693bf312021-07-23 17:40:41 +01004589 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004590 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004591 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004592 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4593 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004594
4595 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4596 additional_data->len ) );
4597
4598 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004599 output, output_size,
4600 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004601
Paul Elliottf1277632021-08-24 18:11:37 +01004602 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4603 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004604 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4605 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004606
4607exit:
4608 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004609 mbedtls_free( output );
4610 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004611 psa_aead_abort( &operation );
4612 PSA_DONE( );
4613}
4614/* END_CASE */
4615
4616/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004617void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4618 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004619 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004620 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004621 data_t *additional_data,
4622 data_t *input_data,
4623 int expected_status_arg )
4624{
4625
4626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4627 psa_key_type_t key_type = key_type_arg;
4628 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004629 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004630 uint8_t *nonce_buffer = NULL;
4631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4632 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4633 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004634 unsigned char *output = NULL;
4635 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004636 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004637 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004638 size_t ciphertext_size = 0;
4639 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004640 size_t tag_length = 0;
4641 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004642 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004643 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004644
4645 PSA_ASSERT( psa_crypto_init( ) );
4646
4647 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4648 psa_set_key_algorithm( &attributes, alg );
4649 psa_set_key_type( &attributes, key_type );
4650
4651 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4652 &key ) );
4653
4654 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4655
4656 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4657
Paul Elliott6f0e7202021-08-25 12:57:18 +01004658 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004659
Paul Elliott6f0e7202021-08-25 12:57:18 +01004660 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004661
Paul Elliott6f0e7202021-08-25 12:57:18 +01004662 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004663
Paul Elliott6f0e7202021-08-25 12:57:18 +01004664 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004665
Paul Elliott863864a2021-07-23 17:28:31 +01004666 status = psa_aead_encrypt_setup( &operation, key, alg );
4667
4668 /* If the operation is not supported, just skip and not fail in case the
4669 * encryption involves a common limitation of cryptography hardwares and
4670 * an alternative implementation. */
4671 if( status == PSA_ERROR_NOT_SUPPORTED )
4672 {
4673 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004674 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004675 }
4676
4677 PSA_ASSERT( status );
4678
Paul Elliott4023ffd2021-09-10 16:21:22 +01004679 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4680 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004681 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004682 /* Arbitrary size buffer, to test zero length valid buffer. */
4683 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004684 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004685 }
4686 else
4687 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004688 /* If length is zero, then this will return NULL. */
4689 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004690 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004691
Paul Elliott4023ffd2021-09-10 16:21:22 +01004692 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004693 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004694 for( index = 0; index < nonce_length - 1; ++index )
4695 {
4696 nonce_buffer[index] = 'a' + index;
4697 }
Paul Elliott66696b52021-08-16 18:42:41 +01004698 }
Paul Elliott863864a2021-07-23 17:28:31 +01004699 }
4700
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004701 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4702 {
4703 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4704 input_data->len ) );
4705 }
4706
Paul Elliott6f0e7202021-08-25 12:57:18 +01004707 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004708
Paul Elliott693bf312021-07-23 17:40:41 +01004709 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004710
4711 if( expected_status == PSA_SUCCESS )
4712 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004713 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4714 {
4715 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4716 input_data->len ) );
4717 }
4718 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4719 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004720
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004721 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4722 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4723 additional_data->len ),
4724 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004725
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004726 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004727 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004728 &ciphertext_length ),
4729 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004730
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004731 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004732 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004733 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4734 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004735 }
4736
4737exit:
4738 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004739 mbedtls_free( output );
4740 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004741 mbedtls_free( nonce_buffer );
4742 psa_aead_abort( &operation );
4743 PSA_DONE( );
4744}
4745/* END_CASE */
4746
4747/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004748void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4749 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004750 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004751 data_t *nonce,
4752 data_t *additional_data,
4753 data_t *input_data,
4754 int expected_status_arg )
4755{
4756
4757 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4758 psa_key_type_t key_type = key_type_arg;
4759 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004760 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004761 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4762 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4763 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004764 unsigned char *output = NULL;
4765 unsigned char *ciphertext = NULL;
4766 size_t output_size = output_size_arg;
4767 size_t ciphertext_size = 0;
4768 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004769 size_t tag_length = 0;
4770 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4771
4772 PSA_ASSERT( psa_crypto_init( ) );
4773
4774 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4775 psa_set_key_algorithm( &attributes, alg );
4776 psa_set_key_type( &attributes, key_type );
4777
4778 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4779 &key ) );
4780
4781 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4782
Paul Elliottc6d11d02021-09-01 12:04:23 +01004783 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004784
Paul Elliottc6d11d02021-09-01 12:04:23 +01004785 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004786
Paul Elliottc6d11d02021-09-01 12:04:23 +01004787 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004788
Paul Elliott43fbda62021-07-23 18:30:59 +01004789 status = psa_aead_encrypt_setup( &operation, key, alg );
4790
4791 /* If the operation is not supported, just skip and not fail in case the
4792 * encryption involves a common limitation of cryptography hardwares and
4793 * an alternative implementation. */
4794 if( status == PSA_ERROR_NOT_SUPPORTED )
4795 {
4796 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4797 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4798 }
4799
4800 PSA_ASSERT( status );
4801
Paul Elliott47b9a142021-10-07 15:04:57 +01004802 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4803 input_data->len ) );
4804
Paul Elliott43fbda62021-07-23 18:30:59 +01004805 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4806
4807 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4808 additional_data->len ) );
4809
4810 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004811 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004812
4813 TEST_EQUAL( status, expected_status );
4814
4815 if( expected_status == PSA_SUCCESS )
4816 {
4817 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004818 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4819 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004820 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4821 }
4822
4823exit:
4824 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004825 mbedtls_free( output );
4826 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004827 psa_aead_abort( &operation );
4828 PSA_DONE( );
4829}
4830/* END_CASE */
4831
Paul Elliott91b021e2021-07-23 18:52:31 +01004832/* BEGIN_CASE */
4833void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4834 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004835 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004836 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004837 data_t *nonce,
4838 data_t *additional_data,
4839 data_t *input_data,
4840 int expected_status_arg )
4841{
4842
4843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4844 psa_key_type_t key_type = key_type_arg;
4845 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004846 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4848 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4849 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004850 unsigned char *ciphertext = NULL;
4851 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004852 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004853 size_t ciphertext_size = 0;
4854 size_t ciphertext_length = 0;
4855 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004856 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004857 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004858
4859 PSA_ASSERT( psa_crypto_init( ) );
4860
4861 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4862 psa_set_key_algorithm( &attributes, alg );
4863 psa_set_key_type( &attributes, key_type );
4864
4865 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4866 &key ) );
4867
4868 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4869
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004870 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004871
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004872 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004873
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004874 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004875
Paul Elliott719c1322021-09-13 18:27:22 +01004876 ASSERT_ALLOC( tag_buffer, tag_size );
4877
Paul Elliott91b021e2021-07-23 18:52:31 +01004878 status = psa_aead_encrypt_setup( &operation, key, alg );
4879
4880 /* If the operation is not supported, just skip and not fail in case the
4881 * encryption involves a common limitation of cryptography hardwares and
4882 * an alternative implementation. */
4883 if( status == PSA_ERROR_NOT_SUPPORTED )
4884 {
4885 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4886 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4887 }
4888
4889 PSA_ASSERT( status );
4890
4891 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4892
Paul Elliott76bda482021-10-07 17:07:23 +01004893 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4894 input_data->len ) );
4895
Paul Elliott91b021e2021-07-23 18:52:31 +01004896 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4897 additional_data->len ) );
4898
4899 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004900 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004901
4902 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004903 status = psa_aead_finish( &operation, finish_ciphertext,
4904 finish_ciphertext_size,
4905 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004906 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004907
4908 TEST_EQUAL( status, expected_status );
4909
4910exit:
4911 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004912 mbedtls_free( ciphertext );
4913 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004914 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004915 psa_aead_abort( &operation );
4916 PSA_DONE( );
4917}
4918/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004919
4920/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004921void aead_multipart_verify( int key_type_arg, data_t *key_data,
4922 int alg_arg,
4923 data_t *nonce,
4924 data_t *additional_data,
4925 data_t *input_data,
4926 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004927 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004928 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004929 int expected_status_arg )
4930{
4931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4932 psa_key_type_t key_type = key_type_arg;
4933 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004934 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004935 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4936 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4937 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004938 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004939 unsigned char *plaintext = NULL;
4940 unsigned char *finish_plaintext = NULL;
4941 size_t plaintext_size = 0;
4942 size_t plaintext_length = 0;
4943 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004944 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004945 unsigned char *tag_buffer = NULL;
4946 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004947
4948 PSA_ASSERT( psa_crypto_init( ) );
4949
4950 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4951 psa_set_key_algorithm( &attributes, alg );
4952 psa_set_key_type( &attributes, key_type );
4953
4954 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4955 &key ) );
4956
4957 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4958
4959 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4960 input_data->len );
4961
4962 ASSERT_ALLOC( plaintext, plaintext_size );
4963
4964 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4965
4966 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4967
Paul Elliott9961a662021-09-17 19:19:02 +01004968 status = psa_aead_decrypt_setup( &operation, key, alg );
4969
4970 /* If the operation is not supported, just skip and not fail in case the
4971 * encryption involves a common limitation of cryptography hardwares and
4972 * an alternative implementation. */
4973 if( status == PSA_ERROR_NOT_SUPPORTED )
4974 {
4975 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4976 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4977 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004978 TEST_EQUAL( status, expected_setup_status );
4979
4980 if( status != PSA_SUCCESS )
4981 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004982
4983 PSA_ASSERT( status );
4984
4985 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4986
Paul Elliottfec6f372021-10-06 17:15:02 +01004987 status = psa_aead_set_lengths( &operation, additional_data->len,
4988 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004989 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004990
Paul Elliott9961a662021-09-17 19:19:02 +01004991 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4992 additional_data->len ) );
4993
4994 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4995 input_data->len,
4996 plaintext, plaintext_size,
4997 &plaintext_length ) );
4998
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004999 if( tag_usage == USE_GIVEN_TAG )
5000 {
5001 tag_buffer = tag->x;
5002 tag_size = tag->len;
5003 }
5004
Paul Elliott9961a662021-09-17 19:19:02 +01005005 status = psa_aead_verify( &operation, finish_plaintext,
5006 verify_plaintext_size,
5007 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005008 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005009
5010 TEST_EQUAL( status, expected_status );
5011
5012exit:
5013 psa_destroy_key( key );
5014 mbedtls_free( plaintext );
5015 mbedtls_free( finish_plaintext );
5016 psa_aead_abort( &operation );
5017 PSA_DONE( );
5018}
5019/* END_CASE */
5020
Paul Elliott9961a662021-09-17 19:19:02 +01005021/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005022void aead_multipart_setup( int key_type_arg, data_t *key_data,
5023 int alg_arg, int expected_status_arg )
5024{
5025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5026 psa_key_type_t key_type = key_type_arg;
5027 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005028 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5030 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5031 psa_status_t expected_status = expected_status_arg;
5032
5033 PSA_ASSERT( psa_crypto_init( ) );
5034
5035 psa_set_key_usage_flags( &attributes,
5036 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5037 psa_set_key_algorithm( &attributes, alg );
5038 psa_set_key_type( &attributes, key_type );
5039
5040 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5041 &key ) );
5042
Paul Elliott5221ef62021-09-19 17:33:03 +01005043 status = psa_aead_encrypt_setup( &operation, key, alg );
5044
5045 TEST_EQUAL( status, expected_status );
5046
5047 psa_aead_abort( &operation );
5048
Paul Elliott5221ef62021-09-19 17:33:03 +01005049 status = psa_aead_decrypt_setup( &operation, key, alg );
5050
5051 TEST_EQUAL(status, expected_status );
5052
5053exit:
5054 psa_destroy_key( key );
5055 psa_aead_abort( &operation );
5056 PSA_DONE( );
5057}
5058/* END_CASE */
5059
5060/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005061void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5062 int alg_arg,
5063 data_t *nonce,
5064 data_t *additional_data,
5065 data_t *input_data )
5066{
5067 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5068 psa_key_type_t key_type = key_type_arg;
5069 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005070 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005071 unsigned char *output_data = NULL;
5072 unsigned char *final_data = NULL;
5073 size_t output_size = 0;
5074 size_t finish_output_size = 0;
5075 size_t output_length = 0;
5076 size_t key_bits = 0;
5077 size_t tag_length = 0;
5078 size_t tag_size = 0;
5079 size_t nonce_length = 0;
5080 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5081 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5082 size_t output_part_length = 0;
5083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5084
5085 PSA_ASSERT( psa_crypto_init( ) );
5086
5087 psa_set_key_usage_flags( & attributes,
5088 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5089 psa_set_key_algorithm( & attributes, alg );
5090 psa_set_key_type( & attributes, key_type );
5091
5092 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5093 &key ) );
5094
5095 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5096 key_bits = psa_get_key_bits( &attributes );
5097
5098 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5099
5100 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
5101
5102 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5103
5104 ASSERT_ALLOC( output_data, output_size );
5105
5106 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5107
5108 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
5109
5110 ASSERT_ALLOC( final_data, finish_output_size );
5111
5112 /* Test all operations error without calling setup first. */
5113
Paul Elliottc23a9a02021-06-21 18:32:46 +01005114 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5115 PSA_ERROR_BAD_STATE );
5116
5117 psa_aead_abort( &operation );
5118
Paul Elliottc23a9a02021-06-21 18:32:46 +01005119 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5120 PSA_AEAD_NONCE_MAX_SIZE,
5121 &nonce_length ),
5122 PSA_ERROR_BAD_STATE );
5123
5124 psa_aead_abort( &operation );
5125
Paul Elliott481be342021-07-16 17:38:47 +01005126 /* ------------------------------------------------------- */
5127
Paul Elliottc23a9a02021-06-21 18:32:46 +01005128 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5129 input_data->len ),
5130 PSA_ERROR_BAD_STATE );
5131
5132 psa_aead_abort( &operation );
5133
Paul Elliott481be342021-07-16 17:38:47 +01005134 /* ------------------------------------------------------- */
5135
Paul Elliottc23a9a02021-06-21 18:32:46 +01005136 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5137 additional_data->len ),
5138 PSA_ERROR_BAD_STATE );
5139
5140 psa_aead_abort( &operation );
5141
Paul Elliott481be342021-07-16 17:38:47 +01005142 /* ------------------------------------------------------- */
5143
Paul Elliottc23a9a02021-06-21 18:32:46 +01005144 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5145 input_data->len, output_data,
5146 output_size, &output_length ),
5147 PSA_ERROR_BAD_STATE );
5148
5149 psa_aead_abort( &operation );
5150
Paul Elliott481be342021-07-16 17:38:47 +01005151 /* ------------------------------------------------------- */
5152
Paul Elliottc23a9a02021-06-21 18:32:46 +01005153 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5154 finish_output_size,
5155 &output_part_length,
5156 tag_buffer, tag_length,
5157 &tag_size ),
5158 PSA_ERROR_BAD_STATE );
5159
5160 psa_aead_abort( &operation );
5161
Paul Elliott481be342021-07-16 17:38:47 +01005162 /* ------------------------------------------------------- */
5163
Paul Elliottc23a9a02021-06-21 18:32:46 +01005164 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5165 finish_output_size,
5166 &output_part_length,
5167 tag_buffer,
5168 tag_length ),
5169 PSA_ERROR_BAD_STATE );
5170
5171 psa_aead_abort( &operation );
5172
5173 /* Test for double setups. */
5174
Paul Elliottc23a9a02021-06-21 18:32:46 +01005175 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5176
5177 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5178 PSA_ERROR_BAD_STATE );
5179
5180 psa_aead_abort( &operation );
5181
Paul Elliott481be342021-07-16 17:38:47 +01005182 /* ------------------------------------------------------- */
5183
Paul Elliottc23a9a02021-06-21 18:32:46 +01005184 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5185
5186 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5187 PSA_ERROR_BAD_STATE );
5188
5189 psa_aead_abort( &operation );
5190
Paul Elliott374a2be2021-07-16 17:53:40 +01005191 /* ------------------------------------------------------- */
5192
Paul Elliott374a2be2021-07-16 17:53:40 +01005193 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5194
5195 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5196 PSA_ERROR_BAD_STATE );
5197
5198 psa_aead_abort( &operation );
5199
5200 /* ------------------------------------------------------- */
5201
Paul Elliott374a2be2021-07-16 17:53:40 +01005202 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5203
5204 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5205 PSA_ERROR_BAD_STATE );
5206
5207 psa_aead_abort( &operation );
5208
Paul Elliottc23a9a02021-06-21 18:32:46 +01005209 /* Test for not setting a nonce. */
5210
Paul Elliottc23a9a02021-06-21 18:32:46 +01005211 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5212
5213 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5214 additional_data->len ),
5215 PSA_ERROR_BAD_STATE );
5216
5217 psa_aead_abort( &operation );
5218
Paul Elliott7f628422021-09-01 12:08:29 +01005219 /* ------------------------------------------------------- */
5220
Paul Elliott7f628422021-09-01 12:08:29 +01005221 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5222
5223 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5224 input_data->len, output_data,
5225 output_size, &output_length ),
5226 PSA_ERROR_BAD_STATE );
5227
5228 psa_aead_abort( &operation );
5229
Paul Elliottbdc2c682021-09-21 18:37:10 +01005230 /* ------------------------------------------------------- */
5231
Paul Elliottbdc2c682021-09-21 18:37:10 +01005232 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5233
5234 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5235 finish_output_size,
5236 &output_part_length,
5237 tag_buffer, tag_length,
5238 &tag_size ),
5239 PSA_ERROR_BAD_STATE );
5240
5241 psa_aead_abort( &operation );
5242
5243 /* ------------------------------------------------------- */
5244
Paul Elliottbdc2c682021-09-21 18:37:10 +01005245 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5246
5247 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5248 finish_output_size,
5249 &output_part_length,
5250 tag_buffer,
5251 tag_length ),
5252 PSA_ERROR_BAD_STATE );
5253
5254 psa_aead_abort( &operation );
5255
Paul Elliottc23a9a02021-06-21 18:32:46 +01005256 /* Test for double setting nonce. */
5257
Paul Elliottc23a9a02021-06-21 18:32:46 +01005258 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5259
5260 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5261
5262 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5263 PSA_ERROR_BAD_STATE );
5264
5265 psa_aead_abort( &operation );
5266
Paul Elliott374a2be2021-07-16 17:53:40 +01005267 /* Test for double generating nonce. */
5268
Paul Elliott374a2be2021-07-16 17:53:40 +01005269 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5270
5271 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5272 PSA_AEAD_NONCE_MAX_SIZE,
5273 &nonce_length ) );
5274
5275 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5276 PSA_AEAD_NONCE_MAX_SIZE,
5277 &nonce_length ),
5278 PSA_ERROR_BAD_STATE );
5279
5280
5281 psa_aead_abort( &operation );
5282
5283 /* Test for generate nonce then set and vice versa */
5284
Paul Elliott374a2be2021-07-16 17:53:40 +01005285 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5286
5287 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5288 PSA_AEAD_NONCE_MAX_SIZE,
5289 &nonce_length ) );
5290
5291 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5292 PSA_ERROR_BAD_STATE );
5293
5294 psa_aead_abort( &operation );
5295
Andrzej Kurekad837522021-12-15 15:28:49 +01005296 /* Test for generating nonce after calling set lengths */
5297
5298 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5299
5300 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5301 input_data->len ) );
5302
5303 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5304 PSA_AEAD_NONCE_MAX_SIZE,
5305 &nonce_length ) );
5306
5307 psa_aead_abort( &operation );
5308
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005309 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005310
5311 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5312
5313 if( operation.alg == PSA_ALG_CCM )
5314 {
5315 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5316 input_data->len ),
5317 PSA_ERROR_INVALID_ARGUMENT );
5318 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5319 PSA_AEAD_NONCE_MAX_SIZE,
5320 &nonce_length ),
5321 PSA_ERROR_BAD_STATE );
5322 }
5323 else
5324 {
5325 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5326 input_data->len ) );
5327 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5328 PSA_AEAD_NONCE_MAX_SIZE,
5329 &nonce_length ) );
5330 }
5331
5332 psa_aead_abort( &operation );
5333
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005334 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005335#if SIZE_MAX > UINT32_MAX
5336 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5337
5338 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5339 {
5340 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5341 input_data->len ),
5342 PSA_ERROR_INVALID_ARGUMENT );
5343 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5344 PSA_AEAD_NONCE_MAX_SIZE,
5345 &nonce_length ),
5346 PSA_ERROR_BAD_STATE );
5347 }
5348 else
5349 {
5350 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5351 input_data->len ) );
5352 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5353 PSA_AEAD_NONCE_MAX_SIZE,
5354 &nonce_length ) );
5355 }
5356
5357 psa_aead_abort( &operation );
5358#endif
5359
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005360 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005361
5362 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5363
5364 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5365 PSA_AEAD_NONCE_MAX_SIZE,
5366 &nonce_length ) );
5367
5368 if( operation.alg == PSA_ALG_CCM )
5369 {
5370 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5371 input_data->len ),
5372 PSA_ERROR_INVALID_ARGUMENT );
5373 }
5374 else
5375 {
5376 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5377 input_data->len ) );
5378 }
5379
5380 psa_aead_abort( &operation );
5381
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005382 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005383 /* Test for setting nonce after calling set lengths */
5384
5385 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5386
5387 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5388 input_data->len ) );
5389
5390 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5391
5392 psa_aead_abort( &operation );
5393
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005394 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005395
5396 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5397
5398 if( operation.alg == PSA_ALG_CCM )
5399 {
5400 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_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, UINT32_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
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005415 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005416#if SIZE_MAX > UINT32_MAX
5417 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5418
5419 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5420 {
5421 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5422 input_data->len ),
5423 PSA_ERROR_INVALID_ARGUMENT );
5424 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5425 PSA_ERROR_BAD_STATE );
5426 }
5427 else
5428 {
5429 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5430 input_data->len ) );
5431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5432 }
5433
5434 psa_aead_abort( &operation );
5435#endif
5436
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005437 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005438
5439 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5440
5441 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5442
5443 if( operation.alg == PSA_ALG_CCM )
5444 {
5445 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5446 input_data->len ),
5447 PSA_ERROR_INVALID_ARGUMENT );
5448 }
5449 else
5450 {
5451 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5452 input_data->len ) );
5453 }
5454
5455 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005456
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005457 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005458#if SIZE_MAX > UINT32_MAX
5459 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5460
5461 if( operation.alg == PSA_ALG_GCM )
5462 {
5463 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5464 SIZE_MAX ),
5465 PSA_ERROR_INVALID_ARGUMENT );
5466 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5467 PSA_ERROR_BAD_STATE );
5468 }
5469 else if ( operation.alg != PSA_ALG_CCM )
5470 {
5471 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5472 SIZE_MAX ) );
5473 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5474 }
5475
5476 psa_aead_abort( &operation );
5477
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005478 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +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 if( operation.alg == PSA_ALG_GCM )
5484 {
5485 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5486 SIZE_MAX ),
5487 PSA_ERROR_INVALID_ARGUMENT );
5488 }
5489 else if ( operation.alg != PSA_ALG_CCM )
5490 {
5491 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5492 SIZE_MAX ) );
5493 }
5494
5495 psa_aead_abort( &operation );
5496#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005497
5498 /* ------------------------------------------------------- */
5499
Paul Elliott374a2be2021-07-16 17:53:40 +01005500 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5501
5502 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5503
5504 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5505 PSA_AEAD_NONCE_MAX_SIZE,
5506 &nonce_length ),
5507 PSA_ERROR_BAD_STATE );
5508
5509 psa_aead_abort( &operation );
5510
Paul Elliott7220cae2021-06-22 17:25:57 +01005511 /* Test for generating nonce in decrypt setup. */
5512
Paul Elliott7220cae2021-06-22 17:25:57 +01005513 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5514
5515 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5516 PSA_AEAD_NONCE_MAX_SIZE,
5517 &nonce_length ),
5518 PSA_ERROR_BAD_STATE );
5519
5520 psa_aead_abort( &operation );
5521
Paul Elliottc23a9a02021-06-21 18:32:46 +01005522 /* Test for setting lengths twice. */
5523
Paul Elliottc23a9a02021-06-21 18:32:46 +01005524 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5525
5526 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5527
5528 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5529 input_data->len ) );
5530
5531 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5532 input_data->len ),
5533 PSA_ERROR_BAD_STATE );
5534
5535 psa_aead_abort( &operation );
5536
Andrzej Kurekad837522021-12-15 15:28:49 +01005537 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005538
Paul Elliottc23a9a02021-06-21 18:32:46 +01005539 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5540
5541 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5542
Andrzej Kurekad837522021-12-15 15:28:49 +01005543 if( operation.alg == PSA_ALG_CCM )
5544 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005545
Andrzej Kurekad837522021-12-15 15:28:49 +01005546 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5547 additional_data->len ),
5548 PSA_ERROR_BAD_STATE );
5549 }
5550 else
5551 {
5552 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5553 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005554
Andrzej Kurekad837522021-12-15 15:28:49 +01005555 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5556 input_data->len ),
5557 PSA_ERROR_BAD_STATE );
5558 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005559 psa_aead_abort( &operation );
5560
5561 /* ------------------------------------------------------- */
5562
Paul Elliottf94bd992021-09-19 18:15:59 +01005563 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5564
5565 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5566
Andrzej Kurekad837522021-12-15 15:28:49 +01005567 if( operation.alg == PSA_ALG_CCM )
5568 {
5569 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5570 input_data->len, output_data,
5571 output_size, &output_length ),
5572 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005573
Andrzej Kurekad837522021-12-15 15:28:49 +01005574 }
5575 else
5576 {
5577 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5578 input_data->len, output_data,
5579 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005580
Andrzej Kurekad837522021-12-15 15:28:49 +01005581 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5582 input_data->len ),
5583 PSA_ERROR_BAD_STATE );
5584 }
5585 psa_aead_abort( &operation );
5586
5587 /* ------------------------------------------------------- */
5588
5589 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5590
5591 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5592
5593 if( operation.alg == PSA_ALG_CCM )
5594 {
5595 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5596 finish_output_size,
5597 &output_part_length,
5598 tag_buffer, tag_length,
5599 &tag_size ) );
5600 }
5601 else
5602 {
5603 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5604 finish_output_size,
5605 &output_part_length,
5606 tag_buffer, tag_length,
5607 &tag_size ) );
5608
5609 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5610 input_data->len ),
5611 PSA_ERROR_BAD_STATE );
5612 }
5613 psa_aead_abort( &operation );
5614
5615 /* Test for setting lengths after generating nonce + already starting data. */
5616
5617 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5618
5619 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5620 PSA_AEAD_NONCE_MAX_SIZE,
5621 &nonce_length ) );
5622 if( operation.alg == PSA_ALG_CCM )
5623 {
5624
5625 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5626 additional_data->len ),
5627 PSA_ERROR_BAD_STATE );
5628 }
5629 else
5630 {
5631 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5632 additional_data->len ) );
5633
5634 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5635 input_data->len ),
5636 PSA_ERROR_BAD_STATE );
5637 }
5638 psa_aead_abort( &operation );
5639
5640 /* ------------------------------------------------------- */
5641
5642 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5643
5644 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5645 PSA_AEAD_NONCE_MAX_SIZE,
5646 &nonce_length ) );
5647 if( operation.alg == PSA_ALG_CCM )
5648 {
5649 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5650 input_data->len, output_data,
5651 output_size, &output_length ),
5652 PSA_ERROR_BAD_STATE );
5653
5654 }
5655 else
5656 {
5657 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5658 input_data->len, output_data,
5659 output_size, &output_length ) );
5660
5661 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5662 input_data->len ),
5663 PSA_ERROR_BAD_STATE );
5664 }
5665 psa_aead_abort( &operation );
5666
5667 /* ------------------------------------------------------- */
5668
5669 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5670
5671 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5672 PSA_AEAD_NONCE_MAX_SIZE,
5673 &nonce_length ) );
5674 if( operation.alg == PSA_ALG_CCM )
5675 {
5676 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5677 finish_output_size,
5678 &output_part_length,
5679 tag_buffer, tag_length,
5680 &tag_size ) );
5681 }
5682 else
5683 {
5684 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5685 finish_output_size,
5686 &output_part_length,
5687 tag_buffer, tag_length,
5688 &tag_size ) );
5689
5690 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5691 input_data->len ),
5692 PSA_ERROR_BAD_STATE );
5693 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005694 psa_aead_abort( &operation );
5695
Paul Elliott243080c2021-07-21 19:01:17 +01005696 /* Test for not sending any additional data or data after setting non zero
5697 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005698
Paul Elliottc23a9a02021-06-21 18:32:46 +01005699 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5700
5701 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5702
5703 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5704 input_data->len ) );
5705
5706 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5707 finish_output_size,
5708 &output_part_length,
5709 tag_buffer, tag_length,
5710 &tag_size ),
5711 PSA_ERROR_INVALID_ARGUMENT );
5712
5713 psa_aead_abort( &operation );
5714
Paul Elliott243080c2021-07-21 19:01:17 +01005715 /* Test for not sending any additional data or data after setting non-zero
5716 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005717
Paul Elliottc23a9a02021-06-21 18:32:46 +01005718 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5719
5720 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5721
5722 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5723 input_data->len ) );
5724
5725 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5726 finish_output_size,
5727 &output_part_length,
5728 tag_buffer,
5729 tag_length ),
5730 PSA_ERROR_INVALID_ARGUMENT );
5731
5732 psa_aead_abort( &operation );
5733
Paul Elliott243080c2021-07-21 19:01:17 +01005734 /* Test for not sending any additional data after setting a non-zero length
5735 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005736
Paul Elliottc23a9a02021-06-21 18:32:46 +01005737 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5738
5739 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5740
5741 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5742 input_data->len ) );
5743
5744 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5745 input_data->len, output_data,
5746 output_size, &output_length ),
5747 PSA_ERROR_INVALID_ARGUMENT );
5748
5749 psa_aead_abort( &operation );
5750
Paul Elliottf94bd992021-09-19 18:15:59 +01005751 /* Test for not sending any data after setting a non-zero length for it.*/
5752
Paul Elliottf94bd992021-09-19 18:15:59 +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, additional_data->len,
5758 input_data->len ) );
5759
5760 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5761 additional_data->len ) );
5762
5763 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5764 finish_output_size,
5765 &output_part_length,
5766 tag_buffer, tag_length,
5767 &tag_size ),
5768 PSA_ERROR_INVALID_ARGUMENT );
5769
5770 psa_aead_abort( &operation );
5771
Paul Elliottb0450fe2021-09-01 15:06:26 +01005772 /* Test for sending too much additional data after setting lengths. */
5773
Paul Elliottb0450fe2021-09-01 15:06:26 +01005774 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5775
5776 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5777
5778 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5779
5780
5781 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5782 additional_data->len ),
5783 PSA_ERROR_INVALID_ARGUMENT );
5784
5785 psa_aead_abort( &operation );
5786
Paul Elliotta2a09b02021-09-22 14:56:40 +01005787 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005788
5789 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5790
5791 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5792
5793 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5794 input_data->len ) );
5795
5796 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5797 additional_data->len ) );
5798
5799 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5800 1 ),
5801 PSA_ERROR_INVALID_ARGUMENT );
5802
5803 psa_aead_abort( &operation );
5804
Paul Elliottb0450fe2021-09-01 15:06:26 +01005805 /* Test for sending too much data after setting lengths. */
5806
Paul Elliottb0450fe2021-09-01 15:06:26 +01005807 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5808
5809 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5810
5811 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5812
5813 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5814 input_data->len, output_data,
5815 output_size, &output_length ),
5816 PSA_ERROR_INVALID_ARGUMENT );
5817
5818 psa_aead_abort( &operation );
5819
Paul Elliotta2a09b02021-09-22 14:56:40 +01005820 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005821
5822 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5823
5824 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5825
5826 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5827 input_data->len ) );
5828
5829 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5830 additional_data->len ) );
5831
5832 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5833 input_data->len, output_data,
5834 output_size, &output_length ) );
5835
5836 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5837 1, output_data,
5838 output_size, &output_length ),
5839 PSA_ERROR_INVALID_ARGUMENT );
5840
5841 psa_aead_abort( &operation );
5842
Paul Elliottc23a9a02021-06-21 18:32:46 +01005843 /* Test sending additional data after data. */
5844
Paul Elliottc23a9a02021-06-21 18:32:46 +01005845 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5846
5847 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5848
Andrzej Kurekad837522021-12-15 15:28:49 +01005849 if( operation.alg != PSA_ALG_CCM )
5850 {
5851 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5852 input_data->len, output_data,
5853 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005854
Andrzej Kurekad837522021-12-15 15:28:49 +01005855 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5856 additional_data->len ),
5857 PSA_ERROR_BAD_STATE );
5858 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005859 psa_aead_abort( &operation );
5860
Paul Elliott534d0b42021-06-22 19:15:20 +01005861 /* Test calling finish on decryption. */
5862
Paul Elliott534d0b42021-06-22 19:15:20 +01005863 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5864
5865 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5866
5867 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5868 finish_output_size,
5869 &output_part_length,
5870 tag_buffer, tag_length,
5871 &tag_size ),
5872 PSA_ERROR_BAD_STATE );
5873
5874 psa_aead_abort( &operation );
5875
5876 /* Test calling verify on encryption. */
5877
Paul Elliott534d0b42021-06-22 19:15:20 +01005878 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5879
5880 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5881
5882 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5883 finish_output_size,
5884 &output_part_length,
5885 tag_buffer,
5886 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005887 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005888
5889 psa_aead_abort( &operation );
5890
5891
Paul Elliottc23a9a02021-06-21 18:32:46 +01005892exit:
5893 psa_destroy_key( key );
5894 psa_aead_abort( &operation );
5895 mbedtls_free( output_data );
5896 mbedtls_free( final_data );
5897 PSA_DONE( );
5898}
5899/* END_CASE */
5900
5901/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005902void signature_size( int type_arg,
5903 int bits,
5904 int alg_arg,
5905 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005906{
5907 psa_key_type_t type = type_arg;
5908 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005909 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005910
Gilles Peskinefe11b722018-12-18 00:24:04 +01005911 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005912
Gilles Peskinee59236f2018-01-27 23:32:46 +01005913exit:
5914 ;
5915}
5916/* END_CASE */
5917
5918/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005919void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5920 int alg_arg, data_t *input_data,
5921 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005922{
Ronald Cron5425a212020-08-04 14:58:35 +02005923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005924 psa_key_type_t key_type = key_type_arg;
5925 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005926 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005927 unsigned char *signature = NULL;
5928 size_t signature_size;
5929 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005931
Gilles Peskine8817f612018-12-18 00:18:46 +01005932 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005933
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005935 psa_set_key_algorithm( &attributes, alg );
5936 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005937
Gilles Peskine049c7532019-05-15 20:22:09 +02005938 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005939 &key ) );
5940 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005941 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005942
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005943 /* Allocate a buffer which has the size advertized by the
5944 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005945 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005946 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005947 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005948 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005949 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005950
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005951 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005952 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005953 input_data->x, input_data->len,
5954 signature, signature_size,
5955 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005956 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005957 ASSERT_COMPARE( output_data->x, output_data->len,
5958 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005959
5960exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005961 /*
5962 * Key attributes may have been returned by psa_get_key_attributes()
5963 * thus reset them as required.
5964 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005965 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005966
Ronald Cron5425a212020-08-04 14:58:35 +02005967 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005968 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005969 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005970}
5971/* END_CASE */
5972
5973/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005974void sign_hash_fail( int key_type_arg, data_t *key_data,
5975 int alg_arg, data_t *input_data,
5976 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005977{
Ronald Cron5425a212020-08-04 14:58:35 +02005978 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005979 psa_key_type_t key_type = key_type_arg;
5980 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005981 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005982 psa_status_t actual_status;
5983 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005984 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005985 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005987
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005988 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005989
Gilles Peskine8817f612018-12-18 00:18:46 +01005990 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005991
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005992 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005993 psa_set_key_algorithm( &attributes, alg );
5994 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005995
Gilles Peskine049c7532019-05-15 20:22:09 +02005996 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005997 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005998
Ronald Cron5425a212020-08-04 14:58:35 +02005999 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006000 input_data->x, input_data->len,
6001 signature, signature_size,
6002 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006003 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006004 /* The value of *signature_length is unspecified on error, but
6005 * whatever it is, it should be less than signature_size, so that
6006 * if the caller tries to read *signature_length bytes without
6007 * checking the error code then they don't overflow a buffer. */
6008 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006009
6010exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006011 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006012 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006013 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006014 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006015}
6016/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006017
6018/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006019void sign_verify_hash( int key_type_arg, data_t *key_data,
6020 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006021{
Ronald Cron5425a212020-08-04 14:58:35 +02006022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006023 psa_key_type_t key_type = key_type_arg;
6024 psa_algorithm_t alg = alg_arg;
6025 size_t key_bits;
6026 unsigned char *signature = NULL;
6027 size_t signature_size;
6028 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006030
Gilles Peskine8817f612018-12-18 00:18:46 +01006031 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006032
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006034 psa_set_key_algorithm( &attributes, alg );
6035 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006036
Gilles Peskine049c7532019-05-15 20:22:09 +02006037 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006038 &key ) );
6039 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006040 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006041
6042 /* Allocate a buffer which has the size advertized by the
6043 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006044 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006045 key_bits, alg );
6046 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006047 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006048 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006049
6050 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006051 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006052 input_data->x, input_data->len,
6053 signature, signature_size,
6054 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006055 /* Check that the signature length looks sensible. */
6056 TEST_ASSERT( signature_length <= signature_size );
6057 TEST_ASSERT( signature_length > 0 );
6058
6059 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006060 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006061 input_data->x, input_data->len,
6062 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006063
6064 if( input_data->len != 0 )
6065 {
6066 /* Flip a bit in the input and verify that the signature is now
6067 * detected as invalid. Flip a bit at the beginning, not at the end,
6068 * because ECDSA may ignore the last few bits of the input. */
6069 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006070 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006071 input_data->x, input_data->len,
6072 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006073 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006074 }
6075
6076exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006077 /*
6078 * Key attributes may have been returned by psa_get_key_attributes()
6079 * thus reset them as required.
6080 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006081 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006082
Ronald Cron5425a212020-08-04 14:58:35 +02006083 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006084 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006085 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006086}
6087/* END_CASE */
6088
6089/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006090void verify_hash( int key_type_arg, data_t *key_data,
6091 int alg_arg, data_t *hash_data,
6092 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006093{
Ronald Cron5425a212020-08-04 14:58:35 +02006094 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006095 psa_key_type_t key_type = key_type_arg;
6096 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006098
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006099 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006100
Gilles Peskine8817f612018-12-18 00:18:46 +01006101 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006102
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006103 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006104 psa_set_key_algorithm( &attributes, alg );
6105 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006106
Gilles Peskine049c7532019-05-15 20:22:09 +02006107 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006108 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006109
Ronald Cron5425a212020-08-04 14:58:35 +02006110 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006111 hash_data->x, hash_data->len,
6112 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006113
itayzafrir5c753392018-05-08 11:18:38 +03006114exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006115 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006116 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006117 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006118}
6119/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006120
6121/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006122void verify_hash_fail( int key_type_arg, data_t *key_data,
6123 int alg_arg, data_t *hash_data,
6124 data_t *signature_data,
6125 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006126{
Ronald Cron5425a212020-08-04 14:58:35 +02006127 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006128 psa_key_type_t key_type = key_type_arg;
6129 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006130 psa_status_t actual_status;
6131 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006132 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006133
Gilles Peskine8817f612018-12-18 00:18:46 +01006134 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006135
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006136 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006137 psa_set_key_algorithm( &attributes, alg );
6138 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006139
Gilles Peskine049c7532019-05-15 20:22:09 +02006140 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006141 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006142
Ronald Cron5425a212020-08-04 14:58:35 +02006143 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006144 hash_data->x, hash_data->len,
6145 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006146 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006147
6148exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006149 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006150 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006151 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006152}
6153/* END_CASE */
6154
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006155/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006156void sign_message_deterministic( int key_type_arg,
6157 data_t *key_data,
6158 int alg_arg,
6159 data_t *input_data,
6160 data_t *output_data )
6161{
6162 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6163 psa_key_type_t key_type = key_type_arg;
6164 psa_algorithm_t alg = alg_arg;
6165 size_t key_bits;
6166 unsigned char *signature = NULL;
6167 size_t signature_size;
6168 size_t signature_length = 0xdeadbeef;
6169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6170
6171 PSA_ASSERT( psa_crypto_init( ) );
6172
6173 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6174 psa_set_key_algorithm( &attributes, alg );
6175 psa_set_key_type( &attributes, key_type );
6176
6177 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6178 &key ) );
6179 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6180 key_bits = psa_get_key_bits( &attributes );
6181
6182 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6183 TEST_ASSERT( signature_size != 0 );
6184 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
6185 ASSERT_ALLOC( signature, signature_size );
6186
6187 PSA_ASSERT( psa_sign_message( key, alg,
6188 input_data->x, input_data->len,
6189 signature, signature_size,
6190 &signature_length ) );
6191
6192 ASSERT_COMPARE( output_data->x, output_data->len,
6193 signature, signature_length );
6194
6195exit:
6196 psa_reset_key_attributes( &attributes );
6197
6198 psa_destroy_key( key );
6199 mbedtls_free( signature );
6200 PSA_DONE( );
6201
6202}
6203/* END_CASE */
6204
6205/* BEGIN_CASE */
6206void sign_message_fail( int key_type_arg,
6207 data_t *key_data,
6208 int alg_arg,
6209 data_t *input_data,
6210 int signature_size_arg,
6211 int expected_status_arg )
6212{
6213 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6214 psa_key_type_t key_type = key_type_arg;
6215 psa_algorithm_t alg = alg_arg;
6216 size_t signature_size = signature_size_arg;
6217 psa_status_t actual_status;
6218 psa_status_t expected_status = expected_status_arg;
6219 unsigned char *signature = NULL;
6220 size_t signature_length = 0xdeadbeef;
6221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6222
6223 ASSERT_ALLOC( signature, signature_size );
6224
6225 PSA_ASSERT( psa_crypto_init( ) );
6226
6227 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6228 psa_set_key_algorithm( &attributes, alg );
6229 psa_set_key_type( &attributes, key_type );
6230
6231 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6232 &key ) );
6233
6234 actual_status = psa_sign_message( key, alg,
6235 input_data->x, input_data->len,
6236 signature, signature_size,
6237 &signature_length );
6238 TEST_EQUAL( actual_status, expected_status );
6239 /* The value of *signature_length is unspecified on error, but
6240 * whatever it is, it should be less than signature_size, so that
6241 * if the caller tries to read *signature_length bytes without
6242 * checking the error code then they don't overflow a buffer. */
6243 TEST_ASSERT( signature_length <= signature_size );
6244
6245exit:
6246 psa_reset_key_attributes( &attributes );
6247 psa_destroy_key( key );
6248 mbedtls_free( signature );
6249 PSA_DONE( );
6250}
6251/* END_CASE */
6252
6253/* BEGIN_CASE */
6254void sign_verify_message( int key_type_arg,
6255 data_t *key_data,
6256 int alg_arg,
6257 data_t *input_data )
6258{
6259 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6260 psa_key_type_t key_type = key_type_arg;
6261 psa_algorithm_t alg = alg_arg;
6262 size_t key_bits;
6263 unsigned char *signature = NULL;
6264 size_t signature_size;
6265 size_t signature_length = 0xdeadbeef;
6266 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6267
6268 PSA_ASSERT( psa_crypto_init( ) );
6269
6270 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6271 PSA_KEY_USAGE_VERIFY_MESSAGE );
6272 psa_set_key_algorithm( &attributes, alg );
6273 psa_set_key_type( &attributes, key_type );
6274
6275 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6276 &key ) );
6277 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6278 key_bits = psa_get_key_bits( &attributes );
6279
6280 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6281 TEST_ASSERT( signature_size != 0 );
6282 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
6283 ASSERT_ALLOC( signature, signature_size );
6284
6285 PSA_ASSERT( psa_sign_message( key, alg,
6286 input_data->x, input_data->len,
6287 signature, signature_size,
6288 &signature_length ) );
6289 TEST_ASSERT( signature_length <= signature_size );
6290 TEST_ASSERT( signature_length > 0 );
6291
6292 PSA_ASSERT( psa_verify_message( key, alg,
6293 input_data->x, input_data->len,
6294 signature, signature_length ) );
6295
6296 if( input_data->len != 0 )
6297 {
6298 /* Flip a bit in the input and verify that the signature is now
6299 * detected as invalid. Flip a bit at the beginning, not at the end,
6300 * because ECDSA may ignore the last few bits of the input. */
6301 input_data->x[0] ^= 1;
6302 TEST_EQUAL( psa_verify_message( key, alg,
6303 input_data->x, input_data->len,
6304 signature, signature_length ),
6305 PSA_ERROR_INVALID_SIGNATURE );
6306 }
6307
6308exit:
6309 psa_reset_key_attributes( &attributes );
6310
6311 psa_destroy_key( key );
6312 mbedtls_free( signature );
6313 PSA_DONE( );
6314}
6315/* END_CASE */
6316
6317/* BEGIN_CASE */
6318void verify_message( int key_type_arg,
6319 data_t *key_data,
6320 int alg_arg,
6321 data_t *input_data,
6322 data_t *signature_data )
6323{
6324 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6325 psa_key_type_t key_type = key_type_arg;
6326 psa_algorithm_t alg = alg_arg;
6327 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6328
6329 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
6330
6331 PSA_ASSERT( psa_crypto_init( ) );
6332
6333 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6334 psa_set_key_algorithm( &attributes, alg );
6335 psa_set_key_type( &attributes, key_type );
6336
6337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6338 &key ) );
6339
6340 PSA_ASSERT( psa_verify_message( key, alg,
6341 input_data->x, input_data->len,
6342 signature_data->x, signature_data->len ) );
6343
6344exit:
6345 psa_reset_key_attributes( &attributes );
6346 psa_destroy_key( key );
6347 PSA_DONE( );
6348}
6349/* END_CASE */
6350
6351/* BEGIN_CASE */
6352void verify_message_fail( int key_type_arg,
6353 data_t *key_data,
6354 int alg_arg,
6355 data_t *hash_data,
6356 data_t *signature_data,
6357 int expected_status_arg )
6358{
6359 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6360 psa_key_type_t key_type = key_type_arg;
6361 psa_algorithm_t alg = alg_arg;
6362 psa_status_t actual_status;
6363 psa_status_t expected_status = expected_status_arg;
6364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6365
6366 PSA_ASSERT( psa_crypto_init( ) );
6367
6368 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6369 psa_set_key_algorithm( &attributes, alg );
6370 psa_set_key_type( &attributes, key_type );
6371
6372 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6373 &key ) );
6374
6375 actual_status = psa_verify_message( key, alg,
6376 hash_data->x, hash_data->len,
6377 signature_data->x,
6378 signature_data->len );
6379 TEST_EQUAL( actual_status, expected_status );
6380
6381exit:
6382 psa_reset_key_attributes( &attributes );
6383 psa_destroy_key( key );
6384 PSA_DONE( );
6385}
6386/* END_CASE */
6387
6388/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006389void asymmetric_encrypt( int key_type_arg,
6390 data_t *key_data,
6391 int alg_arg,
6392 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006393 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006394 int expected_output_length_arg,
6395 int expected_status_arg )
6396{
Ronald Cron5425a212020-08-04 14:58:35 +02006397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006398 psa_key_type_t key_type = key_type_arg;
6399 psa_algorithm_t alg = alg_arg;
6400 size_t expected_output_length = expected_output_length_arg;
6401 size_t key_bits;
6402 unsigned char *output = NULL;
6403 size_t output_size;
6404 size_t output_length = ~0;
6405 psa_status_t actual_status;
6406 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006408
Gilles Peskine8817f612018-12-18 00:18:46 +01006409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006410
Gilles Peskine656896e2018-06-29 19:12:28 +02006411 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6413 psa_set_key_algorithm( &attributes, alg );
6414 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006415 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006416 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006417
6418 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006419 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006420 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006421
Gilles Peskine656896e2018-06-29 19:12:28 +02006422 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006423 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006424 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006425
6426 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006427 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006428 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006429 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006430 output, output_size,
6431 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006432 TEST_EQUAL( actual_status, expected_status );
6433 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006434
Gilles Peskine68428122018-06-30 18:42:41 +02006435 /* If the label is empty, the test framework puts a non-null pointer
6436 * in label->x. Test that a null pointer works as well. */
6437 if( label->len == 0 )
6438 {
6439 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006440 if( output_size != 0 )
6441 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006442 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006443 input_data->x, input_data->len,
6444 NULL, label->len,
6445 output, output_size,
6446 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006447 TEST_EQUAL( actual_status, expected_status );
6448 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006449 }
6450
Gilles Peskine656896e2018-06-29 19:12:28 +02006451exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006452 /*
6453 * Key attributes may have been returned by psa_get_key_attributes()
6454 * thus reset them as required.
6455 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006456 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006457
Ronald Cron5425a212020-08-04 14:58:35 +02006458 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006459 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006460 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006461}
6462/* END_CASE */
6463
6464/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006465void asymmetric_encrypt_decrypt( int key_type_arg,
6466 data_t *key_data,
6467 int alg_arg,
6468 data_t *input_data,
6469 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006470{
Ronald Cron5425a212020-08-04 14:58:35 +02006471 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006472 psa_key_type_t key_type = key_type_arg;
6473 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006474 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006475 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006476 size_t output_size;
6477 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006478 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006479 size_t output2_size;
6480 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006482
Gilles Peskine8817f612018-12-18 00:18:46 +01006483 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006484
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006485 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6486 psa_set_key_algorithm( &attributes, alg );
6487 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006488
Gilles Peskine049c7532019-05-15 20:22:09 +02006489 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006490 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006491
6492 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006493 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006494 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006495
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006496 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006497 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006498 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006499
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006500 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006501 TEST_ASSERT( output2_size <=
6502 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6503 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006504 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006505
Gilles Peskineeebd7382018-06-08 18:11:54 +02006506 /* We test encryption by checking that encrypt-then-decrypt gives back
6507 * the original plaintext because of the non-optional random
6508 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006509 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006510 input_data->x, input_data->len,
6511 label->x, label->len,
6512 output, output_size,
6513 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006514 /* We don't know what ciphertext length to expect, but check that
6515 * it looks sensible. */
6516 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006517
Ronald Cron5425a212020-08-04 14:58:35 +02006518 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006519 output, output_length,
6520 label->x, label->len,
6521 output2, output2_size,
6522 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006523 ASSERT_COMPARE( input_data->x, input_data->len,
6524 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006525
6526exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006527 /*
6528 * Key attributes may have been returned by psa_get_key_attributes()
6529 * thus reset them as required.
6530 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006531 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006532
Ronald Cron5425a212020-08-04 14:58:35 +02006533 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006534 mbedtls_free( output );
6535 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006536 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006537}
6538/* END_CASE */
6539
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006540/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006541void asymmetric_decrypt( int key_type_arg,
6542 data_t *key_data,
6543 int alg_arg,
6544 data_t *input_data,
6545 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006546 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006547{
Ronald Cron5425a212020-08-04 14:58:35 +02006548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006549 psa_key_type_t key_type = key_type_arg;
6550 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006551 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006552 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006553 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006554 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006556
Gilles Peskine8817f612018-12-18 00:18:46 +01006557 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006558
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006559 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6560 psa_set_key_algorithm( &attributes, alg );
6561 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006562
Gilles Peskine049c7532019-05-15 20:22:09 +02006563 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006564 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006565
gabor-mezei-armceface22021-01-21 12:26:17 +01006566 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6567 key_bits = psa_get_key_bits( &attributes );
6568
6569 /* Determine the maximum ciphertext length */
6570 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6571 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6572 ASSERT_ALLOC( output, output_size );
6573
Ronald Cron5425a212020-08-04 14:58:35 +02006574 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006575 input_data->x, input_data->len,
6576 label->x, label->len,
6577 output,
6578 output_size,
6579 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006580 ASSERT_COMPARE( expected_data->x, expected_data->len,
6581 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006582
Gilles Peskine68428122018-06-30 18:42:41 +02006583 /* If the label is empty, the test framework puts a non-null pointer
6584 * in label->x. Test that a null pointer works as well. */
6585 if( label->len == 0 )
6586 {
6587 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006588 if( output_size != 0 )
6589 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006590 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006591 input_data->x, input_data->len,
6592 NULL, label->len,
6593 output,
6594 output_size,
6595 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006596 ASSERT_COMPARE( expected_data->x, expected_data->len,
6597 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006598 }
6599
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006600exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006601 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006602 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006603 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006604 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006605}
6606/* END_CASE */
6607
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006608/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006609void asymmetric_decrypt_fail( int key_type_arg,
6610 data_t *key_data,
6611 int alg_arg,
6612 data_t *input_data,
6613 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006614 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006615 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006616{
Ronald Cron5425a212020-08-04 14:58:35 +02006617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006618 psa_key_type_t key_type = key_type_arg;
6619 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006620 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006621 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006622 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006623 psa_status_t actual_status;
6624 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006626
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006627 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006628
Gilles Peskine8817f612018-12-18 00:18:46 +01006629 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006630
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006631 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6632 psa_set_key_algorithm( &attributes, alg );
6633 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006634
Gilles Peskine049c7532019-05-15 20:22:09 +02006635 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006636 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006637
Ronald Cron5425a212020-08-04 14:58:35 +02006638 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006639 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006640 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006641 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006642 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006643 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006644 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006645
Gilles Peskine68428122018-06-30 18:42:41 +02006646 /* If the label is empty, the test framework puts a non-null pointer
6647 * in label->x. Test that a null pointer works as well. */
6648 if( label->len == 0 )
6649 {
6650 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006651 if( output_size != 0 )
6652 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006653 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006654 input_data->x, input_data->len,
6655 NULL, label->len,
6656 output, output_size,
6657 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006658 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006659 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006660 }
6661
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006662exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006663 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006664 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006665 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006666 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006667}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006668/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006669
6670/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006671void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006672{
6673 /* Test each valid way of initializing the object, except for `= {0}`, as
6674 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6675 * though it's OK by the C standard. We could test for this, but we'd need
6676 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006677 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006678 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6679 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6680 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006681
6682 memset( &zero, 0, sizeof( zero ) );
6683
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006684 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006685 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006686 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006687 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006688 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006689 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006690 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006691
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006692 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006693 PSA_ASSERT( psa_key_derivation_abort(&func) );
6694 PSA_ASSERT( psa_key_derivation_abort(&init) );
6695 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006696}
6697/* END_CASE */
6698
Janos Follath16de4a42019-06-13 16:32:24 +01006699/* BEGIN_CASE */
6700void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006701{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006702 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006703 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006704 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006705
Gilles Peskine8817f612018-12-18 00:18:46 +01006706 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006707
Janos Follath16de4a42019-06-13 16:32:24 +01006708 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006709 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006710
6711exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006712 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006713 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006714}
6715/* END_CASE */
6716
Janos Follathaf3c2a02019-06-12 12:34:34 +01006717/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006718void derive_set_capacity( int alg_arg, int capacity_arg,
6719 int expected_status_arg )
6720{
6721 psa_algorithm_t alg = alg_arg;
6722 size_t capacity = capacity_arg;
6723 psa_status_t expected_status = expected_status_arg;
6724 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6725
6726 PSA_ASSERT( psa_crypto_init( ) );
6727
6728 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6729
6730 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6731 expected_status );
6732
6733exit:
6734 psa_key_derivation_abort( &operation );
6735 PSA_DONE( );
6736}
6737/* END_CASE */
6738
6739/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006740void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006741 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006742 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006743 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006744 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006745 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006746 int expected_status_arg3,
6747 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006748{
6749 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006750 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6751 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006752 psa_status_t expected_statuses[] = {expected_status_arg1,
6753 expected_status_arg2,
6754 expected_status_arg3};
6755 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006756 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6757 MBEDTLS_SVC_KEY_ID_INIT,
6758 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006759 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6760 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6761 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006762 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006763 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006764 psa_status_t expected_output_status = expected_output_status_arg;
6765 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006766
6767 PSA_ASSERT( psa_crypto_init( ) );
6768
6769 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6770 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006771
6772 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6773
6774 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6775 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006776 mbedtls_test_set_step( i );
6777 if( steps[i] == 0 )
6778 {
6779 /* Skip this step */
6780 }
6781 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006782 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006783 psa_set_key_type( &attributes, key_types[i] );
6784 PSA_ASSERT( psa_import_key( &attributes,
6785 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006786 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006787 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6788 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6789 {
6790 // When taking a private key as secret input, use key agreement
6791 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006792 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6793 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006794 expected_statuses[i] );
6795 }
6796 else
6797 {
6798 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006799 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006800 expected_statuses[i] );
6801 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006802 }
6803 else
6804 {
6805 TEST_EQUAL( psa_key_derivation_input_bytes(
6806 &operation, steps[i],
6807 inputs[i]->x, inputs[i]->len ),
6808 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006809 }
6810 }
6811
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006812 if( output_key_type != PSA_KEY_TYPE_NONE )
6813 {
6814 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006815 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006816 psa_set_key_bits( &attributes, 8 );
6817 actual_output_status =
6818 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006819 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006820 }
6821 else
6822 {
6823 uint8_t buffer[1];
6824 actual_output_status =
6825 psa_key_derivation_output_bytes( &operation,
6826 buffer, sizeof( buffer ) );
6827 }
6828 TEST_EQUAL( actual_output_status, expected_output_status );
6829
Janos Follathaf3c2a02019-06-12 12:34:34 +01006830exit:
6831 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006832 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6833 psa_destroy_key( keys[i] );
6834 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006835 PSA_DONE( );
6836}
6837/* END_CASE */
6838
Janos Follathd958bb72019-07-03 15:02:16 +01006839/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006840void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006841{
Janos Follathd958bb72019-07-03 15:02:16 +01006842 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006844 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006845 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006846 unsigned char input1[] = "Input 1";
6847 size_t input1_length = sizeof( input1 );
6848 unsigned char input2[] = "Input 2";
6849 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006850 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006851 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006852 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6853 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6854 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006855 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006856
Gilles Peskine8817f612018-12-18 00:18:46 +01006857 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006858
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006859 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6860 psa_set_key_algorithm( &attributes, alg );
6861 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006862
Gilles Peskine73676cb2019-05-15 20:15:10 +02006863 PSA_ASSERT( psa_import_key( &attributes,
6864 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006865 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006866
6867 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006868 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6869 input1, input1_length,
6870 input2, input2_length,
6871 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006872 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006873
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006874 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006875 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006876 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006877
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006878 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006879
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006880 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006881 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006882
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006883exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006884 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006885 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006886 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006887}
6888/* END_CASE */
6889
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006890/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006891void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006892{
6893 uint8_t output_buffer[16];
6894 size_t buffer_size = 16;
6895 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006896 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006897
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006898 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6899 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006900 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006901
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006902 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006903 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006904
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006905 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006906
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006907 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6908 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006909 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006910
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006911 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006912 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006913
6914exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006915 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006916}
6917/* END_CASE */
6918
6919/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006920void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006921 int step1_arg, data_t *input1,
6922 int step2_arg, data_t *input2,
6923 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006924 int requested_capacity_arg,
6925 data_t *expected_output1,
6926 data_t *expected_output2 )
6927{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006928 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006929 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6930 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006931 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6932 MBEDTLS_SVC_KEY_ID_INIT,
6933 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006934 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006935 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006936 uint8_t *expected_outputs[2] =
6937 {expected_output1->x, expected_output2->x};
6938 size_t output_sizes[2] =
6939 {expected_output1->len, expected_output2->len};
6940 size_t output_buffer_size = 0;
6941 uint8_t *output_buffer = NULL;
6942 size_t expected_capacity;
6943 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006944 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006945 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006946 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006947
6948 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6949 {
6950 if( output_sizes[i] > output_buffer_size )
6951 output_buffer_size = output_sizes[i];
6952 if( output_sizes[i] == 0 )
6953 expected_outputs[i] = NULL;
6954 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006955 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006956 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006957
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006958 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6959 psa_set_key_algorithm( &attributes, alg );
6960 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006961
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006962 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006963 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6964 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6965 requested_capacity ) );
6966 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006967 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006968 switch( steps[i] )
6969 {
6970 case 0:
6971 break;
6972 case PSA_KEY_DERIVATION_INPUT_SECRET:
6973 PSA_ASSERT( psa_import_key( &attributes,
6974 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006975 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006976
6977 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6978 {
6979 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6980 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6981 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6982 }
6983
Gilles Peskine1468da72019-05-29 17:35:49 +02006984 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006985 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006986 break;
6987 default:
6988 PSA_ASSERT( psa_key_derivation_input_bytes(
6989 &operation, steps[i],
6990 inputs[i]->x, inputs[i]->len ) );
6991 break;
6992 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006993 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006994
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006995 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006996 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006997 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006998 expected_capacity = requested_capacity;
6999
7000 /* Expansion phase. */
7001 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7002 {
7003 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007004 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007005 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007006 if( expected_capacity == 0 && output_sizes[i] == 0 )
7007 {
7008 /* Reading 0 bytes when 0 bytes are available can go either way. */
7009 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02007010 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007011 continue;
7012 }
7013 else if( expected_capacity == 0 ||
7014 output_sizes[i] > expected_capacity )
7015 {
7016 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02007017 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007018 expected_capacity = 0;
7019 continue;
7020 }
7021 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01007022 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007023 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007024 ASSERT_COMPARE( output_buffer, output_sizes[i],
7025 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007026 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007027 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007028 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007029 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007030 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007031 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007032 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007033
7034exit:
7035 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007036 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007037 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7038 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007039 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007040}
7041/* END_CASE */
7042
7043/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007044void derive_full( int alg_arg,
7045 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007046 data_t *input1,
7047 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007048 int requested_capacity_arg )
7049{
Ronald Cron5425a212020-08-04 14:58:35 +02007050 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007051 psa_algorithm_t alg = alg_arg;
7052 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007053 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007054 unsigned char output_buffer[16];
7055 size_t expected_capacity = requested_capacity;
7056 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007057 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007058
Gilles Peskine8817f612018-12-18 00:18:46 +01007059 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007060
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007061 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7062 psa_set_key_algorithm( &attributes, alg );
7063 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007064
Gilles Peskine049c7532019-05-15 20:22:09 +02007065 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007066 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007067
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007068 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7069 input1->x, input1->len,
7070 input2->x, input2->len,
7071 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007072 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007073
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, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007077
7078 /* Expansion phase. */
7079 while( current_capacity > 0 )
7080 {
7081 size_t read_size = sizeof( output_buffer );
7082 if( read_size > current_capacity )
7083 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007084 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007085 output_buffer,
7086 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007087 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007088 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007089 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007090 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007091 }
7092
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007093 /* Check that the operation refuses to go over capacity. */
7094 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007095 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007096
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007097 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007098
7099exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007100 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007101 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007102 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007103}
7104/* END_CASE */
7105
Janos Follathe60c9052019-07-03 13:51:30 +01007106/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007107void derive_key_exercise( int alg_arg,
7108 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007109 data_t *input1,
7110 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007111 int derived_type_arg,
7112 int derived_bits_arg,
7113 int derived_usage_arg,
7114 int derived_alg_arg )
7115{
Ronald Cron5425a212020-08-04 14:58:35 +02007116 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7117 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007118 psa_algorithm_t alg = alg_arg;
7119 psa_key_type_t derived_type = derived_type_arg;
7120 size_t derived_bits = derived_bits_arg;
7121 psa_key_usage_t derived_usage = derived_usage_arg;
7122 psa_algorithm_t derived_alg = derived_alg_arg;
7123 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007124 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007125 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007126 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007127
Gilles Peskine8817f612018-12-18 00:18:46 +01007128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007129
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007130 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7131 psa_set_key_algorithm( &attributes, alg );
7132 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007133 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007134 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007135
7136 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007137 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7138 input1->x, input1->len,
7139 input2->x, input2->len,
7140 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007141 goto exit;
7142
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007143 psa_set_key_usage_flags( &attributes, derived_usage );
7144 psa_set_key_algorithm( &attributes, derived_alg );
7145 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007146 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007147 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007148 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007149
7150 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007151 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007152 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7153 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007154
7155 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007156 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007157 goto exit;
7158
7159exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007160 /*
7161 * Key attributes may have been returned by psa_get_key_attributes()
7162 * thus reset them as required.
7163 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007164 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007165
7166 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007167 psa_destroy_key( base_key );
7168 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007169 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007170}
7171/* END_CASE */
7172
Janos Follath42fd8882019-07-03 14:17:09 +01007173/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007174void derive_key_export( int alg_arg,
7175 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007176 data_t *input1,
7177 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007178 int bytes1_arg,
7179 int bytes2_arg )
7180{
Ronald Cron5425a212020-08-04 14:58:35 +02007181 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7182 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007183 psa_algorithm_t alg = alg_arg;
7184 size_t bytes1 = bytes1_arg;
7185 size_t bytes2 = bytes2_arg;
7186 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007187 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007188 uint8_t *output_buffer = NULL;
7189 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007190 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7191 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007192 size_t length;
7193
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007194 ASSERT_ALLOC( output_buffer, capacity );
7195 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007196 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007197
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007198 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7199 psa_set_key_algorithm( &base_attributes, alg );
7200 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007201 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007202 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007203
7204 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007205 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7206 input1->x, input1->len,
7207 input2->x, input2->len,
7208 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007209 goto exit;
7210
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007211 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007212 output_buffer,
7213 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007214 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007215
7216 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007217 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7218 input1->x, input1->len,
7219 input2->x, input2->len,
7220 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007221 goto exit;
7222
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007223 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7224 psa_set_key_algorithm( &derived_attributes, 0 );
7225 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007226 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007227 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007228 &derived_key ) );
7229 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007230 export_buffer, bytes1,
7231 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007232 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007233 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007234 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007235 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007236 &derived_key ) );
7237 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007238 export_buffer + bytes1, bytes2,
7239 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007240 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007241
7242 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007243 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7244 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007245
7246exit:
7247 mbedtls_free( output_buffer );
7248 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007249 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007250 psa_destroy_key( base_key );
7251 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007252 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007253}
7254/* END_CASE */
7255
7256/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007257void derive_key_type( int alg_arg,
7258 data_t *key_data,
7259 data_t *input1,
7260 data_t *input2,
7261 int key_type_arg, int bits_arg,
7262 data_t *expected_export )
7263{
7264 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7265 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
7266 const psa_algorithm_t alg = alg_arg;
7267 const psa_key_type_t key_type = key_type_arg;
7268 const size_t bits = bits_arg;
7269 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7270 const size_t export_buffer_size =
7271 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
7272 uint8_t *export_buffer = NULL;
7273 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7274 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7275 size_t export_length;
7276
7277 ASSERT_ALLOC( export_buffer, export_buffer_size );
7278 PSA_ASSERT( psa_crypto_init( ) );
7279
7280 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7281 psa_set_key_algorithm( &base_attributes, alg );
7282 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7283 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
7284 &base_key ) );
7285
Przemek Stekielc85f0912022-03-08 11:37:54 +01007286 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007287 &operation, base_key, alg,
7288 input1->x, input1->len,
7289 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01007290 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007291 goto exit;
7292
7293 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7294 psa_set_key_algorithm( &derived_attributes, 0 );
7295 psa_set_key_type( &derived_attributes, key_type );
7296 psa_set_key_bits( &derived_attributes, bits );
7297 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
7298 &derived_key ) );
7299
7300 PSA_ASSERT( psa_export_key( derived_key,
7301 export_buffer, export_buffer_size,
7302 &export_length ) );
7303 ASSERT_COMPARE( export_buffer, export_length,
7304 expected_export->x, expected_export->len );
7305
7306exit:
7307 mbedtls_free( export_buffer );
7308 psa_key_derivation_abort( &operation );
7309 psa_destroy_key( base_key );
7310 psa_destroy_key( derived_key );
7311 PSA_DONE( );
7312}
7313/* END_CASE */
7314
7315/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007316void derive_key( int alg_arg,
7317 data_t *key_data, data_t *input1, data_t *input2,
7318 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007319 int expected_status_arg,
7320 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02007321{
Ronald Cron5425a212020-08-04 14:58:35 +02007322 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7323 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02007324 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007325 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02007326 size_t bits = bits_arg;
7327 psa_status_t expected_status = expected_status_arg;
7328 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7329 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7330 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7331
7332 PSA_ASSERT( psa_crypto_init( ) );
7333
7334 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7335 psa_set_key_algorithm( &base_attributes, alg );
7336 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7337 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007338 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02007339
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007340 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7341 input1->x, input1->len,
7342 input2->x, input2->len,
7343 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02007344 goto exit;
7345
7346 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7347 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007348 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02007349 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01007350
7351 psa_status_t status =
7352 psa_key_derivation_output_key( &derived_attributes,
7353 &operation,
7354 &derived_key );
7355 if( is_large_output > 0 )
7356 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7357 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02007358
7359exit:
7360 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007361 psa_destroy_key( base_key );
7362 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02007363 PSA_DONE( );
7364}
7365/* END_CASE */
7366
7367/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02007368void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02007369 int our_key_type_arg, int our_key_alg_arg,
7370 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02007371 int expected_status_arg )
7372{
Ronald Cron5425a212020-08-04 14:58:35 +02007373 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007374 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007375 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007376 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007377 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007379 psa_status_t expected_status = expected_status_arg;
7380 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007381
Gilles Peskine8817f612018-12-18 00:18:46 +01007382 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007383
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007384 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007385 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007386 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007387 PSA_ASSERT( psa_import_key( &attributes,
7388 our_key_data->x, our_key_data->len,
7389 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007390
Gilles Peskine77f40d82019-04-11 21:27:06 +02007391 /* The tests currently include inputs that should fail at either step.
7392 * Test cases that fail at the setup step should be changed to call
7393 * key_derivation_setup instead, and this function should be renamed
7394 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007395 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02007396 if( status == PSA_SUCCESS )
7397 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007398 TEST_EQUAL( psa_key_derivation_key_agreement(
7399 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
7400 our_key,
7401 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02007402 expected_status );
7403 }
7404 else
7405 {
7406 TEST_ASSERT( status == expected_status );
7407 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007408
7409exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007410 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007411 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007412 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007413}
7414/* END_CASE */
7415
7416/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007417void raw_key_agreement( int alg_arg,
7418 int our_key_type_arg, data_t *our_key_data,
7419 data_t *peer_key_data,
7420 data_t *expected_output )
7421{
Ronald Cron5425a212020-08-04 14:58:35 +02007422 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007423 psa_algorithm_t alg = alg_arg;
7424 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007425 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007426 unsigned char *output = NULL;
7427 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007428 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007429
7430 ASSERT_ALLOC( output, expected_output->len );
7431 PSA_ASSERT( psa_crypto_init( ) );
7432
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007433 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7434 psa_set_key_algorithm( &attributes, alg );
7435 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007436 PSA_ASSERT( psa_import_key( &attributes,
7437 our_key_data->x, our_key_data->len,
7438 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007439
gabor-mezei-armceface22021-01-21 12:26:17 +01007440 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7441 key_bits = psa_get_key_bits( &attributes );
7442
Gilles Peskinebe697d82019-05-16 18:00:41 +02007443 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7444 peer_key_data->x, peer_key_data->len,
7445 output, expected_output->len,
7446 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007447 ASSERT_COMPARE( output, output_length,
7448 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01007449 TEST_ASSERT( output_length <=
7450 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7451 TEST_ASSERT( output_length <=
7452 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007453
7454exit:
7455 mbedtls_free( output );
7456 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007457 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007458}
7459/* END_CASE */
7460
7461/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007462void key_agreement_capacity( int alg_arg,
7463 int our_key_type_arg, data_t *our_key_data,
7464 data_t *peer_key_data,
7465 int expected_capacity_arg )
7466{
Ronald Cron5425a212020-08-04 14:58:35 +02007467 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007468 psa_algorithm_t alg = alg_arg;
7469 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007470 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007472 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007473 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007474
Gilles Peskine8817f612018-12-18 00:18:46 +01007475 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007476
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007477 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7478 psa_set_key_algorithm( &attributes, alg );
7479 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007480 PSA_ASSERT( psa_import_key( &attributes,
7481 our_key_data->x, our_key_data->len,
7482 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007483
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007484 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007485 PSA_ASSERT( psa_key_derivation_key_agreement(
7486 &operation,
7487 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7488 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007489 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7490 {
7491 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007492 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007493 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007494 NULL, 0 ) );
7495 }
Gilles Peskine59685592018-09-18 12:11:34 +02007496
Gilles Peskinebf491972018-10-25 22:36:12 +02007497 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007498 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007499 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007500 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007501
Gilles Peskinebf491972018-10-25 22:36:12 +02007502 /* Test the actual capacity by reading the output. */
7503 while( actual_capacity > sizeof( output ) )
7504 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007505 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007506 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007507 actual_capacity -= sizeof( output );
7508 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007509 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007510 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007511 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007512 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007513
Gilles Peskine59685592018-09-18 12:11:34 +02007514exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007515 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007516 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007517 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007518}
7519/* END_CASE */
7520
7521/* BEGIN_CASE */
7522void key_agreement_output( int alg_arg,
7523 int our_key_type_arg, data_t *our_key_data,
7524 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007525 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007526{
Ronald Cron5425a212020-08-04 14:58:35 +02007527 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007528 psa_algorithm_t alg = alg_arg;
7529 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007530 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007531 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007532 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007533
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007534 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7535 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007536
Gilles Peskine8817f612018-12-18 00:18:46 +01007537 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007538
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007539 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7540 psa_set_key_algorithm( &attributes, alg );
7541 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007542 PSA_ASSERT( psa_import_key( &attributes,
7543 our_key_data->x, our_key_data->len,
7544 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007545
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007546 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007547 PSA_ASSERT( psa_key_derivation_key_agreement(
7548 &operation,
7549 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7550 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007551 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7552 {
7553 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007554 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007555 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007556 NULL, 0 ) );
7557 }
Gilles Peskine59685592018-09-18 12:11:34 +02007558
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007559 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007560 actual_output,
7561 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007562 ASSERT_COMPARE( actual_output, expected_output1->len,
7563 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007564 if( expected_output2->len != 0 )
7565 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007566 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007567 actual_output,
7568 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007569 ASSERT_COMPARE( actual_output, expected_output2->len,
7570 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007571 }
Gilles Peskine59685592018-09-18 12:11:34 +02007572
7573exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007574 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007575 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007576 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007577 mbedtls_free( actual_output );
7578}
7579/* END_CASE */
7580
7581/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007582void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007583{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007584 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007585 unsigned char *output = NULL;
7586 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007587 size_t i;
7588 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007589
Simon Butcher49f8e312020-03-03 15:51:50 +00007590 TEST_ASSERT( bytes_arg >= 0 );
7591
Gilles Peskine91892022021-02-08 19:50:26 +01007592 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007593 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007594
Gilles Peskine8817f612018-12-18 00:18:46 +01007595 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007596
Gilles Peskinea50d7392018-06-21 10:22:13 +02007597 /* Run several times, to ensure that every output byte will be
7598 * nonzero at least once with overwhelming probability
7599 * (2^(-8*number_of_runs)). */
7600 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007601 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007602 if( bytes != 0 )
7603 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007604 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007605
Gilles Peskinea50d7392018-06-21 10:22:13 +02007606 for( i = 0; i < bytes; i++ )
7607 {
7608 if( output[i] != 0 )
7609 ++changed[i];
7610 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007611 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007612
7613 /* Check that every byte was changed to nonzero at least once. This
7614 * validates that psa_generate_random is overwriting every byte of
7615 * the output buffer. */
7616 for( i = 0; i < bytes; i++ )
7617 {
7618 TEST_ASSERT( changed[i] != 0 );
7619 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007620
7621exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007622 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007623 mbedtls_free( output );
7624 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007625}
7626/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007627
7628/* BEGIN_CASE */
7629void generate_key( int type_arg,
7630 int bits_arg,
7631 int usage_arg,
7632 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007633 int expected_status_arg,
7634 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007635{
Ronald Cron5425a212020-08-04 14:58:35 +02007636 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007637 psa_key_type_t type = type_arg;
7638 psa_key_usage_t usage = usage_arg;
7639 size_t bits = bits_arg;
7640 psa_algorithm_t alg = alg_arg;
7641 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007643 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007644
Gilles Peskine8817f612018-12-18 00:18:46 +01007645 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007646
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007647 psa_set_key_usage_flags( &attributes, usage );
7648 psa_set_key_algorithm( &attributes, alg );
7649 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007650 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007651
7652 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007653 psa_status_t status = psa_generate_key( &attributes, &key );
7654
7655 if( is_large_key > 0 )
7656 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7657 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007658 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007659 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007660
7661 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007662 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007663 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7664 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007665
Gilles Peskine818ca122018-06-20 18:16:48 +02007666 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007667 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007668 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007669
7670exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007671 /*
7672 * Key attributes may have been returned by psa_get_key_attributes()
7673 * thus reset them as required.
7674 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007675 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007676
Ronald Cron5425a212020-08-04 14:58:35 +02007677 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007678 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007679}
7680/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007681
Ronald Cronee414c72021-03-18 18:50:08 +01007682/* 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 +02007683void generate_key_rsa( int bits_arg,
7684 data_t *e_arg,
7685 int expected_status_arg )
7686{
Ronald Cron5425a212020-08-04 14:58:35 +02007687 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007688 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007689 size_t bits = bits_arg;
7690 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7691 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7692 psa_status_t expected_status = expected_status_arg;
7693 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7694 uint8_t *exported = NULL;
7695 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007696 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007697 size_t exported_length = SIZE_MAX;
7698 uint8_t *e_read_buffer = NULL;
7699 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007700 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007701 size_t e_read_length = SIZE_MAX;
7702
7703 if( e_arg->len == 0 ||
7704 ( e_arg->len == 3 &&
7705 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7706 {
7707 is_default_public_exponent = 1;
7708 e_read_size = 0;
7709 }
7710 ASSERT_ALLOC( e_read_buffer, e_read_size );
7711 ASSERT_ALLOC( exported, exported_size );
7712
7713 PSA_ASSERT( psa_crypto_init( ) );
7714
7715 psa_set_key_usage_flags( &attributes, usage );
7716 psa_set_key_algorithm( &attributes, alg );
7717 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7718 e_arg->x, e_arg->len ) );
7719 psa_set_key_bits( &attributes, bits );
7720
7721 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007722 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007723 if( expected_status != PSA_SUCCESS )
7724 goto exit;
7725
7726 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007727 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007728 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7729 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7730 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7731 e_read_buffer, e_read_size,
7732 &e_read_length ) );
7733 if( is_default_public_exponent )
7734 TEST_EQUAL( e_read_length, 0 );
7735 else
7736 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7737
7738 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007739 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007740 goto exit;
7741
7742 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007743 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007744 exported, exported_size,
7745 &exported_length ) );
7746 {
7747 uint8_t *p = exported;
7748 uint8_t *end = exported + exported_length;
7749 size_t len;
7750 /* RSAPublicKey ::= SEQUENCE {
7751 * modulus INTEGER, -- n
7752 * publicExponent INTEGER } -- e
7753 */
7754 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007755 MBEDTLS_ASN1_SEQUENCE |
7756 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007757 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007758 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7759 MBEDTLS_ASN1_INTEGER ) );
7760 if( len >= 1 && p[0] == 0 )
7761 {
7762 ++p;
7763 --len;
7764 }
7765 if( e_arg->len == 0 )
7766 {
7767 TEST_EQUAL( len, 3 );
7768 TEST_EQUAL( p[0], 1 );
7769 TEST_EQUAL( p[1], 0 );
7770 TEST_EQUAL( p[2], 1 );
7771 }
7772 else
7773 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7774 }
7775
7776exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007777 /*
7778 * Key attributes may have been returned by psa_get_key_attributes() or
7779 * set by psa_set_key_domain_parameters() thus reset them as required.
7780 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007781 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007782
Ronald Cron5425a212020-08-04 14:58:35 +02007783 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007784 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007785 mbedtls_free( e_read_buffer );
7786 mbedtls_free( exported );
7787}
7788/* END_CASE */
7789
Darryl Greend49a4992018-06-18 17:27:26 +01007790/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007791void persistent_key_load_key_from_storage( data_t *data,
7792 int type_arg, int bits_arg,
7793 int usage_flags_arg, int alg_arg,
7794 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007795{
Ronald Cron71016a92020-08-28 19:01:50 +02007796 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007798 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7799 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007800 psa_key_type_t type = type_arg;
7801 size_t bits = bits_arg;
7802 psa_key_usage_t usage_flags = usage_flags_arg;
7803 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007804 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007805 unsigned char *first_export = NULL;
7806 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007807 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007808 size_t first_exported_length;
7809 size_t second_exported_length;
7810
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007811 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7812 {
7813 ASSERT_ALLOC( first_export, export_size );
7814 ASSERT_ALLOC( second_export, export_size );
7815 }
Darryl Greend49a4992018-06-18 17:27:26 +01007816
Gilles Peskine8817f612018-12-18 00:18:46 +01007817 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007818
Gilles Peskinec87af662019-05-15 16:12:22 +02007819 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007820 psa_set_key_usage_flags( &attributes, usage_flags );
7821 psa_set_key_algorithm( &attributes, alg );
7822 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007823 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007824
Darryl Green0c6575a2018-11-07 16:05:30 +00007825 switch( generation_method )
7826 {
7827 case IMPORT_KEY:
7828 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007829 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007830 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007831 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007832
Darryl Green0c6575a2018-11-07 16:05:30 +00007833 case GENERATE_KEY:
7834 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007835 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007836 break;
7837
7838 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007839#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007840 {
7841 /* Create base key */
7842 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7843 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7844 psa_set_key_usage_flags( &base_attributes,
7845 PSA_KEY_USAGE_DERIVE );
7846 psa_set_key_algorithm( &base_attributes, derive_alg );
7847 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007848 PSA_ASSERT( psa_import_key( &base_attributes,
7849 data->x, data->len,
7850 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007851 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007852 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007853 PSA_ASSERT( psa_key_derivation_input_key(
7854 &operation,
7855 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007856 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007857 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007858 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007859 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7860 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007861 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007862 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007863 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007864 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007865 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007866#else
7867 TEST_ASSUME( ! "KDF not supported in this configuration" );
7868#endif
7869 break;
7870
7871 default:
7872 TEST_ASSERT( ! "generation_method not implemented in test" );
7873 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007874 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007875 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007876
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007877 /* Export the key if permitted by the key policy. */
7878 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7879 {
Ronald Cron5425a212020-08-04 14:58:35 +02007880 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007881 first_export, export_size,
7882 &first_exported_length ) );
7883 if( generation_method == IMPORT_KEY )
7884 ASSERT_COMPARE( data->x, data->len,
7885 first_export, first_exported_length );
7886 }
Darryl Greend49a4992018-06-18 17:27:26 +01007887
7888 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007889 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007890 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007891 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007892
Darryl Greend49a4992018-06-18 17:27:26 +01007893 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007894 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007895 TEST_ASSERT( mbedtls_svc_key_id_equal(
7896 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007897 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7898 PSA_KEY_LIFETIME_PERSISTENT );
7899 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7900 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007901 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007902 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007903 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007904
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007905 /* Export the key again if permitted by the key policy. */
7906 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007907 {
Ronald Cron5425a212020-08-04 14:58:35 +02007908 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007909 second_export, export_size,
7910 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007911 ASSERT_COMPARE( first_export, first_exported_length,
7912 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007913 }
7914
7915 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007916 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007917 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007918
7919exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007920 /*
7921 * Key attributes may have been returned by psa_get_key_attributes()
7922 * thus reset them as required.
7923 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007924 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007925
Darryl Greend49a4992018-06-18 17:27:26 +01007926 mbedtls_free( first_export );
7927 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007928 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007929 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007930 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007931 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007932}
7933/* END_CASE */