blob: 7a9c2160b837ceb60f238ace7ea4d48a7606a4df [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010024
Gilles Peskine4023c012021-05-27 13:21:20 +020025/* If this comes up, it's a bug in the test code or in the test data. */
26#define UNUSED 0xdeadbeef
27
Dave Rodgman647791d2021-06-23 12:49:59 +010028/* Assert that an operation is (not) active.
29 * This serves as a proxy for checking if the operation is aborted. */
30#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
31#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010032
33/** An invalid export length that will never be set by psa_export_key(). */
34static const size_t INVALID_EXPORT_LENGTH = ~0U;
35
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036/** Test if a buffer contains a constant byte value.
37 *
38 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 *
40 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042 * \param size Size of the buffer in bytes.
43 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 * \return 1 if the buffer is all-bits-zero.
45 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048{
49 size_t i;
50 for( i = 0; i < size; i++ )
51 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020052 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020055 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020056}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010057#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
59static int asn1_write_10x( unsigned char **p,
60 unsigned char *start,
61 size_t bits,
62 unsigned char x )
63{
64 int ret;
65 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020066 if( bits == 0 )
67 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
68 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020069 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030070 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020071 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
72 *p -= len;
73 ( *p )[len-1] = x;
74 if( bits % 8 == 0 )
75 ( *p )[1] |= 1;
76 else
77 ( *p )[0] |= 1 << ( bits % 8 );
78 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
80 MBEDTLS_ASN1_INTEGER ) );
81 return( len );
82}
83
84static int construct_fake_rsa_key( unsigned char *buffer,
85 size_t buffer_size,
86 unsigned char **p,
87 size_t bits,
88 int keypair )
89{
90 size_t half_bits = ( bits + 1 ) / 2;
91 int ret;
92 int len = 0;
93 /* Construct something that looks like a DER encoding of
94 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
95 * RSAPrivateKey ::= SEQUENCE {
96 * version Version,
97 * modulus INTEGER, -- n
98 * publicExponent INTEGER, -- e
99 * privateExponent INTEGER, -- d
100 * prime1 INTEGER, -- p
101 * prime2 INTEGER, -- q
102 * exponent1 INTEGER, -- d mod (p-1)
103 * exponent2 INTEGER, -- d mod (q-1)
104 * coefficient INTEGER, -- (inverse of q) mod p
105 * otherPrimeInfos OtherPrimeInfos OPTIONAL
106 * }
107 * Or, for a public key, the same structure with only
108 * version, modulus and publicExponent.
109 */
110 *p = buffer + buffer_size;
111 if( keypair )
112 {
113 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
116 asn1_write_10x( p, buffer, half_bits, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
118 asn1_write_10x( p, buffer, half_bits, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* q */
120 asn1_write_10x( p, buffer, half_bits, 1 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x( p, buffer, half_bits, 3 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* d */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 }
126 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
127 asn1_write_10x( p, buffer, 17, 1 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, /* n */
129 asn1_write_10x( p, buffer, bits, 1 ) );
130 if( keypair )
131 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
132 mbedtls_asn1_write_int( p, buffer, 0 ) );
133 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
134 {
135 const unsigned char tag =
136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
137 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
138 }
139 return( len );
140}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100141#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100143int exercise_mac_setup( psa_key_type_t key_type,
144 const unsigned char *key_bytes,
145 size_t key_length,
146 psa_algorithm_t alg,
147 psa_mac_operation_t *operation,
148 psa_status_t *status )
149{
Ronald Cron5425a212020-08-04 14:58:35 +0200150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200154 psa_set_key_algorithm( &attributes, alg );
155 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200156 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157
Ronald Cron5425a212020-08-04 14:58:35 +0200158 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100159 /* Whether setup succeeded or failed, abort must succeed. */
160 PSA_ASSERT( psa_mac_abort( operation ) );
161 /* If setup failed, reproduce the failure, so that the caller can
162 * test the resulting state of the operation object. */
163 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 {
Ronald Cron5425a212020-08-04 14:58:35 +0200165 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 }
167
Ronald Cron5425a212020-08-04 14:58:35 +0200168 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100169 return( 1 );
170
171exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200172 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173 return( 0 );
174}
175
176int exercise_cipher_setup( psa_key_type_t key_type,
177 const unsigned char *key_bytes,
178 size_t key_length,
179 psa_algorithm_t alg,
180 psa_cipher_operation_t *operation,
181 psa_status_t *status )
182{
Ronald Cron5425a212020-08-04 14:58:35 +0200183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
187 psa_set_key_algorithm( &attributes, alg );
188 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200189 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190
Ronald Cron5425a212020-08-04 14:58:35 +0200191 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* Whether setup succeeded or failed, abort must succeed. */
193 PSA_ASSERT( psa_cipher_abort( operation ) );
194 /* If setup failed, reproduce the failure, so that the caller can
195 * test the resulting state of the operation object. */
196 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 {
Ronald Cron5425a212020-08-04 14:58:35 +0200198 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100199 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 }
201
Ronald Cron5425a212020-08-04 14:58:35 +0200202 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203 return( 1 );
204
205exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200206 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207 return( 0 );
208}
209
Ronald Cron5425a212020-08-04 14:58:35 +0200210static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211{
212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 uint8_t buffer[1];
215 size_t length;
216 int ok = 0;
217
Ronald Cronecfb2372020-07-23 17:13:42 +0200218 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
220 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
221 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200224 TEST_EQUAL(
225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
226 TEST_EQUAL(
227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200228 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200229 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
233
Ronald Cron5425a212020-08-04 14:58:35 +0200234 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000235 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200236 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000238 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 ok = 1;
241
242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243 /*
244 * Key attributes may have been returned by psa_get_key_attributes()
245 * thus reset them as required.
246 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200247 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100248
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200249 return( ok );
250}
251
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252/* Assert that a key isn't reported as having a slot number. */
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
255 do \
256 { \
257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
258 TEST_EQUAL( psa_get_key_slot_number( \
259 attributes, \
260 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
261 PSA_ERROR_INVALID_ARGUMENT ); \
262 } \
263 while( 0 )
264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
265#define ASSERT_NO_SLOT_NUMBER( attributes ) \
266 ( (void) 0 )
267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100269/* An overapproximation of the amount of storage needed for a key of the
270 * given type and with the given content. The API doesn't make it easy
271 * to find a good value for the size. The current implementation doesn't
272 * care about the value anyway. */
273#define KEY_BITS_FROM_DATA( type, data ) \
274 ( data )->len
275
Darryl Green0c6575a2018-11-07 16:05:30 +0000276typedef enum {
277 IMPORT_KEY = 0,
278 GENERATE_KEY = 1,
279 DERIVE_KEY = 2
280} generate_method;
281
Paul Elliott33746aa2021-09-15 16:40:40 +0100282typedef enum
283{
284 DO_NOT_SET_LENGTHS = 0,
285 SET_LENGTHS_BEFORE_NONCE = 1,
286 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100287} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100288
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100289typedef enum
290{
291 USE_NULL_TAG = 0,
292 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100293} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100294
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100295/*!
296 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100297 * \param key_type_arg Type of key passed in
298 * \param key_data The encryption / decryption key data
299 * \param alg_arg The type of algorithm used
300 * \param nonce Nonce data
301 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100302 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 * feed additional data in to be encrypted /
304 * decrypted. If -1, no chunking.
305 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100306 * \param data_part_len_arg If not -1, the length of chunks to feed
307 * the data in to be encrypted / decrypted. If
308 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100309 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100310 * expected here, this controls whether or not
311 * to set lengths, and in what order with
312 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100315 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100316 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 */
319static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
320 int alg_arg,
321 data_t *nonce,
322 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100323 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100325 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100326 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100328 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100329 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100330{
331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
332 psa_key_type_t key_type = key_type_arg;
333 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100334 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100335 unsigned char *output_data = NULL;
336 unsigned char *part_data = NULL;
337 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100339 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 size_t output_size = 0;
341 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 size_t output_length = 0;
343 size_t key_bits = 0;
344 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100345 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100346 size_t part_length = 0;
347 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100349 size_t ad_part_len = 0;
350 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100351 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100356 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100357
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 PSA_ASSERT( psa_crypto_init( ) );
359
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 if( is_encrypt )
361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
362 else
363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
364
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 psa_set_key_algorithm( &attributes, alg );
366 psa_set_key_type( &attributes, key_type );
367
368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
369 &key ) );
370
371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
372 key_bits = psa_get_key_bits( &attributes );
373
374 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 if( is_encrypt )
377 {
378 /* Tag gets written at end of buffer. */
379 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
380 ( input_data->len +
381 tag_length ) );
382 data_true_size = input_data->len;
383 }
384 else
385 {
386 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
387 ( input_data->len -
388 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 /* Do not want to attempt to decrypt tag. */
391 data_true_size = input_data->len - tag_length;
392 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 ASSERT_ALLOC( output_data, output_size );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396 if( is_encrypt )
397 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100398 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
399 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 }
401 else
402 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100403 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
404 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100405 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100407 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100408
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100409 if( is_encrypt )
410 status = psa_aead_encrypt_setup( &operation, key, alg );
411 else
412 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
414 /* If the operation is not supported, just skip and not fail in case the
415 * encryption involves a common limitation of cryptography hardwares and
416 * an alternative implementation. */
417 if( status == PSA_ERROR_NOT_SUPPORTED )
418 {
419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
421 }
422
423 PSA_ASSERT( status );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 if( set_lengths_method == DO_NOT_SET_LENGTHS )
426 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
427 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100428 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100429 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
430 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
432 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100433 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100434 {
435 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
436
Paul Elliott33746aa2021-09-15 16:40:40 +0100437 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
438 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100441 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
443 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100444 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100446 for( part_offset = 0, part_count = 0;
447 part_offset < additional_data->len;
448 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100450 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 {
Paul Elliott329d5382021-07-22 17:10:45 +0100452 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100453 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100454 else if( additional_data->len - part_offset < ad_part_len )
455 {
456 part_length = additional_data->len - part_offset;
457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 else
459 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100460 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
462
463 PSA_ASSERT( psa_aead_update_ad( &operation,
464 additional_data->x + part_offset,
465 part_length ) );
466
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 }
468 }
469 else
470 {
471 /* Pass additional data in one go. */
472 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
473 additional_data->len ) );
474 }
475
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100476 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
478 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100479 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100481 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
483 ASSERT_ALLOC( part_data, part_data_size );
484
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100485 for( part_offset = 0, part_count = 0;
486 part_offset < data_true_size;
487 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100489 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 {
Paul Elliott329d5382021-07-22 17:10:45 +0100491 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100493 else if( ( data_true_size - part_offset ) < data_part_len )
494 {
495 part_length = ( data_true_size - part_offset );
496 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 else
498 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
502 PSA_ASSERT( psa_aead_update( &operation,
503 ( input_data->x + part_offset ),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length ) );
507
508 if( output_data && output_part_length )
509 {
510 memcpy( ( output_data + part_offset ), part_data,
511 output_part_length );
512 }
513
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 output_length += output_part_length;
515 }
516 }
517 else
518 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 output_size, &output_length ) );
523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( is_encrypt )
526 PSA_ASSERT( psa_aead_finish( &operation, final_data,
527 final_output_size,
528 &output_part_length,
529 tag_buffer, tag_length,
530 &tag_size ) );
531 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100532 {
Paul Elliott9961a662021-09-17 19:19:02 +0100533 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 final_output_size,
535 &output_part_length,
536 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100537 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100540 if( output_data && output_part_length )
541 memcpy( ( output_data + output_length ), final_data,
542 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
549 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551 TEST_EQUAL( tag_length, tag_size );
552
553 if( output_data && tag_length )
554 memcpy( ( output_data + output_length ), tag_buffer,
555 tag_length );
556
557 output_length += tag_length;
558
559 TEST_EQUAL( output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_ASSERT( output_length <=
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
570 TEST_ASSERT( output_length <=
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Gilles Peskinee59236f2018-01-27 23:32:46 +0100592/* END_HEADER */
593
594/* BEGIN_DEPENDENCIES
595 * depends_on:MBEDTLS_PSA_CRYPTO_C
596 * END_DEPENDENCIES
597 */
598
599/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200600void static_checks( )
601{
602 size_t max_truncated_mac_size =
603 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
604
605 /* Check that the length for a truncated MAC always fits in the algorithm
606 * encoding. The shifted mask is the maximum truncated value. The
607 * untruncated algorithm may be one byte larger. */
608 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
609}
610/* END_CASE */
611
612/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200613void import_with_policy( int type_arg,
614 int usage_arg, int alg_arg,
615 int expected_status_arg )
616{
617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
618 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200620 psa_key_type_t type = type_arg;
621 psa_key_usage_t usage = usage_arg;
622 psa_algorithm_t alg = alg_arg;
623 psa_status_t expected_status = expected_status_arg;
624 const uint8_t key_material[16] = {0};
625 psa_status_t status;
626
627 PSA_ASSERT( psa_crypto_init( ) );
628
629 psa_set_key_type( &attributes, type );
630 psa_set_key_usage_flags( &attributes, usage );
631 psa_set_key_algorithm( &attributes, alg );
632
633 status = psa_import_key( &attributes,
634 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200635 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636 TEST_EQUAL( status, expected_status );
637 if( status != PSA_SUCCESS )
638 goto exit;
639
Ronald Cron5425a212020-08-04 14:58:35 +0200640 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200642 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200643 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200644 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200645 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200646
Ronald Cron5425a212020-08-04 14:58:35 +0200647 PSA_ASSERT( psa_destroy_key( key ) );
648 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200649
650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100651 /*
652 * Key attributes may have been returned by psa_get_key_attributes()
653 * thus reset them as required.
654 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200655 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100656
657 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200658 PSA_DONE( );
659}
660/* END_CASE */
661
662/* BEGIN_CASE */
663void import_with_data( data_t *data, int type_arg,
664 int attr_bits_arg,
665 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200666{
667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
668 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200670 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200671 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200672 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
Gilles Peskine4747d192019-04-17 15:05:45 +0200677 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200678 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200679
Ronald Cron5425a212020-08-04 14:58:35 +0200680 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100681 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200682 if( status != PSA_SUCCESS )
683 goto exit;
684
Ronald Cron5425a212020-08-04 14:58:35 +0200685 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200686 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200687 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200688 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200689 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200690
Ronald Cron5425a212020-08-04 14:58:35 +0200691 PSA_ASSERT( psa_destroy_key( key ) );
692 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693
694exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100695 /*
696 * Key attributes may have been returned by psa_get_key_attributes()
697 * thus reset them as required.
698 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200699 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100700
701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200702 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703}
704/* END_CASE */
705
706/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200707void import_large_key( int type_arg, int byte_size_arg,
708 int expected_status_arg )
709{
710 psa_key_type_t type = type_arg;
711 size_t byte_size = byte_size_arg;
712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
713 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200715 psa_status_t status;
716 uint8_t *buffer = NULL;
717 size_t buffer_size = byte_size + 1;
718 size_t n;
719
Steven Cooreman69967ce2021-01-18 18:01:08 +0100720 /* Skip the test case if the target running the test cannot
721 * accomodate large keys due to heap size constraints */
722 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200723 memset( buffer, 'K', byte_size );
724
725 PSA_ASSERT( psa_crypto_init( ) );
726
727 /* Try importing the key */
728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
729 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200730 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100731 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200732 TEST_EQUAL( status, expected_status );
733
734 if( status == PSA_SUCCESS )
735 {
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200737 TEST_EQUAL( psa_get_key_type( &attributes ), type );
738 TEST_EQUAL( psa_get_key_bits( &attributes ),
739 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200740 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200742 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200743 for( n = 0; n < byte_size; n++ )
744 TEST_EQUAL( buffer[n], 'K' );
745 for( n = byte_size; n < buffer_size; n++ )
746 TEST_EQUAL( buffer[n], 0 );
747 }
748
749exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100750 /*
751 * Key attributes may have been returned by psa_get_key_attributes()
752 * thus reset them as required.
753 */
754 psa_reset_key_attributes( &attributes );
755
Ronald Cron5425a212020-08-04 14:58:35 +0200756 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200757 PSA_DONE( );
758 mbedtls_free( buffer );
759}
760/* END_CASE */
761
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100762/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
764{
Ronald Cron5425a212020-08-04 14:58:35 +0200765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200766 size_t bits = bits_arg;
767 psa_status_t expected_status = expected_status_arg;
768 psa_status_t status;
769 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200770 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200771 size_t buffer_size = /* Slight overapproximations */
772 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200773 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200774 unsigned char *p;
775 int ret;
776 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778
Gilles Peskine8817f612018-12-18 00:18:46 +0100779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200780 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200781
782 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
783 bits, keypair ) ) >= 0 );
784 length = ret;
785
786 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200787 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200788 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100789 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200790
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200791 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200792 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200793
794exit:
795 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200796 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200797}
798/* END_CASE */
799
800/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300801void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300802 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200803 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530804 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 int expected_bits,
806 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200807 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 int canonical_input )
809{
Ronald Cron5425a212020-08-04 14:58:35 +0200810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200812 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200813 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530815 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 unsigned char *exported = NULL;
817 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100819 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200822 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823
Moran Pekercb088e72018-07-17 17:36:59 +0300824 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200825 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200827 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829
Archana4d7ae1d2021-07-07 02:50:22 +0530830 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200831 psa_set_key_usage_flags( &attributes, usage_arg );
832 psa_set_key_algorithm( &attributes, alg );
833 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700834
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200836 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837
838 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200839 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200840 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
841 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200842 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843
844 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200845 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100846 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100847
848 /* The exported length must be set by psa_export_key() to a value between 0
849 * and export_size. On errors, the exported length must be 0. */
850 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
851 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
852 TEST_ASSERT( exported_length <= export_size );
853
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200854 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200855 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100856 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200857 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200860 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861
Gilles Peskineea38a922021-02-13 00:05:16 +0100862 /* Run sanity checks on the exported key. For non-canonical inputs,
863 * this validates the canonical representations. For canonical inputs,
864 * this doesn't directly validate the implementation, but it still helps
865 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530866 if( !psa_key_lifetime_is_external( lifetime ) )
867 {
868 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
869 goto exit;
870 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200873 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 else
875 {
Ronald Cron5425a212020-08-04 14:58:35 +0200876 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200877 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200878 &key2 ) );
879 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100880 reexported,
881 export_size,
882 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200883 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +0530884 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100887 TEST_ASSERT( exported_length <=
Archana4d7ae1d2021-07-07 02:50:22 +0530888 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +0530889 psa_get_key_bits( &got_attributes ) ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100890 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891
892destroy:
893 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200894 PSA_ASSERT( psa_destroy_key( key ) );
895 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896
897exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100898 /*
899 * Key attributes may have been returned by psa_get_key_attributes()
900 * thus reset them as required.
901 */
902 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +0530903 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300904 mbedtls_free( exported );
905 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200906 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907}
908/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100909
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300911void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200912 int type_arg,
913 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530914 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 int export_size_delta,
916 int expected_export_status_arg,
917 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918{
Ronald Cron5425a212020-08-04 14:58:35 +0200919 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300920 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200921 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200922 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300923 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530924 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300925 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100926 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100927 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929
Gilles Peskine8817f612018-12-18 00:18:46 +0100930 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300931
Archana4d7ae1d2021-07-07 02:50:22 +0530932 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
934 psa_set_key_algorithm( &attributes, alg );
935 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300936
937 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200938 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300939
Gilles Peskine49c25912018-10-29 15:15:31 +0100940 /* Export the public key */
941 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200942 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200943 exported, export_size,
944 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100946 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100947 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200948 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100949 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200950 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200951 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100952 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100953 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100954 TEST_ASSERT( expected_public_key->len <=
955 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
956 TEST_ASSERT( expected_public_key->len <=
957 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100958 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
959 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100960 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300961exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100962 /*
963 * Key attributes may have been returned by psa_get_key_attributes()
964 * thus reset them as required.
965 */
966 psa_reset_key_attributes( &attributes );
967
itayzafrir3e02b3b2018-06-12 17:06:52 +0300968 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200969 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200970 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300971}
972/* END_CASE */
973
Gilles Peskine20035e32018-02-03 22:44:14 +0100974/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975void import_and_exercise_key( data_t *data,
976 int type_arg,
977 int bits_arg,
978 int alg_arg )
979{
Ronald Cron5425a212020-08-04 14:58:35 +0200980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200981 psa_key_type_t type = type_arg;
982 size_t bits = bits_arg;
983 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100984 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200986 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200987
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
Gilles Peskine4747d192019-04-17 15:05:45 +0200990 psa_set_key_usage_flags( &attributes, usage );
991 psa_set_key_algorithm( &attributes, alg );
992 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993
994 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200995 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200996
997 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200998 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200999 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1000 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001001
1002 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001003 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001004 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001005
Ronald Cron5425a212020-08-04 14:58:35 +02001006 PSA_ASSERT( psa_destroy_key( key ) );
1007 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001008
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001009exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001010 /*
1011 * Key attributes may have been returned by psa_get_key_attributes()
1012 * thus reset them as required.
1013 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001014 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001015
1016 psa_reset_key_attributes( &attributes );
1017 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001018 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001019}
1020/* END_CASE */
1021
1022/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001023void effective_key_attributes( int type_arg, int expected_type_arg,
1024 int bits_arg, int expected_bits_arg,
1025 int usage_arg, int expected_usage_arg,
1026 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001027{
Ronald Cron5425a212020-08-04 14:58:35 +02001028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001029 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001030 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001031 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001032 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001033 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001034 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001035 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001036 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
Gilles Peskine8817f612018-12-18 00:18:46 +01001039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001040
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001041 psa_set_key_usage_flags( &attributes, usage );
1042 psa_set_key_algorithm( &attributes, alg );
1043 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001044 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001047 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048
Ronald Cron5425a212020-08-04 14:58:35 +02001049 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001050 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1051 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1052 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1053 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001054
1055exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001056 /*
1057 * Key attributes may have been returned by psa_get_key_attributes()
1058 * thus reset them as required.
1059 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001060 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001061
1062 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001063 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001064}
1065/* END_CASE */
1066
1067/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001068void check_key_policy( int type_arg, int bits_arg,
1069 int usage_arg, int alg_arg )
1070{
1071 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001072 usage_arg,
1073 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001074 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001075 goto exit;
1076}
1077/* END_CASE */
1078
1079/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001080void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001081{
1082 /* Test each valid way of initializing the object, except for `= {0}`, as
1083 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1084 * though it's OK by the C standard. We could test for this, but we'd need
1085 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001086 psa_key_attributes_t func = psa_key_attributes_init( );
1087 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1088 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001089
1090 memset( &zero, 0, sizeof( zero ) );
1091
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001092 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1093 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1094 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001096 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1097 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1098 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1099
1100 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1101 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1102 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1103
1104 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1105 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1106 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1107
1108 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1109 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1110 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001111}
1112/* END_CASE */
1113
1114/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001115void mac_key_policy( int policy_usage_arg,
1116 int policy_alg_arg,
1117 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001119 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001120 int expected_status_sign_arg,
1121 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001126 psa_key_type_t key_type = key_type_arg;
1127 psa_algorithm_t policy_alg = policy_alg_arg;
1128 psa_algorithm_t exercise_alg = exercise_alg_arg;
1129 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001130 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001131 psa_status_t expected_status_sign = expected_status_sign_arg;
1132 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001134
Gilles Peskine8817f612018-12-18 00:18:46 +01001135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001137 psa_set_key_usage_flags( &attributes, policy_usage );
1138 psa_set_key_algorithm( &attributes, policy_alg );
1139 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001140
Gilles Peskine049c7532019-05-15 20:22:09 +02001141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001142 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001143
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001144 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1145 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001146
Ronald Cron5425a212020-08-04 14:58:35 +02001147 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001148 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001149
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001150 /* Calculate the MAC, one-shot case. */
1151 uint8_t input[128] = {0};
1152 size_t mac_len;
1153 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1154 input, 128,
1155 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1156 expected_status_sign );
1157
Neil Armstrong3af9b972022-02-07 12:20:21 +01001158 /* Calculate the MAC, multi-part case. */
1159 PSA_ASSERT( psa_mac_abort( &operation ) );
1160 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1161 if( status == PSA_SUCCESS )
1162 {
1163 status = psa_mac_update( &operation, input, 128 );
1164 if( status == PSA_SUCCESS )
1165 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1166 &mac_len ),
1167 expected_status_sign );
1168 else
1169 TEST_EQUAL( status, expected_status_sign );
1170 }
1171 else
1172 {
1173 TEST_EQUAL( status, expected_status_sign );
1174 }
1175 PSA_ASSERT( psa_mac_abort( &operation ) );
1176
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001177 /* Verify correct MAC, one-shot case. */
1178 status = psa_mac_verify( key, exercise_alg, input, 128,
1179 mac, mac_len );
1180
1181 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1182 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001183 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001184 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001185
Neil Armstrong3af9b972022-02-07 12:20:21 +01001186 /* Verify correct MAC, multi-part case. */
1187 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1188 if( status == PSA_SUCCESS )
1189 {
1190 status = psa_mac_update( &operation, input, 128 );
1191 if( status == PSA_SUCCESS )
1192 {
1193 status = psa_mac_verify_finish( &operation, mac, mac_len );
1194 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1195 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1196 else
1197 TEST_EQUAL( status, expected_status_verify );
1198 }
1199 else
1200 {
1201 TEST_EQUAL( status, expected_status_verify );
1202 }
1203 }
1204 else
1205 {
1206 TEST_EQUAL( status, expected_status_verify );
1207 }
1208
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001210
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001212 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001213 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214
1215exit:
1216 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001218 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219}
1220/* END_CASE */
1221
1222/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001223void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001224 int policy_alg,
1225 int key_type,
1226 data_t *key_data,
1227 int exercise_alg )
1228{
Ronald Cron5425a212020-08-04 14:58:35 +02001229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001231 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001232 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001233 size_t output_buffer_size = 0;
1234 size_t input_buffer_size = 0;
1235 size_t output_length = 0;
1236 uint8_t *output = NULL;
1237 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001238 psa_status_t status;
1239
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001240 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1241 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1242 input_buffer_size );
1243
1244 ASSERT_ALLOC( input, input_buffer_size );
1245 ASSERT_ALLOC( output, output_buffer_size );
1246
Gilles Peskine8817f612018-12-18 00:18:46 +01001247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001248
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001249 psa_set_key_usage_flags( &attributes, policy_usage );
1250 psa_set_key_algorithm( &attributes, policy_alg );
1251 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252
Gilles Peskine049c7532019-05-15 20:22:09 +02001253 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001254 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001255
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001256 /* Check if no key usage flag implication is done */
1257 TEST_EQUAL( policy_usage,
1258 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001259
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001260 /* Encrypt check, one-shot */
1261 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1262 output, output_buffer_size,
1263 &output_length);
1264 if( policy_alg == exercise_alg &&
1265 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1266 PSA_ASSERT( status );
1267 else
1268 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1269
1270 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001271 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001272 if( policy_alg == exercise_alg &&
1273 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001274 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001275 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001276 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277 psa_cipher_abort( &operation );
1278
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001279 /* Decrypt check, one-shot */
1280 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1281 input, input_buffer_size,
1282 &output_length);
1283 if( policy_alg == exercise_alg &&
1284 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1285 PSA_ASSERT( status );
1286 else
1287 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1288
1289 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001290 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001291 if( policy_alg == exercise_alg &&
1292 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001293 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001294 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001295 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296
1297exit:
1298 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001299 mbedtls_free( input );
1300 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001301 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001302 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001303}
1304/* END_CASE */
1305
1306/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001307void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001308 int policy_alg,
1309 int key_type,
1310 data_t *key_data,
1311 int nonce_length_arg,
1312 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001313 int exercise_alg,
1314 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001315{
Ronald Cron5425a212020-08-04 14:58:35 +02001316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001318 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001319 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001321 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001322 unsigned char nonce[16] = {0};
1323 size_t nonce_length = nonce_length_arg;
1324 unsigned char tag[16];
1325 size_t tag_length = tag_length_arg;
1326 size_t output_length;
1327
1328 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1329 TEST_ASSERT( tag_length <= sizeof( tag ) );
1330
Gilles Peskine8817f612018-12-18 00:18:46 +01001331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001333 psa_set_key_usage_flags( &attributes, policy_usage );
1334 psa_set_key_algorithm( &attributes, policy_alg );
1335 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001336
Gilles Peskine049c7532019-05-15 20:22:09 +02001337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001338 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001340 /* Check if no key usage implication is done */
1341 TEST_EQUAL( policy_usage,
1342 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001343
Neil Armstrong752d8112022-02-07 14:51:11 +01001344 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02001345 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346 nonce, nonce_length,
1347 NULL, 0,
1348 NULL, 0,
1349 tag, tag_length,
1350 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001351 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1352 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001353 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001354 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001355
Neil Armstrong752d8112022-02-07 14:51:11 +01001356 /* Encrypt check, multi-part */
1357 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
1358 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1359 TEST_EQUAL( status, expected_status );
1360 else
1361 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1362
1363 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001365 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001366 nonce, nonce_length,
1367 NULL, 0,
1368 tag, tag_length,
1369 NULL, 0,
1370 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001371 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1372 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1373 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001374 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001375 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001376 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001377
Neil Armstrong752d8112022-02-07 14:51:11 +01001378 /* Decrypt check, multi-part */
1379 PSA_ASSERT( psa_aead_abort( &operation ) );
1380 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
1381 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1382 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1383 else
1384 TEST_EQUAL( status, expected_status );
1385
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001386exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01001387 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001388 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001389 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001390}
1391/* END_CASE */
1392
1393/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001394void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001395 int policy_alg,
1396 int key_type,
1397 data_t *key_data,
1398 int exercise_alg )
1399{
Ronald Cron5425a212020-08-04 14:58:35 +02001400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001402 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001403 psa_status_t status;
1404 size_t key_bits;
1405 size_t buffer_length;
1406 unsigned char *buffer = NULL;
1407 size_t output_length;
1408
Gilles Peskine8817f612018-12-18 00:18:46 +01001409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001410
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001411 psa_set_key_usage_flags( &attributes, policy_usage );
1412 psa_set_key_algorithm( &attributes, policy_alg );
1413 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001414
Gilles Peskine049c7532019-05-15 20:22:09 +02001415 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001416 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001417
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001418 /* Check if no key usage implication is done */
1419 TEST_EQUAL( policy_usage,
1420 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001421
Ronald Cron5425a212020-08-04 14:58:35 +02001422 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001423 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001424 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1425 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001426 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001427
Ronald Cron5425a212020-08-04 14:58:35 +02001428 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001429 NULL, 0,
1430 NULL, 0,
1431 buffer, buffer_length,
1432 &output_length );
1433 if( policy_alg == exercise_alg &&
1434 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001435 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001436 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001437 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001438
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001439 if( buffer_length != 0 )
1440 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001441 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001442 buffer, buffer_length,
1443 NULL, 0,
1444 buffer, buffer_length,
1445 &output_length );
1446 if( policy_alg == exercise_alg &&
1447 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001448 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001449 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001450 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001451
1452exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001453 /*
1454 * Key attributes may have been returned by psa_get_key_attributes()
1455 * thus reset them as required.
1456 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001457 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001458
1459 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001460 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001461 mbedtls_free( buffer );
1462}
1463/* END_CASE */
1464
1465/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001466void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 int policy_alg,
1468 int key_type,
1469 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001470 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001471 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001472 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001473{
Ronald Cron5425a212020-08-04 14:58:35 +02001474 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001475 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001476 psa_key_usage_t policy_usage = policy_usage_arg;
1477 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001478 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001479 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1480 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1481 * compatible with the policy and `payload_length_arg` is supposed to be
1482 * a valid input length to sign. If `payload_length_arg <= 0`,
1483 * `exercise_alg` is supposed to be forbidden by the policy. */
1484 int compatible_alg = payload_length_arg > 0;
1485 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001486 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001487 size_t signature_length;
1488
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001489 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001490 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001491 TEST_EQUAL( expected_usage,
1492 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001493
Gilles Peskine8817f612018-12-18 00:18:46 +01001494 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001495
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001496 psa_set_key_usage_flags( &attributes, policy_usage );
1497 psa_set_key_algorithm( &attributes, policy_alg );
1498 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001499
Gilles Peskine049c7532019-05-15 20:22:09 +02001500 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001501 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001502
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001503 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1504
Ronald Cron5425a212020-08-04 14:58:35 +02001505 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001506 payload, payload_length,
1507 signature, sizeof( signature ),
1508 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001509 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001510 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001511 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001512 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001513
1514 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001515 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001516 payload, payload_length,
1517 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001518 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001519 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001520 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001521 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001522
Gilles Peskinef7b41372021-09-22 16:15:05 +02001523 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001524 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001525 {
1526 status = psa_sign_message( key, exercise_alg,
1527 payload, payload_length,
1528 signature, sizeof( signature ),
1529 &signature_length );
1530 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1531 PSA_ASSERT( status );
1532 else
1533 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1534
1535 memset( signature, 0, sizeof( signature ) );
1536 status = psa_verify_message( key, exercise_alg,
1537 payload, payload_length,
1538 signature, sizeof( signature ) );
1539 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1540 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1541 else
1542 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1543 }
1544
Gilles Peskined5b33222018-06-18 22:20:03 +02001545exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001546 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001547 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001548}
1549/* END_CASE */
1550
Janos Follathba3fab92019-06-11 14:50:16 +01001551/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001552void derive_key_policy( int policy_usage,
1553 int policy_alg,
1554 int key_type,
1555 data_t *key_data,
1556 int exercise_alg )
1557{
Ronald Cron5425a212020-08-04 14:58:35 +02001558 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001559 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001560 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001561 psa_status_t status;
1562
Gilles Peskine8817f612018-12-18 00:18:46 +01001563 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001564
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001565 psa_set_key_usage_flags( &attributes, policy_usage );
1566 psa_set_key_algorithm( &attributes, policy_alg );
1567 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001568
Gilles Peskine049c7532019-05-15 20:22:09 +02001569 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001570 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001571
Janos Follathba3fab92019-06-11 14:50:16 +01001572 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1573
1574 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1575 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001576 {
Janos Follathba3fab92019-06-11 14:50:16 +01001577 PSA_ASSERT( psa_key_derivation_input_bytes(
1578 &operation,
1579 PSA_KEY_DERIVATION_INPUT_SEED,
1580 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001581 }
Janos Follathba3fab92019-06-11 14:50:16 +01001582
1583 status = psa_key_derivation_input_key( &operation,
1584 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001585 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001586
Gilles Peskineea0fb492018-07-12 17:17:20 +02001587 if( policy_alg == exercise_alg &&
1588 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001589 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001590 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001591 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001592
1593exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001594 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001595 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001596 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001597}
1598/* END_CASE */
1599
1600/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001601void agreement_key_policy( int policy_usage,
1602 int policy_alg,
1603 int key_type_arg,
1604 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001605 int exercise_alg,
1606 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001607{
Ronald Cron5425a212020-08-04 14:58:35 +02001608 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001610 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001611 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001612 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001613 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001614
Gilles Peskine8817f612018-12-18 00:18:46 +01001615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001616
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001617 psa_set_key_usage_flags( &attributes, policy_usage );
1618 psa_set_key_algorithm( &attributes, policy_alg );
1619 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001620
Gilles Peskine049c7532019-05-15 20:22:09 +02001621 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001622 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001623
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001624 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001625 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001626
Steven Cooremance48e852020-10-05 16:02:45 +02001627 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001628
1629exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001630 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001631 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001632 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001633}
1634/* END_CASE */
1635
1636/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001637void key_policy_alg2( int key_type_arg, data_t *key_data,
1638 int usage_arg, int alg_arg, int alg2_arg )
1639{
Ronald Cron5425a212020-08-04 14:58:35 +02001640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001641 psa_key_type_t key_type = key_type_arg;
1642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1643 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1644 psa_key_usage_t usage = usage_arg;
1645 psa_algorithm_t alg = alg_arg;
1646 psa_algorithm_t alg2 = alg2_arg;
1647
1648 PSA_ASSERT( psa_crypto_init( ) );
1649
1650 psa_set_key_usage_flags( &attributes, usage );
1651 psa_set_key_algorithm( &attributes, alg );
1652 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1653 psa_set_key_type( &attributes, key_type );
1654 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001655 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001656
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001657 /* Update the usage flags to obtain implicit usage flags */
1658 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001659 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001660 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1661 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1662 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1663
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001664 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001665 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001666 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001667 goto exit;
1668
1669exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001670 /*
1671 * Key attributes may have been returned by psa_get_key_attributes()
1672 * thus reset them as required.
1673 */
1674 psa_reset_key_attributes( &got_attributes );
1675
Ronald Cron5425a212020-08-04 14:58:35 +02001676 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001677 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001678}
1679/* END_CASE */
1680
1681/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001682void raw_agreement_key_policy( int policy_usage,
1683 int policy_alg,
1684 int key_type_arg,
1685 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001686 int exercise_alg,
1687 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001688{
Ronald Cron5425a212020-08-04 14:58:35 +02001689 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001690 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001691 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001692 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001693 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001694 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001695
1696 PSA_ASSERT( psa_crypto_init( ) );
1697
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001698 psa_set_key_usage_flags( &attributes, policy_usage );
1699 psa_set_key_algorithm( &attributes, policy_alg );
1700 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001701
Gilles Peskine049c7532019-05-15 20:22:09 +02001702 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001703 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001704
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001705 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001706
Steven Cooremance48e852020-10-05 16:02:45 +02001707 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001708
1709exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001710 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001711 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001712 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001713}
1714/* END_CASE */
1715
1716/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001717void copy_success( int source_usage_arg,
1718 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301719 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001720 int type_arg, data_t *material,
1721 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001722 int target_usage_arg,
1723 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301724 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001725 int expected_usage_arg,
1726 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001727{
Gilles Peskineca25db92019-04-19 11:43:08 +02001728 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1729 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001730 psa_key_usage_t expected_usage = expected_usage_arg;
1731 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001732 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301733 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1734 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001735 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1736 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001737 uint8_t *export_buffer = NULL;
1738
Gilles Peskine57ab7212019-01-28 13:03:09 +01001739 PSA_ASSERT( psa_crypto_init( ) );
1740
Gilles Peskineca25db92019-04-19 11:43:08 +02001741 /* Prepare the source key. */
1742 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1743 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001744 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001745 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301746 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001747 PSA_ASSERT( psa_import_key( &source_attributes,
1748 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001749 &source_key ) );
1750 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001751
Gilles Peskineca25db92019-04-19 11:43:08 +02001752 /* Prepare the target attributes. */
1753 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001754 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001755 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001756 }
Archana8a180362021-07-05 02:18:48 +05301757 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001758
Gilles Peskineca25db92019-04-19 11:43:08 +02001759 if( target_usage_arg != -1 )
1760 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1761 if( target_alg_arg != -1 )
1762 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001763 if( target_alg2_arg != -1 )
1764 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001765
Archana8a180362021-07-05 02:18:48 +05301766
Gilles Peskine57ab7212019-01-28 13:03:09 +01001767 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001768 PSA_ASSERT( psa_copy_key( source_key,
1769 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001770
1771 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001772 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001773
1774 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001775 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001776 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1777 psa_get_key_type( &target_attributes ) );
1778 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1779 psa_get_key_bits( &target_attributes ) );
1780 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1781 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001782 TEST_EQUAL( expected_alg2,
1783 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001784 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1785 {
1786 size_t length;
1787 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001788 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001789 material->len, &length ) );
1790 ASSERT_COMPARE( material->x, material->len,
1791 export_buffer, length );
1792 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001793
Archana8a180362021-07-05 02:18:48 +05301794 if( !psa_key_lifetime_is_external( target_lifetime ) )
1795 {
1796 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1797 goto exit;
1798 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1799 goto exit;
1800 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001801
Ronald Cron5425a212020-08-04 14:58:35 +02001802 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001803
1804exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001805 /*
1806 * Source and target key attributes may have been returned by
1807 * psa_get_key_attributes() thus reset them as required.
1808 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001809 psa_reset_key_attributes( &source_attributes );
1810 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001811
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001812 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001813 mbedtls_free( export_buffer );
1814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001818void copy_fail( int source_usage_arg,
1819 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301820 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001821 int type_arg, data_t *material,
1822 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001823 int target_usage_arg,
1824 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001825 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001826 int expected_status_arg )
1827{
1828 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1829 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001830 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1831 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001832 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001833
1834 PSA_ASSERT( psa_crypto_init( ) );
1835
1836 /* Prepare the source key. */
1837 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1838 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001839 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001840 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301841 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001842 PSA_ASSERT( psa_import_key( &source_attributes,
1843 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001844 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001845
1846 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001847 psa_set_key_id( &target_attributes, key_id );
1848 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001849 psa_set_key_type( &target_attributes, target_type_arg );
1850 psa_set_key_bits( &target_attributes, target_bits_arg );
1851 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1852 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001853 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001854
1855 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001856 TEST_EQUAL( psa_copy_key( source_key,
1857 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001858 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001859
Ronald Cron5425a212020-08-04 14:58:35 +02001860 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001861
Gilles Peskine4a644642019-05-03 17:14:08 +02001862exit:
1863 psa_reset_key_attributes( &source_attributes );
1864 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001865 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001866}
1867/* END_CASE */
1868
1869/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001870void hash_operation_init( )
1871{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001872 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001873 /* Test each valid way of initializing the object, except for `= {0}`, as
1874 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1875 * though it's OK by the C standard. We could test for this, but we'd need
1876 * to supress the Clang warning for the test. */
1877 psa_hash_operation_t func = psa_hash_operation_init( );
1878 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1879 psa_hash_operation_t zero;
1880
1881 memset( &zero, 0, sizeof( zero ) );
1882
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001883 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001884 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1885 PSA_ERROR_BAD_STATE );
1886 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1887 PSA_ERROR_BAD_STATE );
1888 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1889 PSA_ERROR_BAD_STATE );
1890
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001891 /* A default hash operation should be abortable without error. */
1892 PSA_ASSERT( psa_hash_abort( &func ) );
1893 PSA_ASSERT( psa_hash_abort( &init ) );
1894 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001895}
1896/* END_CASE */
1897
1898/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001899void hash_setup( int alg_arg,
1900 int expected_status_arg )
1901{
1902 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01001903 uint8_t *output = NULL;
1904 size_t output_size = 0;
1905 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001906 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001907 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001908 psa_status_t status;
1909
Gilles Peskine8817f612018-12-18 00:18:46 +01001910 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001911
Neil Armstrongedb20862022-02-07 15:47:44 +01001912 /* Hash Setup, one-shot */
1913 output_size = PSA_HASH_LENGTH(alg);
1914 ASSERT_ALLOC( output, output_size );
1915
1916 status = psa_hash_compute( alg, NULL, 0,
1917 output, output_size, &output_length );
1918 TEST_EQUAL( status, expected_status );
1919
1920 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001921 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001922 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001923
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001924 /* Whether setup succeeded or failed, abort must succeed. */
1925 PSA_ASSERT( psa_hash_abort( &operation ) );
1926
1927 /* If setup failed, reproduce the failure, so as to
1928 * test the resulting state of the operation object. */
1929 if( status != PSA_SUCCESS )
1930 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1931
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001932 /* Now the operation object should be reusable. */
1933#if defined(KNOWN_SUPPORTED_HASH_ALG)
1934 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1935 PSA_ASSERT( psa_hash_abort( &operation ) );
1936#endif
1937
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001938exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01001939 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001940 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001941}
1942/* END_CASE */
1943
1944/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001945void hash_compute_fail( int alg_arg, data_t *input,
1946 int output_size_arg, int expected_status_arg )
1947{
1948 psa_algorithm_t alg = alg_arg;
1949 uint8_t *output = NULL;
1950 size_t output_size = output_size_arg;
1951 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001952 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01001953 psa_status_t expected_status = expected_status_arg;
1954 psa_status_t status;
1955
1956 ASSERT_ALLOC( output, output_size );
1957
1958 PSA_ASSERT( psa_crypto_init( ) );
1959
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001960 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001961 status = psa_hash_compute( alg, input->x, input->len,
1962 output, output_size, &output_length );
1963 TEST_EQUAL( status, expected_status );
1964 TEST_ASSERT( output_length <= output_size );
1965
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001966 /* Hash Compute, multi-part */
1967 status = psa_hash_setup( &operation, alg );
1968 if( status == PSA_SUCCESS )
1969 {
1970 status = psa_hash_update( &operation, input->x, input->len );
1971 if( status == PSA_SUCCESS )
1972 {
1973 status = psa_hash_finish( &operation, output, output_size,
1974 &output_length );
1975 if( status == PSA_SUCCESS )
1976 TEST_ASSERT( output_length <= output_size );
1977 else
1978 TEST_EQUAL( status, expected_status );
1979 }
1980 else
1981 {
1982 TEST_EQUAL( status, expected_status );
1983 }
1984 }
1985 else
1986 {
1987 TEST_EQUAL( status, expected_status );
1988 }
1989
Gilles Peskine0a749c82019-11-28 19:33:58 +01001990exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001991 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001992 mbedtls_free( output );
1993 PSA_DONE( );
1994}
1995/* END_CASE */
1996
1997/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001998void hash_compare_fail( int alg_arg, data_t *input,
1999 data_t *reference_hash,
2000 int expected_status_arg )
2001{
2002 psa_algorithm_t alg = alg_arg;
2003 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002004 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002005 psa_status_t status;
2006
2007 PSA_ASSERT( psa_crypto_init( ) );
2008
Neil Armstrong55a1be12022-02-07 11:23:20 +01002009 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002010 status = psa_hash_compare( alg, input->x, input->len,
2011 reference_hash->x, reference_hash->len );
2012 TEST_EQUAL( status, expected_status );
2013
Neil Armstrong55a1be12022-02-07 11:23:20 +01002014 /* Hash Compare, multi-part */
2015 status = psa_hash_setup( &operation, alg );
2016 if( status == PSA_SUCCESS )
2017 {
2018 status = psa_hash_update( &operation, input->x, input->len );
2019 if( status == PSA_SUCCESS )
2020 {
2021 status = psa_hash_verify( &operation, reference_hash->x,
2022 reference_hash->len );
2023 TEST_EQUAL( status, expected_status );
2024 }
2025 else
2026 {
2027 TEST_EQUAL( status, expected_status );
2028 }
2029 }
2030 else
2031 {
2032 TEST_EQUAL( status, expected_status );
2033 }
2034
Gilles Peskine88e08462020-01-28 20:43:00 +01002035exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002036 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002037 PSA_DONE( );
2038}
2039/* END_CASE */
2040
2041/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002042void hash_compute_compare( int alg_arg, data_t *input,
2043 data_t *expected_output )
2044{
2045 psa_algorithm_t alg = alg_arg;
2046 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2047 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002048 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002049 size_t i;
2050
2051 PSA_ASSERT( psa_crypto_init( ) );
2052
Neil Armstrongca30a002022-02-07 11:40:23 +01002053 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002054 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002055 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002056 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002057 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002058 ASSERT_COMPARE( output, output_length,
2059 expected_output->x, expected_output->len );
2060
Neil Armstrongca30a002022-02-07 11:40:23 +01002061 /* Compute with tight buffer, multi-part */
2062 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2063 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2064 PSA_ASSERT( psa_hash_finish( &operation, output,
2065 PSA_HASH_LENGTH( alg ),
2066 &output_length ) );
2067 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2068 ASSERT_COMPARE( output, output_length,
2069 expected_output->x, expected_output->len );
2070
2071 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002072 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2073 output, sizeof( output ),
2074 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002075 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002076 ASSERT_COMPARE( output, output_length,
2077 expected_output->x, expected_output->len );
2078
Neil Armstrongca30a002022-02-07 11:40:23 +01002079 /* Compute with larger buffer, multi-part */
2080 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2081 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2082 PSA_ASSERT( psa_hash_finish( &operation, output,
2083 sizeof( output ), &output_length ) );
2084 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2085 ASSERT_COMPARE( output, output_length,
2086 expected_output->x, expected_output->len );
2087
2088 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002089 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2090 output, output_length ) );
2091
Neil Armstrongca30a002022-02-07 11:40:23 +01002092 /* Compare with correct hash, multi-part */
2093 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2094 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2095 PSA_ASSERT( psa_hash_verify( &operation, output,
2096 output_length ) );
2097
2098 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002099 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2100 output, output_length + 1 ),
2101 PSA_ERROR_INVALID_SIGNATURE );
2102
Neil Armstrongca30a002022-02-07 11:40:23 +01002103 /* Compare with trailing garbage, multi-part */
2104 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2105 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2106 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2107 PSA_ERROR_INVALID_SIGNATURE );
2108
2109 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002110 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2111 output, output_length - 1 ),
2112 PSA_ERROR_INVALID_SIGNATURE );
2113
Neil Armstrongca30a002022-02-07 11:40:23 +01002114 /* Compare with truncated hash, multi-part */
2115 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2116 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2117 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2118 PSA_ERROR_INVALID_SIGNATURE );
2119
Gilles Peskine0a749c82019-11-28 19:33:58 +01002120 /* Compare with corrupted value */
2121 for( i = 0; i < output_length; i++ )
2122 {
Chris Jones9634bb12021-01-20 15:56:42 +00002123 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002124 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002125
2126 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002127 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2128 output, output_length ),
2129 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002130
2131 /* Multi-Part */
2132 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2133 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2134 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2135 PSA_ERROR_INVALID_SIGNATURE );
2136
Gilles Peskine0a749c82019-11-28 19:33:58 +01002137 output[i] ^= 1;
2138 }
2139
2140exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002141 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002142 PSA_DONE( );
2143}
2144/* END_CASE */
2145
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002146/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002147void hash_bad_order( )
2148{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002149 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002150 unsigned char input[] = "";
2151 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002152 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002153 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2154 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2155 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002156 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002157 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002158 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002159
Gilles Peskine8817f612018-12-18 00:18:46 +01002160 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002161
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002162 /* Call setup twice in a row. */
2163 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002164 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002165 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2166 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002167 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002168 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002169 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002170
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002171 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002172 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002173 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002174 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002175
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002176 /* Check that update calls abort on error. */
2177 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002178 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002179 ASSERT_OPERATION_IS_ACTIVE( operation );
2180 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2181 PSA_ERROR_BAD_STATE );
2182 ASSERT_OPERATION_IS_INACTIVE( operation );
2183 PSA_ASSERT( psa_hash_abort( &operation ) );
2184 ASSERT_OPERATION_IS_INACTIVE( operation );
2185
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002186 /* Call update after finish. */
2187 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2188 PSA_ASSERT( psa_hash_finish( &operation,
2189 hash, sizeof( hash ), &hash_len ) );
2190 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002191 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002192 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002193
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002194 /* Call verify without calling setup beforehand. */
2195 TEST_EQUAL( psa_hash_verify( &operation,
2196 valid_hash, sizeof( valid_hash ) ),
2197 PSA_ERROR_BAD_STATE );
2198 PSA_ASSERT( psa_hash_abort( &operation ) );
2199
2200 /* Call verify after finish. */
2201 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2202 PSA_ASSERT( psa_hash_finish( &operation,
2203 hash, sizeof( hash ), &hash_len ) );
2204 TEST_EQUAL( psa_hash_verify( &operation,
2205 valid_hash, sizeof( valid_hash ) ),
2206 PSA_ERROR_BAD_STATE );
2207 PSA_ASSERT( psa_hash_abort( &operation ) );
2208
2209 /* Call verify twice in a row. */
2210 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002211 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002212 PSA_ASSERT( psa_hash_verify( &operation,
2213 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002214 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002215 TEST_EQUAL( psa_hash_verify( &operation,
2216 valid_hash, sizeof( valid_hash ) ),
2217 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002218 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002219 PSA_ASSERT( psa_hash_abort( &operation ) );
2220
2221 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002222 TEST_EQUAL( psa_hash_finish( &operation,
2223 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002224 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002225 PSA_ASSERT( psa_hash_abort( &operation ) );
2226
2227 /* Call finish twice in a row. */
2228 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2229 PSA_ASSERT( psa_hash_finish( &operation,
2230 hash, sizeof( hash ), &hash_len ) );
2231 TEST_EQUAL( psa_hash_finish( &operation,
2232 hash, sizeof( hash ), &hash_len ),
2233 PSA_ERROR_BAD_STATE );
2234 PSA_ASSERT( psa_hash_abort( &operation ) );
2235
2236 /* Call finish after calling verify. */
2237 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2238 PSA_ASSERT( psa_hash_verify( &operation,
2239 valid_hash, sizeof( valid_hash ) ) );
2240 TEST_EQUAL( psa_hash_finish( &operation,
2241 hash, sizeof( hash ), &hash_len ),
2242 PSA_ERROR_BAD_STATE );
2243 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002244
2245exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002246 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002247}
2248/* END_CASE */
2249
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002250/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002251void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002252{
2253 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002254 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2255 * appended to it */
2256 unsigned char hash[] = {
2257 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2258 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2259 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002260 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002261 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002262
Gilles Peskine8817f612018-12-18 00:18:46 +01002263 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002264
itayzafrir27e69452018-11-01 14:26:34 +02002265 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002266 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002267 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002268 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002269 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002270 ASSERT_OPERATION_IS_INACTIVE( operation );
2271 PSA_ASSERT( psa_hash_abort( &operation ) );
2272 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002273
itayzafrir27e69452018-11-01 14:26:34 +02002274 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002275 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002276 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002277 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002278
itayzafrir27e69452018-11-01 14:26:34 +02002279 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002280 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002281 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002282 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002283
itayzafrirec93d302018-10-18 18:01:10 +03002284exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002285 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002286}
2287/* END_CASE */
2288
Ronald Cronee414c72021-03-18 18:50:08 +01002289/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002290void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002291{
2292 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002293 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002294 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002295 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002296 size_t hash_len;
2297
Gilles Peskine8817f612018-12-18 00:18:46 +01002298 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002299
itayzafrir58028322018-10-25 10:22:01 +03002300 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002301 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002302 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002303 hash, expected_size - 1, &hash_len ),
2304 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002305
2306exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002307 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002308}
2309/* END_CASE */
2310
Ronald Cronee414c72021-03-18 18:50:08 +01002311/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002312void hash_clone_source_state( )
2313{
2314 psa_algorithm_t alg = PSA_ALG_SHA_256;
2315 unsigned char hash[PSA_HASH_MAX_SIZE];
2316 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2317 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2318 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2319 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2320 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2321 size_t hash_len;
2322
2323 PSA_ASSERT( psa_crypto_init( ) );
2324 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2325
2326 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2327 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2328 PSA_ASSERT( psa_hash_finish( &op_finished,
2329 hash, sizeof( hash ), &hash_len ) );
2330 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2331 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2332
2333 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2334 PSA_ERROR_BAD_STATE );
2335
2336 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2337 PSA_ASSERT( psa_hash_finish( &op_init,
2338 hash, sizeof( hash ), &hash_len ) );
2339 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2340 PSA_ASSERT( psa_hash_finish( &op_finished,
2341 hash, sizeof( hash ), &hash_len ) );
2342 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2343 PSA_ASSERT( psa_hash_finish( &op_aborted,
2344 hash, sizeof( hash ), &hash_len ) );
2345
2346exit:
2347 psa_hash_abort( &op_source );
2348 psa_hash_abort( &op_init );
2349 psa_hash_abort( &op_setup );
2350 psa_hash_abort( &op_finished );
2351 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002352 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002353}
2354/* END_CASE */
2355
Ronald Cronee414c72021-03-18 18:50:08 +01002356/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002357void hash_clone_target_state( )
2358{
2359 psa_algorithm_t alg = PSA_ALG_SHA_256;
2360 unsigned char hash[PSA_HASH_MAX_SIZE];
2361 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2362 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2363 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2364 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2365 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2366 size_t hash_len;
2367
2368 PSA_ASSERT( psa_crypto_init( ) );
2369
2370 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2371 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2372 PSA_ASSERT( psa_hash_finish( &op_finished,
2373 hash, sizeof( hash ), &hash_len ) );
2374 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2375 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2376
2377 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2378 PSA_ASSERT( psa_hash_finish( &op_target,
2379 hash, sizeof( hash ), &hash_len ) );
2380
2381 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2382 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2383 PSA_ERROR_BAD_STATE );
2384 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2385 PSA_ERROR_BAD_STATE );
2386
2387exit:
2388 psa_hash_abort( &op_target );
2389 psa_hash_abort( &op_init );
2390 psa_hash_abort( &op_setup );
2391 psa_hash_abort( &op_finished );
2392 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002393 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002394}
2395/* END_CASE */
2396
itayzafrir58028322018-10-25 10:22:01 +03002397/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002398void mac_operation_init( )
2399{
Jaeden Amero252ef282019-02-15 14:05:35 +00002400 const uint8_t input[1] = { 0 };
2401
Jaeden Amero769ce272019-01-04 11:48:03 +00002402 /* Test each valid way of initializing the object, except for `= {0}`, as
2403 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2404 * though it's OK by the C standard. We could test for this, but we'd need
2405 * to supress the Clang warning for the test. */
2406 psa_mac_operation_t func = psa_mac_operation_init( );
2407 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2408 psa_mac_operation_t zero;
2409
2410 memset( &zero, 0, sizeof( zero ) );
2411
Jaeden Amero252ef282019-02-15 14:05:35 +00002412 /* A freshly-initialized MAC operation should not be usable. */
2413 TEST_EQUAL( psa_mac_update( &func,
2414 input, sizeof( input ) ),
2415 PSA_ERROR_BAD_STATE );
2416 TEST_EQUAL( psa_mac_update( &init,
2417 input, sizeof( input ) ),
2418 PSA_ERROR_BAD_STATE );
2419 TEST_EQUAL( psa_mac_update( &zero,
2420 input, sizeof( input ) ),
2421 PSA_ERROR_BAD_STATE );
2422
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002423 /* A default MAC operation should be abortable without error. */
2424 PSA_ASSERT( psa_mac_abort( &func ) );
2425 PSA_ASSERT( psa_mac_abort( &init ) );
2426 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002427}
2428/* END_CASE */
2429
2430/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002431void mac_setup( int key_type_arg,
2432 data_t *key,
2433 int alg_arg,
2434 int expected_status_arg )
2435{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002436 psa_key_type_t key_type = key_type_arg;
2437 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002438 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002439 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002440 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2441#if defined(KNOWN_SUPPORTED_MAC_ALG)
2442 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2443#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444
Gilles Peskine8817f612018-12-18 00:18:46 +01002445 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002446
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002447 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2448 &operation, &status ) )
2449 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002450 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002451
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002452 /* The operation object should be reusable. */
2453#if defined(KNOWN_SUPPORTED_MAC_ALG)
2454 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2455 smoke_test_key_data,
2456 sizeof( smoke_test_key_data ),
2457 KNOWN_SUPPORTED_MAC_ALG,
2458 &operation, &status ) )
2459 goto exit;
2460 TEST_EQUAL( status, PSA_SUCCESS );
2461#endif
2462
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002463exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002464 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002465}
2466/* END_CASE */
2467
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002468/* 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 +00002469void mac_bad_order( )
2470{
Ronald Cron5425a212020-08-04 14:58:35 +02002471 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002472 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2473 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002474 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002475 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2476 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2477 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002479 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2480 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2481 size_t sign_mac_length = 0;
2482 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2483 const uint8_t verify_mac[] = {
2484 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2485 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2486 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2487
2488 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002489 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002490 psa_set_key_algorithm( &attributes, alg );
2491 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002492
Ronald Cron5425a212020-08-04 14:58:35 +02002493 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2494 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002495
Jaeden Amero252ef282019-02-15 14:05:35 +00002496 /* Call update without calling setup beforehand. */
2497 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2498 PSA_ERROR_BAD_STATE );
2499 PSA_ASSERT( psa_mac_abort( &operation ) );
2500
2501 /* Call sign finish without calling setup beforehand. */
2502 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2503 &sign_mac_length),
2504 PSA_ERROR_BAD_STATE );
2505 PSA_ASSERT( psa_mac_abort( &operation ) );
2506
2507 /* Call verify finish without calling setup beforehand. */
2508 TEST_EQUAL( psa_mac_verify_finish( &operation,
2509 verify_mac, sizeof( verify_mac ) ),
2510 PSA_ERROR_BAD_STATE );
2511 PSA_ASSERT( psa_mac_abort( &operation ) );
2512
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002513 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002514 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002515 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002516 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002517 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002518 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002519 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002520 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002521
Jaeden Amero252ef282019-02-15 14:05:35 +00002522 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002523 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002524 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2525 PSA_ASSERT( psa_mac_sign_finish( &operation,
2526 sign_mac, sizeof( sign_mac ),
2527 &sign_mac_length ) );
2528 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2529 PSA_ERROR_BAD_STATE );
2530 PSA_ASSERT( psa_mac_abort( &operation ) );
2531
2532 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002533 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002534 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2535 PSA_ASSERT( psa_mac_verify_finish( &operation,
2536 verify_mac, sizeof( verify_mac ) ) );
2537 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2538 PSA_ERROR_BAD_STATE );
2539 PSA_ASSERT( psa_mac_abort( &operation ) );
2540
2541 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002542 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002543 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2544 PSA_ASSERT( psa_mac_sign_finish( &operation,
2545 sign_mac, sizeof( sign_mac ),
2546 &sign_mac_length ) );
2547 TEST_EQUAL( psa_mac_sign_finish( &operation,
2548 sign_mac, sizeof( sign_mac ),
2549 &sign_mac_length ),
2550 PSA_ERROR_BAD_STATE );
2551 PSA_ASSERT( psa_mac_abort( &operation ) );
2552
2553 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002554 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002555 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2556 PSA_ASSERT( psa_mac_verify_finish( &operation,
2557 verify_mac, sizeof( verify_mac ) ) );
2558 TEST_EQUAL( psa_mac_verify_finish( &operation,
2559 verify_mac, sizeof( verify_mac ) ),
2560 PSA_ERROR_BAD_STATE );
2561 PSA_ASSERT( psa_mac_abort( &operation ) );
2562
2563 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002564 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002565 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002566 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002567 TEST_EQUAL( psa_mac_verify_finish( &operation,
2568 verify_mac, sizeof( verify_mac ) ),
2569 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002570 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002571 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002572 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002573
2574 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002575 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002576 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002577 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002578 TEST_EQUAL( psa_mac_sign_finish( &operation,
2579 sign_mac, sizeof( sign_mac ),
2580 &sign_mac_length ),
2581 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002582 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002583 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002584 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002585
Ronald Cron5425a212020-08-04 14:58:35 +02002586 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002587
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002588exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002589 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002590}
2591/* END_CASE */
2592
2593/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002594void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002595 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002596 int alg_arg,
2597 data_t *input,
2598 data_t *expected_mac )
2599{
Ronald Cron5425a212020-08-04 14:58:35 +02002600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002601 psa_key_type_t key_type = key_type_arg;
2602 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002603 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002605 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002606 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002607 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002608 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002609 const size_t output_sizes_to_test[] = {
2610 0,
2611 1,
2612 expected_mac->len - 1,
2613 expected_mac->len,
2614 expected_mac->len + 1,
2615 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002616
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002617 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002618 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002619 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002620
Gilles Peskine8817f612018-12-18 00:18:46 +01002621 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002622
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002623 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002624 psa_set_key_algorithm( &attributes, alg );
2625 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002626
Ronald Cron5425a212020-08-04 14:58:35 +02002627 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2628 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002629
Gilles Peskine8b356b52020-08-25 23:44:59 +02002630 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2631 {
2632 const size_t output_size = output_sizes_to_test[i];
2633 psa_status_t expected_status =
2634 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2635 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002636
Chris Jones9634bb12021-01-20 15:56:42 +00002637 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002638 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002639
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002640 /* Calculate the MAC, one-shot case. */
2641 TEST_EQUAL( psa_mac_compute( key, alg,
2642 input->x, input->len,
2643 actual_mac, output_size, &mac_length ),
2644 expected_status );
2645 if( expected_status == PSA_SUCCESS )
2646 {
2647 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2648 actual_mac, mac_length );
2649 }
2650
2651 if( output_size > 0 )
2652 memset( actual_mac, 0, output_size );
2653
2654 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002655 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002656 PSA_ASSERT( psa_mac_update( &operation,
2657 input->x, input->len ) );
2658 TEST_EQUAL( psa_mac_sign_finish( &operation,
2659 actual_mac, output_size,
2660 &mac_length ),
2661 expected_status );
2662 PSA_ASSERT( psa_mac_abort( &operation ) );
2663
2664 if( expected_status == PSA_SUCCESS )
2665 {
2666 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2667 actual_mac, mac_length );
2668 }
2669 mbedtls_free( actual_mac );
2670 actual_mac = NULL;
2671 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002672
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002673exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002674 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002675 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002676 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002677 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002678}
2679/* END_CASE */
2680
2681/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002682void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002683 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002684 int alg_arg,
2685 data_t *input,
2686 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687{
Ronald Cron5425a212020-08-04 14:58:35 +02002688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689 psa_key_type_t key_type = key_type_arg;
2690 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002691 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002693 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694
Gilles Peskine69c12672018-06-28 00:07:19 +02002695 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2696
Gilles Peskine8817f612018-12-18 00:18:46 +01002697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002698
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002699 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002700 psa_set_key_algorithm( &attributes, alg );
2701 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002702
Ronald Cron5425a212020-08-04 14:58:35 +02002703 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2704 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002705
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002706 /* Verify correct MAC, one-shot case. */
2707 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2708 expected_mac->x, expected_mac->len ) );
2709
2710 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002711 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_mac_update( &operation,
2713 input->x, input->len ) );
2714 PSA_ASSERT( psa_mac_verify_finish( &operation,
2715 expected_mac->x,
2716 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002717
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002718 /* Test a MAC that's too short, one-shot case. */
2719 TEST_EQUAL( psa_mac_verify( key, alg,
2720 input->x, input->len,
2721 expected_mac->x,
2722 expected_mac->len - 1 ),
2723 PSA_ERROR_INVALID_SIGNATURE );
2724
2725 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002726 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002727 PSA_ASSERT( psa_mac_update( &operation,
2728 input->x, input->len ) );
2729 TEST_EQUAL( psa_mac_verify_finish( &operation,
2730 expected_mac->x,
2731 expected_mac->len - 1 ),
2732 PSA_ERROR_INVALID_SIGNATURE );
2733
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002734 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002735 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2736 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002737 TEST_EQUAL( psa_mac_verify( key, alg,
2738 input->x, input->len,
2739 perturbed_mac, expected_mac->len + 1 ),
2740 PSA_ERROR_INVALID_SIGNATURE );
2741
2742 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002743 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002744 PSA_ASSERT( psa_mac_update( &operation,
2745 input->x, input->len ) );
2746 TEST_EQUAL( psa_mac_verify_finish( &operation,
2747 perturbed_mac,
2748 expected_mac->len + 1 ),
2749 PSA_ERROR_INVALID_SIGNATURE );
2750
2751 /* Test changing one byte. */
2752 for( size_t i = 0; i < expected_mac->len; i++ )
2753 {
Chris Jones9634bb12021-01-20 15:56:42 +00002754 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002755 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002756
2757 TEST_EQUAL( psa_mac_verify( key, alg,
2758 input->x, input->len,
2759 perturbed_mac, expected_mac->len ),
2760 PSA_ERROR_INVALID_SIGNATURE );
2761
Ronald Cron5425a212020-08-04 14:58:35 +02002762 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002763 PSA_ASSERT( psa_mac_update( &operation,
2764 input->x, input->len ) );
2765 TEST_EQUAL( psa_mac_verify_finish( &operation,
2766 perturbed_mac,
2767 expected_mac->len ),
2768 PSA_ERROR_INVALID_SIGNATURE );
2769 perturbed_mac[i] ^= 1;
2770 }
2771
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002773 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002774 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002775 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002776 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002777}
2778/* END_CASE */
2779
2780/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002781void cipher_operation_init( )
2782{
Jaeden Ameroab439972019-02-15 14:12:05 +00002783 const uint8_t input[1] = { 0 };
2784 unsigned char output[1] = { 0 };
2785 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002786 /* Test each valid way of initializing the object, except for `= {0}`, as
2787 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2788 * though it's OK by the C standard. We could test for this, but we'd need
2789 * to supress the Clang warning for the test. */
2790 psa_cipher_operation_t func = psa_cipher_operation_init( );
2791 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2792 psa_cipher_operation_t zero;
2793
2794 memset( &zero, 0, sizeof( zero ) );
2795
Jaeden Ameroab439972019-02-15 14:12:05 +00002796 /* A freshly-initialized cipher operation should not be usable. */
2797 TEST_EQUAL( psa_cipher_update( &func,
2798 input, sizeof( input ),
2799 output, sizeof( output ),
2800 &output_length ),
2801 PSA_ERROR_BAD_STATE );
2802 TEST_EQUAL( psa_cipher_update( &init,
2803 input, sizeof( input ),
2804 output, sizeof( output ),
2805 &output_length ),
2806 PSA_ERROR_BAD_STATE );
2807 TEST_EQUAL( psa_cipher_update( &zero,
2808 input, sizeof( input ),
2809 output, sizeof( output ),
2810 &output_length ),
2811 PSA_ERROR_BAD_STATE );
2812
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002813 /* A default cipher operation should be abortable without error. */
2814 PSA_ASSERT( psa_cipher_abort( &func ) );
2815 PSA_ASSERT( psa_cipher_abort( &init ) );
2816 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002817}
2818/* END_CASE */
2819
2820/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002821void cipher_setup( int key_type_arg,
2822 data_t *key,
2823 int alg_arg,
2824 int expected_status_arg )
2825{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002826 psa_key_type_t key_type = key_type_arg;
2827 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002828 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002829 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002830 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002831#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002832 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2833#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002834
Gilles Peskine8817f612018-12-18 00:18:46 +01002835 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002836
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002837 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2838 &operation, &status ) )
2839 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002840 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002841
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002842 /* The operation object should be reusable. */
2843#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2844 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2845 smoke_test_key_data,
2846 sizeof( smoke_test_key_data ),
2847 KNOWN_SUPPORTED_CIPHER_ALG,
2848 &operation, &status ) )
2849 goto exit;
2850 TEST_EQUAL( status, PSA_SUCCESS );
2851#endif
2852
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002853exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002854 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002855 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002856}
2857/* END_CASE */
2858
Ronald Cronee414c72021-03-18 18:50:08 +01002859/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002860void cipher_bad_order( )
2861{
Ronald Cron5425a212020-08-04 14:58:35 +02002862 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002863 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2864 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002866 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002867 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002868 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002869 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2870 0xaa, 0xaa, 0xaa, 0xaa };
2871 const uint8_t text[] = {
2872 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2873 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002874 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002875 size_t length = 0;
2876
2877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002878 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2879 psa_set_key_algorithm( &attributes, alg );
2880 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002881 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2882 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002883
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002884 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002885 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002886 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002887 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002888 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002889 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002890 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002891 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002892
2893 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002894 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002895 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002896 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002897 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002898 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002899 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002900 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002901
Jaeden Ameroab439972019-02-15 14:12:05 +00002902 /* Generate an IV without calling setup beforehand. */
2903 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2904 buffer, sizeof( buffer ),
2905 &length ),
2906 PSA_ERROR_BAD_STATE );
2907 PSA_ASSERT( psa_cipher_abort( &operation ) );
2908
2909 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002910 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002911 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2912 buffer, sizeof( buffer ),
2913 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002914 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002915 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2916 buffer, sizeof( buffer ),
2917 &length ),
2918 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002919 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002920 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002921 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002922
2923 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002924 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002925 PSA_ASSERT( psa_cipher_set_iv( &operation,
2926 iv, sizeof( iv ) ) );
2927 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2928 buffer, sizeof( buffer ),
2929 &length ),
2930 PSA_ERROR_BAD_STATE );
2931 PSA_ASSERT( psa_cipher_abort( &operation ) );
2932
2933 /* Set an IV without calling setup beforehand. */
2934 TEST_EQUAL( psa_cipher_set_iv( &operation,
2935 iv, sizeof( iv ) ),
2936 PSA_ERROR_BAD_STATE );
2937 PSA_ASSERT( psa_cipher_abort( &operation ) );
2938
2939 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002940 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002941 PSA_ASSERT( psa_cipher_set_iv( &operation,
2942 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002943 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002944 TEST_EQUAL( psa_cipher_set_iv( &operation,
2945 iv, sizeof( iv ) ),
2946 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002947 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002948 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002949 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002950
2951 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002952 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002953 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2954 buffer, sizeof( buffer ),
2955 &length ) );
2956 TEST_EQUAL( psa_cipher_set_iv( &operation,
2957 iv, sizeof( iv ) ),
2958 PSA_ERROR_BAD_STATE );
2959 PSA_ASSERT( psa_cipher_abort( &operation ) );
2960
2961 /* Call update without calling setup beforehand. */
2962 TEST_EQUAL( psa_cipher_update( &operation,
2963 text, sizeof( text ),
2964 buffer, sizeof( buffer ),
2965 &length ),
2966 PSA_ERROR_BAD_STATE );
2967 PSA_ASSERT( psa_cipher_abort( &operation ) );
2968
2969 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002970 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002971 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002972 TEST_EQUAL( psa_cipher_update( &operation,
2973 text, sizeof( text ),
2974 buffer, sizeof( buffer ),
2975 &length ),
2976 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002977 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002978 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002979 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002980
2981 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002982 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002983 PSA_ASSERT( psa_cipher_set_iv( &operation,
2984 iv, sizeof( iv ) ) );
2985 PSA_ASSERT( psa_cipher_finish( &operation,
2986 buffer, sizeof( buffer ), &length ) );
2987 TEST_EQUAL( psa_cipher_update( &operation,
2988 text, sizeof( text ),
2989 buffer, sizeof( buffer ),
2990 &length ),
2991 PSA_ERROR_BAD_STATE );
2992 PSA_ASSERT( psa_cipher_abort( &operation ) );
2993
2994 /* Call finish without calling setup beforehand. */
2995 TEST_EQUAL( psa_cipher_finish( &operation,
2996 buffer, sizeof( buffer ), &length ),
2997 PSA_ERROR_BAD_STATE );
2998 PSA_ASSERT( psa_cipher_abort( &operation ) );
2999
3000 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003001 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003002 /* Not calling update means we are encrypting an empty buffer, which is OK
3003 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003004 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003005 TEST_EQUAL( psa_cipher_finish( &operation,
3006 buffer, sizeof( buffer ), &length ),
3007 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003008 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003009 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003010 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003011
3012 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003013 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003014 PSA_ASSERT( psa_cipher_set_iv( &operation,
3015 iv, sizeof( iv ) ) );
3016 PSA_ASSERT( psa_cipher_finish( &operation,
3017 buffer, sizeof( buffer ), &length ) );
3018 TEST_EQUAL( psa_cipher_finish( &operation,
3019 buffer, sizeof( buffer ), &length ),
3020 PSA_ERROR_BAD_STATE );
3021 PSA_ASSERT( psa_cipher_abort( &operation ) );
3022
Ronald Cron5425a212020-08-04 14:58:35 +02003023 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003024
Jaeden Ameroab439972019-02-15 14:12:05 +00003025exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003026 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003027 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003028}
3029/* END_CASE */
3030
3031/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003032void cipher_encrypt_fail( int alg_arg,
3033 int key_type_arg,
3034 data_t *key_data,
3035 data_t *input,
3036 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003037{
Ronald Cron5425a212020-08-04 14:58:35 +02003038 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003039 psa_status_t status;
3040 psa_key_type_t key_type = key_type_arg;
3041 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003042 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003043 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003044 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003045 size_t output_length = 0;
3046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3047
3048 if ( PSA_ERROR_BAD_STATE != expected_status )
3049 {
3050 PSA_ASSERT( psa_crypto_init( ) );
3051
3052 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3053 psa_set_key_algorithm( &attributes, alg );
3054 psa_set_key_type( &attributes, key_type );
3055
3056 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3057 input->len );
3058 ASSERT_ALLOC( output, output_buffer_size );
3059
3060 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3061 &key ) );
3062 }
3063
3064 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3065 output_buffer_size, &output_length );
3066
3067 TEST_EQUAL( status, expected_status );
3068
3069exit:
3070 mbedtls_free( output );
3071 psa_destroy_key( key );
3072 PSA_DONE( );
3073}
3074/* END_CASE */
3075
3076/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003077void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3078 data_t *input, int iv_length,
3079 int expected_result )
3080{
3081 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3082 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3084 size_t output_buffer_size = 0;
3085 unsigned char *output = NULL;
3086
3087 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3088 ASSERT_ALLOC( output, output_buffer_size );
3089
3090 PSA_ASSERT( psa_crypto_init( ) );
3091
3092 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3093 psa_set_key_algorithm( &attributes, alg );
3094 psa_set_key_type( &attributes, key_type );
3095
3096 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3097 &key ) );
3098 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3099 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3100 iv_length ) );
3101
3102exit:
3103 psa_cipher_abort( &operation );
3104 mbedtls_free( output );
3105 psa_destroy_key( key );
3106 PSA_DONE( );
3107}
3108/* END_CASE */
3109
3110/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003111void cipher_encrypt_alg_without_iv( int alg_arg,
3112 int key_type_arg,
3113 data_t *key_data,
3114 data_t *input,
3115 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003116{
3117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3118 psa_key_type_t key_type = key_type_arg;
3119 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003120 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3121 uint8_t iv[1] = { 0x5a };
3122 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003123 unsigned char *output = NULL;
3124 size_t output_buffer_size = 0;
3125 size_t output_length = 0;
3126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3127
3128 PSA_ASSERT( psa_crypto_init( ) );
3129
3130 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3131 psa_set_key_algorithm( &attributes, alg );
3132 psa_set_key_type( &attributes, key_type );
3133
3134 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3135 ASSERT_ALLOC( output, output_buffer_size );
3136
3137 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3138 &key ) );
3139
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003140 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3141 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3142 PSA_ERROR_BAD_STATE );
3143 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3144 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
3145 &iv_length ),
3146 PSA_ERROR_BAD_STATE );
3147
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003148 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003149 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
3150 output_buffer_size, &output_length ) );
3151 TEST_ASSERT( output_length <=
3152 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3153 TEST_ASSERT( output_length <=
3154 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3155
3156 ASSERT_COMPARE( expected_output->x, expected_output->len,
3157 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003158
3159 /* Encrypt, multi-part */
3160 PSA_ASSERT( psa_cipher_abort( &operation ) );
3161 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3162
3163 PSA_ASSERT( psa_cipher_update( &operation, input->x, input->len,
3164 output, output_buffer_size,
3165 &output_length) );
3166 TEST_ASSERT( output_length <=
3167 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3168 TEST_ASSERT( output_length <=
3169 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3170
3171 ASSERT_COMPARE( expected_output->x, expected_output->len,
3172 output, output_length );
3173
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003174exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003175 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003176 mbedtls_free( output );
3177 psa_destroy_key( key );
3178 PSA_DONE( );
3179}
3180/* END_CASE */
3181
3182/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003183void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3184{
3185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3186 psa_algorithm_t alg = alg_arg;
3187 psa_key_type_t key_type = key_type_arg;
3188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3189 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3190 psa_status_t status;
3191
3192 PSA_ASSERT( psa_crypto_init( ) );
3193
3194 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3195 psa_set_key_algorithm( &attributes, alg );
3196 psa_set_key_type( &attributes, key_type );
3197
3198 /* Usage of either of these two size macros would cause divide by zero
3199 * with incorrect key types previously. Input length should be irrelevant
3200 * here. */
3201 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3202 0 );
3203 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3204
3205
3206 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3207 &key ) );
3208
3209 /* Should fail due to invalid alg type (to support invalid key type).
3210 * Encrypt or decrypt will end up in the same place. */
3211 status = psa_cipher_encrypt_setup( &operation, key, alg );
3212
3213 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3214
3215exit:
3216 psa_cipher_abort( &operation );
3217 psa_destroy_key( key );
3218 PSA_DONE( );
3219}
3220/* END_CASE */
3221
3222/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003223void cipher_encrypt_validation( int alg_arg,
3224 int key_type_arg,
3225 data_t *key_data,
3226 data_t *input )
3227{
3228 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3229 psa_key_type_t key_type = key_type_arg;
3230 psa_algorithm_t alg = alg_arg;
3231 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3232 unsigned char *output1 = NULL;
3233 size_t output1_buffer_size = 0;
3234 size_t output1_length = 0;
3235 unsigned char *output2 = NULL;
3236 size_t output2_buffer_size = 0;
3237 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003238 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003239 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003241
Gilles Peskine8817f612018-12-18 00:18:46 +01003242 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003243
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003244 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3245 psa_set_key_algorithm( &attributes, alg );
3246 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003247
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003248 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3249 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3250 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3251 ASSERT_ALLOC( output1, output1_buffer_size );
3252 ASSERT_ALLOC( output2, output2_buffer_size );
3253
Ronald Cron5425a212020-08-04 14:58:35 +02003254 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3255 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003256
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003257 /* The one-shot cipher encryption uses generated iv so validating
3258 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003259 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3260 output1_buffer_size, &output1_length ) );
3261 TEST_ASSERT( output1_length <=
3262 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3263 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003264 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003265
3266 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3267 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003268
Gilles Peskine8817f612018-12-18 00:18:46 +01003269 PSA_ASSERT( psa_cipher_update( &operation,
3270 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003271 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003272 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003273 TEST_ASSERT( function_output_length <=
3274 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3275 TEST_ASSERT( function_output_length <=
3276 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003277 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003278
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003279 PSA_ASSERT( psa_cipher_finish( &operation,
3280 output2 + output2_length,
3281 output2_buffer_size - output2_length,
3282 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003283 TEST_ASSERT( function_output_length <=
3284 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3285 TEST_ASSERT( function_output_length <=
3286 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003287 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003288
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003289 PSA_ASSERT( psa_cipher_abort( &operation ) );
3290 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3291 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003292
Gilles Peskine50e586b2018-06-08 14:28:46 +02003293exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003294 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003295 mbedtls_free( output1 );
3296 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003297 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003298 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003299}
3300/* END_CASE */
3301
3302/* BEGIN_CASE */
3303void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003304 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003305 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003306 int first_part_size_arg,
3307 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003308 data_t *expected_output,
3309 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003310{
Ronald Cron5425a212020-08-04 14:58:35 +02003311 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003312 psa_key_type_t key_type = key_type_arg;
3313 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003314 psa_status_t status;
3315 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003316 size_t first_part_size = first_part_size_arg;
3317 size_t output1_length = output1_length_arg;
3318 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003319 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003320 size_t output_buffer_size = 0;
3321 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003322 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003323 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003325
Gilles Peskine8817f612018-12-18 00:18:46 +01003326 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003327
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003328 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3329 psa_set_key_algorithm( &attributes, alg );
3330 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003331
Ronald Cron5425a212020-08-04 14:58:35 +02003332 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3333 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003334
Ronald Cron5425a212020-08-04 14:58:35 +02003335 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003336
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003337 if( iv->len > 0 )
3338 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003339 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003340 }
3341
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003342 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3343 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003344 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003345
Gilles Peskinee0866522019-02-19 19:44:00 +01003346 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003347 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3348 output, output_buffer_size,
3349 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003350 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003351 TEST_ASSERT( function_output_length <=
3352 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3353 TEST_ASSERT( function_output_length <=
3354 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003355 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003356
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003357 if( first_part_size < input->len )
3358 {
3359 PSA_ASSERT( psa_cipher_update( &operation,
3360 input->x + first_part_size,
3361 input->len - first_part_size,
3362 ( output_buffer_size == 0 ? NULL :
3363 output + total_output_length ),
3364 output_buffer_size - total_output_length,
3365 &function_output_length ) );
3366 TEST_ASSERT( function_output_length == output2_length );
3367 TEST_ASSERT( function_output_length <=
3368 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3369 alg,
3370 input->len - first_part_size ) );
3371 TEST_ASSERT( function_output_length <=
3372 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3373 total_output_length += function_output_length;
3374 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003375
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003376 status = psa_cipher_finish( &operation,
3377 ( output_buffer_size == 0 ? NULL :
3378 output + total_output_length ),
3379 output_buffer_size - total_output_length,
3380 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003381 TEST_ASSERT( function_output_length <=
3382 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3383 TEST_ASSERT( function_output_length <=
3384 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003385 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003386 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003387
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003388 if( expected_status == PSA_SUCCESS )
3389 {
3390 PSA_ASSERT( psa_cipher_abort( &operation ) );
3391
3392 ASSERT_COMPARE( expected_output->x, expected_output->len,
3393 output, total_output_length );
3394 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003395
3396exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003397 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003398 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003399 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003400 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003401}
3402/* END_CASE */
3403
3404/* BEGIN_CASE */
3405void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003406 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003407 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003408 int first_part_size_arg,
3409 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003410 data_t *expected_output,
3411 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003412{
Ronald Cron5425a212020-08-04 14:58:35 +02003413 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003414 psa_key_type_t key_type = key_type_arg;
3415 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003416 psa_status_t status;
3417 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003418 size_t first_part_size = first_part_size_arg;
3419 size_t output1_length = output1_length_arg;
3420 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003421 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003422 size_t output_buffer_size = 0;
3423 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003424 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003425 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003426 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003427
Gilles Peskine8817f612018-12-18 00:18:46 +01003428 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003429
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003430 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3431 psa_set_key_algorithm( &attributes, alg );
3432 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003433
Ronald Cron5425a212020-08-04 14:58:35 +02003434 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3435 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003436
Ronald Cron5425a212020-08-04 14:58:35 +02003437 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003438
Steven Cooreman177deba2020-09-07 17:14:14 +02003439 if( iv->len > 0 )
3440 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003441 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003442 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003443
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003444 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3445 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003446 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003447
Gilles Peskinee0866522019-02-19 19:44:00 +01003448 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003449 PSA_ASSERT( psa_cipher_update( &operation,
3450 input->x, first_part_size,
3451 output, output_buffer_size,
3452 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003453 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003454 TEST_ASSERT( function_output_length <=
3455 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3456 TEST_ASSERT( function_output_length <=
3457 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003458 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003459
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003460 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003461 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003462 PSA_ASSERT( psa_cipher_update( &operation,
3463 input->x + first_part_size,
3464 input->len - first_part_size,
3465 ( output_buffer_size == 0 ? NULL :
3466 output + total_output_length ),
3467 output_buffer_size - total_output_length,
3468 &function_output_length ) );
3469 TEST_ASSERT( function_output_length == output2_length );
3470 TEST_ASSERT( function_output_length <=
3471 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3472 alg,
3473 input->len - first_part_size ) );
3474 TEST_ASSERT( function_output_length <=
3475 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3476 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003477 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003478
Gilles Peskine50e586b2018-06-08 14:28:46 +02003479 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003480 ( output_buffer_size == 0 ? NULL :
3481 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003482 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003483 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003484 TEST_ASSERT( function_output_length <=
3485 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3486 TEST_ASSERT( function_output_length <=
3487 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003488 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003489 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003490
3491 if( expected_status == PSA_SUCCESS )
3492 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003493 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003494
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003495 ASSERT_COMPARE( expected_output->x, expected_output->len,
3496 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003497 }
3498
Gilles Peskine50e586b2018-06-08 14:28:46 +02003499exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003500 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003501 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003502 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003503 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003504}
3505/* END_CASE */
3506
Gilles Peskine50e586b2018-06-08 14:28:46 +02003507/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003508void cipher_decrypt_fail( int alg_arg,
3509 int key_type_arg,
3510 data_t *key_data,
3511 data_t *iv,
3512 data_t *input_arg,
3513 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003514{
3515 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3516 psa_status_t status;
3517 psa_key_type_t key_type = key_type_arg;
3518 psa_algorithm_t alg = alg_arg;
3519 psa_status_t expected_status = expected_status_arg;
3520 unsigned char *input = NULL;
3521 size_t input_buffer_size = 0;
3522 unsigned char *output = NULL;
3523 size_t output_buffer_size = 0;
3524 size_t output_length = 0;
3525 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3526
3527 if ( PSA_ERROR_BAD_STATE != expected_status )
3528 {
3529 PSA_ASSERT( psa_crypto_init( ) );
3530
3531 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3532 psa_set_key_algorithm( &attributes, alg );
3533 psa_set_key_type( &attributes, key_type );
3534
3535 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3536 &key ) );
3537 }
3538
3539 /* Allocate input buffer and copy the iv and the plaintext */
3540 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3541 if ( input_buffer_size > 0 )
3542 {
3543 ASSERT_ALLOC( input, input_buffer_size );
3544 memcpy( input, iv->x, iv->len );
3545 memcpy( input + iv->len, input_arg->x, input_arg->len );
3546 }
3547
3548 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3549 ASSERT_ALLOC( output, output_buffer_size );
3550
3551 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3552 output_buffer_size, &output_length );
3553 TEST_EQUAL( status, expected_status );
3554
3555exit:
3556 mbedtls_free( input );
3557 mbedtls_free( output );
3558 psa_destroy_key( key );
3559 PSA_DONE( );
3560}
3561/* END_CASE */
3562
3563/* BEGIN_CASE */
3564void cipher_decrypt( int alg_arg,
3565 int key_type_arg,
3566 data_t *key_data,
3567 data_t *iv,
3568 data_t *input_arg,
3569 data_t *expected_output )
3570{
3571 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3572 psa_key_type_t key_type = key_type_arg;
3573 psa_algorithm_t alg = alg_arg;
3574 unsigned char *input = NULL;
3575 size_t input_buffer_size = 0;
3576 unsigned char *output = NULL;
3577 size_t output_buffer_size = 0;
3578 size_t output_length = 0;
3579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3580
3581 PSA_ASSERT( psa_crypto_init( ) );
3582
3583 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3584 psa_set_key_algorithm( &attributes, alg );
3585 psa_set_key_type( &attributes, key_type );
3586
3587 /* Allocate input buffer and copy the iv and the plaintext */
3588 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3589 if ( input_buffer_size > 0 )
3590 {
3591 ASSERT_ALLOC( input, input_buffer_size );
3592 memcpy( input, iv->x, iv->len );
3593 memcpy( input + iv->len, input_arg->x, input_arg->len );
3594 }
3595
3596 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3597 ASSERT_ALLOC( output, output_buffer_size );
3598
3599 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3600 &key ) );
3601
3602 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3603 output_buffer_size, &output_length ) );
3604 TEST_ASSERT( output_length <=
3605 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3606 TEST_ASSERT( output_length <=
3607 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3608
3609 ASSERT_COMPARE( expected_output->x, expected_output->len,
3610 output, output_length );
3611exit:
3612 mbedtls_free( input );
3613 mbedtls_free( output );
3614 psa_destroy_key( key );
3615 PSA_DONE( );
3616}
3617/* END_CASE */
3618
3619/* BEGIN_CASE */
3620void cipher_verify_output( int alg_arg,
3621 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003622 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003623 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003624{
Ronald Cron5425a212020-08-04 14:58:35 +02003625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003626 psa_key_type_t key_type = key_type_arg;
3627 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003628 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003629 size_t output1_size = 0;
3630 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003631 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003632 size_t output2_size = 0;
3633 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003635
Gilles Peskine8817f612018-12-18 00:18:46 +01003636 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003637
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003638 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3639 psa_set_key_algorithm( &attributes, alg );
3640 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003641
Ronald Cron5425a212020-08-04 14:58:35 +02003642 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3643 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003644 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003645 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003646
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003647 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3648 output1, output1_size,
3649 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003650 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003651 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003652 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003653 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003654
3655 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003656 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003657
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003658 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3659 output2, output2_size,
3660 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003661 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003662 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003663 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003664 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003665
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003666 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003667
3668exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003669 mbedtls_free( output1 );
3670 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003671 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003672 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003673}
3674/* END_CASE */
3675
3676/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003677void cipher_verify_output_multipart( int alg_arg,
3678 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003679 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003680 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003681 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003682{
Ronald Cron5425a212020-08-04 14:58:35 +02003683 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003684 psa_key_type_t key_type = key_type_arg;
3685 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003686 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003687 unsigned char iv[16] = {0};
3688 size_t iv_size = 16;
3689 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003690 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003691 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003692 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003693 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003694 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003695 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003696 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003697 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3698 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003700
Gilles Peskine8817f612018-12-18 00:18:46 +01003701 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003702
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003703 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3704 psa_set_key_algorithm( &attributes, alg );
3705 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003706
Ronald Cron5425a212020-08-04 14:58:35 +02003707 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3708 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003709
Ronald Cron5425a212020-08-04 14:58:35 +02003710 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3711 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003712
Steven Cooreman177deba2020-09-07 17:14:14 +02003713 if( alg != PSA_ALG_ECB_NO_PADDING )
3714 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003715 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3716 iv, iv_size,
3717 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003718 }
3719
gabor-mezei-armceface22021-01-21 12:26:17 +01003720 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3721 TEST_ASSERT( output1_buffer_size <=
3722 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003723 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003724
Gilles Peskinee0866522019-02-19 19:44:00 +01003725 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003726
Gilles Peskine8817f612018-12-18 00:18:46 +01003727 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3728 output1, output1_buffer_size,
3729 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003730 TEST_ASSERT( function_output_length <=
3731 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3732 TEST_ASSERT( function_output_length <=
3733 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003734 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003735
Gilles Peskine8817f612018-12-18 00:18:46 +01003736 PSA_ASSERT( psa_cipher_update( &operation1,
3737 input->x + first_part_size,
3738 input->len - first_part_size,
3739 output1, output1_buffer_size,
3740 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003741 TEST_ASSERT( function_output_length <=
3742 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3743 alg,
3744 input->len - first_part_size ) );
3745 TEST_ASSERT( function_output_length <=
3746 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003747 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003748
Gilles Peskine8817f612018-12-18 00:18:46 +01003749 PSA_ASSERT( psa_cipher_finish( &operation1,
3750 output1 + output1_length,
3751 output1_buffer_size - output1_length,
3752 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003753 TEST_ASSERT( function_output_length <=
3754 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3755 TEST_ASSERT( function_output_length <=
3756 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003757 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003758
Gilles Peskine8817f612018-12-18 00:18:46 +01003759 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003760
Gilles Peskine048b7f02018-06-08 14:20:49 +02003761 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003762 TEST_ASSERT( output2_buffer_size <=
3763 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3764 TEST_ASSERT( output2_buffer_size <=
3765 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003766 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003767
Steven Cooreman177deba2020-09-07 17:14:14 +02003768 if( iv_length > 0 )
3769 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003770 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3771 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003772 }
Moran Pekerded84402018-06-06 16:36:50 +03003773
Gilles Peskine8817f612018-12-18 00:18:46 +01003774 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3775 output2, output2_buffer_size,
3776 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003777 TEST_ASSERT( function_output_length <=
3778 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3779 TEST_ASSERT( function_output_length <=
3780 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003781 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003782
Gilles Peskine8817f612018-12-18 00:18:46 +01003783 PSA_ASSERT( psa_cipher_update( &operation2,
3784 output1 + first_part_size,
3785 output1_length - first_part_size,
3786 output2, output2_buffer_size,
3787 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003788 TEST_ASSERT( function_output_length <=
3789 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3790 alg,
3791 output1_length - first_part_size ) );
3792 TEST_ASSERT( function_output_length <=
3793 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003794 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003795
Gilles Peskine8817f612018-12-18 00:18:46 +01003796 PSA_ASSERT( psa_cipher_finish( &operation2,
3797 output2 + output2_length,
3798 output2_buffer_size - output2_length,
3799 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003800 TEST_ASSERT( function_output_length <=
3801 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3802 TEST_ASSERT( function_output_length <=
3803 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003804 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003805
Gilles Peskine8817f612018-12-18 00:18:46 +01003806 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003807
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003808 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003809
3810exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003811 psa_cipher_abort( &operation1 );
3812 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003813 mbedtls_free( output1 );
3814 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003815 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003816 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003817}
3818/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003819
Gilles Peskine20035e32018-02-03 22:44:14 +01003820/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003821void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003822 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003823 data_t *nonce,
3824 data_t *additional_data,
3825 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003826 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003827{
Ronald Cron5425a212020-08-04 14:58:35 +02003828 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003829 psa_key_type_t key_type = key_type_arg;
3830 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003831 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003832 unsigned char *output_data = NULL;
3833 size_t output_size = 0;
3834 size_t output_length = 0;
3835 unsigned char *output_data2 = NULL;
3836 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003837 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003838 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003839 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003840
Gilles Peskine8817f612018-12-18 00:18:46 +01003841 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003842
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3844 psa_set_key_algorithm( &attributes, alg );
3845 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003846
Gilles Peskine049c7532019-05-15 20:22:09 +02003847 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003848 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003849 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3850 key_bits = psa_get_key_bits( &attributes );
3851
3852 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3853 alg );
3854 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3855 * should be exact. */
3856 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3857 expected_result != PSA_ERROR_NOT_SUPPORTED )
3858 {
3859 TEST_EQUAL( output_size,
3860 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3861 TEST_ASSERT( output_size <=
3862 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3863 }
3864 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003865
Steven Cooremanf49478b2021-02-15 15:19:25 +01003866 status = psa_aead_encrypt( key, alg,
3867 nonce->x, nonce->len,
3868 additional_data->x,
3869 additional_data->len,
3870 input_data->x, input_data->len,
3871 output_data, output_size,
3872 &output_length );
3873
3874 /* If the operation is not supported, just skip and not fail in case the
3875 * encryption involves a common limitation of cryptography hardwares and
3876 * an alternative implementation. */
3877 if( status == PSA_ERROR_NOT_SUPPORTED )
3878 {
3879 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3880 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3881 }
3882
3883 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003884
3885 if( PSA_SUCCESS == expected_result )
3886 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003887 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003888
Gilles Peskine003a4a92019-05-14 16:09:40 +02003889 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3890 * should be exact. */
3891 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003892 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003893
gabor-mezei-armceface22021-01-21 12:26:17 +01003894 TEST_ASSERT( input_data->len <=
3895 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3896
Ronald Cron5425a212020-08-04 14:58:35 +02003897 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003898 nonce->x, nonce->len,
3899 additional_data->x,
3900 additional_data->len,
3901 output_data, output_length,
3902 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003903 &output_length2 ),
3904 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003905
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003906 ASSERT_COMPARE( input_data->x, input_data->len,
3907 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003908 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003909
Gilles Peskinea1cac842018-06-11 19:33:02 +02003910exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003911 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003912 mbedtls_free( output_data );
3913 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003914 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003915}
3916/* END_CASE */
3917
3918/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003919void aead_encrypt( int key_type_arg, data_t *key_data,
3920 int alg_arg,
3921 data_t *nonce,
3922 data_t *additional_data,
3923 data_t *input_data,
3924 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003925{
Ronald Cron5425a212020-08-04 14:58:35 +02003926 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003927 psa_key_type_t key_type = key_type_arg;
3928 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003929 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003930 unsigned char *output_data = NULL;
3931 size_t output_size = 0;
3932 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003934 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003935
Gilles Peskine8817f612018-12-18 00:18:46 +01003936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003937
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003938 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3939 psa_set_key_algorithm( &attributes, alg );
3940 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003941
Gilles Peskine049c7532019-05-15 20:22:09 +02003942 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003943 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003944 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3945 key_bits = psa_get_key_bits( &attributes );
3946
3947 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3948 alg );
3949 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3950 * should be exact. */
3951 TEST_EQUAL( output_size,
3952 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3953 TEST_ASSERT( output_size <=
3954 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3955 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003956
Steven Cooremand588ea12021-01-11 19:36:04 +01003957 status = psa_aead_encrypt( key, alg,
3958 nonce->x, nonce->len,
3959 additional_data->x, additional_data->len,
3960 input_data->x, input_data->len,
3961 output_data, output_size,
3962 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003963
Ronald Cron28a45ed2021-02-09 20:35:42 +01003964 /* If the operation is not supported, just skip and not fail in case the
3965 * encryption involves a common limitation of cryptography hardwares and
3966 * an alternative implementation. */
3967 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003968 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003969 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3970 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003971 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003972
3973 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003974 ASSERT_COMPARE( expected_result->x, expected_result->len,
3975 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003976
Gilles Peskinea1cac842018-06-11 19:33:02 +02003977exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003978 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003979 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003980 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003981}
3982/* END_CASE */
3983
3984/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003985void aead_decrypt( int key_type_arg, data_t *key_data,
3986 int alg_arg,
3987 data_t *nonce,
3988 data_t *additional_data,
3989 data_t *input_data,
3990 data_t *expected_data,
3991 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003992{
Ronald Cron5425a212020-08-04 14:58:35 +02003993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003994 psa_key_type_t key_type = key_type_arg;
3995 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003996 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003997 unsigned char *output_data = NULL;
3998 size_t output_size = 0;
3999 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004000 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004001 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004002 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004003
Gilles Peskine8817f612018-12-18 00:18:46 +01004004 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004005
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004006 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4007 psa_set_key_algorithm( &attributes, alg );
4008 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004009
Gilles Peskine049c7532019-05-15 20:22:09 +02004010 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004011 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004012 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4013 key_bits = psa_get_key_bits( &attributes );
4014
4015 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4016 alg );
4017 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4018 expected_result != PSA_ERROR_NOT_SUPPORTED )
4019 {
4020 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4021 * should be exact. */
4022 TEST_EQUAL( output_size,
4023 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4024 TEST_ASSERT( output_size <=
4025 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4026 }
4027 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004028
Steven Cooremand588ea12021-01-11 19:36:04 +01004029 status = psa_aead_decrypt( key, alg,
4030 nonce->x, nonce->len,
4031 additional_data->x,
4032 additional_data->len,
4033 input_data->x, input_data->len,
4034 output_data, output_size,
4035 &output_length );
4036
Ronald Cron28a45ed2021-02-09 20:35:42 +01004037 /* If the operation is not supported, just skip and not fail in case the
4038 * decryption involves a common limitation of cryptography hardwares and
4039 * an alternative implementation. */
4040 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004041 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004042 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4043 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004044 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004045
4046 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004047
Gilles Peskine2d277862018-06-18 15:41:12 +02004048 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004049 ASSERT_COMPARE( expected_data->x, expected_data->len,
4050 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004051
Gilles Peskinea1cac842018-06-11 19:33:02 +02004052exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004053 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004054 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004055 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004056}
4057/* END_CASE */
4058
4059/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004060void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4061 int alg_arg,
4062 data_t *nonce,
4063 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004064 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004065 int do_set_lengths,
4066 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004067{
Paul Elliottd3f82412021-06-16 16:52:21 +01004068 size_t ad_part_len = 0;
4069 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004070 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004071
Paul Elliott32f46ba2021-09-23 18:24:36 +01004072 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004073 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004074 mbedtls_test_set_step( ad_part_len );
4075
4076 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004077 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004078 if( ad_part_len & 0x01 )
4079 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4080 else
4081 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004082 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004083
4084 /* Split ad into length(ad_part_len) parts. */
4085 if( !aead_multipart_internal_func( key_type_arg, key_data,
4086 alg_arg, nonce,
4087 additional_data,
4088 ad_part_len,
4089 input_data, -1,
4090 set_lengths_method,
4091 expected_output,
4092 1, 0 ) )
4093 break;
4094
4095 /* length(0) part, length(ad_part_len) part, length(0) part... */
4096 mbedtls_test_set_step( 1000 + ad_part_len );
4097
4098 if( !aead_multipart_internal_func( key_type_arg, key_data,
4099 alg_arg, nonce,
4100 additional_data,
4101 ad_part_len,
4102 input_data, -1,
4103 set_lengths_method,
4104 expected_output,
4105 1, 1 ) )
4106 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004107 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004108
Paul Elliott32f46ba2021-09-23 18:24:36 +01004109 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004110 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004111 /* Split data into length(data_part_len) parts. */
4112 mbedtls_test_set_step( 2000 + data_part_len );
4113
4114 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004115 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004116 if( data_part_len & 0x01 )
4117 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4118 else
4119 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004120 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004121
Paul Elliott32f46ba2021-09-23 18:24:36 +01004122 if( !aead_multipart_internal_func( key_type_arg, key_data,
4123 alg_arg, nonce,
4124 additional_data, -1,
4125 input_data, data_part_len,
4126 set_lengths_method,
4127 expected_output,
4128 1, 0 ) )
4129 break;
4130
4131 /* length(0) part, length(data_part_len) part, length(0) part... */
4132 mbedtls_test_set_step( 3000 + data_part_len );
4133
4134 if( !aead_multipart_internal_func( key_type_arg, key_data,
4135 alg_arg, nonce,
4136 additional_data, -1,
4137 input_data, data_part_len,
4138 set_lengths_method,
4139 expected_output,
4140 1, 1 ) )
4141 break;
4142 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004143
Paul Elliott8fc45162021-06-23 16:06:01 +01004144 /* Goto is required to silence warnings about unused labels, as we
4145 * don't actually do any test assertions in this function. */
4146 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004147}
4148/* END_CASE */
4149
4150/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004151void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4152 int alg_arg,
4153 data_t *nonce,
4154 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004155 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004156 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004157 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004158{
Paul Elliottd3f82412021-06-16 16:52:21 +01004159 size_t ad_part_len = 0;
4160 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004161 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004162
Paul Elliott32f46ba2021-09-23 18:24:36 +01004163 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004164 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004165 /* Split ad into length(ad_part_len) parts. */
4166 mbedtls_test_set_step( ad_part_len );
4167
4168 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004169 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004170 if( ad_part_len & 0x01 )
4171 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4172 else
4173 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004174 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004175
4176 if( !aead_multipart_internal_func( key_type_arg, key_data,
4177 alg_arg, nonce,
4178 additional_data,
4179 ad_part_len,
4180 input_data, -1,
4181 set_lengths_method,
4182 expected_output,
4183 0, 0 ) )
4184 break;
4185
4186 /* length(0) part, length(ad_part_len) part, length(0) part... */
4187 mbedtls_test_set_step( 1000 + ad_part_len );
4188
4189 if( !aead_multipart_internal_func( key_type_arg, key_data,
4190 alg_arg, nonce,
4191 additional_data,
4192 ad_part_len,
4193 input_data, -1,
4194 set_lengths_method,
4195 expected_output,
4196 0, 1 ) )
4197 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004198 }
4199
Paul Elliott32f46ba2021-09-23 18:24:36 +01004200 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004201 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004202 /* Split data into length(data_part_len) parts. */
4203 mbedtls_test_set_step( 2000 + data_part_len );
4204
4205 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004206 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004207 if( data_part_len & 0x01 )
4208 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4209 else
4210 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004211 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004212
4213 if( !aead_multipart_internal_func( key_type_arg, key_data,
4214 alg_arg, nonce,
4215 additional_data, -1,
4216 input_data, data_part_len,
4217 set_lengths_method,
4218 expected_output,
4219 0, 0 ) )
4220 break;
4221
4222 /* length(0) part, length(data_part_len) part, length(0) part... */
4223 mbedtls_test_set_step( 3000 + data_part_len );
4224
4225 if( !aead_multipart_internal_func( key_type_arg, key_data,
4226 alg_arg, nonce,
4227 additional_data, -1,
4228 input_data, data_part_len,
4229 set_lengths_method,
4230 expected_output,
4231 0, 1 ) )
4232 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004233 }
4234
Paul Elliott8fc45162021-06-23 16:06:01 +01004235 /* Goto is required to silence warnings about unused labels, as we
4236 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004237 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004238}
4239/* END_CASE */
4240
4241/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004242void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4243 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004244 int nonce_length,
4245 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004246 data_t *additional_data,
4247 data_t *input_data,
4248 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004249{
4250
4251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4252 psa_key_type_t key_type = key_type_arg;
4253 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004254 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004255 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4257 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004258 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004259 size_t actual_nonce_length = 0;
4260 size_t expected_nonce_length = expected_nonce_length_arg;
4261 unsigned char *output = NULL;
4262 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004263 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004264 size_t ciphertext_size = 0;
4265 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004266 size_t tag_length = 0;
4267 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004268
4269 PSA_ASSERT( psa_crypto_init( ) );
4270
4271 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4272 psa_set_key_algorithm( & attributes, alg );
4273 psa_set_key_type( & attributes, key_type );
4274
4275 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4276 &key ) );
4277
4278 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4279
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004280 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4281
Paul Elliottf1277632021-08-24 18:11:37 +01004282 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004283
Paul Elliottf1277632021-08-24 18:11:37 +01004284 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004285
Paul Elliottf1277632021-08-24 18:11:37 +01004286 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004287
Paul Elliottf1277632021-08-24 18:11:37 +01004288 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004289
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004290 status = psa_aead_encrypt_setup( &operation, key, alg );
4291
4292 /* If the operation is not supported, just skip and not fail in case the
4293 * encryption involves a common limitation of cryptography hardwares and
4294 * an alternative implementation. */
4295 if( status == PSA_ERROR_NOT_SUPPORTED )
4296 {
4297 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004298 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004299 }
4300
4301 PSA_ASSERT( status );
4302
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004303 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004304 nonce_length,
4305 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004306
Paul Elliott693bf312021-07-23 17:40:41 +01004307 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004308
Paul Elliottf1277632021-08-24 18:11:37 +01004309 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004310
Paul Elliott88ecbe12021-09-22 17:23:03 +01004311 if( expected_status == PSA_SUCCESS )
4312 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4313 alg ) );
4314
Paul Elliottd79c5c52021-10-06 21:49:41 +01004315 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004316
Paul Elliott693bf312021-07-23 17:40:41 +01004317 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004318 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004319 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004320 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4321 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004322
4323 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4324 additional_data->len ) );
4325
4326 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004327 output, output_size,
4328 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004329
Paul Elliottf1277632021-08-24 18:11:37 +01004330 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4331 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004332 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4333 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004334
4335exit:
4336 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004337 mbedtls_free( output );
4338 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004339 psa_aead_abort( &operation );
4340 PSA_DONE( );
4341}
4342/* END_CASE */
4343
4344/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004345void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4346 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004347 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004348 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004349 data_t *additional_data,
4350 data_t *input_data,
4351 int expected_status_arg )
4352{
4353
4354 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4355 psa_key_type_t key_type = key_type_arg;
4356 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004357 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004358 uint8_t *nonce_buffer = NULL;
4359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4360 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4361 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004362 unsigned char *output = NULL;
4363 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004364 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004365 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004366 size_t ciphertext_size = 0;
4367 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004368 size_t tag_length = 0;
4369 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004370 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004371 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004372
4373 PSA_ASSERT( psa_crypto_init( ) );
4374
4375 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4376 psa_set_key_algorithm( &attributes, alg );
4377 psa_set_key_type( &attributes, key_type );
4378
4379 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4380 &key ) );
4381
4382 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4383
4384 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4385
Paul Elliott6f0e7202021-08-25 12:57:18 +01004386 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004387
Paul Elliott6f0e7202021-08-25 12:57:18 +01004388 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004389
Paul Elliott6f0e7202021-08-25 12:57:18 +01004390 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004391
Paul Elliott6f0e7202021-08-25 12:57:18 +01004392 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004393
Paul Elliott863864a2021-07-23 17:28:31 +01004394 status = psa_aead_encrypt_setup( &operation, key, alg );
4395
4396 /* If the operation is not supported, just skip and not fail in case the
4397 * encryption involves a common limitation of cryptography hardwares and
4398 * an alternative implementation. */
4399 if( status == PSA_ERROR_NOT_SUPPORTED )
4400 {
4401 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004402 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004403 }
4404
4405 PSA_ASSERT( status );
4406
Paul Elliott4023ffd2021-09-10 16:21:22 +01004407 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4408 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004409 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004410 /* Arbitrary size buffer, to test zero length valid buffer. */
4411 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004412 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004413 }
4414 else
4415 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004416 /* If length is zero, then this will return NULL. */
4417 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004418 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004419
Paul Elliott4023ffd2021-09-10 16:21:22 +01004420 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004421 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004422 for( index = 0; index < nonce_length - 1; ++index )
4423 {
4424 nonce_buffer[index] = 'a' + index;
4425 }
Paul Elliott66696b52021-08-16 18:42:41 +01004426 }
Paul Elliott863864a2021-07-23 17:28:31 +01004427 }
4428
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004429 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4430 {
4431 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4432 input_data->len ) );
4433 }
4434
Paul Elliott6f0e7202021-08-25 12:57:18 +01004435 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004436
Paul Elliott693bf312021-07-23 17:40:41 +01004437 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004438
4439 if( expected_status == PSA_SUCCESS )
4440 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004441 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4442 {
4443 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4444 input_data->len ) );
4445 }
4446 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4447 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004448
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004449 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4450 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4451 additional_data->len ),
4452 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004453
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004454 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004455 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004456 &ciphertext_length ),
4457 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004458
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004459 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004460 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004461 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4462 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004463 }
4464
4465exit:
4466 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004467 mbedtls_free( output );
4468 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004469 mbedtls_free( nonce_buffer );
4470 psa_aead_abort( &operation );
4471 PSA_DONE( );
4472}
4473/* END_CASE */
4474
4475/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004476void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4477 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004478 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004479 data_t *nonce,
4480 data_t *additional_data,
4481 data_t *input_data,
4482 int expected_status_arg )
4483{
4484
4485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4486 psa_key_type_t key_type = key_type_arg;
4487 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004488 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4490 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4491 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004492 unsigned char *output = NULL;
4493 unsigned char *ciphertext = NULL;
4494 size_t output_size = output_size_arg;
4495 size_t ciphertext_size = 0;
4496 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004497 size_t tag_length = 0;
4498 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4499
4500 PSA_ASSERT( psa_crypto_init( ) );
4501
4502 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4503 psa_set_key_algorithm( &attributes, alg );
4504 psa_set_key_type( &attributes, key_type );
4505
4506 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4507 &key ) );
4508
4509 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4510
Paul Elliottc6d11d02021-09-01 12:04:23 +01004511 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004512
Paul Elliottc6d11d02021-09-01 12:04:23 +01004513 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004514
Paul Elliottc6d11d02021-09-01 12:04:23 +01004515 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004516
Paul Elliott43fbda62021-07-23 18:30:59 +01004517 status = psa_aead_encrypt_setup( &operation, key, alg );
4518
4519 /* If the operation is not supported, just skip and not fail in case the
4520 * encryption involves a common limitation of cryptography hardwares and
4521 * an alternative implementation. */
4522 if( status == PSA_ERROR_NOT_SUPPORTED )
4523 {
4524 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4525 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4526 }
4527
4528 PSA_ASSERT( status );
4529
Paul Elliott47b9a142021-10-07 15:04:57 +01004530 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4531 input_data->len ) );
4532
Paul Elliott43fbda62021-07-23 18:30:59 +01004533 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4534
4535 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4536 additional_data->len ) );
4537
4538 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004539 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004540
4541 TEST_EQUAL( status, expected_status );
4542
4543 if( expected_status == PSA_SUCCESS )
4544 {
4545 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004546 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4547 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004548 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4549 }
4550
4551exit:
4552 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004553 mbedtls_free( output );
4554 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004555 psa_aead_abort( &operation );
4556 PSA_DONE( );
4557}
4558/* END_CASE */
4559
Paul Elliott91b021e2021-07-23 18:52:31 +01004560/* BEGIN_CASE */
4561void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4562 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004563 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004564 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004565 data_t *nonce,
4566 data_t *additional_data,
4567 data_t *input_data,
4568 int expected_status_arg )
4569{
4570
4571 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4572 psa_key_type_t key_type = key_type_arg;
4573 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004574 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004575 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4576 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4577 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004578 unsigned char *ciphertext = NULL;
4579 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004580 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004581 size_t ciphertext_size = 0;
4582 size_t ciphertext_length = 0;
4583 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004584 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004585 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004586
4587 PSA_ASSERT( psa_crypto_init( ) );
4588
4589 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4590 psa_set_key_algorithm( &attributes, alg );
4591 psa_set_key_type( &attributes, key_type );
4592
4593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4594 &key ) );
4595
4596 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4597
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004598 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004599
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004600 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004601
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004602 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004603
Paul Elliott719c1322021-09-13 18:27:22 +01004604 ASSERT_ALLOC( tag_buffer, tag_size );
4605
Paul Elliott91b021e2021-07-23 18:52:31 +01004606 status = psa_aead_encrypt_setup( &operation, key, alg );
4607
4608 /* If the operation is not supported, just skip and not fail in case the
4609 * encryption involves a common limitation of cryptography hardwares and
4610 * an alternative implementation. */
4611 if( status == PSA_ERROR_NOT_SUPPORTED )
4612 {
4613 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4614 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4615 }
4616
4617 PSA_ASSERT( status );
4618
4619 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4620
Paul Elliott76bda482021-10-07 17:07:23 +01004621 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4622 input_data->len ) );
4623
Paul Elliott91b021e2021-07-23 18:52:31 +01004624 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4625 additional_data->len ) );
4626
4627 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004628 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004629
4630 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004631 status = psa_aead_finish( &operation, finish_ciphertext,
4632 finish_ciphertext_size,
4633 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004634 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004635
4636 TEST_EQUAL( status, expected_status );
4637
4638exit:
4639 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004640 mbedtls_free( ciphertext );
4641 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004642 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004643 psa_aead_abort( &operation );
4644 PSA_DONE( );
4645}
4646/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004647
4648/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004649void aead_multipart_verify( int key_type_arg, data_t *key_data,
4650 int alg_arg,
4651 data_t *nonce,
4652 data_t *additional_data,
4653 data_t *input_data,
4654 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004655 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004656 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004657 int expected_status_arg )
4658{
4659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4660 psa_key_type_t key_type = key_type_arg;
4661 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004662 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4664 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4665 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004666 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004667 unsigned char *plaintext = NULL;
4668 unsigned char *finish_plaintext = NULL;
4669 size_t plaintext_size = 0;
4670 size_t plaintext_length = 0;
4671 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004672 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004673 unsigned char *tag_buffer = NULL;
4674 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004675
4676 PSA_ASSERT( psa_crypto_init( ) );
4677
4678 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4679 psa_set_key_algorithm( &attributes, alg );
4680 psa_set_key_type( &attributes, key_type );
4681
4682 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4683 &key ) );
4684
4685 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4686
4687 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4688 input_data->len );
4689
4690 ASSERT_ALLOC( plaintext, plaintext_size );
4691
4692 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4693
4694 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4695
Paul Elliott9961a662021-09-17 19:19:02 +01004696 status = psa_aead_decrypt_setup( &operation, key, alg );
4697
4698 /* If the operation is not supported, just skip and not fail in case the
4699 * encryption involves a common limitation of cryptography hardwares and
4700 * an alternative implementation. */
4701 if( status == PSA_ERROR_NOT_SUPPORTED )
4702 {
4703 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4704 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4705 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004706 TEST_EQUAL( status, expected_setup_status );
4707
4708 if( status != PSA_SUCCESS )
4709 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004710
4711 PSA_ASSERT( status );
4712
4713 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4714
Paul Elliottfec6f372021-10-06 17:15:02 +01004715 status = psa_aead_set_lengths( &operation, additional_data->len,
4716 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004717 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004718
Paul Elliott9961a662021-09-17 19:19:02 +01004719 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4720 additional_data->len ) );
4721
4722 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4723 input_data->len,
4724 plaintext, plaintext_size,
4725 &plaintext_length ) );
4726
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004727 if( tag_usage == USE_GIVEN_TAG )
4728 {
4729 tag_buffer = tag->x;
4730 tag_size = tag->len;
4731 }
4732
Paul Elliott9961a662021-09-17 19:19:02 +01004733 status = psa_aead_verify( &operation, finish_plaintext,
4734 verify_plaintext_size,
4735 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004736 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004737
4738 TEST_EQUAL( status, expected_status );
4739
4740exit:
4741 psa_destroy_key( key );
4742 mbedtls_free( plaintext );
4743 mbedtls_free( finish_plaintext );
4744 psa_aead_abort( &operation );
4745 PSA_DONE( );
4746}
4747/* END_CASE */
4748
Paul Elliott9961a662021-09-17 19:19:02 +01004749/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004750void aead_multipart_setup( int key_type_arg, data_t *key_data,
4751 int alg_arg, int expected_status_arg )
4752{
4753 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4754 psa_key_type_t key_type = key_type_arg;
4755 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004756 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4758 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4759 psa_status_t expected_status = expected_status_arg;
4760
4761 PSA_ASSERT( psa_crypto_init( ) );
4762
4763 psa_set_key_usage_flags( &attributes,
4764 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4765 psa_set_key_algorithm( &attributes, alg );
4766 psa_set_key_type( &attributes, key_type );
4767
4768 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4769 &key ) );
4770
Paul Elliott5221ef62021-09-19 17:33:03 +01004771 status = psa_aead_encrypt_setup( &operation, key, alg );
4772
4773 TEST_EQUAL( status, expected_status );
4774
4775 psa_aead_abort( &operation );
4776
Paul Elliott5221ef62021-09-19 17:33:03 +01004777 status = psa_aead_decrypt_setup( &operation, key, alg );
4778
4779 TEST_EQUAL(status, expected_status );
4780
4781exit:
4782 psa_destroy_key( key );
4783 psa_aead_abort( &operation );
4784 PSA_DONE( );
4785}
4786/* END_CASE */
4787
4788/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004789void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4790 int alg_arg,
4791 data_t *nonce,
4792 data_t *additional_data,
4793 data_t *input_data )
4794{
4795 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4796 psa_key_type_t key_type = key_type_arg;
4797 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004798 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004799 unsigned char *output_data = NULL;
4800 unsigned char *final_data = NULL;
4801 size_t output_size = 0;
4802 size_t finish_output_size = 0;
4803 size_t output_length = 0;
4804 size_t key_bits = 0;
4805 size_t tag_length = 0;
4806 size_t tag_size = 0;
4807 size_t nonce_length = 0;
4808 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4809 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4810 size_t output_part_length = 0;
4811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4812
4813 PSA_ASSERT( psa_crypto_init( ) );
4814
4815 psa_set_key_usage_flags( & attributes,
4816 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4817 psa_set_key_algorithm( & attributes, alg );
4818 psa_set_key_type( & attributes, key_type );
4819
4820 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4821 &key ) );
4822
4823 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4824 key_bits = psa_get_key_bits( &attributes );
4825
4826 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4827
4828 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4829
4830 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4831
4832 ASSERT_ALLOC( output_data, output_size );
4833
4834 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4835
4836 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4837
4838 ASSERT_ALLOC( final_data, finish_output_size );
4839
4840 /* Test all operations error without calling setup first. */
4841
Paul Elliottc23a9a02021-06-21 18:32:46 +01004842 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4843 PSA_ERROR_BAD_STATE );
4844
4845 psa_aead_abort( &operation );
4846
Paul Elliottc23a9a02021-06-21 18:32:46 +01004847 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4848 PSA_AEAD_NONCE_MAX_SIZE,
4849 &nonce_length ),
4850 PSA_ERROR_BAD_STATE );
4851
4852 psa_aead_abort( &operation );
4853
Paul Elliott481be342021-07-16 17:38:47 +01004854 /* ------------------------------------------------------- */
4855
Paul Elliottc23a9a02021-06-21 18:32:46 +01004856 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4857 input_data->len ),
4858 PSA_ERROR_BAD_STATE );
4859
4860 psa_aead_abort( &operation );
4861
Paul Elliott481be342021-07-16 17:38:47 +01004862 /* ------------------------------------------------------- */
4863
Paul Elliottc23a9a02021-06-21 18:32:46 +01004864 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4865 additional_data->len ),
4866 PSA_ERROR_BAD_STATE );
4867
4868 psa_aead_abort( &operation );
4869
Paul Elliott481be342021-07-16 17:38:47 +01004870 /* ------------------------------------------------------- */
4871
Paul Elliottc23a9a02021-06-21 18:32:46 +01004872 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4873 input_data->len, output_data,
4874 output_size, &output_length ),
4875 PSA_ERROR_BAD_STATE );
4876
4877 psa_aead_abort( &operation );
4878
Paul Elliott481be342021-07-16 17:38:47 +01004879 /* ------------------------------------------------------- */
4880
Paul Elliottc23a9a02021-06-21 18:32:46 +01004881 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4882 finish_output_size,
4883 &output_part_length,
4884 tag_buffer, tag_length,
4885 &tag_size ),
4886 PSA_ERROR_BAD_STATE );
4887
4888 psa_aead_abort( &operation );
4889
Paul Elliott481be342021-07-16 17:38:47 +01004890 /* ------------------------------------------------------- */
4891
Paul Elliottc23a9a02021-06-21 18:32:46 +01004892 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4893 finish_output_size,
4894 &output_part_length,
4895 tag_buffer,
4896 tag_length ),
4897 PSA_ERROR_BAD_STATE );
4898
4899 psa_aead_abort( &operation );
4900
4901 /* Test for double setups. */
4902
Paul Elliottc23a9a02021-06-21 18:32:46 +01004903 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4904
4905 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4906 PSA_ERROR_BAD_STATE );
4907
4908 psa_aead_abort( &operation );
4909
Paul Elliott481be342021-07-16 17:38:47 +01004910 /* ------------------------------------------------------- */
4911
Paul Elliottc23a9a02021-06-21 18:32:46 +01004912 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4913
4914 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4915 PSA_ERROR_BAD_STATE );
4916
4917 psa_aead_abort( &operation );
4918
Paul Elliott374a2be2021-07-16 17:53:40 +01004919 /* ------------------------------------------------------- */
4920
Paul Elliott374a2be2021-07-16 17:53:40 +01004921 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4922
4923 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4924 PSA_ERROR_BAD_STATE );
4925
4926 psa_aead_abort( &operation );
4927
4928 /* ------------------------------------------------------- */
4929
Paul Elliott374a2be2021-07-16 17:53:40 +01004930 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4931
4932 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4933 PSA_ERROR_BAD_STATE );
4934
4935 psa_aead_abort( &operation );
4936
Paul Elliottc23a9a02021-06-21 18:32:46 +01004937 /* Test for not setting a nonce. */
4938
Paul Elliottc23a9a02021-06-21 18:32:46 +01004939 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4940
4941 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4942 additional_data->len ),
4943 PSA_ERROR_BAD_STATE );
4944
4945 psa_aead_abort( &operation );
4946
Paul Elliott7f628422021-09-01 12:08:29 +01004947 /* ------------------------------------------------------- */
4948
Paul Elliott7f628422021-09-01 12:08:29 +01004949 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4950
4951 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4952 input_data->len, output_data,
4953 output_size, &output_length ),
4954 PSA_ERROR_BAD_STATE );
4955
4956 psa_aead_abort( &operation );
4957
Paul Elliottbdc2c682021-09-21 18:37:10 +01004958 /* ------------------------------------------------------- */
4959
Paul Elliottbdc2c682021-09-21 18:37:10 +01004960 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4961
4962 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4963 finish_output_size,
4964 &output_part_length,
4965 tag_buffer, tag_length,
4966 &tag_size ),
4967 PSA_ERROR_BAD_STATE );
4968
4969 psa_aead_abort( &operation );
4970
4971 /* ------------------------------------------------------- */
4972
Paul Elliottbdc2c682021-09-21 18:37:10 +01004973 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4974
4975 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4976 finish_output_size,
4977 &output_part_length,
4978 tag_buffer,
4979 tag_length ),
4980 PSA_ERROR_BAD_STATE );
4981
4982 psa_aead_abort( &operation );
4983
Paul Elliottc23a9a02021-06-21 18:32:46 +01004984 /* Test for double setting nonce. */
4985
Paul Elliottc23a9a02021-06-21 18:32:46 +01004986 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4987
4988 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4989
4990 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4991 PSA_ERROR_BAD_STATE );
4992
4993 psa_aead_abort( &operation );
4994
Paul Elliott374a2be2021-07-16 17:53:40 +01004995 /* Test for double generating nonce. */
4996
Paul Elliott374a2be2021-07-16 17:53:40 +01004997 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4998
4999 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5000 PSA_AEAD_NONCE_MAX_SIZE,
5001 &nonce_length ) );
5002
5003 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5004 PSA_AEAD_NONCE_MAX_SIZE,
5005 &nonce_length ),
5006 PSA_ERROR_BAD_STATE );
5007
5008
5009 psa_aead_abort( &operation );
5010
5011 /* Test for generate nonce then set and vice versa */
5012
Paul Elliott374a2be2021-07-16 17:53:40 +01005013 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5014
5015 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5016 PSA_AEAD_NONCE_MAX_SIZE,
5017 &nonce_length ) );
5018
5019 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5020 PSA_ERROR_BAD_STATE );
5021
5022 psa_aead_abort( &operation );
5023
Andrzej Kurekad837522021-12-15 15:28:49 +01005024 /* Test for generating nonce after calling set lengths */
5025
5026 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5027
5028 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5029 input_data->len ) );
5030
5031 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5032 PSA_AEAD_NONCE_MAX_SIZE,
5033 &nonce_length ) );
5034
5035 psa_aead_abort( &operation );
5036
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005037 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005038
5039 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5040
5041 if( operation.alg == PSA_ALG_CCM )
5042 {
5043 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5044 input_data->len ),
5045 PSA_ERROR_INVALID_ARGUMENT );
5046 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5047 PSA_AEAD_NONCE_MAX_SIZE,
5048 &nonce_length ),
5049 PSA_ERROR_BAD_STATE );
5050 }
5051 else
5052 {
5053 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5054 input_data->len ) );
5055 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5056 PSA_AEAD_NONCE_MAX_SIZE,
5057 &nonce_length ) );
5058 }
5059
5060 psa_aead_abort( &operation );
5061
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005062 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005063#if SIZE_MAX > UINT32_MAX
5064 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5065
5066 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5067 {
5068 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5069 input_data->len ),
5070 PSA_ERROR_INVALID_ARGUMENT );
5071 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5072 PSA_AEAD_NONCE_MAX_SIZE,
5073 &nonce_length ),
5074 PSA_ERROR_BAD_STATE );
5075 }
5076 else
5077 {
5078 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5079 input_data->len ) );
5080 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5081 PSA_AEAD_NONCE_MAX_SIZE,
5082 &nonce_length ) );
5083 }
5084
5085 psa_aead_abort( &operation );
5086#endif
5087
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005088 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005089
5090 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5091
5092 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5093 PSA_AEAD_NONCE_MAX_SIZE,
5094 &nonce_length ) );
5095
5096 if( operation.alg == PSA_ALG_CCM )
5097 {
5098 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5099 input_data->len ),
5100 PSA_ERROR_INVALID_ARGUMENT );
5101 }
5102 else
5103 {
5104 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5105 input_data->len ) );
5106 }
5107
5108 psa_aead_abort( &operation );
5109
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005110 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005111 /* Test for setting nonce after calling set lengths */
5112
5113 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5114
5115 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5116 input_data->len ) );
5117
5118 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5119
5120 psa_aead_abort( &operation );
5121
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005122 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005123
5124 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5125
5126 if( operation.alg == PSA_ALG_CCM )
5127 {
5128 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5129 input_data->len ),
5130 PSA_ERROR_INVALID_ARGUMENT );
5131 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5132 PSA_ERROR_BAD_STATE );
5133 }
5134 else
5135 {
5136 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5137 input_data->len ) );
5138 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5139 }
5140
5141 psa_aead_abort( &operation );
5142
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005143 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005144#if SIZE_MAX > UINT32_MAX
5145 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5146
5147 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5148 {
5149 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5150 input_data->len ),
5151 PSA_ERROR_INVALID_ARGUMENT );
5152 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5153 PSA_ERROR_BAD_STATE );
5154 }
5155 else
5156 {
5157 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5158 input_data->len ) );
5159 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5160 }
5161
5162 psa_aead_abort( &operation );
5163#endif
5164
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005165 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005166
5167 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5168
5169 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5170
5171 if( operation.alg == PSA_ALG_CCM )
5172 {
5173 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5174 input_data->len ),
5175 PSA_ERROR_INVALID_ARGUMENT );
5176 }
5177 else
5178 {
5179 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5180 input_data->len ) );
5181 }
5182
5183 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005184
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005185 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005186#if SIZE_MAX > UINT32_MAX
5187 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5188
5189 if( operation.alg == PSA_ALG_GCM )
5190 {
5191 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5192 SIZE_MAX ),
5193 PSA_ERROR_INVALID_ARGUMENT );
5194 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5195 PSA_ERROR_BAD_STATE );
5196 }
5197 else if ( operation.alg != PSA_ALG_CCM )
5198 {
5199 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5200 SIZE_MAX ) );
5201 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5202 }
5203
5204 psa_aead_abort( &operation );
5205
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005206 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005207 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5208
5209 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5210
5211 if( operation.alg == PSA_ALG_GCM )
5212 {
5213 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5214 SIZE_MAX ),
5215 PSA_ERROR_INVALID_ARGUMENT );
5216 }
5217 else if ( operation.alg != PSA_ALG_CCM )
5218 {
5219 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5220 SIZE_MAX ) );
5221 }
5222
5223 psa_aead_abort( &operation );
5224#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005225
5226 /* ------------------------------------------------------- */
5227
Paul Elliott374a2be2021-07-16 17:53:40 +01005228 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5229
5230 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5231
5232 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5233 PSA_AEAD_NONCE_MAX_SIZE,
5234 &nonce_length ),
5235 PSA_ERROR_BAD_STATE );
5236
5237 psa_aead_abort( &operation );
5238
Paul Elliott7220cae2021-06-22 17:25:57 +01005239 /* Test for generating nonce in decrypt setup. */
5240
Paul Elliott7220cae2021-06-22 17:25:57 +01005241 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5242
5243 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5244 PSA_AEAD_NONCE_MAX_SIZE,
5245 &nonce_length ),
5246 PSA_ERROR_BAD_STATE );
5247
5248 psa_aead_abort( &operation );
5249
Paul Elliottc23a9a02021-06-21 18:32:46 +01005250 /* Test for setting lengths twice. */
5251
Paul Elliottc23a9a02021-06-21 18:32:46 +01005252 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5253
5254 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5255
5256 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5257 input_data->len ) );
5258
5259 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5260 input_data->len ),
5261 PSA_ERROR_BAD_STATE );
5262
5263 psa_aead_abort( &operation );
5264
Andrzej Kurekad837522021-12-15 15:28:49 +01005265 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005266
Paul Elliottc23a9a02021-06-21 18:32:46 +01005267 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5268
5269 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5270
Andrzej Kurekad837522021-12-15 15:28:49 +01005271 if( operation.alg == PSA_ALG_CCM )
5272 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005273
Andrzej Kurekad837522021-12-15 15:28:49 +01005274 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5275 additional_data->len ),
5276 PSA_ERROR_BAD_STATE );
5277 }
5278 else
5279 {
5280 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5281 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005282
Andrzej Kurekad837522021-12-15 15:28:49 +01005283 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5284 input_data->len ),
5285 PSA_ERROR_BAD_STATE );
5286 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005287 psa_aead_abort( &operation );
5288
5289 /* ------------------------------------------------------- */
5290
Paul Elliottf94bd992021-09-19 18:15:59 +01005291 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5292
5293 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5294
Andrzej Kurekad837522021-12-15 15:28:49 +01005295 if( operation.alg == PSA_ALG_CCM )
5296 {
5297 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5298 input_data->len, output_data,
5299 output_size, &output_length ),
5300 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005301
Andrzej Kurekad837522021-12-15 15:28:49 +01005302 }
5303 else
5304 {
5305 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5306 input_data->len, output_data,
5307 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005308
Andrzej Kurekad837522021-12-15 15:28:49 +01005309 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5310 input_data->len ),
5311 PSA_ERROR_BAD_STATE );
5312 }
5313 psa_aead_abort( &operation );
5314
5315 /* ------------------------------------------------------- */
5316
5317 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5318
5319 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5320
5321 if( operation.alg == PSA_ALG_CCM )
5322 {
5323 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5324 finish_output_size,
5325 &output_part_length,
5326 tag_buffer, tag_length,
5327 &tag_size ) );
5328 }
5329 else
5330 {
5331 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5332 finish_output_size,
5333 &output_part_length,
5334 tag_buffer, tag_length,
5335 &tag_size ) );
5336
5337 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5338 input_data->len ),
5339 PSA_ERROR_BAD_STATE );
5340 }
5341 psa_aead_abort( &operation );
5342
5343 /* Test for setting lengths after generating nonce + already starting data. */
5344
5345 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5346
5347 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5348 PSA_AEAD_NONCE_MAX_SIZE,
5349 &nonce_length ) );
5350 if( operation.alg == PSA_ALG_CCM )
5351 {
5352
5353 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5354 additional_data->len ),
5355 PSA_ERROR_BAD_STATE );
5356 }
5357 else
5358 {
5359 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5360 additional_data->len ) );
5361
5362 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5363 input_data->len ),
5364 PSA_ERROR_BAD_STATE );
5365 }
5366 psa_aead_abort( &operation );
5367
5368 /* ------------------------------------------------------- */
5369
5370 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5371
5372 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5373 PSA_AEAD_NONCE_MAX_SIZE,
5374 &nonce_length ) );
5375 if( operation.alg == PSA_ALG_CCM )
5376 {
5377 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5378 input_data->len, output_data,
5379 output_size, &output_length ),
5380 PSA_ERROR_BAD_STATE );
5381
5382 }
5383 else
5384 {
5385 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5386 input_data->len, output_data,
5387 output_size, &output_length ) );
5388
5389 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5390 input_data->len ),
5391 PSA_ERROR_BAD_STATE );
5392 }
5393 psa_aead_abort( &operation );
5394
5395 /* ------------------------------------------------------- */
5396
5397 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5398
5399 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5400 PSA_AEAD_NONCE_MAX_SIZE,
5401 &nonce_length ) );
5402 if( operation.alg == PSA_ALG_CCM )
5403 {
5404 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5405 finish_output_size,
5406 &output_part_length,
5407 tag_buffer, tag_length,
5408 &tag_size ) );
5409 }
5410 else
5411 {
5412 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5413 finish_output_size,
5414 &output_part_length,
5415 tag_buffer, tag_length,
5416 &tag_size ) );
5417
5418 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5419 input_data->len ),
5420 PSA_ERROR_BAD_STATE );
5421 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005422 psa_aead_abort( &operation );
5423
Paul Elliott243080c2021-07-21 19:01:17 +01005424 /* Test for not sending any additional data or data after setting non zero
5425 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005426
Paul Elliottc23a9a02021-06-21 18:32:46 +01005427 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5428
5429 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5430
5431 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5432 input_data->len ) );
5433
5434 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5435 finish_output_size,
5436 &output_part_length,
5437 tag_buffer, tag_length,
5438 &tag_size ),
5439 PSA_ERROR_INVALID_ARGUMENT );
5440
5441 psa_aead_abort( &operation );
5442
Paul Elliott243080c2021-07-21 19:01:17 +01005443 /* Test for not sending any additional data or data after setting non-zero
5444 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005445
Paul Elliottc23a9a02021-06-21 18:32:46 +01005446 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5447
5448 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5449
5450 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5451 input_data->len ) );
5452
5453 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5454 finish_output_size,
5455 &output_part_length,
5456 tag_buffer,
5457 tag_length ),
5458 PSA_ERROR_INVALID_ARGUMENT );
5459
5460 psa_aead_abort( &operation );
5461
Paul Elliott243080c2021-07-21 19:01:17 +01005462 /* Test for not sending any additional data after setting a non-zero length
5463 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005464
Paul Elliottc23a9a02021-06-21 18:32:46 +01005465 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5466
5467 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5468
5469 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5470 input_data->len ) );
5471
5472 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5473 input_data->len, output_data,
5474 output_size, &output_length ),
5475 PSA_ERROR_INVALID_ARGUMENT );
5476
5477 psa_aead_abort( &operation );
5478
Paul Elliottf94bd992021-09-19 18:15:59 +01005479 /* Test for not sending any data after setting a non-zero length for it.*/
5480
Paul Elliottf94bd992021-09-19 18:15:59 +01005481 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5482
5483 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5484
5485 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5486 input_data->len ) );
5487
5488 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5489 additional_data->len ) );
5490
5491 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5492 finish_output_size,
5493 &output_part_length,
5494 tag_buffer, tag_length,
5495 &tag_size ),
5496 PSA_ERROR_INVALID_ARGUMENT );
5497
5498 psa_aead_abort( &operation );
5499
Paul Elliottb0450fe2021-09-01 15:06:26 +01005500 /* Test for sending too much additional data after setting lengths. */
5501
Paul Elliottb0450fe2021-09-01 15:06:26 +01005502 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5503
5504 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5505
5506 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5507
5508
5509 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5510 additional_data->len ),
5511 PSA_ERROR_INVALID_ARGUMENT );
5512
5513 psa_aead_abort( &operation );
5514
Paul Elliotta2a09b02021-09-22 14:56:40 +01005515 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005516
5517 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5518
5519 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5520
5521 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5522 input_data->len ) );
5523
5524 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5525 additional_data->len ) );
5526
5527 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5528 1 ),
5529 PSA_ERROR_INVALID_ARGUMENT );
5530
5531 psa_aead_abort( &operation );
5532
Paul Elliottb0450fe2021-09-01 15:06:26 +01005533 /* Test for sending too much data after setting lengths. */
5534
Paul Elliottb0450fe2021-09-01 15:06:26 +01005535 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5536
5537 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5538
5539 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5540
5541 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5542 input_data->len, output_data,
5543 output_size, &output_length ),
5544 PSA_ERROR_INVALID_ARGUMENT );
5545
5546 psa_aead_abort( &operation );
5547
Paul Elliotta2a09b02021-09-22 14:56:40 +01005548 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005549
5550 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5551
5552 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5553
5554 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5555 input_data->len ) );
5556
5557 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5558 additional_data->len ) );
5559
5560 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5561 input_data->len, output_data,
5562 output_size, &output_length ) );
5563
5564 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5565 1, output_data,
5566 output_size, &output_length ),
5567 PSA_ERROR_INVALID_ARGUMENT );
5568
5569 psa_aead_abort( &operation );
5570
Paul Elliottc23a9a02021-06-21 18:32:46 +01005571 /* Test sending additional data after data. */
5572
Paul Elliottc23a9a02021-06-21 18:32:46 +01005573 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5574
5575 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5576
Andrzej Kurekad837522021-12-15 15:28:49 +01005577 if( operation.alg != PSA_ALG_CCM )
5578 {
5579 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5580 input_data->len, output_data,
5581 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005582
Andrzej Kurekad837522021-12-15 15:28:49 +01005583 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5584 additional_data->len ),
5585 PSA_ERROR_BAD_STATE );
5586 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005587 psa_aead_abort( &operation );
5588
Paul Elliott534d0b42021-06-22 19:15:20 +01005589 /* Test calling finish on decryption. */
5590
Paul Elliott534d0b42021-06-22 19:15:20 +01005591 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5592
5593 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5594
5595 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5596 finish_output_size,
5597 &output_part_length,
5598 tag_buffer, tag_length,
5599 &tag_size ),
5600 PSA_ERROR_BAD_STATE );
5601
5602 psa_aead_abort( &operation );
5603
5604 /* Test calling verify on encryption. */
5605
Paul Elliott534d0b42021-06-22 19:15:20 +01005606 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5607
5608 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5609
5610 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5611 finish_output_size,
5612 &output_part_length,
5613 tag_buffer,
5614 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005615 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005616
5617 psa_aead_abort( &operation );
5618
5619
Paul Elliottc23a9a02021-06-21 18:32:46 +01005620exit:
5621 psa_destroy_key( key );
5622 psa_aead_abort( &operation );
5623 mbedtls_free( output_data );
5624 mbedtls_free( final_data );
5625 PSA_DONE( );
5626}
5627/* END_CASE */
5628
5629/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005630void signature_size( int type_arg,
5631 int bits,
5632 int alg_arg,
5633 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005634{
5635 psa_key_type_t type = type_arg;
5636 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005637 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005638
Gilles Peskinefe11b722018-12-18 00:24:04 +01005639 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005640
Gilles Peskinee59236f2018-01-27 23:32:46 +01005641exit:
5642 ;
5643}
5644/* END_CASE */
5645
5646/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005647void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5648 int alg_arg, data_t *input_data,
5649 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005650{
Ronald Cron5425a212020-08-04 14:58:35 +02005651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005652 psa_key_type_t key_type = key_type_arg;
5653 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005654 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005655 unsigned char *signature = NULL;
5656 size_t signature_size;
5657 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005659
Gilles Peskine8817f612018-12-18 00:18:46 +01005660 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005661
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005662 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005663 psa_set_key_algorithm( &attributes, alg );
5664 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005665
Gilles Peskine049c7532019-05-15 20:22:09 +02005666 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005667 &key ) );
5668 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005669 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005670
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005671 /* Allocate a buffer which has the size advertized by the
5672 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005673 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005674 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005675 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005676 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005677 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005678
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005679 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005680 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005681 input_data->x, input_data->len,
5682 signature, signature_size,
5683 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005684 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005685 ASSERT_COMPARE( output_data->x, output_data->len,
5686 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005687
5688exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005689 /*
5690 * Key attributes may have been returned by psa_get_key_attributes()
5691 * thus reset them as required.
5692 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005693 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005694
Ronald Cron5425a212020-08-04 14:58:35 +02005695 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005696 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005697 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005698}
5699/* END_CASE */
5700
5701/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005702void sign_hash_fail( int key_type_arg, data_t *key_data,
5703 int alg_arg, data_t *input_data,
5704 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005705{
Ronald Cron5425a212020-08-04 14:58:35 +02005706 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005707 psa_key_type_t key_type = key_type_arg;
5708 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005709 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005710 psa_status_t actual_status;
5711 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005712 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005713 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005714 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005715
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005716 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005717
Gilles Peskine8817f612018-12-18 00:18:46 +01005718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005719
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005720 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005721 psa_set_key_algorithm( &attributes, alg );
5722 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005723
Gilles Peskine049c7532019-05-15 20:22:09 +02005724 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005725 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005726
Ronald Cron5425a212020-08-04 14:58:35 +02005727 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005728 input_data->x, input_data->len,
5729 signature, signature_size,
5730 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005731 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005732 /* The value of *signature_length is unspecified on error, but
5733 * whatever it is, it should be less than signature_size, so that
5734 * if the caller tries to read *signature_length bytes without
5735 * checking the error code then they don't overflow a buffer. */
5736 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005737
5738exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005739 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005740 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005741 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005742 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005743}
5744/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005745
5746/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005747void sign_verify_hash( int key_type_arg, data_t *key_data,
5748 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005749{
Ronald Cron5425a212020-08-04 14:58:35 +02005750 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005751 psa_key_type_t key_type = key_type_arg;
5752 psa_algorithm_t alg = alg_arg;
5753 size_t key_bits;
5754 unsigned char *signature = NULL;
5755 size_t signature_size;
5756 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005758
Gilles Peskine8817f612018-12-18 00:18:46 +01005759 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005760
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005761 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005762 psa_set_key_algorithm( &attributes, alg );
5763 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005764
Gilles Peskine049c7532019-05-15 20:22:09 +02005765 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005766 &key ) );
5767 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005768 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005769
5770 /* Allocate a buffer which has the size advertized by the
5771 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005772 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005773 key_bits, alg );
5774 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005775 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005776 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005777
5778 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005779 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005780 input_data->x, input_data->len,
5781 signature, signature_size,
5782 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005783 /* Check that the signature length looks sensible. */
5784 TEST_ASSERT( signature_length <= signature_size );
5785 TEST_ASSERT( signature_length > 0 );
5786
5787 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005788 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005789 input_data->x, input_data->len,
5790 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005791
5792 if( input_data->len != 0 )
5793 {
5794 /* Flip a bit in the input and verify that the signature is now
5795 * detected as invalid. Flip a bit at the beginning, not at the end,
5796 * because ECDSA may ignore the last few bits of the input. */
5797 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005798 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005799 input_data->x, input_data->len,
5800 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005801 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005802 }
5803
5804exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005805 /*
5806 * Key attributes may have been returned by psa_get_key_attributes()
5807 * thus reset them as required.
5808 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005809 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005810
Ronald Cron5425a212020-08-04 14:58:35 +02005811 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005812 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005813 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005814}
5815/* END_CASE */
5816
5817/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005818void verify_hash( int key_type_arg, data_t *key_data,
5819 int alg_arg, data_t *hash_data,
5820 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005821{
Ronald Cron5425a212020-08-04 14:58:35 +02005822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005823 psa_key_type_t key_type = key_type_arg;
5824 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005826
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005827 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005828
Gilles Peskine8817f612018-12-18 00:18:46 +01005829 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005830
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005832 psa_set_key_algorithm( &attributes, alg );
5833 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005834
Gilles Peskine049c7532019-05-15 20:22:09 +02005835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005836 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005837
Ronald Cron5425a212020-08-04 14:58:35 +02005838 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005839 hash_data->x, hash_data->len,
5840 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005841
itayzafrir5c753392018-05-08 11:18:38 +03005842exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005843 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005844 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005845 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005846}
5847/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005848
5849/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005850void verify_hash_fail( int key_type_arg, data_t *key_data,
5851 int alg_arg, data_t *hash_data,
5852 data_t *signature_data,
5853 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005854{
Ronald Cron5425a212020-08-04 14:58:35 +02005855 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005856 psa_key_type_t key_type = key_type_arg;
5857 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005858 psa_status_t actual_status;
5859 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005861
Gilles Peskine8817f612018-12-18 00:18:46 +01005862 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005863
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005864 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005865 psa_set_key_algorithm( &attributes, alg );
5866 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005867
Gilles Peskine049c7532019-05-15 20:22:09 +02005868 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005869 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005870
Ronald Cron5425a212020-08-04 14:58:35 +02005871 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005872 hash_data->x, hash_data->len,
5873 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005874 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005875
5876exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005877 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005878 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005879 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005880}
5881/* END_CASE */
5882
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005883/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005884void sign_message_deterministic( int key_type_arg,
5885 data_t *key_data,
5886 int alg_arg,
5887 data_t *input_data,
5888 data_t *output_data )
5889{
5890 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5891 psa_key_type_t key_type = key_type_arg;
5892 psa_algorithm_t alg = alg_arg;
5893 size_t key_bits;
5894 unsigned char *signature = NULL;
5895 size_t signature_size;
5896 size_t signature_length = 0xdeadbeef;
5897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5898
5899 PSA_ASSERT( psa_crypto_init( ) );
5900
5901 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5902 psa_set_key_algorithm( &attributes, alg );
5903 psa_set_key_type( &attributes, key_type );
5904
5905 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5906 &key ) );
5907 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5908 key_bits = psa_get_key_bits( &attributes );
5909
5910 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5911 TEST_ASSERT( signature_size != 0 );
5912 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5913 ASSERT_ALLOC( signature, signature_size );
5914
5915 PSA_ASSERT( psa_sign_message( key, alg,
5916 input_data->x, input_data->len,
5917 signature, signature_size,
5918 &signature_length ) );
5919
5920 ASSERT_COMPARE( output_data->x, output_data->len,
5921 signature, signature_length );
5922
5923exit:
5924 psa_reset_key_attributes( &attributes );
5925
5926 psa_destroy_key( key );
5927 mbedtls_free( signature );
5928 PSA_DONE( );
5929
5930}
5931/* END_CASE */
5932
5933/* BEGIN_CASE */
5934void sign_message_fail( int key_type_arg,
5935 data_t *key_data,
5936 int alg_arg,
5937 data_t *input_data,
5938 int signature_size_arg,
5939 int expected_status_arg )
5940{
5941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5942 psa_key_type_t key_type = key_type_arg;
5943 psa_algorithm_t alg = alg_arg;
5944 size_t signature_size = signature_size_arg;
5945 psa_status_t actual_status;
5946 psa_status_t expected_status = expected_status_arg;
5947 unsigned char *signature = NULL;
5948 size_t signature_length = 0xdeadbeef;
5949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5950
5951 ASSERT_ALLOC( signature, signature_size );
5952
5953 PSA_ASSERT( psa_crypto_init( ) );
5954
5955 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5956 psa_set_key_algorithm( &attributes, alg );
5957 psa_set_key_type( &attributes, key_type );
5958
5959 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5960 &key ) );
5961
5962 actual_status = psa_sign_message( key, alg,
5963 input_data->x, input_data->len,
5964 signature, signature_size,
5965 &signature_length );
5966 TEST_EQUAL( actual_status, expected_status );
5967 /* The value of *signature_length is unspecified on error, but
5968 * whatever it is, it should be less than signature_size, so that
5969 * if the caller tries to read *signature_length bytes without
5970 * checking the error code then they don't overflow a buffer. */
5971 TEST_ASSERT( signature_length <= signature_size );
5972
5973exit:
5974 psa_reset_key_attributes( &attributes );
5975 psa_destroy_key( key );
5976 mbedtls_free( signature );
5977 PSA_DONE( );
5978}
5979/* END_CASE */
5980
5981/* BEGIN_CASE */
5982void sign_verify_message( int key_type_arg,
5983 data_t *key_data,
5984 int alg_arg,
5985 data_t *input_data )
5986{
5987 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5988 psa_key_type_t key_type = key_type_arg;
5989 psa_algorithm_t alg = alg_arg;
5990 size_t key_bits;
5991 unsigned char *signature = NULL;
5992 size_t signature_size;
5993 size_t signature_length = 0xdeadbeef;
5994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5995
5996 PSA_ASSERT( psa_crypto_init( ) );
5997
5998 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5999 PSA_KEY_USAGE_VERIFY_MESSAGE );
6000 psa_set_key_algorithm( &attributes, alg );
6001 psa_set_key_type( &attributes, key_type );
6002
6003 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6004 &key ) );
6005 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6006 key_bits = psa_get_key_bits( &attributes );
6007
6008 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6009 TEST_ASSERT( signature_size != 0 );
6010 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
6011 ASSERT_ALLOC( signature, signature_size );
6012
6013 PSA_ASSERT( psa_sign_message( key, alg,
6014 input_data->x, input_data->len,
6015 signature, signature_size,
6016 &signature_length ) );
6017 TEST_ASSERT( signature_length <= signature_size );
6018 TEST_ASSERT( signature_length > 0 );
6019
6020 PSA_ASSERT( psa_verify_message( key, alg,
6021 input_data->x, input_data->len,
6022 signature, signature_length ) );
6023
6024 if( input_data->len != 0 )
6025 {
6026 /* Flip a bit in the input and verify that the signature is now
6027 * detected as invalid. Flip a bit at the beginning, not at the end,
6028 * because ECDSA may ignore the last few bits of the input. */
6029 input_data->x[0] ^= 1;
6030 TEST_EQUAL( psa_verify_message( key, alg,
6031 input_data->x, input_data->len,
6032 signature, signature_length ),
6033 PSA_ERROR_INVALID_SIGNATURE );
6034 }
6035
6036exit:
6037 psa_reset_key_attributes( &attributes );
6038
6039 psa_destroy_key( key );
6040 mbedtls_free( signature );
6041 PSA_DONE( );
6042}
6043/* END_CASE */
6044
6045/* BEGIN_CASE */
6046void verify_message( int key_type_arg,
6047 data_t *key_data,
6048 int alg_arg,
6049 data_t *input_data,
6050 data_t *signature_data )
6051{
6052 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6053 psa_key_type_t key_type = key_type_arg;
6054 psa_algorithm_t alg = alg_arg;
6055 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6056
6057 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
6058
6059 PSA_ASSERT( psa_crypto_init( ) );
6060
6061 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6062 psa_set_key_algorithm( &attributes, alg );
6063 psa_set_key_type( &attributes, key_type );
6064
6065 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6066 &key ) );
6067
6068 PSA_ASSERT( psa_verify_message( key, alg,
6069 input_data->x, input_data->len,
6070 signature_data->x, signature_data->len ) );
6071
6072exit:
6073 psa_reset_key_attributes( &attributes );
6074 psa_destroy_key( key );
6075 PSA_DONE( );
6076}
6077/* END_CASE */
6078
6079/* BEGIN_CASE */
6080void verify_message_fail( int key_type_arg,
6081 data_t *key_data,
6082 int alg_arg,
6083 data_t *hash_data,
6084 data_t *signature_data,
6085 int expected_status_arg )
6086{
6087 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6088 psa_key_type_t key_type = key_type_arg;
6089 psa_algorithm_t alg = alg_arg;
6090 psa_status_t actual_status;
6091 psa_status_t expected_status = expected_status_arg;
6092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6093
6094 PSA_ASSERT( psa_crypto_init( ) );
6095
6096 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6097 psa_set_key_algorithm( &attributes, alg );
6098 psa_set_key_type( &attributes, key_type );
6099
6100 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6101 &key ) );
6102
6103 actual_status = psa_verify_message( key, alg,
6104 hash_data->x, hash_data->len,
6105 signature_data->x,
6106 signature_data->len );
6107 TEST_EQUAL( actual_status, expected_status );
6108
6109exit:
6110 psa_reset_key_attributes( &attributes );
6111 psa_destroy_key( key );
6112 PSA_DONE( );
6113}
6114/* END_CASE */
6115
6116/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006117void asymmetric_encrypt( int key_type_arg,
6118 data_t *key_data,
6119 int alg_arg,
6120 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006121 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006122 int expected_output_length_arg,
6123 int expected_status_arg )
6124{
Ronald Cron5425a212020-08-04 14:58:35 +02006125 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006126 psa_key_type_t key_type = key_type_arg;
6127 psa_algorithm_t alg = alg_arg;
6128 size_t expected_output_length = expected_output_length_arg;
6129 size_t key_bits;
6130 unsigned char *output = NULL;
6131 size_t output_size;
6132 size_t output_length = ~0;
6133 psa_status_t actual_status;
6134 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006136
Gilles Peskine8817f612018-12-18 00:18:46 +01006137 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006138
Gilles Peskine656896e2018-06-29 19:12:28 +02006139 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006140 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6141 psa_set_key_algorithm( &attributes, alg );
6142 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006143 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006144 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006145
6146 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006147 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006148 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006149
Gilles Peskine656896e2018-06-29 19:12:28 +02006150 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006151 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006152 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006153
6154 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006155 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006156 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006157 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006158 output, output_size,
6159 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006160 TEST_EQUAL( actual_status, expected_status );
6161 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006162
Gilles Peskine68428122018-06-30 18:42:41 +02006163 /* If the label is empty, the test framework puts a non-null pointer
6164 * in label->x. Test that a null pointer works as well. */
6165 if( label->len == 0 )
6166 {
6167 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006168 if( output_size != 0 )
6169 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006170 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006171 input_data->x, input_data->len,
6172 NULL, label->len,
6173 output, output_size,
6174 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006175 TEST_EQUAL( actual_status, expected_status );
6176 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006177 }
6178
Gilles Peskine656896e2018-06-29 19:12:28 +02006179exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006180 /*
6181 * Key attributes may have been returned by psa_get_key_attributes()
6182 * thus reset them as required.
6183 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006184 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006185
Ronald Cron5425a212020-08-04 14:58:35 +02006186 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006187 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006188 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006189}
6190/* END_CASE */
6191
6192/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006193void asymmetric_encrypt_decrypt( int key_type_arg,
6194 data_t *key_data,
6195 int alg_arg,
6196 data_t *input_data,
6197 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006198{
Ronald Cron5425a212020-08-04 14:58:35 +02006199 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006200 psa_key_type_t key_type = key_type_arg;
6201 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006202 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006203 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006204 size_t output_size;
6205 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006206 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006207 size_t output2_size;
6208 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006210
Gilles Peskine8817f612018-12-18 00:18:46 +01006211 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006212
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006213 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6214 psa_set_key_algorithm( &attributes, alg );
6215 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006216
Gilles Peskine049c7532019-05-15 20:22:09 +02006217 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006218 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006219
6220 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006221 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006222 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006223
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006224 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006225 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006226 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006227
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006228 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006229 TEST_ASSERT( output2_size <=
6230 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6231 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006232 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006233
Gilles Peskineeebd7382018-06-08 18:11:54 +02006234 /* We test encryption by checking that encrypt-then-decrypt gives back
6235 * the original plaintext because of the non-optional random
6236 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006237 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006238 input_data->x, input_data->len,
6239 label->x, label->len,
6240 output, output_size,
6241 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006242 /* We don't know what ciphertext length to expect, but check that
6243 * it looks sensible. */
6244 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006245
Ronald Cron5425a212020-08-04 14:58:35 +02006246 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006247 output, output_length,
6248 label->x, label->len,
6249 output2, output2_size,
6250 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006251 ASSERT_COMPARE( input_data->x, input_data->len,
6252 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006253
6254exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006255 /*
6256 * Key attributes may have been returned by psa_get_key_attributes()
6257 * thus reset them as required.
6258 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006259 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006260
Ronald Cron5425a212020-08-04 14:58:35 +02006261 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006262 mbedtls_free( output );
6263 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006264 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006265}
6266/* END_CASE */
6267
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006268/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006269void asymmetric_decrypt( int key_type_arg,
6270 data_t *key_data,
6271 int alg_arg,
6272 data_t *input_data,
6273 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006274 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006275{
Ronald Cron5425a212020-08-04 14:58:35 +02006276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006277 psa_key_type_t key_type = key_type_arg;
6278 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006279 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006280 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006281 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006282 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006283 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006284
Gilles Peskine8817f612018-12-18 00:18:46 +01006285 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006286
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006287 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6288 psa_set_key_algorithm( &attributes, alg );
6289 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006290
Gilles Peskine049c7532019-05-15 20:22:09 +02006291 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006292 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006293
gabor-mezei-armceface22021-01-21 12:26:17 +01006294 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6295 key_bits = psa_get_key_bits( &attributes );
6296
6297 /* Determine the maximum ciphertext length */
6298 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6299 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6300 ASSERT_ALLOC( output, output_size );
6301
Ronald Cron5425a212020-08-04 14:58:35 +02006302 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006303 input_data->x, input_data->len,
6304 label->x, label->len,
6305 output,
6306 output_size,
6307 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006308 ASSERT_COMPARE( expected_data->x, expected_data->len,
6309 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006310
Gilles Peskine68428122018-06-30 18:42:41 +02006311 /* If the label is empty, the test framework puts a non-null pointer
6312 * in label->x. Test that a null pointer works as well. */
6313 if( label->len == 0 )
6314 {
6315 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006316 if( output_size != 0 )
6317 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006318 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006319 input_data->x, input_data->len,
6320 NULL, label->len,
6321 output,
6322 output_size,
6323 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006324 ASSERT_COMPARE( expected_data->x, expected_data->len,
6325 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006326 }
6327
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006328exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006329 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006330 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006331 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006332 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006333}
6334/* END_CASE */
6335
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006336/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006337void asymmetric_decrypt_fail( int key_type_arg,
6338 data_t *key_data,
6339 int alg_arg,
6340 data_t *input_data,
6341 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006342 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006343 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006344{
Ronald Cron5425a212020-08-04 14:58:35 +02006345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006346 psa_key_type_t key_type = key_type_arg;
6347 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006348 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006349 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006350 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006351 psa_status_t actual_status;
6352 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006354
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006355 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006356
Gilles Peskine8817f612018-12-18 00:18:46 +01006357 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006358
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006359 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6360 psa_set_key_algorithm( &attributes, alg );
6361 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006362
Gilles Peskine049c7532019-05-15 20:22:09 +02006363 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006364 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006365
Ronald Cron5425a212020-08-04 14:58:35 +02006366 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006367 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006368 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006369 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006370 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006371 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006372 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006373
Gilles Peskine68428122018-06-30 18:42:41 +02006374 /* If the label is empty, the test framework puts a non-null pointer
6375 * in label->x. Test that a null pointer works as well. */
6376 if( label->len == 0 )
6377 {
6378 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006379 if( output_size != 0 )
6380 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006381 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006382 input_data->x, input_data->len,
6383 NULL, label->len,
6384 output, output_size,
6385 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006386 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006387 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006388 }
6389
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006390exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006391 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006392 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006393 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006394 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006395}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006396/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006397
6398/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006399void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006400{
6401 /* Test each valid way of initializing the object, except for `= {0}`, as
6402 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6403 * though it's OK by the C standard. We could test for this, but we'd need
6404 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006405 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006406 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6407 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6408 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006409
6410 memset( &zero, 0, sizeof( zero ) );
6411
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006412 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006413 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006414 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006415 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006416 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006417 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006418 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006419
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006420 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006421 PSA_ASSERT( psa_key_derivation_abort(&func) );
6422 PSA_ASSERT( psa_key_derivation_abort(&init) );
6423 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006424}
6425/* END_CASE */
6426
Janos Follath16de4a42019-06-13 16:32:24 +01006427/* BEGIN_CASE */
6428void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006429{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006430 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006431 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006432 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006433
Gilles Peskine8817f612018-12-18 00:18:46 +01006434 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006435
Janos Follath16de4a42019-06-13 16:32:24 +01006436 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006437 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006438
6439exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006440 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006441 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006442}
6443/* END_CASE */
6444
Janos Follathaf3c2a02019-06-12 12:34:34 +01006445/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006446void derive_set_capacity( int alg_arg, int capacity_arg,
6447 int expected_status_arg )
6448{
6449 psa_algorithm_t alg = alg_arg;
6450 size_t capacity = capacity_arg;
6451 psa_status_t expected_status = expected_status_arg;
6452 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6453
6454 PSA_ASSERT( psa_crypto_init( ) );
6455
6456 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6457
6458 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6459 expected_status );
6460
6461exit:
6462 psa_key_derivation_abort( &operation );
6463 PSA_DONE( );
6464}
6465/* END_CASE */
6466
6467/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006468void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006469 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006470 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006471 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006472 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006473 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006474 int expected_status_arg3,
6475 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006476{
6477 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006478 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6479 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006480 psa_status_t expected_statuses[] = {expected_status_arg1,
6481 expected_status_arg2,
6482 expected_status_arg3};
6483 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006484 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6485 MBEDTLS_SVC_KEY_ID_INIT,
6486 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006487 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6489 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006490 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006491 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006492 psa_status_t expected_output_status = expected_output_status_arg;
6493 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006494
6495 PSA_ASSERT( psa_crypto_init( ) );
6496
6497 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6498 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006499
6500 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6501
6502 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6503 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006504 mbedtls_test_set_step( i );
6505 if( steps[i] == 0 )
6506 {
6507 /* Skip this step */
6508 }
6509 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006510 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006511 psa_set_key_type( &attributes, key_types[i] );
6512 PSA_ASSERT( psa_import_key( &attributes,
6513 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006514 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006515 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6516 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6517 {
6518 // When taking a private key as secret input, use key agreement
6519 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006520 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6521 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006522 expected_statuses[i] );
6523 }
6524 else
6525 {
6526 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006527 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006528 expected_statuses[i] );
6529 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006530 }
6531 else
6532 {
6533 TEST_EQUAL( psa_key_derivation_input_bytes(
6534 &operation, steps[i],
6535 inputs[i]->x, inputs[i]->len ),
6536 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006537 }
6538 }
6539
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006540 if( output_key_type != PSA_KEY_TYPE_NONE )
6541 {
6542 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006543 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006544 psa_set_key_bits( &attributes, 8 );
6545 actual_output_status =
6546 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006547 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006548 }
6549 else
6550 {
6551 uint8_t buffer[1];
6552 actual_output_status =
6553 psa_key_derivation_output_bytes( &operation,
6554 buffer, sizeof( buffer ) );
6555 }
6556 TEST_EQUAL( actual_output_status, expected_output_status );
6557
Janos Follathaf3c2a02019-06-12 12:34:34 +01006558exit:
6559 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006560 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6561 psa_destroy_key( keys[i] );
6562 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006563 PSA_DONE( );
6564}
6565/* END_CASE */
6566
Janos Follathd958bb72019-07-03 15:02:16 +01006567/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006568void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006569{
Janos Follathd958bb72019-07-03 15:02:16 +01006570 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006571 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006572 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006573 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006574 unsigned char input1[] = "Input 1";
6575 size_t input1_length = sizeof( input1 );
6576 unsigned char input2[] = "Input 2";
6577 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006578 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006579 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006580 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6581 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6582 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006584
Gilles Peskine8817f612018-12-18 00:18:46 +01006585 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006586
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006587 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6588 psa_set_key_algorithm( &attributes, alg );
6589 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006590
Gilles Peskine73676cb2019-05-15 20:15:10 +02006591 PSA_ASSERT( psa_import_key( &attributes,
6592 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006593 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006594
6595 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006596 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6597 input1, input1_length,
6598 input2, input2_length,
6599 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006600 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006601
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006602 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006603 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006604 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006605
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006606 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006607
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006608 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006609 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006610
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006611exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006612 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006613 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006614 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006615}
6616/* END_CASE */
6617
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006618/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006619void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006620{
6621 uint8_t output_buffer[16];
6622 size_t buffer_size = 16;
6623 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006624 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006625
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006626 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6627 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006628 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006629
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006630 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006631 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006632
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006633 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006634
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006635 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6636 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006637 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006638
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006639 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006640 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006641
6642exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006643 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006644}
6645/* END_CASE */
6646
6647/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006648void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006649 int step1_arg, data_t *input1,
6650 int step2_arg, data_t *input2,
6651 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006652 int requested_capacity_arg,
6653 data_t *expected_output1,
6654 data_t *expected_output2 )
6655{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006656 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006657 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6658 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006659 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6660 MBEDTLS_SVC_KEY_ID_INIT,
6661 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006662 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006663 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006664 uint8_t *expected_outputs[2] =
6665 {expected_output1->x, expected_output2->x};
6666 size_t output_sizes[2] =
6667 {expected_output1->len, expected_output2->len};
6668 size_t output_buffer_size = 0;
6669 uint8_t *output_buffer = NULL;
6670 size_t expected_capacity;
6671 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006672 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006673 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006674 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006675
6676 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6677 {
6678 if( output_sizes[i] > output_buffer_size )
6679 output_buffer_size = output_sizes[i];
6680 if( output_sizes[i] == 0 )
6681 expected_outputs[i] = NULL;
6682 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006683 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006684 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006685
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006686 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6687 psa_set_key_algorithm( &attributes, alg );
6688 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006689
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006690 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006691 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6692 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6693 requested_capacity ) );
6694 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006695 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006696 switch( steps[i] )
6697 {
6698 case 0:
6699 break;
6700 case PSA_KEY_DERIVATION_INPUT_SECRET:
6701 PSA_ASSERT( psa_import_key( &attributes,
6702 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006703 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006704
6705 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6706 {
6707 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6708 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6709 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6710 }
6711
Gilles Peskine1468da72019-05-29 17:35:49 +02006712 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006713 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006714 break;
6715 default:
6716 PSA_ASSERT( psa_key_derivation_input_bytes(
6717 &operation, steps[i],
6718 inputs[i]->x, inputs[i]->len ) );
6719 break;
6720 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006721 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006722
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006723 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006724 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006725 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006726 expected_capacity = requested_capacity;
6727
6728 /* Expansion phase. */
6729 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6730 {
6731 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006732 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006733 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006734 if( expected_capacity == 0 && output_sizes[i] == 0 )
6735 {
6736 /* Reading 0 bytes when 0 bytes are available can go either way. */
6737 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006738 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006739 continue;
6740 }
6741 else if( expected_capacity == 0 ||
6742 output_sizes[i] > expected_capacity )
6743 {
6744 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006745 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006746 expected_capacity = 0;
6747 continue;
6748 }
6749 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006750 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006751 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006752 ASSERT_COMPARE( output_buffer, output_sizes[i],
6753 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006754 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006755 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006756 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006757 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006758 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006759 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006760 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006761
6762exit:
6763 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006764 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006765 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6766 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006767 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006768}
6769/* END_CASE */
6770
6771/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006772void derive_full( int alg_arg,
6773 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006774 data_t *input1,
6775 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006776 int requested_capacity_arg )
6777{
Ronald Cron5425a212020-08-04 14:58:35 +02006778 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006779 psa_algorithm_t alg = alg_arg;
6780 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006781 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006782 unsigned char output_buffer[16];
6783 size_t expected_capacity = requested_capacity;
6784 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006786
Gilles Peskine8817f612018-12-18 00:18:46 +01006787 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006788
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006789 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6790 psa_set_key_algorithm( &attributes, alg );
6791 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006792
Gilles Peskine049c7532019-05-15 20:22:09 +02006793 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006794 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006795
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006796 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6797 input1->x, input1->len,
6798 input2->x, input2->len,
6799 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006800 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006801
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006802 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006803 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006804 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006805
6806 /* Expansion phase. */
6807 while( current_capacity > 0 )
6808 {
6809 size_t read_size = sizeof( output_buffer );
6810 if( read_size > current_capacity )
6811 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006812 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006813 output_buffer,
6814 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006815 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006816 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006817 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006818 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006819 }
6820
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006821 /* Check that the operation refuses to go over capacity. */
6822 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006823 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006824
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006825 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006826
6827exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006828 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006829 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006830 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006831}
6832/* END_CASE */
6833
Janos Follathe60c9052019-07-03 13:51:30 +01006834/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006835void derive_key_exercise( int alg_arg,
6836 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006837 data_t *input1,
6838 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006839 int derived_type_arg,
6840 int derived_bits_arg,
6841 int derived_usage_arg,
6842 int derived_alg_arg )
6843{
Ronald Cron5425a212020-08-04 14:58:35 +02006844 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6845 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006846 psa_algorithm_t alg = alg_arg;
6847 psa_key_type_t derived_type = derived_type_arg;
6848 size_t derived_bits = derived_bits_arg;
6849 psa_key_usage_t derived_usage = derived_usage_arg;
6850 psa_algorithm_t derived_alg = derived_alg_arg;
6851 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006852 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006854 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006855
Gilles Peskine8817f612018-12-18 00:18:46 +01006856 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006857
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006858 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6859 psa_set_key_algorithm( &attributes, alg );
6860 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006861 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006862 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006863
6864 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006865 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6866 input1->x, input1->len,
6867 input2->x, input2->len,
6868 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006869 goto exit;
6870
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006871 psa_set_key_usage_flags( &attributes, derived_usage );
6872 psa_set_key_algorithm( &attributes, derived_alg );
6873 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006874 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006875 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006876 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006877
6878 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006879 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006880 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6881 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006882
6883 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006884 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006885 goto exit;
6886
6887exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006888 /*
6889 * Key attributes may have been returned by psa_get_key_attributes()
6890 * thus reset them as required.
6891 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006892 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006893
6894 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006895 psa_destroy_key( base_key );
6896 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006897 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006898}
6899/* END_CASE */
6900
Janos Follath42fd8882019-07-03 14:17:09 +01006901/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006902void derive_key_export( int alg_arg,
6903 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006904 data_t *input1,
6905 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006906 int bytes1_arg,
6907 int bytes2_arg )
6908{
Ronald Cron5425a212020-08-04 14:58:35 +02006909 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6910 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006911 psa_algorithm_t alg = alg_arg;
6912 size_t bytes1 = bytes1_arg;
6913 size_t bytes2 = bytes2_arg;
6914 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006915 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006916 uint8_t *output_buffer = NULL;
6917 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006918 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6919 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006920 size_t length;
6921
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006922 ASSERT_ALLOC( output_buffer, capacity );
6923 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006924 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006925
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006926 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6927 psa_set_key_algorithm( &base_attributes, alg );
6928 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006929 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006930 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006931
6932 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006933 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6934 input1->x, input1->len,
6935 input2->x, input2->len,
6936 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006937 goto exit;
6938
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006939 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006940 output_buffer,
6941 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006942 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006943
6944 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006945 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6946 input1->x, input1->len,
6947 input2->x, input2->len,
6948 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006949 goto exit;
6950
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006951 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6952 psa_set_key_algorithm( &derived_attributes, 0 );
6953 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006954 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006955 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006956 &derived_key ) );
6957 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006958 export_buffer, bytes1,
6959 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006960 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006961 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006962 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006963 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006964 &derived_key ) );
6965 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006966 export_buffer + bytes1, bytes2,
6967 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006968 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006969
6970 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006971 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6972 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006973
6974exit:
6975 mbedtls_free( output_buffer );
6976 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006977 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006978 psa_destroy_key( base_key );
6979 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006980 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006981}
6982/* END_CASE */
6983
6984/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006985void derive_key( int alg_arg,
6986 data_t *key_data, data_t *input1, data_t *input2,
6987 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006988 int expected_status_arg,
6989 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006990{
Ronald Cron5425a212020-08-04 14:58:35 +02006991 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6992 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006993 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006994 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006995 size_t bits = bits_arg;
6996 psa_status_t expected_status = expected_status_arg;
6997 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6998 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6999 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7000
7001 PSA_ASSERT( psa_crypto_init( ) );
7002
7003 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7004 psa_set_key_algorithm( &base_attributes, alg );
7005 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7006 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007007 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02007008
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007009 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7010 input1->x, input1->len,
7011 input2->x, input2->len,
7012 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02007013 goto exit;
7014
7015 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7016 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007017 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02007018 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01007019
7020 psa_status_t status =
7021 psa_key_derivation_output_key( &derived_attributes,
7022 &operation,
7023 &derived_key );
7024 if( is_large_output > 0 )
7025 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7026 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02007027
7028exit:
7029 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007030 psa_destroy_key( base_key );
7031 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02007032 PSA_DONE( );
7033}
7034/* END_CASE */
7035
7036/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02007037void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02007038 int our_key_type_arg, int our_key_alg_arg,
7039 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02007040 int expected_status_arg )
7041{
Ronald Cron5425a212020-08-04 14:58:35 +02007042 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007043 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007044 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007045 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007046 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007048 psa_status_t expected_status = expected_status_arg;
7049 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007050
Gilles Peskine8817f612018-12-18 00:18:46 +01007051 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007052
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007053 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007054 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007055 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007056 PSA_ASSERT( psa_import_key( &attributes,
7057 our_key_data->x, our_key_data->len,
7058 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007059
Gilles Peskine77f40d82019-04-11 21:27:06 +02007060 /* The tests currently include inputs that should fail at either step.
7061 * Test cases that fail at the setup step should be changed to call
7062 * key_derivation_setup instead, and this function should be renamed
7063 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007064 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02007065 if( status == PSA_SUCCESS )
7066 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007067 TEST_EQUAL( psa_key_derivation_key_agreement(
7068 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
7069 our_key,
7070 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02007071 expected_status );
7072 }
7073 else
7074 {
7075 TEST_ASSERT( status == expected_status );
7076 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007077
7078exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007079 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007080 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007081 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007082}
7083/* END_CASE */
7084
7085/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007086void raw_key_agreement( int alg_arg,
7087 int our_key_type_arg, data_t *our_key_data,
7088 data_t *peer_key_data,
7089 data_t *expected_output )
7090{
Ronald Cron5425a212020-08-04 14:58:35 +02007091 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007092 psa_algorithm_t alg = alg_arg;
7093 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007095 unsigned char *output = NULL;
7096 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007097 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007098
7099 ASSERT_ALLOC( output, expected_output->len );
7100 PSA_ASSERT( psa_crypto_init( ) );
7101
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007102 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7103 psa_set_key_algorithm( &attributes, alg );
7104 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007105 PSA_ASSERT( psa_import_key( &attributes,
7106 our_key_data->x, our_key_data->len,
7107 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007108
gabor-mezei-armceface22021-01-21 12:26:17 +01007109 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7110 key_bits = psa_get_key_bits( &attributes );
7111
Gilles Peskinebe697d82019-05-16 18:00:41 +02007112 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7113 peer_key_data->x, peer_key_data->len,
7114 output, expected_output->len,
7115 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007116 ASSERT_COMPARE( output, output_length,
7117 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01007118 TEST_ASSERT( output_length <=
7119 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7120 TEST_ASSERT( output_length <=
7121 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007122
7123exit:
7124 mbedtls_free( output );
7125 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007126 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007127}
7128/* END_CASE */
7129
7130/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007131void key_agreement_capacity( int alg_arg,
7132 int our_key_type_arg, data_t *our_key_data,
7133 data_t *peer_key_data,
7134 int expected_capacity_arg )
7135{
Ronald Cron5425a212020-08-04 14:58:35 +02007136 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007137 psa_algorithm_t alg = alg_arg;
7138 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007139 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007140 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007141 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007142 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007143
Gilles Peskine8817f612018-12-18 00:18:46 +01007144 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007145
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7147 psa_set_key_algorithm( &attributes, alg );
7148 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007149 PSA_ASSERT( psa_import_key( &attributes,
7150 our_key_data->x, our_key_data->len,
7151 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007152
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007153 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007154 PSA_ASSERT( psa_key_derivation_key_agreement(
7155 &operation,
7156 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7157 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007158 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7159 {
7160 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007161 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007162 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007163 NULL, 0 ) );
7164 }
Gilles Peskine59685592018-09-18 12:11:34 +02007165
Gilles Peskinebf491972018-10-25 22:36:12 +02007166 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007167 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007168 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007169 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007170
Gilles Peskinebf491972018-10-25 22:36:12 +02007171 /* Test the actual capacity by reading the output. */
7172 while( actual_capacity > sizeof( output ) )
7173 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007174 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007175 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007176 actual_capacity -= sizeof( output );
7177 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007178 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007179 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007180 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007181 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007182
Gilles Peskine59685592018-09-18 12:11:34 +02007183exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007184 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007185 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007186 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007187}
7188/* END_CASE */
7189
7190/* BEGIN_CASE */
7191void key_agreement_output( int alg_arg,
7192 int our_key_type_arg, data_t *our_key_data,
7193 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007194 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007195{
Ronald Cron5425a212020-08-04 14:58:35 +02007196 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007197 psa_algorithm_t alg = alg_arg;
7198 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007199 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007201 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007202
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007203 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7204 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007205
Gilles Peskine8817f612018-12-18 00:18:46 +01007206 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007207
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007208 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7209 psa_set_key_algorithm( &attributes, alg );
7210 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007211 PSA_ASSERT( psa_import_key( &attributes,
7212 our_key_data->x, our_key_data->len,
7213 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007214
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007215 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007216 PSA_ASSERT( psa_key_derivation_key_agreement(
7217 &operation,
7218 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7219 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007220 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7221 {
7222 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007223 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007224 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007225 NULL, 0 ) );
7226 }
Gilles Peskine59685592018-09-18 12:11:34 +02007227
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007228 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007229 actual_output,
7230 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007231 ASSERT_COMPARE( actual_output, expected_output1->len,
7232 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007233 if( expected_output2->len != 0 )
7234 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007235 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007236 actual_output,
7237 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007238 ASSERT_COMPARE( actual_output, expected_output2->len,
7239 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007240 }
Gilles Peskine59685592018-09-18 12:11:34 +02007241
7242exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007243 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007244 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007245 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007246 mbedtls_free( actual_output );
7247}
7248/* END_CASE */
7249
7250/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007251void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007252{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007253 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007254 unsigned char *output = NULL;
7255 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007256 size_t i;
7257 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007258
Simon Butcher49f8e312020-03-03 15:51:50 +00007259 TEST_ASSERT( bytes_arg >= 0 );
7260
Gilles Peskine91892022021-02-08 19:50:26 +01007261 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007262 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007263
Gilles Peskine8817f612018-12-18 00:18:46 +01007264 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007265
Gilles Peskinea50d7392018-06-21 10:22:13 +02007266 /* Run several times, to ensure that every output byte will be
7267 * nonzero at least once with overwhelming probability
7268 * (2^(-8*number_of_runs)). */
7269 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007270 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007271 if( bytes != 0 )
7272 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007273 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007274
Gilles Peskinea50d7392018-06-21 10:22:13 +02007275 for( i = 0; i < bytes; i++ )
7276 {
7277 if( output[i] != 0 )
7278 ++changed[i];
7279 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007280 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007281
7282 /* Check that every byte was changed to nonzero at least once. This
7283 * validates that psa_generate_random is overwriting every byte of
7284 * the output buffer. */
7285 for( i = 0; i < bytes; i++ )
7286 {
7287 TEST_ASSERT( changed[i] != 0 );
7288 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007289
7290exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007291 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007292 mbedtls_free( output );
7293 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007294}
7295/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007296
7297/* BEGIN_CASE */
7298void generate_key( int type_arg,
7299 int bits_arg,
7300 int usage_arg,
7301 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007302 int expected_status_arg,
7303 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007304{
Ronald Cron5425a212020-08-04 14:58:35 +02007305 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007306 psa_key_type_t type = type_arg;
7307 psa_key_usage_t usage = usage_arg;
7308 size_t bits = bits_arg;
7309 psa_algorithm_t alg = alg_arg;
7310 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007311 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007312 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007313
Gilles Peskine8817f612018-12-18 00:18:46 +01007314 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007315
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007316 psa_set_key_usage_flags( &attributes, usage );
7317 psa_set_key_algorithm( &attributes, alg );
7318 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007319 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007320
7321 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007322 psa_status_t status = psa_generate_key( &attributes, &key );
7323
7324 if( is_large_key > 0 )
7325 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7326 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007327 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007328 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007329
7330 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007331 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007332 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7333 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007334
Gilles Peskine818ca122018-06-20 18:16:48 +02007335 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007336 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007337 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007338
7339exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007340 /*
7341 * Key attributes may have been returned by psa_get_key_attributes()
7342 * thus reset them as required.
7343 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007344 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007345
Ronald Cron5425a212020-08-04 14:58:35 +02007346 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007347 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007348}
7349/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007350
Ronald Cronee414c72021-03-18 18:50:08 +01007351/* 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 +02007352void generate_key_rsa( int bits_arg,
7353 data_t *e_arg,
7354 int expected_status_arg )
7355{
Ronald Cron5425a212020-08-04 14:58:35 +02007356 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007357 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007358 size_t bits = bits_arg;
7359 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7360 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7361 psa_status_t expected_status = expected_status_arg;
7362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7363 uint8_t *exported = NULL;
7364 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007365 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007366 size_t exported_length = SIZE_MAX;
7367 uint8_t *e_read_buffer = NULL;
7368 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007369 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007370 size_t e_read_length = SIZE_MAX;
7371
7372 if( e_arg->len == 0 ||
7373 ( e_arg->len == 3 &&
7374 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7375 {
7376 is_default_public_exponent = 1;
7377 e_read_size = 0;
7378 }
7379 ASSERT_ALLOC( e_read_buffer, e_read_size );
7380 ASSERT_ALLOC( exported, exported_size );
7381
7382 PSA_ASSERT( psa_crypto_init( ) );
7383
7384 psa_set_key_usage_flags( &attributes, usage );
7385 psa_set_key_algorithm( &attributes, alg );
7386 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7387 e_arg->x, e_arg->len ) );
7388 psa_set_key_bits( &attributes, bits );
7389
7390 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007391 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007392 if( expected_status != PSA_SUCCESS )
7393 goto exit;
7394
7395 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007396 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007397 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7398 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7399 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7400 e_read_buffer, e_read_size,
7401 &e_read_length ) );
7402 if( is_default_public_exponent )
7403 TEST_EQUAL( e_read_length, 0 );
7404 else
7405 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7406
7407 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007408 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007409 goto exit;
7410
7411 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007412 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007413 exported, exported_size,
7414 &exported_length ) );
7415 {
7416 uint8_t *p = exported;
7417 uint8_t *end = exported + exported_length;
7418 size_t len;
7419 /* RSAPublicKey ::= SEQUENCE {
7420 * modulus INTEGER, -- n
7421 * publicExponent INTEGER } -- e
7422 */
7423 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007424 MBEDTLS_ASN1_SEQUENCE |
7425 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007426 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007427 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7428 MBEDTLS_ASN1_INTEGER ) );
7429 if( len >= 1 && p[0] == 0 )
7430 {
7431 ++p;
7432 --len;
7433 }
7434 if( e_arg->len == 0 )
7435 {
7436 TEST_EQUAL( len, 3 );
7437 TEST_EQUAL( p[0], 1 );
7438 TEST_EQUAL( p[1], 0 );
7439 TEST_EQUAL( p[2], 1 );
7440 }
7441 else
7442 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7443 }
7444
7445exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007446 /*
7447 * Key attributes may have been returned by psa_get_key_attributes() or
7448 * set by psa_set_key_domain_parameters() thus reset them as required.
7449 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007450 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007451
Ronald Cron5425a212020-08-04 14:58:35 +02007452 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007453 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007454 mbedtls_free( e_read_buffer );
7455 mbedtls_free( exported );
7456}
7457/* END_CASE */
7458
Darryl Greend49a4992018-06-18 17:27:26 +01007459/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007460void persistent_key_load_key_from_storage( data_t *data,
7461 int type_arg, int bits_arg,
7462 int usage_flags_arg, int alg_arg,
7463 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007464{
Ronald Cron71016a92020-08-28 19:01:50 +02007465 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007466 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007467 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7468 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007469 psa_key_type_t type = type_arg;
7470 size_t bits = bits_arg;
7471 psa_key_usage_t usage_flags = usage_flags_arg;
7472 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007473 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007474 unsigned char *first_export = NULL;
7475 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007476 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007477 size_t first_exported_length;
7478 size_t second_exported_length;
7479
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007480 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7481 {
7482 ASSERT_ALLOC( first_export, export_size );
7483 ASSERT_ALLOC( second_export, export_size );
7484 }
Darryl Greend49a4992018-06-18 17:27:26 +01007485
Gilles Peskine8817f612018-12-18 00:18:46 +01007486 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007487
Gilles Peskinec87af662019-05-15 16:12:22 +02007488 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007489 psa_set_key_usage_flags( &attributes, usage_flags );
7490 psa_set_key_algorithm( &attributes, alg );
7491 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007492 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007493
Darryl Green0c6575a2018-11-07 16:05:30 +00007494 switch( generation_method )
7495 {
7496 case IMPORT_KEY:
7497 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007498 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007499 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007500 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007501
Darryl Green0c6575a2018-11-07 16:05:30 +00007502 case GENERATE_KEY:
7503 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007504 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007505 break;
7506
7507 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007508#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007509 {
7510 /* Create base key */
7511 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7512 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7513 psa_set_key_usage_flags( &base_attributes,
7514 PSA_KEY_USAGE_DERIVE );
7515 psa_set_key_algorithm( &base_attributes, derive_alg );
7516 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007517 PSA_ASSERT( psa_import_key( &base_attributes,
7518 data->x, data->len,
7519 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007520 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007521 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007522 PSA_ASSERT( psa_key_derivation_input_key(
7523 &operation,
7524 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007525 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007526 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007527 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007528 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7529 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007530 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007531 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007532 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007533 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007534 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007535#else
7536 TEST_ASSUME( ! "KDF not supported in this configuration" );
7537#endif
7538 break;
7539
7540 default:
7541 TEST_ASSERT( ! "generation_method not implemented in test" );
7542 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007543 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007544 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007545
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007546 /* Export the key if permitted by the key policy. */
7547 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7548 {
Ronald Cron5425a212020-08-04 14:58:35 +02007549 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007550 first_export, export_size,
7551 &first_exported_length ) );
7552 if( generation_method == IMPORT_KEY )
7553 ASSERT_COMPARE( data->x, data->len,
7554 first_export, first_exported_length );
7555 }
Darryl Greend49a4992018-06-18 17:27:26 +01007556
7557 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007558 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007559 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007560 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007561
Darryl Greend49a4992018-06-18 17:27:26 +01007562 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007563 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007564 TEST_ASSERT( mbedtls_svc_key_id_equal(
7565 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007566 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7567 PSA_KEY_LIFETIME_PERSISTENT );
7568 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7569 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007570 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007571 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007572 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007573
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007574 /* Export the key again if permitted by the key policy. */
7575 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007576 {
Ronald Cron5425a212020-08-04 14:58:35 +02007577 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007578 second_export, export_size,
7579 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007580 ASSERT_COMPARE( first_export, first_exported_length,
7581 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007582 }
7583
7584 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007585 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007586 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007587
7588exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007589 /*
7590 * Key attributes may have been returned by psa_get_key_attributes()
7591 * thus reset them as required.
7592 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007593 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007594
Darryl Greend49a4992018-06-18 17:27:26 +01007595 mbedtls_free( first_export );
7596 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007597 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007598 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007599 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007600 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007601}
7602/* END_CASE */