blob: a29950585cb279348bdf780fd16742fbc088c38d [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010024
Gilles Peskine4023c012021-05-27 13:21:20 +020025/* If this comes up, it's a bug in the test code or in the test data. */
26#define UNUSED 0xdeadbeef
27
Dave Rodgman647791d2021-06-23 12:49:59 +010028/* Assert that an operation is (not) active.
29 * This serves as a proxy for checking if the operation is aborted. */
30#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
31#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010032
33/** An invalid export length that will never be set by psa_export_key(). */
34static const size_t INVALID_EXPORT_LENGTH = ~0U;
35
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036/** Test if a buffer contains a constant byte value.
37 *
38 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 *
40 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042 * \param size Size of the buffer in bytes.
43 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 * \return 1 if the buffer is all-bits-zero.
45 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048{
49 size_t i;
50 for( i = 0; i < size; i++ )
51 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020052 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020055 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020056}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010057#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
59static int asn1_write_10x( unsigned char **p,
60 unsigned char *start,
61 size_t bits,
62 unsigned char x )
63{
64 int ret;
65 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020066 if( bits == 0 )
67 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
68 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020069 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030070 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020071 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
72 *p -= len;
73 ( *p )[len-1] = x;
74 if( bits % 8 == 0 )
75 ( *p )[1] |= 1;
76 else
77 ( *p )[0] |= 1 << ( bits % 8 );
78 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
80 MBEDTLS_ASN1_INTEGER ) );
81 return( len );
82}
83
84static int construct_fake_rsa_key( unsigned char *buffer,
85 size_t buffer_size,
86 unsigned char **p,
87 size_t bits,
88 int keypair )
89{
90 size_t half_bits = ( bits + 1 ) / 2;
91 int ret;
92 int len = 0;
93 /* Construct something that looks like a DER encoding of
94 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
95 * RSAPrivateKey ::= SEQUENCE {
96 * version Version,
97 * modulus INTEGER, -- n
98 * publicExponent INTEGER, -- e
99 * privateExponent INTEGER, -- d
100 * prime1 INTEGER, -- p
101 * prime2 INTEGER, -- q
102 * exponent1 INTEGER, -- d mod (p-1)
103 * exponent2 INTEGER, -- d mod (q-1)
104 * coefficient INTEGER, -- (inverse of q) mod p
105 * otherPrimeInfos OtherPrimeInfos OPTIONAL
106 * }
107 * Or, for a public key, the same structure with only
108 * version, modulus and publicExponent.
109 */
110 *p = buffer + buffer_size;
111 if( keypair )
112 {
113 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
116 asn1_write_10x( p, buffer, half_bits, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
118 asn1_write_10x( p, buffer, half_bits, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* q */
120 asn1_write_10x( p, buffer, half_bits, 1 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x( p, buffer, half_bits, 3 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* d */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 }
126 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
127 asn1_write_10x( p, buffer, 17, 1 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, /* n */
129 asn1_write_10x( p, buffer, bits, 1 ) );
130 if( keypair )
131 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
132 mbedtls_asn1_write_int( p, buffer, 0 ) );
133 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
134 {
135 const unsigned char tag =
136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
137 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
138 }
139 return( len );
140}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100141#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100143int exercise_mac_setup( psa_key_type_t key_type,
144 const unsigned char *key_bytes,
145 size_t key_length,
146 psa_algorithm_t alg,
147 psa_mac_operation_t *operation,
148 psa_status_t *status )
149{
Ronald Cron5425a212020-08-04 14:58:35 +0200150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200154 psa_set_key_algorithm( &attributes, alg );
155 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200156 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157
Ronald Cron5425a212020-08-04 14:58:35 +0200158 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100159 /* Whether setup succeeded or failed, abort must succeed. */
160 PSA_ASSERT( psa_mac_abort( operation ) );
161 /* If setup failed, reproduce the failure, so that the caller can
162 * test the resulting state of the operation object. */
163 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 {
Ronald Cron5425a212020-08-04 14:58:35 +0200165 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 }
167
Ronald Cron5425a212020-08-04 14:58:35 +0200168 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100169 return( 1 );
170
171exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200172 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173 return( 0 );
174}
175
176int exercise_cipher_setup( psa_key_type_t key_type,
177 const unsigned char *key_bytes,
178 size_t key_length,
179 psa_algorithm_t alg,
180 psa_cipher_operation_t *operation,
181 psa_status_t *status )
182{
Ronald Cron5425a212020-08-04 14:58:35 +0200183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
187 psa_set_key_algorithm( &attributes, alg );
188 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200189 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190
Ronald Cron5425a212020-08-04 14:58:35 +0200191 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* Whether setup succeeded or failed, abort must succeed. */
193 PSA_ASSERT( psa_cipher_abort( operation ) );
194 /* If setup failed, reproduce the failure, so that the caller can
195 * test the resulting state of the operation object. */
196 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 {
Ronald Cron5425a212020-08-04 14:58:35 +0200198 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100199 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 }
201
Ronald Cron5425a212020-08-04 14:58:35 +0200202 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203 return( 1 );
204
205exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200206 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207 return( 0 );
208}
209
Ronald Cron5425a212020-08-04 14:58:35 +0200210static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211{
212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 uint8_t buffer[1];
215 size_t length;
216 int ok = 0;
217
Ronald Cronecfb2372020-07-23 17:13:42 +0200218 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
220 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
221 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200224 TEST_EQUAL(
225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
226 TEST_EQUAL(
227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200228 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200229 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
233
Ronald Cron5425a212020-08-04 14:58:35 +0200234 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000235 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200236 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000238 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 ok = 1;
241
242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243 /*
244 * Key attributes may have been returned by psa_get_key_attributes()
245 * thus reset them as required.
246 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200247 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100248
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200249 return( ok );
250}
251
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252/* Assert that a key isn't reported as having a slot number. */
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
255 do \
256 { \
257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
258 TEST_EQUAL( psa_get_key_slot_number( \
259 attributes, \
260 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
261 PSA_ERROR_INVALID_ARGUMENT ); \
262 } \
263 while( 0 )
264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
265#define ASSERT_NO_SLOT_NUMBER( attributes ) \
266 ( (void) 0 )
267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100269/* An overapproximation of the amount of storage needed for a key of the
270 * given type and with the given content. The API doesn't make it easy
271 * to find a good value for the size. The current implementation doesn't
272 * care about the value anyway. */
273#define KEY_BITS_FROM_DATA( type, data ) \
274 ( data )->len
275
Darryl Green0c6575a2018-11-07 16:05:30 +0000276typedef enum {
277 IMPORT_KEY = 0,
278 GENERATE_KEY = 1,
279 DERIVE_KEY = 2
280} generate_method;
281
Paul Elliott33746aa2021-09-15 16:40:40 +0100282typedef enum
283{
284 DO_NOT_SET_LENGTHS = 0,
285 SET_LENGTHS_BEFORE_NONCE = 1,
286 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100287} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100288
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100289typedef enum
290{
291 USE_NULL_TAG = 0,
292 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100293} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100294
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100295/*!
296 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100297 * \param key_type_arg Type of key passed in
298 * \param key_data The encryption / decryption key data
299 * \param alg_arg The type of algorithm used
300 * \param nonce Nonce data
301 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100302 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 * feed additional data in to be encrypted /
304 * decrypted. If -1, no chunking.
305 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100306 * \param data_part_len_arg If not -1, the length of chunks to feed
307 * the data in to be encrypted / decrypted. If
308 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100309 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100310 * expected here, this controls whether or not
311 * to set lengths, and in what order with
312 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100315 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100316 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 */
319static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
320 int alg_arg,
321 data_t *nonce,
322 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100323 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100325 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100326 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100328 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100329 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100330{
331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
332 psa_key_type_t key_type = key_type_arg;
333 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100334 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100335 unsigned char *output_data = NULL;
336 unsigned char *part_data = NULL;
337 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100339 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 size_t output_size = 0;
341 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 size_t output_length = 0;
343 size_t key_bits = 0;
344 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100345 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100346 size_t part_length = 0;
347 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100349 size_t ad_part_len = 0;
350 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100351 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100356 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100357
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 PSA_ASSERT( psa_crypto_init( ) );
359
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 if( is_encrypt )
361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
362 else
363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
364
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 psa_set_key_algorithm( &attributes, alg );
366 psa_set_key_type( &attributes, key_type );
367
368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
369 &key ) );
370
371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
372 key_bits = psa_get_key_bits( &attributes );
373
374 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 if( is_encrypt )
377 {
378 /* Tag gets written at end of buffer. */
379 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
380 ( input_data->len +
381 tag_length ) );
382 data_true_size = input_data->len;
383 }
384 else
385 {
386 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
387 ( input_data->len -
388 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 /* Do not want to attempt to decrypt tag. */
391 data_true_size = input_data->len - tag_length;
392 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 ASSERT_ALLOC( output_data, output_size );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396 if( is_encrypt )
397 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100398 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
399 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 }
401 else
402 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100403 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
404 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100405 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100407 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100408
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100409 if( is_encrypt )
410 status = psa_aead_encrypt_setup( &operation, key, alg );
411 else
412 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
414 /* If the operation is not supported, just skip and not fail in case the
415 * encryption involves a common limitation of cryptography hardwares and
416 * an alternative implementation. */
417 if( status == PSA_ERROR_NOT_SUPPORTED )
418 {
419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
421 }
422
423 PSA_ASSERT( status );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 if( set_lengths_method == DO_NOT_SET_LENGTHS )
426 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
427 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100428 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100429 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
430 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
432 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100433 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100434 {
435 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
436
Paul Elliott33746aa2021-09-15 16:40:40 +0100437 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
438 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100441 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
443 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100444 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100446 for( part_offset = 0, part_count = 0;
447 part_offset < additional_data->len;
448 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100450 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 {
Paul Elliott329d5382021-07-22 17:10:45 +0100452 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100453 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100454 else if( additional_data->len - part_offset < ad_part_len )
455 {
456 part_length = additional_data->len - part_offset;
457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 else
459 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100460 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
462
463 PSA_ASSERT( psa_aead_update_ad( &operation,
464 additional_data->x + part_offset,
465 part_length ) );
466
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 }
468 }
469 else
470 {
471 /* Pass additional data in one go. */
472 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
473 additional_data->len ) );
474 }
475
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100476 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
478 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100479 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100481 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
483 ASSERT_ALLOC( part_data, part_data_size );
484
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100485 for( part_offset = 0, part_count = 0;
486 part_offset < data_true_size;
487 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100489 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 {
Paul Elliott329d5382021-07-22 17:10:45 +0100491 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100493 else if( ( data_true_size - part_offset ) < data_part_len )
494 {
495 part_length = ( data_true_size - part_offset );
496 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 else
498 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
502 PSA_ASSERT( psa_aead_update( &operation,
503 ( input_data->x + part_offset ),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length ) );
507
508 if( output_data && output_part_length )
509 {
510 memcpy( ( output_data + part_offset ), part_data,
511 output_part_length );
512 }
513
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 output_length += output_part_length;
515 }
516 }
517 else
518 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 output_size, &output_length ) );
523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( is_encrypt )
526 PSA_ASSERT( psa_aead_finish( &operation, final_data,
527 final_output_size,
528 &output_part_length,
529 tag_buffer, tag_length,
530 &tag_size ) );
531 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100532 {
Paul Elliott9961a662021-09-17 19:19:02 +0100533 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 final_output_size,
535 &output_part_length,
536 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100537 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100540 if( output_data && output_part_length )
541 memcpy( ( output_data + output_length ), final_data,
542 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
549 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551 TEST_EQUAL( tag_length, tag_size );
552
553 if( output_data && tag_length )
554 memcpy( ( output_data + output_length ), tag_buffer,
555 tag_length );
556
557 output_length += tag_length;
558
559 TEST_EQUAL( output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_ASSERT( output_length <=
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
570 TEST_ASSERT( output_length <=
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Gilles Peskinee59236f2018-01-27 23:32:46 +0100592/* END_HEADER */
593
594/* BEGIN_DEPENDENCIES
595 * depends_on:MBEDTLS_PSA_CRYPTO_C
596 * END_DEPENDENCIES
597 */
598
599/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200600void static_checks( )
601{
602 size_t max_truncated_mac_size =
603 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
604
605 /* Check that the length for a truncated MAC always fits in the algorithm
606 * encoding. The shifted mask is the maximum truncated value. The
607 * untruncated algorithm may be one byte larger. */
608 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
609}
610/* END_CASE */
611
612/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200613void import_with_policy( int type_arg,
614 int usage_arg, int alg_arg,
615 int expected_status_arg )
616{
617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
618 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200620 psa_key_type_t type = type_arg;
621 psa_key_usage_t usage = usage_arg;
622 psa_algorithm_t alg = alg_arg;
623 psa_status_t expected_status = expected_status_arg;
624 const uint8_t key_material[16] = {0};
625 psa_status_t status;
626
627 PSA_ASSERT( psa_crypto_init( ) );
628
629 psa_set_key_type( &attributes, type );
630 psa_set_key_usage_flags( &attributes, usage );
631 psa_set_key_algorithm( &attributes, alg );
632
633 status = psa_import_key( &attributes,
634 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200635 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636 TEST_EQUAL( status, expected_status );
637 if( status != PSA_SUCCESS )
638 goto exit;
639
Ronald Cron5425a212020-08-04 14:58:35 +0200640 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200642 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200643 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200644 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200645 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200646
Ronald Cron5425a212020-08-04 14:58:35 +0200647 PSA_ASSERT( psa_destroy_key( key ) );
648 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200649
650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100651 /*
652 * Key attributes may have been returned by psa_get_key_attributes()
653 * thus reset them as required.
654 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200655 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100656
657 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200658 PSA_DONE( );
659}
660/* END_CASE */
661
662/* BEGIN_CASE */
663void import_with_data( data_t *data, int type_arg,
664 int attr_bits_arg,
665 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200666{
667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
668 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200670 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200671 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200672 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
Gilles Peskine4747d192019-04-17 15:05:45 +0200677 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200678 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200679
Ronald Cron5425a212020-08-04 14:58:35 +0200680 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100681 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200682 if( status != PSA_SUCCESS )
683 goto exit;
684
Ronald Cron5425a212020-08-04 14:58:35 +0200685 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200686 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200687 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200688 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200689 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200690
Ronald Cron5425a212020-08-04 14:58:35 +0200691 PSA_ASSERT( psa_destroy_key( key ) );
692 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693
694exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100695 /*
696 * Key attributes may have been returned by psa_get_key_attributes()
697 * thus reset them as required.
698 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200699 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100700
701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200702 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703}
704/* END_CASE */
705
706/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200707void import_large_key( int type_arg, int byte_size_arg,
708 int expected_status_arg )
709{
710 psa_key_type_t type = type_arg;
711 size_t byte_size = byte_size_arg;
712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
713 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200715 psa_status_t status;
716 uint8_t *buffer = NULL;
717 size_t buffer_size = byte_size + 1;
718 size_t n;
719
Steven Cooreman69967ce2021-01-18 18:01:08 +0100720 /* Skip the test case if the target running the test cannot
721 * accomodate large keys due to heap size constraints */
722 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200723 memset( buffer, 'K', byte_size );
724
725 PSA_ASSERT( psa_crypto_init( ) );
726
727 /* Try importing the key */
728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
729 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200730 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100731 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200732 TEST_EQUAL( status, expected_status );
733
734 if( status == PSA_SUCCESS )
735 {
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200737 TEST_EQUAL( psa_get_key_type( &attributes ), type );
738 TEST_EQUAL( psa_get_key_bits( &attributes ),
739 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200740 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200742 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200743 for( n = 0; n < byte_size; n++ )
744 TEST_EQUAL( buffer[n], 'K' );
745 for( n = byte_size; n < buffer_size; n++ )
746 TEST_EQUAL( buffer[n], 0 );
747 }
748
749exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100750 /*
751 * Key attributes may have been returned by psa_get_key_attributes()
752 * thus reset them as required.
753 */
754 psa_reset_key_attributes( &attributes );
755
Ronald Cron5425a212020-08-04 14:58:35 +0200756 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200757 PSA_DONE( );
758 mbedtls_free( buffer );
759}
760/* END_CASE */
761
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100762/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
764{
Ronald Cron5425a212020-08-04 14:58:35 +0200765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200766 size_t bits = bits_arg;
767 psa_status_t expected_status = expected_status_arg;
768 psa_status_t status;
769 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200770 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200771 size_t buffer_size = /* Slight overapproximations */
772 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200773 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200774 unsigned char *p;
775 int ret;
776 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778
Gilles Peskine8817f612018-12-18 00:18:46 +0100779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200780 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200781
782 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
783 bits, keypair ) ) >= 0 );
784 length = ret;
785
786 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200787 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200788 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100789 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200790
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200791 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200792 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200793
794exit:
795 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200796 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200797}
798/* END_CASE */
799
800/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300801void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300802 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200803 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530804 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 int expected_bits,
806 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200807 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 int canonical_input )
809{
Ronald Cron5425a212020-08-04 14:58:35 +0200810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200812 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200813 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530815 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 unsigned char *exported = NULL;
817 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100819 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200822 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823
Moran Pekercb088e72018-07-17 17:36:59 +0300824 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200825 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200827 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829
Archana4d7ae1d2021-07-07 02:50:22 +0530830 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200831 psa_set_key_usage_flags( &attributes, usage_arg );
832 psa_set_key_algorithm( &attributes, alg );
833 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700834
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200836 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837
838 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200839 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200840 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
841 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200842 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843
844 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200845 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100846 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100847
848 /* The exported length must be set by psa_export_key() to a value between 0
849 * and export_size. On errors, the exported length must be 0. */
850 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
851 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
852 TEST_ASSERT( exported_length <= export_size );
853
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200854 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200855 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100856 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200857 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200860 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861
Gilles Peskineea38a922021-02-13 00:05:16 +0100862 /* Run sanity checks on the exported key. For non-canonical inputs,
863 * this validates the canonical representations. For canonical inputs,
864 * this doesn't directly validate the implementation, but it still helps
865 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530866 if( !psa_key_lifetime_is_external( lifetime ) )
867 {
868 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
869 goto exit;
870 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200873 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 else
875 {
Ronald Cron5425a212020-08-04 14:58:35 +0200876 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200877 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200878 &key2 ) );
879 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100880 reexported,
881 export_size,
882 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200883 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +0530884 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100887 TEST_ASSERT( exported_length <=
Archana4d7ae1d2021-07-07 02:50:22 +0530888 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +0530889 psa_get_key_bits( &got_attributes ) ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100890 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891
892destroy:
893 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200894 PSA_ASSERT( psa_destroy_key( key ) );
895 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896
897exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100898 /*
899 * Key attributes may have been returned by psa_get_key_attributes()
900 * thus reset them as required.
901 */
902 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +0530903 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300904 mbedtls_free( exported );
905 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200906 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907}
908/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100909
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300911void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200912 int type_arg,
913 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530914 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 int export_size_delta,
916 int expected_export_status_arg,
917 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918{
Ronald Cron5425a212020-08-04 14:58:35 +0200919 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300920 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200921 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200922 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300923 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530924 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300925 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100926 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100927 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929
Gilles Peskine8817f612018-12-18 00:18:46 +0100930 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300931
Archana4d7ae1d2021-07-07 02:50:22 +0530932 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
934 psa_set_key_algorithm( &attributes, alg );
935 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300936
937 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200938 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300939
Gilles Peskine49c25912018-10-29 15:15:31 +0100940 /* Export the public key */
941 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200942 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200943 exported, export_size,
944 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100946 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100947 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200948 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100949 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200950 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200951 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100952 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100953 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100954 TEST_ASSERT( expected_public_key->len <=
955 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
956 TEST_ASSERT( expected_public_key->len <=
957 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100958 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
959 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100960 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300961exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100962 /*
963 * Key attributes may have been returned by psa_get_key_attributes()
964 * thus reset them as required.
965 */
966 psa_reset_key_attributes( &attributes );
967
itayzafrir3e02b3b2018-06-12 17:06:52 +0300968 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200969 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200970 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300971}
972/* END_CASE */
973
Gilles Peskine20035e32018-02-03 22:44:14 +0100974/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975void import_and_exercise_key( data_t *data,
976 int type_arg,
977 int bits_arg,
978 int alg_arg )
979{
Ronald Cron5425a212020-08-04 14:58:35 +0200980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200981 psa_key_type_t type = type_arg;
982 size_t bits = bits_arg;
983 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100984 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200986 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200987
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
Gilles Peskine4747d192019-04-17 15:05:45 +0200990 psa_set_key_usage_flags( &attributes, usage );
991 psa_set_key_algorithm( &attributes, alg );
992 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993
994 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200995 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200996
997 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200998 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200999 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1000 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001001
1002 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001003 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001004 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001005
Ronald Cron5425a212020-08-04 14:58:35 +02001006 PSA_ASSERT( psa_destroy_key( key ) );
1007 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001008
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001009exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001010 /*
1011 * Key attributes may have been returned by psa_get_key_attributes()
1012 * thus reset them as required.
1013 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001014 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001015
1016 psa_reset_key_attributes( &attributes );
1017 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001018 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001019}
1020/* END_CASE */
1021
1022/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001023void effective_key_attributes( int type_arg, int expected_type_arg,
1024 int bits_arg, int expected_bits_arg,
1025 int usage_arg, int expected_usage_arg,
1026 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001027{
Ronald Cron5425a212020-08-04 14:58:35 +02001028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001029 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001030 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001031 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001032 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001033 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001034 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001035 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001036 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
Gilles Peskine8817f612018-12-18 00:18:46 +01001039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001040
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001041 psa_set_key_usage_flags( &attributes, usage );
1042 psa_set_key_algorithm( &attributes, alg );
1043 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001044 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001047 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048
Ronald Cron5425a212020-08-04 14:58:35 +02001049 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001050 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1051 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1052 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1053 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001054
1055exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001056 /*
1057 * Key attributes may have been returned by psa_get_key_attributes()
1058 * thus reset them as required.
1059 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001060 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001061
1062 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001063 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001064}
1065/* END_CASE */
1066
1067/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001068void check_key_policy( int type_arg, int bits_arg,
1069 int usage_arg, int alg_arg )
1070{
1071 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001072 usage_arg,
1073 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001074 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001075 goto exit;
1076}
1077/* END_CASE */
1078
1079/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001080void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001081{
1082 /* Test each valid way of initializing the object, except for `= {0}`, as
1083 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1084 * though it's OK by the C standard. We could test for this, but we'd need
1085 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001086 psa_key_attributes_t func = psa_key_attributes_init( );
1087 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1088 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001089
1090 memset( &zero, 0, sizeof( zero ) );
1091
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001092 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1093 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1094 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001096 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1097 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1098 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1099
1100 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1101 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1102 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1103
1104 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1105 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1106 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1107
1108 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1109 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1110 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001111}
1112/* END_CASE */
1113
1114/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001115void mac_key_policy( int policy_usage_arg,
1116 int policy_alg_arg,
1117 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001119 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001120 int expected_status_sign_arg,
1121 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001126 psa_key_type_t key_type = key_type_arg;
1127 psa_algorithm_t policy_alg = policy_alg_arg;
1128 psa_algorithm_t exercise_alg = exercise_alg_arg;
1129 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001130 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001131 psa_status_t expected_status_sign = expected_status_sign_arg;
1132 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001134
Gilles Peskine8817f612018-12-18 00:18:46 +01001135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001137 psa_set_key_usage_flags( &attributes, policy_usage );
1138 psa_set_key_algorithm( &attributes, policy_alg );
1139 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001140
Gilles Peskine049c7532019-05-15 20:22:09 +02001141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001142 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001143
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001144 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1145 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001146
Ronald Cron5425a212020-08-04 14:58:35 +02001147 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001148 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001149
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001150 /* Calculate the MAC, one-shot case. */
1151 uint8_t input[128] = {0};
1152 size_t mac_len;
1153 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1154 input, 128,
1155 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1156 expected_status_sign );
1157
Neil Armstrong3af9b972022-02-07 12:20:21 +01001158 /* Calculate the MAC, multi-part case. */
1159 PSA_ASSERT( psa_mac_abort( &operation ) );
1160 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1161 if( status == PSA_SUCCESS )
1162 {
1163 status = psa_mac_update( &operation, input, 128 );
1164 if( status == PSA_SUCCESS )
1165 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1166 &mac_len ),
1167 expected_status_sign );
1168 else
1169 TEST_EQUAL( status, expected_status_sign );
1170 }
1171 else
1172 {
1173 TEST_EQUAL( status, expected_status_sign );
1174 }
1175 PSA_ASSERT( psa_mac_abort( &operation ) );
1176
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001177 /* Verify correct MAC, one-shot case. */
1178 status = psa_mac_verify( key, exercise_alg, input, 128,
1179 mac, mac_len );
1180
1181 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1182 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001183 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001184 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001185
Neil Armstrong3af9b972022-02-07 12:20:21 +01001186 /* Verify correct MAC, multi-part case. */
1187 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1188 if( status == PSA_SUCCESS )
1189 {
1190 status = psa_mac_update( &operation, input, 128 );
1191 if( status == PSA_SUCCESS )
1192 {
1193 status = psa_mac_verify_finish( &operation, mac, mac_len );
1194 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1195 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1196 else
1197 TEST_EQUAL( status, expected_status_verify );
1198 }
1199 else
1200 {
1201 TEST_EQUAL( status, expected_status_verify );
1202 }
1203 }
1204 else
1205 {
1206 TEST_EQUAL( status, expected_status_verify );
1207 }
1208
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001210
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001212 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001213 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214
1215exit:
1216 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001218 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219}
1220/* END_CASE */
1221
1222/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001223void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001224 int policy_alg,
1225 int key_type,
1226 data_t *key_data,
1227 int exercise_alg )
1228{
Ronald Cron5425a212020-08-04 14:58:35 +02001229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001231 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001232 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001233 psa_status_t status;
1234
Gilles Peskine8817f612018-12-18 00:18:46 +01001235 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001236
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001237 psa_set_key_usage_flags( &attributes, policy_usage );
1238 psa_set_key_algorithm( &attributes, policy_alg );
1239 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001240
Gilles Peskine049c7532019-05-15 20:22:09 +02001241 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001242 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001243
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001244 /* Check if no key usage flag implication is done */
1245 TEST_EQUAL( policy_usage,
1246 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001247
Ronald Cron5425a212020-08-04 14:58:35 +02001248 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001249 if( policy_alg == exercise_alg &&
1250 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001251 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001253 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001254 psa_cipher_abort( &operation );
1255
Ronald Cron5425a212020-08-04 14:58:35 +02001256 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001257 if( policy_alg == exercise_alg &&
1258 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001259 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001260 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001261 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001262
1263exit:
1264 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001265 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001266 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001267}
1268/* END_CASE */
1269
1270/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001271void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001272 int policy_alg,
1273 int key_type,
1274 data_t *key_data,
1275 int nonce_length_arg,
1276 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001277 int exercise_alg,
1278 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001279{
Ronald Cron5425a212020-08-04 14:58:35 +02001280 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001282 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001283 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001284 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001285 unsigned char nonce[16] = {0};
1286 size_t nonce_length = nonce_length_arg;
1287 unsigned char tag[16];
1288 size_t tag_length = tag_length_arg;
1289 size_t output_length;
1290
1291 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1292 TEST_ASSERT( tag_length <= sizeof( tag ) );
1293
Gilles Peskine8817f612018-12-18 00:18:46 +01001294 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001295
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001296 psa_set_key_usage_flags( &attributes, policy_usage );
1297 psa_set_key_algorithm( &attributes, policy_alg );
1298 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001299
Gilles Peskine049c7532019-05-15 20:22:09 +02001300 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001301 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001302
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001303 /* Check if no key usage implication is done */
1304 TEST_EQUAL( policy_usage,
1305 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001306
Ronald Cron5425a212020-08-04 14:58:35 +02001307 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001308 nonce, nonce_length,
1309 NULL, 0,
1310 NULL, 0,
1311 tag, tag_length,
1312 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001313 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1314 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001315 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001316 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317
1318 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001319 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 nonce, nonce_length,
1321 NULL, 0,
1322 tag, tag_length,
1323 NULL, 0,
1324 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001325 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1326 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1327 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001328 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001329 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001330 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001331
1332exit:
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 asymmetric_encryption_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;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001347 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001348 psa_status_t status;
1349 size_t key_bits;
1350 size_t buffer_length;
1351 unsigned char *buffer = NULL;
1352 size_t output_length;
1353
Gilles Peskine8817f612018-12-18 00:18:46 +01001354 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001355
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001356 psa_set_key_usage_flags( &attributes, policy_usage );
1357 psa_set_key_algorithm( &attributes, policy_alg );
1358 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001359
Gilles Peskine049c7532019-05-15 20:22:09 +02001360 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001361 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001362
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001363 /* Check if no key usage implication is done */
1364 TEST_EQUAL( policy_usage,
1365 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001366
Ronald Cron5425a212020-08-04 14:58:35 +02001367 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001368 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001369 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1370 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001371 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001372
Ronald Cron5425a212020-08-04 14:58:35 +02001373 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001374 NULL, 0,
1375 NULL, 0,
1376 buffer, buffer_length,
1377 &output_length );
1378 if( policy_alg == exercise_alg &&
1379 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001380 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001381 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001382 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001383
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001384 if( buffer_length != 0 )
1385 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001386 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001387 buffer, buffer_length,
1388 NULL, 0,
1389 buffer, buffer_length,
1390 &output_length );
1391 if( policy_alg == exercise_alg &&
1392 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001393 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001394 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001395 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001396
1397exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001398 /*
1399 * Key attributes may have been returned by psa_get_key_attributes()
1400 * thus reset them as required.
1401 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001402 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001403
1404 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001405 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001406 mbedtls_free( buffer );
1407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001411void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001412 int policy_alg,
1413 int key_type,
1414 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001415 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001416 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001417 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001418{
Ronald Cron5425a212020-08-04 14:58:35 +02001419 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001421 psa_key_usage_t policy_usage = policy_usage_arg;
1422 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001423 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001424 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1425 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1426 * compatible with the policy and `payload_length_arg` is supposed to be
1427 * a valid input length to sign. If `payload_length_arg <= 0`,
1428 * `exercise_alg` is supposed to be forbidden by the policy. */
1429 int compatible_alg = payload_length_arg > 0;
1430 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001431 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001432 size_t signature_length;
1433
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001434 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001435 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001436 TEST_EQUAL( expected_usage,
1437 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001438
Gilles Peskine8817f612018-12-18 00:18:46 +01001439 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001440
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001441 psa_set_key_usage_flags( &attributes, policy_usage );
1442 psa_set_key_algorithm( &attributes, policy_alg );
1443 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001444
Gilles Peskine049c7532019-05-15 20:22:09 +02001445 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001446 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001447
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001448 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1449
Ronald Cron5425a212020-08-04 14:58:35 +02001450 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001451 payload, payload_length,
1452 signature, sizeof( signature ),
1453 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001454 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001455 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001456 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001457 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001458
1459 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001460 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001461 payload, payload_length,
1462 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001463 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001464 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001465 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001466 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001467
Gilles Peskinef7b41372021-09-22 16:15:05 +02001468 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001469 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001470 {
1471 status = psa_sign_message( key, exercise_alg,
1472 payload, payload_length,
1473 signature, sizeof( signature ),
1474 &signature_length );
1475 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1476 PSA_ASSERT( status );
1477 else
1478 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1479
1480 memset( signature, 0, sizeof( signature ) );
1481 status = psa_verify_message( key, exercise_alg,
1482 payload, payload_length,
1483 signature, sizeof( signature ) );
1484 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1485 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1486 else
1487 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1488 }
1489
Gilles Peskined5b33222018-06-18 22:20:03 +02001490exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001491 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001492 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001493}
1494/* END_CASE */
1495
Janos Follathba3fab92019-06-11 14:50:16 +01001496/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001497void derive_key_policy( int policy_usage,
1498 int policy_alg,
1499 int key_type,
1500 data_t *key_data,
1501 int exercise_alg )
1502{
Ronald Cron5425a212020-08-04 14:58:35 +02001503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001505 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001506 psa_status_t status;
1507
Gilles Peskine8817f612018-12-18 00:18:46 +01001508 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001509
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001510 psa_set_key_usage_flags( &attributes, policy_usage );
1511 psa_set_key_algorithm( &attributes, policy_alg );
1512 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001513
Gilles Peskine049c7532019-05-15 20:22:09 +02001514 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001515 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001516
Janos Follathba3fab92019-06-11 14:50:16 +01001517 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1518
1519 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1520 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001521 {
Janos Follathba3fab92019-06-11 14:50:16 +01001522 PSA_ASSERT( psa_key_derivation_input_bytes(
1523 &operation,
1524 PSA_KEY_DERIVATION_INPUT_SEED,
1525 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001526 }
Janos Follathba3fab92019-06-11 14:50:16 +01001527
1528 status = psa_key_derivation_input_key( &operation,
1529 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001530 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001531
Gilles Peskineea0fb492018-07-12 17:17:20 +02001532 if( policy_alg == exercise_alg &&
1533 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001534 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001535 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001536 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001537
1538exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001539 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001540 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001541 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001542}
1543/* END_CASE */
1544
1545/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001546void agreement_key_policy( int policy_usage,
1547 int policy_alg,
1548 int key_type_arg,
1549 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001550 int exercise_alg,
1551 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001552{
Ronald Cron5425a212020-08-04 14:58:35 +02001553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001555 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001556 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001557 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001558 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001559
Gilles Peskine8817f612018-12-18 00:18:46 +01001560 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001561
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001562 psa_set_key_usage_flags( &attributes, policy_usage );
1563 psa_set_key_algorithm( &attributes, policy_alg );
1564 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001565
Gilles Peskine049c7532019-05-15 20:22:09 +02001566 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001567 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001568
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001569 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001570 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001571
Steven Cooremance48e852020-10-05 16:02:45 +02001572 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001573
1574exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001575 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001576 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001577 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001578}
1579/* END_CASE */
1580
1581/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001582void key_policy_alg2( int key_type_arg, data_t *key_data,
1583 int usage_arg, int alg_arg, int alg2_arg )
1584{
Ronald Cron5425a212020-08-04 14:58:35 +02001585 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001586 psa_key_type_t key_type = key_type_arg;
1587 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1588 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1589 psa_key_usage_t usage = usage_arg;
1590 psa_algorithm_t alg = alg_arg;
1591 psa_algorithm_t alg2 = alg2_arg;
1592
1593 PSA_ASSERT( psa_crypto_init( ) );
1594
1595 psa_set_key_usage_flags( &attributes, usage );
1596 psa_set_key_algorithm( &attributes, alg );
1597 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1598 psa_set_key_type( &attributes, key_type );
1599 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001600 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001601
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001602 /* Update the usage flags to obtain implicit usage flags */
1603 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001604 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001605 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1606 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1607 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1608
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001609 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001610 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001611 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001612 goto exit;
1613
1614exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001615 /*
1616 * Key attributes may have been returned by psa_get_key_attributes()
1617 * thus reset them as required.
1618 */
1619 psa_reset_key_attributes( &got_attributes );
1620
Ronald Cron5425a212020-08-04 14:58:35 +02001621 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001622 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001623}
1624/* END_CASE */
1625
1626/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001627void raw_agreement_key_policy( int policy_usage,
1628 int policy_alg,
1629 int key_type_arg,
1630 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001631 int exercise_alg,
1632 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001633{
Ronald Cron5425a212020-08-04 14:58:35 +02001634 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001636 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001637 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001638 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001639 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001640
1641 PSA_ASSERT( psa_crypto_init( ) );
1642
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001643 psa_set_key_usage_flags( &attributes, policy_usage );
1644 psa_set_key_algorithm( &attributes, policy_alg );
1645 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001646
Gilles Peskine049c7532019-05-15 20:22:09 +02001647 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001648 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001649
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001650 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001651
Steven Cooremance48e852020-10-05 16:02:45 +02001652 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001653
1654exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001655 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001656 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001657 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001658}
1659/* END_CASE */
1660
1661/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001662void copy_success( int source_usage_arg,
1663 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301664 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001665 int type_arg, data_t *material,
1666 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001667 int target_usage_arg,
1668 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301669 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001670 int expected_usage_arg,
1671 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001672{
Gilles Peskineca25db92019-04-19 11:43:08 +02001673 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1674 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001675 psa_key_usage_t expected_usage = expected_usage_arg;
1676 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001677 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301678 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1679 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001680 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1681 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001682 uint8_t *export_buffer = NULL;
1683
Gilles Peskine57ab7212019-01-28 13:03:09 +01001684 PSA_ASSERT( psa_crypto_init( ) );
1685
Gilles Peskineca25db92019-04-19 11:43:08 +02001686 /* Prepare the source key. */
1687 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1688 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001689 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001690 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301691 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001692 PSA_ASSERT( psa_import_key( &source_attributes,
1693 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001694 &source_key ) );
1695 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001696
Gilles Peskineca25db92019-04-19 11:43:08 +02001697 /* Prepare the target attributes. */
1698 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001699 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001700 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001701 }
Archana8a180362021-07-05 02:18:48 +05301702 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001703
Gilles Peskineca25db92019-04-19 11:43:08 +02001704 if( target_usage_arg != -1 )
1705 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1706 if( target_alg_arg != -1 )
1707 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001708 if( target_alg2_arg != -1 )
1709 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001710
Archana8a180362021-07-05 02:18:48 +05301711
Gilles Peskine57ab7212019-01-28 13:03:09 +01001712 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001713 PSA_ASSERT( psa_copy_key( source_key,
1714 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001715
1716 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001717 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001718
1719 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001720 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001721 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1722 psa_get_key_type( &target_attributes ) );
1723 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1724 psa_get_key_bits( &target_attributes ) );
1725 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1726 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001727 TEST_EQUAL( expected_alg2,
1728 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001729 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1730 {
1731 size_t length;
1732 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001733 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001734 material->len, &length ) );
1735 ASSERT_COMPARE( material->x, material->len,
1736 export_buffer, length );
1737 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001738
Archana8a180362021-07-05 02:18:48 +05301739 if( !psa_key_lifetime_is_external( target_lifetime ) )
1740 {
1741 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1742 goto exit;
1743 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1744 goto exit;
1745 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001746
Ronald Cron5425a212020-08-04 14:58:35 +02001747 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001748
1749exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001750 /*
1751 * Source and target key attributes may have been returned by
1752 * psa_get_key_attributes() thus reset them as required.
1753 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001754 psa_reset_key_attributes( &source_attributes );
1755 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001756
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001757 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001758 mbedtls_free( export_buffer );
1759}
1760/* END_CASE */
1761
1762/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001763void copy_fail( int source_usage_arg,
1764 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301765 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001766 int type_arg, data_t *material,
1767 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001768 int target_usage_arg,
1769 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001770 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001771 int expected_status_arg )
1772{
1773 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1774 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001775 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1776 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001777 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001778
1779 PSA_ASSERT( psa_crypto_init( ) );
1780
1781 /* Prepare the source key. */
1782 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1783 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001784 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001785 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301786 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001787 PSA_ASSERT( psa_import_key( &source_attributes,
1788 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001789 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001790
1791 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001792 psa_set_key_id( &target_attributes, key_id );
1793 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001794 psa_set_key_type( &target_attributes, target_type_arg );
1795 psa_set_key_bits( &target_attributes, target_bits_arg );
1796 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1797 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001798 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001799
1800 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001801 TEST_EQUAL( psa_copy_key( source_key,
1802 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001803 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001804
Ronald Cron5425a212020-08-04 14:58:35 +02001805 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001806
Gilles Peskine4a644642019-05-03 17:14:08 +02001807exit:
1808 psa_reset_key_attributes( &source_attributes );
1809 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001810 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001811}
1812/* END_CASE */
1813
1814/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001815void hash_operation_init( )
1816{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001817 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001818 /* Test each valid way of initializing the object, except for `= {0}`, as
1819 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1820 * though it's OK by the C standard. We could test for this, but we'd need
1821 * to supress the Clang warning for the test. */
1822 psa_hash_operation_t func = psa_hash_operation_init( );
1823 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1824 psa_hash_operation_t zero;
1825
1826 memset( &zero, 0, sizeof( zero ) );
1827
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001828 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001829 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1830 PSA_ERROR_BAD_STATE );
1831 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1832 PSA_ERROR_BAD_STATE );
1833 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1834 PSA_ERROR_BAD_STATE );
1835
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001836 /* A default hash operation should be abortable without error. */
1837 PSA_ASSERT( psa_hash_abort( &func ) );
1838 PSA_ASSERT( psa_hash_abort( &init ) );
1839 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001840}
1841/* END_CASE */
1842
1843/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001844void hash_setup( int alg_arg,
1845 int expected_status_arg )
1846{
1847 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01001848 uint8_t *output = NULL;
1849 size_t output_size = 0;
1850 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001851 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001852 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001853 psa_status_t status;
1854
Gilles Peskine8817f612018-12-18 00:18:46 +01001855 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001856
Neil Armstrongedb20862022-02-07 15:47:44 +01001857 /* Hash Setup, one-shot */
1858 output_size = PSA_HASH_LENGTH(alg);
1859 ASSERT_ALLOC( output, output_size );
1860
1861 status = psa_hash_compute( alg, NULL, 0,
1862 output, output_size, &output_length );
1863 TEST_EQUAL( status, expected_status );
1864
1865 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001866 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001867 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001868
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001869 /* Whether setup succeeded or failed, abort must succeed. */
1870 PSA_ASSERT( psa_hash_abort( &operation ) );
1871
1872 /* If setup failed, reproduce the failure, so as to
1873 * test the resulting state of the operation object. */
1874 if( status != PSA_SUCCESS )
1875 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1876
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001877 /* Now the operation object should be reusable. */
1878#if defined(KNOWN_SUPPORTED_HASH_ALG)
1879 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1880 PSA_ASSERT( psa_hash_abort( &operation ) );
1881#endif
1882
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001883exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01001884 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001885 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001886}
1887/* END_CASE */
1888
1889/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001890void hash_compute_fail( int alg_arg, data_t *input,
1891 int output_size_arg, int expected_status_arg )
1892{
1893 psa_algorithm_t alg = alg_arg;
1894 uint8_t *output = NULL;
1895 size_t output_size = output_size_arg;
1896 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001897 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01001898 psa_status_t expected_status = expected_status_arg;
1899 psa_status_t status;
1900
1901 ASSERT_ALLOC( output, output_size );
1902
1903 PSA_ASSERT( psa_crypto_init( ) );
1904
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001905 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001906 status = psa_hash_compute( alg, input->x, input->len,
1907 output, output_size, &output_length );
1908 TEST_EQUAL( status, expected_status );
1909 TEST_ASSERT( output_length <= output_size );
1910
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001911 /* Hash Compute, multi-part */
1912 status = psa_hash_setup( &operation, alg );
1913 if( status == PSA_SUCCESS )
1914 {
1915 status = psa_hash_update( &operation, input->x, input->len );
1916 if( status == PSA_SUCCESS )
1917 {
1918 status = psa_hash_finish( &operation, output, output_size,
1919 &output_length );
1920 if( status == PSA_SUCCESS )
1921 TEST_ASSERT( output_length <= output_size );
1922 else
1923 TEST_EQUAL( status, expected_status );
1924 }
1925 else
1926 {
1927 TEST_EQUAL( status, expected_status );
1928 }
1929 }
1930 else
1931 {
1932 TEST_EQUAL( status, expected_status );
1933 }
1934
Gilles Peskine0a749c82019-11-28 19:33:58 +01001935exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001936 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001937 mbedtls_free( output );
1938 PSA_DONE( );
1939}
1940/* END_CASE */
1941
1942/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001943void hash_compare_fail( int alg_arg, data_t *input,
1944 data_t *reference_hash,
1945 int expected_status_arg )
1946{
1947 psa_algorithm_t alg = alg_arg;
1948 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01001949 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01001950 psa_status_t status;
1951
1952 PSA_ASSERT( psa_crypto_init( ) );
1953
Neil Armstrong55a1be12022-02-07 11:23:20 +01001954 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01001955 status = psa_hash_compare( alg, input->x, input->len,
1956 reference_hash->x, reference_hash->len );
1957 TEST_EQUAL( status, expected_status );
1958
Neil Armstrong55a1be12022-02-07 11:23:20 +01001959 /* Hash Compare, multi-part */
1960 status = psa_hash_setup( &operation, alg );
1961 if( status == PSA_SUCCESS )
1962 {
1963 status = psa_hash_update( &operation, input->x, input->len );
1964 if( status == PSA_SUCCESS )
1965 {
1966 status = psa_hash_verify( &operation, reference_hash->x,
1967 reference_hash->len );
1968 TEST_EQUAL( status, expected_status );
1969 }
1970 else
1971 {
1972 TEST_EQUAL( status, expected_status );
1973 }
1974 }
1975 else
1976 {
1977 TEST_EQUAL( status, expected_status );
1978 }
1979
Gilles Peskine88e08462020-01-28 20:43:00 +01001980exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01001981 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01001982 PSA_DONE( );
1983}
1984/* END_CASE */
1985
1986/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001987void hash_compute_compare( int alg_arg, data_t *input,
1988 data_t *expected_output )
1989{
1990 psa_algorithm_t alg = alg_arg;
1991 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1992 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01001993 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01001994 size_t i;
1995
1996 PSA_ASSERT( psa_crypto_init( ) );
1997
Neil Armstrongca30a002022-02-07 11:40:23 +01001998 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001999 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002000 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002001 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002002 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002003 ASSERT_COMPARE( output, output_length,
2004 expected_output->x, expected_output->len );
2005
Neil Armstrongca30a002022-02-07 11:40:23 +01002006 /* Compute with tight buffer, multi-part */
2007 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2008 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2009 PSA_ASSERT( psa_hash_finish( &operation, output,
2010 PSA_HASH_LENGTH( alg ),
2011 &output_length ) );
2012 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2013 ASSERT_COMPARE( output, output_length,
2014 expected_output->x, expected_output->len );
2015
2016 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002017 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2018 output, sizeof( output ),
2019 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002020 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002021 ASSERT_COMPARE( output, output_length,
2022 expected_output->x, expected_output->len );
2023
Neil Armstrongca30a002022-02-07 11:40:23 +01002024 /* Compute with larger buffer, multi-part */
2025 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2026 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2027 PSA_ASSERT( psa_hash_finish( &operation, output,
2028 sizeof( output ), &output_length ) );
2029 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2030 ASSERT_COMPARE( output, output_length,
2031 expected_output->x, expected_output->len );
2032
2033 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002034 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2035 output, output_length ) );
2036
Neil Armstrongca30a002022-02-07 11:40:23 +01002037 /* Compare with correct hash, multi-part */
2038 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2039 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2040 PSA_ASSERT( psa_hash_verify( &operation, output,
2041 output_length ) );
2042
2043 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002044 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2045 output, output_length + 1 ),
2046 PSA_ERROR_INVALID_SIGNATURE );
2047
Neil Armstrongca30a002022-02-07 11:40:23 +01002048 /* Compare with trailing garbage, multi-part */
2049 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2050 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2051 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2052 PSA_ERROR_INVALID_SIGNATURE );
2053
2054 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002055 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2056 output, output_length - 1 ),
2057 PSA_ERROR_INVALID_SIGNATURE );
2058
Neil Armstrongca30a002022-02-07 11:40:23 +01002059 /* Compare with truncated hash, multi-part */
2060 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2061 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2062 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2063 PSA_ERROR_INVALID_SIGNATURE );
2064
Gilles Peskine0a749c82019-11-28 19:33:58 +01002065 /* Compare with corrupted value */
2066 for( i = 0; i < output_length; i++ )
2067 {
Chris Jones9634bb12021-01-20 15:56:42 +00002068 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002069 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002070
2071 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002072 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2073 output, output_length ),
2074 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002075
2076 /* Multi-Part */
2077 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2078 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2079 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2080 PSA_ERROR_INVALID_SIGNATURE );
2081
Gilles Peskine0a749c82019-11-28 19:33:58 +01002082 output[i] ^= 1;
2083 }
2084
2085exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002086 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002087 PSA_DONE( );
2088}
2089/* END_CASE */
2090
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002091/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002092void hash_bad_order( )
2093{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002094 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002095 unsigned char input[] = "";
2096 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002097 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002098 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2099 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2100 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002101 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002102 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002103 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002104
Gilles Peskine8817f612018-12-18 00:18:46 +01002105 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002106
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002107 /* Call setup twice in a row. */
2108 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002109 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002110 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2111 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002112 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002113 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002114 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002115
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002116 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002117 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002118 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002119 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002120
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002121 /* Check that update calls abort on error. */
2122 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002123 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002124 ASSERT_OPERATION_IS_ACTIVE( operation );
2125 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2126 PSA_ERROR_BAD_STATE );
2127 ASSERT_OPERATION_IS_INACTIVE( operation );
2128 PSA_ASSERT( psa_hash_abort( &operation ) );
2129 ASSERT_OPERATION_IS_INACTIVE( operation );
2130
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002131 /* Call update after finish. */
2132 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2133 PSA_ASSERT( psa_hash_finish( &operation,
2134 hash, sizeof( hash ), &hash_len ) );
2135 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002136 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002137 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002138
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002139 /* Call verify without calling setup beforehand. */
2140 TEST_EQUAL( psa_hash_verify( &operation,
2141 valid_hash, sizeof( valid_hash ) ),
2142 PSA_ERROR_BAD_STATE );
2143 PSA_ASSERT( psa_hash_abort( &operation ) );
2144
2145 /* Call verify after finish. */
2146 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2147 PSA_ASSERT( psa_hash_finish( &operation,
2148 hash, sizeof( hash ), &hash_len ) );
2149 TEST_EQUAL( psa_hash_verify( &operation,
2150 valid_hash, sizeof( valid_hash ) ),
2151 PSA_ERROR_BAD_STATE );
2152 PSA_ASSERT( psa_hash_abort( &operation ) );
2153
2154 /* Call verify twice in a row. */
2155 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002156 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002157 PSA_ASSERT( psa_hash_verify( &operation,
2158 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002159 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002160 TEST_EQUAL( psa_hash_verify( &operation,
2161 valid_hash, sizeof( valid_hash ) ),
2162 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002163 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002164 PSA_ASSERT( psa_hash_abort( &operation ) );
2165
2166 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002167 TEST_EQUAL( psa_hash_finish( &operation,
2168 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002169 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002170 PSA_ASSERT( psa_hash_abort( &operation ) );
2171
2172 /* Call finish twice in a row. */
2173 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2174 PSA_ASSERT( psa_hash_finish( &operation,
2175 hash, sizeof( hash ), &hash_len ) );
2176 TEST_EQUAL( psa_hash_finish( &operation,
2177 hash, sizeof( hash ), &hash_len ),
2178 PSA_ERROR_BAD_STATE );
2179 PSA_ASSERT( psa_hash_abort( &operation ) );
2180
2181 /* Call finish after calling verify. */
2182 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2183 PSA_ASSERT( psa_hash_verify( &operation,
2184 valid_hash, sizeof( valid_hash ) ) );
2185 TEST_EQUAL( psa_hash_finish( &operation,
2186 hash, sizeof( hash ), &hash_len ),
2187 PSA_ERROR_BAD_STATE );
2188 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002189
2190exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002191 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002192}
2193/* END_CASE */
2194
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002195/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002196void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002197{
2198 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002199 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2200 * appended to it */
2201 unsigned char hash[] = {
2202 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2203 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2204 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002205 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002206 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002207
Gilles Peskine8817f612018-12-18 00:18:46 +01002208 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002209
itayzafrir27e69452018-11-01 14:26:34 +02002210 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002211 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002212 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002213 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002214 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002215 ASSERT_OPERATION_IS_INACTIVE( operation );
2216 PSA_ASSERT( psa_hash_abort( &operation ) );
2217 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002218
itayzafrir27e69452018-11-01 14:26:34 +02002219 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002220 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002221 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002222 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002223
itayzafrir27e69452018-11-01 14:26:34 +02002224 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002225 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002226 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002227 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002228
itayzafrirec93d302018-10-18 18:01:10 +03002229exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002230 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002231}
2232/* END_CASE */
2233
Ronald Cronee414c72021-03-18 18:50:08 +01002234/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002235void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002236{
2237 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002238 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002239 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002240 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002241 size_t hash_len;
2242
Gilles Peskine8817f612018-12-18 00:18:46 +01002243 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002244
itayzafrir58028322018-10-25 10:22:01 +03002245 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002246 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002247 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002248 hash, expected_size - 1, &hash_len ),
2249 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002250
2251exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002252 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002253}
2254/* END_CASE */
2255
Ronald Cronee414c72021-03-18 18:50:08 +01002256/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002257void hash_clone_source_state( )
2258{
2259 psa_algorithm_t alg = PSA_ALG_SHA_256;
2260 unsigned char hash[PSA_HASH_MAX_SIZE];
2261 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2262 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2263 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2264 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2265 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2266 size_t hash_len;
2267
2268 PSA_ASSERT( psa_crypto_init( ) );
2269 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2270
2271 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2272 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2273 PSA_ASSERT( psa_hash_finish( &op_finished,
2274 hash, sizeof( hash ), &hash_len ) );
2275 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2276 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2277
2278 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2279 PSA_ERROR_BAD_STATE );
2280
2281 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2282 PSA_ASSERT( psa_hash_finish( &op_init,
2283 hash, sizeof( hash ), &hash_len ) );
2284 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2285 PSA_ASSERT( psa_hash_finish( &op_finished,
2286 hash, sizeof( hash ), &hash_len ) );
2287 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2288 PSA_ASSERT( psa_hash_finish( &op_aborted,
2289 hash, sizeof( hash ), &hash_len ) );
2290
2291exit:
2292 psa_hash_abort( &op_source );
2293 psa_hash_abort( &op_init );
2294 psa_hash_abort( &op_setup );
2295 psa_hash_abort( &op_finished );
2296 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002297 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002298}
2299/* END_CASE */
2300
Ronald Cronee414c72021-03-18 18:50:08 +01002301/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002302void hash_clone_target_state( )
2303{
2304 psa_algorithm_t alg = PSA_ALG_SHA_256;
2305 unsigned char hash[PSA_HASH_MAX_SIZE];
2306 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2307 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2308 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2309 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2310 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2311 size_t hash_len;
2312
2313 PSA_ASSERT( psa_crypto_init( ) );
2314
2315 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2316 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2317 PSA_ASSERT( psa_hash_finish( &op_finished,
2318 hash, sizeof( hash ), &hash_len ) );
2319 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2320 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2321
2322 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2323 PSA_ASSERT( psa_hash_finish( &op_target,
2324 hash, sizeof( hash ), &hash_len ) );
2325
2326 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2327 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2328 PSA_ERROR_BAD_STATE );
2329 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2330 PSA_ERROR_BAD_STATE );
2331
2332exit:
2333 psa_hash_abort( &op_target );
2334 psa_hash_abort( &op_init );
2335 psa_hash_abort( &op_setup );
2336 psa_hash_abort( &op_finished );
2337 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002338 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002339}
2340/* END_CASE */
2341
itayzafrir58028322018-10-25 10:22:01 +03002342/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002343void mac_operation_init( )
2344{
Jaeden Amero252ef282019-02-15 14:05:35 +00002345 const uint8_t input[1] = { 0 };
2346
Jaeden Amero769ce272019-01-04 11:48:03 +00002347 /* Test each valid way of initializing the object, except for `= {0}`, as
2348 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2349 * though it's OK by the C standard. We could test for this, but we'd need
2350 * to supress the Clang warning for the test. */
2351 psa_mac_operation_t func = psa_mac_operation_init( );
2352 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2353 psa_mac_operation_t zero;
2354
2355 memset( &zero, 0, sizeof( zero ) );
2356
Jaeden Amero252ef282019-02-15 14:05:35 +00002357 /* A freshly-initialized MAC operation should not be usable. */
2358 TEST_EQUAL( psa_mac_update( &func,
2359 input, sizeof( input ) ),
2360 PSA_ERROR_BAD_STATE );
2361 TEST_EQUAL( psa_mac_update( &init,
2362 input, sizeof( input ) ),
2363 PSA_ERROR_BAD_STATE );
2364 TEST_EQUAL( psa_mac_update( &zero,
2365 input, sizeof( input ) ),
2366 PSA_ERROR_BAD_STATE );
2367
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002368 /* A default MAC operation should be abortable without error. */
2369 PSA_ASSERT( psa_mac_abort( &func ) );
2370 PSA_ASSERT( psa_mac_abort( &init ) );
2371 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002372}
2373/* END_CASE */
2374
2375/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002376void mac_setup( int key_type_arg,
2377 data_t *key,
2378 int alg_arg,
2379 int expected_status_arg )
2380{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002381 psa_key_type_t key_type = key_type_arg;
2382 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002383 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002384 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002385 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2386#if defined(KNOWN_SUPPORTED_MAC_ALG)
2387 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2388#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002389
Gilles Peskine8817f612018-12-18 00:18:46 +01002390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002391
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002392 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2393 &operation, &status ) )
2394 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002395 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002396
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002397 /* The operation object should be reusable. */
2398#if defined(KNOWN_SUPPORTED_MAC_ALG)
2399 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2400 smoke_test_key_data,
2401 sizeof( smoke_test_key_data ),
2402 KNOWN_SUPPORTED_MAC_ALG,
2403 &operation, &status ) )
2404 goto exit;
2405 TEST_EQUAL( status, PSA_SUCCESS );
2406#endif
2407
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002408exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002409 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002410}
2411/* END_CASE */
2412
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002413/* 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 +00002414void mac_bad_order( )
2415{
Ronald Cron5425a212020-08-04 14:58:35 +02002416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002417 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2418 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002419 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002420 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2421 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2422 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002423 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002424 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2425 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2426 size_t sign_mac_length = 0;
2427 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2428 const uint8_t verify_mac[] = {
2429 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2430 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2431 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2432
2433 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002434 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002435 psa_set_key_algorithm( &attributes, alg );
2436 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002437
Ronald Cron5425a212020-08-04 14:58:35 +02002438 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2439 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002440
Jaeden Amero252ef282019-02-15 14:05:35 +00002441 /* Call update without calling setup beforehand. */
2442 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2443 PSA_ERROR_BAD_STATE );
2444 PSA_ASSERT( psa_mac_abort( &operation ) );
2445
2446 /* Call sign finish without calling setup beforehand. */
2447 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2448 &sign_mac_length),
2449 PSA_ERROR_BAD_STATE );
2450 PSA_ASSERT( psa_mac_abort( &operation ) );
2451
2452 /* Call verify finish without calling setup beforehand. */
2453 TEST_EQUAL( psa_mac_verify_finish( &operation,
2454 verify_mac, sizeof( verify_mac ) ),
2455 PSA_ERROR_BAD_STATE );
2456 PSA_ASSERT( psa_mac_abort( &operation ) );
2457
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002458 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002459 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002460 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002461 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002462 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002463 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002464 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002465 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002466
Jaeden Amero252ef282019-02-15 14:05:35 +00002467 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002468 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002469 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2470 PSA_ASSERT( psa_mac_sign_finish( &operation,
2471 sign_mac, sizeof( sign_mac ),
2472 &sign_mac_length ) );
2473 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2474 PSA_ERROR_BAD_STATE );
2475 PSA_ASSERT( psa_mac_abort( &operation ) );
2476
2477 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002478 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002479 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2480 PSA_ASSERT( psa_mac_verify_finish( &operation,
2481 verify_mac, sizeof( verify_mac ) ) );
2482 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2483 PSA_ERROR_BAD_STATE );
2484 PSA_ASSERT( psa_mac_abort( &operation ) );
2485
2486 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002487 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002488 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2489 PSA_ASSERT( psa_mac_sign_finish( &operation,
2490 sign_mac, sizeof( sign_mac ),
2491 &sign_mac_length ) );
2492 TEST_EQUAL( psa_mac_sign_finish( &operation,
2493 sign_mac, sizeof( sign_mac ),
2494 &sign_mac_length ),
2495 PSA_ERROR_BAD_STATE );
2496 PSA_ASSERT( psa_mac_abort( &operation ) );
2497
2498 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002499 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002500 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2501 PSA_ASSERT( psa_mac_verify_finish( &operation,
2502 verify_mac, sizeof( verify_mac ) ) );
2503 TEST_EQUAL( psa_mac_verify_finish( &operation,
2504 verify_mac, sizeof( verify_mac ) ),
2505 PSA_ERROR_BAD_STATE );
2506 PSA_ASSERT( psa_mac_abort( &operation ) );
2507
2508 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002509 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002510 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002511 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002512 TEST_EQUAL( psa_mac_verify_finish( &operation,
2513 verify_mac, sizeof( verify_mac ) ),
2514 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002515 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002516 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002517 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002518
2519 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002520 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002521 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002522 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002523 TEST_EQUAL( psa_mac_sign_finish( &operation,
2524 sign_mac, sizeof( sign_mac ),
2525 &sign_mac_length ),
2526 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002527 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002528 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002529 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002530
Ronald Cron5425a212020-08-04 14:58:35 +02002531 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002532
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002533exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002534 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002535}
2536/* END_CASE */
2537
2538/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002539void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002540 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002541 int alg_arg,
2542 data_t *input,
2543 data_t *expected_mac )
2544{
Ronald Cron5425a212020-08-04 14:58:35 +02002545 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002546 psa_key_type_t key_type = key_type_arg;
2547 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002548 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002549 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002550 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002551 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002552 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002553 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002554 const size_t output_sizes_to_test[] = {
2555 0,
2556 1,
2557 expected_mac->len - 1,
2558 expected_mac->len,
2559 expected_mac->len + 1,
2560 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002561
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002562 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002563 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002564 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002565
Gilles Peskine8817f612018-12-18 00:18:46 +01002566 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002567
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002568 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002569 psa_set_key_algorithm( &attributes, alg );
2570 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002571
Ronald Cron5425a212020-08-04 14:58:35 +02002572 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2573 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002574
Gilles Peskine8b356b52020-08-25 23:44:59 +02002575 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2576 {
2577 const size_t output_size = output_sizes_to_test[i];
2578 psa_status_t expected_status =
2579 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2580 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002581
Chris Jones9634bb12021-01-20 15:56:42 +00002582 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002583 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002584
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002585 /* Calculate the MAC, one-shot case. */
2586 TEST_EQUAL( psa_mac_compute( key, alg,
2587 input->x, input->len,
2588 actual_mac, output_size, &mac_length ),
2589 expected_status );
2590 if( expected_status == PSA_SUCCESS )
2591 {
2592 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2593 actual_mac, mac_length );
2594 }
2595
2596 if( output_size > 0 )
2597 memset( actual_mac, 0, output_size );
2598
2599 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002600 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002601 PSA_ASSERT( psa_mac_update( &operation,
2602 input->x, input->len ) );
2603 TEST_EQUAL( psa_mac_sign_finish( &operation,
2604 actual_mac, output_size,
2605 &mac_length ),
2606 expected_status );
2607 PSA_ASSERT( psa_mac_abort( &operation ) );
2608
2609 if( expected_status == PSA_SUCCESS )
2610 {
2611 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2612 actual_mac, mac_length );
2613 }
2614 mbedtls_free( actual_mac );
2615 actual_mac = NULL;
2616 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002617
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002618exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002619 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002620 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002621 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002622 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002623}
2624/* END_CASE */
2625
2626/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002627void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002628 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002629 int alg_arg,
2630 data_t *input,
2631 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632{
Ronald Cron5425a212020-08-04 14:58:35 +02002633 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002634 psa_key_type_t key_type = key_type_arg;
2635 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002636 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002637 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002638 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639
Gilles Peskine69c12672018-06-28 00:07:19 +02002640 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2641
Gilles Peskine8817f612018-12-18 00:18:46 +01002642 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002644 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002645 psa_set_key_algorithm( &attributes, alg );
2646 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002647
Ronald Cron5425a212020-08-04 14:58:35 +02002648 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2649 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002650
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002651 /* Verify correct MAC, one-shot case. */
2652 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2653 expected_mac->x, expected_mac->len ) );
2654
2655 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002656 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002657 PSA_ASSERT( psa_mac_update( &operation,
2658 input->x, input->len ) );
2659 PSA_ASSERT( psa_mac_verify_finish( &operation,
2660 expected_mac->x,
2661 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002662
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002663 /* Test a MAC that's too short, one-shot case. */
2664 TEST_EQUAL( psa_mac_verify( key, alg,
2665 input->x, input->len,
2666 expected_mac->x,
2667 expected_mac->len - 1 ),
2668 PSA_ERROR_INVALID_SIGNATURE );
2669
2670 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002671 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002672 PSA_ASSERT( psa_mac_update( &operation,
2673 input->x, input->len ) );
2674 TEST_EQUAL( psa_mac_verify_finish( &operation,
2675 expected_mac->x,
2676 expected_mac->len - 1 ),
2677 PSA_ERROR_INVALID_SIGNATURE );
2678
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002679 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002680 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2681 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002682 TEST_EQUAL( psa_mac_verify( key, alg,
2683 input->x, input->len,
2684 perturbed_mac, expected_mac->len + 1 ),
2685 PSA_ERROR_INVALID_SIGNATURE );
2686
2687 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002688 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002689 PSA_ASSERT( psa_mac_update( &operation,
2690 input->x, input->len ) );
2691 TEST_EQUAL( psa_mac_verify_finish( &operation,
2692 perturbed_mac,
2693 expected_mac->len + 1 ),
2694 PSA_ERROR_INVALID_SIGNATURE );
2695
2696 /* Test changing one byte. */
2697 for( size_t i = 0; i < expected_mac->len; i++ )
2698 {
Chris Jones9634bb12021-01-20 15:56:42 +00002699 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002700 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002701
2702 TEST_EQUAL( psa_mac_verify( key, alg,
2703 input->x, input->len,
2704 perturbed_mac, expected_mac->len ),
2705 PSA_ERROR_INVALID_SIGNATURE );
2706
Ronald Cron5425a212020-08-04 14:58:35 +02002707 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002708 PSA_ASSERT( psa_mac_update( &operation,
2709 input->x, input->len ) );
2710 TEST_EQUAL( psa_mac_verify_finish( &operation,
2711 perturbed_mac,
2712 expected_mac->len ),
2713 PSA_ERROR_INVALID_SIGNATURE );
2714 perturbed_mac[i] ^= 1;
2715 }
2716
Gilles Peskine8c9def32018-02-08 10:02:12 +01002717exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002718 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002719 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002720 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002721 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722}
2723/* END_CASE */
2724
2725/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002726void cipher_operation_init( )
2727{
Jaeden Ameroab439972019-02-15 14:12:05 +00002728 const uint8_t input[1] = { 0 };
2729 unsigned char output[1] = { 0 };
2730 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002731 /* Test each valid way of initializing the object, except for `= {0}`, as
2732 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2733 * though it's OK by the C standard. We could test for this, but we'd need
2734 * to supress the Clang warning for the test. */
2735 psa_cipher_operation_t func = psa_cipher_operation_init( );
2736 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2737 psa_cipher_operation_t zero;
2738
2739 memset( &zero, 0, sizeof( zero ) );
2740
Jaeden Ameroab439972019-02-15 14:12:05 +00002741 /* A freshly-initialized cipher operation should not be usable. */
2742 TEST_EQUAL( psa_cipher_update( &func,
2743 input, sizeof( input ),
2744 output, sizeof( output ),
2745 &output_length ),
2746 PSA_ERROR_BAD_STATE );
2747 TEST_EQUAL( psa_cipher_update( &init,
2748 input, sizeof( input ),
2749 output, sizeof( output ),
2750 &output_length ),
2751 PSA_ERROR_BAD_STATE );
2752 TEST_EQUAL( psa_cipher_update( &zero,
2753 input, sizeof( input ),
2754 output, sizeof( output ),
2755 &output_length ),
2756 PSA_ERROR_BAD_STATE );
2757
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002758 /* A default cipher operation should be abortable without error. */
2759 PSA_ASSERT( psa_cipher_abort( &func ) );
2760 PSA_ASSERT( psa_cipher_abort( &init ) );
2761 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002762}
2763/* END_CASE */
2764
2765/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002766void cipher_setup( int key_type_arg,
2767 data_t *key,
2768 int alg_arg,
2769 int expected_status_arg )
2770{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002771 psa_key_type_t key_type = key_type_arg;
2772 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002773 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002774 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002775 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002776#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002777 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2778#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002779
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002781
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002782 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2783 &operation, &status ) )
2784 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002785 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002786
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002787 /* The operation object should be reusable. */
2788#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2789 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2790 smoke_test_key_data,
2791 sizeof( smoke_test_key_data ),
2792 KNOWN_SUPPORTED_CIPHER_ALG,
2793 &operation, &status ) )
2794 goto exit;
2795 TEST_EQUAL( status, PSA_SUCCESS );
2796#endif
2797
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002798exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002799 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002800 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002801}
2802/* END_CASE */
2803
Ronald Cronee414c72021-03-18 18:50:08 +01002804/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002805void cipher_bad_order( )
2806{
Ronald Cron5425a212020-08-04 14:58:35 +02002807 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002808 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2809 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002810 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002811 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002812 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002813 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002814 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2815 0xaa, 0xaa, 0xaa, 0xaa };
2816 const uint8_t text[] = {
2817 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2818 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002819 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002820 size_t length = 0;
2821
2822 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002823 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2824 psa_set_key_algorithm( &attributes, alg );
2825 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002826 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2827 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002828
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002829 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002830 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002831 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002832 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002833 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002834 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002835 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002836 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002837
2838 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002839 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002840 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002841 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002842 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002843 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002844 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002845 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002846
Jaeden Ameroab439972019-02-15 14:12:05 +00002847 /* Generate an IV without calling setup beforehand. */
2848 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2849 buffer, sizeof( buffer ),
2850 &length ),
2851 PSA_ERROR_BAD_STATE );
2852 PSA_ASSERT( psa_cipher_abort( &operation ) );
2853
2854 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002855 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002856 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2857 buffer, sizeof( buffer ),
2858 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002859 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002860 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2861 buffer, sizeof( buffer ),
2862 &length ),
2863 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002864 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002865 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002866 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002867
2868 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002869 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002870 PSA_ASSERT( psa_cipher_set_iv( &operation,
2871 iv, sizeof( iv ) ) );
2872 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2873 buffer, sizeof( buffer ),
2874 &length ),
2875 PSA_ERROR_BAD_STATE );
2876 PSA_ASSERT( psa_cipher_abort( &operation ) );
2877
2878 /* Set an IV without calling setup beforehand. */
2879 TEST_EQUAL( psa_cipher_set_iv( &operation,
2880 iv, sizeof( iv ) ),
2881 PSA_ERROR_BAD_STATE );
2882 PSA_ASSERT( psa_cipher_abort( &operation ) );
2883
2884 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002885 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002886 PSA_ASSERT( psa_cipher_set_iv( &operation,
2887 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002888 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002889 TEST_EQUAL( psa_cipher_set_iv( &operation,
2890 iv, sizeof( iv ) ),
2891 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002892 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002893 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002894 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002895
2896 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002897 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002898 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2899 buffer, sizeof( buffer ),
2900 &length ) );
2901 TEST_EQUAL( psa_cipher_set_iv( &operation,
2902 iv, sizeof( iv ) ),
2903 PSA_ERROR_BAD_STATE );
2904 PSA_ASSERT( psa_cipher_abort( &operation ) );
2905
2906 /* Call update without calling setup beforehand. */
2907 TEST_EQUAL( psa_cipher_update( &operation,
2908 text, sizeof( text ),
2909 buffer, sizeof( buffer ),
2910 &length ),
2911 PSA_ERROR_BAD_STATE );
2912 PSA_ASSERT( psa_cipher_abort( &operation ) );
2913
2914 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002915 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002916 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002917 TEST_EQUAL( psa_cipher_update( &operation,
2918 text, sizeof( text ),
2919 buffer, sizeof( buffer ),
2920 &length ),
2921 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002922 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002923 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002924 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002925
2926 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002927 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002928 PSA_ASSERT( psa_cipher_set_iv( &operation,
2929 iv, sizeof( iv ) ) );
2930 PSA_ASSERT( psa_cipher_finish( &operation,
2931 buffer, sizeof( buffer ), &length ) );
2932 TEST_EQUAL( psa_cipher_update( &operation,
2933 text, sizeof( text ),
2934 buffer, sizeof( buffer ),
2935 &length ),
2936 PSA_ERROR_BAD_STATE );
2937 PSA_ASSERT( psa_cipher_abort( &operation ) );
2938
2939 /* Call finish without calling setup beforehand. */
2940 TEST_EQUAL( psa_cipher_finish( &operation,
2941 buffer, sizeof( buffer ), &length ),
2942 PSA_ERROR_BAD_STATE );
2943 PSA_ASSERT( psa_cipher_abort( &operation ) );
2944
2945 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002946 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002947 /* Not calling update means we are encrypting an empty buffer, which is OK
2948 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002949 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002950 TEST_EQUAL( psa_cipher_finish( &operation,
2951 buffer, sizeof( buffer ), &length ),
2952 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002953 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002954 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002955 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002956
2957 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002958 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002959 PSA_ASSERT( psa_cipher_set_iv( &operation,
2960 iv, sizeof( iv ) ) );
2961 PSA_ASSERT( psa_cipher_finish( &operation,
2962 buffer, sizeof( buffer ), &length ) );
2963 TEST_EQUAL( psa_cipher_finish( &operation,
2964 buffer, sizeof( buffer ), &length ),
2965 PSA_ERROR_BAD_STATE );
2966 PSA_ASSERT( psa_cipher_abort( &operation ) );
2967
Ronald Cron5425a212020-08-04 14:58:35 +02002968 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002969
Jaeden Ameroab439972019-02-15 14:12:05 +00002970exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002971 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002972 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002973}
2974/* END_CASE */
2975
2976/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002977void cipher_encrypt_fail( int alg_arg,
2978 int key_type_arg,
2979 data_t *key_data,
2980 data_t *input,
2981 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002982{
Ronald Cron5425a212020-08-04 14:58:35 +02002983 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002984 psa_status_t status;
2985 psa_key_type_t key_type = key_type_arg;
2986 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002987 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002988 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002989 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002990 size_t output_length = 0;
2991 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2992
2993 if ( PSA_ERROR_BAD_STATE != expected_status )
2994 {
2995 PSA_ASSERT( psa_crypto_init( ) );
2996
2997 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2998 psa_set_key_algorithm( &attributes, alg );
2999 psa_set_key_type( &attributes, key_type );
3000
3001 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3002 input->len );
3003 ASSERT_ALLOC( output, output_buffer_size );
3004
3005 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3006 &key ) );
3007 }
3008
3009 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3010 output_buffer_size, &output_length );
3011
3012 TEST_EQUAL( status, expected_status );
3013
3014exit:
3015 mbedtls_free( output );
3016 psa_destroy_key( key );
3017 PSA_DONE( );
3018}
3019/* END_CASE */
3020
3021/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003022void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3023 data_t *input, int iv_length,
3024 int expected_result )
3025{
3026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3027 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3029 size_t output_buffer_size = 0;
3030 unsigned char *output = NULL;
3031
3032 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3033 ASSERT_ALLOC( output, output_buffer_size );
3034
3035 PSA_ASSERT( psa_crypto_init( ) );
3036
3037 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3038 psa_set_key_algorithm( &attributes, alg );
3039 psa_set_key_type( &attributes, key_type );
3040
3041 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3042 &key ) );
3043 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3044 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3045 iv_length ) );
3046
3047exit:
3048 psa_cipher_abort( &operation );
3049 mbedtls_free( output );
3050 psa_destroy_key( key );
3051 PSA_DONE( );
3052}
3053/* END_CASE */
3054
3055/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003056void cipher_encrypt_alg_without_iv( int alg_arg,
3057 int key_type_arg,
3058 data_t *key_data,
3059 data_t *input,
3060 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003061{
3062 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3063 psa_key_type_t key_type = key_type_arg;
3064 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003065 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3066 uint8_t iv[1] = { 0x5a };
3067 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003068 unsigned char *output = NULL;
3069 size_t output_buffer_size = 0;
3070 size_t output_length = 0;
3071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3072
3073 PSA_ASSERT( psa_crypto_init( ) );
3074
3075 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3076 psa_set_key_algorithm( &attributes, alg );
3077 psa_set_key_type( &attributes, key_type );
3078
3079 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3080 ASSERT_ALLOC( output, output_buffer_size );
3081
3082 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3083 &key ) );
3084
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003085 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3086 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3087 PSA_ERROR_BAD_STATE );
3088 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3089 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
3090 &iv_length ),
3091 PSA_ERROR_BAD_STATE );
3092
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003093 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
3094 output_buffer_size, &output_length ) );
3095 TEST_ASSERT( output_length <=
3096 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3097 TEST_ASSERT( output_length <=
3098 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3099
3100 ASSERT_COMPARE( expected_output->x, expected_output->len,
3101 output, output_length );
3102exit:
3103 mbedtls_free( output );
3104 psa_destroy_key( key );
3105 PSA_DONE( );
3106}
3107/* END_CASE */
3108
3109/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003110void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3111{
3112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3113 psa_algorithm_t alg = alg_arg;
3114 psa_key_type_t key_type = key_type_arg;
3115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3116 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3117 psa_status_t status;
3118
3119 PSA_ASSERT( psa_crypto_init( ) );
3120
3121 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3122 psa_set_key_algorithm( &attributes, alg );
3123 psa_set_key_type( &attributes, key_type );
3124
3125 /* Usage of either of these two size macros would cause divide by zero
3126 * with incorrect key types previously. Input length should be irrelevant
3127 * here. */
3128 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3129 0 );
3130 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3131
3132
3133 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3134 &key ) );
3135
3136 /* Should fail due to invalid alg type (to support invalid key type).
3137 * Encrypt or decrypt will end up in the same place. */
3138 status = psa_cipher_encrypt_setup( &operation, key, alg );
3139
3140 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3141
3142exit:
3143 psa_cipher_abort( &operation );
3144 psa_destroy_key( key );
3145 PSA_DONE( );
3146}
3147/* END_CASE */
3148
3149/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003150void cipher_encrypt_validation( int alg_arg,
3151 int key_type_arg,
3152 data_t *key_data,
3153 data_t *input )
3154{
3155 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3156 psa_key_type_t key_type = key_type_arg;
3157 psa_algorithm_t alg = alg_arg;
3158 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3159 unsigned char *output1 = NULL;
3160 size_t output1_buffer_size = 0;
3161 size_t output1_length = 0;
3162 unsigned char *output2 = NULL;
3163 size_t output2_buffer_size = 0;
3164 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003165 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003166 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003168
Gilles Peskine8817f612018-12-18 00:18:46 +01003169 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3172 psa_set_key_algorithm( &attributes, alg );
3173 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003174
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003175 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3176 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3177 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3178 ASSERT_ALLOC( output1, output1_buffer_size );
3179 ASSERT_ALLOC( output2, output2_buffer_size );
3180
Ronald Cron5425a212020-08-04 14:58:35 +02003181 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3182 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003183
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003184 /* The one-shot cipher encryption uses generated iv so validating
3185 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003186 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3187 output1_buffer_size, &output1_length ) );
3188 TEST_ASSERT( output1_length <=
3189 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3190 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003191 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003192
3193 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3194 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003195
Gilles Peskine8817f612018-12-18 00:18:46 +01003196 PSA_ASSERT( psa_cipher_update( &operation,
3197 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003198 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003199 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003200 TEST_ASSERT( function_output_length <=
3201 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3202 TEST_ASSERT( function_output_length <=
3203 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003204 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003205
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003206 PSA_ASSERT( psa_cipher_finish( &operation,
3207 output2 + output2_length,
3208 output2_buffer_size - output2_length,
3209 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003210 TEST_ASSERT( function_output_length <=
3211 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3212 TEST_ASSERT( function_output_length <=
3213 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003214 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003215
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003216 PSA_ASSERT( psa_cipher_abort( &operation ) );
3217 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3218 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003219
Gilles Peskine50e586b2018-06-08 14:28:46 +02003220exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003221 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003222 mbedtls_free( output1 );
3223 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003224 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003225 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003226}
3227/* END_CASE */
3228
3229/* BEGIN_CASE */
3230void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003231 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003232 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003233 int first_part_size_arg,
3234 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003235 data_t *expected_output,
3236 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003237{
Ronald Cron5425a212020-08-04 14:58:35 +02003238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003239 psa_key_type_t key_type = key_type_arg;
3240 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003241 psa_status_t status;
3242 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003243 size_t first_part_size = first_part_size_arg;
3244 size_t output1_length = output1_length_arg;
3245 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003246 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003247 size_t output_buffer_size = 0;
3248 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003249 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003250 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003252
Gilles Peskine8817f612018-12-18 00:18:46 +01003253 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003254
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003255 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3256 psa_set_key_algorithm( &attributes, alg );
3257 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003258
Ronald Cron5425a212020-08-04 14:58:35 +02003259 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3260 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003261
Ronald Cron5425a212020-08-04 14:58:35 +02003262 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003263
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003264 if( iv->len > 0 )
3265 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003266 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003267 }
3268
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003269 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3270 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003271 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003272
Gilles Peskinee0866522019-02-19 19:44:00 +01003273 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003274 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3275 output, output_buffer_size,
3276 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003277 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003278 TEST_ASSERT( function_output_length <=
3279 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3280 TEST_ASSERT( function_output_length <=
3281 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003282 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003283
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003284 if( first_part_size < input->len )
3285 {
3286 PSA_ASSERT( psa_cipher_update( &operation,
3287 input->x + first_part_size,
3288 input->len - first_part_size,
3289 ( output_buffer_size == 0 ? NULL :
3290 output + total_output_length ),
3291 output_buffer_size - total_output_length,
3292 &function_output_length ) );
3293 TEST_ASSERT( function_output_length == output2_length );
3294 TEST_ASSERT( function_output_length <=
3295 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3296 alg,
3297 input->len - first_part_size ) );
3298 TEST_ASSERT( function_output_length <=
3299 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3300 total_output_length += function_output_length;
3301 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003302
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003303 status = psa_cipher_finish( &operation,
3304 ( output_buffer_size == 0 ? NULL :
3305 output + total_output_length ),
3306 output_buffer_size - total_output_length,
3307 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003308 TEST_ASSERT( function_output_length <=
3309 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3310 TEST_ASSERT( function_output_length <=
3311 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003312 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003313 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003314
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003315 if( expected_status == PSA_SUCCESS )
3316 {
3317 PSA_ASSERT( psa_cipher_abort( &operation ) );
3318
3319 ASSERT_COMPARE( expected_output->x, expected_output->len,
3320 output, total_output_length );
3321 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003322
3323exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003324 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003325 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003326 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003327 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003328}
3329/* END_CASE */
3330
3331/* BEGIN_CASE */
3332void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003333 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003334 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003335 int first_part_size_arg,
3336 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003337 data_t *expected_output,
3338 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003339{
Ronald Cron5425a212020-08-04 14:58:35 +02003340 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003341 psa_key_type_t key_type = key_type_arg;
3342 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003343 psa_status_t status;
3344 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003345 size_t first_part_size = first_part_size_arg;
3346 size_t output1_length = output1_length_arg;
3347 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003348 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003349 size_t output_buffer_size = 0;
3350 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003351 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003352 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003354
Gilles Peskine8817f612018-12-18 00:18:46 +01003355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003356
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003357 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3358 psa_set_key_algorithm( &attributes, alg );
3359 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003360
Ronald Cron5425a212020-08-04 14:58:35 +02003361 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3362 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003363
Ronald Cron5425a212020-08-04 14:58:35 +02003364 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003365
Steven Cooreman177deba2020-09-07 17:14:14 +02003366 if( iv->len > 0 )
3367 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003368 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003369 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003370
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003371 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3372 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003373 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003374
Gilles Peskinee0866522019-02-19 19:44:00 +01003375 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003376 PSA_ASSERT( psa_cipher_update( &operation,
3377 input->x, first_part_size,
3378 output, output_buffer_size,
3379 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003380 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003381 TEST_ASSERT( function_output_length <=
3382 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3383 TEST_ASSERT( function_output_length <=
3384 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003385 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003386
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003387 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003388 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003389 PSA_ASSERT( psa_cipher_update( &operation,
3390 input->x + first_part_size,
3391 input->len - first_part_size,
3392 ( output_buffer_size == 0 ? NULL :
3393 output + total_output_length ),
3394 output_buffer_size - total_output_length,
3395 &function_output_length ) );
3396 TEST_ASSERT( function_output_length == output2_length );
3397 TEST_ASSERT( function_output_length <=
3398 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3399 alg,
3400 input->len - first_part_size ) );
3401 TEST_ASSERT( function_output_length <=
3402 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3403 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003404 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003405
Gilles Peskine50e586b2018-06-08 14:28:46 +02003406 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003407 ( output_buffer_size == 0 ? NULL :
3408 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003409 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003410 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003411 TEST_ASSERT( function_output_length <=
3412 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3413 TEST_ASSERT( function_output_length <=
3414 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003415 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003416 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003417
3418 if( expected_status == PSA_SUCCESS )
3419 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003420 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003421
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003422 ASSERT_COMPARE( expected_output->x, expected_output->len,
3423 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003424 }
3425
Gilles Peskine50e586b2018-06-08 14:28:46 +02003426exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003427 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003428 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003429 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003430 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003431}
3432/* END_CASE */
3433
Gilles Peskine50e586b2018-06-08 14:28:46 +02003434/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003435void cipher_decrypt_fail( int alg_arg,
3436 int key_type_arg,
3437 data_t *key_data,
3438 data_t *iv,
3439 data_t *input_arg,
3440 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003441{
3442 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3443 psa_status_t status;
3444 psa_key_type_t key_type = key_type_arg;
3445 psa_algorithm_t alg = alg_arg;
3446 psa_status_t expected_status = expected_status_arg;
3447 unsigned char *input = NULL;
3448 size_t input_buffer_size = 0;
3449 unsigned char *output = NULL;
3450 size_t output_buffer_size = 0;
3451 size_t output_length = 0;
3452 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3453
3454 if ( PSA_ERROR_BAD_STATE != expected_status )
3455 {
3456 PSA_ASSERT( psa_crypto_init( ) );
3457
3458 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3459 psa_set_key_algorithm( &attributes, alg );
3460 psa_set_key_type( &attributes, key_type );
3461
3462 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3463 &key ) );
3464 }
3465
3466 /* Allocate input buffer and copy the iv and the plaintext */
3467 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3468 if ( input_buffer_size > 0 )
3469 {
3470 ASSERT_ALLOC( input, input_buffer_size );
3471 memcpy( input, iv->x, iv->len );
3472 memcpy( input + iv->len, input_arg->x, input_arg->len );
3473 }
3474
3475 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3476 ASSERT_ALLOC( output, output_buffer_size );
3477
3478 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3479 output_buffer_size, &output_length );
3480 TEST_EQUAL( status, expected_status );
3481
3482exit:
3483 mbedtls_free( input );
3484 mbedtls_free( output );
3485 psa_destroy_key( key );
3486 PSA_DONE( );
3487}
3488/* END_CASE */
3489
3490/* BEGIN_CASE */
3491void cipher_decrypt( int alg_arg,
3492 int key_type_arg,
3493 data_t *key_data,
3494 data_t *iv,
3495 data_t *input_arg,
3496 data_t *expected_output )
3497{
3498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3499 psa_key_type_t key_type = key_type_arg;
3500 psa_algorithm_t alg = alg_arg;
3501 unsigned char *input = NULL;
3502 size_t input_buffer_size = 0;
3503 unsigned char *output = NULL;
3504 size_t output_buffer_size = 0;
3505 size_t output_length = 0;
3506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3507
3508 PSA_ASSERT( psa_crypto_init( ) );
3509
3510 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3511 psa_set_key_algorithm( &attributes, alg );
3512 psa_set_key_type( &attributes, key_type );
3513
3514 /* Allocate input buffer and copy the iv and the plaintext */
3515 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3516 if ( input_buffer_size > 0 )
3517 {
3518 ASSERT_ALLOC( input, input_buffer_size );
3519 memcpy( input, iv->x, iv->len );
3520 memcpy( input + iv->len, input_arg->x, input_arg->len );
3521 }
3522
3523 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3524 ASSERT_ALLOC( output, output_buffer_size );
3525
3526 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3527 &key ) );
3528
3529 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3530 output_buffer_size, &output_length ) );
3531 TEST_ASSERT( output_length <=
3532 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3533 TEST_ASSERT( output_length <=
3534 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3535
3536 ASSERT_COMPARE( expected_output->x, expected_output->len,
3537 output, output_length );
3538exit:
3539 mbedtls_free( input );
3540 mbedtls_free( output );
3541 psa_destroy_key( key );
3542 PSA_DONE( );
3543}
3544/* END_CASE */
3545
3546/* BEGIN_CASE */
3547void cipher_verify_output( int alg_arg,
3548 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003549 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003550 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003551{
Ronald Cron5425a212020-08-04 14:58:35 +02003552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003553 psa_key_type_t key_type = key_type_arg;
3554 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003555 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003556 size_t output1_size = 0;
3557 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003558 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003559 size_t output2_size = 0;
3560 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003561 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003562
Gilles Peskine8817f612018-12-18 00:18:46 +01003563 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003564
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003565 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3566 psa_set_key_algorithm( &attributes, alg );
3567 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003568
Ronald Cron5425a212020-08-04 14:58:35 +02003569 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3570 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003571 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003572 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003573
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003574 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3575 output1, output1_size,
3576 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003577 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003578 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003579 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003580 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003581
3582 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003583 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003584
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003585 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3586 output2, output2_size,
3587 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003588 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003589 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003590 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003591 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003592
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003593 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003594
3595exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003596 mbedtls_free( output1 );
3597 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003598 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003599 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003600}
3601/* END_CASE */
3602
3603/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003604void cipher_verify_output_multipart( int alg_arg,
3605 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003606 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003607 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003608 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003609{
Ronald Cron5425a212020-08-04 14:58:35 +02003610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003611 psa_key_type_t key_type = key_type_arg;
3612 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003613 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003614 unsigned char iv[16] = {0};
3615 size_t iv_size = 16;
3616 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003617 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003618 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003619 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003620 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003621 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003622 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003623 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003624 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3625 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003627
Gilles Peskine8817f612018-12-18 00:18:46 +01003628 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003629
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003630 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3631 psa_set_key_algorithm( &attributes, alg );
3632 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003633
Ronald Cron5425a212020-08-04 14:58:35 +02003634 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3635 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003636
Ronald Cron5425a212020-08-04 14:58:35 +02003637 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3638 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003639
Steven Cooreman177deba2020-09-07 17:14:14 +02003640 if( alg != PSA_ALG_ECB_NO_PADDING )
3641 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003642 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3643 iv, iv_size,
3644 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003645 }
3646
gabor-mezei-armceface22021-01-21 12:26:17 +01003647 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3648 TEST_ASSERT( output1_buffer_size <=
3649 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003650 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003651
Gilles Peskinee0866522019-02-19 19:44:00 +01003652 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003653
Gilles Peskine8817f612018-12-18 00:18:46 +01003654 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3655 output1, output1_buffer_size,
3656 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003657 TEST_ASSERT( function_output_length <=
3658 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3659 TEST_ASSERT( function_output_length <=
3660 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003661 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003662
Gilles Peskine8817f612018-12-18 00:18:46 +01003663 PSA_ASSERT( psa_cipher_update( &operation1,
3664 input->x + first_part_size,
3665 input->len - first_part_size,
3666 output1, output1_buffer_size,
3667 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003668 TEST_ASSERT( function_output_length <=
3669 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3670 alg,
3671 input->len - first_part_size ) );
3672 TEST_ASSERT( function_output_length <=
3673 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003674 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003675
Gilles Peskine8817f612018-12-18 00:18:46 +01003676 PSA_ASSERT( psa_cipher_finish( &operation1,
3677 output1 + output1_length,
3678 output1_buffer_size - output1_length,
3679 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003680 TEST_ASSERT( function_output_length <=
3681 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3682 TEST_ASSERT( function_output_length <=
3683 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003684 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003685
Gilles Peskine8817f612018-12-18 00:18:46 +01003686 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003687
Gilles Peskine048b7f02018-06-08 14:20:49 +02003688 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003689 TEST_ASSERT( output2_buffer_size <=
3690 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3691 TEST_ASSERT( output2_buffer_size <=
3692 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003693 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003694
Steven Cooreman177deba2020-09-07 17:14:14 +02003695 if( iv_length > 0 )
3696 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003697 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3698 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003699 }
Moran Pekerded84402018-06-06 16:36:50 +03003700
Gilles Peskine8817f612018-12-18 00:18:46 +01003701 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3702 output2, output2_buffer_size,
3703 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003704 TEST_ASSERT( function_output_length <=
3705 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3706 TEST_ASSERT( function_output_length <=
3707 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003708 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003709
Gilles Peskine8817f612018-12-18 00:18:46 +01003710 PSA_ASSERT( psa_cipher_update( &operation2,
3711 output1 + first_part_size,
3712 output1_length - first_part_size,
3713 output2, output2_buffer_size,
3714 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003715 TEST_ASSERT( function_output_length <=
3716 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3717 alg,
3718 output1_length - first_part_size ) );
3719 TEST_ASSERT( function_output_length <=
3720 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003721 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003722
Gilles Peskine8817f612018-12-18 00:18:46 +01003723 PSA_ASSERT( psa_cipher_finish( &operation2,
3724 output2 + output2_length,
3725 output2_buffer_size - output2_length,
3726 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003727 TEST_ASSERT( function_output_length <=
3728 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3729 TEST_ASSERT( function_output_length <=
3730 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003731 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003732
Gilles Peskine8817f612018-12-18 00:18:46 +01003733 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003734
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003735 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003736
3737exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003738 psa_cipher_abort( &operation1 );
3739 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003740 mbedtls_free( output1 );
3741 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003742 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003743 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003744}
3745/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003746
Gilles Peskine20035e32018-02-03 22:44:14 +01003747/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003748void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003749 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003750 data_t *nonce,
3751 data_t *additional_data,
3752 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003753 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003754{
Ronald Cron5425a212020-08-04 14:58:35 +02003755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003756 psa_key_type_t key_type = key_type_arg;
3757 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003758 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003759 unsigned char *output_data = NULL;
3760 size_t output_size = 0;
3761 size_t output_length = 0;
3762 unsigned char *output_data2 = NULL;
3763 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003764 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003765 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003767
Gilles Peskine8817f612018-12-18 00:18:46 +01003768 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003769
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003770 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3771 psa_set_key_algorithm( &attributes, alg );
3772 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003773
Gilles Peskine049c7532019-05-15 20:22:09 +02003774 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003775 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003776 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3777 key_bits = psa_get_key_bits( &attributes );
3778
3779 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3780 alg );
3781 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3782 * should be exact. */
3783 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3784 expected_result != PSA_ERROR_NOT_SUPPORTED )
3785 {
3786 TEST_EQUAL( output_size,
3787 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3788 TEST_ASSERT( output_size <=
3789 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3790 }
3791 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003792
Steven Cooremanf49478b2021-02-15 15:19:25 +01003793 status = psa_aead_encrypt( key, alg,
3794 nonce->x, nonce->len,
3795 additional_data->x,
3796 additional_data->len,
3797 input_data->x, input_data->len,
3798 output_data, output_size,
3799 &output_length );
3800
3801 /* If the operation is not supported, just skip and not fail in case the
3802 * encryption involves a common limitation of cryptography hardwares and
3803 * an alternative implementation. */
3804 if( status == PSA_ERROR_NOT_SUPPORTED )
3805 {
3806 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3807 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3808 }
3809
3810 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003811
3812 if( PSA_SUCCESS == expected_result )
3813 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003814 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003815
Gilles Peskine003a4a92019-05-14 16:09:40 +02003816 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3817 * should be exact. */
3818 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003819 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003820
gabor-mezei-armceface22021-01-21 12:26:17 +01003821 TEST_ASSERT( input_data->len <=
3822 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3823
Ronald Cron5425a212020-08-04 14:58:35 +02003824 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003825 nonce->x, nonce->len,
3826 additional_data->x,
3827 additional_data->len,
3828 output_data, output_length,
3829 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003830 &output_length2 ),
3831 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003832
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003833 ASSERT_COMPARE( input_data->x, input_data->len,
3834 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003835 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003836
Gilles Peskinea1cac842018-06-11 19:33:02 +02003837exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003838 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003839 mbedtls_free( output_data );
3840 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003841 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003842}
3843/* END_CASE */
3844
3845/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003846void aead_encrypt( int key_type_arg, data_t *key_data,
3847 int alg_arg,
3848 data_t *nonce,
3849 data_t *additional_data,
3850 data_t *input_data,
3851 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003852{
Ronald Cron5425a212020-08-04 14:58:35 +02003853 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003854 psa_key_type_t key_type = key_type_arg;
3855 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003856 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003857 unsigned char *output_data = NULL;
3858 size_t output_size = 0;
3859 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003861 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003862
Gilles Peskine8817f612018-12-18 00:18:46 +01003863 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003864
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003865 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3866 psa_set_key_algorithm( &attributes, alg );
3867 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003868
Gilles Peskine049c7532019-05-15 20:22:09 +02003869 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003870 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003871 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3872 key_bits = psa_get_key_bits( &attributes );
3873
3874 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3875 alg );
3876 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3877 * should be exact. */
3878 TEST_EQUAL( output_size,
3879 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3880 TEST_ASSERT( output_size <=
3881 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3882 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003883
Steven Cooremand588ea12021-01-11 19:36:04 +01003884 status = psa_aead_encrypt( key, alg,
3885 nonce->x, nonce->len,
3886 additional_data->x, additional_data->len,
3887 input_data->x, input_data->len,
3888 output_data, output_size,
3889 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003890
Ronald Cron28a45ed2021-02-09 20:35:42 +01003891 /* If the operation is not supported, just skip and not fail in case the
3892 * encryption involves a common limitation of cryptography hardwares and
3893 * an alternative implementation. */
3894 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003895 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003896 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3897 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003898 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003899
3900 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003901 ASSERT_COMPARE( expected_result->x, expected_result->len,
3902 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003903
Gilles Peskinea1cac842018-06-11 19:33:02 +02003904exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003905 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003906 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003907 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003908}
3909/* END_CASE */
3910
3911/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003912void aead_decrypt( int key_type_arg, data_t *key_data,
3913 int alg_arg,
3914 data_t *nonce,
3915 data_t *additional_data,
3916 data_t *input_data,
3917 data_t *expected_data,
3918 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003919{
Ronald Cron5425a212020-08-04 14:58:35 +02003920 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003921 psa_key_type_t key_type = key_type_arg;
3922 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003923 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003924 unsigned char *output_data = NULL;
3925 size_t output_size = 0;
3926 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003928 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003929 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003930
Gilles Peskine8817f612018-12-18 00:18:46 +01003931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003932
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3934 psa_set_key_algorithm( &attributes, alg );
3935 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003936
Gilles Peskine049c7532019-05-15 20:22:09 +02003937 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003938 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003939 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3940 key_bits = psa_get_key_bits( &attributes );
3941
3942 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3943 alg );
3944 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3945 expected_result != PSA_ERROR_NOT_SUPPORTED )
3946 {
3947 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3948 * should be exact. */
3949 TEST_EQUAL( output_size,
3950 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3951 TEST_ASSERT( output_size <=
3952 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3953 }
3954 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003955
Steven Cooremand588ea12021-01-11 19:36:04 +01003956 status = psa_aead_decrypt( key, alg,
3957 nonce->x, nonce->len,
3958 additional_data->x,
3959 additional_data->len,
3960 input_data->x, input_data->len,
3961 output_data, output_size,
3962 &output_length );
3963
Ronald Cron28a45ed2021-02-09 20:35:42 +01003964 /* If the operation is not supported, just skip and not fail in case the
3965 * decryption involves a common limitation of cryptography hardwares and
3966 * an alternative implementation. */
3967 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003968 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003969 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3970 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003971 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003972
3973 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003974
Gilles Peskine2d277862018-06-18 15:41:12 +02003975 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003976 ASSERT_COMPARE( expected_data->x, expected_data->len,
3977 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003978
Gilles Peskinea1cac842018-06-11 19:33:02 +02003979exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003980 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003981 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003982 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003983}
3984/* END_CASE */
3985
3986/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003987void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3988 int alg_arg,
3989 data_t *nonce,
3990 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003991 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003992 int do_set_lengths,
3993 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003994{
Paul Elliottd3f82412021-06-16 16:52:21 +01003995 size_t ad_part_len = 0;
3996 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003997 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003998
Paul Elliott32f46ba2021-09-23 18:24:36 +01003999 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004000 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004001 mbedtls_test_set_step( ad_part_len );
4002
4003 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004004 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004005 if( ad_part_len & 0x01 )
4006 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4007 else
4008 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004009 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004010
4011 /* Split ad into length(ad_part_len) parts. */
4012 if( !aead_multipart_internal_func( key_type_arg, key_data,
4013 alg_arg, nonce,
4014 additional_data,
4015 ad_part_len,
4016 input_data, -1,
4017 set_lengths_method,
4018 expected_output,
4019 1, 0 ) )
4020 break;
4021
4022 /* length(0) part, length(ad_part_len) part, length(0) part... */
4023 mbedtls_test_set_step( 1000 + ad_part_len );
4024
4025 if( !aead_multipart_internal_func( key_type_arg, key_data,
4026 alg_arg, nonce,
4027 additional_data,
4028 ad_part_len,
4029 input_data, -1,
4030 set_lengths_method,
4031 expected_output,
4032 1, 1 ) )
4033 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004034 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004035
Paul Elliott32f46ba2021-09-23 18:24:36 +01004036 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004037 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004038 /* Split data into length(data_part_len) parts. */
4039 mbedtls_test_set_step( 2000 + data_part_len );
4040
4041 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004042 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004043 if( data_part_len & 0x01 )
4044 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4045 else
4046 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004047 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004048
Paul Elliott32f46ba2021-09-23 18:24:36 +01004049 if( !aead_multipart_internal_func( key_type_arg, key_data,
4050 alg_arg, nonce,
4051 additional_data, -1,
4052 input_data, data_part_len,
4053 set_lengths_method,
4054 expected_output,
4055 1, 0 ) )
4056 break;
4057
4058 /* length(0) part, length(data_part_len) part, length(0) part... */
4059 mbedtls_test_set_step( 3000 + data_part_len );
4060
4061 if( !aead_multipart_internal_func( key_type_arg, key_data,
4062 alg_arg, nonce,
4063 additional_data, -1,
4064 input_data, data_part_len,
4065 set_lengths_method,
4066 expected_output,
4067 1, 1 ) )
4068 break;
4069 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004070
Paul Elliott8fc45162021-06-23 16:06:01 +01004071 /* Goto is required to silence warnings about unused labels, as we
4072 * don't actually do any test assertions in this function. */
4073 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004074}
4075/* END_CASE */
4076
4077/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004078void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4079 int alg_arg,
4080 data_t *nonce,
4081 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004082 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004083 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004084 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004085{
Paul Elliottd3f82412021-06-16 16:52:21 +01004086 size_t ad_part_len = 0;
4087 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004088 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004089
Paul Elliott32f46ba2021-09-23 18:24:36 +01004090 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004091 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004092 /* Split ad into length(ad_part_len) parts. */
4093 mbedtls_test_set_step( ad_part_len );
4094
4095 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004096 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004097 if( ad_part_len & 0x01 )
4098 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4099 else
4100 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004101 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004102
4103 if( !aead_multipart_internal_func( key_type_arg, key_data,
4104 alg_arg, nonce,
4105 additional_data,
4106 ad_part_len,
4107 input_data, -1,
4108 set_lengths_method,
4109 expected_output,
4110 0, 0 ) )
4111 break;
4112
4113 /* length(0) part, length(ad_part_len) part, length(0) part... */
4114 mbedtls_test_set_step( 1000 + ad_part_len );
4115
4116 if( !aead_multipart_internal_func( key_type_arg, key_data,
4117 alg_arg, nonce,
4118 additional_data,
4119 ad_part_len,
4120 input_data, -1,
4121 set_lengths_method,
4122 expected_output,
4123 0, 1 ) )
4124 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004125 }
4126
Paul Elliott32f46ba2021-09-23 18:24:36 +01004127 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004128 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004129 /* Split data into length(data_part_len) parts. */
4130 mbedtls_test_set_step( 2000 + data_part_len );
4131
4132 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004133 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004134 if( data_part_len & 0x01 )
4135 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4136 else
4137 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004138 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004139
4140 if( !aead_multipart_internal_func( key_type_arg, key_data,
4141 alg_arg, nonce,
4142 additional_data, -1,
4143 input_data, data_part_len,
4144 set_lengths_method,
4145 expected_output,
4146 0, 0 ) )
4147 break;
4148
4149 /* length(0) part, length(data_part_len) part, length(0) part... */
4150 mbedtls_test_set_step( 3000 + data_part_len );
4151
4152 if( !aead_multipart_internal_func( key_type_arg, key_data,
4153 alg_arg, nonce,
4154 additional_data, -1,
4155 input_data, data_part_len,
4156 set_lengths_method,
4157 expected_output,
4158 0, 1 ) )
4159 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004160 }
4161
Paul Elliott8fc45162021-06-23 16:06:01 +01004162 /* Goto is required to silence warnings about unused labels, as we
4163 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004164 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004165}
4166/* END_CASE */
4167
4168/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004169void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4170 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004171 int nonce_length,
4172 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004173 data_t *additional_data,
4174 data_t *input_data,
4175 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004176{
4177
4178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4179 psa_key_type_t key_type = key_type_arg;
4180 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004181 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004182 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4184 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004185 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004186 size_t actual_nonce_length = 0;
4187 size_t expected_nonce_length = expected_nonce_length_arg;
4188 unsigned char *output = NULL;
4189 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004190 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004191 size_t ciphertext_size = 0;
4192 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004193 size_t tag_length = 0;
4194 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004195
4196 PSA_ASSERT( psa_crypto_init( ) );
4197
4198 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4199 psa_set_key_algorithm( & attributes, alg );
4200 psa_set_key_type( & attributes, key_type );
4201
4202 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4203 &key ) );
4204
4205 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4206
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004207 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4208
Paul Elliottf1277632021-08-24 18:11:37 +01004209 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004210
Paul Elliottf1277632021-08-24 18:11:37 +01004211 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004212
Paul Elliottf1277632021-08-24 18:11:37 +01004213 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004214
Paul Elliottf1277632021-08-24 18:11:37 +01004215 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004216
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004217 status = psa_aead_encrypt_setup( &operation, key, alg );
4218
4219 /* If the operation is not supported, just skip and not fail in case the
4220 * encryption involves a common limitation of cryptography hardwares and
4221 * an alternative implementation. */
4222 if( status == PSA_ERROR_NOT_SUPPORTED )
4223 {
4224 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004225 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004226 }
4227
4228 PSA_ASSERT( status );
4229
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004230 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004231 nonce_length,
4232 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004233
Paul Elliott693bf312021-07-23 17:40:41 +01004234 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004235
Paul Elliottf1277632021-08-24 18:11:37 +01004236 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004237
Paul Elliott88ecbe12021-09-22 17:23:03 +01004238 if( expected_status == PSA_SUCCESS )
4239 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4240 alg ) );
4241
Paul Elliottd79c5c52021-10-06 21:49:41 +01004242 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004243
Paul Elliott693bf312021-07-23 17:40:41 +01004244 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004245 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004246 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004247 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4248 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004249
4250 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4251 additional_data->len ) );
4252
4253 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004254 output, output_size,
4255 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004256
Paul Elliottf1277632021-08-24 18:11:37 +01004257 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4258 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004259 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4260 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004261
4262exit:
4263 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004264 mbedtls_free( output );
4265 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004266 psa_aead_abort( &operation );
4267 PSA_DONE( );
4268}
4269/* END_CASE */
4270
4271/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004272void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4273 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004274 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004275 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004276 data_t *additional_data,
4277 data_t *input_data,
4278 int expected_status_arg )
4279{
4280
4281 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4282 psa_key_type_t key_type = key_type_arg;
4283 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004284 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004285 uint8_t *nonce_buffer = NULL;
4286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4287 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4288 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004289 unsigned char *output = NULL;
4290 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004291 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004292 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004293 size_t ciphertext_size = 0;
4294 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004295 size_t tag_length = 0;
4296 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004297 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004298 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004299
4300 PSA_ASSERT( psa_crypto_init( ) );
4301
4302 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4303 psa_set_key_algorithm( &attributes, alg );
4304 psa_set_key_type( &attributes, key_type );
4305
4306 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4307 &key ) );
4308
4309 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4310
4311 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4312
Paul Elliott6f0e7202021-08-25 12:57:18 +01004313 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004314
Paul Elliott6f0e7202021-08-25 12:57:18 +01004315 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004316
Paul Elliott6f0e7202021-08-25 12:57:18 +01004317 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004318
Paul Elliott6f0e7202021-08-25 12:57:18 +01004319 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004320
Paul Elliott863864a2021-07-23 17:28:31 +01004321 status = psa_aead_encrypt_setup( &operation, key, alg );
4322
4323 /* If the operation is not supported, just skip and not fail in case the
4324 * encryption involves a common limitation of cryptography hardwares and
4325 * an alternative implementation. */
4326 if( status == PSA_ERROR_NOT_SUPPORTED )
4327 {
4328 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004329 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004330 }
4331
4332 PSA_ASSERT( status );
4333
Paul Elliott4023ffd2021-09-10 16:21:22 +01004334 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4335 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004336 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004337 /* Arbitrary size buffer, to test zero length valid buffer. */
4338 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004339 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004340 }
4341 else
4342 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004343 /* If length is zero, then this will return NULL. */
4344 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004345 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004346
Paul Elliott4023ffd2021-09-10 16:21:22 +01004347 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004348 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004349 for( index = 0; index < nonce_length - 1; ++index )
4350 {
4351 nonce_buffer[index] = 'a' + index;
4352 }
Paul Elliott66696b52021-08-16 18:42:41 +01004353 }
Paul Elliott863864a2021-07-23 17:28:31 +01004354 }
4355
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004356 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4357 {
4358 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4359 input_data->len ) );
4360 }
4361
Paul Elliott6f0e7202021-08-25 12:57:18 +01004362 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004363
Paul Elliott693bf312021-07-23 17:40:41 +01004364 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004365
4366 if( expected_status == PSA_SUCCESS )
4367 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004368 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4369 {
4370 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4371 input_data->len ) );
4372 }
4373 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4374 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004375
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004376 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4377 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4378 additional_data->len ),
4379 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004380
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004381 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004382 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004383 &ciphertext_length ),
4384 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004385
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004386 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004387 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004388 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4389 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004390 }
4391
4392exit:
4393 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004394 mbedtls_free( output );
4395 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004396 mbedtls_free( nonce_buffer );
4397 psa_aead_abort( &operation );
4398 PSA_DONE( );
4399}
4400/* END_CASE */
4401
4402/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004403void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4404 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004405 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004406 data_t *nonce,
4407 data_t *additional_data,
4408 data_t *input_data,
4409 int expected_status_arg )
4410{
4411
4412 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4413 psa_key_type_t key_type = key_type_arg;
4414 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004415 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4417 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4418 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004419 unsigned char *output = NULL;
4420 unsigned char *ciphertext = NULL;
4421 size_t output_size = output_size_arg;
4422 size_t ciphertext_size = 0;
4423 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004424 size_t tag_length = 0;
4425 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4426
4427 PSA_ASSERT( psa_crypto_init( ) );
4428
4429 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4430 psa_set_key_algorithm( &attributes, alg );
4431 psa_set_key_type( &attributes, key_type );
4432
4433 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4434 &key ) );
4435
4436 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4437
Paul Elliottc6d11d02021-09-01 12:04:23 +01004438 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004439
Paul Elliottc6d11d02021-09-01 12:04:23 +01004440 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004441
Paul Elliottc6d11d02021-09-01 12:04:23 +01004442 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004443
Paul Elliott43fbda62021-07-23 18:30:59 +01004444 status = psa_aead_encrypt_setup( &operation, key, alg );
4445
4446 /* If the operation is not supported, just skip and not fail in case the
4447 * encryption involves a common limitation of cryptography hardwares and
4448 * an alternative implementation. */
4449 if( status == PSA_ERROR_NOT_SUPPORTED )
4450 {
4451 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4452 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4453 }
4454
4455 PSA_ASSERT( status );
4456
Paul Elliott47b9a142021-10-07 15:04:57 +01004457 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4458 input_data->len ) );
4459
Paul Elliott43fbda62021-07-23 18:30:59 +01004460 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4461
4462 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4463 additional_data->len ) );
4464
4465 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004466 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004467
4468 TEST_EQUAL( status, expected_status );
4469
4470 if( expected_status == PSA_SUCCESS )
4471 {
4472 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004473 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4474 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004475 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4476 }
4477
4478exit:
4479 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004480 mbedtls_free( output );
4481 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004482 psa_aead_abort( &operation );
4483 PSA_DONE( );
4484}
4485/* END_CASE */
4486
Paul Elliott91b021e2021-07-23 18:52:31 +01004487/* BEGIN_CASE */
4488void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4489 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004490 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004491 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004492 data_t *nonce,
4493 data_t *additional_data,
4494 data_t *input_data,
4495 int expected_status_arg )
4496{
4497
4498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4499 psa_key_type_t key_type = key_type_arg;
4500 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004501 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4503 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4504 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004505 unsigned char *ciphertext = NULL;
4506 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004507 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004508 size_t ciphertext_size = 0;
4509 size_t ciphertext_length = 0;
4510 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004511 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004512 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004513
4514 PSA_ASSERT( psa_crypto_init( ) );
4515
4516 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4517 psa_set_key_algorithm( &attributes, alg );
4518 psa_set_key_type( &attributes, key_type );
4519
4520 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4521 &key ) );
4522
4523 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4524
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004525 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004526
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004527 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004528
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004529 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004530
Paul Elliott719c1322021-09-13 18:27:22 +01004531 ASSERT_ALLOC( tag_buffer, tag_size );
4532
Paul Elliott91b021e2021-07-23 18:52:31 +01004533 status = psa_aead_encrypt_setup( &operation, key, alg );
4534
4535 /* If the operation is not supported, just skip and not fail in case the
4536 * encryption involves a common limitation of cryptography hardwares and
4537 * an alternative implementation. */
4538 if( status == PSA_ERROR_NOT_SUPPORTED )
4539 {
4540 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4541 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4542 }
4543
4544 PSA_ASSERT( status );
4545
4546 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4547
Paul Elliott76bda482021-10-07 17:07:23 +01004548 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4549 input_data->len ) );
4550
Paul Elliott91b021e2021-07-23 18:52:31 +01004551 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4552 additional_data->len ) );
4553
4554 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004555 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004556
4557 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004558 status = psa_aead_finish( &operation, finish_ciphertext,
4559 finish_ciphertext_size,
4560 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004561 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004562
4563 TEST_EQUAL( status, expected_status );
4564
4565exit:
4566 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004567 mbedtls_free( ciphertext );
4568 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004569 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004570 psa_aead_abort( &operation );
4571 PSA_DONE( );
4572}
4573/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004574
4575/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004576void aead_multipart_verify( int key_type_arg, data_t *key_data,
4577 int alg_arg,
4578 data_t *nonce,
4579 data_t *additional_data,
4580 data_t *input_data,
4581 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004582 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004583 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004584 int expected_status_arg )
4585{
4586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4587 psa_key_type_t key_type = key_type_arg;
4588 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004589 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004590 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4591 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4592 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004593 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004594 unsigned char *plaintext = NULL;
4595 unsigned char *finish_plaintext = NULL;
4596 size_t plaintext_size = 0;
4597 size_t plaintext_length = 0;
4598 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004599 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004600 unsigned char *tag_buffer = NULL;
4601 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004602
4603 PSA_ASSERT( psa_crypto_init( ) );
4604
4605 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4606 psa_set_key_algorithm( &attributes, alg );
4607 psa_set_key_type( &attributes, key_type );
4608
4609 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4610 &key ) );
4611
4612 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4613
4614 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4615 input_data->len );
4616
4617 ASSERT_ALLOC( plaintext, plaintext_size );
4618
4619 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4620
4621 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4622
Paul Elliott9961a662021-09-17 19:19:02 +01004623 status = psa_aead_decrypt_setup( &operation, key, alg );
4624
4625 /* If the operation is not supported, just skip and not fail in case the
4626 * encryption involves a common limitation of cryptography hardwares and
4627 * an alternative implementation. */
4628 if( status == PSA_ERROR_NOT_SUPPORTED )
4629 {
4630 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4631 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4632 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004633 TEST_EQUAL( status, expected_setup_status );
4634
4635 if( status != PSA_SUCCESS )
4636 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004637
4638 PSA_ASSERT( status );
4639
4640 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4641
Paul Elliottfec6f372021-10-06 17:15:02 +01004642 status = psa_aead_set_lengths( &operation, additional_data->len,
4643 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004644 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004645
Paul Elliott9961a662021-09-17 19:19:02 +01004646 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4647 additional_data->len ) );
4648
4649 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4650 input_data->len,
4651 plaintext, plaintext_size,
4652 &plaintext_length ) );
4653
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004654 if( tag_usage == USE_GIVEN_TAG )
4655 {
4656 tag_buffer = tag->x;
4657 tag_size = tag->len;
4658 }
4659
Paul Elliott9961a662021-09-17 19:19:02 +01004660 status = psa_aead_verify( &operation, finish_plaintext,
4661 verify_plaintext_size,
4662 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004663 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004664
4665 TEST_EQUAL( status, expected_status );
4666
4667exit:
4668 psa_destroy_key( key );
4669 mbedtls_free( plaintext );
4670 mbedtls_free( finish_plaintext );
4671 psa_aead_abort( &operation );
4672 PSA_DONE( );
4673}
4674/* END_CASE */
4675
Paul Elliott9961a662021-09-17 19:19:02 +01004676/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004677void aead_multipart_setup( int key_type_arg, data_t *key_data,
4678 int alg_arg, int expected_status_arg )
4679{
4680 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4681 psa_key_type_t key_type = key_type_arg;
4682 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004683 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4685 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4686 psa_status_t expected_status = expected_status_arg;
4687
4688 PSA_ASSERT( psa_crypto_init( ) );
4689
4690 psa_set_key_usage_flags( &attributes,
4691 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4692 psa_set_key_algorithm( &attributes, alg );
4693 psa_set_key_type( &attributes, key_type );
4694
4695 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4696 &key ) );
4697
Paul Elliott5221ef62021-09-19 17:33:03 +01004698 status = psa_aead_encrypt_setup( &operation, key, alg );
4699
4700 TEST_EQUAL( status, expected_status );
4701
4702 psa_aead_abort( &operation );
4703
Paul Elliott5221ef62021-09-19 17:33:03 +01004704 status = psa_aead_decrypt_setup( &operation, key, alg );
4705
4706 TEST_EQUAL(status, expected_status );
4707
4708exit:
4709 psa_destroy_key( key );
4710 psa_aead_abort( &operation );
4711 PSA_DONE( );
4712}
4713/* END_CASE */
4714
4715/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004716void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4717 int alg_arg,
4718 data_t *nonce,
4719 data_t *additional_data,
4720 data_t *input_data )
4721{
4722 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4723 psa_key_type_t key_type = key_type_arg;
4724 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004725 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004726 unsigned char *output_data = NULL;
4727 unsigned char *final_data = NULL;
4728 size_t output_size = 0;
4729 size_t finish_output_size = 0;
4730 size_t output_length = 0;
4731 size_t key_bits = 0;
4732 size_t tag_length = 0;
4733 size_t tag_size = 0;
4734 size_t nonce_length = 0;
4735 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4736 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4737 size_t output_part_length = 0;
4738 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4739
4740 PSA_ASSERT( psa_crypto_init( ) );
4741
4742 psa_set_key_usage_flags( & attributes,
4743 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4744 psa_set_key_algorithm( & attributes, alg );
4745 psa_set_key_type( & attributes, key_type );
4746
4747 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4748 &key ) );
4749
4750 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4751 key_bits = psa_get_key_bits( &attributes );
4752
4753 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4754
4755 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4756
4757 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4758
4759 ASSERT_ALLOC( output_data, output_size );
4760
4761 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4762
4763 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4764
4765 ASSERT_ALLOC( final_data, finish_output_size );
4766
4767 /* Test all operations error without calling setup first. */
4768
Paul Elliottc23a9a02021-06-21 18:32:46 +01004769 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4770 PSA_ERROR_BAD_STATE );
4771
4772 psa_aead_abort( &operation );
4773
Paul Elliottc23a9a02021-06-21 18:32:46 +01004774 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4775 PSA_AEAD_NONCE_MAX_SIZE,
4776 &nonce_length ),
4777 PSA_ERROR_BAD_STATE );
4778
4779 psa_aead_abort( &operation );
4780
Paul Elliott481be342021-07-16 17:38:47 +01004781 /* ------------------------------------------------------- */
4782
Paul Elliottc23a9a02021-06-21 18:32:46 +01004783 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4784 input_data->len ),
4785 PSA_ERROR_BAD_STATE );
4786
4787 psa_aead_abort( &operation );
4788
Paul Elliott481be342021-07-16 17:38:47 +01004789 /* ------------------------------------------------------- */
4790
Paul Elliottc23a9a02021-06-21 18:32:46 +01004791 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4792 additional_data->len ),
4793 PSA_ERROR_BAD_STATE );
4794
4795 psa_aead_abort( &operation );
4796
Paul Elliott481be342021-07-16 17:38:47 +01004797 /* ------------------------------------------------------- */
4798
Paul Elliottc23a9a02021-06-21 18:32:46 +01004799 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4800 input_data->len, output_data,
4801 output_size, &output_length ),
4802 PSA_ERROR_BAD_STATE );
4803
4804 psa_aead_abort( &operation );
4805
Paul Elliott481be342021-07-16 17:38:47 +01004806 /* ------------------------------------------------------- */
4807
Paul Elliottc23a9a02021-06-21 18:32:46 +01004808 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4809 finish_output_size,
4810 &output_part_length,
4811 tag_buffer, tag_length,
4812 &tag_size ),
4813 PSA_ERROR_BAD_STATE );
4814
4815 psa_aead_abort( &operation );
4816
Paul Elliott481be342021-07-16 17:38:47 +01004817 /* ------------------------------------------------------- */
4818
Paul Elliottc23a9a02021-06-21 18:32:46 +01004819 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4820 finish_output_size,
4821 &output_part_length,
4822 tag_buffer,
4823 tag_length ),
4824 PSA_ERROR_BAD_STATE );
4825
4826 psa_aead_abort( &operation );
4827
4828 /* Test for double setups. */
4829
Paul Elliottc23a9a02021-06-21 18:32:46 +01004830 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4831
4832 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4833 PSA_ERROR_BAD_STATE );
4834
4835 psa_aead_abort( &operation );
4836
Paul Elliott481be342021-07-16 17:38:47 +01004837 /* ------------------------------------------------------- */
4838
Paul Elliottc23a9a02021-06-21 18:32:46 +01004839 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4840
4841 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4842 PSA_ERROR_BAD_STATE );
4843
4844 psa_aead_abort( &operation );
4845
Paul Elliott374a2be2021-07-16 17:53:40 +01004846 /* ------------------------------------------------------- */
4847
Paul Elliott374a2be2021-07-16 17:53:40 +01004848 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4849
4850 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4851 PSA_ERROR_BAD_STATE );
4852
4853 psa_aead_abort( &operation );
4854
4855 /* ------------------------------------------------------- */
4856
Paul Elliott374a2be2021-07-16 17:53:40 +01004857 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4858
4859 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4860 PSA_ERROR_BAD_STATE );
4861
4862 psa_aead_abort( &operation );
4863
Paul Elliottc23a9a02021-06-21 18:32:46 +01004864 /* Test for not setting a nonce. */
4865
Paul Elliottc23a9a02021-06-21 18:32:46 +01004866 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4867
4868 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4869 additional_data->len ),
4870 PSA_ERROR_BAD_STATE );
4871
4872 psa_aead_abort( &operation );
4873
Paul Elliott7f628422021-09-01 12:08:29 +01004874 /* ------------------------------------------------------- */
4875
Paul Elliott7f628422021-09-01 12:08:29 +01004876 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4877
4878 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4879 input_data->len, output_data,
4880 output_size, &output_length ),
4881 PSA_ERROR_BAD_STATE );
4882
4883 psa_aead_abort( &operation );
4884
Paul Elliottbdc2c682021-09-21 18:37:10 +01004885 /* ------------------------------------------------------- */
4886
Paul Elliottbdc2c682021-09-21 18:37:10 +01004887 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4888
4889 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4890 finish_output_size,
4891 &output_part_length,
4892 tag_buffer, tag_length,
4893 &tag_size ),
4894 PSA_ERROR_BAD_STATE );
4895
4896 psa_aead_abort( &operation );
4897
4898 /* ------------------------------------------------------- */
4899
Paul Elliottbdc2c682021-09-21 18:37:10 +01004900 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4901
4902 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4903 finish_output_size,
4904 &output_part_length,
4905 tag_buffer,
4906 tag_length ),
4907 PSA_ERROR_BAD_STATE );
4908
4909 psa_aead_abort( &operation );
4910
Paul Elliottc23a9a02021-06-21 18:32:46 +01004911 /* Test for double setting nonce. */
4912
Paul Elliottc23a9a02021-06-21 18:32:46 +01004913 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4914
4915 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4916
4917 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4918 PSA_ERROR_BAD_STATE );
4919
4920 psa_aead_abort( &operation );
4921
Paul Elliott374a2be2021-07-16 17:53:40 +01004922 /* Test for double generating nonce. */
4923
Paul Elliott374a2be2021-07-16 17:53:40 +01004924 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4925
4926 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4927 PSA_AEAD_NONCE_MAX_SIZE,
4928 &nonce_length ) );
4929
4930 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4931 PSA_AEAD_NONCE_MAX_SIZE,
4932 &nonce_length ),
4933 PSA_ERROR_BAD_STATE );
4934
4935
4936 psa_aead_abort( &operation );
4937
4938 /* Test for generate nonce then set and vice versa */
4939
Paul Elliott374a2be2021-07-16 17:53:40 +01004940 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4941
4942 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4943 PSA_AEAD_NONCE_MAX_SIZE,
4944 &nonce_length ) );
4945
4946 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4947 PSA_ERROR_BAD_STATE );
4948
4949 psa_aead_abort( &operation );
4950
Andrzej Kurekad837522021-12-15 15:28:49 +01004951 /* Test for generating nonce after calling set lengths */
4952
4953 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4954
4955 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4956 input_data->len ) );
4957
4958 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4959 PSA_AEAD_NONCE_MAX_SIZE,
4960 &nonce_length ) );
4961
4962 psa_aead_abort( &operation );
4963
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004964 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004965
4966 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4967
4968 if( operation.alg == PSA_ALG_CCM )
4969 {
4970 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4971 input_data->len ),
4972 PSA_ERROR_INVALID_ARGUMENT );
4973 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4974 PSA_AEAD_NONCE_MAX_SIZE,
4975 &nonce_length ),
4976 PSA_ERROR_BAD_STATE );
4977 }
4978 else
4979 {
4980 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4981 input_data->len ) );
4982 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4983 PSA_AEAD_NONCE_MAX_SIZE,
4984 &nonce_length ) );
4985 }
4986
4987 psa_aead_abort( &operation );
4988
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004989 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004990#if SIZE_MAX > UINT32_MAX
4991 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4992
4993 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
4994 {
4995 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
4996 input_data->len ),
4997 PSA_ERROR_INVALID_ARGUMENT );
4998 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4999 PSA_AEAD_NONCE_MAX_SIZE,
5000 &nonce_length ),
5001 PSA_ERROR_BAD_STATE );
5002 }
5003 else
5004 {
5005 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5006 input_data->len ) );
5007 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5008 PSA_AEAD_NONCE_MAX_SIZE,
5009 &nonce_length ) );
5010 }
5011
5012 psa_aead_abort( &operation );
5013#endif
5014
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005015 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005016
5017 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5018
5019 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5020 PSA_AEAD_NONCE_MAX_SIZE,
5021 &nonce_length ) );
5022
5023 if( operation.alg == PSA_ALG_CCM )
5024 {
5025 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5026 input_data->len ),
5027 PSA_ERROR_INVALID_ARGUMENT );
5028 }
5029 else
5030 {
5031 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5032 input_data->len ) );
5033 }
5034
5035 psa_aead_abort( &operation );
5036
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005037 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005038 /* Test for setting nonce after calling set lengths */
5039
5040 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5041
5042 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5043 input_data->len ) );
5044
5045 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5046
5047 psa_aead_abort( &operation );
5048
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005049 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005050
5051 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5052
5053 if( operation.alg == PSA_ALG_CCM )
5054 {
5055 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5056 input_data->len ),
5057 PSA_ERROR_INVALID_ARGUMENT );
5058 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5059 PSA_ERROR_BAD_STATE );
5060 }
5061 else
5062 {
5063 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5064 input_data->len ) );
5065 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5066 }
5067
5068 psa_aead_abort( &operation );
5069
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005070 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005071#if SIZE_MAX > UINT32_MAX
5072 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5073
5074 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5075 {
5076 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5077 input_data->len ),
5078 PSA_ERROR_INVALID_ARGUMENT );
5079 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5080 PSA_ERROR_BAD_STATE );
5081 }
5082 else
5083 {
5084 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5085 input_data->len ) );
5086 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5087 }
5088
5089 psa_aead_abort( &operation );
5090#endif
5091
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005092 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005093
5094 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5095
5096 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5097
5098 if( operation.alg == PSA_ALG_CCM )
5099 {
5100 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5101 input_data->len ),
5102 PSA_ERROR_INVALID_ARGUMENT );
5103 }
5104 else
5105 {
5106 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5107 input_data->len ) );
5108 }
5109
5110 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005111
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005112 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005113#if SIZE_MAX > UINT32_MAX
5114 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5115
5116 if( operation.alg == PSA_ALG_GCM )
5117 {
5118 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5119 SIZE_MAX ),
5120 PSA_ERROR_INVALID_ARGUMENT );
5121 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5122 PSA_ERROR_BAD_STATE );
5123 }
5124 else if ( operation.alg != PSA_ALG_CCM )
5125 {
5126 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5127 SIZE_MAX ) );
5128 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5129 }
5130
5131 psa_aead_abort( &operation );
5132
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005133 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005134 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5135
5136 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5137
5138 if( operation.alg == PSA_ALG_GCM )
5139 {
5140 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5141 SIZE_MAX ),
5142 PSA_ERROR_INVALID_ARGUMENT );
5143 }
5144 else if ( operation.alg != PSA_ALG_CCM )
5145 {
5146 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5147 SIZE_MAX ) );
5148 }
5149
5150 psa_aead_abort( &operation );
5151#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005152
5153 /* ------------------------------------------------------- */
5154
Paul Elliott374a2be2021-07-16 17:53:40 +01005155 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5156
5157 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5158
5159 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5160 PSA_AEAD_NONCE_MAX_SIZE,
5161 &nonce_length ),
5162 PSA_ERROR_BAD_STATE );
5163
5164 psa_aead_abort( &operation );
5165
Paul Elliott7220cae2021-06-22 17:25:57 +01005166 /* Test for generating nonce in decrypt setup. */
5167
Paul Elliott7220cae2021-06-22 17:25:57 +01005168 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5169
5170 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5171 PSA_AEAD_NONCE_MAX_SIZE,
5172 &nonce_length ),
5173 PSA_ERROR_BAD_STATE );
5174
5175 psa_aead_abort( &operation );
5176
Paul Elliottc23a9a02021-06-21 18:32:46 +01005177 /* Test for setting lengths twice. */
5178
Paul Elliottc23a9a02021-06-21 18:32:46 +01005179 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5180
5181 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5182
5183 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5184 input_data->len ) );
5185
5186 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5187 input_data->len ),
5188 PSA_ERROR_BAD_STATE );
5189
5190 psa_aead_abort( &operation );
5191
Andrzej Kurekad837522021-12-15 15:28:49 +01005192 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005193
Paul Elliottc23a9a02021-06-21 18:32:46 +01005194 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5195
5196 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5197
Andrzej Kurekad837522021-12-15 15:28:49 +01005198 if( operation.alg == PSA_ALG_CCM )
5199 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005200
Andrzej Kurekad837522021-12-15 15:28:49 +01005201 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5202 additional_data->len ),
5203 PSA_ERROR_BAD_STATE );
5204 }
5205 else
5206 {
5207 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5208 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005209
Andrzej Kurekad837522021-12-15 15:28:49 +01005210 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5211 input_data->len ),
5212 PSA_ERROR_BAD_STATE );
5213 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005214 psa_aead_abort( &operation );
5215
5216 /* ------------------------------------------------------- */
5217
Paul Elliottf94bd992021-09-19 18:15:59 +01005218 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5219
5220 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5221
Andrzej Kurekad837522021-12-15 15:28:49 +01005222 if( operation.alg == PSA_ALG_CCM )
5223 {
5224 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5225 input_data->len, output_data,
5226 output_size, &output_length ),
5227 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005228
Andrzej Kurekad837522021-12-15 15:28:49 +01005229 }
5230 else
5231 {
5232 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5233 input_data->len, output_data,
5234 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005235
Andrzej Kurekad837522021-12-15 15:28:49 +01005236 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5237 input_data->len ),
5238 PSA_ERROR_BAD_STATE );
5239 }
5240 psa_aead_abort( &operation );
5241
5242 /* ------------------------------------------------------- */
5243
5244 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5245
5246 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5247
5248 if( operation.alg == PSA_ALG_CCM )
5249 {
5250 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5251 finish_output_size,
5252 &output_part_length,
5253 tag_buffer, tag_length,
5254 &tag_size ) );
5255 }
5256 else
5257 {
5258 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5259 finish_output_size,
5260 &output_part_length,
5261 tag_buffer, tag_length,
5262 &tag_size ) );
5263
5264 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5265 input_data->len ),
5266 PSA_ERROR_BAD_STATE );
5267 }
5268 psa_aead_abort( &operation );
5269
5270 /* Test for setting lengths after generating nonce + already starting data. */
5271
5272 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5273
5274 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5275 PSA_AEAD_NONCE_MAX_SIZE,
5276 &nonce_length ) );
5277 if( operation.alg == PSA_ALG_CCM )
5278 {
5279
5280 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5281 additional_data->len ),
5282 PSA_ERROR_BAD_STATE );
5283 }
5284 else
5285 {
5286 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5287 additional_data->len ) );
5288
5289 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5290 input_data->len ),
5291 PSA_ERROR_BAD_STATE );
5292 }
5293 psa_aead_abort( &operation );
5294
5295 /* ------------------------------------------------------- */
5296
5297 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5298
5299 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5300 PSA_AEAD_NONCE_MAX_SIZE,
5301 &nonce_length ) );
5302 if( operation.alg == PSA_ALG_CCM )
5303 {
5304 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5305 input_data->len, output_data,
5306 output_size, &output_length ),
5307 PSA_ERROR_BAD_STATE );
5308
5309 }
5310 else
5311 {
5312 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5313 input_data->len, output_data,
5314 output_size, &output_length ) );
5315
5316 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5317 input_data->len ),
5318 PSA_ERROR_BAD_STATE );
5319 }
5320 psa_aead_abort( &operation );
5321
5322 /* ------------------------------------------------------- */
5323
5324 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5325
5326 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5327 PSA_AEAD_NONCE_MAX_SIZE,
5328 &nonce_length ) );
5329 if( operation.alg == PSA_ALG_CCM )
5330 {
5331 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5332 finish_output_size,
5333 &output_part_length,
5334 tag_buffer, tag_length,
5335 &tag_size ) );
5336 }
5337 else
5338 {
5339 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5340 finish_output_size,
5341 &output_part_length,
5342 tag_buffer, tag_length,
5343 &tag_size ) );
5344
5345 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5346 input_data->len ),
5347 PSA_ERROR_BAD_STATE );
5348 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005349 psa_aead_abort( &operation );
5350
Paul Elliott243080c2021-07-21 19:01:17 +01005351 /* Test for not sending any additional data or data after setting non zero
5352 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005353
Paul Elliottc23a9a02021-06-21 18:32:46 +01005354 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5355
5356 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5357
5358 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5359 input_data->len ) );
5360
5361 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5362 finish_output_size,
5363 &output_part_length,
5364 tag_buffer, tag_length,
5365 &tag_size ),
5366 PSA_ERROR_INVALID_ARGUMENT );
5367
5368 psa_aead_abort( &operation );
5369
Paul Elliott243080c2021-07-21 19:01:17 +01005370 /* Test for not sending any additional data or data after setting non-zero
5371 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005372
Paul Elliottc23a9a02021-06-21 18:32:46 +01005373 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5374
5375 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5376
5377 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5378 input_data->len ) );
5379
5380 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5381 finish_output_size,
5382 &output_part_length,
5383 tag_buffer,
5384 tag_length ),
5385 PSA_ERROR_INVALID_ARGUMENT );
5386
5387 psa_aead_abort( &operation );
5388
Paul Elliott243080c2021-07-21 19:01:17 +01005389 /* Test for not sending any additional data after setting a non-zero length
5390 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005391
Paul Elliottc23a9a02021-06-21 18:32:46 +01005392 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5393
5394 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5395
5396 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5397 input_data->len ) );
5398
5399 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5400 input_data->len, output_data,
5401 output_size, &output_length ),
5402 PSA_ERROR_INVALID_ARGUMENT );
5403
5404 psa_aead_abort( &operation );
5405
Paul Elliottf94bd992021-09-19 18:15:59 +01005406 /* Test for not sending any data after setting a non-zero length for it.*/
5407
Paul Elliottf94bd992021-09-19 18:15:59 +01005408 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5409
5410 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5411
5412 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5413 input_data->len ) );
5414
5415 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5416 additional_data->len ) );
5417
5418 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5419 finish_output_size,
5420 &output_part_length,
5421 tag_buffer, tag_length,
5422 &tag_size ),
5423 PSA_ERROR_INVALID_ARGUMENT );
5424
5425 psa_aead_abort( &operation );
5426
Paul Elliottb0450fe2021-09-01 15:06:26 +01005427 /* Test for sending too much additional data after setting lengths. */
5428
Paul Elliottb0450fe2021-09-01 15:06:26 +01005429 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5430
5431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5432
5433 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5434
5435
5436 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5437 additional_data->len ),
5438 PSA_ERROR_INVALID_ARGUMENT );
5439
5440 psa_aead_abort( &operation );
5441
Paul Elliotta2a09b02021-09-22 14:56:40 +01005442 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005443
5444 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5445
5446 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5447
5448 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5449 input_data->len ) );
5450
5451 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5452 additional_data->len ) );
5453
5454 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5455 1 ),
5456 PSA_ERROR_INVALID_ARGUMENT );
5457
5458 psa_aead_abort( &operation );
5459
Paul Elliottb0450fe2021-09-01 15:06:26 +01005460 /* Test for sending too much data after setting lengths. */
5461
Paul Elliottb0450fe2021-09-01 15:06:26 +01005462 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5463
5464 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5465
5466 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5467
5468 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5469 input_data->len, output_data,
5470 output_size, &output_length ),
5471 PSA_ERROR_INVALID_ARGUMENT );
5472
5473 psa_aead_abort( &operation );
5474
Paul Elliotta2a09b02021-09-22 14:56:40 +01005475 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005476
5477 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5478
5479 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5480
5481 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5482 input_data->len ) );
5483
5484 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5485 additional_data->len ) );
5486
5487 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5488 input_data->len, output_data,
5489 output_size, &output_length ) );
5490
5491 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5492 1, output_data,
5493 output_size, &output_length ),
5494 PSA_ERROR_INVALID_ARGUMENT );
5495
5496 psa_aead_abort( &operation );
5497
Paul Elliottc23a9a02021-06-21 18:32:46 +01005498 /* Test sending additional data after data. */
5499
Paul Elliottc23a9a02021-06-21 18:32:46 +01005500 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5501
5502 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5503
Andrzej Kurekad837522021-12-15 15:28:49 +01005504 if( operation.alg != PSA_ALG_CCM )
5505 {
5506 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5507 input_data->len, output_data,
5508 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005509
Andrzej Kurekad837522021-12-15 15:28:49 +01005510 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5511 additional_data->len ),
5512 PSA_ERROR_BAD_STATE );
5513 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005514 psa_aead_abort( &operation );
5515
Paul Elliott534d0b42021-06-22 19:15:20 +01005516 /* Test calling finish on decryption. */
5517
Paul Elliott534d0b42021-06-22 19:15:20 +01005518 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5519
5520 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5521
5522 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5523 finish_output_size,
5524 &output_part_length,
5525 tag_buffer, tag_length,
5526 &tag_size ),
5527 PSA_ERROR_BAD_STATE );
5528
5529 psa_aead_abort( &operation );
5530
5531 /* Test calling verify on encryption. */
5532
Paul Elliott534d0b42021-06-22 19:15:20 +01005533 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5534
5535 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5536
5537 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5538 finish_output_size,
5539 &output_part_length,
5540 tag_buffer,
5541 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005542 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005543
5544 psa_aead_abort( &operation );
5545
5546
Paul Elliottc23a9a02021-06-21 18:32:46 +01005547exit:
5548 psa_destroy_key( key );
5549 psa_aead_abort( &operation );
5550 mbedtls_free( output_data );
5551 mbedtls_free( final_data );
5552 PSA_DONE( );
5553}
5554/* END_CASE */
5555
5556/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005557void signature_size( int type_arg,
5558 int bits,
5559 int alg_arg,
5560 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005561{
5562 psa_key_type_t type = type_arg;
5563 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005564 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005565
Gilles Peskinefe11b722018-12-18 00:24:04 +01005566 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005567
Gilles Peskinee59236f2018-01-27 23:32:46 +01005568exit:
5569 ;
5570}
5571/* END_CASE */
5572
5573/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005574void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5575 int alg_arg, data_t *input_data,
5576 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005577{
Ronald Cron5425a212020-08-04 14:58:35 +02005578 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005579 psa_key_type_t key_type = key_type_arg;
5580 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005581 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005582 unsigned char *signature = NULL;
5583 size_t signature_size;
5584 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005586
Gilles Peskine8817f612018-12-18 00:18:46 +01005587 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005588
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005589 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005590 psa_set_key_algorithm( &attributes, alg );
5591 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005592
Gilles Peskine049c7532019-05-15 20:22:09 +02005593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005594 &key ) );
5595 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005596 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005597
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005598 /* Allocate a buffer which has the size advertized by the
5599 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005600 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005601 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005602 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005603 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005604 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005605
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005606 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005607 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005608 input_data->x, input_data->len,
5609 signature, signature_size,
5610 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005611 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005612 ASSERT_COMPARE( output_data->x, output_data->len,
5613 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005614
5615exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005616 /*
5617 * Key attributes may have been returned by psa_get_key_attributes()
5618 * thus reset them as required.
5619 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005620 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005621
Ronald Cron5425a212020-08-04 14:58:35 +02005622 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005623 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005624 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005625}
5626/* END_CASE */
5627
5628/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005629void sign_hash_fail( int key_type_arg, data_t *key_data,
5630 int alg_arg, data_t *input_data,
5631 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005632{
Ronald Cron5425a212020-08-04 14:58:35 +02005633 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005634 psa_key_type_t key_type = key_type_arg;
5635 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005636 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005637 psa_status_t actual_status;
5638 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005639 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005640 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005642
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005643 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005644
Gilles Peskine8817f612018-12-18 00:18:46 +01005645 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005646
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005647 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005648 psa_set_key_algorithm( &attributes, alg );
5649 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005650
Gilles Peskine049c7532019-05-15 20:22:09 +02005651 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005652 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005653
Ronald Cron5425a212020-08-04 14:58:35 +02005654 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005655 input_data->x, input_data->len,
5656 signature, signature_size,
5657 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005658 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005659 /* The value of *signature_length is unspecified on error, but
5660 * whatever it is, it should be less than signature_size, so that
5661 * if the caller tries to read *signature_length bytes without
5662 * checking the error code then they don't overflow a buffer. */
5663 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005664
5665exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005666 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005667 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005668 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005669 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005670}
5671/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005672
5673/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005674void sign_verify_hash( int key_type_arg, data_t *key_data,
5675 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005676{
Ronald Cron5425a212020-08-04 14:58:35 +02005677 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005678 psa_key_type_t key_type = key_type_arg;
5679 psa_algorithm_t alg = alg_arg;
5680 size_t key_bits;
5681 unsigned char *signature = NULL;
5682 size_t signature_size;
5683 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005685
Gilles Peskine8817f612018-12-18 00:18:46 +01005686 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005687
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005688 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005689 psa_set_key_algorithm( &attributes, alg );
5690 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005691
Gilles Peskine049c7532019-05-15 20:22:09 +02005692 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005693 &key ) );
5694 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005695 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005696
5697 /* Allocate a buffer which has the size advertized by the
5698 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005699 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005700 key_bits, alg );
5701 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005702 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005703 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005704
5705 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005706 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005707 input_data->x, input_data->len,
5708 signature, signature_size,
5709 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005710 /* Check that the signature length looks sensible. */
5711 TEST_ASSERT( signature_length <= signature_size );
5712 TEST_ASSERT( signature_length > 0 );
5713
5714 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005715 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005716 input_data->x, input_data->len,
5717 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005718
5719 if( input_data->len != 0 )
5720 {
5721 /* Flip a bit in the input and verify that the signature is now
5722 * detected as invalid. Flip a bit at the beginning, not at the end,
5723 * because ECDSA may ignore the last few bits of the input. */
5724 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005725 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005726 input_data->x, input_data->len,
5727 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005728 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005729 }
5730
5731exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005732 /*
5733 * Key attributes may have been returned by psa_get_key_attributes()
5734 * thus reset them as required.
5735 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005736 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005737
Ronald Cron5425a212020-08-04 14:58:35 +02005738 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005739 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005740 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005741}
5742/* END_CASE */
5743
5744/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005745void verify_hash( int key_type_arg, data_t *key_data,
5746 int alg_arg, data_t *hash_data,
5747 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005748{
Ronald Cron5425a212020-08-04 14:58:35 +02005749 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005750 psa_key_type_t key_type = key_type_arg;
5751 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005752 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005753
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005754 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005755
Gilles Peskine8817f612018-12-18 00:18:46 +01005756 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005757
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005758 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005759 psa_set_key_algorithm( &attributes, alg );
5760 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005761
Gilles Peskine049c7532019-05-15 20:22:09 +02005762 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005763 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005764
Ronald Cron5425a212020-08-04 14:58:35 +02005765 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005766 hash_data->x, hash_data->len,
5767 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005768
itayzafrir5c753392018-05-08 11:18:38 +03005769exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005770 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005771 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005772 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005773}
5774/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005775
5776/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005777void verify_hash_fail( int key_type_arg, data_t *key_data,
5778 int alg_arg, data_t *hash_data,
5779 data_t *signature_data,
5780 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005781{
Ronald Cron5425a212020-08-04 14:58:35 +02005782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005783 psa_key_type_t key_type = key_type_arg;
5784 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005785 psa_status_t actual_status;
5786 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005788
Gilles Peskine8817f612018-12-18 00:18:46 +01005789 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005790
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005791 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005792 psa_set_key_algorithm( &attributes, alg );
5793 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005794
Gilles Peskine049c7532019-05-15 20:22:09 +02005795 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005796 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005797
Ronald Cron5425a212020-08-04 14:58:35 +02005798 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005799 hash_data->x, hash_data->len,
5800 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005801 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005802
5803exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005804 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005805 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005806 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005807}
5808/* END_CASE */
5809
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005810/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005811void sign_message_deterministic( int key_type_arg,
5812 data_t *key_data,
5813 int alg_arg,
5814 data_t *input_data,
5815 data_t *output_data )
5816{
5817 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5818 psa_key_type_t key_type = key_type_arg;
5819 psa_algorithm_t alg = alg_arg;
5820 size_t key_bits;
5821 unsigned char *signature = NULL;
5822 size_t signature_size;
5823 size_t signature_length = 0xdeadbeef;
5824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5825
5826 PSA_ASSERT( psa_crypto_init( ) );
5827
5828 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5829 psa_set_key_algorithm( &attributes, alg );
5830 psa_set_key_type( &attributes, key_type );
5831
5832 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5833 &key ) );
5834 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5835 key_bits = psa_get_key_bits( &attributes );
5836
5837 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5838 TEST_ASSERT( signature_size != 0 );
5839 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5840 ASSERT_ALLOC( signature, signature_size );
5841
5842 PSA_ASSERT( psa_sign_message( key, alg,
5843 input_data->x, input_data->len,
5844 signature, signature_size,
5845 &signature_length ) );
5846
5847 ASSERT_COMPARE( output_data->x, output_data->len,
5848 signature, signature_length );
5849
5850exit:
5851 psa_reset_key_attributes( &attributes );
5852
5853 psa_destroy_key( key );
5854 mbedtls_free( signature );
5855 PSA_DONE( );
5856
5857}
5858/* END_CASE */
5859
5860/* BEGIN_CASE */
5861void sign_message_fail( int key_type_arg,
5862 data_t *key_data,
5863 int alg_arg,
5864 data_t *input_data,
5865 int signature_size_arg,
5866 int expected_status_arg )
5867{
5868 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5869 psa_key_type_t key_type = key_type_arg;
5870 psa_algorithm_t alg = alg_arg;
5871 size_t signature_size = signature_size_arg;
5872 psa_status_t actual_status;
5873 psa_status_t expected_status = expected_status_arg;
5874 unsigned char *signature = NULL;
5875 size_t signature_length = 0xdeadbeef;
5876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5877
5878 ASSERT_ALLOC( signature, signature_size );
5879
5880 PSA_ASSERT( psa_crypto_init( ) );
5881
5882 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5883 psa_set_key_algorithm( &attributes, alg );
5884 psa_set_key_type( &attributes, key_type );
5885
5886 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5887 &key ) );
5888
5889 actual_status = psa_sign_message( key, alg,
5890 input_data->x, input_data->len,
5891 signature, signature_size,
5892 &signature_length );
5893 TEST_EQUAL( actual_status, expected_status );
5894 /* The value of *signature_length is unspecified on error, but
5895 * whatever it is, it should be less than signature_size, so that
5896 * if the caller tries to read *signature_length bytes without
5897 * checking the error code then they don't overflow a buffer. */
5898 TEST_ASSERT( signature_length <= signature_size );
5899
5900exit:
5901 psa_reset_key_attributes( &attributes );
5902 psa_destroy_key( key );
5903 mbedtls_free( signature );
5904 PSA_DONE( );
5905}
5906/* END_CASE */
5907
5908/* BEGIN_CASE */
5909void sign_verify_message( int key_type_arg,
5910 data_t *key_data,
5911 int alg_arg,
5912 data_t *input_data )
5913{
5914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5915 psa_key_type_t key_type = key_type_arg;
5916 psa_algorithm_t alg = alg_arg;
5917 size_t key_bits;
5918 unsigned char *signature = NULL;
5919 size_t signature_size;
5920 size_t signature_length = 0xdeadbeef;
5921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5922
5923 PSA_ASSERT( psa_crypto_init( ) );
5924
5925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5926 PSA_KEY_USAGE_VERIFY_MESSAGE );
5927 psa_set_key_algorithm( &attributes, alg );
5928 psa_set_key_type( &attributes, key_type );
5929
5930 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5931 &key ) );
5932 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5933 key_bits = psa_get_key_bits( &attributes );
5934
5935 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5936 TEST_ASSERT( signature_size != 0 );
5937 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5938 ASSERT_ALLOC( signature, signature_size );
5939
5940 PSA_ASSERT( psa_sign_message( key, alg,
5941 input_data->x, input_data->len,
5942 signature, signature_size,
5943 &signature_length ) );
5944 TEST_ASSERT( signature_length <= signature_size );
5945 TEST_ASSERT( signature_length > 0 );
5946
5947 PSA_ASSERT( psa_verify_message( key, alg,
5948 input_data->x, input_data->len,
5949 signature, signature_length ) );
5950
5951 if( input_data->len != 0 )
5952 {
5953 /* Flip a bit in the input and verify that the signature is now
5954 * detected as invalid. Flip a bit at the beginning, not at the end,
5955 * because ECDSA may ignore the last few bits of the input. */
5956 input_data->x[0] ^= 1;
5957 TEST_EQUAL( psa_verify_message( key, alg,
5958 input_data->x, input_data->len,
5959 signature, signature_length ),
5960 PSA_ERROR_INVALID_SIGNATURE );
5961 }
5962
5963exit:
5964 psa_reset_key_attributes( &attributes );
5965
5966 psa_destroy_key( key );
5967 mbedtls_free( signature );
5968 PSA_DONE( );
5969}
5970/* END_CASE */
5971
5972/* BEGIN_CASE */
5973void verify_message( int key_type_arg,
5974 data_t *key_data,
5975 int alg_arg,
5976 data_t *input_data,
5977 data_t *signature_data )
5978{
5979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5980 psa_key_type_t key_type = key_type_arg;
5981 psa_algorithm_t alg = alg_arg;
5982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5983
5984 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5985
5986 PSA_ASSERT( psa_crypto_init( ) );
5987
5988 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5989 psa_set_key_algorithm( &attributes, alg );
5990 psa_set_key_type( &attributes, key_type );
5991
5992 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5993 &key ) );
5994
5995 PSA_ASSERT( psa_verify_message( key, alg,
5996 input_data->x, input_data->len,
5997 signature_data->x, signature_data->len ) );
5998
5999exit:
6000 psa_reset_key_attributes( &attributes );
6001 psa_destroy_key( key );
6002 PSA_DONE( );
6003}
6004/* END_CASE */
6005
6006/* BEGIN_CASE */
6007void verify_message_fail( int key_type_arg,
6008 data_t *key_data,
6009 int alg_arg,
6010 data_t *hash_data,
6011 data_t *signature_data,
6012 int expected_status_arg )
6013{
6014 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6015 psa_key_type_t key_type = key_type_arg;
6016 psa_algorithm_t alg = alg_arg;
6017 psa_status_t actual_status;
6018 psa_status_t expected_status = expected_status_arg;
6019 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6020
6021 PSA_ASSERT( psa_crypto_init( ) );
6022
6023 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6024 psa_set_key_algorithm( &attributes, alg );
6025 psa_set_key_type( &attributes, key_type );
6026
6027 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6028 &key ) );
6029
6030 actual_status = psa_verify_message( key, alg,
6031 hash_data->x, hash_data->len,
6032 signature_data->x,
6033 signature_data->len );
6034 TEST_EQUAL( actual_status, expected_status );
6035
6036exit:
6037 psa_reset_key_attributes( &attributes );
6038 psa_destroy_key( key );
6039 PSA_DONE( );
6040}
6041/* END_CASE */
6042
6043/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006044void asymmetric_encrypt( int key_type_arg,
6045 data_t *key_data,
6046 int alg_arg,
6047 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006048 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006049 int expected_output_length_arg,
6050 int expected_status_arg )
6051{
Ronald Cron5425a212020-08-04 14:58:35 +02006052 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006053 psa_key_type_t key_type = key_type_arg;
6054 psa_algorithm_t alg = alg_arg;
6055 size_t expected_output_length = expected_output_length_arg;
6056 size_t key_bits;
6057 unsigned char *output = NULL;
6058 size_t output_size;
6059 size_t output_length = ~0;
6060 psa_status_t actual_status;
6061 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006063
Gilles Peskine8817f612018-12-18 00:18:46 +01006064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006065
Gilles Peskine656896e2018-06-29 19:12:28 +02006066 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006067 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6068 psa_set_key_algorithm( &attributes, alg );
6069 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006070 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006071 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006072
6073 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006074 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006075 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006076
Gilles Peskine656896e2018-06-29 19:12:28 +02006077 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006078 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006079 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006080
6081 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006082 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006083 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006084 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006085 output, output_size,
6086 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006087 TEST_EQUAL( actual_status, expected_status );
6088 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006089
Gilles Peskine68428122018-06-30 18:42:41 +02006090 /* If the label is empty, the test framework puts a non-null pointer
6091 * in label->x. Test that a null pointer works as well. */
6092 if( label->len == 0 )
6093 {
6094 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006095 if( output_size != 0 )
6096 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006097 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006098 input_data->x, input_data->len,
6099 NULL, label->len,
6100 output, output_size,
6101 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006102 TEST_EQUAL( actual_status, expected_status );
6103 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006104 }
6105
Gilles Peskine656896e2018-06-29 19:12:28 +02006106exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006107 /*
6108 * Key attributes may have been returned by psa_get_key_attributes()
6109 * thus reset them as required.
6110 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006111 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006112
Ronald Cron5425a212020-08-04 14:58:35 +02006113 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006114 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006115 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006116}
6117/* END_CASE */
6118
6119/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006120void asymmetric_encrypt_decrypt( int key_type_arg,
6121 data_t *key_data,
6122 int alg_arg,
6123 data_t *input_data,
6124 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006125{
Ronald Cron5425a212020-08-04 14:58:35 +02006126 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006127 psa_key_type_t key_type = key_type_arg;
6128 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006129 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006130 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006131 size_t output_size;
6132 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006133 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006134 size_t output2_size;
6135 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006137
Gilles Peskine8817f612018-12-18 00:18:46 +01006138 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006139
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006140 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6141 psa_set_key_algorithm( &attributes, alg );
6142 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006143
Gilles Peskine049c7532019-05-15 20:22:09 +02006144 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006145 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006146
6147 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006148 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006149 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006150
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006151 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006152 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006153 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006154
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006155 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006156 TEST_ASSERT( output2_size <=
6157 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6158 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006159 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006160
Gilles Peskineeebd7382018-06-08 18:11:54 +02006161 /* We test encryption by checking that encrypt-then-decrypt gives back
6162 * the original plaintext because of the non-optional random
6163 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006164 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006165 input_data->x, input_data->len,
6166 label->x, label->len,
6167 output, output_size,
6168 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006169 /* We don't know what ciphertext length to expect, but check that
6170 * it looks sensible. */
6171 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006172
Ronald Cron5425a212020-08-04 14:58:35 +02006173 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006174 output, output_length,
6175 label->x, label->len,
6176 output2, output2_size,
6177 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006178 ASSERT_COMPARE( input_data->x, input_data->len,
6179 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006180
6181exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006182 /*
6183 * Key attributes may have been returned by psa_get_key_attributes()
6184 * thus reset them as required.
6185 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006186 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006187
Ronald Cron5425a212020-08-04 14:58:35 +02006188 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006189 mbedtls_free( output );
6190 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006191 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006192}
6193/* END_CASE */
6194
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006195/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006196void asymmetric_decrypt( int key_type_arg,
6197 data_t *key_data,
6198 int alg_arg,
6199 data_t *input_data,
6200 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006201 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006202{
Ronald Cron5425a212020-08-04 14:58:35 +02006203 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006204 psa_key_type_t key_type = key_type_arg;
6205 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006206 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006207 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006208 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006209 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006210 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006211
Gilles Peskine8817f612018-12-18 00:18:46 +01006212 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006213
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006214 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6215 psa_set_key_algorithm( &attributes, alg );
6216 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006217
Gilles Peskine049c7532019-05-15 20:22:09 +02006218 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006219 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006220
gabor-mezei-armceface22021-01-21 12:26:17 +01006221 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6222 key_bits = psa_get_key_bits( &attributes );
6223
6224 /* Determine the maximum ciphertext length */
6225 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6226 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6227 ASSERT_ALLOC( output, output_size );
6228
Ronald Cron5425a212020-08-04 14:58:35 +02006229 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006230 input_data->x, input_data->len,
6231 label->x, label->len,
6232 output,
6233 output_size,
6234 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006235 ASSERT_COMPARE( expected_data->x, expected_data->len,
6236 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006237
Gilles Peskine68428122018-06-30 18:42:41 +02006238 /* If the label is empty, the test framework puts a non-null pointer
6239 * in label->x. Test that a null pointer works as well. */
6240 if( label->len == 0 )
6241 {
6242 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006243 if( output_size != 0 )
6244 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006245 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006246 input_data->x, input_data->len,
6247 NULL, label->len,
6248 output,
6249 output_size,
6250 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006251 ASSERT_COMPARE( expected_data->x, expected_data->len,
6252 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006253 }
6254
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006255exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006256 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006257 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006258 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006259 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006260}
6261/* END_CASE */
6262
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006263/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006264void asymmetric_decrypt_fail( int key_type_arg,
6265 data_t *key_data,
6266 int alg_arg,
6267 data_t *input_data,
6268 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006269 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006270 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006271{
Ronald Cron5425a212020-08-04 14:58:35 +02006272 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006273 psa_key_type_t key_type = key_type_arg;
6274 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006275 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006276 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006277 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006278 psa_status_t actual_status;
6279 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006281
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006282 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006283
Gilles Peskine8817f612018-12-18 00:18:46 +01006284 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006285
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006286 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6287 psa_set_key_algorithm( &attributes, alg );
6288 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006289
Gilles Peskine049c7532019-05-15 20:22:09 +02006290 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006291 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006292
Ronald Cron5425a212020-08-04 14:58:35 +02006293 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006294 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006295 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006296 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006297 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006298 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006299 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006300
Gilles Peskine68428122018-06-30 18:42:41 +02006301 /* If the label is empty, the test framework puts a non-null pointer
6302 * in label->x. Test that a null pointer works as well. */
6303 if( label->len == 0 )
6304 {
6305 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006306 if( output_size != 0 )
6307 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006308 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006309 input_data->x, input_data->len,
6310 NULL, label->len,
6311 output, output_size,
6312 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006313 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006314 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006315 }
6316
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006317exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006318 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006319 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006320 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006321 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006322}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006323/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006324
6325/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006326void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006327{
6328 /* Test each valid way of initializing the object, except for `= {0}`, as
6329 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6330 * though it's OK by the C standard. We could test for this, but we'd need
6331 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006332 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006333 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6334 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6335 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006336
6337 memset( &zero, 0, sizeof( zero ) );
6338
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006339 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006340 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006341 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006342 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006343 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006344 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006345 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006346
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006347 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006348 PSA_ASSERT( psa_key_derivation_abort(&func) );
6349 PSA_ASSERT( psa_key_derivation_abort(&init) );
6350 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006351}
6352/* END_CASE */
6353
Janos Follath16de4a42019-06-13 16:32:24 +01006354/* BEGIN_CASE */
6355void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006356{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006357 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006358 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006359 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006360
Gilles Peskine8817f612018-12-18 00:18:46 +01006361 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006362
Janos Follath16de4a42019-06-13 16:32:24 +01006363 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006364 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006365
6366exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006367 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006368 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006369}
6370/* END_CASE */
6371
Janos Follathaf3c2a02019-06-12 12:34:34 +01006372/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006373void derive_set_capacity( int alg_arg, int capacity_arg,
6374 int expected_status_arg )
6375{
6376 psa_algorithm_t alg = alg_arg;
6377 size_t capacity = capacity_arg;
6378 psa_status_t expected_status = expected_status_arg;
6379 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6380
6381 PSA_ASSERT( psa_crypto_init( ) );
6382
6383 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6384
6385 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6386 expected_status );
6387
6388exit:
6389 psa_key_derivation_abort( &operation );
6390 PSA_DONE( );
6391}
6392/* END_CASE */
6393
6394/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006395void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006396 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006397 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006398 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006399 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006400 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006401 int expected_status_arg3,
6402 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006403{
6404 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006405 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6406 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006407 psa_status_t expected_statuses[] = {expected_status_arg1,
6408 expected_status_arg2,
6409 expected_status_arg3};
6410 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006411 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6412 MBEDTLS_SVC_KEY_ID_INIT,
6413 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006414 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6416 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006417 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006418 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006419 psa_status_t expected_output_status = expected_output_status_arg;
6420 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006421
6422 PSA_ASSERT( psa_crypto_init( ) );
6423
6424 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6425 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006426
6427 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6428
6429 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6430 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006431 mbedtls_test_set_step( i );
6432 if( steps[i] == 0 )
6433 {
6434 /* Skip this step */
6435 }
6436 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006437 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006438 psa_set_key_type( &attributes, key_types[i] );
6439 PSA_ASSERT( psa_import_key( &attributes,
6440 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006441 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006442 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6443 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6444 {
6445 // When taking a private key as secret input, use key agreement
6446 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006447 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6448 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006449 expected_statuses[i] );
6450 }
6451 else
6452 {
6453 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006454 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006455 expected_statuses[i] );
6456 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006457 }
6458 else
6459 {
6460 TEST_EQUAL( psa_key_derivation_input_bytes(
6461 &operation, steps[i],
6462 inputs[i]->x, inputs[i]->len ),
6463 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006464 }
6465 }
6466
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006467 if( output_key_type != PSA_KEY_TYPE_NONE )
6468 {
6469 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006470 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006471 psa_set_key_bits( &attributes, 8 );
6472 actual_output_status =
6473 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006474 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006475 }
6476 else
6477 {
6478 uint8_t buffer[1];
6479 actual_output_status =
6480 psa_key_derivation_output_bytes( &operation,
6481 buffer, sizeof( buffer ) );
6482 }
6483 TEST_EQUAL( actual_output_status, expected_output_status );
6484
Janos Follathaf3c2a02019-06-12 12:34:34 +01006485exit:
6486 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006487 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6488 psa_destroy_key( keys[i] );
6489 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006490 PSA_DONE( );
6491}
6492/* END_CASE */
6493
Janos Follathd958bb72019-07-03 15:02:16 +01006494/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006495void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006496{
Janos Follathd958bb72019-07-03 15:02:16 +01006497 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006499 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006500 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006501 unsigned char input1[] = "Input 1";
6502 size_t input1_length = sizeof( input1 );
6503 unsigned char input2[] = "Input 2";
6504 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006505 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006506 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006507 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6508 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6509 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006511
Gilles Peskine8817f612018-12-18 00:18:46 +01006512 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006513
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006514 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6515 psa_set_key_algorithm( &attributes, alg );
6516 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006517
Gilles Peskine73676cb2019-05-15 20:15:10 +02006518 PSA_ASSERT( psa_import_key( &attributes,
6519 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006520 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006521
6522 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006523 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6524 input1, input1_length,
6525 input2, input2_length,
6526 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006527 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006528
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006529 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006530 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006531 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006532
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006533 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006534
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006535 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006536 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006537
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006538exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006539 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006540 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006541 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006542}
6543/* END_CASE */
6544
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006545/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006546void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006547{
6548 uint8_t output_buffer[16];
6549 size_t buffer_size = 16;
6550 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006551 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006552
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006553 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6554 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006555 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006556
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006557 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006558 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006559
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006560 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006561
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006562 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6563 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006564 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006565
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006566 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006567 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006568
6569exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006570 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006571}
6572/* END_CASE */
6573
6574/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006575void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006576 int step1_arg, data_t *input1,
6577 int step2_arg, data_t *input2,
6578 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006579 int requested_capacity_arg,
6580 data_t *expected_output1,
6581 data_t *expected_output2 )
6582{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006583 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006584 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6585 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006586 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6587 MBEDTLS_SVC_KEY_ID_INIT,
6588 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006589 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006590 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006591 uint8_t *expected_outputs[2] =
6592 {expected_output1->x, expected_output2->x};
6593 size_t output_sizes[2] =
6594 {expected_output1->len, expected_output2->len};
6595 size_t output_buffer_size = 0;
6596 uint8_t *output_buffer = NULL;
6597 size_t expected_capacity;
6598 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006600 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006601 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006602
6603 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6604 {
6605 if( output_sizes[i] > output_buffer_size )
6606 output_buffer_size = output_sizes[i];
6607 if( output_sizes[i] == 0 )
6608 expected_outputs[i] = NULL;
6609 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006610 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006611 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006612
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006613 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6614 psa_set_key_algorithm( &attributes, alg );
6615 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006616
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006617 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006618 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6619 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6620 requested_capacity ) );
6621 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006622 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006623 switch( steps[i] )
6624 {
6625 case 0:
6626 break;
6627 case PSA_KEY_DERIVATION_INPUT_SECRET:
6628 PSA_ASSERT( psa_import_key( &attributes,
6629 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006630 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006631
6632 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6633 {
6634 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6635 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6636 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6637 }
6638
Gilles Peskine1468da72019-05-29 17:35:49 +02006639 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006640 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006641 break;
6642 default:
6643 PSA_ASSERT( psa_key_derivation_input_bytes(
6644 &operation, steps[i],
6645 inputs[i]->x, inputs[i]->len ) );
6646 break;
6647 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006648 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006649
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006650 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006651 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006652 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006653 expected_capacity = requested_capacity;
6654
6655 /* Expansion phase. */
6656 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6657 {
6658 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006659 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006660 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006661 if( expected_capacity == 0 && output_sizes[i] == 0 )
6662 {
6663 /* Reading 0 bytes when 0 bytes are available can go either way. */
6664 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006665 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006666 continue;
6667 }
6668 else if( expected_capacity == 0 ||
6669 output_sizes[i] > expected_capacity )
6670 {
6671 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006672 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006673 expected_capacity = 0;
6674 continue;
6675 }
6676 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006677 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006678 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006679 ASSERT_COMPARE( output_buffer, output_sizes[i],
6680 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006681 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006682 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006683 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006684 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006685 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006686 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006687 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006688
6689exit:
6690 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006691 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006692 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6693 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006694 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006695}
6696/* END_CASE */
6697
6698/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006699void derive_full( int alg_arg,
6700 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006701 data_t *input1,
6702 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006703 int requested_capacity_arg )
6704{
Ronald Cron5425a212020-08-04 14:58:35 +02006705 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006706 psa_algorithm_t alg = alg_arg;
6707 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006708 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006709 unsigned char output_buffer[16];
6710 size_t expected_capacity = requested_capacity;
6711 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006713
Gilles Peskine8817f612018-12-18 00:18:46 +01006714 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006715
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006716 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6717 psa_set_key_algorithm( &attributes, alg );
6718 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006719
Gilles Peskine049c7532019-05-15 20:22:09 +02006720 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006721 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006722
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006723 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6724 input1->x, input1->len,
6725 input2->x, input2->len,
6726 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006727 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006728
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006729 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006730 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006731 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006732
6733 /* Expansion phase. */
6734 while( current_capacity > 0 )
6735 {
6736 size_t read_size = sizeof( output_buffer );
6737 if( read_size > current_capacity )
6738 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006739 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006740 output_buffer,
6741 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006742 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006743 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006744 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006745 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006746 }
6747
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006748 /* Check that the operation refuses to go over capacity. */
6749 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006750 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006751
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006752 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006753
6754exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006755 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006756 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006757 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006758}
6759/* END_CASE */
6760
Janos Follathe60c9052019-07-03 13:51:30 +01006761/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006762void derive_key_exercise( int alg_arg,
6763 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006764 data_t *input1,
6765 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006766 int derived_type_arg,
6767 int derived_bits_arg,
6768 int derived_usage_arg,
6769 int derived_alg_arg )
6770{
Ronald Cron5425a212020-08-04 14:58:35 +02006771 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6772 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006773 psa_algorithm_t alg = alg_arg;
6774 psa_key_type_t derived_type = derived_type_arg;
6775 size_t derived_bits = derived_bits_arg;
6776 psa_key_usage_t derived_usage = derived_usage_arg;
6777 psa_algorithm_t derived_alg = derived_alg_arg;
6778 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006779 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006780 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006781 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006782
Gilles Peskine8817f612018-12-18 00:18:46 +01006783 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006784
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006785 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6786 psa_set_key_algorithm( &attributes, alg );
6787 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006788 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006789 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006790
6791 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006792 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6793 input1->x, input1->len,
6794 input2->x, input2->len,
6795 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006796 goto exit;
6797
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006798 psa_set_key_usage_flags( &attributes, derived_usage );
6799 psa_set_key_algorithm( &attributes, derived_alg );
6800 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006801 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006802 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006803 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006804
6805 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006806 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006807 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6808 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006809
6810 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006811 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006812 goto exit;
6813
6814exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006815 /*
6816 * Key attributes may have been returned by psa_get_key_attributes()
6817 * thus reset them as required.
6818 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006819 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006820
6821 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006822 psa_destroy_key( base_key );
6823 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006824 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006825}
6826/* END_CASE */
6827
Janos Follath42fd8882019-07-03 14:17:09 +01006828/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006829void derive_key_export( int alg_arg,
6830 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006831 data_t *input1,
6832 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006833 int bytes1_arg,
6834 int bytes2_arg )
6835{
Ronald Cron5425a212020-08-04 14:58:35 +02006836 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6837 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006838 psa_algorithm_t alg = alg_arg;
6839 size_t bytes1 = bytes1_arg;
6840 size_t bytes2 = bytes2_arg;
6841 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006842 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006843 uint8_t *output_buffer = NULL;
6844 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006845 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6846 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006847 size_t length;
6848
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006849 ASSERT_ALLOC( output_buffer, capacity );
6850 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006851 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006852
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006853 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6854 psa_set_key_algorithm( &base_attributes, alg );
6855 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006856 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006857 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006858
6859 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006860 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6861 input1->x, input1->len,
6862 input2->x, input2->len,
6863 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006864 goto exit;
6865
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006866 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006867 output_buffer,
6868 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006869 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006870
6871 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006872 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6873 input1->x, input1->len,
6874 input2->x, input2->len,
6875 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006876 goto exit;
6877
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006878 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6879 psa_set_key_algorithm( &derived_attributes, 0 );
6880 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006881 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006882 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006883 &derived_key ) );
6884 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006885 export_buffer, bytes1,
6886 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006887 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006888 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006889 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006890 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006891 &derived_key ) );
6892 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006893 export_buffer + bytes1, bytes2,
6894 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006895 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006896
6897 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006898 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6899 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006900
6901exit:
6902 mbedtls_free( output_buffer );
6903 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006904 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006905 psa_destroy_key( base_key );
6906 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006907 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006908}
6909/* END_CASE */
6910
6911/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006912void derive_key( int alg_arg,
6913 data_t *key_data, data_t *input1, data_t *input2,
6914 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006915 int expected_status_arg,
6916 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006917{
Ronald Cron5425a212020-08-04 14:58:35 +02006918 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6919 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006920 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006921 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006922 size_t bits = bits_arg;
6923 psa_status_t expected_status = expected_status_arg;
6924 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6925 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6926 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6927
6928 PSA_ASSERT( psa_crypto_init( ) );
6929
6930 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6931 psa_set_key_algorithm( &base_attributes, alg );
6932 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6933 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006934 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006935
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006936 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6937 input1->x, input1->len,
6938 input2->x, input2->len,
6939 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006940 goto exit;
6941
6942 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6943 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006944 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006945 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006946
6947 psa_status_t status =
6948 psa_key_derivation_output_key( &derived_attributes,
6949 &operation,
6950 &derived_key );
6951 if( is_large_output > 0 )
6952 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6953 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006954
6955exit:
6956 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006957 psa_destroy_key( base_key );
6958 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006959 PSA_DONE( );
6960}
6961/* END_CASE */
6962
6963/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006964void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006965 int our_key_type_arg, int our_key_alg_arg,
6966 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006967 int expected_status_arg )
6968{
Ronald Cron5425a212020-08-04 14:58:35 +02006969 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006970 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006971 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006972 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006973 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006975 psa_status_t expected_status = expected_status_arg;
6976 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006977
Gilles Peskine8817f612018-12-18 00:18:46 +01006978 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006979
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006980 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006981 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006982 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006983 PSA_ASSERT( psa_import_key( &attributes,
6984 our_key_data->x, our_key_data->len,
6985 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006986
Gilles Peskine77f40d82019-04-11 21:27:06 +02006987 /* The tests currently include inputs that should fail at either step.
6988 * Test cases that fail at the setup step should be changed to call
6989 * key_derivation_setup instead, and this function should be renamed
6990 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006991 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006992 if( status == PSA_SUCCESS )
6993 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006994 TEST_EQUAL( psa_key_derivation_key_agreement(
6995 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6996 our_key,
6997 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006998 expected_status );
6999 }
7000 else
7001 {
7002 TEST_ASSERT( status == expected_status );
7003 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007004
7005exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007006 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007007 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007008 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007009}
7010/* END_CASE */
7011
7012/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007013void raw_key_agreement( int alg_arg,
7014 int our_key_type_arg, data_t *our_key_data,
7015 data_t *peer_key_data,
7016 data_t *expected_output )
7017{
Ronald Cron5425a212020-08-04 14:58:35 +02007018 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007019 psa_algorithm_t alg = alg_arg;
7020 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007022 unsigned char *output = NULL;
7023 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007024 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007025
7026 ASSERT_ALLOC( output, expected_output->len );
7027 PSA_ASSERT( psa_crypto_init( ) );
7028
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007029 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7030 psa_set_key_algorithm( &attributes, alg );
7031 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007032 PSA_ASSERT( psa_import_key( &attributes,
7033 our_key_data->x, our_key_data->len,
7034 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007035
gabor-mezei-armceface22021-01-21 12:26:17 +01007036 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7037 key_bits = psa_get_key_bits( &attributes );
7038
Gilles Peskinebe697d82019-05-16 18:00:41 +02007039 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7040 peer_key_data->x, peer_key_data->len,
7041 output, expected_output->len,
7042 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007043 ASSERT_COMPARE( output, output_length,
7044 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01007045 TEST_ASSERT( output_length <=
7046 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7047 TEST_ASSERT( output_length <=
7048 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007049
7050exit:
7051 mbedtls_free( output );
7052 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007053 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007054}
7055/* END_CASE */
7056
7057/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007058void key_agreement_capacity( int alg_arg,
7059 int our_key_type_arg, data_t *our_key_data,
7060 data_t *peer_key_data,
7061 int expected_capacity_arg )
7062{
Ronald Cron5425a212020-08-04 14:58:35 +02007063 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007064 psa_algorithm_t alg = alg_arg;
7065 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007066 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007068 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007069 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007070
Gilles Peskine8817f612018-12-18 00:18:46 +01007071 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007072
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007073 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7074 psa_set_key_algorithm( &attributes, alg );
7075 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007076 PSA_ASSERT( psa_import_key( &attributes,
7077 our_key_data->x, our_key_data->len,
7078 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007079
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007080 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007081 PSA_ASSERT( psa_key_derivation_key_agreement(
7082 &operation,
7083 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7084 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007085 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7086 {
7087 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007088 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007089 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007090 NULL, 0 ) );
7091 }
Gilles Peskine59685592018-09-18 12:11:34 +02007092
Gilles Peskinebf491972018-10-25 22:36:12 +02007093 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007094 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007095 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007096 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007097
Gilles Peskinebf491972018-10-25 22:36:12 +02007098 /* Test the actual capacity by reading the output. */
7099 while( actual_capacity > sizeof( output ) )
7100 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007101 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007102 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007103 actual_capacity -= sizeof( output );
7104 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007105 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007106 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007107 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007108 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007109
Gilles Peskine59685592018-09-18 12:11:34 +02007110exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007111 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007112 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007113 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007114}
7115/* END_CASE */
7116
7117/* BEGIN_CASE */
7118void key_agreement_output( int alg_arg,
7119 int our_key_type_arg, data_t *our_key_data,
7120 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007121 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007122{
Ronald Cron5425a212020-08-04 14:58:35 +02007123 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007124 psa_algorithm_t alg = alg_arg;
7125 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007126 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007127 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007128 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007129
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007130 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7131 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007132
Gilles Peskine8817f612018-12-18 00:18:46 +01007133 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007134
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007135 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7136 psa_set_key_algorithm( &attributes, alg );
7137 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007138 PSA_ASSERT( psa_import_key( &attributes,
7139 our_key_data->x, our_key_data->len,
7140 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007141
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007142 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007143 PSA_ASSERT( psa_key_derivation_key_agreement(
7144 &operation,
7145 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7146 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007147 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7148 {
7149 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007150 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007151 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007152 NULL, 0 ) );
7153 }
Gilles Peskine59685592018-09-18 12:11:34 +02007154
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007155 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007156 actual_output,
7157 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007158 ASSERT_COMPARE( actual_output, expected_output1->len,
7159 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007160 if( expected_output2->len != 0 )
7161 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007162 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007163 actual_output,
7164 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007165 ASSERT_COMPARE( actual_output, expected_output2->len,
7166 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007167 }
Gilles Peskine59685592018-09-18 12:11:34 +02007168
7169exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007170 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007171 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007172 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007173 mbedtls_free( actual_output );
7174}
7175/* END_CASE */
7176
7177/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007178void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007179{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007180 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007181 unsigned char *output = NULL;
7182 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007183 size_t i;
7184 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007185
Simon Butcher49f8e312020-03-03 15:51:50 +00007186 TEST_ASSERT( bytes_arg >= 0 );
7187
Gilles Peskine91892022021-02-08 19:50:26 +01007188 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007189 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007190
Gilles Peskine8817f612018-12-18 00:18:46 +01007191 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007192
Gilles Peskinea50d7392018-06-21 10:22:13 +02007193 /* Run several times, to ensure that every output byte will be
7194 * nonzero at least once with overwhelming probability
7195 * (2^(-8*number_of_runs)). */
7196 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007197 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007198 if( bytes != 0 )
7199 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007200 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007201
Gilles Peskinea50d7392018-06-21 10:22:13 +02007202 for( i = 0; i < bytes; i++ )
7203 {
7204 if( output[i] != 0 )
7205 ++changed[i];
7206 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007207 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007208
7209 /* Check that every byte was changed to nonzero at least once. This
7210 * validates that psa_generate_random is overwriting every byte of
7211 * the output buffer. */
7212 for( i = 0; i < bytes; i++ )
7213 {
7214 TEST_ASSERT( changed[i] != 0 );
7215 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007216
7217exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007218 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007219 mbedtls_free( output );
7220 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007221}
7222/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007223
7224/* BEGIN_CASE */
7225void generate_key( int type_arg,
7226 int bits_arg,
7227 int usage_arg,
7228 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007229 int expected_status_arg,
7230 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007231{
Ronald Cron5425a212020-08-04 14:58:35 +02007232 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007233 psa_key_type_t type = type_arg;
7234 psa_key_usage_t usage = usage_arg;
7235 size_t bits = bits_arg;
7236 psa_algorithm_t alg = alg_arg;
7237 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007239 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007240
Gilles Peskine8817f612018-12-18 00:18:46 +01007241 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007242
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007243 psa_set_key_usage_flags( &attributes, usage );
7244 psa_set_key_algorithm( &attributes, alg );
7245 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007246 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007247
7248 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007249 psa_status_t status = psa_generate_key( &attributes, &key );
7250
7251 if( is_large_key > 0 )
7252 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7253 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007254 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007255 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007256
7257 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007258 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007259 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7260 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007261
Gilles Peskine818ca122018-06-20 18:16:48 +02007262 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007263 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007264 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007265
7266exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007267 /*
7268 * Key attributes may have been returned by psa_get_key_attributes()
7269 * thus reset them as required.
7270 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007271 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007272
Ronald Cron5425a212020-08-04 14:58:35 +02007273 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007274 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007275}
7276/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007277
Ronald Cronee414c72021-03-18 18:50:08 +01007278/* 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 +02007279void generate_key_rsa( int bits_arg,
7280 data_t *e_arg,
7281 int expected_status_arg )
7282{
Ronald Cron5425a212020-08-04 14:58:35 +02007283 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007284 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007285 size_t bits = bits_arg;
7286 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7287 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7288 psa_status_t expected_status = expected_status_arg;
7289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7290 uint8_t *exported = NULL;
7291 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007292 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007293 size_t exported_length = SIZE_MAX;
7294 uint8_t *e_read_buffer = NULL;
7295 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007296 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007297 size_t e_read_length = SIZE_MAX;
7298
7299 if( e_arg->len == 0 ||
7300 ( e_arg->len == 3 &&
7301 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7302 {
7303 is_default_public_exponent = 1;
7304 e_read_size = 0;
7305 }
7306 ASSERT_ALLOC( e_read_buffer, e_read_size );
7307 ASSERT_ALLOC( exported, exported_size );
7308
7309 PSA_ASSERT( psa_crypto_init( ) );
7310
7311 psa_set_key_usage_flags( &attributes, usage );
7312 psa_set_key_algorithm( &attributes, alg );
7313 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7314 e_arg->x, e_arg->len ) );
7315 psa_set_key_bits( &attributes, bits );
7316
7317 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007318 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007319 if( expected_status != PSA_SUCCESS )
7320 goto exit;
7321
7322 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007323 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007324 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7325 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7326 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7327 e_read_buffer, e_read_size,
7328 &e_read_length ) );
7329 if( is_default_public_exponent )
7330 TEST_EQUAL( e_read_length, 0 );
7331 else
7332 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7333
7334 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007335 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007336 goto exit;
7337
7338 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007339 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007340 exported, exported_size,
7341 &exported_length ) );
7342 {
7343 uint8_t *p = exported;
7344 uint8_t *end = exported + exported_length;
7345 size_t len;
7346 /* RSAPublicKey ::= SEQUENCE {
7347 * modulus INTEGER, -- n
7348 * publicExponent INTEGER } -- e
7349 */
7350 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007351 MBEDTLS_ASN1_SEQUENCE |
7352 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007353 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007354 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7355 MBEDTLS_ASN1_INTEGER ) );
7356 if( len >= 1 && p[0] == 0 )
7357 {
7358 ++p;
7359 --len;
7360 }
7361 if( e_arg->len == 0 )
7362 {
7363 TEST_EQUAL( len, 3 );
7364 TEST_EQUAL( p[0], 1 );
7365 TEST_EQUAL( p[1], 0 );
7366 TEST_EQUAL( p[2], 1 );
7367 }
7368 else
7369 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7370 }
7371
7372exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007373 /*
7374 * Key attributes may have been returned by psa_get_key_attributes() or
7375 * set by psa_set_key_domain_parameters() thus reset them as required.
7376 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007377 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007378
Ronald Cron5425a212020-08-04 14:58:35 +02007379 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007380 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007381 mbedtls_free( e_read_buffer );
7382 mbedtls_free( exported );
7383}
7384/* END_CASE */
7385
Darryl Greend49a4992018-06-18 17:27:26 +01007386/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007387void persistent_key_load_key_from_storage( data_t *data,
7388 int type_arg, int bits_arg,
7389 int usage_flags_arg, int alg_arg,
7390 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007391{
Ronald Cron71016a92020-08-28 19:01:50 +02007392 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007394 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7395 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007396 psa_key_type_t type = type_arg;
7397 size_t bits = bits_arg;
7398 psa_key_usage_t usage_flags = usage_flags_arg;
7399 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007400 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007401 unsigned char *first_export = NULL;
7402 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007403 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007404 size_t first_exported_length;
7405 size_t second_exported_length;
7406
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007407 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7408 {
7409 ASSERT_ALLOC( first_export, export_size );
7410 ASSERT_ALLOC( second_export, export_size );
7411 }
Darryl Greend49a4992018-06-18 17:27:26 +01007412
Gilles Peskine8817f612018-12-18 00:18:46 +01007413 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007414
Gilles Peskinec87af662019-05-15 16:12:22 +02007415 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007416 psa_set_key_usage_flags( &attributes, usage_flags );
7417 psa_set_key_algorithm( &attributes, alg );
7418 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007419 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007420
Darryl Green0c6575a2018-11-07 16:05:30 +00007421 switch( generation_method )
7422 {
7423 case IMPORT_KEY:
7424 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007425 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007426 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007427 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007428
Darryl Green0c6575a2018-11-07 16:05:30 +00007429 case GENERATE_KEY:
7430 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007431 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007432 break;
7433
7434 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007435#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007436 {
7437 /* Create base key */
7438 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7439 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7440 psa_set_key_usage_flags( &base_attributes,
7441 PSA_KEY_USAGE_DERIVE );
7442 psa_set_key_algorithm( &base_attributes, derive_alg );
7443 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007444 PSA_ASSERT( psa_import_key( &base_attributes,
7445 data->x, data->len,
7446 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007447 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007448 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007449 PSA_ASSERT( psa_key_derivation_input_key(
7450 &operation,
7451 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007452 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007453 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007454 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007455 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7456 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007457 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007458 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007459 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007460 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007461 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007462#else
7463 TEST_ASSUME( ! "KDF not supported in this configuration" );
7464#endif
7465 break;
7466
7467 default:
7468 TEST_ASSERT( ! "generation_method not implemented in test" );
7469 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007470 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007471 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007472
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007473 /* Export the key if permitted by the key policy. */
7474 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7475 {
Ronald Cron5425a212020-08-04 14:58:35 +02007476 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007477 first_export, export_size,
7478 &first_exported_length ) );
7479 if( generation_method == IMPORT_KEY )
7480 ASSERT_COMPARE( data->x, data->len,
7481 first_export, first_exported_length );
7482 }
Darryl Greend49a4992018-06-18 17:27:26 +01007483
7484 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007485 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007486 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007487 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007488
Darryl Greend49a4992018-06-18 17:27:26 +01007489 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007490 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007491 TEST_ASSERT( mbedtls_svc_key_id_equal(
7492 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007493 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7494 PSA_KEY_LIFETIME_PERSISTENT );
7495 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7496 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007497 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007498 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007499 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007500
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007501 /* Export the key again if permitted by the key policy. */
7502 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007503 {
Ronald Cron5425a212020-08-04 14:58:35 +02007504 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007505 second_export, export_size,
7506 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007507 ASSERT_COMPARE( first_export, first_exported_length,
7508 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007509 }
7510
7511 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007512 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007513 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007514
7515exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007516 /*
7517 * Key attributes may have been returned by psa_get_key_attributes()
7518 * thus reset them as required.
7519 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007520 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007521
Darryl Greend49a4992018-06-18 17:27:26 +01007522 mbedtls_free( first_export );
7523 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007524 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007525 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007526 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007527 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007528}
7529/* END_CASE */