blob: 97bb8e74012a2b43c23376dedc58b44c932af63e [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 */
Neil Armstrongee9686b2022-02-25 15:47:34 +01001913 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01001914 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;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003043 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3044 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3045 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003046 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003047 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003048 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003049 size_t function_output_length;
3050 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003051 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3052
3053 if ( PSA_ERROR_BAD_STATE != expected_status )
3054 {
3055 PSA_ASSERT( psa_crypto_init( ) );
3056
3057 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3058 psa_set_key_algorithm( &attributes, alg );
3059 psa_set_key_type( &attributes, key_type );
3060
3061 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3062 input->len );
3063 ASSERT_ALLOC( output, output_buffer_size );
3064
3065 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3066 &key ) );
3067 }
3068
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003069 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003070 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3071 output_buffer_size, &output_length );
3072
3073 TEST_EQUAL( status, expected_status );
3074
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003075 /* Encrypt, multi-part */
3076 status = psa_cipher_encrypt_setup( &operation, key, alg );
3077 if( status == PSA_SUCCESS )
3078 {
3079 if( alg != PSA_ALG_ECB_NO_PADDING )
3080 {
3081 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3082 iv, iv_size,
3083 &iv_length ) );
3084 }
3085
3086 status = psa_cipher_update( &operation, input->x, input->len,
3087 output, output_buffer_size,
3088 &function_output_length );
3089 if( status == PSA_SUCCESS )
3090 {
3091 output_length += function_output_length;
3092
3093 status = psa_cipher_finish( &operation, output + output_length,
3094 output_buffer_size - output_length,
3095 &function_output_length );
3096
3097 TEST_EQUAL( status, expected_status );
3098 }
3099 else
3100 {
3101 TEST_EQUAL( status, expected_status );
3102 }
3103 }
3104 else
3105 {
3106 TEST_EQUAL( status, expected_status );
3107 }
3108
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003109exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003110 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003111 mbedtls_free( output );
3112 psa_destroy_key( key );
3113 PSA_DONE( );
3114}
3115/* END_CASE */
3116
3117/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003118void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3119 data_t *input, int iv_length,
3120 int expected_result )
3121{
3122 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3123 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3125 size_t output_buffer_size = 0;
3126 unsigned char *output = NULL;
3127
3128 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3129 ASSERT_ALLOC( output, output_buffer_size );
3130
3131 PSA_ASSERT( psa_crypto_init( ) );
3132
3133 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3134 psa_set_key_algorithm( &attributes, alg );
3135 psa_set_key_type( &attributes, key_type );
3136
3137 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3138 &key ) );
3139 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3140 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3141 iv_length ) );
3142
3143exit:
3144 psa_cipher_abort( &operation );
3145 mbedtls_free( output );
3146 psa_destroy_key( key );
3147 PSA_DONE( );
3148}
3149/* END_CASE */
3150
3151/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003152void cipher_encrypt_alg_without_iv( int alg_arg,
3153 int key_type_arg,
3154 data_t *key_data,
3155 data_t *input,
3156 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003157{
3158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3159 psa_key_type_t key_type = key_type_arg;
3160 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003161 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3162 uint8_t iv[1] = { 0x5a };
3163 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003164 unsigned char *output = NULL;
3165 size_t output_buffer_size = 0;
3166 size_t output_length = 0;
3167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3168
3169 PSA_ASSERT( psa_crypto_init( ) );
3170
3171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3172 psa_set_key_algorithm( &attributes, alg );
3173 psa_set_key_type( &attributes, key_type );
3174
3175 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3176 ASSERT_ALLOC( output, output_buffer_size );
3177
3178 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3179 &key ) );
3180
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003181 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3182 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3183 PSA_ERROR_BAD_STATE );
3184 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3185 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
3186 &iv_length ),
3187 PSA_ERROR_BAD_STATE );
3188
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003189 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003190 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
3191 output_buffer_size, &output_length ) );
3192 TEST_ASSERT( output_length <=
3193 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3194 TEST_ASSERT( output_length <=
3195 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3196
3197 ASSERT_COMPARE( expected_output->x, expected_output->len,
3198 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003199
3200 /* Encrypt, multi-part */
3201 PSA_ASSERT( psa_cipher_abort( &operation ) );
3202 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3203
3204 PSA_ASSERT( psa_cipher_update( &operation, input->x, input->len,
3205 output, output_buffer_size,
3206 &output_length) );
3207 TEST_ASSERT( output_length <=
3208 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3209 TEST_ASSERT( output_length <=
3210 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
3211
3212 ASSERT_COMPARE( expected_output->x, expected_output->len,
3213 output, output_length );
3214
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003215exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003216 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003217 mbedtls_free( output );
3218 psa_destroy_key( key );
3219 PSA_DONE( );
3220}
3221/* END_CASE */
3222
3223/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003224void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3225{
3226 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3227 psa_algorithm_t alg = alg_arg;
3228 psa_key_type_t key_type = key_type_arg;
3229 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3230 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3231 psa_status_t status;
3232
3233 PSA_ASSERT( psa_crypto_init( ) );
3234
3235 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3236 psa_set_key_algorithm( &attributes, alg );
3237 psa_set_key_type( &attributes, key_type );
3238
3239 /* Usage of either of these two size macros would cause divide by zero
3240 * with incorrect key types previously. Input length should be irrelevant
3241 * here. */
3242 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3243 0 );
3244 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3245
3246
3247 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3248 &key ) );
3249
3250 /* Should fail due to invalid alg type (to support invalid key type).
3251 * Encrypt or decrypt will end up in the same place. */
3252 status = psa_cipher_encrypt_setup( &operation, key, alg );
3253
3254 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3255
3256exit:
3257 psa_cipher_abort( &operation );
3258 psa_destroy_key( key );
3259 PSA_DONE( );
3260}
3261/* END_CASE */
3262
3263/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003264void cipher_encrypt_validation( int alg_arg,
3265 int key_type_arg,
3266 data_t *key_data,
3267 data_t *input )
3268{
3269 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3270 psa_key_type_t key_type = key_type_arg;
3271 psa_algorithm_t alg = alg_arg;
3272 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3273 unsigned char *output1 = NULL;
3274 size_t output1_buffer_size = 0;
3275 size_t output1_length = 0;
3276 unsigned char *output2 = NULL;
3277 size_t output2_buffer_size = 0;
3278 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003279 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003280 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003282
Gilles Peskine8817f612018-12-18 00:18:46 +01003283 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003284
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003285 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3286 psa_set_key_algorithm( &attributes, alg );
3287 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003288
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003289 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3290 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3291 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3292 ASSERT_ALLOC( output1, output1_buffer_size );
3293 ASSERT_ALLOC( output2, output2_buffer_size );
3294
Ronald Cron5425a212020-08-04 14:58:35 +02003295 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3296 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003297
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003298 /* The one-shot cipher encryption uses generated iv so validating
3299 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003300 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3301 output1_buffer_size, &output1_length ) );
3302 TEST_ASSERT( output1_length <=
3303 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3304 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003305 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003306
3307 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3308 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003309
Gilles Peskine8817f612018-12-18 00:18:46 +01003310 PSA_ASSERT( psa_cipher_update( &operation,
3311 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003312 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003313 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003314 TEST_ASSERT( function_output_length <=
3315 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3316 TEST_ASSERT( function_output_length <=
3317 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003318 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003319
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003320 PSA_ASSERT( psa_cipher_finish( &operation,
3321 output2 + output2_length,
3322 output2_buffer_size - output2_length,
3323 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003324 TEST_ASSERT( function_output_length <=
3325 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3326 TEST_ASSERT( function_output_length <=
3327 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003328 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003329
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003330 PSA_ASSERT( psa_cipher_abort( &operation ) );
3331 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3332 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003333
Gilles Peskine50e586b2018-06-08 14:28:46 +02003334exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003335 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003336 mbedtls_free( output1 );
3337 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003338 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003339 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003340}
3341/* END_CASE */
3342
3343/* BEGIN_CASE */
3344void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003345 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003346 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003347 int first_part_size_arg,
3348 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003349 data_t *expected_output,
3350 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003351{
Ronald Cron5425a212020-08-04 14:58:35 +02003352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003353 psa_key_type_t key_type = key_type_arg;
3354 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003355 psa_status_t status;
3356 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003357 size_t first_part_size = first_part_size_arg;
3358 size_t output1_length = output1_length_arg;
3359 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003360 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003361 size_t output_buffer_size = 0;
3362 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003363 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003364 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003366
Gilles Peskine8817f612018-12-18 00:18:46 +01003367 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003368
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003369 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3370 psa_set_key_algorithm( &attributes, alg );
3371 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003372
Ronald Cron5425a212020-08-04 14:58:35 +02003373 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3374 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003375
Ronald Cron5425a212020-08-04 14:58:35 +02003376 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003377
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003378 if( iv->len > 0 )
3379 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003380 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003381 }
3382
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003383 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3384 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003385 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003386
Gilles Peskinee0866522019-02-19 19:44:00 +01003387 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003388 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3389 output, output_buffer_size,
3390 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003391 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003392 TEST_ASSERT( function_output_length <=
3393 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3394 TEST_ASSERT( function_output_length <=
3395 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003396 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003397
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003398 if( first_part_size < input->len )
3399 {
3400 PSA_ASSERT( psa_cipher_update( &operation,
3401 input->x + first_part_size,
3402 input->len - first_part_size,
3403 ( output_buffer_size == 0 ? NULL :
3404 output + total_output_length ),
3405 output_buffer_size - total_output_length,
3406 &function_output_length ) );
3407 TEST_ASSERT( function_output_length == output2_length );
3408 TEST_ASSERT( function_output_length <=
3409 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3410 alg,
3411 input->len - first_part_size ) );
3412 TEST_ASSERT( function_output_length <=
3413 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3414 total_output_length += function_output_length;
3415 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003416
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003417 status = psa_cipher_finish( &operation,
3418 ( output_buffer_size == 0 ? NULL :
3419 output + total_output_length ),
3420 output_buffer_size - total_output_length,
3421 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003422 TEST_ASSERT( function_output_length <=
3423 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3424 TEST_ASSERT( function_output_length <=
3425 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003426 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003427 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003428
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003429 if( expected_status == PSA_SUCCESS )
3430 {
3431 PSA_ASSERT( psa_cipher_abort( &operation ) );
3432
3433 ASSERT_COMPARE( expected_output->x, expected_output->len,
3434 output, total_output_length );
3435 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003436
3437exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003438 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003439 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003440 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003441 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003442}
3443/* END_CASE */
3444
3445/* BEGIN_CASE */
3446void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003447 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003448 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003449 int first_part_size_arg,
3450 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003451 data_t *expected_output,
3452 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003453{
Ronald Cron5425a212020-08-04 14:58:35 +02003454 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003455 psa_key_type_t key_type = key_type_arg;
3456 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003457 psa_status_t status;
3458 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003459 size_t first_part_size = first_part_size_arg;
3460 size_t output1_length = output1_length_arg;
3461 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003462 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003463 size_t output_buffer_size = 0;
3464 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003465 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003466 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003468
Gilles Peskine8817f612018-12-18 00:18:46 +01003469 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003470
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003471 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3472 psa_set_key_algorithm( &attributes, alg );
3473 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003474
Ronald Cron5425a212020-08-04 14:58:35 +02003475 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3476 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003477
Ronald Cron5425a212020-08-04 14:58:35 +02003478 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003479
Steven Cooreman177deba2020-09-07 17:14:14 +02003480 if( iv->len > 0 )
3481 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003482 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003483 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003484
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003485 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3486 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003487 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003488
Gilles Peskinee0866522019-02-19 19:44:00 +01003489 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003490 PSA_ASSERT( psa_cipher_update( &operation,
3491 input->x, first_part_size,
3492 output, output_buffer_size,
3493 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003494 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003495 TEST_ASSERT( function_output_length <=
3496 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3497 TEST_ASSERT( function_output_length <=
3498 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003499 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003500
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003501 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003502 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003503 PSA_ASSERT( psa_cipher_update( &operation,
3504 input->x + first_part_size,
3505 input->len - first_part_size,
3506 ( output_buffer_size == 0 ? NULL :
3507 output + total_output_length ),
3508 output_buffer_size - total_output_length,
3509 &function_output_length ) );
3510 TEST_ASSERT( function_output_length == output2_length );
3511 TEST_ASSERT( function_output_length <=
3512 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3513 alg,
3514 input->len - first_part_size ) );
3515 TEST_ASSERT( function_output_length <=
3516 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3517 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003518 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003519
Gilles Peskine50e586b2018-06-08 14:28:46 +02003520 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003521 ( output_buffer_size == 0 ? NULL :
3522 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003523 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003524 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003525 TEST_ASSERT( function_output_length <=
3526 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3527 TEST_ASSERT( function_output_length <=
3528 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003529 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003530 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003531
3532 if( expected_status == PSA_SUCCESS )
3533 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003534 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003535
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003536 ASSERT_COMPARE( expected_output->x, expected_output->len,
3537 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003538 }
3539
Gilles Peskine50e586b2018-06-08 14:28:46 +02003540exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003541 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003542 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003543 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003544 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003545}
3546/* END_CASE */
3547
Gilles Peskine50e586b2018-06-08 14:28:46 +02003548/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003549void cipher_decrypt_fail( int alg_arg,
3550 int key_type_arg,
3551 data_t *key_data,
3552 data_t *iv,
3553 data_t *input_arg,
3554 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003555{
3556 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3557 psa_status_t status;
3558 psa_key_type_t key_type = key_type_arg;
3559 psa_algorithm_t alg = alg_arg;
3560 psa_status_t expected_status = expected_status_arg;
3561 unsigned char *input = NULL;
3562 size_t input_buffer_size = 0;
3563 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01003564 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003565 size_t output_buffer_size = 0;
3566 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01003567 size_t function_output_length;
3568 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3570
3571 if ( PSA_ERROR_BAD_STATE != expected_status )
3572 {
3573 PSA_ASSERT( psa_crypto_init( ) );
3574
3575 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3576 psa_set_key_algorithm( &attributes, alg );
3577 psa_set_key_type( &attributes, key_type );
3578
3579 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3580 &key ) );
3581 }
3582
3583 /* Allocate input buffer and copy the iv and the plaintext */
3584 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3585 if ( input_buffer_size > 0 )
3586 {
3587 ASSERT_ALLOC( input, input_buffer_size );
3588 memcpy( input, iv->x, iv->len );
3589 memcpy( input + iv->len, input_arg->x, input_arg->len );
3590 }
3591
3592 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3593 ASSERT_ALLOC( output, output_buffer_size );
3594
Neil Armstrong66a479f2022-02-07 15:41:19 +01003595 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003596 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3597 output_buffer_size, &output_length );
3598 TEST_EQUAL( status, expected_status );
3599
Neil Armstrong66a479f2022-02-07 15:41:19 +01003600 /* Decrypt, multi-part */
3601 status = psa_cipher_decrypt_setup( &operation, key, alg );
3602 if( status == PSA_SUCCESS )
3603 {
3604 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
3605 input_arg->len ) +
3606 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3607 ASSERT_ALLOC( output_multi, output_buffer_size );
3608
3609 if( iv->len > 0 )
3610 {
3611 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
3612
3613 if( status != PSA_SUCCESS )
3614 TEST_EQUAL( status, expected_status );
3615 }
3616
3617 if( status == PSA_SUCCESS )
3618 {
3619 status = psa_cipher_update( &operation,
3620 input_arg->x, input_arg->len,
3621 output_multi, output_buffer_size,
3622 &function_output_length );
3623 if( status == PSA_SUCCESS )
3624 {
3625 output_length = function_output_length;
3626
3627 status = psa_cipher_finish( &operation,
3628 output_multi + output_length,
3629 output_buffer_size - output_length,
3630 &function_output_length );
3631
3632 TEST_EQUAL( status, expected_status );
3633 }
3634 else
3635 {
3636 TEST_EQUAL( status, expected_status );
3637 }
3638 }
3639 else
3640 {
3641 TEST_EQUAL( status, expected_status );
3642 }
3643 }
3644 else
3645 {
3646 TEST_EQUAL( status, expected_status );
3647 }
3648
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003649exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01003650 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003651 mbedtls_free( input );
3652 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01003653 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003654 psa_destroy_key( key );
3655 PSA_DONE( );
3656}
3657/* END_CASE */
3658
3659/* BEGIN_CASE */
3660void cipher_decrypt( int alg_arg,
3661 int key_type_arg,
3662 data_t *key_data,
3663 data_t *iv,
3664 data_t *input_arg,
3665 data_t *expected_output )
3666{
3667 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3668 psa_key_type_t key_type = key_type_arg;
3669 psa_algorithm_t alg = alg_arg;
3670 unsigned char *input = NULL;
3671 size_t input_buffer_size = 0;
3672 unsigned char *output = NULL;
3673 size_t output_buffer_size = 0;
3674 size_t output_length = 0;
3675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3676
3677 PSA_ASSERT( psa_crypto_init( ) );
3678
3679 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3680 psa_set_key_algorithm( &attributes, alg );
3681 psa_set_key_type( &attributes, key_type );
3682
3683 /* Allocate input buffer and copy the iv and the plaintext */
3684 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3685 if ( input_buffer_size > 0 )
3686 {
3687 ASSERT_ALLOC( input, input_buffer_size );
3688 memcpy( input, iv->x, iv->len );
3689 memcpy( input + iv->len, input_arg->x, input_arg->len );
3690 }
3691
3692 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3693 ASSERT_ALLOC( output, output_buffer_size );
3694
3695 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3696 &key ) );
3697
3698 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3699 output_buffer_size, &output_length ) );
3700 TEST_ASSERT( output_length <=
3701 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3702 TEST_ASSERT( output_length <=
3703 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3704
3705 ASSERT_COMPARE( expected_output->x, expected_output->len,
3706 output, output_length );
3707exit:
3708 mbedtls_free( input );
3709 mbedtls_free( output );
3710 psa_destroy_key( key );
3711 PSA_DONE( );
3712}
3713/* END_CASE */
3714
3715/* BEGIN_CASE */
3716void cipher_verify_output( int alg_arg,
3717 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003718 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003719 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003720{
Ronald Cron5425a212020-08-04 14:58:35 +02003721 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003722 psa_key_type_t key_type = key_type_arg;
3723 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003724 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003725 size_t output1_size = 0;
3726 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003727 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003728 size_t output2_size = 0;
3729 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003730 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003731
Gilles Peskine8817f612018-12-18 00:18:46 +01003732 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003733
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003734 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3735 psa_set_key_algorithm( &attributes, alg );
3736 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003737
Ronald Cron5425a212020-08-04 14:58:35 +02003738 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3739 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003740 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003741 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003742
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003743 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3744 output1, output1_size,
3745 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003746 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003747 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003748 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003749 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003750
3751 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003752 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003753
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003754 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3755 output2, output2_size,
3756 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003757 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003758 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003759 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003760 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003761
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003762 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003763
3764exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003765 mbedtls_free( output1 );
3766 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003767 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003768 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003769}
3770/* END_CASE */
3771
3772/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003773void cipher_verify_output_multipart( int alg_arg,
3774 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003775 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003776 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003777 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003778{
Ronald Cron5425a212020-08-04 14:58:35 +02003779 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003780 psa_key_type_t key_type = key_type_arg;
3781 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003782 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003783 unsigned char iv[16] = {0};
3784 size_t iv_size = 16;
3785 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003786 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003787 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003788 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003789 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003790 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003791 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003792 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003793 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3794 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003795 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003796
Gilles Peskine8817f612018-12-18 00:18:46 +01003797 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003798
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003799 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3800 psa_set_key_algorithm( &attributes, alg );
3801 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003802
Ronald Cron5425a212020-08-04 14:58:35 +02003803 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3804 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003805
Ronald Cron5425a212020-08-04 14:58:35 +02003806 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3807 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003808
Steven Cooreman177deba2020-09-07 17:14:14 +02003809 if( alg != PSA_ALG_ECB_NO_PADDING )
3810 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003811 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3812 iv, iv_size,
3813 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003814 }
3815
gabor-mezei-armceface22021-01-21 12:26:17 +01003816 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3817 TEST_ASSERT( output1_buffer_size <=
3818 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003819 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003820
Gilles Peskinee0866522019-02-19 19:44:00 +01003821 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003822
Gilles Peskine8817f612018-12-18 00:18:46 +01003823 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3824 output1, output1_buffer_size,
3825 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003826 TEST_ASSERT( function_output_length <=
3827 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3828 TEST_ASSERT( function_output_length <=
3829 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003830 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003831
Gilles Peskine8817f612018-12-18 00:18:46 +01003832 PSA_ASSERT( psa_cipher_update( &operation1,
3833 input->x + first_part_size,
3834 input->len - first_part_size,
3835 output1, output1_buffer_size,
3836 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003837 TEST_ASSERT( function_output_length <=
3838 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3839 alg,
3840 input->len - first_part_size ) );
3841 TEST_ASSERT( function_output_length <=
3842 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003843 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003844
Gilles Peskine8817f612018-12-18 00:18:46 +01003845 PSA_ASSERT( psa_cipher_finish( &operation1,
3846 output1 + output1_length,
3847 output1_buffer_size - output1_length,
3848 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003849 TEST_ASSERT( function_output_length <=
3850 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3851 TEST_ASSERT( function_output_length <=
3852 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003853 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003854
Gilles Peskine8817f612018-12-18 00:18:46 +01003855 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003856
Gilles Peskine048b7f02018-06-08 14:20:49 +02003857 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003858 TEST_ASSERT( output2_buffer_size <=
3859 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3860 TEST_ASSERT( output2_buffer_size <=
3861 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003862 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003863
Steven Cooreman177deba2020-09-07 17:14:14 +02003864 if( iv_length > 0 )
3865 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003866 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3867 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003868 }
Moran Pekerded84402018-06-06 16:36:50 +03003869
Gilles Peskine8817f612018-12-18 00:18:46 +01003870 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3871 output2, output2_buffer_size,
3872 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003873 TEST_ASSERT( function_output_length <=
3874 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3875 TEST_ASSERT( function_output_length <=
3876 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003877 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003878
Gilles Peskine8817f612018-12-18 00:18:46 +01003879 PSA_ASSERT( psa_cipher_update( &operation2,
3880 output1 + first_part_size,
3881 output1_length - first_part_size,
3882 output2, output2_buffer_size,
3883 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003884 TEST_ASSERT( function_output_length <=
3885 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3886 alg,
3887 output1_length - first_part_size ) );
3888 TEST_ASSERT( function_output_length <=
3889 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003890 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003891
Gilles Peskine8817f612018-12-18 00:18:46 +01003892 PSA_ASSERT( psa_cipher_finish( &operation2,
3893 output2 + output2_length,
3894 output2_buffer_size - output2_length,
3895 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003896 TEST_ASSERT( function_output_length <=
3897 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3898 TEST_ASSERT( function_output_length <=
3899 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003900 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003901
Gilles Peskine8817f612018-12-18 00:18:46 +01003902 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003903
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003904 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003905
3906exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003907 psa_cipher_abort( &operation1 );
3908 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003909 mbedtls_free( output1 );
3910 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003911 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003912 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003913}
3914/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003915
Gilles Peskine20035e32018-02-03 22:44:14 +01003916/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003917void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003918 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003919 data_t *nonce,
3920 data_t *additional_data,
3921 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003922 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003923{
Ronald Cron5425a212020-08-04 14:58:35 +02003924 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003925 psa_key_type_t key_type = key_type_arg;
3926 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003927 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003928 unsigned char *output_data = NULL;
3929 size_t output_size = 0;
3930 size_t output_length = 0;
3931 unsigned char *output_data2 = NULL;
3932 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003933 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003934 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003935 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003936
Gilles Peskine8817f612018-12-18 00:18:46 +01003937 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003938
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003939 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3940 psa_set_key_algorithm( &attributes, alg );
3941 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003942
Gilles Peskine049c7532019-05-15 20:22:09 +02003943 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003944 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003945 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3946 key_bits = psa_get_key_bits( &attributes );
3947
3948 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3949 alg );
3950 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3951 * should be exact. */
3952 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3953 expected_result != PSA_ERROR_NOT_SUPPORTED )
3954 {
3955 TEST_EQUAL( output_size,
3956 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3957 TEST_ASSERT( output_size <=
3958 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3959 }
3960 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003961
Steven Cooremanf49478b2021-02-15 15:19:25 +01003962 status = psa_aead_encrypt( key, alg,
3963 nonce->x, nonce->len,
3964 additional_data->x,
3965 additional_data->len,
3966 input_data->x, input_data->len,
3967 output_data, output_size,
3968 &output_length );
3969
3970 /* If the operation is not supported, just skip and not fail in case the
3971 * encryption involves a common limitation of cryptography hardwares and
3972 * an alternative implementation. */
3973 if( status == PSA_ERROR_NOT_SUPPORTED )
3974 {
3975 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3976 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3977 }
3978
3979 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003980
3981 if( PSA_SUCCESS == expected_result )
3982 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003983 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003984
Gilles Peskine003a4a92019-05-14 16:09:40 +02003985 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3986 * should be exact. */
3987 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003988 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003989
gabor-mezei-armceface22021-01-21 12:26:17 +01003990 TEST_ASSERT( input_data->len <=
3991 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3992
Ronald Cron5425a212020-08-04 14:58:35 +02003993 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003994 nonce->x, nonce->len,
3995 additional_data->x,
3996 additional_data->len,
3997 output_data, output_length,
3998 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003999 &output_length2 ),
4000 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004001
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004002 ASSERT_COMPARE( input_data->x, input_data->len,
4003 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004004 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004005
Gilles Peskinea1cac842018-06-11 19:33:02 +02004006exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004007 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004008 mbedtls_free( output_data );
4009 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004010 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004011}
4012/* END_CASE */
4013
4014/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004015void aead_encrypt( int key_type_arg, data_t *key_data,
4016 int alg_arg,
4017 data_t *nonce,
4018 data_t *additional_data,
4019 data_t *input_data,
4020 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004021{
Ronald Cron5425a212020-08-04 14:58:35 +02004022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004023 psa_key_type_t key_type = key_type_arg;
4024 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004025 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004026 unsigned char *output_data = NULL;
4027 size_t output_size = 0;
4028 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004030 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004031
Gilles Peskine8817f612018-12-18 00:18:46 +01004032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004033
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004034 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4035 psa_set_key_algorithm( &attributes, alg );
4036 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004037
Gilles Peskine049c7532019-05-15 20:22:09 +02004038 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004039 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004040 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4041 key_bits = psa_get_key_bits( &attributes );
4042
4043 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4044 alg );
4045 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4046 * should be exact. */
4047 TEST_EQUAL( output_size,
4048 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4049 TEST_ASSERT( output_size <=
4050 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4051 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004052
Steven Cooremand588ea12021-01-11 19:36:04 +01004053 status = psa_aead_encrypt( key, alg,
4054 nonce->x, nonce->len,
4055 additional_data->x, additional_data->len,
4056 input_data->x, input_data->len,
4057 output_data, output_size,
4058 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004059
Ronald Cron28a45ed2021-02-09 20:35:42 +01004060 /* If the operation is not supported, just skip and not fail in case the
4061 * encryption involves a common limitation of cryptography hardwares and
4062 * an alternative implementation. */
4063 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004064 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004065 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4066 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004067 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004068
4069 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004070 ASSERT_COMPARE( expected_result->x, expected_result->len,
4071 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004072
Gilles Peskinea1cac842018-06-11 19:33:02 +02004073exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004074 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004075 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004076 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004077}
4078/* END_CASE */
4079
4080/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004081void aead_decrypt( int key_type_arg, data_t *key_data,
4082 int alg_arg,
4083 data_t *nonce,
4084 data_t *additional_data,
4085 data_t *input_data,
4086 data_t *expected_data,
4087 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004088{
Ronald Cron5425a212020-08-04 14:58:35 +02004089 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004090 psa_key_type_t key_type = key_type_arg;
4091 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004092 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004093 unsigned char *output_data = NULL;
4094 size_t output_size = 0;
4095 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004097 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004098 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004099
Gilles Peskine8817f612018-12-18 00:18:46 +01004100 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004101
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004102 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4103 psa_set_key_algorithm( &attributes, alg );
4104 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004105
Gilles Peskine049c7532019-05-15 20:22:09 +02004106 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004107 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004108 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4109 key_bits = psa_get_key_bits( &attributes );
4110
4111 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4112 alg );
4113 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4114 expected_result != PSA_ERROR_NOT_SUPPORTED )
4115 {
4116 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4117 * should be exact. */
4118 TEST_EQUAL( output_size,
4119 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4120 TEST_ASSERT( output_size <=
4121 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4122 }
4123 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004124
Steven Cooremand588ea12021-01-11 19:36:04 +01004125 status = psa_aead_decrypt( key, alg,
4126 nonce->x, nonce->len,
4127 additional_data->x,
4128 additional_data->len,
4129 input_data->x, input_data->len,
4130 output_data, output_size,
4131 &output_length );
4132
Ronald Cron28a45ed2021-02-09 20:35:42 +01004133 /* If the operation is not supported, just skip and not fail in case the
4134 * decryption involves a common limitation of cryptography hardwares and
4135 * an alternative implementation. */
4136 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004137 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004138 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4139 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004140 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004141
4142 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004143
Gilles Peskine2d277862018-06-18 15:41:12 +02004144 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004145 ASSERT_COMPARE( expected_data->x, expected_data->len,
4146 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004147
Gilles Peskinea1cac842018-06-11 19:33:02 +02004148exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004149 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004150 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004151 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004152}
4153/* END_CASE */
4154
4155/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004156void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4157 int alg_arg,
4158 data_t *nonce,
4159 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004160 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004161 int do_set_lengths,
4162 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004163{
Paul Elliottd3f82412021-06-16 16:52:21 +01004164 size_t ad_part_len = 0;
4165 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004166 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004167
Paul Elliott32f46ba2021-09-23 18:24:36 +01004168 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004169 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004170 mbedtls_test_set_step( ad_part_len );
4171
4172 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004173 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004174 if( ad_part_len & 0x01 )
4175 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4176 else
4177 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004178 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004179
4180 /* Split ad into length(ad_part_len) parts. */
4181 if( !aead_multipart_internal_func( key_type_arg, key_data,
4182 alg_arg, nonce,
4183 additional_data,
4184 ad_part_len,
4185 input_data, -1,
4186 set_lengths_method,
4187 expected_output,
4188 1, 0 ) )
4189 break;
4190
4191 /* length(0) part, length(ad_part_len) part, length(0) part... */
4192 mbedtls_test_set_step( 1000 + ad_part_len );
4193
4194 if( !aead_multipart_internal_func( key_type_arg, key_data,
4195 alg_arg, nonce,
4196 additional_data,
4197 ad_part_len,
4198 input_data, -1,
4199 set_lengths_method,
4200 expected_output,
4201 1, 1 ) )
4202 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004203 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004204
Paul Elliott32f46ba2021-09-23 18:24:36 +01004205 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004206 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004207 /* Split data into length(data_part_len) parts. */
4208 mbedtls_test_set_step( 2000 + data_part_len );
4209
4210 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004211 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004212 if( data_part_len & 0x01 )
4213 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4214 else
4215 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004216 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004217
Paul Elliott32f46ba2021-09-23 18:24:36 +01004218 if( !aead_multipart_internal_func( key_type_arg, key_data,
4219 alg_arg, nonce,
4220 additional_data, -1,
4221 input_data, data_part_len,
4222 set_lengths_method,
4223 expected_output,
4224 1, 0 ) )
4225 break;
4226
4227 /* length(0) part, length(data_part_len) part, length(0) part... */
4228 mbedtls_test_set_step( 3000 + data_part_len );
4229
4230 if( !aead_multipart_internal_func( key_type_arg, key_data,
4231 alg_arg, nonce,
4232 additional_data, -1,
4233 input_data, data_part_len,
4234 set_lengths_method,
4235 expected_output,
4236 1, 1 ) )
4237 break;
4238 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004239
Paul Elliott8fc45162021-06-23 16:06:01 +01004240 /* Goto is required to silence warnings about unused labels, as we
4241 * don't actually do any test assertions in this function. */
4242 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004243}
4244/* END_CASE */
4245
4246/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004247void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4248 int alg_arg,
4249 data_t *nonce,
4250 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004251 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004252 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004253 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004254{
Paul Elliottd3f82412021-06-16 16:52:21 +01004255 size_t ad_part_len = 0;
4256 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004257 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004258
Paul Elliott32f46ba2021-09-23 18:24:36 +01004259 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004260 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004261 /* Split ad into length(ad_part_len) parts. */
4262 mbedtls_test_set_step( ad_part_len );
4263
4264 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004265 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004266 if( ad_part_len & 0x01 )
4267 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4268 else
4269 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004270 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004271
4272 if( !aead_multipart_internal_func( key_type_arg, key_data,
4273 alg_arg, nonce,
4274 additional_data,
4275 ad_part_len,
4276 input_data, -1,
4277 set_lengths_method,
4278 expected_output,
4279 0, 0 ) )
4280 break;
4281
4282 /* length(0) part, length(ad_part_len) part, length(0) part... */
4283 mbedtls_test_set_step( 1000 + ad_part_len );
4284
4285 if( !aead_multipart_internal_func( key_type_arg, key_data,
4286 alg_arg, nonce,
4287 additional_data,
4288 ad_part_len,
4289 input_data, -1,
4290 set_lengths_method,
4291 expected_output,
4292 0, 1 ) )
4293 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004294 }
4295
Paul Elliott32f46ba2021-09-23 18:24:36 +01004296 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004297 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004298 /* Split data into length(data_part_len) parts. */
4299 mbedtls_test_set_step( 2000 + data_part_len );
4300
4301 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004302 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004303 if( data_part_len & 0x01 )
4304 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4305 else
4306 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004307 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004308
4309 if( !aead_multipart_internal_func( key_type_arg, key_data,
4310 alg_arg, nonce,
4311 additional_data, -1,
4312 input_data, data_part_len,
4313 set_lengths_method,
4314 expected_output,
4315 0, 0 ) )
4316 break;
4317
4318 /* length(0) part, length(data_part_len) part, length(0) part... */
4319 mbedtls_test_set_step( 3000 + data_part_len );
4320
4321 if( !aead_multipart_internal_func( key_type_arg, key_data,
4322 alg_arg, nonce,
4323 additional_data, -1,
4324 input_data, data_part_len,
4325 set_lengths_method,
4326 expected_output,
4327 0, 1 ) )
4328 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004329 }
4330
Paul Elliott8fc45162021-06-23 16:06:01 +01004331 /* Goto is required to silence warnings about unused labels, as we
4332 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004333 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004334}
4335/* END_CASE */
4336
4337/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004338void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4339 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004340 int nonce_length,
4341 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004342 data_t *additional_data,
4343 data_t *input_data,
4344 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004345{
4346
4347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4348 psa_key_type_t key_type = key_type_arg;
4349 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004350 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004351 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004354 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004355 size_t actual_nonce_length = 0;
4356 size_t expected_nonce_length = expected_nonce_length_arg;
4357 unsigned char *output = NULL;
4358 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004359 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004360 size_t ciphertext_size = 0;
4361 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004362 size_t tag_length = 0;
4363 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004364
4365 PSA_ASSERT( psa_crypto_init( ) );
4366
4367 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4368 psa_set_key_algorithm( & attributes, alg );
4369 psa_set_key_type( & attributes, key_type );
4370
4371 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4372 &key ) );
4373
4374 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4375
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004376 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4377
Paul Elliottf1277632021-08-24 18:11:37 +01004378 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004379
Paul Elliottf1277632021-08-24 18:11:37 +01004380 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004381
Paul Elliottf1277632021-08-24 18:11:37 +01004382 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004383
Paul Elliottf1277632021-08-24 18:11:37 +01004384 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004385
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004386 status = psa_aead_encrypt_setup( &operation, key, alg );
4387
4388 /* If the operation is not supported, just skip and not fail in case the
4389 * encryption involves a common limitation of cryptography hardwares and
4390 * an alternative implementation. */
4391 if( status == PSA_ERROR_NOT_SUPPORTED )
4392 {
4393 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004394 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004395 }
4396
4397 PSA_ASSERT( status );
4398
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004399 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004400 nonce_length,
4401 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004402
Paul Elliott693bf312021-07-23 17:40:41 +01004403 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004404
Paul Elliottf1277632021-08-24 18:11:37 +01004405 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004406
Paul Elliott88ecbe12021-09-22 17:23:03 +01004407 if( expected_status == PSA_SUCCESS )
4408 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4409 alg ) );
4410
Paul Elliottd79c5c52021-10-06 21:49:41 +01004411 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004412
Paul Elliott693bf312021-07-23 17:40:41 +01004413 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004414 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004415 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004416 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4417 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004418
4419 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4420 additional_data->len ) );
4421
4422 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004423 output, output_size,
4424 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004425
Paul Elliottf1277632021-08-24 18:11:37 +01004426 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4427 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004428 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4429 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004430
4431exit:
4432 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004433 mbedtls_free( output );
4434 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004435 psa_aead_abort( &operation );
4436 PSA_DONE( );
4437}
4438/* END_CASE */
4439
4440/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004441void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4442 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004443 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004444 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004445 data_t *additional_data,
4446 data_t *input_data,
4447 int expected_status_arg )
4448{
4449
4450 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4451 psa_key_type_t key_type = key_type_arg;
4452 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004453 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004454 uint8_t *nonce_buffer = NULL;
4455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4456 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4457 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004458 unsigned char *output = NULL;
4459 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004460 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004461 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004462 size_t ciphertext_size = 0;
4463 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004464 size_t tag_length = 0;
4465 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004466 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004467 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004468
4469 PSA_ASSERT( psa_crypto_init( ) );
4470
4471 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4472 psa_set_key_algorithm( &attributes, alg );
4473 psa_set_key_type( &attributes, key_type );
4474
4475 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4476 &key ) );
4477
4478 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4479
4480 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4481
Paul Elliott6f0e7202021-08-25 12:57:18 +01004482 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004483
Paul Elliott6f0e7202021-08-25 12:57:18 +01004484 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004485
Paul Elliott6f0e7202021-08-25 12:57:18 +01004486 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004487
Paul Elliott6f0e7202021-08-25 12:57:18 +01004488 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004489
Paul Elliott863864a2021-07-23 17:28:31 +01004490 status = psa_aead_encrypt_setup( &operation, key, alg );
4491
4492 /* If the operation is not supported, just skip and not fail in case the
4493 * encryption involves a common limitation of cryptography hardwares and
4494 * an alternative implementation. */
4495 if( status == PSA_ERROR_NOT_SUPPORTED )
4496 {
4497 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004498 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004499 }
4500
4501 PSA_ASSERT( status );
4502
Paul Elliott4023ffd2021-09-10 16:21:22 +01004503 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4504 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004505 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004506 /* Arbitrary size buffer, to test zero length valid buffer. */
4507 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004508 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004509 }
4510 else
4511 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004512 /* If length is zero, then this will return NULL. */
4513 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004514 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004515
Paul Elliott4023ffd2021-09-10 16:21:22 +01004516 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004517 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004518 for( index = 0; index < nonce_length - 1; ++index )
4519 {
4520 nonce_buffer[index] = 'a' + index;
4521 }
Paul Elliott66696b52021-08-16 18:42:41 +01004522 }
Paul Elliott863864a2021-07-23 17:28:31 +01004523 }
4524
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004525 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4526 {
4527 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4528 input_data->len ) );
4529 }
4530
Paul Elliott6f0e7202021-08-25 12:57:18 +01004531 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004532
Paul Elliott693bf312021-07-23 17:40:41 +01004533 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004534
4535 if( expected_status == PSA_SUCCESS )
4536 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004537 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4538 {
4539 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4540 input_data->len ) );
4541 }
4542 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4543 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004544
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004545 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4546 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4547 additional_data->len ),
4548 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004549
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004550 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004551 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004552 &ciphertext_length ),
4553 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004554
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004555 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004556 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004557 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4558 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004559 }
4560
4561exit:
4562 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004563 mbedtls_free( output );
4564 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004565 mbedtls_free( nonce_buffer );
4566 psa_aead_abort( &operation );
4567 PSA_DONE( );
4568}
4569/* END_CASE */
4570
4571/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004572void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4573 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004574 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004575 data_t *nonce,
4576 data_t *additional_data,
4577 data_t *input_data,
4578 int expected_status_arg )
4579{
4580
4581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4582 psa_key_type_t key_type = key_type_arg;
4583 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004584 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4586 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4587 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004588 unsigned char *output = NULL;
4589 unsigned char *ciphertext = NULL;
4590 size_t output_size = output_size_arg;
4591 size_t ciphertext_size = 0;
4592 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004593 size_t tag_length = 0;
4594 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4595
4596 PSA_ASSERT( psa_crypto_init( ) );
4597
4598 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4599 psa_set_key_algorithm( &attributes, alg );
4600 psa_set_key_type( &attributes, key_type );
4601
4602 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4603 &key ) );
4604
4605 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4606
Paul Elliottc6d11d02021-09-01 12:04:23 +01004607 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004608
Paul Elliottc6d11d02021-09-01 12:04:23 +01004609 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004610
Paul Elliottc6d11d02021-09-01 12:04:23 +01004611 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004612
Paul Elliott43fbda62021-07-23 18:30:59 +01004613 status = psa_aead_encrypt_setup( &operation, key, alg );
4614
4615 /* If the operation is not supported, just skip and not fail in case the
4616 * encryption involves a common limitation of cryptography hardwares and
4617 * an alternative implementation. */
4618 if( status == PSA_ERROR_NOT_SUPPORTED )
4619 {
4620 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4621 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4622 }
4623
4624 PSA_ASSERT( status );
4625
Paul Elliott47b9a142021-10-07 15:04:57 +01004626 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4627 input_data->len ) );
4628
Paul Elliott43fbda62021-07-23 18:30:59 +01004629 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4630
4631 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4632 additional_data->len ) );
4633
4634 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004635 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004636
4637 TEST_EQUAL( status, expected_status );
4638
4639 if( expected_status == PSA_SUCCESS )
4640 {
4641 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004642 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4643 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004644 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4645 }
4646
4647exit:
4648 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004649 mbedtls_free( output );
4650 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004651 psa_aead_abort( &operation );
4652 PSA_DONE( );
4653}
4654/* END_CASE */
4655
Paul Elliott91b021e2021-07-23 18:52:31 +01004656/* BEGIN_CASE */
4657void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4658 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004659 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004660 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004661 data_t *nonce,
4662 data_t *additional_data,
4663 data_t *input_data,
4664 int expected_status_arg )
4665{
4666
4667 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4668 psa_key_type_t key_type = key_type_arg;
4669 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004670 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4672 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4673 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004674 unsigned char *ciphertext = NULL;
4675 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004676 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004677 size_t ciphertext_size = 0;
4678 size_t ciphertext_length = 0;
4679 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004680 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004681 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004682
4683 PSA_ASSERT( psa_crypto_init( ) );
4684
4685 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4686 psa_set_key_algorithm( &attributes, alg );
4687 psa_set_key_type( &attributes, key_type );
4688
4689 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4690 &key ) );
4691
4692 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4693
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004694 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004695
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004696 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004697
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004698 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004699
Paul Elliott719c1322021-09-13 18:27:22 +01004700 ASSERT_ALLOC( tag_buffer, tag_size );
4701
Paul Elliott91b021e2021-07-23 18:52:31 +01004702 status = psa_aead_encrypt_setup( &operation, key, alg );
4703
4704 /* If the operation is not supported, just skip and not fail in case the
4705 * encryption involves a common limitation of cryptography hardwares and
4706 * an alternative implementation. */
4707 if( status == PSA_ERROR_NOT_SUPPORTED )
4708 {
4709 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4710 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4711 }
4712
4713 PSA_ASSERT( status );
4714
4715 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4716
Paul Elliott76bda482021-10-07 17:07:23 +01004717 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4718 input_data->len ) );
4719
Paul Elliott91b021e2021-07-23 18:52:31 +01004720 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4721 additional_data->len ) );
4722
4723 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004724 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004725
4726 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004727 status = psa_aead_finish( &operation, finish_ciphertext,
4728 finish_ciphertext_size,
4729 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004730 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004731
4732 TEST_EQUAL( status, expected_status );
4733
4734exit:
4735 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004736 mbedtls_free( ciphertext );
4737 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004738 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004739 psa_aead_abort( &operation );
4740 PSA_DONE( );
4741}
4742/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004743
4744/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004745void aead_multipart_verify( int key_type_arg, data_t *key_data,
4746 int alg_arg,
4747 data_t *nonce,
4748 data_t *additional_data,
4749 data_t *input_data,
4750 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004751 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004752 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004753 int expected_status_arg )
4754{
4755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4756 psa_key_type_t key_type = key_type_arg;
4757 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004758 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4760 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4761 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004762 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004763 unsigned char *plaintext = NULL;
4764 unsigned char *finish_plaintext = NULL;
4765 size_t plaintext_size = 0;
4766 size_t plaintext_length = 0;
4767 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004768 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004769 unsigned char *tag_buffer = NULL;
4770 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004771
4772 PSA_ASSERT( psa_crypto_init( ) );
4773
4774 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4775 psa_set_key_algorithm( &attributes, alg );
4776 psa_set_key_type( &attributes, key_type );
4777
4778 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4779 &key ) );
4780
4781 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4782
4783 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4784 input_data->len );
4785
4786 ASSERT_ALLOC( plaintext, plaintext_size );
4787
4788 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4789
4790 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4791
Paul Elliott9961a662021-09-17 19:19:02 +01004792 status = psa_aead_decrypt_setup( &operation, key, alg );
4793
4794 /* If the operation is not supported, just skip and not fail in case the
4795 * encryption involves a common limitation of cryptography hardwares and
4796 * an alternative implementation. */
4797 if( status == PSA_ERROR_NOT_SUPPORTED )
4798 {
4799 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4800 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4801 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004802 TEST_EQUAL( status, expected_setup_status );
4803
4804 if( status != PSA_SUCCESS )
4805 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004806
4807 PSA_ASSERT( status );
4808
4809 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4810
Paul Elliottfec6f372021-10-06 17:15:02 +01004811 status = psa_aead_set_lengths( &operation, additional_data->len,
4812 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004813 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004814
Paul Elliott9961a662021-09-17 19:19:02 +01004815 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4816 additional_data->len ) );
4817
4818 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4819 input_data->len,
4820 plaintext, plaintext_size,
4821 &plaintext_length ) );
4822
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004823 if( tag_usage == USE_GIVEN_TAG )
4824 {
4825 tag_buffer = tag->x;
4826 tag_size = tag->len;
4827 }
4828
Paul Elliott9961a662021-09-17 19:19:02 +01004829 status = psa_aead_verify( &operation, finish_plaintext,
4830 verify_plaintext_size,
4831 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004832 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004833
4834 TEST_EQUAL( status, expected_status );
4835
4836exit:
4837 psa_destroy_key( key );
4838 mbedtls_free( plaintext );
4839 mbedtls_free( finish_plaintext );
4840 psa_aead_abort( &operation );
4841 PSA_DONE( );
4842}
4843/* END_CASE */
4844
Paul Elliott9961a662021-09-17 19:19:02 +01004845/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004846void aead_multipart_setup( int key_type_arg, data_t *key_data,
4847 int alg_arg, int expected_status_arg )
4848{
4849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4850 psa_key_type_t key_type = key_type_arg;
4851 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004852 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4854 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4855 psa_status_t expected_status = expected_status_arg;
4856
4857 PSA_ASSERT( psa_crypto_init( ) );
4858
4859 psa_set_key_usage_flags( &attributes,
4860 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4861 psa_set_key_algorithm( &attributes, alg );
4862 psa_set_key_type( &attributes, key_type );
4863
4864 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4865 &key ) );
4866
Paul Elliott5221ef62021-09-19 17:33:03 +01004867 status = psa_aead_encrypt_setup( &operation, key, alg );
4868
4869 TEST_EQUAL( status, expected_status );
4870
4871 psa_aead_abort( &operation );
4872
Paul Elliott5221ef62021-09-19 17:33:03 +01004873 status = psa_aead_decrypt_setup( &operation, key, alg );
4874
4875 TEST_EQUAL(status, expected_status );
4876
4877exit:
4878 psa_destroy_key( key );
4879 psa_aead_abort( &operation );
4880 PSA_DONE( );
4881}
4882/* END_CASE */
4883
4884/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004885void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4886 int alg_arg,
4887 data_t *nonce,
4888 data_t *additional_data,
4889 data_t *input_data )
4890{
4891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4892 psa_key_type_t key_type = key_type_arg;
4893 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004894 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004895 unsigned char *output_data = NULL;
4896 unsigned char *final_data = NULL;
4897 size_t output_size = 0;
4898 size_t finish_output_size = 0;
4899 size_t output_length = 0;
4900 size_t key_bits = 0;
4901 size_t tag_length = 0;
4902 size_t tag_size = 0;
4903 size_t nonce_length = 0;
4904 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4905 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4906 size_t output_part_length = 0;
4907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4908
4909 PSA_ASSERT( psa_crypto_init( ) );
4910
4911 psa_set_key_usage_flags( & attributes,
4912 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4913 psa_set_key_algorithm( & attributes, alg );
4914 psa_set_key_type( & attributes, key_type );
4915
4916 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4917 &key ) );
4918
4919 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4920 key_bits = psa_get_key_bits( &attributes );
4921
4922 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4923
4924 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4925
4926 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4927
4928 ASSERT_ALLOC( output_data, output_size );
4929
4930 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4931
4932 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4933
4934 ASSERT_ALLOC( final_data, finish_output_size );
4935
4936 /* Test all operations error without calling setup first. */
4937
Paul Elliottc23a9a02021-06-21 18:32:46 +01004938 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4939 PSA_ERROR_BAD_STATE );
4940
4941 psa_aead_abort( &operation );
4942
Paul Elliottc23a9a02021-06-21 18:32:46 +01004943 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4944 PSA_AEAD_NONCE_MAX_SIZE,
4945 &nonce_length ),
4946 PSA_ERROR_BAD_STATE );
4947
4948 psa_aead_abort( &operation );
4949
Paul Elliott481be342021-07-16 17:38:47 +01004950 /* ------------------------------------------------------- */
4951
Paul Elliottc23a9a02021-06-21 18:32:46 +01004952 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4953 input_data->len ),
4954 PSA_ERROR_BAD_STATE );
4955
4956 psa_aead_abort( &operation );
4957
Paul Elliott481be342021-07-16 17:38:47 +01004958 /* ------------------------------------------------------- */
4959
Paul Elliottc23a9a02021-06-21 18:32:46 +01004960 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4961 additional_data->len ),
4962 PSA_ERROR_BAD_STATE );
4963
4964 psa_aead_abort( &operation );
4965
Paul Elliott481be342021-07-16 17:38:47 +01004966 /* ------------------------------------------------------- */
4967
Paul Elliottc23a9a02021-06-21 18:32:46 +01004968 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4969 input_data->len, output_data,
4970 output_size, &output_length ),
4971 PSA_ERROR_BAD_STATE );
4972
4973 psa_aead_abort( &operation );
4974
Paul Elliott481be342021-07-16 17:38:47 +01004975 /* ------------------------------------------------------- */
4976
Paul Elliottc23a9a02021-06-21 18:32:46 +01004977 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4978 finish_output_size,
4979 &output_part_length,
4980 tag_buffer, tag_length,
4981 &tag_size ),
4982 PSA_ERROR_BAD_STATE );
4983
4984 psa_aead_abort( &operation );
4985
Paul Elliott481be342021-07-16 17:38:47 +01004986 /* ------------------------------------------------------- */
4987
Paul Elliottc23a9a02021-06-21 18:32:46 +01004988 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4989 finish_output_size,
4990 &output_part_length,
4991 tag_buffer,
4992 tag_length ),
4993 PSA_ERROR_BAD_STATE );
4994
4995 psa_aead_abort( &operation );
4996
4997 /* Test for double setups. */
4998
Paul Elliottc23a9a02021-06-21 18:32:46 +01004999 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5000
5001 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5002 PSA_ERROR_BAD_STATE );
5003
5004 psa_aead_abort( &operation );
5005
Paul Elliott481be342021-07-16 17:38:47 +01005006 /* ------------------------------------------------------- */
5007
Paul Elliottc23a9a02021-06-21 18:32:46 +01005008 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5009
5010 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5011 PSA_ERROR_BAD_STATE );
5012
5013 psa_aead_abort( &operation );
5014
Paul Elliott374a2be2021-07-16 17:53:40 +01005015 /* ------------------------------------------------------- */
5016
Paul Elliott374a2be2021-07-16 17:53:40 +01005017 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5018
5019 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5020 PSA_ERROR_BAD_STATE );
5021
5022 psa_aead_abort( &operation );
5023
5024 /* ------------------------------------------------------- */
5025
Paul Elliott374a2be2021-07-16 17:53:40 +01005026 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5027
5028 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5029 PSA_ERROR_BAD_STATE );
5030
5031 psa_aead_abort( &operation );
5032
Paul Elliottc23a9a02021-06-21 18:32:46 +01005033 /* Test for not setting a nonce. */
5034
Paul Elliottc23a9a02021-06-21 18:32:46 +01005035 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5036
5037 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5038 additional_data->len ),
5039 PSA_ERROR_BAD_STATE );
5040
5041 psa_aead_abort( &operation );
5042
Paul Elliott7f628422021-09-01 12:08:29 +01005043 /* ------------------------------------------------------- */
5044
Paul Elliott7f628422021-09-01 12:08:29 +01005045 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5046
5047 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5048 input_data->len, output_data,
5049 output_size, &output_length ),
5050 PSA_ERROR_BAD_STATE );
5051
5052 psa_aead_abort( &operation );
5053
Paul Elliottbdc2c682021-09-21 18:37:10 +01005054 /* ------------------------------------------------------- */
5055
Paul Elliottbdc2c682021-09-21 18:37:10 +01005056 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5057
5058 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5059 finish_output_size,
5060 &output_part_length,
5061 tag_buffer, tag_length,
5062 &tag_size ),
5063 PSA_ERROR_BAD_STATE );
5064
5065 psa_aead_abort( &operation );
5066
5067 /* ------------------------------------------------------- */
5068
Paul Elliottbdc2c682021-09-21 18:37:10 +01005069 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5070
5071 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5072 finish_output_size,
5073 &output_part_length,
5074 tag_buffer,
5075 tag_length ),
5076 PSA_ERROR_BAD_STATE );
5077
5078 psa_aead_abort( &operation );
5079
Paul Elliottc23a9a02021-06-21 18:32:46 +01005080 /* Test for double setting nonce. */
5081
Paul Elliottc23a9a02021-06-21 18:32:46 +01005082 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5083
5084 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5085
5086 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5087 PSA_ERROR_BAD_STATE );
5088
5089 psa_aead_abort( &operation );
5090
Paul Elliott374a2be2021-07-16 17:53:40 +01005091 /* Test for double generating nonce. */
5092
Paul Elliott374a2be2021-07-16 17:53:40 +01005093 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5094
5095 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5096 PSA_AEAD_NONCE_MAX_SIZE,
5097 &nonce_length ) );
5098
5099 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5100 PSA_AEAD_NONCE_MAX_SIZE,
5101 &nonce_length ),
5102 PSA_ERROR_BAD_STATE );
5103
5104
5105 psa_aead_abort( &operation );
5106
5107 /* Test for generate nonce then set and vice versa */
5108
Paul Elliott374a2be2021-07-16 17:53:40 +01005109 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5110
5111 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5112 PSA_AEAD_NONCE_MAX_SIZE,
5113 &nonce_length ) );
5114
5115 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5116 PSA_ERROR_BAD_STATE );
5117
5118 psa_aead_abort( &operation );
5119
Andrzej Kurekad837522021-12-15 15:28:49 +01005120 /* Test for generating nonce after calling set lengths */
5121
5122 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5123
5124 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5125 input_data->len ) );
5126
5127 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5128 PSA_AEAD_NONCE_MAX_SIZE,
5129 &nonce_length ) );
5130
5131 psa_aead_abort( &operation );
5132
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005133 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005134
5135 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5136
5137 if( operation.alg == PSA_ALG_CCM )
5138 {
5139 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5140 input_data->len ),
5141 PSA_ERROR_INVALID_ARGUMENT );
5142 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5143 PSA_AEAD_NONCE_MAX_SIZE,
5144 &nonce_length ),
5145 PSA_ERROR_BAD_STATE );
5146 }
5147 else
5148 {
5149 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5150 input_data->len ) );
5151 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5152 PSA_AEAD_NONCE_MAX_SIZE,
5153 &nonce_length ) );
5154 }
5155
5156 psa_aead_abort( &operation );
5157
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005158 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005159#if SIZE_MAX > UINT32_MAX
5160 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5161
5162 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5163 {
5164 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5165 input_data->len ),
5166 PSA_ERROR_INVALID_ARGUMENT );
5167 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5168 PSA_AEAD_NONCE_MAX_SIZE,
5169 &nonce_length ),
5170 PSA_ERROR_BAD_STATE );
5171 }
5172 else
5173 {
5174 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5175 input_data->len ) );
5176 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5177 PSA_AEAD_NONCE_MAX_SIZE,
5178 &nonce_length ) );
5179 }
5180
5181 psa_aead_abort( &operation );
5182#endif
5183
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005184 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005185
5186 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5187
5188 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5189 PSA_AEAD_NONCE_MAX_SIZE,
5190 &nonce_length ) );
5191
5192 if( operation.alg == PSA_ALG_CCM )
5193 {
5194 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5195 input_data->len ),
5196 PSA_ERROR_INVALID_ARGUMENT );
5197 }
5198 else
5199 {
5200 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5201 input_data->len ) );
5202 }
5203
5204 psa_aead_abort( &operation );
5205
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005206 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005207 /* Test for setting nonce after calling set lengths */
5208
5209 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5210
5211 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5212 input_data->len ) );
5213
5214 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5215
5216 psa_aead_abort( &operation );
5217
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005218 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005219
5220 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5221
5222 if( operation.alg == PSA_ALG_CCM )
5223 {
5224 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5225 input_data->len ),
5226 PSA_ERROR_INVALID_ARGUMENT );
5227 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5228 PSA_ERROR_BAD_STATE );
5229 }
5230 else
5231 {
5232 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5233 input_data->len ) );
5234 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5235 }
5236
5237 psa_aead_abort( &operation );
5238
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005239 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005240#if SIZE_MAX > UINT32_MAX
5241 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5242
5243 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5244 {
5245 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5246 input_data->len ),
5247 PSA_ERROR_INVALID_ARGUMENT );
5248 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5249 PSA_ERROR_BAD_STATE );
5250 }
5251 else
5252 {
5253 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5254 input_data->len ) );
5255 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5256 }
5257
5258 psa_aead_abort( &operation );
5259#endif
5260
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005261 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005262
5263 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5264
5265 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5266
5267 if( operation.alg == PSA_ALG_CCM )
5268 {
5269 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5270 input_data->len ),
5271 PSA_ERROR_INVALID_ARGUMENT );
5272 }
5273 else
5274 {
5275 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5276 input_data->len ) );
5277 }
5278
5279 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005280
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005281 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005282#if SIZE_MAX > UINT32_MAX
5283 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5284
5285 if( operation.alg == PSA_ALG_GCM )
5286 {
5287 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5288 SIZE_MAX ),
5289 PSA_ERROR_INVALID_ARGUMENT );
5290 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5291 PSA_ERROR_BAD_STATE );
5292 }
5293 else if ( operation.alg != PSA_ALG_CCM )
5294 {
5295 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5296 SIZE_MAX ) );
5297 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5298 }
5299
5300 psa_aead_abort( &operation );
5301
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005302 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005303 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5304
5305 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5306
5307 if( operation.alg == PSA_ALG_GCM )
5308 {
5309 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5310 SIZE_MAX ),
5311 PSA_ERROR_INVALID_ARGUMENT );
5312 }
5313 else if ( operation.alg != PSA_ALG_CCM )
5314 {
5315 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5316 SIZE_MAX ) );
5317 }
5318
5319 psa_aead_abort( &operation );
5320#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005321
5322 /* ------------------------------------------------------- */
5323
Paul Elliott374a2be2021-07-16 17:53:40 +01005324 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5325
5326 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5327
5328 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5329 PSA_AEAD_NONCE_MAX_SIZE,
5330 &nonce_length ),
5331 PSA_ERROR_BAD_STATE );
5332
5333 psa_aead_abort( &operation );
5334
Paul Elliott7220cae2021-06-22 17:25:57 +01005335 /* Test for generating nonce in decrypt setup. */
5336
Paul Elliott7220cae2021-06-22 17:25:57 +01005337 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5338
5339 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5340 PSA_AEAD_NONCE_MAX_SIZE,
5341 &nonce_length ),
5342 PSA_ERROR_BAD_STATE );
5343
5344 psa_aead_abort( &operation );
5345
Paul Elliottc23a9a02021-06-21 18:32:46 +01005346 /* Test for setting lengths twice. */
5347
Paul Elliottc23a9a02021-06-21 18:32:46 +01005348 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5349
5350 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5351
5352 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5353 input_data->len ) );
5354
5355 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5356 input_data->len ),
5357 PSA_ERROR_BAD_STATE );
5358
5359 psa_aead_abort( &operation );
5360
Andrzej Kurekad837522021-12-15 15:28:49 +01005361 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005362
Paul Elliottc23a9a02021-06-21 18:32:46 +01005363 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5364
5365 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5366
Andrzej Kurekad837522021-12-15 15:28:49 +01005367 if( operation.alg == PSA_ALG_CCM )
5368 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005369
Andrzej Kurekad837522021-12-15 15:28:49 +01005370 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5371 additional_data->len ),
5372 PSA_ERROR_BAD_STATE );
5373 }
5374 else
5375 {
5376 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5377 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005378
Andrzej Kurekad837522021-12-15 15:28:49 +01005379 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5380 input_data->len ),
5381 PSA_ERROR_BAD_STATE );
5382 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005383 psa_aead_abort( &operation );
5384
5385 /* ------------------------------------------------------- */
5386
Paul Elliottf94bd992021-09-19 18:15:59 +01005387 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5388
5389 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5390
Andrzej Kurekad837522021-12-15 15:28:49 +01005391 if( operation.alg == PSA_ALG_CCM )
5392 {
5393 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5394 input_data->len, output_data,
5395 output_size, &output_length ),
5396 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005397
Andrzej Kurekad837522021-12-15 15:28:49 +01005398 }
5399 else
5400 {
5401 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5402 input_data->len, output_data,
5403 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005404
Andrzej Kurekad837522021-12-15 15:28:49 +01005405 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5406 input_data->len ),
5407 PSA_ERROR_BAD_STATE );
5408 }
5409 psa_aead_abort( &operation );
5410
5411 /* ------------------------------------------------------- */
5412
5413 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5414
5415 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5416
5417 if( operation.alg == PSA_ALG_CCM )
5418 {
5419 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5420 finish_output_size,
5421 &output_part_length,
5422 tag_buffer, tag_length,
5423 &tag_size ) );
5424 }
5425 else
5426 {
5427 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5428 finish_output_size,
5429 &output_part_length,
5430 tag_buffer, tag_length,
5431 &tag_size ) );
5432
5433 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5434 input_data->len ),
5435 PSA_ERROR_BAD_STATE );
5436 }
5437 psa_aead_abort( &operation );
5438
5439 /* Test for setting lengths after generating nonce + already starting data. */
5440
5441 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5442
5443 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5444 PSA_AEAD_NONCE_MAX_SIZE,
5445 &nonce_length ) );
5446 if( operation.alg == PSA_ALG_CCM )
5447 {
5448
5449 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5450 additional_data->len ),
5451 PSA_ERROR_BAD_STATE );
5452 }
5453 else
5454 {
5455 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5456 additional_data->len ) );
5457
5458 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5459 input_data->len ),
5460 PSA_ERROR_BAD_STATE );
5461 }
5462 psa_aead_abort( &operation );
5463
5464 /* ------------------------------------------------------- */
5465
5466 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5467
5468 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5469 PSA_AEAD_NONCE_MAX_SIZE,
5470 &nonce_length ) );
5471 if( operation.alg == PSA_ALG_CCM )
5472 {
5473 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5474 input_data->len, output_data,
5475 output_size, &output_length ),
5476 PSA_ERROR_BAD_STATE );
5477
5478 }
5479 else
5480 {
5481 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5482 input_data->len, output_data,
5483 output_size, &output_length ) );
5484
5485 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5486 input_data->len ),
5487 PSA_ERROR_BAD_STATE );
5488 }
5489 psa_aead_abort( &operation );
5490
5491 /* ------------------------------------------------------- */
5492
5493 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5494
5495 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5496 PSA_AEAD_NONCE_MAX_SIZE,
5497 &nonce_length ) );
5498 if( operation.alg == PSA_ALG_CCM )
5499 {
5500 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5501 finish_output_size,
5502 &output_part_length,
5503 tag_buffer, tag_length,
5504 &tag_size ) );
5505 }
5506 else
5507 {
5508 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5509 finish_output_size,
5510 &output_part_length,
5511 tag_buffer, tag_length,
5512 &tag_size ) );
5513
5514 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5515 input_data->len ),
5516 PSA_ERROR_BAD_STATE );
5517 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005518 psa_aead_abort( &operation );
5519
Paul Elliott243080c2021-07-21 19:01:17 +01005520 /* Test for not sending any additional data or data after setting non zero
5521 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005522
Paul Elliottc23a9a02021-06-21 18:32:46 +01005523 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5524
5525 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5526
5527 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5528 input_data->len ) );
5529
5530 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5531 finish_output_size,
5532 &output_part_length,
5533 tag_buffer, tag_length,
5534 &tag_size ),
5535 PSA_ERROR_INVALID_ARGUMENT );
5536
5537 psa_aead_abort( &operation );
5538
Paul Elliott243080c2021-07-21 19:01:17 +01005539 /* Test for not sending any additional data or data after setting non-zero
5540 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005541
Paul Elliottc23a9a02021-06-21 18:32:46 +01005542 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5543
5544 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5545
5546 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5547 input_data->len ) );
5548
5549 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5550 finish_output_size,
5551 &output_part_length,
5552 tag_buffer,
5553 tag_length ),
5554 PSA_ERROR_INVALID_ARGUMENT );
5555
5556 psa_aead_abort( &operation );
5557
Paul Elliott243080c2021-07-21 19:01:17 +01005558 /* Test for not sending any additional data after setting a non-zero length
5559 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005560
Paul Elliottc23a9a02021-06-21 18:32:46 +01005561 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5562
5563 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5564
5565 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5566 input_data->len ) );
5567
5568 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5569 input_data->len, output_data,
5570 output_size, &output_length ),
5571 PSA_ERROR_INVALID_ARGUMENT );
5572
5573 psa_aead_abort( &operation );
5574
Paul Elliottf94bd992021-09-19 18:15:59 +01005575 /* Test for not sending any data after setting a non-zero length for it.*/
5576
Paul Elliottf94bd992021-09-19 18:15:59 +01005577 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5578
5579 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5580
5581 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5582 input_data->len ) );
5583
5584 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5585 additional_data->len ) );
5586
5587 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5588 finish_output_size,
5589 &output_part_length,
5590 tag_buffer, tag_length,
5591 &tag_size ),
5592 PSA_ERROR_INVALID_ARGUMENT );
5593
5594 psa_aead_abort( &operation );
5595
Paul Elliottb0450fe2021-09-01 15:06:26 +01005596 /* Test for sending too much additional data after setting lengths. */
5597
Paul Elliottb0450fe2021-09-01 15:06:26 +01005598 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5599
5600 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5601
5602 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5603
5604
5605 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5606 additional_data->len ),
5607 PSA_ERROR_INVALID_ARGUMENT );
5608
5609 psa_aead_abort( &operation );
5610
Paul Elliotta2a09b02021-09-22 14:56:40 +01005611 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005612
5613 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5614
5615 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5616
5617 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5618 input_data->len ) );
5619
5620 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5621 additional_data->len ) );
5622
5623 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5624 1 ),
5625 PSA_ERROR_INVALID_ARGUMENT );
5626
5627 psa_aead_abort( &operation );
5628
Paul Elliottb0450fe2021-09-01 15:06:26 +01005629 /* Test for sending too much data after setting lengths. */
5630
Paul Elliottb0450fe2021-09-01 15:06:26 +01005631 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5632
5633 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5634
5635 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5636
5637 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5638 input_data->len, output_data,
5639 output_size, &output_length ),
5640 PSA_ERROR_INVALID_ARGUMENT );
5641
5642 psa_aead_abort( &operation );
5643
Paul Elliotta2a09b02021-09-22 14:56:40 +01005644 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005645
5646 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5647
5648 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5649
5650 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5651 input_data->len ) );
5652
5653 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5654 additional_data->len ) );
5655
5656 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5657 input_data->len, output_data,
5658 output_size, &output_length ) );
5659
5660 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5661 1, output_data,
5662 output_size, &output_length ),
5663 PSA_ERROR_INVALID_ARGUMENT );
5664
5665 psa_aead_abort( &operation );
5666
Paul Elliottc23a9a02021-06-21 18:32:46 +01005667 /* Test sending additional data after data. */
5668
Paul Elliottc23a9a02021-06-21 18:32:46 +01005669 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5670
5671 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5672
Andrzej Kurekad837522021-12-15 15:28:49 +01005673 if( operation.alg != PSA_ALG_CCM )
5674 {
5675 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5676 input_data->len, output_data,
5677 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005678
Andrzej Kurekad837522021-12-15 15:28:49 +01005679 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5680 additional_data->len ),
5681 PSA_ERROR_BAD_STATE );
5682 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005683 psa_aead_abort( &operation );
5684
Paul Elliott534d0b42021-06-22 19:15:20 +01005685 /* Test calling finish on decryption. */
5686
Paul Elliott534d0b42021-06-22 19:15:20 +01005687 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5688
5689 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5690
5691 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5692 finish_output_size,
5693 &output_part_length,
5694 tag_buffer, tag_length,
5695 &tag_size ),
5696 PSA_ERROR_BAD_STATE );
5697
5698 psa_aead_abort( &operation );
5699
5700 /* Test calling verify on encryption. */
5701
Paul Elliott534d0b42021-06-22 19:15:20 +01005702 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5703
5704 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5705
5706 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5707 finish_output_size,
5708 &output_part_length,
5709 tag_buffer,
5710 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005711 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005712
5713 psa_aead_abort( &operation );
5714
5715
Paul Elliottc23a9a02021-06-21 18:32:46 +01005716exit:
5717 psa_destroy_key( key );
5718 psa_aead_abort( &operation );
5719 mbedtls_free( output_data );
5720 mbedtls_free( final_data );
5721 PSA_DONE( );
5722}
5723/* END_CASE */
5724
5725/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005726void signature_size( int type_arg,
5727 int bits,
5728 int alg_arg,
5729 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005730{
5731 psa_key_type_t type = type_arg;
5732 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005733 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005734
Gilles Peskinefe11b722018-12-18 00:24:04 +01005735 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005736
Gilles Peskinee59236f2018-01-27 23:32:46 +01005737exit:
5738 ;
5739}
5740/* END_CASE */
5741
5742/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005743void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5744 int alg_arg, data_t *input_data,
5745 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005746{
Ronald Cron5425a212020-08-04 14:58:35 +02005747 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005748 psa_key_type_t key_type = key_type_arg;
5749 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005750 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005751 unsigned char *signature = NULL;
5752 size_t signature_size;
5753 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005754 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005755
Gilles Peskine8817f612018-12-18 00:18:46 +01005756 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005757
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005758 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005759 psa_set_key_algorithm( &attributes, alg );
5760 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005761
Gilles Peskine049c7532019-05-15 20:22:09 +02005762 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005763 &key ) );
5764 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005765 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005766
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005767 /* Allocate a buffer which has the size advertized by the
5768 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005769 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005770 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005771 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005772 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005773 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005774
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005775 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005776 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005777 input_data->x, input_data->len,
5778 signature, signature_size,
5779 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005780 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005781 ASSERT_COMPARE( output_data->x, output_data->len,
5782 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005783
5784exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005785 /*
5786 * Key attributes may have been returned by psa_get_key_attributes()
5787 * thus reset them as required.
5788 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005789 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005790
Ronald Cron5425a212020-08-04 14:58:35 +02005791 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005792 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005793 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005794}
5795/* END_CASE */
5796
5797/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005798void sign_hash_fail( int key_type_arg, data_t *key_data,
5799 int alg_arg, data_t *input_data,
5800 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005801{
Ronald Cron5425a212020-08-04 14:58:35 +02005802 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005803 psa_key_type_t key_type = key_type_arg;
5804 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005805 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005806 psa_status_t actual_status;
5807 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005808 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005809 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005810 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005811
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005812 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005813
Gilles Peskine8817f612018-12-18 00:18:46 +01005814 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005815
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005816 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005817 psa_set_key_algorithm( &attributes, alg );
5818 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005819
Gilles Peskine049c7532019-05-15 20:22:09 +02005820 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005821 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005822
Ronald Cron5425a212020-08-04 14:58:35 +02005823 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005824 input_data->x, input_data->len,
5825 signature, signature_size,
5826 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005827 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005828 /* The value of *signature_length is unspecified on error, but
5829 * whatever it is, it should be less than signature_size, so that
5830 * if the caller tries to read *signature_length bytes without
5831 * checking the error code then they don't overflow a buffer. */
5832 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005833
5834exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005835 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005836 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005837 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005838 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005839}
5840/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005841
5842/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005843void sign_verify_hash( int key_type_arg, data_t *key_data,
5844 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005845{
Ronald Cron5425a212020-08-04 14:58:35 +02005846 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005847 psa_key_type_t key_type = key_type_arg;
5848 psa_algorithm_t alg = alg_arg;
5849 size_t key_bits;
5850 unsigned char *signature = NULL;
5851 size_t signature_size;
5852 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005854
Gilles Peskine8817f612018-12-18 00:18:46 +01005855 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005856
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005857 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005858 psa_set_key_algorithm( &attributes, alg );
5859 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005860
Gilles Peskine049c7532019-05-15 20:22:09 +02005861 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005862 &key ) );
5863 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005864 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005865
5866 /* Allocate a buffer which has the size advertized by the
5867 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005868 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005869 key_bits, alg );
5870 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005871 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005872 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005873
5874 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005875 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005876 input_data->x, input_data->len,
5877 signature, signature_size,
5878 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005879 /* Check that the signature length looks sensible. */
5880 TEST_ASSERT( signature_length <= signature_size );
5881 TEST_ASSERT( signature_length > 0 );
5882
5883 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005884 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005885 input_data->x, input_data->len,
5886 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005887
5888 if( input_data->len != 0 )
5889 {
5890 /* Flip a bit in the input and verify that the signature is now
5891 * detected as invalid. Flip a bit at the beginning, not at the end,
5892 * because ECDSA may ignore the last few bits of the input. */
5893 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005894 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005895 input_data->x, input_data->len,
5896 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005897 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005898 }
5899
5900exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005901 /*
5902 * Key attributes may have been returned by psa_get_key_attributes()
5903 * thus reset them as required.
5904 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005905 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005906
Ronald Cron5425a212020-08-04 14:58:35 +02005907 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005908 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005909 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005910}
5911/* END_CASE */
5912
5913/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005914void verify_hash( int key_type_arg, data_t *key_data,
5915 int alg_arg, data_t *hash_data,
5916 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005917{
Ronald Cron5425a212020-08-04 14:58:35 +02005918 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005919 psa_key_type_t key_type = key_type_arg;
5920 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005922
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005923 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005924
Gilles Peskine8817f612018-12-18 00:18:46 +01005925 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005926
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005927 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005928 psa_set_key_algorithm( &attributes, alg );
5929 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005930
Gilles Peskine049c7532019-05-15 20:22:09 +02005931 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005932 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005933
Ronald Cron5425a212020-08-04 14:58:35 +02005934 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005935 hash_data->x, hash_data->len,
5936 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005937
itayzafrir5c753392018-05-08 11:18:38 +03005938exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005939 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005940 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005941 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005942}
5943/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005944
5945/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005946void verify_hash_fail( int key_type_arg, data_t *key_data,
5947 int alg_arg, data_t *hash_data,
5948 data_t *signature_data,
5949 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005950{
Ronald Cron5425a212020-08-04 14:58:35 +02005951 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005952 psa_key_type_t key_type = key_type_arg;
5953 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005954 psa_status_t actual_status;
5955 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005956 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005957
Gilles Peskine8817f612018-12-18 00:18:46 +01005958 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005959
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005960 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005961 psa_set_key_algorithm( &attributes, alg );
5962 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005963
Gilles Peskine049c7532019-05-15 20:22:09 +02005964 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005965 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005966
Ronald Cron5425a212020-08-04 14:58:35 +02005967 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005968 hash_data->x, hash_data->len,
5969 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005970 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005971
5972exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005973 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005974 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005975 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005976}
5977/* END_CASE */
5978
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005979/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005980void sign_message_deterministic( int key_type_arg,
5981 data_t *key_data,
5982 int alg_arg,
5983 data_t *input_data,
5984 data_t *output_data )
5985{
5986 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5987 psa_key_type_t key_type = key_type_arg;
5988 psa_algorithm_t alg = alg_arg;
5989 size_t key_bits;
5990 unsigned char *signature = NULL;
5991 size_t signature_size;
5992 size_t signature_length = 0xdeadbeef;
5993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5994
5995 PSA_ASSERT( psa_crypto_init( ) );
5996
5997 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5998 psa_set_key_algorithm( &attributes, alg );
5999 psa_set_key_type( &attributes, key_type );
6000
6001 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6002 &key ) );
6003 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6004 key_bits = psa_get_key_bits( &attributes );
6005
6006 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6007 TEST_ASSERT( signature_size != 0 );
6008 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
6009 ASSERT_ALLOC( signature, signature_size );
6010
6011 PSA_ASSERT( psa_sign_message( key, alg,
6012 input_data->x, input_data->len,
6013 signature, signature_size,
6014 &signature_length ) );
6015
6016 ASSERT_COMPARE( output_data->x, output_data->len,
6017 signature, signature_length );
6018
6019exit:
6020 psa_reset_key_attributes( &attributes );
6021
6022 psa_destroy_key( key );
6023 mbedtls_free( signature );
6024 PSA_DONE( );
6025
6026}
6027/* END_CASE */
6028
6029/* BEGIN_CASE */
6030void sign_message_fail( int key_type_arg,
6031 data_t *key_data,
6032 int alg_arg,
6033 data_t *input_data,
6034 int signature_size_arg,
6035 int expected_status_arg )
6036{
6037 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6038 psa_key_type_t key_type = key_type_arg;
6039 psa_algorithm_t alg = alg_arg;
6040 size_t signature_size = signature_size_arg;
6041 psa_status_t actual_status;
6042 psa_status_t expected_status = expected_status_arg;
6043 unsigned char *signature = NULL;
6044 size_t signature_length = 0xdeadbeef;
6045 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6046
6047 ASSERT_ALLOC( signature, signature_size );
6048
6049 PSA_ASSERT( psa_crypto_init( ) );
6050
6051 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6052 psa_set_key_algorithm( &attributes, alg );
6053 psa_set_key_type( &attributes, key_type );
6054
6055 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6056 &key ) );
6057
6058 actual_status = psa_sign_message( key, alg,
6059 input_data->x, input_data->len,
6060 signature, signature_size,
6061 &signature_length );
6062 TEST_EQUAL( actual_status, expected_status );
6063 /* The value of *signature_length is unspecified on error, but
6064 * whatever it is, it should be less than signature_size, so that
6065 * if the caller tries to read *signature_length bytes without
6066 * checking the error code then they don't overflow a buffer. */
6067 TEST_ASSERT( signature_length <= signature_size );
6068
6069exit:
6070 psa_reset_key_attributes( &attributes );
6071 psa_destroy_key( key );
6072 mbedtls_free( signature );
6073 PSA_DONE( );
6074}
6075/* END_CASE */
6076
6077/* BEGIN_CASE */
6078void sign_verify_message( int key_type_arg,
6079 data_t *key_data,
6080 int alg_arg,
6081 data_t *input_data )
6082{
6083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6084 psa_key_type_t key_type = key_type_arg;
6085 psa_algorithm_t alg = alg_arg;
6086 size_t key_bits;
6087 unsigned char *signature = NULL;
6088 size_t signature_size;
6089 size_t signature_length = 0xdeadbeef;
6090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6091
6092 PSA_ASSERT( psa_crypto_init( ) );
6093
6094 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6095 PSA_KEY_USAGE_VERIFY_MESSAGE );
6096 psa_set_key_algorithm( &attributes, alg );
6097 psa_set_key_type( &attributes, key_type );
6098
6099 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6100 &key ) );
6101 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6102 key_bits = psa_get_key_bits( &attributes );
6103
6104 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6105 TEST_ASSERT( signature_size != 0 );
6106 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
6107 ASSERT_ALLOC( signature, signature_size );
6108
6109 PSA_ASSERT( psa_sign_message( key, alg,
6110 input_data->x, input_data->len,
6111 signature, signature_size,
6112 &signature_length ) );
6113 TEST_ASSERT( signature_length <= signature_size );
6114 TEST_ASSERT( signature_length > 0 );
6115
6116 PSA_ASSERT( psa_verify_message( key, alg,
6117 input_data->x, input_data->len,
6118 signature, signature_length ) );
6119
6120 if( input_data->len != 0 )
6121 {
6122 /* Flip a bit in the input and verify that the signature is now
6123 * detected as invalid. Flip a bit at the beginning, not at the end,
6124 * because ECDSA may ignore the last few bits of the input. */
6125 input_data->x[0] ^= 1;
6126 TEST_EQUAL( psa_verify_message( key, alg,
6127 input_data->x, input_data->len,
6128 signature, signature_length ),
6129 PSA_ERROR_INVALID_SIGNATURE );
6130 }
6131
6132exit:
6133 psa_reset_key_attributes( &attributes );
6134
6135 psa_destroy_key( key );
6136 mbedtls_free( signature );
6137 PSA_DONE( );
6138}
6139/* END_CASE */
6140
6141/* BEGIN_CASE */
6142void verify_message( int key_type_arg,
6143 data_t *key_data,
6144 int alg_arg,
6145 data_t *input_data,
6146 data_t *signature_data )
6147{
6148 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6149 psa_key_type_t key_type = key_type_arg;
6150 psa_algorithm_t alg = alg_arg;
6151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6152
6153 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
6154
6155 PSA_ASSERT( psa_crypto_init( ) );
6156
6157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6158 psa_set_key_algorithm( &attributes, alg );
6159 psa_set_key_type( &attributes, key_type );
6160
6161 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6162 &key ) );
6163
6164 PSA_ASSERT( psa_verify_message( key, alg,
6165 input_data->x, input_data->len,
6166 signature_data->x, signature_data->len ) );
6167
6168exit:
6169 psa_reset_key_attributes( &attributes );
6170 psa_destroy_key( key );
6171 PSA_DONE( );
6172}
6173/* END_CASE */
6174
6175/* BEGIN_CASE */
6176void verify_message_fail( int key_type_arg,
6177 data_t *key_data,
6178 int alg_arg,
6179 data_t *hash_data,
6180 data_t *signature_data,
6181 int expected_status_arg )
6182{
6183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6184 psa_key_type_t key_type = key_type_arg;
6185 psa_algorithm_t alg = alg_arg;
6186 psa_status_t actual_status;
6187 psa_status_t expected_status = expected_status_arg;
6188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6189
6190 PSA_ASSERT( psa_crypto_init( ) );
6191
6192 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6193 psa_set_key_algorithm( &attributes, alg );
6194 psa_set_key_type( &attributes, key_type );
6195
6196 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6197 &key ) );
6198
6199 actual_status = psa_verify_message( key, alg,
6200 hash_data->x, hash_data->len,
6201 signature_data->x,
6202 signature_data->len );
6203 TEST_EQUAL( actual_status, expected_status );
6204
6205exit:
6206 psa_reset_key_attributes( &attributes );
6207 psa_destroy_key( key );
6208 PSA_DONE( );
6209}
6210/* END_CASE */
6211
6212/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006213void asymmetric_encrypt( int key_type_arg,
6214 data_t *key_data,
6215 int alg_arg,
6216 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006217 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006218 int expected_output_length_arg,
6219 int expected_status_arg )
6220{
Ronald Cron5425a212020-08-04 14:58:35 +02006221 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006222 psa_key_type_t key_type = key_type_arg;
6223 psa_algorithm_t alg = alg_arg;
6224 size_t expected_output_length = expected_output_length_arg;
6225 size_t key_bits;
6226 unsigned char *output = NULL;
6227 size_t output_size;
6228 size_t output_length = ~0;
6229 psa_status_t actual_status;
6230 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006232
Gilles Peskine8817f612018-12-18 00:18:46 +01006233 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006234
Gilles Peskine656896e2018-06-29 19:12:28 +02006235 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006236 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6237 psa_set_key_algorithm( &attributes, alg );
6238 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006239 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006240 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006241
6242 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006243 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006244 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006245
Gilles Peskine656896e2018-06-29 19:12:28 +02006246 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006247 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006248 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006249
6250 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006251 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006252 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006253 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006254 output, output_size,
6255 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006256 TEST_EQUAL( actual_status, expected_status );
6257 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006258
Gilles Peskine68428122018-06-30 18:42:41 +02006259 /* If the label is empty, the test framework puts a non-null pointer
6260 * in label->x. Test that a null pointer works as well. */
6261 if( label->len == 0 )
6262 {
6263 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006264 if( output_size != 0 )
6265 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006266 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006267 input_data->x, input_data->len,
6268 NULL, label->len,
6269 output, output_size,
6270 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006271 TEST_EQUAL( actual_status, expected_status );
6272 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006273 }
6274
Gilles Peskine656896e2018-06-29 19:12:28 +02006275exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006276 /*
6277 * Key attributes may have been returned by psa_get_key_attributes()
6278 * thus reset them as required.
6279 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006280 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006281
Ronald Cron5425a212020-08-04 14:58:35 +02006282 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006283 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006284 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006285}
6286/* END_CASE */
6287
6288/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006289void asymmetric_encrypt_decrypt( int key_type_arg,
6290 data_t *key_data,
6291 int alg_arg,
6292 data_t *input_data,
6293 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006294{
Ronald Cron5425a212020-08-04 14:58:35 +02006295 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006296 psa_key_type_t key_type = key_type_arg;
6297 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006298 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006299 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006300 size_t output_size;
6301 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006302 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006303 size_t output2_size;
6304 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006305 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006306
Gilles Peskine8817f612018-12-18 00:18:46 +01006307 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006308
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006309 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6310 psa_set_key_algorithm( &attributes, alg );
6311 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006312
Gilles Peskine049c7532019-05-15 20:22:09 +02006313 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006314 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006315
6316 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006317 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006318 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006319
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006320 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006321 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006322 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006323
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006324 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006325 TEST_ASSERT( output2_size <=
6326 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6327 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006328 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006329
Gilles Peskineeebd7382018-06-08 18:11:54 +02006330 /* We test encryption by checking that encrypt-then-decrypt gives back
6331 * the original plaintext because of the non-optional random
6332 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006333 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006334 input_data->x, input_data->len,
6335 label->x, label->len,
6336 output, output_size,
6337 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006338 /* We don't know what ciphertext length to expect, but check that
6339 * it looks sensible. */
6340 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006341
Ronald Cron5425a212020-08-04 14:58:35 +02006342 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006343 output, output_length,
6344 label->x, label->len,
6345 output2, output2_size,
6346 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006347 ASSERT_COMPARE( input_data->x, input_data->len,
6348 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006349
6350exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006351 /*
6352 * Key attributes may have been returned by psa_get_key_attributes()
6353 * thus reset them as required.
6354 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006355 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006356
Ronald Cron5425a212020-08-04 14:58:35 +02006357 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006358 mbedtls_free( output );
6359 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006360 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006361}
6362/* END_CASE */
6363
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006364/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006365void asymmetric_decrypt( int key_type_arg,
6366 data_t *key_data,
6367 int alg_arg,
6368 data_t *input_data,
6369 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006370 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006371{
Ronald Cron5425a212020-08-04 14:58:35 +02006372 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006373 psa_key_type_t key_type = key_type_arg;
6374 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006375 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006376 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006377 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006378 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006379 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006380
Gilles Peskine8817f612018-12-18 00:18:46 +01006381 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006382
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006383 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6384 psa_set_key_algorithm( &attributes, alg );
6385 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006386
Gilles Peskine049c7532019-05-15 20:22:09 +02006387 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006388 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006389
gabor-mezei-armceface22021-01-21 12:26:17 +01006390 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6391 key_bits = psa_get_key_bits( &attributes );
6392
6393 /* Determine the maximum ciphertext length */
6394 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6395 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6396 ASSERT_ALLOC( output, output_size );
6397
Ronald Cron5425a212020-08-04 14:58:35 +02006398 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006399 input_data->x, input_data->len,
6400 label->x, label->len,
6401 output,
6402 output_size,
6403 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006404 ASSERT_COMPARE( expected_data->x, expected_data->len,
6405 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006406
Gilles Peskine68428122018-06-30 18:42:41 +02006407 /* If the label is empty, the test framework puts a non-null pointer
6408 * in label->x. Test that a null pointer works as well. */
6409 if( label->len == 0 )
6410 {
6411 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006412 if( output_size != 0 )
6413 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006414 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006415 input_data->x, input_data->len,
6416 NULL, label->len,
6417 output,
6418 output_size,
6419 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006420 ASSERT_COMPARE( expected_data->x, expected_data->len,
6421 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006422 }
6423
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006424exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006425 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006426 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006427 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006428 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006429}
6430/* END_CASE */
6431
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006432/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006433void asymmetric_decrypt_fail( int key_type_arg,
6434 data_t *key_data,
6435 int alg_arg,
6436 data_t *input_data,
6437 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006438 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006439 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006440{
Ronald Cron5425a212020-08-04 14:58:35 +02006441 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006442 psa_key_type_t key_type = key_type_arg;
6443 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006444 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006445 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006446 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006447 psa_status_t actual_status;
6448 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006450
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006451 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006452
Gilles Peskine8817f612018-12-18 00:18:46 +01006453 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006454
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006455 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6456 psa_set_key_algorithm( &attributes, alg );
6457 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006458
Gilles Peskine049c7532019-05-15 20:22:09 +02006459 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006460 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006461
Ronald Cron5425a212020-08-04 14:58:35 +02006462 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006463 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006464 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006465 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006466 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006467 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006468 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006469
Gilles Peskine68428122018-06-30 18:42:41 +02006470 /* If the label is empty, the test framework puts a non-null pointer
6471 * in label->x. Test that a null pointer works as well. */
6472 if( label->len == 0 )
6473 {
6474 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006475 if( output_size != 0 )
6476 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006477 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006478 input_data->x, input_data->len,
6479 NULL, label->len,
6480 output, output_size,
6481 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006482 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006483 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006484 }
6485
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006486exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006487 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006488 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006489 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006490 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006491}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006492/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006493
6494/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006495void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006496{
6497 /* Test each valid way of initializing the object, except for `= {0}`, as
6498 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6499 * though it's OK by the C standard. We could test for this, but we'd need
6500 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006501 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006502 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6503 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6504 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006505
6506 memset( &zero, 0, sizeof( zero ) );
6507
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006508 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006509 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006510 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006511 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006512 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006513 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006514 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006515
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006516 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006517 PSA_ASSERT( psa_key_derivation_abort(&func) );
6518 PSA_ASSERT( psa_key_derivation_abort(&init) );
6519 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006520}
6521/* END_CASE */
6522
Janos Follath16de4a42019-06-13 16:32:24 +01006523/* BEGIN_CASE */
6524void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006525{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006526 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006527 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006528 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006529
Gilles Peskine8817f612018-12-18 00:18:46 +01006530 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006531
Janos Follath16de4a42019-06-13 16:32:24 +01006532 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006533 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006534
6535exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006536 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006537 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006538}
6539/* END_CASE */
6540
Janos Follathaf3c2a02019-06-12 12:34:34 +01006541/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006542void derive_set_capacity( int alg_arg, int capacity_arg,
6543 int expected_status_arg )
6544{
6545 psa_algorithm_t alg = alg_arg;
6546 size_t capacity = capacity_arg;
6547 psa_status_t expected_status = expected_status_arg;
6548 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6549
6550 PSA_ASSERT( psa_crypto_init( ) );
6551
6552 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6553
6554 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6555 expected_status );
6556
6557exit:
6558 psa_key_derivation_abort( &operation );
6559 PSA_DONE( );
6560}
6561/* END_CASE */
6562
6563/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006564void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006565 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006566 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006567 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006568 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006569 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006570 int expected_status_arg3,
6571 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006572{
6573 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006574 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6575 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006576 psa_status_t expected_statuses[] = {expected_status_arg1,
6577 expected_status_arg2,
6578 expected_status_arg3};
6579 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006580 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6581 MBEDTLS_SVC_KEY_ID_INIT,
6582 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006583 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6585 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006586 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006587 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006588 psa_status_t expected_output_status = expected_output_status_arg;
6589 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006590
6591 PSA_ASSERT( psa_crypto_init( ) );
6592
6593 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6594 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006595
6596 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6597
6598 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6599 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006600 mbedtls_test_set_step( i );
6601 if( steps[i] == 0 )
6602 {
6603 /* Skip this step */
6604 }
6605 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006606 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006607 psa_set_key_type( &attributes, key_types[i] );
6608 PSA_ASSERT( psa_import_key( &attributes,
6609 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006610 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006611 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6612 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6613 {
6614 // When taking a private key as secret input, use key agreement
6615 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006616 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6617 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006618 expected_statuses[i] );
6619 }
6620 else
6621 {
6622 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006623 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006624 expected_statuses[i] );
6625 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006626 }
6627 else
6628 {
6629 TEST_EQUAL( psa_key_derivation_input_bytes(
6630 &operation, steps[i],
6631 inputs[i]->x, inputs[i]->len ),
6632 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006633 }
6634 }
6635
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006636 if( output_key_type != PSA_KEY_TYPE_NONE )
6637 {
6638 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006639 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006640 psa_set_key_bits( &attributes, 8 );
6641 actual_output_status =
6642 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006643 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006644 }
6645 else
6646 {
6647 uint8_t buffer[1];
6648 actual_output_status =
6649 psa_key_derivation_output_bytes( &operation,
6650 buffer, sizeof( buffer ) );
6651 }
6652 TEST_EQUAL( actual_output_status, expected_output_status );
6653
Janos Follathaf3c2a02019-06-12 12:34:34 +01006654exit:
6655 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006656 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6657 psa_destroy_key( keys[i] );
6658 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006659 PSA_DONE( );
6660}
6661/* END_CASE */
6662
Janos Follathd958bb72019-07-03 15:02:16 +01006663/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006664void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006665{
Janos Follathd958bb72019-07-03 15:02:16 +01006666 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006667 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006668 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006669 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006670 unsigned char input1[] = "Input 1";
6671 size_t input1_length = sizeof( input1 );
6672 unsigned char input2[] = "Input 2";
6673 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006674 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006675 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006676 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6677 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6678 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006679 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006680
Gilles Peskine8817f612018-12-18 00:18:46 +01006681 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006682
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006683 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6684 psa_set_key_algorithm( &attributes, alg );
6685 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006686
Gilles Peskine73676cb2019-05-15 20:15:10 +02006687 PSA_ASSERT( psa_import_key( &attributes,
6688 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006689 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006690
6691 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006692 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6693 input1, input1_length,
6694 input2, input2_length,
6695 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006696 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006697
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006698 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006699 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006700 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006701
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006702 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006703
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006704 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006705 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006706
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006707exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006708 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006709 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006710 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006711}
6712/* END_CASE */
6713
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006714/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006715void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006716{
6717 uint8_t output_buffer[16];
6718 size_t buffer_size = 16;
6719 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006720 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006721
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006722 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6723 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006724 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006725
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006726 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006727 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006728
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006729 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006730
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006731 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6732 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006733 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006734
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006735 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006736 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006737
6738exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006739 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006740}
6741/* END_CASE */
6742
6743/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006744void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006745 int step1_arg, data_t *input1,
6746 int step2_arg, data_t *input2,
6747 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006748 int requested_capacity_arg,
6749 data_t *expected_output1,
6750 data_t *expected_output2 )
6751{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006752 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006753 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6754 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006755 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6756 MBEDTLS_SVC_KEY_ID_INIT,
6757 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006758 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006759 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006760 uint8_t *expected_outputs[2] =
6761 {expected_output1->x, expected_output2->x};
6762 size_t output_sizes[2] =
6763 {expected_output1->len, expected_output2->len};
6764 size_t output_buffer_size = 0;
6765 uint8_t *output_buffer = NULL;
6766 size_t expected_capacity;
6767 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006769 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006770 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006771
6772 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6773 {
6774 if( output_sizes[i] > output_buffer_size )
6775 output_buffer_size = output_sizes[i];
6776 if( output_sizes[i] == 0 )
6777 expected_outputs[i] = NULL;
6778 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006779 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006781
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006782 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6783 psa_set_key_algorithm( &attributes, alg );
6784 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006785
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006786 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006787 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6788 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6789 requested_capacity ) );
6790 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006791 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006792 switch( steps[i] )
6793 {
6794 case 0:
6795 break;
6796 case PSA_KEY_DERIVATION_INPUT_SECRET:
6797 PSA_ASSERT( psa_import_key( &attributes,
6798 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006799 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006800
6801 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6802 {
6803 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6804 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6805 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6806 }
6807
Gilles Peskine1468da72019-05-29 17:35:49 +02006808 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006809 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006810 break;
6811 default:
6812 PSA_ASSERT( psa_key_derivation_input_bytes(
6813 &operation, steps[i],
6814 inputs[i]->x, inputs[i]->len ) );
6815 break;
6816 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006817 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006818
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006819 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006820 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006821 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006822 expected_capacity = requested_capacity;
6823
6824 /* Expansion phase. */
6825 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6826 {
6827 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006828 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006829 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006830 if( expected_capacity == 0 && output_sizes[i] == 0 )
6831 {
6832 /* Reading 0 bytes when 0 bytes are available can go either way. */
6833 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006834 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006835 continue;
6836 }
6837 else if( expected_capacity == 0 ||
6838 output_sizes[i] > expected_capacity )
6839 {
6840 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006841 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006842 expected_capacity = 0;
6843 continue;
6844 }
6845 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006846 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006847 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006848 ASSERT_COMPARE( output_buffer, output_sizes[i],
6849 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006850 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006851 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006852 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006853 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006854 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006855 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006856 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006857
6858exit:
6859 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006860 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006861 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6862 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006863 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006864}
6865/* END_CASE */
6866
6867/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006868void derive_full( int alg_arg,
6869 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006870 data_t *input1,
6871 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006872 int requested_capacity_arg )
6873{
Ronald Cron5425a212020-08-04 14:58:35 +02006874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006875 psa_algorithm_t alg = alg_arg;
6876 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006877 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006878 unsigned char output_buffer[16];
6879 size_t expected_capacity = requested_capacity;
6880 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006881 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006882
Gilles Peskine8817f612018-12-18 00:18:46 +01006883 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006884
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006885 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6886 psa_set_key_algorithm( &attributes, alg );
6887 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006888
Gilles Peskine049c7532019-05-15 20:22:09 +02006889 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006890 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006891
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006892 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6893 input1->x, input1->len,
6894 input2->x, input2->len,
6895 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006896 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006897
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006898 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006899 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006900 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006901
6902 /* Expansion phase. */
6903 while( current_capacity > 0 )
6904 {
6905 size_t read_size = sizeof( output_buffer );
6906 if( read_size > current_capacity )
6907 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006908 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006909 output_buffer,
6910 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006911 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006912 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006913 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006914 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006915 }
6916
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006917 /* Check that the operation refuses to go over capacity. */
6918 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006919 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006920
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006921 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006922
6923exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006924 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006925 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006926 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006927}
6928/* END_CASE */
6929
Janos Follathe60c9052019-07-03 13:51:30 +01006930/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006931void derive_key_exercise( int alg_arg,
6932 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006933 data_t *input1,
6934 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006935 int derived_type_arg,
6936 int derived_bits_arg,
6937 int derived_usage_arg,
6938 int derived_alg_arg )
6939{
Ronald Cron5425a212020-08-04 14:58:35 +02006940 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6941 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006942 psa_algorithm_t alg = alg_arg;
6943 psa_key_type_t derived_type = derived_type_arg;
6944 size_t derived_bits = derived_bits_arg;
6945 psa_key_usage_t derived_usage = derived_usage_arg;
6946 psa_algorithm_t derived_alg = derived_alg_arg;
6947 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006948 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006950 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006951
Gilles Peskine8817f612018-12-18 00:18:46 +01006952 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006953
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006954 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6955 psa_set_key_algorithm( &attributes, alg );
6956 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006957 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006958 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006959
6960 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006961 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6962 input1->x, input1->len,
6963 input2->x, input2->len,
6964 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006965 goto exit;
6966
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006967 psa_set_key_usage_flags( &attributes, derived_usage );
6968 psa_set_key_algorithm( &attributes, derived_alg );
6969 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006970 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006971 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006972 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006973
6974 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006975 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006976 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6977 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006978
6979 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006980 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006981 goto exit;
6982
6983exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006984 /*
6985 * Key attributes may have been returned by psa_get_key_attributes()
6986 * thus reset them as required.
6987 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006988 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006989
6990 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006991 psa_destroy_key( base_key );
6992 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006993 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006994}
6995/* END_CASE */
6996
Janos Follath42fd8882019-07-03 14:17:09 +01006997/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006998void derive_key_export( int alg_arg,
6999 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007000 data_t *input1,
7001 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007002 int bytes1_arg,
7003 int bytes2_arg )
7004{
Ronald Cron5425a212020-08-04 14:58:35 +02007005 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7006 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007007 psa_algorithm_t alg = alg_arg;
7008 size_t bytes1 = bytes1_arg;
7009 size_t bytes2 = bytes2_arg;
7010 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007011 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007012 uint8_t *output_buffer = NULL;
7013 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007014 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7015 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007016 size_t length;
7017
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007018 ASSERT_ALLOC( output_buffer, capacity );
7019 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007020 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007021
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007022 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7023 psa_set_key_algorithm( &base_attributes, alg );
7024 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007025 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007026 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007027
7028 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007029 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7030 input1->x, input1->len,
7031 input2->x, input2->len,
7032 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007033 goto exit;
7034
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007035 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007036 output_buffer,
7037 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007038 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007039
7040 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007041 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7042 input1->x, input1->len,
7043 input2->x, input2->len,
7044 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007045 goto exit;
7046
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007047 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7048 psa_set_key_algorithm( &derived_attributes, 0 );
7049 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007050 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007051 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007052 &derived_key ) );
7053 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007054 export_buffer, bytes1,
7055 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007056 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007057 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007058 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007059 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007060 &derived_key ) );
7061 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007062 export_buffer + bytes1, bytes2,
7063 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007064 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007065
7066 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007067 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7068 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007069
7070exit:
7071 mbedtls_free( output_buffer );
7072 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007073 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007074 psa_destroy_key( base_key );
7075 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007076 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007077}
7078/* END_CASE */
7079
7080/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007081void derive_key( int alg_arg,
7082 data_t *key_data, data_t *input1, data_t *input2,
7083 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007084 int expected_status_arg,
7085 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02007086{
Ronald Cron5425a212020-08-04 14:58:35 +02007087 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7088 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02007089 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007090 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02007091 size_t bits = bits_arg;
7092 psa_status_t expected_status = expected_status_arg;
7093 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7094 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7095 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7096
7097 PSA_ASSERT( psa_crypto_init( ) );
7098
7099 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7100 psa_set_key_algorithm( &base_attributes, alg );
7101 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7102 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007103 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02007104
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007105 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7106 input1->x, input1->len,
7107 input2->x, input2->len,
7108 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02007109 goto exit;
7110
7111 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7112 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007113 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02007114 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01007115
7116 psa_status_t status =
7117 psa_key_derivation_output_key( &derived_attributes,
7118 &operation,
7119 &derived_key );
7120 if( is_large_output > 0 )
7121 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7122 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02007123
7124exit:
7125 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007126 psa_destroy_key( base_key );
7127 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02007128 PSA_DONE( );
7129}
7130/* END_CASE */
7131
7132/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02007133void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02007134 int our_key_type_arg, int our_key_alg_arg,
7135 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02007136 int expected_status_arg )
7137{
Ronald Cron5425a212020-08-04 14:58:35 +02007138 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007139 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007140 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007141 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007142 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007144 psa_status_t expected_status = expected_status_arg;
7145 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007146
Gilles Peskine8817f612018-12-18 00:18:46 +01007147 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007148
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007149 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007150 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007151 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007152 PSA_ASSERT( psa_import_key( &attributes,
7153 our_key_data->x, our_key_data->len,
7154 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007155
Gilles Peskine77f40d82019-04-11 21:27:06 +02007156 /* The tests currently include inputs that should fail at either step.
7157 * Test cases that fail at the setup step should be changed to call
7158 * key_derivation_setup instead, and this function should be renamed
7159 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007160 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02007161 if( status == PSA_SUCCESS )
7162 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007163 TEST_EQUAL( psa_key_derivation_key_agreement(
7164 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
7165 our_key,
7166 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02007167 expected_status );
7168 }
7169 else
7170 {
7171 TEST_ASSERT( status == expected_status );
7172 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007173
7174exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007175 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007176 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007177 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007178}
7179/* END_CASE */
7180
7181/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007182void raw_key_agreement( int alg_arg,
7183 int our_key_type_arg, data_t *our_key_data,
7184 data_t *peer_key_data,
7185 data_t *expected_output )
7186{
Ronald Cron5425a212020-08-04 14:58:35 +02007187 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007188 psa_algorithm_t alg = alg_arg;
7189 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007190 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007191 unsigned char *output = NULL;
7192 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007193 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007194
7195 ASSERT_ALLOC( output, expected_output->len );
7196 PSA_ASSERT( psa_crypto_init( ) );
7197
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007198 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7199 psa_set_key_algorithm( &attributes, alg );
7200 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007201 PSA_ASSERT( psa_import_key( &attributes,
7202 our_key_data->x, our_key_data->len,
7203 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007204
gabor-mezei-armceface22021-01-21 12:26:17 +01007205 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7206 key_bits = psa_get_key_bits( &attributes );
7207
Gilles Peskinebe697d82019-05-16 18:00:41 +02007208 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7209 peer_key_data->x, peer_key_data->len,
7210 output, expected_output->len,
7211 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007212 ASSERT_COMPARE( output, output_length,
7213 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01007214 TEST_ASSERT( output_length <=
7215 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7216 TEST_ASSERT( output_length <=
7217 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007218
7219exit:
7220 mbedtls_free( output );
7221 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007222 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007223}
7224/* END_CASE */
7225
7226/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007227void key_agreement_capacity( int alg_arg,
7228 int our_key_type_arg, data_t *our_key_data,
7229 data_t *peer_key_data,
7230 int expected_capacity_arg )
7231{
Ronald Cron5425a212020-08-04 14:58:35 +02007232 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007233 psa_algorithm_t alg = alg_arg;
7234 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007235 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007236 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007237 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007238 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007239
Gilles Peskine8817f612018-12-18 00:18:46 +01007240 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007241
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007242 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7243 psa_set_key_algorithm( &attributes, alg );
7244 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007245 PSA_ASSERT( psa_import_key( &attributes,
7246 our_key_data->x, our_key_data->len,
7247 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007248
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007249 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007250 PSA_ASSERT( psa_key_derivation_key_agreement(
7251 &operation,
7252 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7253 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007254 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7255 {
7256 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007257 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007258 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007259 NULL, 0 ) );
7260 }
Gilles Peskine59685592018-09-18 12:11:34 +02007261
Gilles Peskinebf491972018-10-25 22:36:12 +02007262 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007263 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007264 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007265 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007266
Gilles Peskinebf491972018-10-25 22:36:12 +02007267 /* Test the actual capacity by reading the output. */
7268 while( actual_capacity > sizeof( output ) )
7269 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007270 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007271 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007272 actual_capacity -= sizeof( output );
7273 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007274 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007275 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007276 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007277 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007278
Gilles Peskine59685592018-09-18 12:11:34 +02007279exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007280 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007281 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007282 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007283}
7284/* END_CASE */
7285
7286/* BEGIN_CASE */
7287void key_agreement_output( int alg_arg,
7288 int our_key_type_arg, data_t *our_key_data,
7289 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007290 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007291{
Ronald Cron5425a212020-08-04 14:58:35 +02007292 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007293 psa_algorithm_t alg = alg_arg;
7294 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007295 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007296 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007297 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007298
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007299 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7300 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007301
Gilles Peskine8817f612018-12-18 00:18:46 +01007302 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007303
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007304 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7305 psa_set_key_algorithm( &attributes, alg );
7306 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007307 PSA_ASSERT( psa_import_key( &attributes,
7308 our_key_data->x, our_key_data->len,
7309 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007310
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007311 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007312 PSA_ASSERT( psa_key_derivation_key_agreement(
7313 &operation,
7314 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7315 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007316 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7317 {
7318 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007319 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007320 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007321 NULL, 0 ) );
7322 }
Gilles Peskine59685592018-09-18 12:11:34 +02007323
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007324 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007325 actual_output,
7326 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007327 ASSERT_COMPARE( actual_output, expected_output1->len,
7328 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007329 if( expected_output2->len != 0 )
7330 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007331 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007332 actual_output,
7333 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007334 ASSERT_COMPARE( actual_output, expected_output2->len,
7335 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007336 }
Gilles Peskine59685592018-09-18 12:11:34 +02007337
7338exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007339 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007340 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007341 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007342 mbedtls_free( actual_output );
7343}
7344/* END_CASE */
7345
7346/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007347void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007348{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007349 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007350 unsigned char *output = NULL;
7351 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007352 size_t i;
7353 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007354
Simon Butcher49f8e312020-03-03 15:51:50 +00007355 TEST_ASSERT( bytes_arg >= 0 );
7356
Gilles Peskine91892022021-02-08 19:50:26 +01007357 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007358 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007359
Gilles Peskine8817f612018-12-18 00:18:46 +01007360 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007361
Gilles Peskinea50d7392018-06-21 10:22:13 +02007362 /* Run several times, to ensure that every output byte will be
7363 * nonzero at least once with overwhelming probability
7364 * (2^(-8*number_of_runs)). */
7365 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007366 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007367 if( bytes != 0 )
7368 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007369 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007370
Gilles Peskinea50d7392018-06-21 10:22:13 +02007371 for( i = 0; i < bytes; i++ )
7372 {
7373 if( output[i] != 0 )
7374 ++changed[i];
7375 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007376 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007377
7378 /* Check that every byte was changed to nonzero at least once. This
7379 * validates that psa_generate_random is overwriting every byte of
7380 * the output buffer. */
7381 for( i = 0; i < bytes; i++ )
7382 {
7383 TEST_ASSERT( changed[i] != 0 );
7384 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007385
7386exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007387 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007388 mbedtls_free( output );
7389 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007390}
7391/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007392
7393/* BEGIN_CASE */
7394void generate_key( int type_arg,
7395 int bits_arg,
7396 int usage_arg,
7397 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007398 int expected_status_arg,
7399 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007400{
Ronald Cron5425a212020-08-04 14:58:35 +02007401 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007402 psa_key_type_t type = type_arg;
7403 psa_key_usage_t usage = usage_arg;
7404 size_t bits = bits_arg;
7405 psa_algorithm_t alg = alg_arg;
7406 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007408 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007409
Gilles Peskine8817f612018-12-18 00:18:46 +01007410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007411
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007412 psa_set_key_usage_flags( &attributes, usage );
7413 psa_set_key_algorithm( &attributes, alg );
7414 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007415 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007416
7417 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007418 psa_status_t status = psa_generate_key( &attributes, &key );
7419
7420 if( is_large_key > 0 )
7421 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7422 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007423 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007424 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007425
7426 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007427 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007428 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7429 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007430
Gilles Peskine818ca122018-06-20 18:16:48 +02007431 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007432 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007433 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007434
7435exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007436 /*
7437 * Key attributes may have been returned by psa_get_key_attributes()
7438 * thus reset them as required.
7439 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007440 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007441
Ronald Cron5425a212020-08-04 14:58:35 +02007442 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007443 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007444}
7445/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007446
Ronald Cronee414c72021-03-18 18:50:08 +01007447/* 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 +02007448void generate_key_rsa( int bits_arg,
7449 data_t *e_arg,
7450 int expected_status_arg )
7451{
Ronald Cron5425a212020-08-04 14:58:35 +02007452 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007453 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007454 size_t bits = bits_arg;
7455 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7456 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7457 psa_status_t expected_status = expected_status_arg;
7458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7459 uint8_t *exported = NULL;
7460 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007461 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007462 size_t exported_length = SIZE_MAX;
7463 uint8_t *e_read_buffer = NULL;
7464 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007465 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007466 size_t e_read_length = SIZE_MAX;
7467
7468 if( e_arg->len == 0 ||
7469 ( e_arg->len == 3 &&
7470 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7471 {
7472 is_default_public_exponent = 1;
7473 e_read_size = 0;
7474 }
7475 ASSERT_ALLOC( e_read_buffer, e_read_size );
7476 ASSERT_ALLOC( exported, exported_size );
7477
7478 PSA_ASSERT( psa_crypto_init( ) );
7479
7480 psa_set_key_usage_flags( &attributes, usage );
7481 psa_set_key_algorithm( &attributes, alg );
7482 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7483 e_arg->x, e_arg->len ) );
7484 psa_set_key_bits( &attributes, bits );
7485
7486 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007487 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007488 if( expected_status != PSA_SUCCESS )
7489 goto exit;
7490
7491 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007492 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007493 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7494 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7495 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7496 e_read_buffer, e_read_size,
7497 &e_read_length ) );
7498 if( is_default_public_exponent )
7499 TEST_EQUAL( e_read_length, 0 );
7500 else
7501 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7502
7503 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007504 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007505 goto exit;
7506
7507 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007508 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007509 exported, exported_size,
7510 &exported_length ) );
7511 {
7512 uint8_t *p = exported;
7513 uint8_t *end = exported + exported_length;
7514 size_t len;
7515 /* RSAPublicKey ::= SEQUENCE {
7516 * modulus INTEGER, -- n
7517 * publicExponent INTEGER } -- e
7518 */
7519 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007520 MBEDTLS_ASN1_SEQUENCE |
7521 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007522 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007523 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7524 MBEDTLS_ASN1_INTEGER ) );
7525 if( len >= 1 && p[0] == 0 )
7526 {
7527 ++p;
7528 --len;
7529 }
7530 if( e_arg->len == 0 )
7531 {
7532 TEST_EQUAL( len, 3 );
7533 TEST_EQUAL( p[0], 1 );
7534 TEST_EQUAL( p[1], 0 );
7535 TEST_EQUAL( p[2], 1 );
7536 }
7537 else
7538 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7539 }
7540
7541exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007542 /*
7543 * Key attributes may have been returned by psa_get_key_attributes() or
7544 * set by psa_set_key_domain_parameters() thus reset them as required.
7545 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007546 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007547
Ronald Cron5425a212020-08-04 14:58:35 +02007548 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007549 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007550 mbedtls_free( e_read_buffer );
7551 mbedtls_free( exported );
7552}
7553/* END_CASE */
7554
Darryl Greend49a4992018-06-18 17:27:26 +01007555/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007556void persistent_key_load_key_from_storage( data_t *data,
7557 int type_arg, int bits_arg,
7558 int usage_flags_arg, int alg_arg,
7559 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007560{
Ronald Cron71016a92020-08-28 19:01:50 +02007561 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007562 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007563 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7564 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007565 psa_key_type_t type = type_arg;
7566 size_t bits = bits_arg;
7567 psa_key_usage_t usage_flags = usage_flags_arg;
7568 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007569 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007570 unsigned char *first_export = NULL;
7571 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007572 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007573 size_t first_exported_length;
7574 size_t second_exported_length;
7575
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007576 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7577 {
7578 ASSERT_ALLOC( first_export, export_size );
7579 ASSERT_ALLOC( second_export, export_size );
7580 }
Darryl Greend49a4992018-06-18 17:27:26 +01007581
Gilles Peskine8817f612018-12-18 00:18:46 +01007582 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007583
Gilles Peskinec87af662019-05-15 16:12:22 +02007584 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007585 psa_set_key_usage_flags( &attributes, usage_flags );
7586 psa_set_key_algorithm( &attributes, alg );
7587 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007588 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007589
Darryl Green0c6575a2018-11-07 16:05:30 +00007590 switch( generation_method )
7591 {
7592 case IMPORT_KEY:
7593 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007594 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007595 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007596 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007597
Darryl Green0c6575a2018-11-07 16:05:30 +00007598 case GENERATE_KEY:
7599 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007600 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007601 break;
7602
7603 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007604#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007605 {
7606 /* Create base key */
7607 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7608 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7609 psa_set_key_usage_flags( &base_attributes,
7610 PSA_KEY_USAGE_DERIVE );
7611 psa_set_key_algorithm( &base_attributes, derive_alg );
7612 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007613 PSA_ASSERT( psa_import_key( &base_attributes,
7614 data->x, data->len,
7615 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007616 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007617 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007618 PSA_ASSERT( psa_key_derivation_input_key(
7619 &operation,
7620 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007621 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007622 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007623 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007624 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7625 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007626 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007627 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007628 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007629 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007630 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007631#else
7632 TEST_ASSUME( ! "KDF not supported in this configuration" );
7633#endif
7634 break;
7635
7636 default:
7637 TEST_ASSERT( ! "generation_method not implemented in test" );
7638 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007639 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007640 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007641
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007642 /* Export the key if permitted by the key policy. */
7643 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7644 {
Ronald Cron5425a212020-08-04 14:58:35 +02007645 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007646 first_export, export_size,
7647 &first_exported_length ) );
7648 if( generation_method == IMPORT_KEY )
7649 ASSERT_COMPARE( data->x, data->len,
7650 first_export, first_exported_length );
7651 }
Darryl Greend49a4992018-06-18 17:27:26 +01007652
7653 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007654 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007655 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007656 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007657
Darryl Greend49a4992018-06-18 17:27:26 +01007658 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007659 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007660 TEST_ASSERT( mbedtls_svc_key_id_equal(
7661 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007662 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7663 PSA_KEY_LIFETIME_PERSISTENT );
7664 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7665 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007666 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007667 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007668 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007669
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007670 /* Export the key again if permitted by the key policy. */
7671 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007672 {
Ronald Cron5425a212020-08-04 14:58:35 +02007673 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007674 second_export, export_size,
7675 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007676 ASSERT_COMPARE( first_export, first_exported_length,
7677 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007678 }
7679
7680 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007681 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007682 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007683
7684exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007685 /*
7686 * Key attributes may have been returned by psa_get_key_attributes()
7687 * thus reset them as required.
7688 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007689 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007690
Darryl Greend49a4992018-06-18 17:27:26 +01007691 mbedtls_free( first_export );
7692 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007693 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007694 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007695 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007696 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007697}
7698/* END_CASE */