blob: 79235d718538e4102f6a0c5a061675d1f21c649a [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
1158 /* Verify correct MAC, one-shot case. */
1159 status = psa_mac_verify( key, exercise_alg, input, 128,
1160 mac, mac_len );
1161
1162 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1163 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001165 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001166
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001168
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001170 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001171 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
1173exit:
1174 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001176 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177}
1178/* END_CASE */
1179
1180/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001181void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182 int policy_alg,
1183 int key_type,
1184 data_t *key_data,
1185 int exercise_alg )
1186{
Ronald Cron5425a212020-08-04 14:58:35 +02001187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001189 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001190 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191 psa_status_t status;
1192
Gilles Peskine8817f612018-12-18 00:18:46 +01001193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001195 psa_set_key_usage_flags( &attributes, policy_usage );
1196 psa_set_key_algorithm( &attributes, policy_alg );
1197 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001198
Gilles Peskine049c7532019-05-15 20:22:09 +02001199 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001200 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001202 /* Check if no key usage flag implication is done */
1203 TEST_EQUAL( policy_usage,
1204 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001205
Ronald Cron5425a212020-08-04 14:58:35 +02001206 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207 if( policy_alg == exercise_alg &&
1208 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001209 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001211 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001212 psa_cipher_abort( &operation );
1213
Ronald Cron5425a212020-08-04 14:58:35 +02001214 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001215 if( policy_alg == exercise_alg &&
1216 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001217 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001219 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220
1221exit:
1222 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001223 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001224 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225}
1226/* END_CASE */
1227
1228/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001229void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 int policy_alg,
1231 int key_type,
1232 data_t *key_data,
1233 int nonce_length_arg,
1234 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001235 int exercise_alg,
1236 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001237{
Ronald Cron5425a212020-08-04 14:58:35 +02001238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001240 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001241 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001242 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001243 unsigned char nonce[16] = {0};
1244 size_t nonce_length = nonce_length_arg;
1245 unsigned char tag[16];
1246 size_t tag_length = tag_length_arg;
1247 size_t output_length;
1248
1249 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1250 TEST_ASSERT( tag_length <= sizeof( tag ) );
1251
Gilles Peskine8817f612018-12-18 00:18:46 +01001252 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001253
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001254 psa_set_key_usage_flags( &attributes, policy_usage );
1255 psa_set_key_algorithm( &attributes, policy_alg );
1256 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001257
Gilles Peskine049c7532019-05-15 20:22:09 +02001258 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001259 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001260
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001261 /* Check if no key usage implication is done */
1262 TEST_EQUAL( policy_usage,
1263 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001264
Ronald Cron5425a212020-08-04 14:58:35 +02001265 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266 nonce, nonce_length,
1267 NULL, 0,
1268 NULL, 0,
1269 tag, tag_length,
1270 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001271 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1272 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001273 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001274 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001275
1276 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001277 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001278 nonce, nonce_length,
1279 NULL, 0,
1280 tag, tag_length,
1281 NULL, 0,
1282 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001283 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1284 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1285 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001286 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001288 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001289
1290exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001291 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001292 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001293}
1294/* END_CASE */
1295
1296/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001297void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001298 int policy_alg,
1299 int key_type,
1300 data_t *key_data,
1301 int exercise_alg )
1302{
Ronald Cron5425a212020-08-04 14:58:35 +02001303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001305 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001306 psa_status_t status;
1307 size_t key_bits;
1308 size_t buffer_length;
1309 unsigned char *buffer = NULL;
1310 size_t output_length;
1311
Gilles Peskine8817f612018-12-18 00:18:46 +01001312 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001313
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001314 psa_set_key_usage_flags( &attributes, policy_usage );
1315 psa_set_key_algorithm( &attributes, policy_alg );
1316 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317
Gilles Peskine049c7532019-05-15 20:22:09 +02001318 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001319 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001321 /* Check if no key usage implication is done */
1322 TEST_EQUAL( policy_usage,
1323 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001324
Ronald Cron5425a212020-08-04 14:58:35 +02001325 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001326 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001327 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1328 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001329 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001330
Ronald Cron5425a212020-08-04 14:58:35 +02001331 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332 NULL, 0,
1333 NULL, 0,
1334 buffer, buffer_length,
1335 &output_length );
1336 if( policy_alg == exercise_alg &&
1337 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001338 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001340 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001341
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001342 if( buffer_length != 0 )
1343 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001344 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001345 buffer, buffer_length,
1346 NULL, 0,
1347 buffer, buffer_length,
1348 &output_length );
1349 if( policy_alg == exercise_alg &&
1350 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001351 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001352 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001353 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354
1355exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001356 /*
1357 * Key attributes may have been returned by psa_get_key_attributes()
1358 * thus reset them as required.
1359 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001360 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001361
1362 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001363 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364 mbedtls_free( buffer );
1365}
1366/* END_CASE */
1367
1368/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001369void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001370 int policy_alg,
1371 int key_type,
1372 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001373 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001374 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001375 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001376{
Ronald Cron5425a212020-08-04 14:58:35 +02001377 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001379 psa_key_usage_t policy_usage = policy_usage_arg;
1380 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001381 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001382 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1383 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1384 * compatible with the policy and `payload_length_arg` is supposed to be
1385 * a valid input length to sign. If `payload_length_arg <= 0`,
1386 * `exercise_alg` is supposed to be forbidden by the policy. */
1387 int compatible_alg = payload_length_arg > 0;
1388 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001389 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001390 size_t signature_length;
1391
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001392 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001393 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001394 TEST_EQUAL( expected_usage,
1395 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001396
Gilles Peskine8817f612018-12-18 00:18:46 +01001397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001398
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001399 psa_set_key_usage_flags( &attributes, policy_usage );
1400 psa_set_key_algorithm( &attributes, policy_alg );
1401 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001402
Gilles Peskine049c7532019-05-15 20:22:09 +02001403 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001404 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001405
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001406 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1407
Ronald Cron5425a212020-08-04 14:58:35 +02001408 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001409 payload, payload_length,
1410 signature, sizeof( signature ),
1411 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001412 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001413 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001414 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001415 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001416
1417 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001418 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001419 payload, payload_length,
1420 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001421 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001422 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001423 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001424 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001425
Gilles Peskinef7b41372021-09-22 16:15:05 +02001426 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001427 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001428 {
1429 status = psa_sign_message( key, exercise_alg,
1430 payload, payload_length,
1431 signature, sizeof( signature ),
1432 &signature_length );
1433 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1434 PSA_ASSERT( status );
1435 else
1436 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1437
1438 memset( signature, 0, sizeof( signature ) );
1439 status = psa_verify_message( key, exercise_alg,
1440 payload, payload_length,
1441 signature, sizeof( signature ) );
1442 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1443 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1444 else
1445 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1446 }
1447
Gilles Peskined5b33222018-06-18 22:20:03 +02001448exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001449 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001450 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001451}
1452/* END_CASE */
1453
Janos Follathba3fab92019-06-11 14:50:16 +01001454/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001455void derive_key_policy( int policy_usage,
1456 int policy_alg,
1457 int key_type,
1458 data_t *key_data,
1459 int exercise_alg )
1460{
Ronald Cron5425a212020-08-04 14:58:35 +02001461 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001463 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001464 psa_status_t status;
1465
Gilles Peskine8817f612018-12-18 00:18:46 +01001466 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001467
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001468 psa_set_key_usage_flags( &attributes, policy_usage );
1469 psa_set_key_algorithm( &attributes, policy_alg );
1470 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001471
Gilles Peskine049c7532019-05-15 20:22:09 +02001472 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001473 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001474
Janos Follathba3fab92019-06-11 14:50:16 +01001475 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1476
1477 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1478 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001479 {
Janos Follathba3fab92019-06-11 14:50:16 +01001480 PSA_ASSERT( psa_key_derivation_input_bytes(
1481 &operation,
1482 PSA_KEY_DERIVATION_INPUT_SEED,
1483 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001484 }
Janos Follathba3fab92019-06-11 14:50:16 +01001485
1486 status = psa_key_derivation_input_key( &operation,
1487 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001488 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001489
Gilles Peskineea0fb492018-07-12 17:17:20 +02001490 if( policy_alg == exercise_alg &&
1491 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001492 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001493 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001494 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001495
1496exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001497 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001498 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001499 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001500}
1501/* END_CASE */
1502
1503/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001504void agreement_key_policy( int policy_usage,
1505 int policy_alg,
1506 int key_type_arg,
1507 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001508 int exercise_alg,
1509 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001510{
Ronald Cron5425a212020-08-04 14:58:35 +02001511 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001513 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001514 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001515 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001516 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001517
Gilles Peskine8817f612018-12-18 00:18:46 +01001518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001519
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001520 psa_set_key_usage_flags( &attributes, policy_usage );
1521 psa_set_key_algorithm( &attributes, policy_alg );
1522 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001523
Gilles Peskine049c7532019-05-15 20:22:09 +02001524 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001525 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001526
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001527 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001528 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001529
Steven Cooremance48e852020-10-05 16:02:45 +02001530 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001531
1532exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001533 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001534 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001535 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001536}
1537/* END_CASE */
1538
1539/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001540void key_policy_alg2( int key_type_arg, data_t *key_data,
1541 int usage_arg, int alg_arg, int alg2_arg )
1542{
Ronald Cron5425a212020-08-04 14:58:35 +02001543 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001544 psa_key_type_t key_type = key_type_arg;
1545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1546 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1547 psa_key_usage_t usage = usage_arg;
1548 psa_algorithm_t alg = alg_arg;
1549 psa_algorithm_t alg2 = alg2_arg;
1550
1551 PSA_ASSERT( psa_crypto_init( ) );
1552
1553 psa_set_key_usage_flags( &attributes, usage );
1554 psa_set_key_algorithm( &attributes, alg );
1555 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1556 psa_set_key_type( &attributes, key_type );
1557 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001558 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001559
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001560 /* Update the usage flags to obtain implicit usage flags */
1561 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001562 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001563 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1564 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1565 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1566
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001567 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001568 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001569 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001570 goto exit;
1571
1572exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001573 /*
1574 * Key attributes may have been returned by psa_get_key_attributes()
1575 * thus reset them as required.
1576 */
1577 psa_reset_key_attributes( &got_attributes );
1578
Ronald Cron5425a212020-08-04 14:58:35 +02001579 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001580 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001581}
1582/* END_CASE */
1583
1584/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001585void raw_agreement_key_policy( int policy_usage,
1586 int policy_alg,
1587 int key_type_arg,
1588 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001589 int exercise_alg,
1590 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001591{
Ronald Cron5425a212020-08-04 14:58:35 +02001592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001594 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001595 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001596 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001597 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001598
1599 PSA_ASSERT( psa_crypto_init( ) );
1600
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001601 psa_set_key_usage_flags( &attributes, policy_usage );
1602 psa_set_key_algorithm( &attributes, policy_alg );
1603 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001604
Gilles Peskine049c7532019-05-15 20:22:09 +02001605 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001606 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001607
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001608 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001609
Steven Cooremance48e852020-10-05 16:02:45 +02001610 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001611
1612exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001613 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001614 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001615 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001616}
1617/* END_CASE */
1618
1619/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001620void copy_success( int source_usage_arg,
1621 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301622 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001623 int type_arg, data_t *material,
1624 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001625 int target_usage_arg,
1626 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301627 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001628 int expected_usage_arg,
1629 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001630{
Gilles Peskineca25db92019-04-19 11:43:08 +02001631 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1632 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001633 psa_key_usage_t expected_usage = expected_usage_arg;
1634 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001635 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301636 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1637 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001638 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1639 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001640 uint8_t *export_buffer = NULL;
1641
Gilles Peskine57ab7212019-01-28 13:03:09 +01001642 PSA_ASSERT( psa_crypto_init( ) );
1643
Gilles Peskineca25db92019-04-19 11:43:08 +02001644 /* Prepare the source key. */
1645 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1646 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001647 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001648 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301649 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001650 PSA_ASSERT( psa_import_key( &source_attributes,
1651 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001652 &source_key ) );
1653 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001654
Gilles Peskineca25db92019-04-19 11:43:08 +02001655 /* Prepare the target attributes. */
1656 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001657 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001658 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001659 }
Archana8a180362021-07-05 02:18:48 +05301660 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001661
Gilles Peskineca25db92019-04-19 11:43:08 +02001662 if( target_usage_arg != -1 )
1663 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1664 if( target_alg_arg != -1 )
1665 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001666 if( target_alg2_arg != -1 )
1667 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001668
Archana8a180362021-07-05 02:18:48 +05301669
Gilles Peskine57ab7212019-01-28 13:03:09 +01001670 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001671 PSA_ASSERT( psa_copy_key( source_key,
1672 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001673
1674 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001675 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001676
1677 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001678 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001679 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1680 psa_get_key_type( &target_attributes ) );
1681 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1682 psa_get_key_bits( &target_attributes ) );
1683 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1684 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001685 TEST_EQUAL( expected_alg2,
1686 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001687 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1688 {
1689 size_t length;
1690 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001691 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001692 material->len, &length ) );
1693 ASSERT_COMPARE( material->x, material->len,
1694 export_buffer, length );
1695 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001696
Archana8a180362021-07-05 02:18:48 +05301697 if( !psa_key_lifetime_is_external( target_lifetime ) )
1698 {
1699 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1700 goto exit;
1701 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1702 goto exit;
1703 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001704
Ronald Cron5425a212020-08-04 14:58:35 +02001705 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001706
1707exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001708 /*
1709 * Source and target key attributes may have been returned by
1710 * psa_get_key_attributes() thus reset them as required.
1711 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001712 psa_reset_key_attributes( &source_attributes );
1713 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001714
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001715 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001716 mbedtls_free( export_buffer );
1717}
1718/* END_CASE */
1719
1720/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001721void copy_fail( int source_usage_arg,
1722 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301723 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001724 int type_arg, data_t *material,
1725 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001726 int target_usage_arg,
1727 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001728 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001729 int expected_status_arg )
1730{
1731 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1732 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001733 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1734 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001735 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001736
1737 PSA_ASSERT( psa_crypto_init( ) );
1738
1739 /* Prepare the source key. */
1740 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1741 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001742 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001743 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301744 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001745 PSA_ASSERT( psa_import_key( &source_attributes,
1746 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001747 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001748
1749 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001750 psa_set_key_id( &target_attributes, key_id );
1751 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001752 psa_set_key_type( &target_attributes, target_type_arg );
1753 psa_set_key_bits( &target_attributes, target_bits_arg );
1754 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1755 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001756 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001757
1758 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001759 TEST_EQUAL( psa_copy_key( source_key,
1760 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001761 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001762
Ronald Cron5425a212020-08-04 14:58:35 +02001763 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001764
Gilles Peskine4a644642019-05-03 17:14:08 +02001765exit:
1766 psa_reset_key_attributes( &source_attributes );
1767 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001768 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001769}
1770/* END_CASE */
1771
1772/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001773void hash_operation_init( )
1774{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001775 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001776 /* Test each valid way of initializing the object, except for `= {0}`, as
1777 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1778 * though it's OK by the C standard. We could test for this, but we'd need
1779 * to supress the Clang warning for the test. */
1780 psa_hash_operation_t func = psa_hash_operation_init( );
1781 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1782 psa_hash_operation_t zero;
1783
1784 memset( &zero, 0, sizeof( zero ) );
1785
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001786 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001787 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1788 PSA_ERROR_BAD_STATE );
1789 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1790 PSA_ERROR_BAD_STATE );
1791 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1792 PSA_ERROR_BAD_STATE );
1793
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001794 /* A default hash operation should be abortable without error. */
1795 PSA_ASSERT( psa_hash_abort( &func ) );
1796 PSA_ASSERT( psa_hash_abort( &init ) );
1797 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001798}
1799/* END_CASE */
1800
1801/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001802void hash_setup( int alg_arg,
1803 int expected_status_arg )
1804{
1805 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01001806 uint8_t *output = NULL;
1807 size_t output_size = 0;
1808 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001809 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001810 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001811 psa_status_t status;
1812
Gilles Peskine8817f612018-12-18 00:18:46 +01001813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001814
Neil Armstrongedb20862022-02-07 15:47:44 +01001815 /* Hash Setup, one-shot */
1816 output_size = PSA_HASH_LENGTH(alg);
1817 ASSERT_ALLOC( output, output_size );
1818
1819 status = psa_hash_compute( alg, NULL, 0,
1820 output, output_size, &output_length );
1821 TEST_EQUAL( status, expected_status );
1822
1823 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001824 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001825 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001826
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001827 /* Whether setup succeeded or failed, abort must succeed. */
1828 PSA_ASSERT( psa_hash_abort( &operation ) );
1829
1830 /* If setup failed, reproduce the failure, so as to
1831 * test the resulting state of the operation object. */
1832 if( status != PSA_SUCCESS )
1833 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1834
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001835 /* Now the operation object should be reusable. */
1836#if defined(KNOWN_SUPPORTED_HASH_ALG)
1837 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1838 PSA_ASSERT( psa_hash_abort( &operation ) );
1839#endif
1840
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001841exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01001842 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001843 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001844}
1845/* END_CASE */
1846
1847/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001848void hash_compute_fail( int alg_arg, data_t *input,
1849 int output_size_arg, int expected_status_arg )
1850{
1851 psa_algorithm_t alg = alg_arg;
1852 uint8_t *output = NULL;
1853 size_t output_size = output_size_arg;
1854 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001855 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01001856 psa_status_t expected_status = expected_status_arg;
1857 psa_status_t status;
1858
1859 ASSERT_ALLOC( output, output_size );
1860
1861 PSA_ASSERT( psa_crypto_init( ) );
1862
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001863 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001864 status = psa_hash_compute( alg, input->x, input->len,
1865 output, output_size, &output_length );
1866 TEST_EQUAL( status, expected_status );
1867 TEST_ASSERT( output_length <= output_size );
1868
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001869 /* Hash Compute, multi-part */
1870 status = psa_hash_setup( &operation, alg );
1871 if( status == PSA_SUCCESS )
1872 {
1873 status = psa_hash_update( &operation, input->x, input->len );
1874 if( status == PSA_SUCCESS )
1875 {
1876 status = psa_hash_finish( &operation, output, output_size,
1877 &output_length );
1878 if( status == PSA_SUCCESS )
1879 TEST_ASSERT( output_length <= output_size );
1880 else
1881 TEST_EQUAL( status, expected_status );
1882 }
1883 else
1884 {
1885 TEST_EQUAL( status, expected_status );
1886 }
1887 }
1888 else
1889 {
1890 TEST_EQUAL( status, expected_status );
1891 }
1892
Gilles Peskine0a749c82019-11-28 19:33:58 +01001893exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001894 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001895 mbedtls_free( output );
1896 PSA_DONE( );
1897}
1898/* END_CASE */
1899
1900/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001901void hash_compare_fail( int alg_arg, data_t *input,
1902 data_t *reference_hash,
1903 int expected_status_arg )
1904{
1905 psa_algorithm_t alg = alg_arg;
1906 psa_status_t expected_status = expected_status_arg;
1907 psa_status_t status;
1908
1909 PSA_ASSERT( psa_crypto_init( ) );
1910
1911 status = psa_hash_compare( alg, input->x, input->len,
1912 reference_hash->x, reference_hash->len );
1913 TEST_EQUAL( status, expected_status );
1914
1915exit:
1916 PSA_DONE( );
1917}
1918/* END_CASE */
1919
1920/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001921void hash_compute_compare( int alg_arg, data_t *input,
1922 data_t *expected_output )
1923{
1924 psa_algorithm_t alg = alg_arg;
1925 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1926 size_t output_length = INVALID_EXPORT_LENGTH;
1927 size_t i;
1928
1929 PSA_ASSERT( psa_crypto_init( ) );
1930
1931 /* Compute with tight buffer */
1932 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001933 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001934 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001935 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001936 ASSERT_COMPARE( output, output_length,
1937 expected_output->x, expected_output->len );
1938
1939 /* Compute with larger buffer */
1940 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1941 output, sizeof( output ),
1942 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001943 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001944 ASSERT_COMPARE( output, output_length,
1945 expected_output->x, expected_output->len );
1946
1947 /* Compare with correct hash */
1948 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1949 output, output_length ) );
1950
1951 /* Compare with trailing garbage */
1952 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1953 output, output_length + 1 ),
1954 PSA_ERROR_INVALID_SIGNATURE );
1955
1956 /* Compare with truncated hash */
1957 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1958 output, output_length - 1 ),
1959 PSA_ERROR_INVALID_SIGNATURE );
1960
1961 /* Compare with corrupted value */
1962 for( i = 0; i < output_length; i++ )
1963 {
Chris Jones9634bb12021-01-20 15:56:42 +00001964 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001965 output[i] ^= 1;
1966 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1967 output, output_length ),
1968 PSA_ERROR_INVALID_SIGNATURE );
1969 output[i] ^= 1;
1970 }
1971
1972exit:
1973 PSA_DONE( );
1974}
1975/* END_CASE */
1976
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001977/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001978void hash_bad_order( )
1979{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001980 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001981 unsigned char input[] = "";
1982 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001983 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001984 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1985 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1986 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001987 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001988 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001989 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001990
Gilles Peskine8817f612018-12-18 00:18:46 +01001991 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001992
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001993 /* Call setup twice in a row. */
1994 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001995 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001996 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1997 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001998 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001999 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002000 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002001
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002002 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002003 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002004 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002005 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002006
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002007 /* Check that update calls abort on error. */
2008 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002009 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002010 ASSERT_OPERATION_IS_ACTIVE( operation );
2011 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2012 PSA_ERROR_BAD_STATE );
2013 ASSERT_OPERATION_IS_INACTIVE( operation );
2014 PSA_ASSERT( psa_hash_abort( &operation ) );
2015 ASSERT_OPERATION_IS_INACTIVE( operation );
2016
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002017 /* Call update after finish. */
2018 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2019 PSA_ASSERT( psa_hash_finish( &operation,
2020 hash, sizeof( hash ), &hash_len ) );
2021 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002022 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002023 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002024
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002025 /* Call verify without calling setup beforehand. */
2026 TEST_EQUAL( psa_hash_verify( &operation,
2027 valid_hash, sizeof( valid_hash ) ),
2028 PSA_ERROR_BAD_STATE );
2029 PSA_ASSERT( psa_hash_abort( &operation ) );
2030
2031 /* Call verify after finish. */
2032 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2033 PSA_ASSERT( psa_hash_finish( &operation,
2034 hash, sizeof( hash ), &hash_len ) );
2035 TEST_EQUAL( psa_hash_verify( &operation,
2036 valid_hash, sizeof( valid_hash ) ),
2037 PSA_ERROR_BAD_STATE );
2038 PSA_ASSERT( psa_hash_abort( &operation ) );
2039
2040 /* Call verify twice in a row. */
2041 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002042 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002043 PSA_ASSERT( psa_hash_verify( &operation,
2044 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002045 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002046 TEST_EQUAL( psa_hash_verify( &operation,
2047 valid_hash, sizeof( valid_hash ) ),
2048 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002049 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002050 PSA_ASSERT( psa_hash_abort( &operation ) );
2051
2052 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002053 TEST_EQUAL( psa_hash_finish( &operation,
2054 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002055 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002056 PSA_ASSERT( psa_hash_abort( &operation ) );
2057
2058 /* Call finish twice in a row. */
2059 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2060 PSA_ASSERT( psa_hash_finish( &operation,
2061 hash, sizeof( hash ), &hash_len ) );
2062 TEST_EQUAL( psa_hash_finish( &operation,
2063 hash, sizeof( hash ), &hash_len ),
2064 PSA_ERROR_BAD_STATE );
2065 PSA_ASSERT( psa_hash_abort( &operation ) );
2066
2067 /* Call finish after calling verify. */
2068 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2069 PSA_ASSERT( psa_hash_verify( &operation,
2070 valid_hash, sizeof( valid_hash ) ) );
2071 TEST_EQUAL( psa_hash_finish( &operation,
2072 hash, sizeof( hash ), &hash_len ),
2073 PSA_ERROR_BAD_STATE );
2074 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002075
2076exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002077 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002078}
2079/* END_CASE */
2080
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002081/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002082void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002083{
2084 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002085 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2086 * appended to it */
2087 unsigned char hash[] = {
2088 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2089 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2090 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002091 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002092 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002093
Gilles Peskine8817f612018-12-18 00:18:46 +01002094 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002095
itayzafrir27e69452018-11-01 14:26:34 +02002096 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002097 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002098 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002099 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002100 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002101 ASSERT_OPERATION_IS_INACTIVE( operation );
2102 PSA_ASSERT( psa_hash_abort( &operation ) );
2103 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002104
itayzafrir27e69452018-11-01 14:26:34 +02002105 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002106 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002107 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002108 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002109
itayzafrir27e69452018-11-01 14:26:34 +02002110 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002111 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002112 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002113 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002114
itayzafrirec93d302018-10-18 18:01:10 +03002115exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002116 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002117}
2118/* END_CASE */
2119
Ronald Cronee414c72021-03-18 18:50:08 +01002120/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002121void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002122{
2123 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002124 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002125 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002126 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002127 size_t hash_len;
2128
Gilles Peskine8817f612018-12-18 00:18:46 +01002129 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002130
itayzafrir58028322018-10-25 10:22:01 +03002131 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002132 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002133 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002134 hash, expected_size - 1, &hash_len ),
2135 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002136
2137exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002138 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002139}
2140/* END_CASE */
2141
Ronald Cronee414c72021-03-18 18:50:08 +01002142/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002143void hash_clone_source_state( )
2144{
2145 psa_algorithm_t alg = PSA_ALG_SHA_256;
2146 unsigned char hash[PSA_HASH_MAX_SIZE];
2147 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2148 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2149 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2150 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2151 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2152 size_t hash_len;
2153
2154 PSA_ASSERT( psa_crypto_init( ) );
2155 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2156
2157 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2158 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2159 PSA_ASSERT( psa_hash_finish( &op_finished,
2160 hash, sizeof( hash ), &hash_len ) );
2161 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2162 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2163
2164 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2165 PSA_ERROR_BAD_STATE );
2166
2167 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2168 PSA_ASSERT( psa_hash_finish( &op_init,
2169 hash, sizeof( hash ), &hash_len ) );
2170 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2171 PSA_ASSERT( psa_hash_finish( &op_finished,
2172 hash, sizeof( hash ), &hash_len ) );
2173 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2174 PSA_ASSERT( psa_hash_finish( &op_aborted,
2175 hash, sizeof( hash ), &hash_len ) );
2176
2177exit:
2178 psa_hash_abort( &op_source );
2179 psa_hash_abort( &op_init );
2180 psa_hash_abort( &op_setup );
2181 psa_hash_abort( &op_finished );
2182 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002183 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002184}
2185/* END_CASE */
2186
Ronald Cronee414c72021-03-18 18:50:08 +01002187/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002188void hash_clone_target_state( )
2189{
2190 psa_algorithm_t alg = PSA_ALG_SHA_256;
2191 unsigned char hash[PSA_HASH_MAX_SIZE];
2192 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2193 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2194 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2195 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2196 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2197 size_t hash_len;
2198
2199 PSA_ASSERT( psa_crypto_init( ) );
2200
2201 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2202 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2203 PSA_ASSERT( psa_hash_finish( &op_finished,
2204 hash, sizeof( hash ), &hash_len ) );
2205 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2206 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2207
2208 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2209 PSA_ASSERT( psa_hash_finish( &op_target,
2210 hash, sizeof( hash ), &hash_len ) );
2211
2212 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2213 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2214 PSA_ERROR_BAD_STATE );
2215 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2216 PSA_ERROR_BAD_STATE );
2217
2218exit:
2219 psa_hash_abort( &op_target );
2220 psa_hash_abort( &op_init );
2221 psa_hash_abort( &op_setup );
2222 psa_hash_abort( &op_finished );
2223 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002224 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002225}
2226/* END_CASE */
2227
itayzafrir58028322018-10-25 10:22:01 +03002228/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002229void mac_operation_init( )
2230{
Jaeden Amero252ef282019-02-15 14:05:35 +00002231 const uint8_t input[1] = { 0 };
2232
Jaeden Amero769ce272019-01-04 11:48:03 +00002233 /* Test each valid way of initializing the object, except for `= {0}`, as
2234 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2235 * though it's OK by the C standard. We could test for this, but we'd need
2236 * to supress the Clang warning for the test. */
2237 psa_mac_operation_t func = psa_mac_operation_init( );
2238 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2239 psa_mac_operation_t zero;
2240
2241 memset( &zero, 0, sizeof( zero ) );
2242
Jaeden Amero252ef282019-02-15 14:05:35 +00002243 /* A freshly-initialized MAC operation should not be usable. */
2244 TEST_EQUAL( psa_mac_update( &func,
2245 input, sizeof( input ) ),
2246 PSA_ERROR_BAD_STATE );
2247 TEST_EQUAL( psa_mac_update( &init,
2248 input, sizeof( input ) ),
2249 PSA_ERROR_BAD_STATE );
2250 TEST_EQUAL( psa_mac_update( &zero,
2251 input, sizeof( input ) ),
2252 PSA_ERROR_BAD_STATE );
2253
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002254 /* A default MAC operation should be abortable without error. */
2255 PSA_ASSERT( psa_mac_abort( &func ) );
2256 PSA_ASSERT( psa_mac_abort( &init ) );
2257 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002258}
2259/* END_CASE */
2260
2261/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002262void mac_setup( int key_type_arg,
2263 data_t *key,
2264 int alg_arg,
2265 int expected_status_arg )
2266{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002267 psa_key_type_t key_type = key_type_arg;
2268 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002269 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002270 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002271 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2272#if defined(KNOWN_SUPPORTED_MAC_ALG)
2273 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2274#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002275
Gilles Peskine8817f612018-12-18 00:18:46 +01002276 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002277
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002278 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2279 &operation, &status ) )
2280 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002281 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002282
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002283 /* The operation object should be reusable. */
2284#if defined(KNOWN_SUPPORTED_MAC_ALG)
2285 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2286 smoke_test_key_data,
2287 sizeof( smoke_test_key_data ),
2288 KNOWN_SUPPORTED_MAC_ALG,
2289 &operation, &status ) )
2290 goto exit;
2291 TEST_EQUAL( status, PSA_SUCCESS );
2292#endif
2293
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002294exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002295 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002296}
2297/* END_CASE */
2298
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002299/* 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 +00002300void mac_bad_order( )
2301{
Ronald Cron5425a212020-08-04 14:58:35 +02002302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002303 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2304 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002305 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002306 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2307 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2308 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002309 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002310 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2311 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2312 size_t sign_mac_length = 0;
2313 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2314 const uint8_t verify_mac[] = {
2315 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2316 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2317 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2318
2319 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002320 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002321 psa_set_key_algorithm( &attributes, alg );
2322 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002323
Ronald Cron5425a212020-08-04 14:58:35 +02002324 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2325 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002326
Jaeden Amero252ef282019-02-15 14:05:35 +00002327 /* Call update without calling setup beforehand. */
2328 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2329 PSA_ERROR_BAD_STATE );
2330 PSA_ASSERT( psa_mac_abort( &operation ) );
2331
2332 /* Call sign finish without calling setup beforehand. */
2333 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2334 &sign_mac_length),
2335 PSA_ERROR_BAD_STATE );
2336 PSA_ASSERT( psa_mac_abort( &operation ) );
2337
2338 /* Call verify finish without calling setup beforehand. */
2339 TEST_EQUAL( psa_mac_verify_finish( &operation,
2340 verify_mac, sizeof( verify_mac ) ),
2341 PSA_ERROR_BAD_STATE );
2342 PSA_ASSERT( psa_mac_abort( &operation ) );
2343
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002344 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002345 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002346 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002347 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002348 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002349 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002350 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002351 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002352
Jaeden Amero252ef282019-02-15 14:05:35 +00002353 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002354 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002355 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2356 PSA_ASSERT( psa_mac_sign_finish( &operation,
2357 sign_mac, sizeof( sign_mac ),
2358 &sign_mac_length ) );
2359 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2360 PSA_ERROR_BAD_STATE );
2361 PSA_ASSERT( psa_mac_abort( &operation ) );
2362
2363 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002364 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002365 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2366 PSA_ASSERT( psa_mac_verify_finish( &operation,
2367 verify_mac, sizeof( verify_mac ) ) );
2368 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2369 PSA_ERROR_BAD_STATE );
2370 PSA_ASSERT( psa_mac_abort( &operation ) );
2371
2372 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002373 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002374 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2375 PSA_ASSERT( psa_mac_sign_finish( &operation,
2376 sign_mac, sizeof( sign_mac ),
2377 &sign_mac_length ) );
2378 TEST_EQUAL( psa_mac_sign_finish( &operation,
2379 sign_mac, sizeof( sign_mac ),
2380 &sign_mac_length ),
2381 PSA_ERROR_BAD_STATE );
2382 PSA_ASSERT( psa_mac_abort( &operation ) );
2383
2384 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002386 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2387 PSA_ASSERT( psa_mac_verify_finish( &operation,
2388 verify_mac, sizeof( verify_mac ) ) );
2389 TEST_EQUAL( psa_mac_verify_finish( &operation,
2390 verify_mac, sizeof( verify_mac ) ),
2391 PSA_ERROR_BAD_STATE );
2392 PSA_ASSERT( psa_mac_abort( &operation ) );
2393
2394 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002395 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002396 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002397 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002398 TEST_EQUAL( psa_mac_verify_finish( &operation,
2399 verify_mac, sizeof( verify_mac ) ),
2400 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002401 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002402 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002403 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002404
2405 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002406 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002407 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002408 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002409 TEST_EQUAL( psa_mac_sign_finish( &operation,
2410 sign_mac, sizeof( sign_mac ),
2411 &sign_mac_length ),
2412 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002413 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002414 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002415 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002416
Ronald Cron5425a212020-08-04 14:58:35 +02002417 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002418
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002419exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002420 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002421}
2422/* END_CASE */
2423
2424/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002425void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002426 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002427 int alg_arg,
2428 data_t *input,
2429 data_t *expected_mac )
2430{
Ronald Cron5425a212020-08-04 14:58:35 +02002431 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002432 psa_key_type_t key_type = key_type_arg;
2433 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002434 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002436 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002437 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002438 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002439 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002440 const size_t output_sizes_to_test[] = {
2441 0,
2442 1,
2443 expected_mac->len - 1,
2444 expected_mac->len,
2445 expected_mac->len + 1,
2446 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002447
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002448 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002449 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002450 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002451
Gilles Peskine8817f612018-12-18 00:18:46 +01002452 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002453
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002454 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002455 psa_set_key_algorithm( &attributes, alg );
2456 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002457
Ronald Cron5425a212020-08-04 14:58:35 +02002458 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2459 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002460
Gilles Peskine8b356b52020-08-25 23:44:59 +02002461 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2462 {
2463 const size_t output_size = output_sizes_to_test[i];
2464 psa_status_t expected_status =
2465 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2466 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002467
Chris Jones9634bb12021-01-20 15:56:42 +00002468 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002469 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002470
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002471 /* Calculate the MAC, one-shot case. */
2472 TEST_EQUAL( psa_mac_compute( key, alg,
2473 input->x, input->len,
2474 actual_mac, output_size, &mac_length ),
2475 expected_status );
2476 if( expected_status == PSA_SUCCESS )
2477 {
2478 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2479 actual_mac, mac_length );
2480 }
2481
2482 if( output_size > 0 )
2483 memset( actual_mac, 0, output_size );
2484
2485 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002486 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002487 PSA_ASSERT( psa_mac_update( &operation,
2488 input->x, input->len ) );
2489 TEST_EQUAL( psa_mac_sign_finish( &operation,
2490 actual_mac, output_size,
2491 &mac_length ),
2492 expected_status );
2493 PSA_ASSERT( psa_mac_abort( &operation ) );
2494
2495 if( expected_status == PSA_SUCCESS )
2496 {
2497 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2498 actual_mac, mac_length );
2499 }
2500 mbedtls_free( actual_mac );
2501 actual_mac = NULL;
2502 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002503
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002504exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002505 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002506 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002507 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002508 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002509}
2510/* END_CASE */
2511
2512/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002513void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002514 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002515 int alg_arg,
2516 data_t *input,
2517 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518{
Ronald Cron5425a212020-08-04 14:58:35 +02002519 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520 psa_key_type_t key_type = key_type_arg;
2521 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002522 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002524 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525
Gilles Peskine69c12672018-06-28 00:07:19 +02002526 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2527
Gilles Peskine8817f612018-12-18 00:18:46 +01002528 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002530 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002531 psa_set_key_algorithm( &attributes, alg );
2532 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002533
Ronald Cron5425a212020-08-04 14:58:35 +02002534 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2535 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002536
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002537 /* Verify correct MAC, one-shot case. */
2538 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2539 expected_mac->x, expected_mac->len ) );
2540
2541 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002542 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002543 PSA_ASSERT( psa_mac_update( &operation,
2544 input->x, input->len ) );
2545 PSA_ASSERT( psa_mac_verify_finish( &operation,
2546 expected_mac->x,
2547 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002549 /* Test a MAC that's too short, one-shot case. */
2550 TEST_EQUAL( psa_mac_verify( key, alg,
2551 input->x, input->len,
2552 expected_mac->x,
2553 expected_mac->len - 1 ),
2554 PSA_ERROR_INVALID_SIGNATURE );
2555
2556 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002557 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002558 PSA_ASSERT( psa_mac_update( &operation,
2559 input->x, input->len ) );
2560 TEST_EQUAL( psa_mac_verify_finish( &operation,
2561 expected_mac->x,
2562 expected_mac->len - 1 ),
2563 PSA_ERROR_INVALID_SIGNATURE );
2564
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002565 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002566 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2567 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002568 TEST_EQUAL( psa_mac_verify( key, alg,
2569 input->x, input->len,
2570 perturbed_mac, expected_mac->len + 1 ),
2571 PSA_ERROR_INVALID_SIGNATURE );
2572
2573 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002574 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002575 PSA_ASSERT( psa_mac_update( &operation,
2576 input->x, input->len ) );
2577 TEST_EQUAL( psa_mac_verify_finish( &operation,
2578 perturbed_mac,
2579 expected_mac->len + 1 ),
2580 PSA_ERROR_INVALID_SIGNATURE );
2581
2582 /* Test changing one byte. */
2583 for( size_t i = 0; i < expected_mac->len; i++ )
2584 {
Chris Jones9634bb12021-01-20 15:56:42 +00002585 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002586 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002587
2588 TEST_EQUAL( psa_mac_verify( key, alg,
2589 input->x, input->len,
2590 perturbed_mac, expected_mac->len ),
2591 PSA_ERROR_INVALID_SIGNATURE );
2592
Ronald Cron5425a212020-08-04 14:58:35 +02002593 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002594 PSA_ASSERT( psa_mac_update( &operation,
2595 input->x, input->len ) );
2596 TEST_EQUAL( psa_mac_verify_finish( &operation,
2597 perturbed_mac,
2598 expected_mac->len ),
2599 PSA_ERROR_INVALID_SIGNATURE );
2600 perturbed_mac[i] ^= 1;
2601 }
2602
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002604 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002605 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002606 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002607 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608}
2609/* END_CASE */
2610
2611/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002612void cipher_operation_init( )
2613{
Jaeden Ameroab439972019-02-15 14:12:05 +00002614 const uint8_t input[1] = { 0 };
2615 unsigned char output[1] = { 0 };
2616 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002617 /* Test each valid way of initializing the object, except for `= {0}`, as
2618 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2619 * though it's OK by the C standard. We could test for this, but we'd need
2620 * to supress the Clang warning for the test. */
2621 psa_cipher_operation_t func = psa_cipher_operation_init( );
2622 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2623 psa_cipher_operation_t zero;
2624
2625 memset( &zero, 0, sizeof( zero ) );
2626
Jaeden Ameroab439972019-02-15 14:12:05 +00002627 /* A freshly-initialized cipher operation should not be usable. */
2628 TEST_EQUAL( psa_cipher_update( &func,
2629 input, sizeof( input ),
2630 output, sizeof( output ),
2631 &output_length ),
2632 PSA_ERROR_BAD_STATE );
2633 TEST_EQUAL( psa_cipher_update( &init,
2634 input, sizeof( input ),
2635 output, sizeof( output ),
2636 &output_length ),
2637 PSA_ERROR_BAD_STATE );
2638 TEST_EQUAL( psa_cipher_update( &zero,
2639 input, sizeof( input ),
2640 output, sizeof( output ),
2641 &output_length ),
2642 PSA_ERROR_BAD_STATE );
2643
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002644 /* A default cipher operation should be abortable without error. */
2645 PSA_ASSERT( psa_cipher_abort( &func ) );
2646 PSA_ASSERT( psa_cipher_abort( &init ) );
2647 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002648}
2649/* END_CASE */
2650
2651/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002652void cipher_setup( int key_type_arg,
2653 data_t *key,
2654 int alg_arg,
2655 int expected_status_arg )
2656{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002657 psa_key_type_t key_type = key_type_arg;
2658 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002659 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002660 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002661 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002662#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002663 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2664#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002665
Gilles Peskine8817f612018-12-18 00:18:46 +01002666 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002667
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002668 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2669 &operation, &status ) )
2670 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002671 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002672
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002673 /* The operation object should be reusable. */
2674#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2675 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2676 smoke_test_key_data,
2677 sizeof( smoke_test_key_data ),
2678 KNOWN_SUPPORTED_CIPHER_ALG,
2679 &operation, &status ) )
2680 goto exit;
2681 TEST_EQUAL( status, PSA_SUCCESS );
2682#endif
2683
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002684exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002685 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002686 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002687}
2688/* END_CASE */
2689
Ronald Cronee414c72021-03-18 18:50:08 +01002690/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002691void cipher_bad_order( )
2692{
Ronald Cron5425a212020-08-04 14:58:35 +02002693 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002694 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2695 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002696 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002697 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002698 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002699 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002700 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2701 0xaa, 0xaa, 0xaa, 0xaa };
2702 const uint8_t text[] = {
2703 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2704 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002705 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002706 size_t length = 0;
2707
2708 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002709 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2710 psa_set_key_algorithm( &attributes, alg );
2711 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002712 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2713 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002714
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002715 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002716 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002717 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002718 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002719 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002720 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002721 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002722 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002723
2724 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002725 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002726 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002727 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002728 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002729 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002730 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002731 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002732
Jaeden Ameroab439972019-02-15 14:12:05 +00002733 /* Generate an IV without calling setup beforehand. */
2734 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2735 buffer, sizeof( buffer ),
2736 &length ),
2737 PSA_ERROR_BAD_STATE );
2738 PSA_ASSERT( psa_cipher_abort( &operation ) );
2739
2740 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002741 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002742 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2743 buffer, sizeof( buffer ),
2744 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002745 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002746 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2747 buffer, sizeof( buffer ),
2748 &length ),
2749 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002750 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002751 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002752 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002753
2754 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002755 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002756 PSA_ASSERT( psa_cipher_set_iv( &operation,
2757 iv, sizeof( iv ) ) );
2758 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2759 buffer, sizeof( buffer ),
2760 &length ),
2761 PSA_ERROR_BAD_STATE );
2762 PSA_ASSERT( psa_cipher_abort( &operation ) );
2763
2764 /* Set an IV without calling setup beforehand. */
2765 TEST_EQUAL( psa_cipher_set_iv( &operation,
2766 iv, sizeof( iv ) ),
2767 PSA_ERROR_BAD_STATE );
2768 PSA_ASSERT( psa_cipher_abort( &operation ) );
2769
2770 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002771 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002772 PSA_ASSERT( psa_cipher_set_iv( &operation,
2773 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002774 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002775 TEST_EQUAL( psa_cipher_set_iv( &operation,
2776 iv, sizeof( iv ) ),
2777 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002778 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002779 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002780 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002781
2782 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002783 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002784 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2785 buffer, sizeof( buffer ),
2786 &length ) );
2787 TEST_EQUAL( psa_cipher_set_iv( &operation,
2788 iv, sizeof( iv ) ),
2789 PSA_ERROR_BAD_STATE );
2790 PSA_ASSERT( psa_cipher_abort( &operation ) );
2791
2792 /* Call update without calling setup beforehand. */
2793 TEST_EQUAL( psa_cipher_update( &operation,
2794 text, sizeof( text ),
2795 buffer, sizeof( buffer ),
2796 &length ),
2797 PSA_ERROR_BAD_STATE );
2798 PSA_ASSERT( psa_cipher_abort( &operation ) );
2799
2800 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002801 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002802 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002803 TEST_EQUAL( psa_cipher_update( &operation,
2804 text, sizeof( text ),
2805 buffer, sizeof( buffer ),
2806 &length ),
2807 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002808 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002809 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002810 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002811
2812 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002813 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002814 PSA_ASSERT( psa_cipher_set_iv( &operation,
2815 iv, sizeof( iv ) ) );
2816 PSA_ASSERT( psa_cipher_finish( &operation,
2817 buffer, sizeof( buffer ), &length ) );
2818 TEST_EQUAL( psa_cipher_update( &operation,
2819 text, sizeof( text ),
2820 buffer, sizeof( buffer ),
2821 &length ),
2822 PSA_ERROR_BAD_STATE );
2823 PSA_ASSERT( psa_cipher_abort( &operation ) );
2824
2825 /* Call finish without calling setup beforehand. */
2826 TEST_EQUAL( psa_cipher_finish( &operation,
2827 buffer, sizeof( buffer ), &length ),
2828 PSA_ERROR_BAD_STATE );
2829 PSA_ASSERT( psa_cipher_abort( &operation ) );
2830
2831 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002832 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002833 /* Not calling update means we are encrypting an empty buffer, which is OK
2834 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002835 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002836 TEST_EQUAL( psa_cipher_finish( &operation,
2837 buffer, sizeof( buffer ), &length ),
2838 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002839 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002840 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002841 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002842
2843 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002844 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002845 PSA_ASSERT( psa_cipher_set_iv( &operation,
2846 iv, sizeof( iv ) ) );
2847 PSA_ASSERT( psa_cipher_finish( &operation,
2848 buffer, sizeof( buffer ), &length ) );
2849 TEST_EQUAL( psa_cipher_finish( &operation,
2850 buffer, sizeof( buffer ), &length ),
2851 PSA_ERROR_BAD_STATE );
2852 PSA_ASSERT( psa_cipher_abort( &operation ) );
2853
Ronald Cron5425a212020-08-04 14:58:35 +02002854 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002855
Jaeden Ameroab439972019-02-15 14:12:05 +00002856exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002857 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002858 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002859}
2860/* END_CASE */
2861
2862/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002863void cipher_encrypt_fail( int alg_arg,
2864 int key_type_arg,
2865 data_t *key_data,
2866 data_t *input,
2867 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002868{
Ronald Cron5425a212020-08-04 14:58:35 +02002869 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002870 psa_status_t status;
2871 psa_key_type_t key_type = key_type_arg;
2872 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002873 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002874 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002876 size_t output_length = 0;
2877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2878
2879 if ( PSA_ERROR_BAD_STATE != expected_status )
2880 {
2881 PSA_ASSERT( psa_crypto_init( ) );
2882
2883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2884 psa_set_key_algorithm( &attributes, alg );
2885 psa_set_key_type( &attributes, key_type );
2886
2887 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2888 input->len );
2889 ASSERT_ALLOC( output, output_buffer_size );
2890
2891 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2892 &key ) );
2893 }
2894
2895 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2896 output_buffer_size, &output_length );
2897
2898 TEST_EQUAL( status, expected_status );
2899
2900exit:
2901 mbedtls_free( output );
2902 psa_destroy_key( key );
2903 PSA_DONE( );
2904}
2905/* END_CASE */
2906
2907/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02002908void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
2909 data_t *input, int iv_length,
2910 int expected_result )
2911{
2912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2913 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2915 size_t output_buffer_size = 0;
2916 unsigned char *output = NULL;
2917
2918 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2919 ASSERT_ALLOC( output, output_buffer_size );
2920
2921 PSA_ASSERT( psa_crypto_init( ) );
2922
2923 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2924 psa_set_key_algorithm( &attributes, alg );
2925 psa_set_key_type( &attributes, key_type );
2926
2927 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2928 &key ) );
2929 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2930 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
2931 iv_length ) );
2932
2933exit:
2934 psa_cipher_abort( &operation );
2935 mbedtls_free( output );
2936 psa_destroy_key( key );
2937 PSA_DONE( );
2938}
2939/* END_CASE */
2940
2941/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002942void cipher_encrypt_alg_without_iv( int alg_arg,
2943 int key_type_arg,
2944 data_t *key_data,
2945 data_t *input,
2946 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002947{
2948 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2949 psa_key_type_t key_type = key_type_arg;
2950 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02002951 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2952 uint8_t iv[1] = { 0x5a };
2953 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002954 unsigned char *output = NULL;
2955 size_t output_buffer_size = 0;
2956 size_t output_length = 0;
2957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2958
2959 PSA_ASSERT( psa_crypto_init( ) );
2960
2961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2962 psa_set_key_algorithm( &attributes, alg );
2963 psa_set_key_type( &attributes, key_type );
2964
2965 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2966 ASSERT_ALLOC( output, output_buffer_size );
2967
2968 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2969 &key ) );
2970
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02002971 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2972 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
2973 PSA_ERROR_BAD_STATE );
2974 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2975 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
2976 &iv_length ),
2977 PSA_ERROR_BAD_STATE );
2978
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002979 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2980 output_buffer_size, &output_length ) );
2981 TEST_ASSERT( output_length <=
2982 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2983 TEST_ASSERT( output_length <=
2984 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2985
2986 ASSERT_COMPARE( expected_output->x, expected_output->len,
2987 output, output_length );
2988exit:
2989 mbedtls_free( output );
2990 psa_destroy_key( key );
2991 PSA_DONE( );
2992}
2993/* END_CASE */
2994
2995/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01002996void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2997{
2998 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2999 psa_algorithm_t alg = alg_arg;
3000 psa_key_type_t key_type = key_type_arg;
3001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3002 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3003 psa_status_t status;
3004
3005 PSA_ASSERT( psa_crypto_init( ) );
3006
3007 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3008 psa_set_key_algorithm( &attributes, alg );
3009 psa_set_key_type( &attributes, key_type );
3010
3011 /* Usage of either of these two size macros would cause divide by zero
3012 * with incorrect key types previously. Input length should be irrelevant
3013 * here. */
3014 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3015 0 );
3016 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3017
3018
3019 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3020 &key ) );
3021
3022 /* Should fail due to invalid alg type (to support invalid key type).
3023 * Encrypt or decrypt will end up in the same place. */
3024 status = psa_cipher_encrypt_setup( &operation, key, alg );
3025
3026 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3027
3028exit:
3029 psa_cipher_abort( &operation );
3030 psa_destroy_key( key );
3031 PSA_DONE( );
3032}
3033/* END_CASE */
3034
3035/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003036void cipher_encrypt_validation( int alg_arg,
3037 int key_type_arg,
3038 data_t *key_data,
3039 data_t *input )
3040{
3041 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3042 psa_key_type_t key_type = key_type_arg;
3043 psa_algorithm_t alg = alg_arg;
3044 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3045 unsigned char *output1 = NULL;
3046 size_t output1_buffer_size = 0;
3047 size_t output1_length = 0;
3048 unsigned char *output2 = NULL;
3049 size_t output2_buffer_size = 0;
3050 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003051 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003052 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003054
Gilles Peskine8817f612018-12-18 00:18:46 +01003055 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003056
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003057 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3058 psa_set_key_algorithm( &attributes, alg );
3059 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003060
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003061 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3062 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3063 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3064 ASSERT_ALLOC( output1, output1_buffer_size );
3065 ASSERT_ALLOC( output2, output2_buffer_size );
3066
Ronald Cron5425a212020-08-04 14:58:35 +02003067 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3068 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003069
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003070 /* The one-shot cipher encryption uses generated iv so validating
3071 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003072 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3073 output1_buffer_size, &output1_length ) );
3074 TEST_ASSERT( output1_length <=
3075 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3076 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003077 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003078
3079 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3080 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003081
Gilles Peskine8817f612018-12-18 00:18:46 +01003082 PSA_ASSERT( psa_cipher_update( &operation,
3083 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003084 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003085 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003086 TEST_ASSERT( function_output_length <=
3087 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3088 TEST_ASSERT( function_output_length <=
3089 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003090 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003091
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003092 PSA_ASSERT( psa_cipher_finish( &operation,
3093 output2 + output2_length,
3094 output2_buffer_size - output2_length,
3095 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003096 TEST_ASSERT( function_output_length <=
3097 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3098 TEST_ASSERT( function_output_length <=
3099 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003100 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003101
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003102 PSA_ASSERT( psa_cipher_abort( &operation ) );
3103 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3104 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003105
Gilles Peskine50e586b2018-06-08 14:28:46 +02003106exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003107 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003108 mbedtls_free( output1 );
3109 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003110 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003111 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003112}
3113/* END_CASE */
3114
3115/* BEGIN_CASE */
3116void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003117 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003118 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003119 int first_part_size_arg,
3120 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003121 data_t *expected_output,
3122 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003123{
Ronald Cron5425a212020-08-04 14:58:35 +02003124 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003125 psa_key_type_t key_type = key_type_arg;
3126 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003127 psa_status_t status;
3128 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003129 size_t first_part_size = first_part_size_arg;
3130 size_t output1_length = output1_length_arg;
3131 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003132 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003133 size_t output_buffer_size = 0;
3134 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003135 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003136 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003137 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003138
Gilles Peskine8817f612018-12-18 00:18:46 +01003139 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003140
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3142 psa_set_key_algorithm( &attributes, alg );
3143 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003144
Ronald Cron5425a212020-08-04 14:58:35 +02003145 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3146 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003147
Ronald Cron5425a212020-08-04 14:58:35 +02003148 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003149
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003150 if( iv->len > 0 )
3151 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003152 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003153 }
3154
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003155 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3156 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003157 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003158
Gilles Peskinee0866522019-02-19 19:44:00 +01003159 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003160 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3161 output, output_buffer_size,
3162 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003163 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003164 TEST_ASSERT( function_output_length <=
3165 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3166 TEST_ASSERT( function_output_length <=
3167 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003168 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003169
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003170 if( first_part_size < input->len )
3171 {
3172 PSA_ASSERT( psa_cipher_update( &operation,
3173 input->x + first_part_size,
3174 input->len - first_part_size,
3175 ( output_buffer_size == 0 ? NULL :
3176 output + total_output_length ),
3177 output_buffer_size - total_output_length,
3178 &function_output_length ) );
3179 TEST_ASSERT( function_output_length == output2_length );
3180 TEST_ASSERT( function_output_length <=
3181 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3182 alg,
3183 input->len - first_part_size ) );
3184 TEST_ASSERT( function_output_length <=
3185 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3186 total_output_length += function_output_length;
3187 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003188
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003189 status = psa_cipher_finish( &operation,
3190 ( output_buffer_size == 0 ? NULL :
3191 output + total_output_length ),
3192 output_buffer_size - total_output_length,
3193 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003194 TEST_ASSERT( function_output_length <=
3195 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3196 TEST_ASSERT( function_output_length <=
3197 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003198 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003199 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003200
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003201 if( expected_status == PSA_SUCCESS )
3202 {
3203 PSA_ASSERT( psa_cipher_abort( &operation ) );
3204
3205 ASSERT_COMPARE( expected_output->x, expected_output->len,
3206 output, total_output_length );
3207 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003208
3209exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003210 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003211 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003212 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003213 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003214}
3215/* END_CASE */
3216
3217/* BEGIN_CASE */
3218void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003219 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003220 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003221 int first_part_size_arg,
3222 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003223 data_t *expected_output,
3224 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003225{
Ronald Cron5425a212020-08-04 14:58:35 +02003226 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003227 psa_key_type_t key_type = key_type_arg;
3228 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003229 psa_status_t status;
3230 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003231 size_t first_part_size = first_part_size_arg;
3232 size_t output1_length = output1_length_arg;
3233 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003234 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003235 size_t output_buffer_size = 0;
3236 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003237 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003238 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003240
Gilles Peskine8817f612018-12-18 00:18:46 +01003241 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003242
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003243 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3244 psa_set_key_algorithm( &attributes, alg );
3245 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003246
Ronald Cron5425a212020-08-04 14:58:35 +02003247 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3248 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003249
Ronald Cron5425a212020-08-04 14:58:35 +02003250 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003251
Steven Cooreman177deba2020-09-07 17:14:14 +02003252 if( iv->len > 0 )
3253 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003254 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003255 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003256
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003257 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3258 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003259 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003260
Gilles Peskinee0866522019-02-19 19:44:00 +01003261 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003262 PSA_ASSERT( psa_cipher_update( &operation,
3263 input->x, first_part_size,
3264 output, output_buffer_size,
3265 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003266 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003267 TEST_ASSERT( function_output_length <=
3268 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3269 TEST_ASSERT( function_output_length <=
3270 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003271 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003272
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003273 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003274 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003275 PSA_ASSERT( psa_cipher_update( &operation,
3276 input->x + first_part_size,
3277 input->len - first_part_size,
3278 ( output_buffer_size == 0 ? NULL :
3279 output + total_output_length ),
3280 output_buffer_size - total_output_length,
3281 &function_output_length ) );
3282 TEST_ASSERT( function_output_length == output2_length );
3283 TEST_ASSERT( function_output_length <=
3284 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3285 alg,
3286 input->len - first_part_size ) );
3287 TEST_ASSERT( function_output_length <=
3288 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3289 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003290 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003291
Gilles Peskine50e586b2018-06-08 14:28:46 +02003292 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003293 ( output_buffer_size == 0 ? NULL :
3294 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003295 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003296 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003297 TEST_ASSERT( function_output_length <=
3298 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3299 TEST_ASSERT( function_output_length <=
3300 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003301 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003302 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003303
3304 if( expected_status == PSA_SUCCESS )
3305 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003306 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003307
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003308 ASSERT_COMPARE( expected_output->x, expected_output->len,
3309 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003310 }
3311
Gilles Peskine50e586b2018-06-08 14:28:46 +02003312exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003313 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003314 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003315 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003316 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003317}
3318/* END_CASE */
3319
Gilles Peskine50e586b2018-06-08 14:28:46 +02003320/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003321void cipher_decrypt_fail( int alg_arg,
3322 int key_type_arg,
3323 data_t *key_data,
3324 data_t *iv,
3325 data_t *input_arg,
3326 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003327{
3328 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3329 psa_status_t status;
3330 psa_key_type_t key_type = key_type_arg;
3331 psa_algorithm_t alg = alg_arg;
3332 psa_status_t expected_status = expected_status_arg;
3333 unsigned char *input = NULL;
3334 size_t input_buffer_size = 0;
3335 unsigned char *output = NULL;
3336 size_t output_buffer_size = 0;
3337 size_t output_length = 0;
3338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3339
3340 if ( PSA_ERROR_BAD_STATE != expected_status )
3341 {
3342 PSA_ASSERT( psa_crypto_init( ) );
3343
3344 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3345 psa_set_key_algorithm( &attributes, alg );
3346 psa_set_key_type( &attributes, key_type );
3347
3348 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3349 &key ) );
3350 }
3351
3352 /* Allocate input buffer and copy the iv and the plaintext */
3353 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3354 if ( input_buffer_size > 0 )
3355 {
3356 ASSERT_ALLOC( input, input_buffer_size );
3357 memcpy( input, iv->x, iv->len );
3358 memcpy( input + iv->len, input_arg->x, input_arg->len );
3359 }
3360
3361 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3362 ASSERT_ALLOC( output, output_buffer_size );
3363
3364 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3365 output_buffer_size, &output_length );
3366 TEST_EQUAL( status, expected_status );
3367
3368exit:
3369 mbedtls_free( input );
3370 mbedtls_free( output );
3371 psa_destroy_key( key );
3372 PSA_DONE( );
3373}
3374/* END_CASE */
3375
3376/* BEGIN_CASE */
3377void cipher_decrypt( int alg_arg,
3378 int key_type_arg,
3379 data_t *key_data,
3380 data_t *iv,
3381 data_t *input_arg,
3382 data_t *expected_output )
3383{
3384 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3385 psa_key_type_t key_type = key_type_arg;
3386 psa_algorithm_t alg = alg_arg;
3387 unsigned char *input = NULL;
3388 size_t input_buffer_size = 0;
3389 unsigned char *output = NULL;
3390 size_t output_buffer_size = 0;
3391 size_t output_length = 0;
3392 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3393
3394 PSA_ASSERT( psa_crypto_init( ) );
3395
3396 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3397 psa_set_key_algorithm( &attributes, alg );
3398 psa_set_key_type( &attributes, key_type );
3399
3400 /* Allocate input buffer and copy the iv and the plaintext */
3401 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3402 if ( input_buffer_size > 0 )
3403 {
3404 ASSERT_ALLOC( input, input_buffer_size );
3405 memcpy( input, iv->x, iv->len );
3406 memcpy( input + iv->len, input_arg->x, input_arg->len );
3407 }
3408
3409 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3410 ASSERT_ALLOC( output, output_buffer_size );
3411
3412 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3413 &key ) );
3414
3415 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3416 output_buffer_size, &output_length ) );
3417 TEST_ASSERT( output_length <=
3418 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3419 TEST_ASSERT( output_length <=
3420 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3421
3422 ASSERT_COMPARE( expected_output->x, expected_output->len,
3423 output, output_length );
3424exit:
3425 mbedtls_free( input );
3426 mbedtls_free( output );
3427 psa_destroy_key( key );
3428 PSA_DONE( );
3429}
3430/* END_CASE */
3431
3432/* BEGIN_CASE */
3433void cipher_verify_output( int alg_arg,
3434 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003435 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003436 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003437{
Ronald Cron5425a212020-08-04 14:58:35 +02003438 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003439 psa_key_type_t key_type = key_type_arg;
3440 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003441 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003442 size_t output1_size = 0;
3443 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003444 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003445 size_t output2_size = 0;
3446 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003448
Gilles Peskine8817f612018-12-18 00:18:46 +01003449 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003450
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3452 psa_set_key_algorithm( &attributes, alg );
3453 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003454
Ronald Cron5425a212020-08-04 14:58:35 +02003455 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3456 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003457 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003458 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003459
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003460 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3461 output1, output1_size,
3462 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003463 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003464 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003465 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003466 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003467
3468 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003469 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003470
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003471 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3472 output2, output2_size,
3473 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003474 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003475 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003476 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003477 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003478
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003479 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003480
3481exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003482 mbedtls_free( output1 );
3483 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003484 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003485 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003486}
3487/* END_CASE */
3488
3489/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003490void cipher_verify_output_multipart( int alg_arg,
3491 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003492 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003493 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003494 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003495{
Ronald Cron5425a212020-08-04 14:58:35 +02003496 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003497 psa_key_type_t key_type = key_type_arg;
3498 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003499 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003500 unsigned char iv[16] = {0};
3501 size_t iv_size = 16;
3502 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003503 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003504 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003505 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003506 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003507 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003508 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003509 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003510 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3511 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003513
Gilles Peskine8817f612018-12-18 00:18:46 +01003514 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003515
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003516 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3517 psa_set_key_algorithm( &attributes, alg );
3518 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003519
Ronald Cron5425a212020-08-04 14:58:35 +02003520 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3521 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003522
Ronald Cron5425a212020-08-04 14:58:35 +02003523 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3524 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003525
Steven Cooreman177deba2020-09-07 17:14:14 +02003526 if( alg != PSA_ALG_ECB_NO_PADDING )
3527 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003528 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3529 iv, iv_size,
3530 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003531 }
3532
gabor-mezei-armceface22021-01-21 12:26:17 +01003533 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3534 TEST_ASSERT( output1_buffer_size <=
3535 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003536 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003537
Gilles Peskinee0866522019-02-19 19:44:00 +01003538 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003539
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3541 output1, output1_buffer_size,
3542 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003543 TEST_ASSERT( function_output_length <=
3544 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3545 TEST_ASSERT( function_output_length <=
3546 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003547 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003548
Gilles Peskine8817f612018-12-18 00:18:46 +01003549 PSA_ASSERT( psa_cipher_update( &operation1,
3550 input->x + first_part_size,
3551 input->len - first_part_size,
3552 output1, output1_buffer_size,
3553 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003554 TEST_ASSERT( function_output_length <=
3555 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3556 alg,
3557 input->len - first_part_size ) );
3558 TEST_ASSERT( function_output_length <=
3559 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003560 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003561
Gilles Peskine8817f612018-12-18 00:18:46 +01003562 PSA_ASSERT( psa_cipher_finish( &operation1,
3563 output1 + output1_length,
3564 output1_buffer_size - output1_length,
3565 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003566 TEST_ASSERT( function_output_length <=
3567 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3568 TEST_ASSERT( function_output_length <=
3569 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003570 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003571
Gilles Peskine8817f612018-12-18 00:18:46 +01003572 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003573
Gilles Peskine048b7f02018-06-08 14:20:49 +02003574 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003575 TEST_ASSERT( output2_buffer_size <=
3576 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3577 TEST_ASSERT( output2_buffer_size <=
3578 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003579 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003580
Steven Cooreman177deba2020-09-07 17:14:14 +02003581 if( iv_length > 0 )
3582 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003583 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3584 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003585 }
Moran Pekerded84402018-06-06 16:36:50 +03003586
Gilles Peskine8817f612018-12-18 00:18:46 +01003587 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3588 output2, output2_buffer_size,
3589 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003590 TEST_ASSERT( function_output_length <=
3591 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3592 TEST_ASSERT( function_output_length <=
3593 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003594 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003595
Gilles Peskine8817f612018-12-18 00:18:46 +01003596 PSA_ASSERT( psa_cipher_update( &operation2,
3597 output1 + first_part_size,
3598 output1_length - first_part_size,
3599 output2, output2_buffer_size,
3600 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003601 TEST_ASSERT( function_output_length <=
3602 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3603 alg,
3604 output1_length - first_part_size ) );
3605 TEST_ASSERT( function_output_length <=
3606 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003607 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003608
Gilles Peskine8817f612018-12-18 00:18:46 +01003609 PSA_ASSERT( psa_cipher_finish( &operation2,
3610 output2 + output2_length,
3611 output2_buffer_size - output2_length,
3612 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003613 TEST_ASSERT( function_output_length <=
3614 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3615 TEST_ASSERT( function_output_length <=
3616 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003617 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003618
Gilles Peskine8817f612018-12-18 00:18:46 +01003619 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003620
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003621 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003622
3623exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003624 psa_cipher_abort( &operation1 );
3625 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003626 mbedtls_free( output1 );
3627 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003628 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003629 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003630}
3631/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003632
Gilles Peskine20035e32018-02-03 22:44:14 +01003633/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003634void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003635 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003636 data_t *nonce,
3637 data_t *additional_data,
3638 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003639 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003640{
Ronald Cron5425a212020-08-04 14:58:35 +02003641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003642 psa_key_type_t key_type = key_type_arg;
3643 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003644 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003645 unsigned char *output_data = NULL;
3646 size_t output_size = 0;
3647 size_t output_length = 0;
3648 unsigned char *output_data2 = NULL;
3649 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003650 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003651 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003652 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003653
Gilles Peskine8817f612018-12-18 00:18:46 +01003654 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003655
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003656 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3657 psa_set_key_algorithm( &attributes, alg );
3658 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003659
Gilles Peskine049c7532019-05-15 20:22:09 +02003660 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003661 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003662 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3663 key_bits = psa_get_key_bits( &attributes );
3664
3665 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3666 alg );
3667 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3668 * should be exact. */
3669 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3670 expected_result != PSA_ERROR_NOT_SUPPORTED )
3671 {
3672 TEST_EQUAL( output_size,
3673 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3674 TEST_ASSERT( output_size <=
3675 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3676 }
3677 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003678
Steven Cooremanf49478b2021-02-15 15:19:25 +01003679 status = psa_aead_encrypt( key, alg,
3680 nonce->x, nonce->len,
3681 additional_data->x,
3682 additional_data->len,
3683 input_data->x, input_data->len,
3684 output_data, output_size,
3685 &output_length );
3686
3687 /* If the operation is not supported, just skip and not fail in case the
3688 * encryption involves a common limitation of cryptography hardwares and
3689 * an alternative implementation. */
3690 if( status == PSA_ERROR_NOT_SUPPORTED )
3691 {
3692 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3693 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3694 }
3695
3696 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003697
3698 if( PSA_SUCCESS == expected_result )
3699 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003700 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003701
Gilles Peskine003a4a92019-05-14 16:09:40 +02003702 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3703 * should be exact. */
3704 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003705 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003706
gabor-mezei-armceface22021-01-21 12:26:17 +01003707 TEST_ASSERT( input_data->len <=
3708 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3709
Ronald Cron5425a212020-08-04 14:58:35 +02003710 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003711 nonce->x, nonce->len,
3712 additional_data->x,
3713 additional_data->len,
3714 output_data, output_length,
3715 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003716 &output_length2 ),
3717 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003718
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003719 ASSERT_COMPARE( input_data->x, input_data->len,
3720 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003721 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003722
Gilles Peskinea1cac842018-06-11 19:33:02 +02003723exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003724 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003725 mbedtls_free( output_data );
3726 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003727 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003728}
3729/* END_CASE */
3730
3731/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003732void aead_encrypt( int key_type_arg, data_t *key_data,
3733 int alg_arg,
3734 data_t *nonce,
3735 data_t *additional_data,
3736 data_t *input_data,
3737 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003738{
Ronald Cron5425a212020-08-04 14:58:35 +02003739 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003740 psa_key_type_t key_type = key_type_arg;
3741 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003742 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003743 unsigned char *output_data = NULL;
3744 size_t output_size = 0;
3745 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003747 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003748
Gilles Peskine8817f612018-12-18 00:18:46 +01003749 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003750
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003751 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3752 psa_set_key_algorithm( &attributes, alg );
3753 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003754
Gilles Peskine049c7532019-05-15 20:22:09 +02003755 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003756 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003757 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3758 key_bits = psa_get_key_bits( &attributes );
3759
3760 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3761 alg );
3762 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3763 * should be exact. */
3764 TEST_EQUAL( output_size,
3765 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3766 TEST_ASSERT( output_size <=
3767 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3768 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003769
Steven Cooremand588ea12021-01-11 19:36:04 +01003770 status = psa_aead_encrypt( key, alg,
3771 nonce->x, nonce->len,
3772 additional_data->x, additional_data->len,
3773 input_data->x, input_data->len,
3774 output_data, output_size,
3775 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003776
Ronald Cron28a45ed2021-02-09 20:35:42 +01003777 /* If the operation is not supported, just skip and not fail in case the
3778 * encryption involves a common limitation of cryptography hardwares and
3779 * an alternative implementation. */
3780 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003781 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003782 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3783 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003784 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003785
3786 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003787 ASSERT_COMPARE( expected_result->x, expected_result->len,
3788 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003789
Gilles Peskinea1cac842018-06-11 19:33:02 +02003790exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003791 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003792 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003793 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003794}
3795/* END_CASE */
3796
3797/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003798void aead_decrypt( int key_type_arg, data_t *key_data,
3799 int alg_arg,
3800 data_t *nonce,
3801 data_t *additional_data,
3802 data_t *input_data,
3803 data_t *expected_data,
3804 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003805{
Ronald Cron5425a212020-08-04 14:58:35 +02003806 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003807 psa_key_type_t key_type = key_type_arg;
3808 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003809 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003810 unsigned char *output_data = NULL;
3811 size_t output_size = 0;
3812 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003813 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003814 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003815 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003816
Gilles Peskine8817f612018-12-18 00:18:46 +01003817 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003818
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003819 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3820 psa_set_key_algorithm( &attributes, alg );
3821 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003822
Gilles Peskine049c7532019-05-15 20:22:09 +02003823 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003824 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003825 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3826 key_bits = psa_get_key_bits( &attributes );
3827
3828 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3829 alg );
3830 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3831 expected_result != PSA_ERROR_NOT_SUPPORTED )
3832 {
3833 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3834 * should be exact. */
3835 TEST_EQUAL( output_size,
3836 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3837 TEST_ASSERT( output_size <=
3838 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3839 }
3840 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003841
Steven Cooremand588ea12021-01-11 19:36:04 +01003842 status = psa_aead_decrypt( key, alg,
3843 nonce->x, nonce->len,
3844 additional_data->x,
3845 additional_data->len,
3846 input_data->x, input_data->len,
3847 output_data, output_size,
3848 &output_length );
3849
Ronald Cron28a45ed2021-02-09 20:35:42 +01003850 /* If the operation is not supported, just skip and not fail in case the
3851 * decryption involves a common limitation of cryptography hardwares and
3852 * an alternative implementation. */
3853 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003854 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003855 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3856 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003857 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003858
3859 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003860
Gilles Peskine2d277862018-06-18 15:41:12 +02003861 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003862 ASSERT_COMPARE( expected_data->x, expected_data->len,
3863 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003864
Gilles Peskinea1cac842018-06-11 19:33:02 +02003865exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003866 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003867 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003868 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003869}
3870/* END_CASE */
3871
3872/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003873void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3874 int alg_arg,
3875 data_t *nonce,
3876 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003877 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003878 int do_set_lengths,
3879 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003880{
Paul Elliottd3f82412021-06-16 16:52:21 +01003881 size_t ad_part_len = 0;
3882 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003883 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003884
Paul Elliott32f46ba2021-09-23 18:24:36 +01003885 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003886 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003887 mbedtls_test_set_step( ad_part_len );
3888
3889 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003890 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003891 if( ad_part_len & 0x01 )
3892 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3893 else
3894 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003895 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003896
3897 /* Split ad into length(ad_part_len) parts. */
3898 if( !aead_multipart_internal_func( key_type_arg, key_data,
3899 alg_arg, nonce,
3900 additional_data,
3901 ad_part_len,
3902 input_data, -1,
3903 set_lengths_method,
3904 expected_output,
3905 1, 0 ) )
3906 break;
3907
3908 /* length(0) part, length(ad_part_len) part, length(0) part... */
3909 mbedtls_test_set_step( 1000 + ad_part_len );
3910
3911 if( !aead_multipart_internal_func( key_type_arg, key_data,
3912 alg_arg, nonce,
3913 additional_data,
3914 ad_part_len,
3915 input_data, -1,
3916 set_lengths_method,
3917 expected_output,
3918 1, 1 ) )
3919 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003920 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003921
Paul Elliott32f46ba2021-09-23 18:24:36 +01003922 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003923 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003924 /* Split data into length(data_part_len) parts. */
3925 mbedtls_test_set_step( 2000 + data_part_len );
3926
3927 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003928 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003929 if( data_part_len & 0x01 )
3930 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3931 else
3932 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003933 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003934
Paul Elliott32f46ba2021-09-23 18:24:36 +01003935 if( !aead_multipart_internal_func( key_type_arg, key_data,
3936 alg_arg, nonce,
3937 additional_data, -1,
3938 input_data, data_part_len,
3939 set_lengths_method,
3940 expected_output,
3941 1, 0 ) )
3942 break;
3943
3944 /* length(0) part, length(data_part_len) part, length(0) part... */
3945 mbedtls_test_set_step( 3000 + data_part_len );
3946
3947 if( !aead_multipart_internal_func( key_type_arg, key_data,
3948 alg_arg, nonce,
3949 additional_data, -1,
3950 input_data, data_part_len,
3951 set_lengths_method,
3952 expected_output,
3953 1, 1 ) )
3954 break;
3955 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003956
Paul Elliott8fc45162021-06-23 16:06:01 +01003957 /* Goto is required to silence warnings about unused labels, as we
3958 * don't actually do any test assertions in this function. */
3959 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003960}
3961/* END_CASE */
3962
3963/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003964void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3965 int alg_arg,
3966 data_t *nonce,
3967 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003968 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003969 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003970 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003971{
Paul Elliottd3f82412021-06-16 16:52:21 +01003972 size_t ad_part_len = 0;
3973 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003974 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003975
Paul Elliott32f46ba2021-09-23 18:24:36 +01003976 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003977 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003978 /* Split ad into length(ad_part_len) parts. */
3979 mbedtls_test_set_step( ad_part_len );
3980
3981 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003982 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003983 if( ad_part_len & 0x01 )
3984 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3985 else
3986 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003987 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003988
3989 if( !aead_multipart_internal_func( key_type_arg, key_data,
3990 alg_arg, nonce,
3991 additional_data,
3992 ad_part_len,
3993 input_data, -1,
3994 set_lengths_method,
3995 expected_output,
3996 0, 0 ) )
3997 break;
3998
3999 /* length(0) part, length(ad_part_len) part, length(0) part... */
4000 mbedtls_test_set_step( 1000 + ad_part_len );
4001
4002 if( !aead_multipart_internal_func( key_type_arg, key_data,
4003 alg_arg, nonce,
4004 additional_data,
4005 ad_part_len,
4006 input_data, -1,
4007 set_lengths_method,
4008 expected_output,
4009 0, 1 ) )
4010 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004011 }
4012
Paul Elliott32f46ba2021-09-23 18:24:36 +01004013 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004014 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004015 /* Split data into length(data_part_len) parts. */
4016 mbedtls_test_set_step( 2000 + data_part_len );
4017
4018 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004019 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004020 if( data_part_len & 0x01 )
4021 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4022 else
4023 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004024 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004025
4026 if( !aead_multipart_internal_func( key_type_arg, key_data,
4027 alg_arg, nonce,
4028 additional_data, -1,
4029 input_data, data_part_len,
4030 set_lengths_method,
4031 expected_output,
4032 0, 0 ) )
4033 break;
4034
4035 /* length(0) part, length(data_part_len) part, length(0) part... */
4036 mbedtls_test_set_step( 3000 + data_part_len );
4037
4038 if( !aead_multipart_internal_func( key_type_arg, key_data,
4039 alg_arg, nonce,
4040 additional_data, -1,
4041 input_data, data_part_len,
4042 set_lengths_method,
4043 expected_output,
4044 0, 1 ) )
4045 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004046 }
4047
Paul Elliott8fc45162021-06-23 16:06:01 +01004048 /* Goto is required to silence warnings about unused labels, as we
4049 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004050 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004051}
4052/* END_CASE */
4053
4054/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004055void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4056 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004057 int nonce_length,
4058 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004059 data_t *additional_data,
4060 data_t *input_data,
4061 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004062{
4063
4064 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4065 psa_key_type_t key_type = key_type_arg;
4066 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004067 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004068 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4069 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4070 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004071 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004072 size_t actual_nonce_length = 0;
4073 size_t expected_nonce_length = expected_nonce_length_arg;
4074 unsigned char *output = NULL;
4075 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004076 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004077 size_t ciphertext_size = 0;
4078 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004079 size_t tag_length = 0;
4080 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004081
4082 PSA_ASSERT( psa_crypto_init( ) );
4083
4084 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4085 psa_set_key_algorithm( & attributes, alg );
4086 psa_set_key_type( & attributes, key_type );
4087
4088 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4089 &key ) );
4090
4091 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4092
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004093 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4094
Paul Elliottf1277632021-08-24 18:11:37 +01004095 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004096
Paul Elliottf1277632021-08-24 18:11:37 +01004097 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004098
Paul Elliottf1277632021-08-24 18:11:37 +01004099 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004100
Paul Elliottf1277632021-08-24 18:11:37 +01004101 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004102
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004103 status = psa_aead_encrypt_setup( &operation, key, alg );
4104
4105 /* If the operation is not supported, just skip and not fail in case the
4106 * encryption involves a common limitation of cryptography hardwares and
4107 * an alternative implementation. */
4108 if( status == PSA_ERROR_NOT_SUPPORTED )
4109 {
4110 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004111 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004112 }
4113
4114 PSA_ASSERT( status );
4115
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004116 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004117 nonce_length,
4118 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004119
Paul Elliott693bf312021-07-23 17:40:41 +01004120 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004121
Paul Elliottf1277632021-08-24 18:11:37 +01004122 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004123
Paul Elliott88ecbe12021-09-22 17:23:03 +01004124 if( expected_status == PSA_SUCCESS )
4125 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4126 alg ) );
4127
Paul Elliottd79c5c52021-10-06 21:49:41 +01004128 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004129
Paul Elliott693bf312021-07-23 17:40:41 +01004130 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004131 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004132 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004133 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4134 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004135
4136 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4137 additional_data->len ) );
4138
4139 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004140 output, output_size,
4141 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004142
Paul Elliottf1277632021-08-24 18:11:37 +01004143 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4144 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004145 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4146 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004147
4148exit:
4149 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004150 mbedtls_free( output );
4151 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004152 psa_aead_abort( &operation );
4153 PSA_DONE( );
4154}
4155/* END_CASE */
4156
4157/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004158void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4159 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004160 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004161 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004162 data_t *additional_data,
4163 data_t *input_data,
4164 int expected_status_arg )
4165{
4166
4167 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4168 psa_key_type_t key_type = key_type_arg;
4169 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004170 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004171 uint8_t *nonce_buffer = NULL;
4172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4173 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4174 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004175 unsigned char *output = NULL;
4176 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004177 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004178 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004179 size_t ciphertext_size = 0;
4180 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004181 size_t tag_length = 0;
4182 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004183 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004184 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004185
4186 PSA_ASSERT( psa_crypto_init( ) );
4187
4188 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4189 psa_set_key_algorithm( &attributes, alg );
4190 psa_set_key_type( &attributes, key_type );
4191
4192 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4193 &key ) );
4194
4195 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4196
4197 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4198
Paul Elliott6f0e7202021-08-25 12:57:18 +01004199 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004200
Paul Elliott6f0e7202021-08-25 12:57:18 +01004201 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004202
Paul Elliott6f0e7202021-08-25 12:57:18 +01004203 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004204
Paul Elliott6f0e7202021-08-25 12:57:18 +01004205 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004206
Paul Elliott863864a2021-07-23 17:28:31 +01004207 status = psa_aead_encrypt_setup( &operation, key, alg );
4208
4209 /* If the operation is not supported, just skip and not fail in case the
4210 * encryption involves a common limitation of cryptography hardwares and
4211 * an alternative implementation. */
4212 if( status == PSA_ERROR_NOT_SUPPORTED )
4213 {
4214 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004215 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004216 }
4217
4218 PSA_ASSERT( status );
4219
Paul Elliott4023ffd2021-09-10 16:21:22 +01004220 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4221 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004222 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004223 /* Arbitrary size buffer, to test zero length valid buffer. */
4224 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004225 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004226 }
4227 else
4228 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004229 /* If length is zero, then this will return NULL. */
4230 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004231 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004232
Paul Elliott4023ffd2021-09-10 16:21:22 +01004233 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004234 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004235 for( index = 0; index < nonce_length - 1; ++index )
4236 {
4237 nonce_buffer[index] = 'a' + index;
4238 }
Paul Elliott66696b52021-08-16 18:42:41 +01004239 }
Paul Elliott863864a2021-07-23 17:28:31 +01004240 }
4241
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004242 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4243 {
4244 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4245 input_data->len ) );
4246 }
4247
Paul Elliott6f0e7202021-08-25 12:57:18 +01004248 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004249
Paul Elliott693bf312021-07-23 17:40:41 +01004250 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004251
4252 if( expected_status == PSA_SUCCESS )
4253 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004254 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4255 {
4256 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4257 input_data->len ) );
4258 }
4259 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4260 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004261
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004262 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4263 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4264 additional_data->len ),
4265 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004266
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004267 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004268 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004269 &ciphertext_length ),
4270 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004271
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004272 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004273 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004274 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4275 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004276 }
4277
4278exit:
4279 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004280 mbedtls_free( output );
4281 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004282 mbedtls_free( nonce_buffer );
4283 psa_aead_abort( &operation );
4284 PSA_DONE( );
4285}
4286/* END_CASE */
4287
4288/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004289void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4290 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004291 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004292 data_t *nonce,
4293 data_t *additional_data,
4294 data_t *input_data,
4295 int expected_status_arg )
4296{
4297
4298 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4299 psa_key_type_t key_type = key_type_arg;
4300 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004301 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004302 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4303 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4304 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004305 unsigned char *output = NULL;
4306 unsigned char *ciphertext = NULL;
4307 size_t output_size = output_size_arg;
4308 size_t ciphertext_size = 0;
4309 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004310 size_t tag_length = 0;
4311 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4312
4313 PSA_ASSERT( psa_crypto_init( ) );
4314
4315 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4316 psa_set_key_algorithm( &attributes, alg );
4317 psa_set_key_type( &attributes, key_type );
4318
4319 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4320 &key ) );
4321
4322 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4323
Paul Elliottc6d11d02021-09-01 12:04:23 +01004324 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004325
Paul Elliottc6d11d02021-09-01 12:04:23 +01004326 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004327
Paul Elliottc6d11d02021-09-01 12:04:23 +01004328 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004329
Paul Elliott43fbda62021-07-23 18:30:59 +01004330 status = psa_aead_encrypt_setup( &operation, key, alg );
4331
4332 /* If the operation is not supported, just skip and not fail in case the
4333 * encryption involves a common limitation of cryptography hardwares and
4334 * an alternative implementation. */
4335 if( status == PSA_ERROR_NOT_SUPPORTED )
4336 {
4337 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4338 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4339 }
4340
4341 PSA_ASSERT( status );
4342
Paul Elliott47b9a142021-10-07 15:04:57 +01004343 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4344 input_data->len ) );
4345
Paul Elliott43fbda62021-07-23 18:30:59 +01004346 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4347
4348 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4349 additional_data->len ) );
4350
4351 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004352 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004353
4354 TEST_EQUAL( status, expected_status );
4355
4356 if( expected_status == PSA_SUCCESS )
4357 {
4358 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004359 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4360 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004361 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4362 }
4363
4364exit:
4365 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004366 mbedtls_free( output );
4367 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004368 psa_aead_abort( &operation );
4369 PSA_DONE( );
4370}
4371/* END_CASE */
4372
Paul Elliott91b021e2021-07-23 18:52:31 +01004373/* BEGIN_CASE */
4374void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4375 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004376 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004377 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004378 data_t *nonce,
4379 data_t *additional_data,
4380 data_t *input_data,
4381 int expected_status_arg )
4382{
4383
4384 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4385 psa_key_type_t key_type = key_type_arg;
4386 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004387 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004388 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4389 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4390 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004391 unsigned char *ciphertext = NULL;
4392 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004393 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004394 size_t ciphertext_size = 0;
4395 size_t ciphertext_length = 0;
4396 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004397 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004398 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004399
4400 PSA_ASSERT( psa_crypto_init( ) );
4401
4402 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4403 psa_set_key_algorithm( &attributes, alg );
4404 psa_set_key_type( &attributes, key_type );
4405
4406 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4407 &key ) );
4408
4409 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4410
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004411 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004412
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004413 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004414
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004415 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004416
Paul Elliott719c1322021-09-13 18:27:22 +01004417 ASSERT_ALLOC( tag_buffer, tag_size );
4418
Paul Elliott91b021e2021-07-23 18:52:31 +01004419 status = psa_aead_encrypt_setup( &operation, key, alg );
4420
4421 /* If the operation is not supported, just skip and not fail in case the
4422 * encryption involves a common limitation of cryptography hardwares and
4423 * an alternative implementation. */
4424 if( status == PSA_ERROR_NOT_SUPPORTED )
4425 {
4426 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4427 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4428 }
4429
4430 PSA_ASSERT( status );
4431
4432 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4433
Paul Elliott76bda482021-10-07 17:07:23 +01004434 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4435 input_data->len ) );
4436
Paul Elliott91b021e2021-07-23 18:52:31 +01004437 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4438 additional_data->len ) );
4439
4440 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004441 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004442
4443 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004444 status = psa_aead_finish( &operation, finish_ciphertext,
4445 finish_ciphertext_size,
4446 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004447 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004448
4449 TEST_EQUAL( status, expected_status );
4450
4451exit:
4452 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004453 mbedtls_free( ciphertext );
4454 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004455 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004456 psa_aead_abort( &operation );
4457 PSA_DONE( );
4458}
4459/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004460
4461/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004462void aead_multipart_verify( int key_type_arg, data_t *key_data,
4463 int alg_arg,
4464 data_t *nonce,
4465 data_t *additional_data,
4466 data_t *input_data,
4467 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004468 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004469 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004470 int expected_status_arg )
4471{
4472 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4473 psa_key_type_t key_type = key_type_arg;
4474 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004475 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4477 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4478 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004479 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004480 unsigned char *plaintext = NULL;
4481 unsigned char *finish_plaintext = NULL;
4482 size_t plaintext_size = 0;
4483 size_t plaintext_length = 0;
4484 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004485 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004486 unsigned char *tag_buffer = NULL;
4487 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004488
4489 PSA_ASSERT( psa_crypto_init( ) );
4490
4491 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4492 psa_set_key_algorithm( &attributes, alg );
4493 psa_set_key_type( &attributes, key_type );
4494
4495 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4496 &key ) );
4497
4498 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4499
4500 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4501 input_data->len );
4502
4503 ASSERT_ALLOC( plaintext, plaintext_size );
4504
4505 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4506
4507 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4508
Paul Elliott9961a662021-09-17 19:19:02 +01004509 status = psa_aead_decrypt_setup( &operation, key, alg );
4510
4511 /* If the operation is not supported, just skip and not fail in case the
4512 * encryption involves a common limitation of cryptography hardwares and
4513 * an alternative implementation. */
4514 if( status == PSA_ERROR_NOT_SUPPORTED )
4515 {
4516 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4517 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4518 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004519 TEST_EQUAL( status, expected_setup_status );
4520
4521 if( status != PSA_SUCCESS )
4522 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004523
4524 PSA_ASSERT( status );
4525
4526 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4527
Paul Elliottfec6f372021-10-06 17:15:02 +01004528 status = psa_aead_set_lengths( &operation, additional_data->len,
4529 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004530 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004531
Paul Elliott9961a662021-09-17 19:19:02 +01004532 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4533 additional_data->len ) );
4534
4535 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4536 input_data->len,
4537 plaintext, plaintext_size,
4538 &plaintext_length ) );
4539
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004540 if( tag_usage == USE_GIVEN_TAG )
4541 {
4542 tag_buffer = tag->x;
4543 tag_size = tag->len;
4544 }
4545
Paul Elliott9961a662021-09-17 19:19:02 +01004546 status = psa_aead_verify( &operation, finish_plaintext,
4547 verify_plaintext_size,
4548 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004549 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004550
4551 TEST_EQUAL( status, expected_status );
4552
4553exit:
4554 psa_destroy_key( key );
4555 mbedtls_free( plaintext );
4556 mbedtls_free( finish_plaintext );
4557 psa_aead_abort( &operation );
4558 PSA_DONE( );
4559}
4560/* END_CASE */
4561
Paul Elliott9961a662021-09-17 19:19:02 +01004562/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004563void aead_multipart_setup( int key_type_arg, data_t *key_data,
4564 int alg_arg, int expected_status_arg )
4565{
4566 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4567 psa_key_type_t key_type = key_type_arg;
4568 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004569 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4571 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4572 psa_status_t expected_status = expected_status_arg;
4573
4574 PSA_ASSERT( psa_crypto_init( ) );
4575
4576 psa_set_key_usage_flags( &attributes,
4577 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4578 psa_set_key_algorithm( &attributes, alg );
4579 psa_set_key_type( &attributes, key_type );
4580
4581 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4582 &key ) );
4583
Paul Elliott5221ef62021-09-19 17:33:03 +01004584 status = psa_aead_encrypt_setup( &operation, key, alg );
4585
4586 TEST_EQUAL( status, expected_status );
4587
4588 psa_aead_abort( &operation );
4589
Paul Elliott5221ef62021-09-19 17:33:03 +01004590 status = psa_aead_decrypt_setup( &operation, key, alg );
4591
4592 TEST_EQUAL(status, expected_status );
4593
4594exit:
4595 psa_destroy_key( key );
4596 psa_aead_abort( &operation );
4597 PSA_DONE( );
4598}
4599/* END_CASE */
4600
4601/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004602void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4603 int alg_arg,
4604 data_t *nonce,
4605 data_t *additional_data,
4606 data_t *input_data )
4607{
4608 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4609 psa_key_type_t key_type = key_type_arg;
4610 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004611 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004612 unsigned char *output_data = NULL;
4613 unsigned char *final_data = NULL;
4614 size_t output_size = 0;
4615 size_t finish_output_size = 0;
4616 size_t output_length = 0;
4617 size_t key_bits = 0;
4618 size_t tag_length = 0;
4619 size_t tag_size = 0;
4620 size_t nonce_length = 0;
4621 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4622 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4623 size_t output_part_length = 0;
4624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4625
4626 PSA_ASSERT( psa_crypto_init( ) );
4627
4628 psa_set_key_usage_flags( & attributes,
4629 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4630 psa_set_key_algorithm( & attributes, alg );
4631 psa_set_key_type( & attributes, key_type );
4632
4633 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4634 &key ) );
4635
4636 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4637 key_bits = psa_get_key_bits( &attributes );
4638
4639 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4640
4641 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4642
4643 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4644
4645 ASSERT_ALLOC( output_data, output_size );
4646
4647 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4648
4649 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4650
4651 ASSERT_ALLOC( final_data, finish_output_size );
4652
4653 /* Test all operations error without calling setup first. */
4654
Paul Elliottc23a9a02021-06-21 18:32:46 +01004655 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4656 PSA_ERROR_BAD_STATE );
4657
4658 psa_aead_abort( &operation );
4659
Paul Elliottc23a9a02021-06-21 18:32:46 +01004660 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4661 PSA_AEAD_NONCE_MAX_SIZE,
4662 &nonce_length ),
4663 PSA_ERROR_BAD_STATE );
4664
4665 psa_aead_abort( &operation );
4666
Paul Elliott481be342021-07-16 17:38:47 +01004667 /* ------------------------------------------------------- */
4668
Paul Elliottc23a9a02021-06-21 18:32:46 +01004669 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4670 input_data->len ),
4671 PSA_ERROR_BAD_STATE );
4672
4673 psa_aead_abort( &operation );
4674
Paul Elliott481be342021-07-16 17:38:47 +01004675 /* ------------------------------------------------------- */
4676
Paul Elliottc23a9a02021-06-21 18:32:46 +01004677 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4678 additional_data->len ),
4679 PSA_ERROR_BAD_STATE );
4680
4681 psa_aead_abort( &operation );
4682
Paul Elliott481be342021-07-16 17:38:47 +01004683 /* ------------------------------------------------------- */
4684
Paul Elliottc23a9a02021-06-21 18:32:46 +01004685 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4686 input_data->len, output_data,
4687 output_size, &output_length ),
4688 PSA_ERROR_BAD_STATE );
4689
4690 psa_aead_abort( &operation );
4691
Paul Elliott481be342021-07-16 17:38:47 +01004692 /* ------------------------------------------------------- */
4693
Paul Elliottc23a9a02021-06-21 18:32:46 +01004694 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4695 finish_output_size,
4696 &output_part_length,
4697 tag_buffer, tag_length,
4698 &tag_size ),
4699 PSA_ERROR_BAD_STATE );
4700
4701 psa_aead_abort( &operation );
4702
Paul Elliott481be342021-07-16 17:38:47 +01004703 /* ------------------------------------------------------- */
4704
Paul Elliottc23a9a02021-06-21 18:32:46 +01004705 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4706 finish_output_size,
4707 &output_part_length,
4708 tag_buffer,
4709 tag_length ),
4710 PSA_ERROR_BAD_STATE );
4711
4712 psa_aead_abort( &operation );
4713
4714 /* Test for double setups. */
4715
Paul Elliottc23a9a02021-06-21 18:32:46 +01004716 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4717
4718 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4719 PSA_ERROR_BAD_STATE );
4720
4721 psa_aead_abort( &operation );
4722
Paul Elliott481be342021-07-16 17:38:47 +01004723 /* ------------------------------------------------------- */
4724
Paul Elliottc23a9a02021-06-21 18:32:46 +01004725 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4726
4727 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4728 PSA_ERROR_BAD_STATE );
4729
4730 psa_aead_abort( &operation );
4731
Paul Elliott374a2be2021-07-16 17:53:40 +01004732 /* ------------------------------------------------------- */
4733
Paul Elliott374a2be2021-07-16 17:53:40 +01004734 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4735
4736 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4737 PSA_ERROR_BAD_STATE );
4738
4739 psa_aead_abort( &operation );
4740
4741 /* ------------------------------------------------------- */
4742
Paul Elliott374a2be2021-07-16 17:53:40 +01004743 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4744
4745 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4746 PSA_ERROR_BAD_STATE );
4747
4748 psa_aead_abort( &operation );
4749
Paul Elliottc23a9a02021-06-21 18:32:46 +01004750 /* Test for not setting a nonce. */
4751
Paul Elliottc23a9a02021-06-21 18:32:46 +01004752 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4753
4754 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4755 additional_data->len ),
4756 PSA_ERROR_BAD_STATE );
4757
4758 psa_aead_abort( &operation );
4759
Paul Elliott7f628422021-09-01 12:08:29 +01004760 /* ------------------------------------------------------- */
4761
Paul Elliott7f628422021-09-01 12:08:29 +01004762 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4763
4764 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4765 input_data->len, output_data,
4766 output_size, &output_length ),
4767 PSA_ERROR_BAD_STATE );
4768
4769 psa_aead_abort( &operation );
4770
Paul Elliottbdc2c682021-09-21 18:37:10 +01004771 /* ------------------------------------------------------- */
4772
Paul Elliottbdc2c682021-09-21 18:37:10 +01004773 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4774
4775 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4776 finish_output_size,
4777 &output_part_length,
4778 tag_buffer, tag_length,
4779 &tag_size ),
4780 PSA_ERROR_BAD_STATE );
4781
4782 psa_aead_abort( &operation );
4783
4784 /* ------------------------------------------------------- */
4785
Paul Elliottbdc2c682021-09-21 18:37:10 +01004786 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4787
4788 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4789 finish_output_size,
4790 &output_part_length,
4791 tag_buffer,
4792 tag_length ),
4793 PSA_ERROR_BAD_STATE );
4794
4795 psa_aead_abort( &operation );
4796
Paul Elliottc23a9a02021-06-21 18:32:46 +01004797 /* Test for double setting nonce. */
4798
Paul Elliottc23a9a02021-06-21 18:32:46 +01004799 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4800
4801 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4802
4803 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4804 PSA_ERROR_BAD_STATE );
4805
4806 psa_aead_abort( &operation );
4807
Paul Elliott374a2be2021-07-16 17:53:40 +01004808 /* Test for double generating nonce. */
4809
Paul Elliott374a2be2021-07-16 17:53:40 +01004810 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4811
4812 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4813 PSA_AEAD_NONCE_MAX_SIZE,
4814 &nonce_length ) );
4815
4816 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4817 PSA_AEAD_NONCE_MAX_SIZE,
4818 &nonce_length ),
4819 PSA_ERROR_BAD_STATE );
4820
4821
4822 psa_aead_abort( &operation );
4823
4824 /* Test for generate nonce then set and vice versa */
4825
Paul Elliott374a2be2021-07-16 17:53:40 +01004826 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4827
4828 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4829 PSA_AEAD_NONCE_MAX_SIZE,
4830 &nonce_length ) );
4831
4832 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4833 PSA_ERROR_BAD_STATE );
4834
4835 psa_aead_abort( &operation );
4836
Andrzej Kurekad837522021-12-15 15:28:49 +01004837 /* Test for generating nonce after calling set lengths */
4838
4839 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4840
4841 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4842 input_data->len ) );
4843
4844 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4845 PSA_AEAD_NONCE_MAX_SIZE,
4846 &nonce_length ) );
4847
4848 psa_aead_abort( &operation );
4849
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004850 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004851
4852 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4853
4854 if( operation.alg == PSA_ALG_CCM )
4855 {
4856 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4857 input_data->len ),
4858 PSA_ERROR_INVALID_ARGUMENT );
4859 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4860 PSA_AEAD_NONCE_MAX_SIZE,
4861 &nonce_length ),
4862 PSA_ERROR_BAD_STATE );
4863 }
4864 else
4865 {
4866 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4867 input_data->len ) );
4868 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4869 PSA_AEAD_NONCE_MAX_SIZE,
4870 &nonce_length ) );
4871 }
4872
4873 psa_aead_abort( &operation );
4874
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004875 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004876#if SIZE_MAX > UINT32_MAX
4877 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4878
4879 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
4880 {
4881 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
4882 input_data->len ),
4883 PSA_ERROR_INVALID_ARGUMENT );
4884 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4885 PSA_AEAD_NONCE_MAX_SIZE,
4886 &nonce_length ),
4887 PSA_ERROR_BAD_STATE );
4888 }
4889 else
4890 {
4891 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
4892 input_data->len ) );
4893 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4894 PSA_AEAD_NONCE_MAX_SIZE,
4895 &nonce_length ) );
4896 }
4897
4898 psa_aead_abort( &operation );
4899#endif
4900
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004901 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01004902
4903 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4904
4905 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4906 PSA_AEAD_NONCE_MAX_SIZE,
4907 &nonce_length ) );
4908
4909 if( operation.alg == PSA_ALG_CCM )
4910 {
4911 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4912 input_data->len ),
4913 PSA_ERROR_INVALID_ARGUMENT );
4914 }
4915 else
4916 {
4917 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4918 input_data->len ) );
4919 }
4920
4921 psa_aead_abort( &operation );
4922
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004923 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004924 /* Test for setting nonce after calling set lengths */
4925
4926 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4927
4928 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4929 input_data->len ) );
4930
4931 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4932
4933 psa_aead_abort( &operation );
4934
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004935 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004936
4937 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4938
4939 if( operation.alg == PSA_ALG_CCM )
4940 {
4941 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4942 input_data->len ),
4943 PSA_ERROR_INVALID_ARGUMENT );
4944 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4945 PSA_ERROR_BAD_STATE );
4946 }
4947 else
4948 {
4949 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4950 input_data->len ) );
4951 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4952 }
4953
4954 psa_aead_abort( &operation );
4955
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004956 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004957#if SIZE_MAX > UINT32_MAX
4958 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4959
4960 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
4961 {
4962 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
4963 input_data->len ),
4964 PSA_ERROR_INVALID_ARGUMENT );
4965 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4966 PSA_ERROR_BAD_STATE );
4967 }
4968 else
4969 {
4970 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
4971 input_data->len ) );
4972 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4973 }
4974
4975 psa_aead_abort( &operation );
4976#endif
4977
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004978 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004979
4980 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4981
4982 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4983
4984 if( operation.alg == PSA_ALG_CCM )
4985 {
4986 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4987 input_data->len ),
4988 PSA_ERROR_INVALID_ARGUMENT );
4989 }
4990 else
4991 {
4992 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4993 input_data->len ) );
4994 }
4995
4996 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01004997
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004998 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004999#if SIZE_MAX > UINT32_MAX
5000 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5001
5002 if( operation.alg == PSA_ALG_GCM )
5003 {
5004 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5005 SIZE_MAX ),
5006 PSA_ERROR_INVALID_ARGUMENT );
5007 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5008 PSA_ERROR_BAD_STATE );
5009 }
5010 else if ( operation.alg != PSA_ALG_CCM )
5011 {
5012 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5013 SIZE_MAX ) );
5014 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5015 }
5016
5017 psa_aead_abort( &operation );
5018
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005019 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005020 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5021
5022 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5023
5024 if( operation.alg == PSA_ALG_GCM )
5025 {
5026 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5027 SIZE_MAX ),
5028 PSA_ERROR_INVALID_ARGUMENT );
5029 }
5030 else if ( operation.alg != PSA_ALG_CCM )
5031 {
5032 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5033 SIZE_MAX ) );
5034 }
5035
5036 psa_aead_abort( &operation );
5037#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005038
5039 /* ------------------------------------------------------- */
5040
Paul Elliott374a2be2021-07-16 17:53:40 +01005041 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5042
5043 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5044
5045 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5046 PSA_AEAD_NONCE_MAX_SIZE,
5047 &nonce_length ),
5048 PSA_ERROR_BAD_STATE );
5049
5050 psa_aead_abort( &operation );
5051
Paul Elliott7220cae2021-06-22 17:25:57 +01005052 /* Test for generating nonce in decrypt setup. */
5053
Paul Elliott7220cae2021-06-22 17:25:57 +01005054 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5055
5056 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5057 PSA_AEAD_NONCE_MAX_SIZE,
5058 &nonce_length ),
5059 PSA_ERROR_BAD_STATE );
5060
5061 psa_aead_abort( &operation );
5062
Paul Elliottc23a9a02021-06-21 18:32:46 +01005063 /* Test for setting lengths twice. */
5064
Paul Elliottc23a9a02021-06-21 18:32:46 +01005065 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5066
5067 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5068
5069 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5070 input_data->len ) );
5071
5072 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5073 input_data->len ),
5074 PSA_ERROR_BAD_STATE );
5075
5076 psa_aead_abort( &operation );
5077
Andrzej Kurekad837522021-12-15 15:28:49 +01005078 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005079
Paul Elliottc23a9a02021-06-21 18:32:46 +01005080 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5081
5082 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5083
Andrzej Kurekad837522021-12-15 15:28:49 +01005084 if( operation.alg == PSA_ALG_CCM )
5085 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005086
Andrzej Kurekad837522021-12-15 15:28:49 +01005087 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5088 additional_data->len ),
5089 PSA_ERROR_BAD_STATE );
5090 }
5091 else
5092 {
5093 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5094 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005095
Andrzej Kurekad837522021-12-15 15:28:49 +01005096 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5097 input_data->len ),
5098 PSA_ERROR_BAD_STATE );
5099 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005100 psa_aead_abort( &operation );
5101
5102 /* ------------------------------------------------------- */
5103
Paul Elliottf94bd992021-09-19 18:15:59 +01005104 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5105
5106 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5107
Andrzej Kurekad837522021-12-15 15:28:49 +01005108 if( operation.alg == PSA_ALG_CCM )
5109 {
5110 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5111 input_data->len, output_data,
5112 output_size, &output_length ),
5113 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005114
Andrzej Kurekad837522021-12-15 15:28:49 +01005115 }
5116 else
5117 {
5118 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5119 input_data->len, output_data,
5120 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005121
Andrzej Kurekad837522021-12-15 15:28:49 +01005122 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5123 input_data->len ),
5124 PSA_ERROR_BAD_STATE );
5125 }
5126 psa_aead_abort( &operation );
5127
5128 /* ------------------------------------------------------- */
5129
5130 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5131
5132 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5133
5134 if( operation.alg == PSA_ALG_CCM )
5135 {
5136 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5137 finish_output_size,
5138 &output_part_length,
5139 tag_buffer, tag_length,
5140 &tag_size ) );
5141 }
5142 else
5143 {
5144 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5145 finish_output_size,
5146 &output_part_length,
5147 tag_buffer, tag_length,
5148 &tag_size ) );
5149
5150 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5151 input_data->len ),
5152 PSA_ERROR_BAD_STATE );
5153 }
5154 psa_aead_abort( &operation );
5155
5156 /* Test for setting lengths after generating nonce + already starting data. */
5157
5158 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5159
5160 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5161 PSA_AEAD_NONCE_MAX_SIZE,
5162 &nonce_length ) );
5163 if( operation.alg == PSA_ALG_CCM )
5164 {
5165
5166 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5167 additional_data->len ),
5168 PSA_ERROR_BAD_STATE );
5169 }
5170 else
5171 {
5172 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5173 additional_data->len ) );
5174
5175 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5176 input_data->len ),
5177 PSA_ERROR_BAD_STATE );
5178 }
5179 psa_aead_abort( &operation );
5180
5181 /* ------------------------------------------------------- */
5182
5183 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5184
5185 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5186 PSA_AEAD_NONCE_MAX_SIZE,
5187 &nonce_length ) );
5188 if( operation.alg == PSA_ALG_CCM )
5189 {
5190 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5191 input_data->len, output_data,
5192 output_size, &output_length ),
5193 PSA_ERROR_BAD_STATE );
5194
5195 }
5196 else
5197 {
5198 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5199 input_data->len, output_data,
5200 output_size, &output_length ) );
5201
5202 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5203 input_data->len ),
5204 PSA_ERROR_BAD_STATE );
5205 }
5206 psa_aead_abort( &operation );
5207
5208 /* ------------------------------------------------------- */
5209
5210 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5211
5212 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5213 PSA_AEAD_NONCE_MAX_SIZE,
5214 &nonce_length ) );
5215 if( operation.alg == PSA_ALG_CCM )
5216 {
5217 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5218 finish_output_size,
5219 &output_part_length,
5220 tag_buffer, tag_length,
5221 &tag_size ) );
5222 }
5223 else
5224 {
5225 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5226 finish_output_size,
5227 &output_part_length,
5228 tag_buffer, tag_length,
5229 &tag_size ) );
5230
5231 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5232 input_data->len ),
5233 PSA_ERROR_BAD_STATE );
5234 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005235 psa_aead_abort( &operation );
5236
Paul Elliott243080c2021-07-21 19:01:17 +01005237 /* Test for not sending any additional data or data after setting non zero
5238 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005239
Paul Elliottc23a9a02021-06-21 18:32:46 +01005240 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5241
5242 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5243
5244 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5245 input_data->len ) );
5246
5247 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5248 finish_output_size,
5249 &output_part_length,
5250 tag_buffer, tag_length,
5251 &tag_size ),
5252 PSA_ERROR_INVALID_ARGUMENT );
5253
5254 psa_aead_abort( &operation );
5255
Paul Elliott243080c2021-07-21 19:01:17 +01005256 /* Test for not sending any additional data or data after setting non-zero
5257 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005258
Paul Elliottc23a9a02021-06-21 18:32:46 +01005259 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5260
5261 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5262
5263 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5264 input_data->len ) );
5265
5266 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5267 finish_output_size,
5268 &output_part_length,
5269 tag_buffer,
5270 tag_length ),
5271 PSA_ERROR_INVALID_ARGUMENT );
5272
5273 psa_aead_abort( &operation );
5274
Paul Elliott243080c2021-07-21 19:01:17 +01005275 /* Test for not sending any additional data after setting a non-zero length
5276 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005277
Paul Elliottc23a9a02021-06-21 18:32:46 +01005278 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5279
5280 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5281
5282 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5283 input_data->len ) );
5284
5285 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5286 input_data->len, output_data,
5287 output_size, &output_length ),
5288 PSA_ERROR_INVALID_ARGUMENT );
5289
5290 psa_aead_abort( &operation );
5291
Paul Elliottf94bd992021-09-19 18:15:59 +01005292 /* Test for not sending any data after setting a non-zero length for it.*/
5293
Paul Elliottf94bd992021-09-19 18:15:59 +01005294 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5295
5296 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5297
5298 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5299 input_data->len ) );
5300
5301 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5302 additional_data->len ) );
5303
5304 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5305 finish_output_size,
5306 &output_part_length,
5307 tag_buffer, tag_length,
5308 &tag_size ),
5309 PSA_ERROR_INVALID_ARGUMENT );
5310
5311 psa_aead_abort( &operation );
5312
Paul Elliottb0450fe2021-09-01 15:06:26 +01005313 /* Test for sending too much additional data after setting lengths. */
5314
Paul Elliottb0450fe2021-09-01 15:06:26 +01005315 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5316
5317 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5318
5319 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5320
5321
5322 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5323 additional_data->len ),
5324 PSA_ERROR_INVALID_ARGUMENT );
5325
5326 psa_aead_abort( &operation );
5327
Paul Elliotta2a09b02021-09-22 14:56:40 +01005328 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005329
5330 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5331
5332 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5333
5334 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5335 input_data->len ) );
5336
5337 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5338 additional_data->len ) );
5339
5340 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5341 1 ),
5342 PSA_ERROR_INVALID_ARGUMENT );
5343
5344 psa_aead_abort( &operation );
5345
Paul Elliottb0450fe2021-09-01 15:06:26 +01005346 /* Test for sending too much data after setting lengths. */
5347
Paul Elliottb0450fe2021-09-01 15:06:26 +01005348 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5349
5350 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5351
5352 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5353
5354 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5355 input_data->len, output_data,
5356 output_size, &output_length ),
5357 PSA_ERROR_INVALID_ARGUMENT );
5358
5359 psa_aead_abort( &operation );
5360
Paul Elliotta2a09b02021-09-22 14:56:40 +01005361 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005362
5363 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5364
5365 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5366
5367 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5368 input_data->len ) );
5369
5370 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5371 additional_data->len ) );
5372
5373 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5374 input_data->len, output_data,
5375 output_size, &output_length ) );
5376
5377 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5378 1, output_data,
5379 output_size, &output_length ),
5380 PSA_ERROR_INVALID_ARGUMENT );
5381
5382 psa_aead_abort( &operation );
5383
Paul Elliottc23a9a02021-06-21 18:32:46 +01005384 /* Test sending additional data after data. */
5385
Paul Elliottc23a9a02021-06-21 18:32:46 +01005386 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5387
5388 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5389
Andrzej Kurekad837522021-12-15 15:28:49 +01005390 if( operation.alg != PSA_ALG_CCM )
5391 {
5392 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5393 input_data->len, output_data,
5394 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005395
Andrzej Kurekad837522021-12-15 15:28:49 +01005396 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5397 additional_data->len ),
5398 PSA_ERROR_BAD_STATE );
5399 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005400 psa_aead_abort( &operation );
5401
Paul Elliott534d0b42021-06-22 19:15:20 +01005402 /* Test calling finish on decryption. */
5403
Paul Elliott534d0b42021-06-22 19:15:20 +01005404 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5405
5406 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5407
5408 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5409 finish_output_size,
5410 &output_part_length,
5411 tag_buffer, tag_length,
5412 &tag_size ),
5413 PSA_ERROR_BAD_STATE );
5414
5415 psa_aead_abort( &operation );
5416
5417 /* Test calling verify on encryption. */
5418
Paul Elliott534d0b42021-06-22 19:15:20 +01005419 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5420
5421 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5422
5423 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5424 finish_output_size,
5425 &output_part_length,
5426 tag_buffer,
5427 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005428 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005429
5430 psa_aead_abort( &operation );
5431
5432
Paul Elliottc23a9a02021-06-21 18:32:46 +01005433exit:
5434 psa_destroy_key( key );
5435 psa_aead_abort( &operation );
5436 mbedtls_free( output_data );
5437 mbedtls_free( final_data );
5438 PSA_DONE( );
5439}
5440/* END_CASE */
5441
5442/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005443void signature_size( int type_arg,
5444 int bits,
5445 int alg_arg,
5446 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005447{
5448 psa_key_type_t type = type_arg;
5449 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005450 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005451
Gilles Peskinefe11b722018-12-18 00:24:04 +01005452 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005453
Gilles Peskinee59236f2018-01-27 23:32:46 +01005454exit:
5455 ;
5456}
5457/* END_CASE */
5458
5459/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005460void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5461 int alg_arg, data_t *input_data,
5462 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005463{
Ronald Cron5425a212020-08-04 14:58:35 +02005464 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005465 psa_key_type_t key_type = key_type_arg;
5466 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005467 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005468 unsigned char *signature = NULL;
5469 size_t signature_size;
5470 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005472
Gilles Peskine8817f612018-12-18 00:18:46 +01005473 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005474
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005475 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005476 psa_set_key_algorithm( &attributes, alg );
5477 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005478
Gilles Peskine049c7532019-05-15 20:22:09 +02005479 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005480 &key ) );
5481 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005482 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005483
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005484 /* Allocate a buffer which has the size advertized by the
5485 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005486 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005487 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005488 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005489 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005490 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005491
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005492 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005493 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005494 input_data->x, input_data->len,
5495 signature, signature_size,
5496 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005497 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005498 ASSERT_COMPARE( output_data->x, output_data->len,
5499 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005500
5501exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005502 /*
5503 * Key attributes may have been returned by psa_get_key_attributes()
5504 * thus reset them as required.
5505 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005506 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005507
Ronald Cron5425a212020-08-04 14:58:35 +02005508 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005509 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005510 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005511}
5512/* END_CASE */
5513
5514/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005515void sign_hash_fail( int key_type_arg, data_t *key_data,
5516 int alg_arg, data_t *input_data,
5517 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005518{
Ronald Cron5425a212020-08-04 14:58:35 +02005519 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005520 psa_key_type_t key_type = key_type_arg;
5521 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005522 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005523 psa_status_t actual_status;
5524 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005525 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005526 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005528
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005529 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005530
Gilles Peskine8817f612018-12-18 00:18:46 +01005531 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005532
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005533 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005534 psa_set_key_algorithm( &attributes, alg );
5535 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005536
Gilles Peskine049c7532019-05-15 20:22:09 +02005537 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005538 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005539
Ronald Cron5425a212020-08-04 14:58:35 +02005540 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005541 input_data->x, input_data->len,
5542 signature, signature_size,
5543 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005544 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005545 /* The value of *signature_length is unspecified on error, but
5546 * whatever it is, it should be less than signature_size, so that
5547 * if the caller tries to read *signature_length bytes without
5548 * checking the error code then they don't overflow a buffer. */
5549 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005550
5551exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005552 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005553 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005554 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005555 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005556}
5557/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005558
5559/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005560void sign_verify_hash( int key_type_arg, data_t *key_data,
5561 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005562{
Ronald Cron5425a212020-08-04 14:58:35 +02005563 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005564 psa_key_type_t key_type = key_type_arg;
5565 psa_algorithm_t alg = alg_arg;
5566 size_t key_bits;
5567 unsigned char *signature = NULL;
5568 size_t signature_size;
5569 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005571
Gilles Peskine8817f612018-12-18 00:18:46 +01005572 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005573
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005574 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005575 psa_set_key_algorithm( &attributes, alg );
5576 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005577
Gilles Peskine049c7532019-05-15 20:22:09 +02005578 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005579 &key ) );
5580 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005581 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005582
5583 /* Allocate a buffer which has the size advertized by the
5584 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005585 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005586 key_bits, alg );
5587 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005588 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005589 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005590
5591 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005592 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005593 input_data->x, input_data->len,
5594 signature, signature_size,
5595 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005596 /* Check that the signature length looks sensible. */
5597 TEST_ASSERT( signature_length <= signature_size );
5598 TEST_ASSERT( signature_length > 0 );
5599
5600 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005601 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005602 input_data->x, input_data->len,
5603 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005604
5605 if( input_data->len != 0 )
5606 {
5607 /* Flip a bit in the input and verify that the signature is now
5608 * detected as invalid. Flip a bit at the beginning, not at the end,
5609 * because ECDSA may ignore the last few bits of the input. */
5610 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005611 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005612 input_data->x, input_data->len,
5613 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005614 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005615 }
5616
5617exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005618 /*
5619 * Key attributes may have been returned by psa_get_key_attributes()
5620 * thus reset them as required.
5621 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005622 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005623
Ronald Cron5425a212020-08-04 14:58:35 +02005624 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005625 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005626 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005627}
5628/* END_CASE */
5629
5630/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005631void verify_hash( int key_type_arg, data_t *key_data,
5632 int alg_arg, data_t *hash_data,
5633 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005634{
Ronald Cron5425a212020-08-04 14:58:35 +02005635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005636 psa_key_type_t key_type = key_type_arg;
5637 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005638 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005639
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005640 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005641
Gilles Peskine8817f612018-12-18 00:18:46 +01005642 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005643
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005644 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005645 psa_set_key_algorithm( &attributes, alg );
5646 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005647
Gilles Peskine049c7532019-05-15 20:22:09 +02005648 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005649 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005650
Ronald Cron5425a212020-08-04 14:58:35 +02005651 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005652 hash_data->x, hash_data->len,
5653 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005654
itayzafrir5c753392018-05-08 11:18:38 +03005655exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005656 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005657 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005658 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005659}
5660/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005661
5662/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005663void verify_hash_fail( int key_type_arg, data_t *key_data,
5664 int alg_arg, data_t *hash_data,
5665 data_t *signature_data,
5666 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005667{
Ronald Cron5425a212020-08-04 14:58:35 +02005668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005669 psa_key_type_t key_type = key_type_arg;
5670 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005671 psa_status_t actual_status;
5672 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005674
Gilles Peskine8817f612018-12-18 00:18:46 +01005675 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005676
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005677 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005678 psa_set_key_algorithm( &attributes, alg );
5679 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005680
Gilles Peskine049c7532019-05-15 20:22:09 +02005681 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005682 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005683
Ronald Cron5425a212020-08-04 14:58:35 +02005684 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005685 hash_data->x, hash_data->len,
5686 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005687 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005688
5689exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005690 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005691 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005692 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005693}
5694/* END_CASE */
5695
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005696/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005697void sign_message_deterministic( int key_type_arg,
5698 data_t *key_data,
5699 int alg_arg,
5700 data_t *input_data,
5701 data_t *output_data )
5702{
5703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5704 psa_key_type_t key_type = key_type_arg;
5705 psa_algorithm_t alg = alg_arg;
5706 size_t key_bits;
5707 unsigned char *signature = NULL;
5708 size_t signature_size;
5709 size_t signature_length = 0xdeadbeef;
5710 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5711
5712 PSA_ASSERT( psa_crypto_init( ) );
5713
5714 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5715 psa_set_key_algorithm( &attributes, alg );
5716 psa_set_key_type( &attributes, key_type );
5717
5718 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5719 &key ) );
5720 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5721 key_bits = psa_get_key_bits( &attributes );
5722
5723 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5724 TEST_ASSERT( signature_size != 0 );
5725 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5726 ASSERT_ALLOC( signature, signature_size );
5727
5728 PSA_ASSERT( psa_sign_message( key, alg,
5729 input_data->x, input_data->len,
5730 signature, signature_size,
5731 &signature_length ) );
5732
5733 ASSERT_COMPARE( output_data->x, output_data->len,
5734 signature, signature_length );
5735
5736exit:
5737 psa_reset_key_attributes( &attributes );
5738
5739 psa_destroy_key( key );
5740 mbedtls_free( signature );
5741 PSA_DONE( );
5742
5743}
5744/* END_CASE */
5745
5746/* BEGIN_CASE */
5747void sign_message_fail( int key_type_arg,
5748 data_t *key_data,
5749 int alg_arg,
5750 data_t *input_data,
5751 int signature_size_arg,
5752 int expected_status_arg )
5753{
5754 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5755 psa_key_type_t key_type = key_type_arg;
5756 psa_algorithm_t alg = alg_arg;
5757 size_t signature_size = signature_size_arg;
5758 psa_status_t actual_status;
5759 psa_status_t expected_status = expected_status_arg;
5760 unsigned char *signature = NULL;
5761 size_t signature_length = 0xdeadbeef;
5762 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5763
5764 ASSERT_ALLOC( signature, signature_size );
5765
5766 PSA_ASSERT( psa_crypto_init( ) );
5767
5768 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5769 psa_set_key_algorithm( &attributes, alg );
5770 psa_set_key_type( &attributes, key_type );
5771
5772 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5773 &key ) );
5774
5775 actual_status = psa_sign_message( key, alg,
5776 input_data->x, input_data->len,
5777 signature, signature_size,
5778 &signature_length );
5779 TEST_EQUAL( actual_status, expected_status );
5780 /* The value of *signature_length is unspecified on error, but
5781 * whatever it is, it should be less than signature_size, so that
5782 * if the caller tries to read *signature_length bytes without
5783 * checking the error code then they don't overflow a buffer. */
5784 TEST_ASSERT( signature_length <= signature_size );
5785
5786exit:
5787 psa_reset_key_attributes( &attributes );
5788 psa_destroy_key( key );
5789 mbedtls_free( signature );
5790 PSA_DONE( );
5791}
5792/* END_CASE */
5793
5794/* BEGIN_CASE */
5795void sign_verify_message( int key_type_arg,
5796 data_t *key_data,
5797 int alg_arg,
5798 data_t *input_data )
5799{
5800 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5801 psa_key_type_t key_type = key_type_arg;
5802 psa_algorithm_t alg = alg_arg;
5803 size_t key_bits;
5804 unsigned char *signature = NULL;
5805 size_t signature_size;
5806 size_t signature_length = 0xdeadbeef;
5807 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5808
5809 PSA_ASSERT( psa_crypto_init( ) );
5810
5811 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5812 PSA_KEY_USAGE_VERIFY_MESSAGE );
5813 psa_set_key_algorithm( &attributes, alg );
5814 psa_set_key_type( &attributes, key_type );
5815
5816 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5817 &key ) );
5818 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5819 key_bits = psa_get_key_bits( &attributes );
5820
5821 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5822 TEST_ASSERT( signature_size != 0 );
5823 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5824 ASSERT_ALLOC( signature, signature_size );
5825
5826 PSA_ASSERT( psa_sign_message( key, alg,
5827 input_data->x, input_data->len,
5828 signature, signature_size,
5829 &signature_length ) );
5830 TEST_ASSERT( signature_length <= signature_size );
5831 TEST_ASSERT( signature_length > 0 );
5832
5833 PSA_ASSERT( psa_verify_message( key, alg,
5834 input_data->x, input_data->len,
5835 signature, signature_length ) );
5836
5837 if( input_data->len != 0 )
5838 {
5839 /* Flip a bit in the input and verify that the signature is now
5840 * detected as invalid. Flip a bit at the beginning, not at the end,
5841 * because ECDSA may ignore the last few bits of the input. */
5842 input_data->x[0] ^= 1;
5843 TEST_EQUAL( psa_verify_message( key, alg,
5844 input_data->x, input_data->len,
5845 signature, signature_length ),
5846 PSA_ERROR_INVALID_SIGNATURE );
5847 }
5848
5849exit:
5850 psa_reset_key_attributes( &attributes );
5851
5852 psa_destroy_key( key );
5853 mbedtls_free( signature );
5854 PSA_DONE( );
5855}
5856/* END_CASE */
5857
5858/* BEGIN_CASE */
5859void verify_message( int key_type_arg,
5860 data_t *key_data,
5861 int alg_arg,
5862 data_t *input_data,
5863 data_t *signature_data )
5864{
5865 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5866 psa_key_type_t key_type = key_type_arg;
5867 psa_algorithm_t alg = alg_arg;
5868 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5869
5870 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5871
5872 PSA_ASSERT( psa_crypto_init( ) );
5873
5874 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5875 psa_set_key_algorithm( &attributes, alg );
5876 psa_set_key_type( &attributes, key_type );
5877
5878 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5879 &key ) );
5880
5881 PSA_ASSERT( psa_verify_message( key, alg,
5882 input_data->x, input_data->len,
5883 signature_data->x, signature_data->len ) );
5884
5885exit:
5886 psa_reset_key_attributes( &attributes );
5887 psa_destroy_key( key );
5888 PSA_DONE( );
5889}
5890/* END_CASE */
5891
5892/* BEGIN_CASE */
5893void verify_message_fail( int key_type_arg,
5894 data_t *key_data,
5895 int alg_arg,
5896 data_t *hash_data,
5897 data_t *signature_data,
5898 int expected_status_arg )
5899{
5900 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5901 psa_key_type_t key_type = key_type_arg;
5902 psa_algorithm_t alg = alg_arg;
5903 psa_status_t actual_status;
5904 psa_status_t expected_status = expected_status_arg;
5905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5906
5907 PSA_ASSERT( psa_crypto_init( ) );
5908
5909 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5910 psa_set_key_algorithm( &attributes, alg );
5911 psa_set_key_type( &attributes, key_type );
5912
5913 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5914 &key ) );
5915
5916 actual_status = psa_verify_message( key, alg,
5917 hash_data->x, hash_data->len,
5918 signature_data->x,
5919 signature_data->len );
5920 TEST_EQUAL( actual_status, expected_status );
5921
5922exit:
5923 psa_reset_key_attributes( &attributes );
5924 psa_destroy_key( key );
5925 PSA_DONE( );
5926}
5927/* END_CASE */
5928
5929/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005930void asymmetric_encrypt( int key_type_arg,
5931 data_t *key_data,
5932 int alg_arg,
5933 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005934 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005935 int expected_output_length_arg,
5936 int expected_status_arg )
5937{
Ronald Cron5425a212020-08-04 14:58:35 +02005938 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005939 psa_key_type_t key_type = key_type_arg;
5940 psa_algorithm_t alg = alg_arg;
5941 size_t expected_output_length = expected_output_length_arg;
5942 size_t key_bits;
5943 unsigned char *output = NULL;
5944 size_t output_size;
5945 size_t output_length = ~0;
5946 psa_status_t actual_status;
5947 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005949
Gilles Peskine8817f612018-12-18 00:18:46 +01005950 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005951
Gilles Peskine656896e2018-06-29 19:12:28 +02005952 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005953 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5954 psa_set_key_algorithm( &attributes, alg );
5955 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005956 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005957 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005958
5959 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005960 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005961 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005962
Gilles Peskine656896e2018-06-29 19:12:28 +02005963 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005964 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005965 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005966
5967 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005968 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005969 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005970 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005971 output, output_size,
5972 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005973 TEST_EQUAL( actual_status, expected_status );
5974 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005975
Gilles Peskine68428122018-06-30 18:42:41 +02005976 /* If the label is empty, the test framework puts a non-null pointer
5977 * in label->x. Test that a null pointer works as well. */
5978 if( label->len == 0 )
5979 {
5980 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005981 if( output_size != 0 )
5982 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005983 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005984 input_data->x, input_data->len,
5985 NULL, label->len,
5986 output, output_size,
5987 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005988 TEST_EQUAL( actual_status, expected_status );
5989 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005990 }
5991
Gilles Peskine656896e2018-06-29 19:12:28 +02005992exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005993 /*
5994 * Key attributes may have been returned by psa_get_key_attributes()
5995 * thus reset them as required.
5996 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005997 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005998
Ronald Cron5425a212020-08-04 14:58:35 +02005999 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006000 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006001 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006002}
6003/* END_CASE */
6004
6005/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006006void asymmetric_encrypt_decrypt( int key_type_arg,
6007 data_t *key_data,
6008 int alg_arg,
6009 data_t *input_data,
6010 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006011{
Ronald Cron5425a212020-08-04 14:58:35 +02006012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006013 psa_key_type_t key_type = key_type_arg;
6014 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006015 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006016 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006017 size_t output_size;
6018 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006019 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006020 size_t output2_size;
6021 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006022 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006023
Gilles Peskine8817f612018-12-18 00:18:46 +01006024 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006025
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006026 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6027 psa_set_key_algorithm( &attributes, alg );
6028 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006029
Gilles Peskine049c7532019-05-15 20:22:09 +02006030 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006031 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006032
6033 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006034 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006035 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006036
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006037 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006038 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006039 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006040
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006041 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006042 TEST_ASSERT( output2_size <=
6043 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6044 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006045 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006046
Gilles Peskineeebd7382018-06-08 18:11:54 +02006047 /* We test encryption by checking that encrypt-then-decrypt gives back
6048 * the original plaintext because of the non-optional random
6049 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006050 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006051 input_data->x, input_data->len,
6052 label->x, label->len,
6053 output, output_size,
6054 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006055 /* We don't know what ciphertext length to expect, but check that
6056 * it looks sensible. */
6057 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006058
Ronald Cron5425a212020-08-04 14:58:35 +02006059 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006060 output, output_length,
6061 label->x, label->len,
6062 output2, output2_size,
6063 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006064 ASSERT_COMPARE( input_data->x, input_data->len,
6065 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006066
6067exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006068 /*
6069 * Key attributes may have been returned by psa_get_key_attributes()
6070 * thus reset them as required.
6071 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006072 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006073
Ronald Cron5425a212020-08-04 14:58:35 +02006074 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006075 mbedtls_free( output );
6076 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006077 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006078}
6079/* END_CASE */
6080
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006081/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006082void asymmetric_decrypt( int key_type_arg,
6083 data_t *key_data,
6084 int alg_arg,
6085 data_t *input_data,
6086 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006087 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006088{
Ronald Cron5425a212020-08-04 14:58:35 +02006089 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006090 psa_key_type_t key_type = key_type_arg;
6091 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006092 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006093 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006094 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006095 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006097
Gilles Peskine8817f612018-12-18 00:18:46 +01006098 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006099
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6101 psa_set_key_algorithm( &attributes, alg );
6102 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006103
Gilles Peskine049c7532019-05-15 20:22:09 +02006104 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006105 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006106
gabor-mezei-armceface22021-01-21 12:26:17 +01006107 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6108 key_bits = psa_get_key_bits( &attributes );
6109
6110 /* Determine the maximum ciphertext length */
6111 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6112 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6113 ASSERT_ALLOC( output, output_size );
6114
Ronald Cron5425a212020-08-04 14:58:35 +02006115 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006116 input_data->x, input_data->len,
6117 label->x, label->len,
6118 output,
6119 output_size,
6120 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006121 ASSERT_COMPARE( expected_data->x, expected_data->len,
6122 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006123
Gilles Peskine68428122018-06-30 18:42:41 +02006124 /* If the label is empty, the test framework puts a non-null pointer
6125 * in label->x. Test that a null pointer works as well. */
6126 if( label->len == 0 )
6127 {
6128 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006129 if( output_size != 0 )
6130 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006131 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006132 input_data->x, input_data->len,
6133 NULL, label->len,
6134 output,
6135 output_size,
6136 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006137 ASSERT_COMPARE( expected_data->x, expected_data->len,
6138 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006139 }
6140
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006141exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006142 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006143 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006144 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006145 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006146}
6147/* END_CASE */
6148
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006149/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006150void asymmetric_decrypt_fail( int key_type_arg,
6151 data_t *key_data,
6152 int alg_arg,
6153 data_t *input_data,
6154 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006155 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006156 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006157{
Ronald Cron5425a212020-08-04 14:58:35 +02006158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006159 psa_key_type_t key_type = key_type_arg;
6160 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006161 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006162 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006163 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006164 psa_status_t actual_status;
6165 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006167
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006168 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006169
Gilles Peskine8817f612018-12-18 00:18:46 +01006170 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006171
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006172 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6173 psa_set_key_algorithm( &attributes, alg );
6174 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006175
Gilles Peskine049c7532019-05-15 20:22:09 +02006176 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006177 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006178
Ronald Cron5425a212020-08-04 14:58:35 +02006179 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006180 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006181 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006182 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006183 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006184 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006185 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006186
Gilles Peskine68428122018-06-30 18:42:41 +02006187 /* If the label is empty, the test framework puts a non-null pointer
6188 * in label->x. Test that a null pointer works as well. */
6189 if( label->len == 0 )
6190 {
6191 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006192 if( output_size != 0 )
6193 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006194 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006195 input_data->x, input_data->len,
6196 NULL, label->len,
6197 output, output_size,
6198 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006199 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006200 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006201 }
6202
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006203exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006204 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006205 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006206 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006207 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006208}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006209/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006210
6211/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006212void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006213{
6214 /* Test each valid way of initializing the object, except for `= {0}`, as
6215 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6216 * though it's OK by the C standard. We could test for this, but we'd need
6217 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006218 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006219 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6220 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6221 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006222
6223 memset( &zero, 0, sizeof( zero ) );
6224
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006225 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006226 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006227 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006228 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006229 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006230 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006231 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006232
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006233 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006234 PSA_ASSERT( psa_key_derivation_abort(&func) );
6235 PSA_ASSERT( psa_key_derivation_abort(&init) );
6236 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006237}
6238/* END_CASE */
6239
Janos Follath16de4a42019-06-13 16:32:24 +01006240/* BEGIN_CASE */
6241void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006242{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006243 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006244 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006245 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006246
Gilles Peskine8817f612018-12-18 00:18:46 +01006247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006248
Janos Follath16de4a42019-06-13 16:32:24 +01006249 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006250 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006251
6252exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006253 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006254 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006255}
6256/* END_CASE */
6257
Janos Follathaf3c2a02019-06-12 12:34:34 +01006258/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006259void derive_set_capacity( int alg_arg, int capacity_arg,
6260 int expected_status_arg )
6261{
6262 psa_algorithm_t alg = alg_arg;
6263 size_t capacity = capacity_arg;
6264 psa_status_t expected_status = expected_status_arg;
6265 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6266
6267 PSA_ASSERT( psa_crypto_init( ) );
6268
6269 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6270
6271 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6272 expected_status );
6273
6274exit:
6275 psa_key_derivation_abort( &operation );
6276 PSA_DONE( );
6277}
6278/* END_CASE */
6279
6280/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006281void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006282 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006283 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006284 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006285 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006286 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006287 int expected_status_arg3,
6288 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006289{
6290 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006291 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6292 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006293 psa_status_t expected_statuses[] = {expected_status_arg1,
6294 expected_status_arg2,
6295 expected_status_arg3};
6296 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006297 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6298 MBEDTLS_SVC_KEY_ID_INIT,
6299 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006300 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6301 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6302 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006303 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006304 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006305 psa_status_t expected_output_status = expected_output_status_arg;
6306 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006307
6308 PSA_ASSERT( psa_crypto_init( ) );
6309
6310 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6311 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006312
6313 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6314
6315 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6316 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006317 mbedtls_test_set_step( i );
6318 if( steps[i] == 0 )
6319 {
6320 /* Skip this step */
6321 }
6322 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006323 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006324 psa_set_key_type( &attributes, key_types[i] );
6325 PSA_ASSERT( psa_import_key( &attributes,
6326 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006327 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006328 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6329 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6330 {
6331 // When taking a private key as secret input, use key agreement
6332 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006333 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6334 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006335 expected_statuses[i] );
6336 }
6337 else
6338 {
6339 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006340 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006341 expected_statuses[i] );
6342 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006343 }
6344 else
6345 {
6346 TEST_EQUAL( psa_key_derivation_input_bytes(
6347 &operation, steps[i],
6348 inputs[i]->x, inputs[i]->len ),
6349 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006350 }
6351 }
6352
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006353 if( output_key_type != PSA_KEY_TYPE_NONE )
6354 {
6355 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006356 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006357 psa_set_key_bits( &attributes, 8 );
6358 actual_output_status =
6359 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006360 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006361 }
6362 else
6363 {
6364 uint8_t buffer[1];
6365 actual_output_status =
6366 psa_key_derivation_output_bytes( &operation,
6367 buffer, sizeof( buffer ) );
6368 }
6369 TEST_EQUAL( actual_output_status, expected_output_status );
6370
Janos Follathaf3c2a02019-06-12 12:34:34 +01006371exit:
6372 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006373 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6374 psa_destroy_key( keys[i] );
6375 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006376 PSA_DONE( );
6377}
6378/* END_CASE */
6379
Janos Follathd958bb72019-07-03 15:02:16 +01006380/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006381void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006382{
Janos Follathd958bb72019-07-03 15:02:16 +01006383 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006384 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006385 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006386 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006387 unsigned char input1[] = "Input 1";
6388 size_t input1_length = sizeof( input1 );
6389 unsigned char input2[] = "Input 2";
6390 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006391 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006392 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006393 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6394 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6395 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006397
Gilles Peskine8817f612018-12-18 00:18:46 +01006398 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006399
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006400 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6401 psa_set_key_algorithm( &attributes, alg );
6402 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006403
Gilles Peskine73676cb2019-05-15 20:15:10 +02006404 PSA_ASSERT( psa_import_key( &attributes,
6405 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006406 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006407
6408 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006409 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6410 input1, input1_length,
6411 input2, input2_length,
6412 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006413 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006414
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006415 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006416 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006417 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006418
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006419 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006420
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006421 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006422 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006423
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006424exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006425 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006426 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006427 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006428}
6429/* END_CASE */
6430
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006431/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006432void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006433{
6434 uint8_t output_buffer[16];
6435 size_t buffer_size = 16;
6436 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006437 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006438
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006439 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6440 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006441 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006442
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006443 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006444 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006445
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006446 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006447
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006448 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6449 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006450 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006451
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006452 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006453 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006454
6455exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006456 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006457}
6458/* END_CASE */
6459
6460/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006461void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006462 int step1_arg, data_t *input1,
6463 int step2_arg, data_t *input2,
6464 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006465 int requested_capacity_arg,
6466 data_t *expected_output1,
6467 data_t *expected_output2 )
6468{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006469 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006470 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6471 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006472 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6473 MBEDTLS_SVC_KEY_ID_INIT,
6474 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006475 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006476 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006477 uint8_t *expected_outputs[2] =
6478 {expected_output1->x, expected_output2->x};
6479 size_t output_sizes[2] =
6480 {expected_output1->len, expected_output2->len};
6481 size_t output_buffer_size = 0;
6482 uint8_t *output_buffer = NULL;
6483 size_t expected_capacity;
6484 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006486 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006487 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006488
6489 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6490 {
6491 if( output_sizes[i] > output_buffer_size )
6492 output_buffer_size = output_sizes[i];
6493 if( output_sizes[i] == 0 )
6494 expected_outputs[i] = NULL;
6495 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006496 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006497 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006498
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006499 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6500 psa_set_key_algorithm( &attributes, alg );
6501 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006502
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006503 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006504 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6505 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6506 requested_capacity ) );
6507 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006508 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006509 switch( steps[i] )
6510 {
6511 case 0:
6512 break;
6513 case PSA_KEY_DERIVATION_INPUT_SECRET:
6514 PSA_ASSERT( psa_import_key( &attributes,
6515 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006516 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006517
6518 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6519 {
6520 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6521 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6522 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6523 }
6524
Gilles Peskine1468da72019-05-29 17:35:49 +02006525 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006526 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006527 break;
6528 default:
6529 PSA_ASSERT( psa_key_derivation_input_bytes(
6530 &operation, steps[i],
6531 inputs[i]->x, inputs[i]->len ) );
6532 break;
6533 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006534 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006535
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006536 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006537 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006538 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006539 expected_capacity = requested_capacity;
6540
6541 /* Expansion phase. */
6542 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6543 {
6544 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006545 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006546 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006547 if( expected_capacity == 0 && output_sizes[i] == 0 )
6548 {
6549 /* Reading 0 bytes when 0 bytes are available can go either way. */
6550 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006551 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006552 continue;
6553 }
6554 else if( expected_capacity == 0 ||
6555 output_sizes[i] > expected_capacity )
6556 {
6557 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006558 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006559 expected_capacity = 0;
6560 continue;
6561 }
6562 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006563 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006564 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006565 ASSERT_COMPARE( output_buffer, output_sizes[i],
6566 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006567 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006568 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006569 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006570 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006571 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006572 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006573 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006574
6575exit:
6576 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006577 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006578 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6579 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006580 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006581}
6582/* END_CASE */
6583
6584/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006585void derive_full( int alg_arg,
6586 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006587 data_t *input1,
6588 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006589 int requested_capacity_arg )
6590{
Ronald Cron5425a212020-08-04 14:58:35 +02006591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006592 psa_algorithm_t alg = alg_arg;
6593 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006594 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006595 unsigned char output_buffer[16];
6596 size_t expected_capacity = requested_capacity;
6597 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006599
Gilles Peskine8817f612018-12-18 00:18:46 +01006600 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006601
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006602 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6603 psa_set_key_algorithm( &attributes, alg );
6604 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006605
Gilles Peskine049c7532019-05-15 20:22:09 +02006606 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006607 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006608
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006609 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6610 input1->x, input1->len,
6611 input2->x, input2->len,
6612 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006613 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006614
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006615 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006616 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006617 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006618
6619 /* Expansion phase. */
6620 while( current_capacity > 0 )
6621 {
6622 size_t read_size = sizeof( output_buffer );
6623 if( read_size > current_capacity )
6624 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006625 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006626 output_buffer,
6627 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006628 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006629 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006630 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006631 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006632 }
6633
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006634 /* Check that the operation refuses to go over capacity. */
6635 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006636 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006637
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006638 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006639
6640exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006641 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006642 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006643 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006644}
6645/* END_CASE */
6646
Janos Follathe60c9052019-07-03 13:51:30 +01006647/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006648void derive_key_exercise( int alg_arg,
6649 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006650 data_t *input1,
6651 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006652 int derived_type_arg,
6653 int derived_bits_arg,
6654 int derived_usage_arg,
6655 int derived_alg_arg )
6656{
Ronald Cron5425a212020-08-04 14:58:35 +02006657 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6658 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006659 psa_algorithm_t alg = alg_arg;
6660 psa_key_type_t derived_type = derived_type_arg;
6661 size_t derived_bits = derived_bits_arg;
6662 psa_key_usage_t derived_usage = derived_usage_arg;
6663 psa_algorithm_t derived_alg = derived_alg_arg;
6664 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006665 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006667 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006668
Gilles Peskine8817f612018-12-18 00:18:46 +01006669 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006670
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006671 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6672 psa_set_key_algorithm( &attributes, alg );
6673 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006674 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006675 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006676
6677 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006678 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6679 input1->x, input1->len,
6680 input2->x, input2->len,
6681 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006682 goto exit;
6683
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006684 psa_set_key_usage_flags( &attributes, derived_usage );
6685 psa_set_key_algorithm( &attributes, derived_alg );
6686 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006687 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006688 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006689 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006690
6691 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006692 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006693 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6694 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006695
6696 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006697 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006698 goto exit;
6699
6700exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006701 /*
6702 * Key attributes may have been returned by psa_get_key_attributes()
6703 * thus reset them as required.
6704 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006705 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006706
6707 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006708 psa_destroy_key( base_key );
6709 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006710 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006711}
6712/* END_CASE */
6713
Janos Follath42fd8882019-07-03 14:17:09 +01006714/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006715void derive_key_export( int alg_arg,
6716 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006717 data_t *input1,
6718 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006719 int bytes1_arg,
6720 int bytes2_arg )
6721{
Ronald Cron5425a212020-08-04 14:58:35 +02006722 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6723 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006724 psa_algorithm_t alg = alg_arg;
6725 size_t bytes1 = bytes1_arg;
6726 size_t bytes2 = bytes2_arg;
6727 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006728 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006729 uint8_t *output_buffer = NULL;
6730 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006731 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6732 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006733 size_t length;
6734
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006735 ASSERT_ALLOC( output_buffer, capacity );
6736 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006737 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006738
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006739 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6740 psa_set_key_algorithm( &base_attributes, alg );
6741 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006742 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006743 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006744
6745 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006746 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6747 input1->x, input1->len,
6748 input2->x, input2->len,
6749 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006750 goto exit;
6751
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006752 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006753 output_buffer,
6754 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006755 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006756
6757 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006758 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6759 input1->x, input1->len,
6760 input2->x, input2->len,
6761 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006762 goto exit;
6763
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006764 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6765 psa_set_key_algorithm( &derived_attributes, 0 );
6766 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006767 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006768 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006769 &derived_key ) );
6770 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006771 export_buffer, bytes1,
6772 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006773 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006774 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006775 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006776 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006777 &derived_key ) );
6778 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006779 export_buffer + bytes1, bytes2,
6780 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006781 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006782
6783 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006784 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6785 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006786
6787exit:
6788 mbedtls_free( output_buffer );
6789 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006790 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006791 psa_destroy_key( base_key );
6792 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006793 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006794}
6795/* END_CASE */
6796
6797/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006798void derive_key( int alg_arg,
6799 data_t *key_data, data_t *input1, data_t *input2,
6800 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006801 int expected_status_arg,
6802 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006803{
Ronald Cron5425a212020-08-04 14:58:35 +02006804 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6805 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006806 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006807 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006808 size_t bits = bits_arg;
6809 psa_status_t expected_status = expected_status_arg;
6810 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6811 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6812 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6813
6814 PSA_ASSERT( psa_crypto_init( ) );
6815
6816 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6817 psa_set_key_algorithm( &base_attributes, alg );
6818 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6819 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006820 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006821
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006822 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6823 input1->x, input1->len,
6824 input2->x, input2->len,
6825 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006826 goto exit;
6827
6828 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6829 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006830 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006831 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006832
6833 psa_status_t status =
6834 psa_key_derivation_output_key( &derived_attributes,
6835 &operation,
6836 &derived_key );
6837 if( is_large_output > 0 )
6838 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6839 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006840
6841exit:
6842 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006843 psa_destroy_key( base_key );
6844 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006845 PSA_DONE( );
6846}
6847/* END_CASE */
6848
6849/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006850void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006851 int our_key_type_arg, int our_key_alg_arg,
6852 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006853 int expected_status_arg )
6854{
Ronald Cron5425a212020-08-04 14:58:35 +02006855 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006856 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006857 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006858 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006859 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006861 psa_status_t expected_status = expected_status_arg;
6862 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006863
Gilles Peskine8817f612018-12-18 00:18:46 +01006864 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006865
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006866 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006867 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006868 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006869 PSA_ASSERT( psa_import_key( &attributes,
6870 our_key_data->x, our_key_data->len,
6871 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006872
Gilles Peskine77f40d82019-04-11 21:27:06 +02006873 /* The tests currently include inputs that should fail at either step.
6874 * Test cases that fail at the setup step should be changed to call
6875 * key_derivation_setup instead, and this function should be renamed
6876 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006877 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006878 if( status == PSA_SUCCESS )
6879 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006880 TEST_EQUAL( psa_key_derivation_key_agreement(
6881 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6882 our_key,
6883 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006884 expected_status );
6885 }
6886 else
6887 {
6888 TEST_ASSERT( status == expected_status );
6889 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006890
6891exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006892 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006893 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006894 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006895}
6896/* END_CASE */
6897
6898/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006899void raw_key_agreement( int alg_arg,
6900 int our_key_type_arg, data_t *our_key_data,
6901 data_t *peer_key_data,
6902 data_t *expected_output )
6903{
Ronald Cron5425a212020-08-04 14:58:35 +02006904 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006905 psa_algorithm_t alg = alg_arg;
6906 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006908 unsigned char *output = NULL;
6909 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006910 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006911
6912 ASSERT_ALLOC( output, expected_output->len );
6913 PSA_ASSERT( psa_crypto_init( ) );
6914
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006915 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6916 psa_set_key_algorithm( &attributes, alg );
6917 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006918 PSA_ASSERT( psa_import_key( &attributes,
6919 our_key_data->x, our_key_data->len,
6920 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006921
gabor-mezei-armceface22021-01-21 12:26:17 +01006922 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6923 key_bits = psa_get_key_bits( &attributes );
6924
Gilles Peskinebe697d82019-05-16 18:00:41 +02006925 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6926 peer_key_data->x, peer_key_data->len,
6927 output, expected_output->len,
6928 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006929 ASSERT_COMPARE( output, output_length,
6930 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006931 TEST_ASSERT( output_length <=
6932 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6933 TEST_ASSERT( output_length <=
6934 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006935
6936exit:
6937 mbedtls_free( output );
6938 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006939 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006940}
6941/* END_CASE */
6942
6943/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006944void key_agreement_capacity( int alg_arg,
6945 int our_key_type_arg, data_t *our_key_data,
6946 data_t *peer_key_data,
6947 int expected_capacity_arg )
6948{
Ronald Cron5425a212020-08-04 14:58:35 +02006949 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006950 psa_algorithm_t alg = alg_arg;
6951 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006952 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006954 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006955 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006956
Gilles Peskine8817f612018-12-18 00:18:46 +01006957 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006958
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6960 psa_set_key_algorithm( &attributes, alg );
6961 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006962 PSA_ASSERT( psa_import_key( &attributes,
6963 our_key_data->x, our_key_data->len,
6964 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006965
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006966 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006967 PSA_ASSERT( psa_key_derivation_key_agreement(
6968 &operation,
6969 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6970 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006971 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6972 {
6973 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006974 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006975 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006976 NULL, 0 ) );
6977 }
Gilles Peskine59685592018-09-18 12:11:34 +02006978
Gilles Peskinebf491972018-10-25 22:36:12 +02006979 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006980 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006981 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006982 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006983
Gilles Peskinebf491972018-10-25 22:36:12 +02006984 /* Test the actual capacity by reading the output. */
6985 while( actual_capacity > sizeof( output ) )
6986 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006987 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006988 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006989 actual_capacity -= sizeof( output );
6990 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006991 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006992 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006993 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006994 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006995
Gilles Peskine59685592018-09-18 12:11:34 +02006996exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006997 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006998 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006999 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007000}
7001/* END_CASE */
7002
7003/* BEGIN_CASE */
7004void key_agreement_output( int alg_arg,
7005 int our_key_type_arg, data_t *our_key_data,
7006 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007007 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007008{
Ronald Cron5425a212020-08-04 14:58:35 +02007009 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007010 psa_algorithm_t alg = alg_arg;
7011 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007012 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007014 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007015
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007016 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7017 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007018
Gilles Peskine8817f612018-12-18 00:18:46 +01007019 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007020
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007021 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7022 psa_set_key_algorithm( &attributes, alg );
7023 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007024 PSA_ASSERT( psa_import_key( &attributes,
7025 our_key_data->x, our_key_data->len,
7026 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007027
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007028 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007029 PSA_ASSERT( psa_key_derivation_key_agreement(
7030 &operation,
7031 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7032 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007033 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7034 {
7035 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007036 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007037 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007038 NULL, 0 ) );
7039 }
Gilles Peskine59685592018-09-18 12:11:34 +02007040
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007041 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007042 actual_output,
7043 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007044 ASSERT_COMPARE( actual_output, expected_output1->len,
7045 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007046 if( expected_output2->len != 0 )
7047 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007048 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007049 actual_output,
7050 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007051 ASSERT_COMPARE( actual_output, expected_output2->len,
7052 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007053 }
Gilles Peskine59685592018-09-18 12:11:34 +02007054
7055exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007056 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007057 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007058 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007059 mbedtls_free( actual_output );
7060}
7061/* END_CASE */
7062
7063/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007064void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007065{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007066 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007067 unsigned char *output = NULL;
7068 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007069 size_t i;
7070 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007071
Simon Butcher49f8e312020-03-03 15:51:50 +00007072 TEST_ASSERT( bytes_arg >= 0 );
7073
Gilles Peskine91892022021-02-08 19:50:26 +01007074 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007075 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007076
Gilles Peskine8817f612018-12-18 00:18:46 +01007077 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007078
Gilles Peskinea50d7392018-06-21 10:22:13 +02007079 /* Run several times, to ensure that every output byte will be
7080 * nonzero at least once with overwhelming probability
7081 * (2^(-8*number_of_runs)). */
7082 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007083 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007084 if( bytes != 0 )
7085 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007086 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007087
Gilles Peskinea50d7392018-06-21 10:22:13 +02007088 for( i = 0; i < bytes; i++ )
7089 {
7090 if( output[i] != 0 )
7091 ++changed[i];
7092 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007093 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007094
7095 /* Check that every byte was changed to nonzero at least once. This
7096 * validates that psa_generate_random is overwriting every byte of
7097 * the output buffer. */
7098 for( i = 0; i < bytes; i++ )
7099 {
7100 TEST_ASSERT( changed[i] != 0 );
7101 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007102
7103exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007104 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007105 mbedtls_free( output );
7106 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007107}
7108/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007109
7110/* BEGIN_CASE */
7111void generate_key( int type_arg,
7112 int bits_arg,
7113 int usage_arg,
7114 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007115 int expected_status_arg,
7116 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007117{
Ronald Cron5425a212020-08-04 14:58:35 +02007118 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007119 psa_key_type_t type = type_arg;
7120 psa_key_usage_t usage = usage_arg;
7121 size_t bits = bits_arg;
7122 psa_algorithm_t alg = alg_arg;
7123 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007125 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007126
Gilles Peskine8817f612018-12-18 00:18:46 +01007127 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007128
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007129 psa_set_key_usage_flags( &attributes, usage );
7130 psa_set_key_algorithm( &attributes, alg );
7131 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007132 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007133
7134 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007135 psa_status_t status = psa_generate_key( &attributes, &key );
7136
7137 if( is_large_key > 0 )
7138 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7139 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007140 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007141 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007142
7143 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007144 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007145 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7146 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007147
Gilles Peskine818ca122018-06-20 18:16:48 +02007148 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007149 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007150 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007151
7152exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007153 /*
7154 * Key attributes may have been returned by psa_get_key_attributes()
7155 * thus reset them as required.
7156 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007157 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007158
Ronald Cron5425a212020-08-04 14:58:35 +02007159 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007160 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007161}
7162/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007163
Ronald Cronee414c72021-03-18 18:50:08 +01007164/* 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 +02007165void generate_key_rsa( int bits_arg,
7166 data_t *e_arg,
7167 int expected_status_arg )
7168{
Ronald Cron5425a212020-08-04 14:58:35 +02007169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007170 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007171 size_t bits = bits_arg;
7172 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7173 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7174 psa_status_t expected_status = expected_status_arg;
7175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7176 uint8_t *exported = NULL;
7177 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007178 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007179 size_t exported_length = SIZE_MAX;
7180 uint8_t *e_read_buffer = NULL;
7181 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007182 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007183 size_t e_read_length = SIZE_MAX;
7184
7185 if( e_arg->len == 0 ||
7186 ( e_arg->len == 3 &&
7187 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7188 {
7189 is_default_public_exponent = 1;
7190 e_read_size = 0;
7191 }
7192 ASSERT_ALLOC( e_read_buffer, e_read_size );
7193 ASSERT_ALLOC( exported, exported_size );
7194
7195 PSA_ASSERT( psa_crypto_init( ) );
7196
7197 psa_set_key_usage_flags( &attributes, usage );
7198 psa_set_key_algorithm( &attributes, alg );
7199 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7200 e_arg->x, e_arg->len ) );
7201 psa_set_key_bits( &attributes, bits );
7202
7203 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007204 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007205 if( expected_status != PSA_SUCCESS )
7206 goto exit;
7207
7208 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007209 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007210 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7211 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7212 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7213 e_read_buffer, e_read_size,
7214 &e_read_length ) );
7215 if( is_default_public_exponent )
7216 TEST_EQUAL( e_read_length, 0 );
7217 else
7218 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7219
7220 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007221 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007222 goto exit;
7223
7224 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007225 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007226 exported, exported_size,
7227 &exported_length ) );
7228 {
7229 uint8_t *p = exported;
7230 uint8_t *end = exported + exported_length;
7231 size_t len;
7232 /* RSAPublicKey ::= SEQUENCE {
7233 * modulus INTEGER, -- n
7234 * publicExponent INTEGER } -- e
7235 */
7236 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007237 MBEDTLS_ASN1_SEQUENCE |
7238 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007239 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007240 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7241 MBEDTLS_ASN1_INTEGER ) );
7242 if( len >= 1 && p[0] == 0 )
7243 {
7244 ++p;
7245 --len;
7246 }
7247 if( e_arg->len == 0 )
7248 {
7249 TEST_EQUAL( len, 3 );
7250 TEST_EQUAL( p[0], 1 );
7251 TEST_EQUAL( p[1], 0 );
7252 TEST_EQUAL( p[2], 1 );
7253 }
7254 else
7255 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7256 }
7257
7258exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007259 /*
7260 * Key attributes may have been returned by psa_get_key_attributes() or
7261 * set by psa_set_key_domain_parameters() thus reset them as required.
7262 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007263 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007264
Ronald Cron5425a212020-08-04 14:58:35 +02007265 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007266 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007267 mbedtls_free( e_read_buffer );
7268 mbedtls_free( exported );
7269}
7270/* END_CASE */
7271
Darryl Greend49a4992018-06-18 17:27:26 +01007272/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007273void persistent_key_load_key_from_storage( data_t *data,
7274 int type_arg, int bits_arg,
7275 int usage_flags_arg, int alg_arg,
7276 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007277{
Ronald Cron71016a92020-08-28 19:01:50 +02007278 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007280 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7281 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007282 psa_key_type_t type = type_arg;
7283 size_t bits = bits_arg;
7284 psa_key_usage_t usage_flags = usage_flags_arg;
7285 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007286 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007287 unsigned char *first_export = NULL;
7288 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007289 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007290 size_t first_exported_length;
7291 size_t second_exported_length;
7292
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007293 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7294 {
7295 ASSERT_ALLOC( first_export, export_size );
7296 ASSERT_ALLOC( second_export, export_size );
7297 }
Darryl Greend49a4992018-06-18 17:27:26 +01007298
Gilles Peskine8817f612018-12-18 00:18:46 +01007299 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007300
Gilles Peskinec87af662019-05-15 16:12:22 +02007301 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007302 psa_set_key_usage_flags( &attributes, usage_flags );
7303 psa_set_key_algorithm( &attributes, alg );
7304 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007305 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007306
Darryl Green0c6575a2018-11-07 16:05:30 +00007307 switch( generation_method )
7308 {
7309 case IMPORT_KEY:
7310 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007311 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007312 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007313 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007314
Darryl Green0c6575a2018-11-07 16:05:30 +00007315 case GENERATE_KEY:
7316 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007317 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007318 break;
7319
7320 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007321#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007322 {
7323 /* Create base key */
7324 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7325 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7326 psa_set_key_usage_flags( &base_attributes,
7327 PSA_KEY_USAGE_DERIVE );
7328 psa_set_key_algorithm( &base_attributes, derive_alg );
7329 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007330 PSA_ASSERT( psa_import_key( &base_attributes,
7331 data->x, data->len,
7332 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007333 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007334 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007335 PSA_ASSERT( psa_key_derivation_input_key(
7336 &operation,
7337 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007338 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007339 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007340 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007341 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7342 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007343 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007344 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007345 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007346 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007347 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007348#else
7349 TEST_ASSUME( ! "KDF not supported in this configuration" );
7350#endif
7351 break;
7352
7353 default:
7354 TEST_ASSERT( ! "generation_method not implemented in test" );
7355 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007356 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007357 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007358
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007359 /* Export the key if permitted by the key policy. */
7360 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7361 {
Ronald Cron5425a212020-08-04 14:58:35 +02007362 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007363 first_export, export_size,
7364 &first_exported_length ) );
7365 if( generation_method == IMPORT_KEY )
7366 ASSERT_COMPARE( data->x, data->len,
7367 first_export, first_exported_length );
7368 }
Darryl Greend49a4992018-06-18 17:27:26 +01007369
7370 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007371 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007372 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007373 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007374
Darryl Greend49a4992018-06-18 17:27:26 +01007375 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007376 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007377 TEST_ASSERT( mbedtls_svc_key_id_equal(
7378 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007379 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7380 PSA_KEY_LIFETIME_PERSISTENT );
7381 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7382 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007383 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007384 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007385 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007386
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007387 /* Export the key again if permitted by the key policy. */
7388 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007389 {
Ronald Cron5425a212020-08-04 14:58:35 +02007390 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007391 second_export, export_size,
7392 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007393 ASSERT_COMPARE( first_export, first_exported_length,
7394 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007395 }
7396
7397 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007398 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007399 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007400
7401exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007402 /*
7403 * Key attributes may have been returned by psa_get_key_attributes()
7404 * thus reset them as required.
7405 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007406 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007407
Darryl Greend49a4992018-06-18 17:27:26 +01007408 mbedtls_free( first_export );
7409 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007410 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007411 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007412 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007413 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007414}
7415/* END_CASE */