blob: 40dca9d0cb9d5309f7840a74e3a837cbd19a1a4b [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010024
Gilles Peskine4023c012021-05-27 13:21:20 +020025/* If this comes up, it's a bug in the test code or in the test data. */
26#define UNUSED 0xdeadbeef
27
Dave Rodgman647791d2021-06-23 12:49:59 +010028/* Assert that an operation is (not) active.
29 * This serves as a proxy for checking if the operation is aborted. */
30#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
31#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010032
33/** An invalid export length that will never be set by psa_export_key(). */
34static const size_t INVALID_EXPORT_LENGTH = ~0U;
35
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036/** Test if a buffer contains a constant byte value.
37 *
38 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 *
40 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042 * \param size Size of the buffer in bytes.
43 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 * \return 1 if the buffer is all-bits-zero.
45 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048{
49 size_t i;
50 for( i = 0; i < size; i++ )
51 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020052 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020055 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020056}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010057#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
59static int asn1_write_10x( unsigned char **p,
60 unsigned char *start,
61 size_t bits,
62 unsigned char x )
63{
64 int ret;
65 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020066 if( bits == 0 )
67 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
68 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020069 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030070 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020071 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
72 *p -= len;
73 ( *p )[len-1] = x;
74 if( bits % 8 == 0 )
75 ( *p )[1] |= 1;
76 else
77 ( *p )[0] |= 1 << ( bits % 8 );
78 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
80 MBEDTLS_ASN1_INTEGER ) );
81 return( len );
82}
83
84static int construct_fake_rsa_key( unsigned char *buffer,
85 size_t buffer_size,
86 unsigned char **p,
87 size_t bits,
88 int keypair )
89{
90 size_t half_bits = ( bits + 1 ) / 2;
91 int ret;
92 int len = 0;
93 /* Construct something that looks like a DER encoding of
94 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
95 * RSAPrivateKey ::= SEQUENCE {
96 * version Version,
97 * modulus INTEGER, -- n
98 * publicExponent INTEGER, -- e
99 * privateExponent INTEGER, -- d
100 * prime1 INTEGER, -- p
101 * prime2 INTEGER, -- q
102 * exponent1 INTEGER, -- d mod (p-1)
103 * exponent2 INTEGER, -- d mod (q-1)
104 * coefficient INTEGER, -- (inverse of q) mod p
105 * otherPrimeInfos OtherPrimeInfos OPTIONAL
106 * }
107 * Or, for a public key, the same structure with only
108 * version, modulus and publicExponent.
109 */
110 *p = buffer + buffer_size;
111 if( keypair )
112 {
113 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
116 asn1_write_10x( p, buffer, half_bits, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
118 asn1_write_10x( p, buffer, half_bits, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* q */
120 asn1_write_10x( p, buffer, half_bits, 1 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x( p, buffer, half_bits, 3 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* d */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 }
126 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
127 asn1_write_10x( p, buffer, 17, 1 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, /* n */
129 asn1_write_10x( p, buffer, bits, 1 ) );
130 if( keypair )
131 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
132 mbedtls_asn1_write_int( p, buffer, 0 ) );
133 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
134 {
135 const unsigned char tag =
136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
137 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
138 }
139 return( len );
140}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100141#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100143int exercise_mac_setup( psa_key_type_t key_type,
144 const unsigned char *key_bytes,
145 size_t key_length,
146 psa_algorithm_t alg,
147 psa_mac_operation_t *operation,
148 psa_status_t *status )
149{
Ronald Cron5425a212020-08-04 14:58:35 +0200150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200154 psa_set_key_algorithm( &attributes, alg );
155 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200156 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157
Ronald Cron5425a212020-08-04 14:58:35 +0200158 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100159 /* Whether setup succeeded or failed, abort must succeed. */
160 PSA_ASSERT( psa_mac_abort( operation ) );
161 /* If setup failed, reproduce the failure, so that the caller can
162 * test the resulting state of the operation object. */
163 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 {
Ronald Cron5425a212020-08-04 14:58:35 +0200165 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 }
167
Ronald Cron5425a212020-08-04 14:58:35 +0200168 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100169 return( 1 );
170
171exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200172 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173 return( 0 );
174}
175
176int exercise_cipher_setup( psa_key_type_t key_type,
177 const unsigned char *key_bytes,
178 size_t key_length,
179 psa_algorithm_t alg,
180 psa_cipher_operation_t *operation,
181 psa_status_t *status )
182{
Ronald Cron5425a212020-08-04 14:58:35 +0200183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
187 psa_set_key_algorithm( &attributes, alg );
188 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200189 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190
Ronald Cron5425a212020-08-04 14:58:35 +0200191 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* Whether setup succeeded or failed, abort must succeed. */
193 PSA_ASSERT( psa_cipher_abort( operation ) );
194 /* If setup failed, reproduce the failure, so that the caller can
195 * test the resulting state of the operation object. */
196 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 {
Ronald Cron5425a212020-08-04 14:58:35 +0200198 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100199 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 }
201
Ronald Cron5425a212020-08-04 14:58:35 +0200202 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203 return( 1 );
204
205exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200206 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207 return( 0 );
208}
209
Ronald Cron5425a212020-08-04 14:58:35 +0200210static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211{
212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 uint8_t buffer[1];
215 size_t length;
216 int ok = 0;
217
Ronald Cronecfb2372020-07-23 17:13:42 +0200218 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
220 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
221 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200224 TEST_EQUAL(
225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
226 TEST_EQUAL(
227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200228 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200229 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
233
Ronald Cron5425a212020-08-04 14:58:35 +0200234 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000235 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200236 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000238 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 ok = 1;
241
242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243 /*
244 * Key attributes may have been returned by psa_get_key_attributes()
245 * thus reset them as required.
246 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200247 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100248
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200249 return( ok );
250}
251
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252/* Assert that a key isn't reported as having a slot number. */
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
255 do \
256 { \
257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
258 TEST_EQUAL( psa_get_key_slot_number( \
259 attributes, \
260 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
261 PSA_ERROR_INVALID_ARGUMENT ); \
262 } \
263 while( 0 )
264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
265#define ASSERT_NO_SLOT_NUMBER( attributes ) \
266 ( (void) 0 )
267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100269/* An overapproximation of the amount of storage needed for a key of the
270 * given type and with the given content. The API doesn't make it easy
271 * to find a good value for the size. The current implementation doesn't
272 * care about the value anyway. */
273#define KEY_BITS_FROM_DATA( type, data ) \
274 ( data )->len
275
Darryl Green0c6575a2018-11-07 16:05:30 +0000276typedef enum {
277 IMPORT_KEY = 0,
278 GENERATE_KEY = 1,
279 DERIVE_KEY = 2
280} generate_method;
281
Paul Elliott33746aa2021-09-15 16:40:40 +0100282typedef enum
283{
284 DO_NOT_SET_LENGTHS = 0,
285 SET_LENGTHS_BEFORE_NONCE = 1,
286 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100287} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100288
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100289typedef enum
290{
291 USE_NULL_TAG = 0,
292 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100293} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100294
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100295/*!
296 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100297 * \param key_type_arg Type of key passed in
298 * \param key_data The encryption / decryption key data
299 * \param alg_arg The type of algorithm used
300 * \param nonce Nonce data
301 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100302 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 * feed additional data in to be encrypted /
304 * decrypted. If -1, no chunking.
305 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100306 * \param data_part_len_arg If not -1, the length of chunks to feed
307 * the data in to be encrypted / decrypted. If
308 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100309 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100310 * expected here, this controls whether or not
311 * to set lengths, and in what order with
312 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100315 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100316 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 */
319static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
320 int alg_arg,
321 data_t *nonce,
322 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100323 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100325 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100326 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100328 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100329 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100330{
331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
332 psa_key_type_t key_type = key_type_arg;
333 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100334 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100335 unsigned char *output_data = NULL;
336 unsigned char *part_data = NULL;
337 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100339 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 size_t output_size = 0;
341 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 size_t output_length = 0;
343 size_t key_bits = 0;
344 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100345 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100346 size_t part_length = 0;
347 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100349 size_t ad_part_len = 0;
350 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100351 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100356 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100357
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 PSA_ASSERT( psa_crypto_init( ) );
359
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 if( is_encrypt )
361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
362 else
363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
364
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 psa_set_key_algorithm( &attributes, alg );
366 psa_set_key_type( &attributes, key_type );
367
368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
369 &key ) );
370
371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
372 key_bits = psa_get_key_bits( &attributes );
373
374 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 if( is_encrypt )
377 {
378 /* Tag gets written at end of buffer. */
379 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
380 ( input_data->len +
381 tag_length ) );
382 data_true_size = input_data->len;
383 }
384 else
385 {
386 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
387 ( input_data->len -
388 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 /* Do not want to attempt to decrypt tag. */
391 data_true_size = input_data->len - tag_length;
392 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 ASSERT_ALLOC( output_data, output_size );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396 if( is_encrypt )
397 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100398 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
399 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 }
401 else
402 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100403 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
404 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100405 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100407 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100408
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100409 if( is_encrypt )
410 status = psa_aead_encrypt_setup( &operation, key, alg );
411 else
412 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
414 /* If the operation is not supported, just skip and not fail in case the
415 * encryption involves a common limitation of cryptography hardwares and
416 * an alternative implementation. */
417 if( status == PSA_ERROR_NOT_SUPPORTED )
418 {
419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
421 }
422
423 PSA_ASSERT( status );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 if( set_lengths_method == DO_NOT_SET_LENGTHS )
426 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
427 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100428 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100429 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
430 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
432 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100433 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100434 {
435 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
436
Paul Elliott33746aa2021-09-15 16:40:40 +0100437 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
438 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100441 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
443 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100444 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100446 for( part_offset = 0, part_count = 0;
447 part_offset < additional_data->len;
448 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100450 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 {
Paul Elliott329d5382021-07-22 17:10:45 +0100452 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100453 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100454 else if( additional_data->len - part_offset < ad_part_len )
455 {
456 part_length = additional_data->len - part_offset;
457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 else
459 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100460 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
462
463 PSA_ASSERT( psa_aead_update_ad( &operation,
464 additional_data->x + part_offset,
465 part_length ) );
466
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 }
468 }
469 else
470 {
471 /* Pass additional data in one go. */
472 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
473 additional_data->len ) );
474 }
475
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100476 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
478 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100479 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100481 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
483 ASSERT_ALLOC( part_data, part_data_size );
484
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100485 for( part_offset = 0, part_count = 0;
486 part_offset < data_true_size;
487 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100489 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 {
Paul Elliott329d5382021-07-22 17:10:45 +0100491 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100493 else if( ( data_true_size - part_offset ) < data_part_len )
494 {
495 part_length = ( data_true_size - part_offset );
496 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 else
498 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
502 PSA_ASSERT( psa_aead_update( &operation,
503 ( input_data->x + part_offset ),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length ) );
507
508 if( output_data && output_part_length )
509 {
510 memcpy( ( output_data + part_offset ), part_data,
511 output_part_length );
512 }
513
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 output_length += output_part_length;
515 }
516 }
517 else
518 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 output_size, &output_length ) );
523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( is_encrypt )
526 PSA_ASSERT( psa_aead_finish( &operation, final_data,
527 final_output_size,
528 &output_part_length,
529 tag_buffer, tag_length,
530 &tag_size ) );
531 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100532 {
Paul Elliott9961a662021-09-17 19:19:02 +0100533 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 final_output_size,
535 &output_part_length,
536 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100537 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100540 if( output_data && output_part_length )
541 memcpy( ( output_data + output_length ), final_data,
542 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
549 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551 TEST_EQUAL( tag_length, tag_size );
552
553 if( output_data && tag_length )
554 memcpy( ( output_data + output_length ), tag_buffer,
555 tag_length );
556
557 output_length += tag_length;
558
559 TEST_EQUAL( output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_ASSERT( output_length <=
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
570 TEST_ASSERT( output_length <=
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Gilles Peskinee59236f2018-01-27 23:32:46 +0100592/* END_HEADER */
593
594/* BEGIN_DEPENDENCIES
595 * depends_on:MBEDTLS_PSA_CRYPTO_C
596 * END_DEPENDENCIES
597 */
598
599/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200600void static_checks( )
601{
602 size_t max_truncated_mac_size =
603 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
604
605 /* Check that the length for a truncated MAC always fits in the algorithm
606 * encoding. The shifted mask is the maximum truncated value. The
607 * untruncated algorithm may be one byte larger. */
608 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
609}
610/* END_CASE */
611
612/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200613void import_with_policy( int type_arg,
614 int usage_arg, int alg_arg,
615 int expected_status_arg )
616{
617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
618 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200620 psa_key_type_t type = type_arg;
621 psa_key_usage_t usage = usage_arg;
622 psa_algorithm_t alg = alg_arg;
623 psa_status_t expected_status = expected_status_arg;
624 const uint8_t key_material[16] = {0};
625 psa_status_t status;
626
627 PSA_ASSERT( psa_crypto_init( ) );
628
629 psa_set_key_type( &attributes, type );
630 psa_set_key_usage_flags( &attributes, usage );
631 psa_set_key_algorithm( &attributes, alg );
632
633 status = psa_import_key( &attributes,
634 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200635 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636 TEST_EQUAL( status, expected_status );
637 if( status != PSA_SUCCESS )
638 goto exit;
639
Ronald Cron5425a212020-08-04 14:58:35 +0200640 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200642 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200643 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200644 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200645 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200646
Ronald Cron5425a212020-08-04 14:58:35 +0200647 PSA_ASSERT( psa_destroy_key( key ) );
648 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200649
650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100651 /*
652 * Key attributes may have been returned by psa_get_key_attributes()
653 * thus reset them as required.
654 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200655 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100656
657 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200658 PSA_DONE( );
659}
660/* END_CASE */
661
662/* BEGIN_CASE */
663void import_with_data( data_t *data, int type_arg,
664 int attr_bits_arg,
665 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200666{
667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
668 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200670 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200671 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200672 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
Gilles Peskine4747d192019-04-17 15:05:45 +0200677 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200678 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200679
Ronald Cron5425a212020-08-04 14:58:35 +0200680 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100681 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200682 if( status != PSA_SUCCESS )
683 goto exit;
684
Ronald Cron5425a212020-08-04 14:58:35 +0200685 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200686 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200687 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200688 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200689 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200690
Ronald Cron5425a212020-08-04 14:58:35 +0200691 PSA_ASSERT( psa_destroy_key( key ) );
692 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693
694exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100695 /*
696 * Key attributes may have been returned by psa_get_key_attributes()
697 * thus reset them as required.
698 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200699 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100700
701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200702 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703}
704/* END_CASE */
705
706/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200707void import_large_key( int type_arg, int byte_size_arg,
708 int expected_status_arg )
709{
710 psa_key_type_t type = type_arg;
711 size_t byte_size = byte_size_arg;
712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
713 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200715 psa_status_t status;
716 uint8_t *buffer = NULL;
717 size_t buffer_size = byte_size + 1;
718 size_t n;
719
Steven Cooreman69967ce2021-01-18 18:01:08 +0100720 /* Skip the test case if the target running the test cannot
721 * accomodate large keys due to heap size constraints */
722 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200723 memset( buffer, 'K', byte_size );
724
725 PSA_ASSERT( psa_crypto_init( ) );
726
727 /* Try importing the key */
728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
729 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200730 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100731 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200732 TEST_EQUAL( status, expected_status );
733
734 if( status == PSA_SUCCESS )
735 {
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200737 TEST_EQUAL( psa_get_key_type( &attributes ), type );
738 TEST_EQUAL( psa_get_key_bits( &attributes ),
739 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200740 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200742 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200743 for( n = 0; n < byte_size; n++ )
744 TEST_EQUAL( buffer[n], 'K' );
745 for( n = byte_size; n < buffer_size; n++ )
746 TEST_EQUAL( buffer[n], 0 );
747 }
748
749exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100750 /*
751 * Key attributes may have been returned by psa_get_key_attributes()
752 * thus reset them as required.
753 */
754 psa_reset_key_attributes( &attributes );
755
Ronald Cron5425a212020-08-04 14:58:35 +0200756 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200757 PSA_DONE( );
758 mbedtls_free( buffer );
759}
760/* END_CASE */
761
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100762/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
764{
Ronald Cron5425a212020-08-04 14:58:35 +0200765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200766 size_t bits = bits_arg;
767 psa_status_t expected_status = expected_status_arg;
768 psa_status_t status;
769 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200770 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200771 size_t buffer_size = /* Slight overapproximations */
772 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200773 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200774 unsigned char *p;
775 int ret;
776 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778
Gilles Peskine8817f612018-12-18 00:18:46 +0100779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200780 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200781
782 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
783 bits, keypair ) ) >= 0 );
784 length = ret;
785
786 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200787 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200788 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100789 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200790
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200791 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200792 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200793
794exit:
795 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200796 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200797}
798/* END_CASE */
799
800/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300801void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300802 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200803 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530804 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 int expected_bits,
806 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200807 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 int canonical_input )
809{
Ronald Cron5425a212020-08-04 14:58:35 +0200810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200812 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200813 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530815 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 unsigned char *exported = NULL;
817 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100819 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200822 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823
Moran Pekercb088e72018-07-17 17:36:59 +0300824 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200825 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200827 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829
Archana4d7ae1d2021-07-07 02:50:22 +0530830 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200831 psa_set_key_usage_flags( &attributes, usage_arg );
832 psa_set_key_algorithm( &attributes, alg );
833 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700834
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200836 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837
838 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200839 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200840 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
841 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200842 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843
844 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200845 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100846 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100847
848 /* The exported length must be set by psa_export_key() to a value between 0
849 * and export_size. On errors, the exported length must be 0. */
850 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
851 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
852 TEST_ASSERT( exported_length <= export_size );
853
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200854 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200855 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100856 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200857 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200860 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861
Gilles Peskineea38a922021-02-13 00:05:16 +0100862 /* Run sanity checks on the exported key. For non-canonical inputs,
863 * this validates the canonical representations. For canonical inputs,
864 * this doesn't directly validate the implementation, but it still helps
865 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530866 if( !psa_key_lifetime_is_external( lifetime ) )
867 {
868 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
869 goto exit;
870 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200873 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 else
875 {
Ronald Cron5425a212020-08-04 14:58:35 +0200876 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200877 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200878 &key2 ) );
879 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100880 reexported,
881 export_size,
882 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200883 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +0530884 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100887 TEST_ASSERT( exported_length <=
Archana4d7ae1d2021-07-07 02:50:22 +0530888 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +0530889 psa_get_key_bits( &got_attributes ) ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100890 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891
892destroy:
893 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200894 PSA_ASSERT( psa_destroy_key( key ) );
895 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896
897exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100898 /*
899 * Key attributes may have been returned by psa_get_key_attributes()
900 * thus reset them as required.
901 */
902 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +0530903 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300904 mbedtls_free( exported );
905 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200906 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907}
908/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100909
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300911void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200912 int type_arg,
913 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530914 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 int export_size_delta,
916 int expected_export_status_arg,
917 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918{
Ronald Cron5425a212020-08-04 14:58:35 +0200919 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300920 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200921 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200922 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300923 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530924 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300925 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100926 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100927 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929
Gilles Peskine8817f612018-12-18 00:18:46 +0100930 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300931
Archana4d7ae1d2021-07-07 02:50:22 +0530932 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
934 psa_set_key_algorithm( &attributes, alg );
935 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300936
937 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200938 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300939
Gilles Peskine49c25912018-10-29 15:15:31 +0100940 /* Export the public key */
941 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200942 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200943 exported, export_size,
944 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100946 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100947 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200948 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100949 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200950 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200951 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100952 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100953 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100954 TEST_ASSERT( expected_public_key->len <=
955 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
956 TEST_ASSERT( expected_public_key->len <=
957 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100958 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
959 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100960 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300961exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100962 /*
963 * Key attributes may have been returned by psa_get_key_attributes()
964 * thus reset them as required.
965 */
966 psa_reset_key_attributes( &attributes );
967
itayzafrir3e02b3b2018-06-12 17:06:52 +0300968 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200969 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200970 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300971}
972/* END_CASE */
973
Gilles Peskine20035e32018-02-03 22:44:14 +0100974/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975void import_and_exercise_key( data_t *data,
976 int type_arg,
977 int bits_arg,
978 int alg_arg )
979{
Ronald Cron5425a212020-08-04 14:58:35 +0200980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200981 psa_key_type_t type = type_arg;
982 size_t bits = bits_arg;
983 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100984 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200986 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200987
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
Gilles Peskine4747d192019-04-17 15:05:45 +0200990 psa_set_key_usage_flags( &attributes, usage );
991 psa_set_key_algorithm( &attributes, alg );
992 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993
994 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200995 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200996
997 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200998 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200999 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1000 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001001
1002 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001003 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001004 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001005
Ronald Cron5425a212020-08-04 14:58:35 +02001006 PSA_ASSERT( psa_destroy_key( key ) );
1007 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001008
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001009exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001010 /*
1011 * Key attributes may have been returned by psa_get_key_attributes()
1012 * thus reset them as required.
1013 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001014 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001015
1016 psa_reset_key_attributes( &attributes );
1017 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001018 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001019}
1020/* END_CASE */
1021
1022/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001023void effective_key_attributes( int type_arg, int expected_type_arg,
1024 int bits_arg, int expected_bits_arg,
1025 int usage_arg, int expected_usage_arg,
1026 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001027{
Ronald Cron5425a212020-08-04 14:58:35 +02001028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001029 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001030 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001031 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001032 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001033 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001034 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001035 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001036 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
Gilles Peskine8817f612018-12-18 00:18:46 +01001039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001040
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001041 psa_set_key_usage_flags( &attributes, usage );
1042 psa_set_key_algorithm( &attributes, alg );
1043 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001044 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001047 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048
Ronald Cron5425a212020-08-04 14:58:35 +02001049 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001050 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1051 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1052 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1053 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001054
1055exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001056 /*
1057 * Key attributes may have been returned by psa_get_key_attributes()
1058 * thus reset them as required.
1059 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001060 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001061
1062 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001063 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001064}
1065/* END_CASE */
1066
1067/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001068void check_key_policy( int type_arg, int bits_arg,
1069 int usage_arg, int alg_arg )
1070{
1071 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001072 usage_arg,
1073 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001074 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001075 goto exit;
1076}
1077/* END_CASE */
1078
1079/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001080void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001081{
1082 /* Test each valid way of initializing the object, except for `= {0}`, as
1083 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1084 * though it's OK by the C standard. We could test for this, but we'd need
1085 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001086 psa_key_attributes_t func = psa_key_attributes_init( );
1087 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1088 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001089
1090 memset( &zero, 0, sizeof( zero ) );
1091
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001092 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1093 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1094 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001096 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1097 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1098 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1099
1100 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1101 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1102 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1103
1104 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1105 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1106 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1107
1108 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1109 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1110 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001111}
1112/* END_CASE */
1113
1114/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001115void mac_key_policy( int policy_usage_arg,
1116 int policy_alg_arg,
1117 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001119 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001120 int expected_status_sign_arg,
1121 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001126 psa_key_type_t key_type = key_type_arg;
1127 psa_algorithm_t policy_alg = policy_alg_arg;
1128 psa_algorithm_t exercise_alg = exercise_alg_arg;
1129 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001130 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001131 psa_status_t expected_status_sign = expected_status_sign_arg;
1132 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001134
Gilles Peskine8817f612018-12-18 00:18:46 +01001135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001137 psa_set_key_usage_flags( &attributes, policy_usage );
1138 psa_set_key_algorithm( &attributes, policy_alg );
1139 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001140
Gilles Peskine049c7532019-05-15 20:22:09 +02001141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001142 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001143
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001144 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1145 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001146
Ronald Cron5425a212020-08-04 14:58:35 +02001147 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001148 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001149
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001150 /* Calculate the MAC, one-shot case. */
1151 uint8_t input[128] = {0};
1152 size_t mac_len;
1153 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1154 input, 128,
1155 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1156 expected_status_sign );
1157
Neil Armstrong3af9b972022-02-07 12:20:21 +01001158 /* Calculate the MAC, multi-part case. */
1159 PSA_ASSERT( psa_mac_abort( &operation ) );
1160 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1161 if( status == PSA_SUCCESS )
1162 {
1163 status = psa_mac_update( &operation, input, 128 );
1164 if( status == PSA_SUCCESS )
1165 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1166 &mac_len ),
1167 expected_status_sign );
1168 else
1169 TEST_EQUAL( status, expected_status_sign );
1170 }
1171 else
1172 {
1173 TEST_EQUAL( status, expected_status_sign );
1174 }
1175 PSA_ASSERT( psa_mac_abort( &operation ) );
1176
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001177 /* Verify correct MAC, one-shot case. */
1178 status = psa_mac_verify( key, exercise_alg, input, 128,
1179 mac, mac_len );
1180
1181 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1182 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001183 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001184 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001185
Neil Armstrong3af9b972022-02-07 12:20:21 +01001186 /* Verify correct MAC, multi-part case. */
1187 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1188 if( status == PSA_SUCCESS )
1189 {
1190 status = psa_mac_update( &operation, input, 128 );
1191 if( status == PSA_SUCCESS )
1192 {
1193 status = psa_mac_verify_finish( &operation, mac, mac_len );
1194 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1195 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1196 else
1197 TEST_EQUAL( status, expected_status_verify );
1198 }
1199 else
1200 {
1201 TEST_EQUAL( status, expected_status_verify );
1202 }
1203 }
1204 else
1205 {
1206 TEST_EQUAL( status, expected_status_verify );
1207 }
1208
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001210
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001212 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001213 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214
1215exit:
1216 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001218 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219}
1220/* END_CASE */
1221
1222/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001223void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001224 int policy_alg,
1225 int key_type,
1226 data_t *key_data,
1227 int exercise_alg )
1228{
Ronald Cron5425a212020-08-04 14:58:35 +02001229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001231 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001232 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001233 size_t output_buffer_size = 0;
1234 size_t input_buffer_size = 0;
1235 size_t output_length = 0;
1236 uint8_t *output = NULL;
1237 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001238 psa_status_t status;
1239
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001240 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1241 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1242 input_buffer_size );
1243
1244 ASSERT_ALLOC( input, input_buffer_size );
1245 ASSERT_ALLOC( output, output_buffer_size );
1246
Gilles Peskine8817f612018-12-18 00:18:46 +01001247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001248
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001249 psa_set_key_usage_flags( &attributes, policy_usage );
1250 psa_set_key_algorithm( &attributes, policy_alg );
1251 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252
Gilles Peskine049c7532019-05-15 20:22:09 +02001253 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001254 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001255
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001256 /* Check if no key usage flag implication is done */
1257 TEST_EQUAL( policy_usage,
1258 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001259
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001260 /* Encrypt check, one-shot */
1261 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1262 output, output_buffer_size,
1263 &output_length);
1264 if( policy_alg == exercise_alg &&
1265 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1266 PSA_ASSERT( status );
1267 else
1268 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1269
1270 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001271 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001272 if( policy_alg == exercise_alg &&
1273 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001274 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001275 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001276 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277 psa_cipher_abort( &operation );
1278
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001279 /* Decrypt check, one-shot */
1280 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1281 input, input_buffer_size,
1282 &output_length);
1283 if( policy_alg == exercise_alg &&
1284 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1285 PSA_ASSERT( status );
1286 else
1287 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1288
1289 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001290 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001291 if( policy_alg == exercise_alg &&
1292 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001293 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001294 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001295 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296
1297exit:
1298 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001299 mbedtls_free( input );
1300 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001301 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001302 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001303}
1304/* END_CASE */
1305
1306/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001307void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001308 int policy_alg,
1309 int key_type,
1310 data_t *key_data,
1311 int nonce_length_arg,
1312 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001313 int exercise_alg,
1314 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001315{
Ronald Cron5425a212020-08-04 14:58:35 +02001316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001318 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001319 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001321 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001322 unsigned char nonce[16] = {0};
1323 size_t nonce_length = nonce_length_arg;
1324 unsigned char tag[16];
1325 size_t tag_length = tag_length_arg;
1326 size_t output_length;
1327
1328 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1329 TEST_ASSERT( tag_length <= sizeof( tag ) );
1330
Gilles Peskine8817f612018-12-18 00:18:46 +01001331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001333 psa_set_key_usage_flags( &attributes, policy_usage );
1334 psa_set_key_algorithm( &attributes, policy_alg );
1335 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001336
Gilles Peskine049c7532019-05-15 20:22:09 +02001337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001338 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001340 /* Check if no key usage implication is done */
1341 TEST_EQUAL( policy_usage,
1342 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001343
Neil Armstrong752d8112022-02-07 14:51:11 +01001344 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02001345 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346 nonce, nonce_length,
1347 NULL, 0,
1348 NULL, 0,
1349 tag, tag_length,
1350 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001351 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1352 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001353 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001354 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001355
Neil Armstrong752d8112022-02-07 14:51:11 +01001356 /* Encrypt check, multi-part */
1357 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
1358 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1359 TEST_EQUAL( status, expected_status );
1360 else
1361 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1362
1363 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001365 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001366 nonce, nonce_length,
1367 NULL, 0,
1368 tag, tag_length,
1369 NULL, 0,
1370 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001371 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1372 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1373 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001374 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001375 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001376 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001377
Neil Armstrong752d8112022-02-07 14:51:11 +01001378 /* Decrypt check, multi-part */
1379 PSA_ASSERT( psa_aead_abort( &operation ) );
1380 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
1381 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1382 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1383 else
1384 TEST_EQUAL( status, expected_status );
1385
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001386exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01001387 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001388 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001389 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001390}
1391/* END_CASE */
1392
1393/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001394void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001395 int policy_alg,
1396 int key_type,
1397 data_t *key_data,
1398 int exercise_alg )
1399{
Ronald Cron5425a212020-08-04 14:58:35 +02001400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001402 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001403 psa_status_t status;
1404 size_t key_bits;
1405 size_t buffer_length;
1406 unsigned char *buffer = NULL;
1407 size_t output_length;
1408
Gilles Peskine8817f612018-12-18 00:18:46 +01001409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001410
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001411 psa_set_key_usage_flags( &attributes, policy_usage );
1412 psa_set_key_algorithm( &attributes, policy_alg );
1413 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001414
Gilles Peskine049c7532019-05-15 20:22:09 +02001415 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001416 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001417
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001418 /* Check if no key usage implication is done */
1419 TEST_EQUAL( policy_usage,
1420 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001421
Ronald Cron5425a212020-08-04 14:58:35 +02001422 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001423 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001424 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1425 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001426 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001427
Ronald Cron5425a212020-08-04 14:58:35 +02001428 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001429 NULL, 0,
1430 NULL, 0,
1431 buffer, buffer_length,
1432 &output_length );
1433 if( policy_alg == exercise_alg &&
1434 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001435 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001436 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001437 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001438
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001439 if( buffer_length != 0 )
1440 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001441 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001442 buffer, buffer_length,
1443 NULL, 0,
1444 buffer, buffer_length,
1445 &output_length );
1446 if( policy_alg == exercise_alg &&
1447 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001448 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001449 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001450 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001451
1452exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001453 /*
1454 * Key attributes may have been returned by psa_get_key_attributes()
1455 * thus reset them as required.
1456 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001457 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001458
1459 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001460 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001461 mbedtls_free( buffer );
1462}
1463/* END_CASE */
1464
1465/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001466void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 int policy_alg,
1468 int key_type,
1469 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001470 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001471 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001472 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001473{
Ronald Cron5425a212020-08-04 14:58:35 +02001474 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001475 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001476 psa_key_usage_t policy_usage = policy_usage_arg;
1477 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001478 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001479 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1480 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1481 * compatible with the policy and `payload_length_arg` is supposed to be
1482 * a valid input length to sign. If `payload_length_arg <= 0`,
1483 * `exercise_alg` is supposed to be forbidden by the policy. */
1484 int compatible_alg = payload_length_arg > 0;
1485 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001486 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001487 size_t signature_length;
1488
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001489 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001490 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001491 TEST_EQUAL( expected_usage,
1492 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001493
Gilles Peskine8817f612018-12-18 00:18:46 +01001494 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001495
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001496 psa_set_key_usage_flags( &attributes, policy_usage );
1497 psa_set_key_algorithm( &attributes, policy_alg );
1498 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001499
Gilles Peskine049c7532019-05-15 20:22:09 +02001500 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001501 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001502
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001503 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1504
Ronald Cron5425a212020-08-04 14:58:35 +02001505 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001506 payload, payload_length,
1507 signature, sizeof( signature ),
1508 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001509 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001510 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001511 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001512 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001513
1514 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001515 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001516 payload, payload_length,
1517 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001518 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001519 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001520 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001521 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001522
Gilles Peskinef7b41372021-09-22 16:15:05 +02001523 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001524 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001525 {
1526 status = psa_sign_message( key, exercise_alg,
1527 payload, payload_length,
1528 signature, sizeof( signature ),
1529 &signature_length );
1530 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1531 PSA_ASSERT( status );
1532 else
1533 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1534
1535 memset( signature, 0, sizeof( signature ) );
1536 status = psa_verify_message( key, exercise_alg,
1537 payload, payload_length,
1538 signature, sizeof( signature ) );
1539 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1540 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1541 else
1542 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1543 }
1544
Gilles Peskined5b33222018-06-18 22:20:03 +02001545exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001546 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001547 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001548}
1549/* END_CASE */
1550
Janos Follathba3fab92019-06-11 14:50:16 +01001551/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001552void derive_key_policy( int policy_usage,
1553 int policy_alg,
1554 int key_type,
1555 data_t *key_data,
1556 int exercise_alg )
1557{
Ronald Cron5425a212020-08-04 14:58:35 +02001558 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001559 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001560 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001561 psa_status_t status;
1562
Gilles Peskine8817f612018-12-18 00:18:46 +01001563 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001564
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001565 psa_set_key_usage_flags( &attributes, policy_usage );
1566 psa_set_key_algorithm( &attributes, policy_alg );
1567 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001568
Gilles Peskine049c7532019-05-15 20:22:09 +02001569 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001570 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001571
Janos Follathba3fab92019-06-11 14:50:16 +01001572 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1573
1574 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1575 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001576 {
Janos Follathba3fab92019-06-11 14:50:16 +01001577 PSA_ASSERT( psa_key_derivation_input_bytes(
1578 &operation,
1579 PSA_KEY_DERIVATION_INPUT_SEED,
1580 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001581 }
Janos Follathba3fab92019-06-11 14:50:16 +01001582
1583 status = psa_key_derivation_input_key( &operation,
1584 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001585 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001586
Gilles Peskineea0fb492018-07-12 17:17:20 +02001587 if( policy_alg == exercise_alg &&
1588 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001589 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001590 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001591 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001592
1593exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001594 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001595 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001596 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001597}
1598/* END_CASE */
1599
1600/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001601void agreement_key_policy( int policy_usage,
1602 int policy_alg,
1603 int key_type_arg,
1604 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001605 int exercise_alg,
1606 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001607{
Ronald Cron5425a212020-08-04 14:58:35 +02001608 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001610 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001611 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001612 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001613 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001614
Gilles Peskine8817f612018-12-18 00:18:46 +01001615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001616
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001617 psa_set_key_usage_flags( &attributes, policy_usage );
1618 psa_set_key_algorithm( &attributes, policy_alg );
1619 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001620
Gilles Peskine049c7532019-05-15 20:22:09 +02001621 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001622 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001623
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001624 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001625 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001626
Steven Cooremance48e852020-10-05 16:02:45 +02001627 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001628
1629exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001630 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001631 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001632 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001633}
1634/* END_CASE */
1635
1636/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001637void key_policy_alg2( int key_type_arg, data_t *key_data,
1638 int usage_arg, int alg_arg, int alg2_arg )
1639{
Ronald Cron5425a212020-08-04 14:58:35 +02001640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001641 psa_key_type_t key_type = key_type_arg;
1642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1643 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1644 psa_key_usage_t usage = usage_arg;
1645 psa_algorithm_t alg = alg_arg;
1646 psa_algorithm_t alg2 = alg2_arg;
1647
1648 PSA_ASSERT( psa_crypto_init( ) );
1649
1650 psa_set_key_usage_flags( &attributes, usage );
1651 psa_set_key_algorithm( &attributes, alg );
1652 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1653 psa_set_key_type( &attributes, key_type );
1654 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001655 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001656
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001657 /* Update the usage flags to obtain implicit usage flags */
1658 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001659 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001660 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1661 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1662 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1663
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001664 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001665 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001666 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001667 goto exit;
1668
1669exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001670 /*
1671 * Key attributes may have been returned by psa_get_key_attributes()
1672 * thus reset them as required.
1673 */
1674 psa_reset_key_attributes( &got_attributes );
1675
Ronald Cron5425a212020-08-04 14:58:35 +02001676 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001677 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001678}
1679/* END_CASE */
1680
1681/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001682void raw_agreement_key_policy( int policy_usage,
1683 int policy_alg,
1684 int key_type_arg,
1685 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001686 int exercise_alg,
1687 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001688{
Ronald Cron5425a212020-08-04 14:58:35 +02001689 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001690 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001691 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001692 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001693 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001694 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001695
1696 PSA_ASSERT( psa_crypto_init( ) );
1697
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001698 psa_set_key_usage_flags( &attributes, policy_usage );
1699 psa_set_key_algorithm( &attributes, policy_alg );
1700 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001701
Gilles Peskine049c7532019-05-15 20:22:09 +02001702 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001703 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001704
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001705 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001706
Steven Cooremance48e852020-10-05 16:02:45 +02001707 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001708
1709exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001710 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001711 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001712 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001713}
1714/* END_CASE */
1715
1716/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001717void copy_success( int source_usage_arg,
1718 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301719 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001720 int type_arg, data_t *material,
1721 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001722 int target_usage_arg,
1723 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301724 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001725 int expected_usage_arg,
1726 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001727{
Gilles Peskineca25db92019-04-19 11:43:08 +02001728 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1729 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001730 psa_key_usage_t expected_usage = expected_usage_arg;
1731 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001732 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301733 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1734 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001735 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1736 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001737 uint8_t *export_buffer = NULL;
1738
Gilles Peskine57ab7212019-01-28 13:03:09 +01001739 PSA_ASSERT( psa_crypto_init( ) );
1740
Gilles Peskineca25db92019-04-19 11:43:08 +02001741 /* Prepare the source key. */
1742 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1743 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001744 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001745 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301746 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001747 PSA_ASSERT( psa_import_key( &source_attributes,
1748 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001749 &source_key ) );
1750 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001751
Gilles Peskineca25db92019-04-19 11:43:08 +02001752 /* Prepare the target attributes. */
1753 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001754 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001755 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001756 }
Archana8a180362021-07-05 02:18:48 +05301757 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001758
Gilles Peskineca25db92019-04-19 11:43:08 +02001759 if( target_usage_arg != -1 )
1760 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1761 if( target_alg_arg != -1 )
1762 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001763 if( target_alg2_arg != -1 )
1764 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001765
Archana8a180362021-07-05 02:18:48 +05301766
Gilles Peskine57ab7212019-01-28 13:03:09 +01001767 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001768 PSA_ASSERT( psa_copy_key( source_key,
1769 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001770
1771 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001772 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001773
1774 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001775 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001776 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1777 psa_get_key_type( &target_attributes ) );
1778 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1779 psa_get_key_bits( &target_attributes ) );
1780 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1781 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001782 TEST_EQUAL( expected_alg2,
1783 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001784 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1785 {
1786 size_t length;
1787 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001788 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001789 material->len, &length ) );
1790 ASSERT_COMPARE( material->x, material->len,
1791 export_buffer, length );
1792 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001793
Archana8a180362021-07-05 02:18:48 +05301794 if( !psa_key_lifetime_is_external( target_lifetime ) )
1795 {
1796 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1797 goto exit;
1798 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1799 goto exit;
1800 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001801
Ronald Cron5425a212020-08-04 14:58:35 +02001802 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001803
1804exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001805 /*
1806 * Source and target key attributes may have been returned by
1807 * psa_get_key_attributes() thus reset them as required.
1808 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001809 psa_reset_key_attributes( &source_attributes );
1810 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001811
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001812 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001813 mbedtls_free( export_buffer );
1814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001818void copy_fail( int source_usage_arg,
1819 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301820 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001821 int type_arg, data_t *material,
1822 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001823 int target_usage_arg,
1824 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001825 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001826 int expected_status_arg )
1827{
1828 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1829 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001830 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1831 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001832 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001833
1834 PSA_ASSERT( psa_crypto_init( ) );
1835
1836 /* Prepare the source key. */
1837 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1838 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001839 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001840 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301841 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001842 PSA_ASSERT( psa_import_key( &source_attributes,
1843 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001844 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001845
1846 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001847 psa_set_key_id( &target_attributes, key_id );
1848 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001849 psa_set_key_type( &target_attributes, target_type_arg );
1850 psa_set_key_bits( &target_attributes, target_bits_arg );
1851 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1852 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001853 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001854
1855 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001856 TEST_EQUAL( psa_copy_key( source_key,
1857 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001858 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001859
Ronald Cron5425a212020-08-04 14:58:35 +02001860 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001861
Gilles Peskine4a644642019-05-03 17:14:08 +02001862exit:
1863 psa_reset_key_attributes( &source_attributes );
1864 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001865 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001866}
1867/* END_CASE */
1868
1869/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001870void hash_operation_init( )
1871{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001872 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001873 /* Test each valid way of initializing the object, except for `= {0}`, as
1874 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1875 * though it's OK by the C standard. We could test for this, but we'd need
1876 * to supress the Clang warning for the test. */
1877 psa_hash_operation_t func = psa_hash_operation_init( );
1878 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1879 psa_hash_operation_t zero;
1880
1881 memset( &zero, 0, sizeof( zero ) );
1882
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001883 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001884 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1885 PSA_ERROR_BAD_STATE );
1886 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1887 PSA_ERROR_BAD_STATE );
1888 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1889 PSA_ERROR_BAD_STATE );
1890
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001891 /* A default hash operation should be abortable without error. */
1892 PSA_ASSERT( psa_hash_abort( &func ) );
1893 PSA_ASSERT( psa_hash_abort( &init ) );
1894 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001895}
1896/* END_CASE */
1897
1898/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001899void hash_setup( int alg_arg,
1900 int expected_status_arg )
1901{
1902 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01001903 uint8_t *output = NULL;
1904 size_t output_size = 0;
1905 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001906 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001907 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001908 psa_status_t status;
1909
Gilles Peskine8817f612018-12-18 00:18:46 +01001910 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001911
Neil Armstrongedb20862022-02-07 15:47:44 +01001912 /* Hash Setup, one-shot */
1913 output_size = PSA_HASH_LENGTH(alg);
1914 ASSERT_ALLOC( output, output_size );
1915
1916 status = psa_hash_compute( alg, NULL, 0,
1917 output, output_size, &output_length );
1918 TEST_EQUAL( status, expected_status );
1919
1920 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001921 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001922 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001923
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001924 /* Whether setup succeeded or failed, abort must succeed. */
1925 PSA_ASSERT( psa_hash_abort( &operation ) );
1926
1927 /* If setup failed, reproduce the failure, so as to
1928 * test the resulting state of the operation object. */
1929 if( status != PSA_SUCCESS )
1930 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1931
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001932 /* Now the operation object should be reusable. */
1933#if defined(KNOWN_SUPPORTED_HASH_ALG)
1934 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1935 PSA_ASSERT( psa_hash_abort( &operation ) );
1936#endif
1937
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001938exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01001939 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001940 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001941}
1942/* END_CASE */
1943
1944/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001945void hash_compute_fail( int alg_arg, data_t *input,
1946 int output_size_arg, int expected_status_arg )
1947{
1948 psa_algorithm_t alg = alg_arg;
1949 uint8_t *output = NULL;
1950 size_t output_size = output_size_arg;
1951 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001952 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01001953 psa_status_t expected_status = expected_status_arg;
1954 psa_status_t status;
1955
1956 ASSERT_ALLOC( output, output_size );
1957
1958 PSA_ASSERT( psa_crypto_init( ) );
1959
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001960 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001961 status = psa_hash_compute( alg, input->x, input->len,
1962 output, output_size, &output_length );
1963 TEST_EQUAL( status, expected_status );
1964 TEST_ASSERT( output_length <= output_size );
1965
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001966 /* Hash Compute, multi-part */
1967 status = psa_hash_setup( &operation, alg );
1968 if( status == PSA_SUCCESS )
1969 {
1970 status = psa_hash_update( &operation, input->x, input->len );
1971 if( status == PSA_SUCCESS )
1972 {
1973 status = psa_hash_finish( &operation, output, output_size,
1974 &output_length );
1975 if( status == PSA_SUCCESS )
1976 TEST_ASSERT( output_length <= output_size );
1977 else
1978 TEST_EQUAL( status, expected_status );
1979 }
1980 else
1981 {
1982 TEST_EQUAL( status, expected_status );
1983 }
1984 }
1985 else
1986 {
1987 TEST_EQUAL( status, expected_status );
1988 }
1989
Gilles Peskine0a749c82019-11-28 19:33:58 +01001990exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01001991 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001992 mbedtls_free( output );
1993 PSA_DONE( );
1994}
1995/* END_CASE */
1996
1997/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001998void hash_compare_fail( int alg_arg, data_t *input,
1999 data_t *reference_hash,
2000 int expected_status_arg )
2001{
2002 psa_algorithm_t alg = alg_arg;
2003 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002004 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002005 psa_status_t status;
2006
2007 PSA_ASSERT( psa_crypto_init( ) );
2008
Neil Armstrong55a1be12022-02-07 11:23:20 +01002009 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002010 status = psa_hash_compare( alg, input->x, input->len,
2011 reference_hash->x, reference_hash->len );
2012 TEST_EQUAL( status, expected_status );
2013
Neil Armstrong55a1be12022-02-07 11:23:20 +01002014 /* Hash Compare, multi-part */
2015 status = psa_hash_setup( &operation, alg );
2016 if( status == PSA_SUCCESS )
2017 {
2018 status = psa_hash_update( &operation, input->x, input->len );
2019 if( status == PSA_SUCCESS )
2020 {
2021 status = psa_hash_verify( &operation, reference_hash->x,
2022 reference_hash->len );
2023 TEST_EQUAL( status, expected_status );
2024 }
2025 else
2026 {
2027 TEST_EQUAL( status, expected_status );
2028 }
2029 }
2030 else
2031 {
2032 TEST_EQUAL( status, expected_status );
2033 }
2034
Gilles Peskine88e08462020-01-28 20:43:00 +01002035exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002036 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002037 PSA_DONE( );
2038}
2039/* END_CASE */
2040
2041/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002042void hash_compute_compare( int alg_arg, data_t *input,
2043 data_t *expected_output )
2044{
2045 psa_algorithm_t alg = alg_arg;
2046 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2047 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002048 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002049 size_t i;
2050
2051 PSA_ASSERT( psa_crypto_init( ) );
2052
Neil Armstrongca30a002022-02-07 11:40:23 +01002053 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002054 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002055 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002056 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002057 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002058 ASSERT_COMPARE( output, output_length,
2059 expected_output->x, expected_output->len );
2060
Neil Armstrongca30a002022-02-07 11:40:23 +01002061 /* Compute with tight buffer, multi-part */
2062 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2063 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2064 PSA_ASSERT( psa_hash_finish( &operation, output,
2065 PSA_HASH_LENGTH( alg ),
2066 &output_length ) );
2067 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2068 ASSERT_COMPARE( output, output_length,
2069 expected_output->x, expected_output->len );
2070
2071 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002072 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2073 output, sizeof( output ),
2074 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002075 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002076 ASSERT_COMPARE( output, output_length,
2077 expected_output->x, expected_output->len );
2078
Neil Armstrongca30a002022-02-07 11:40:23 +01002079 /* Compute with larger buffer, multi-part */
2080 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2081 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2082 PSA_ASSERT( psa_hash_finish( &operation, output,
2083 sizeof( output ), &output_length ) );
2084 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2085 ASSERT_COMPARE( output, output_length,
2086 expected_output->x, expected_output->len );
2087
2088 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002089 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2090 output, output_length ) );
2091
Neil Armstrongca30a002022-02-07 11:40:23 +01002092 /* Compare with correct hash, multi-part */
2093 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2094 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2095 PSA_ASSERT( psa_hash_verify( &operation, output,
2096 output_length ) );
2097
2098 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002099 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2100 output, output_length + 1 ),
2101 PSA_ERROR_INVALID_SIGNATURE );
2102
Neil Armstrongca30a002022-02-07 11:40:23 +01002103 /* Compare with trailing garbage, multi-part */
2104 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2105 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2106 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2107 PSA_ERROR_INVALID_SIGNATURE );
2108
2109 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002110 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2111 output, output_length - 1 ),
2112 PSA_ERROR_INVALID_SIGNATURE );
2113
Neil Armstrongca30a002022-02-07 11:40:23 +01002114 /* Compare with truncated hash, multi-part */
2115 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2116 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2117 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2118 PSA_ERROR_INVALID_SIGNATURE );
2119
Gilles Peskine0a749c82019-11-28 19:33:58 +01002120 /* Compare with corrupted value */
2121 for( i = 0; i < output_length; i++ )
2122 {
Chris Jones9634bb12021-01-20 15:56:42 +00002123 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002124 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002125
2126 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002127 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2128 output, output_length ),
2129 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002130
2131 /* Multi-Part */
2132 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2133 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2134 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2135 PSA_ERROR_INVALID_SIGNATURE );
2136
Gilles Peskine0a749c82019-11-28 19:33:58 +01002137 output[i] ^= 1;
2138 }
2139
2140exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002141 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002142 PSA_DONE( );
2143}
2144/* END_CASE */
2145
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002146/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002147void hash_bad_order( )
2148{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002149 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002150 unsigned char input[] = "";
2151 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002152 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002153 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2154 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2155 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002156 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002157 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002158 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002159
Gilles Peskine8817f612018-12-18 00:18:46 +01002160 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002161
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002162 /* Call setup twice in a row. */
2163 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002164 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002165 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2166 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002167 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002168 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002169 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002170
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002171 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002172 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002173 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002174 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002175
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002176 /* Check that update calls abort on error. */
2177 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002178 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002179 ASSERT_OPERATION_IS_ACTIVE( operation );
2180 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2181 PSA_ERROR_BAD_STATE );
2182 ASSERT_OPERATION_IS_INACTIVE( operation );
2183 PSA_ASSERT( psa_hash_abort( &operation ) );
2184 ASSERT_OPERATION_IS_INACTIVE( operation );
2185
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002186 /* Call update after finish. */
2187 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2188 PSA_ASSERT( psa_hash_finish( &operation,
2189 hash, sizeof( hash ), &hash_len ) );
2190 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002191 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002192 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002193
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002194 /* Call verify without calling setup beforehand. */
2195 TEST_EQUAL( psa_hash_verify( &operation,
2196 valid_hash, sizeof( valid_hash ) ),
2197 PSA_ERROR_BAD_STATE );
2198 PSA_ASSERT( psa_hash_abort( &operation ) );
2199
2200 /* Call verify after finish. */
2201 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2202 PSA_ASSERT( psa_hash_finish( &operation,
2203 hash, sizeof( hash ), &hash_len ) );
2204 TEST_EQUAL( psa_hash_verify( &operation,
2205 valid_hash, sizeof( valid_hash ) ),
2206 PSA_ERROR_BAD_STATE );
2207 PSA_ASSERT( psa_hash_abort( &operation ) );
2208
2209 /* Call verify twice in a row. */
2210 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002211 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002212 PSA_ASSERT( psa_hash_verify( &operation,
2213 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002214 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002215 TEST_EQUAL( psa_hash_verify( &operation,
2216 valid_hash, sizeof( valid_hash ) ),
2217 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002218 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002219 PSA_ASSERT( psa_hash_abort( &operation ) );
2220
2221 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002222 TEST_EQUAL( psa_hash_finish( &operation,
2223 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002224 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002225 PSA_ASSERT( psa_hash_abort( &operation ) );
2226
2227 /* Call finish twice in a row. */
2228 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2229 PSA_ASSERT( psa_hash_finish( &operation,
2230 hash, sizeof( hash ), &hash_len ) );
2231 TEST_EQUAL( psa_hash_finish( &operation,
2232 hash, sizeof( hash ), &hash_len ),
2233 PSA_ERROR_BAD_STATE );
2234 PSA_ASSERT( psa_hash_abort( &operation ) );
2235
2236 /* Call finish after calling verify. */
2237 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2238 PSA_ASSERT( psa_hash_verify( &operation,
2239 valid_hash, sizeof( valid_hash ) ) );
2240 TEST_EQUAL( psa_hash_finish( &operation,
2241 hash, sizeof( hash ), &hash_len ),
2242 PSA_ERROR_BAD_STATE );
2243 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002244
2245exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002246 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002247}
2248/* END_CASE */
2249
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002250/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002251void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002252{
2253 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002254 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2255 * appended to it */
2256 unsigned char hash[] = {
2257 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2258 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2259 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002260 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002261 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002262
Gilles Peskine8817f612018-12-18 00:18:46 +01002263 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002264
itayzafrir27e69452018-11-01 14:26:34 +02002265 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002266 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002267 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002268 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002269 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002270 ASSERT_OPERATION_IS_INACTIVE( operation );
2271 PSA_ASSERT( psa_hash_abort( &operation ) );
2272 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002273
itayzafrir27e69452018-11-01 14:26:34 +02002274 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002275 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002276 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002277 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002278
itayzafrir27e69452018-11-01 14:26:34 +02002279 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002280 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002281 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002282 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002283
itayzafrirec93d302018-10-18 18:01:10 +03002284exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002285 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002286}
2287/* END_CASE */
2288
Ronald Cronee414c72021-03-18 18:50:08 +01002289/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002290void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002291{
2292 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002293 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002294 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002295 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002296 size_t hash_len;
2297
Gilles Peskine8817f612018-12-18 00:18:46 +01002298 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002299
itayzafrir58028322018-10-25 10:22:01 +03002300 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002301 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002302 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002303 hash, expected_size - 1, &hash_len ),
2304 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002305
2306exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002307 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002308}
2309/* END_CASE */
2310
Ronald Cronee414c72021-03-18 18:50:08 +01002311/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002312void hash_clone_source_state( )
2313{
2314 psa_algorithm_t alg = PSA_ALG_SHA_256;
2315 unsigned char hash[PSA_HASH_MAX_SIZE];
2316 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2317 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2318 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2319 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2320 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2321 size_t hash_len;
2322
2323 PSA_ASSERT( psa_crypto_init( ) );
2324 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2325
2326 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2327 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2328 PSA_ASSERT( psa_hash_finish( &op_finished,
2329 hash, sizeof( hash ), &hash_len ) );
2330 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2331 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2332
2333 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2334 PSA_ERROR_BAD_STATE );
2335
2336 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2337 PSA_ASSERT( psa_hash_finish( &op_init,
2338 hash, sizeof( hash ), &hash_len ) );
2339 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2340 PSA_ASSERT( psa_hash_finish( &op_finished,
2341 hash, sizeof( hash ), &hash_len ) );
2342 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2343 PSA_ASSERT( psa_hash_finish( &op_aborted,
2344 hash, sizeof( hash ), &hash_len ) );
2345
2346exit:
2347 psa_hash_abort( &op_source );
2348 psa_hash_abort( &op_init );
2349 psa_hash_abort( &op_setup );
2350 psa_hash_abort( &op_finished );
2351 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002352 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002353}
2354/* END_CASE */
2355
Ronald Cronee414c72021-03-18 18:50:08 +01002356/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002357void hash_clone_target_state( )
2358{
2359 psa_algorithm_t alg = PSA_ALG_SHA_256;
2360 unsigned char hash[PSA_HASH_MAX_SIZE];
2361 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2362 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2363 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2364 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2365 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2366 size_t hash_len;
2367
2368 PSA_ASSERT( psa_crypto_init( ) );
2369
2370 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2371 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2372 PSA_ASSERT( psa_hash_finish( &op_finished,
2373 hash, sizeof( hash ), &hash_len ) );
2374 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2375 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2376
2377 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2378 PSA_ASSERT( psa_hash_finish( &op_target,
2379 hash, sizeof( hash ), &hash_len ) );
2380
2381 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2382 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2383 PSA_ERROR_BAD_STATE );
2384 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2385 PSA_ERROR_BAD_STATE );
2386
2387exit:
2388 psa_hash_abort( &op_target );
2389 psa_hash_abort( &op_init );
2390 psa_hash_abort( &op_setup );
2391 psa_hash_abort( &op_finished );
2392 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002393 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002394}
2395/* END_CASE */
2396
itayzafrir58028322018-10-25 10:22:01 +03002397/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002398void mac_operation_init( )
2399{
Jaeden Amero252ef282019-02-15 14:05:35 +00002400 const uint8_t input[1] = { 0 };
2401
Jaeden Amero769ce272019-01-04 11:48:03 +00002402 /* Test each valid way of initializing the object, except for `= {0}`, as
2403 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2404 * though it's OK by the C standard. We could test for this, but we'd need
2405 * to supress the Clang warning for the test. */
2406 psa_mac_operation_t func = psa_mac_operation_init( );
2407 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2408 psa_mac_operation_t zero;
2409
2410 memset( &zero, 0, sizeof( zero ) );
2411
Jaeden Amero252ef282019-02-15 14:05:35 +00002412 /* A freshly-initialized MAC operation should not be usable. */
2413 TEST_EQUAL( psa_mac_update( &func,
2414 input, sizeof( input ) ),
2415 PSA_ERROR_BAD_STATE );
2416 TEST_EQUAL( psa_mac_update( &init,
2417 input, sizeof( input ) ),
2418 PSA_ERROR_BAD_STATE );
2419 TEST_EQUAL( psa_mac_update( &zero,
2420 input, sizeof( input ) ),
2421 PSA_ERROR_BAD_STATE );
2422
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002423 /* A default MAC operation should be abortable without error. */
2424 PSA_ASSERT( psa_mac_abort( &func ) );
2425 PSA_ASSERT( psa_mac_abort( &init ) );
2426 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002427}
2428/* END_CASE */
2429
2430/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002431void mac_setup( int key_type_arg,
2432 data_t *key,
2433 int alg_arg,
2434 int expected_status_arg )
2435{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002436 psa_key_type_t key_type = key_type_arg;
2437 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002438 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002439 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002440 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2441#if defined(KNOWN_SUPPORTED_MAC_ALG)
2442 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2443#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444
Gilles Peskine8817f612018-12-18 00:18:46 +01002445 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002446
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002447 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2448 &operation, &status ) )
2449 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002450 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002451
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002452 /* The operation object should be reusable. */
2453#if defined(KNOWN_SUPPORTED_MAC_ALG)
2454 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2455 smoke_test_key_data,
2456 sizeof( smoke_test_key_data ),
2457 KNOWN_SUPPORTED_MAC_ALG,
2458 &operation, &status ) )
2459 goto exit;
2460 TEST_EQUAL( status, PSA_SUCCESS );
2461#endif
2462
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002463exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002464 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002465}
2466/* END_CASE */
2467
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002468/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00002469void mac_bad_order( )
2470{
Ronald Cron5425a212020-08-04 14:58:35 +02002471 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002472 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2473 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002474 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002475 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2476 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2477 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002479 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2480 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2481 size_t sign_mac_length = 0;
2482 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2483 const uint8_t verify_mac[] = {
2484 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2485 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2486 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2487
2488 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002489 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002490 psa_set_key_algorithm( &attributes, alg );
2491 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002492
Ronald Cron5425a212020-08-04 14:58:35 +02002493 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2494 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002495
Jaeden Amero252ef282019-02-15 14:05:35 +00002496 /* Call update without calling setup beforehand. */
2497 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2498 PSA_ERROR_BAD_STATE );
2499 PSA_ASSERT( psa_mac_abort( &operation ) );
2500
2501 /* Call sign finish without calling setup beforehand. */
2502 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2503 &sign_mac_length),
2504 PSA_ERROR_BAD_STATE );
2505 PSA_ASSERT( psa_mac_abort( &operation ) );
2506
2507 /* Call verify finish without calling setup beforehand. */
2508 TEST_EQUAL( psa_mac_verify_finish( &operation,
2509 verify_mac, sizeof( verify_mac ) ),
2510 PSA_ERROR_BAD_STATE );
2511 PSA_ASSERT( psa_mac_abort( &operation ) );
2512
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002513 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002514 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002515 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002516 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002517 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002518 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002519 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002520 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002521
Jaeden Amero252ef282019-02-15 14:05:35 +00002522 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002523 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002524 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2525 PSA_ASSERT( psa_mac_sign_finish( &operation,
2526 sign_mac, sizeof( sign_mac ),
2527 &sign_mac_length ) );
2528 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2529 PSA_ERROR_BAD_STATE );
2530 PSA_ASSERT( psa_mac_abort( &operation ) );
2531
2532 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002533 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002534 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2535 PSA_ASSERT( psa_mac_verify_finish( &operation,
2536 verify_mac, sizeof( verify_mac ) ) );
2537 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2538 PSA_ERROR_BAD_STATE );
2539 PSA_ASSERT( psa_mac_abort( &operation ) );
2540
2541 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002542 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002543 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2544 PSA_ASSERT( psa_mac_sign_finish( &operation,
2545 sign_mac, sizeof( sign_mac ),
2546 &sign_mac_length ) );
2547 TEST_EQUAL( psa_mac_sign_finish( &operation,
2548 sign_mac, sizeof( sign_mac ),
2549 &sign_mac_length ),
2550 PSA_ERROR_BAD_STATE );
2551 PSA_ASSERT( psa_mac_abort( &operation ) );
2552
2553 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002554 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002555 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2556 PSA_ASSERT( psa_mac_verify_finish( &operation,
2557 verify_mac, sizeof( verify_mac ) ) );
2558 TEST_EQUAL( psa_mac_verify_finish( &operation,
2559 verify_mac, sizeof( verify_mac ) ),
2560 PSA_ERROR_BAD_STATE );
2561 PSA_ASSERT( psa_mac_abort( &operation ) );
2562
2563 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002564 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002565 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002566 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002567 TEST_EQUAL( psa_mac_verify_finish( &operation,
2568 verify_mac, sizeof( verify_mac ) ),
2569 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002570 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002571 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002572 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002573
2574 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002575 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002576 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002577 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002578 TEST_EQUAL( psa_mac_sign_finish( &operation,
2579 sign_mac, sizeof( sign_mac ),
2580 &sign_mac_length ),
2581 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002582 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002583 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002584 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002585
Ronald Cron5425a212020-08-04 14:58:35 +02002586 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002587
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002588exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002589 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002590}
2591/* END_CASE */
2592
2593/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002594void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002595 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002596 int alg_arg,
2597 data_t *input,
2598 data_t *expected_mac )
2599{
Ronald Cron5425a212020-08-04 14:58:35 +02002600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002601 psa_key_type_t key_type = key_type_arg;
2602 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002603 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002605 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002606 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002607 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002608 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002609 const size_t output_sizes_to_test[] = {
2610 0,
2611 1,
2612 expected_mac->len - 1,
2613 expected_mac->len,
2614 expected_mac->len + 1,
2615 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002616
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002617 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002618 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002619 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002620
Gilles Peskine8817f612018-12-18 00:18:46 +01002621 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002622
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002623 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002624 psa_set_key_algorithm( &attributes, alg );
2625 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002626
Ronald Cron5425a212020-08-04 14:58:35 +02002627 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2628 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002629
Gilles Peskine8b356b52020-08-25 23:44:59 +02002630 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2631 {
2632 const size_t output_size = output_sizes_to_test[i];
2633 psa_status_t expected_status =
2634 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2635 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002636
Chris Jones9634bb12021-01-20 15:56:42 +00002637 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002638 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002639
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002640 /* Calculate the MAC, one-shot case. */
2641 TEST_EQUAL( psa_mac_compute( key, alg,
2642 input->x, input->len,
2643 actual_mac, output_size, &mac_length ),
2644 expected_status );
2645 if( expected_status == PSA_SUCCESS )
2646 {
2647 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2648 actual_mac, mac_length );
2649 }
2650
2651 if( output_size > 0 )
2652 memset( actual_mac, 0, output_size );
2653
2654 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002655 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002656 PSA_ASSERT( psa_mac_update( &operation,
2657 input->x, input->len ) );
2658 TEST_EQUAL( psa_mac_sign_finish( &operation,
2659 actual_mac, output_size,
2660 &mac_length ),
2661 expected_status );
2662 PSA_ASSERT( psa_mac_abort( &operation ) );
2663
2664 if( expected_status == PSA_SUCCESS )
2665 {
2666 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2667 actual_mac, mac_length );
2668 }
2669 mbedtls_free( actual_mac );
2670 actual_mac = NULL;
2671 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002672
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002673exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002674 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002675 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002676 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002677 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002678}
2679/* END_CASE */
2680
2681/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002682void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002683 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002684 int alg_arg,
2685 data_t *input,
2686 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687{
Ronald Cron5425a212020-08-04 14:58:35 +02002688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689 psa_key_type_t key_type = key_type_arg;
2690 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002691 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002693 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694
Gilles Peskine69c12672018-06-28 00:07:19 +02002695 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2696
Gilles Peskine8817f612018-12-18 00:18:46 +01002697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002698
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002699 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002700 psa_set_key_algorithm( &attributes, alg );
2701 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002702
Ronald Cron5425a212020-08-04 14:58:35 +02002703 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2704 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002705
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002706 /* Verify correct MAC, one-shot case. */
2707 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2708 expected_mac->x, expected_mac->len ) );
2709
2710 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002711 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_mac_update( &operation,
2713 input->x, input->len ) );
2714 PSA_ASSERT( psa_mac_verify_finish( &operation,
2715 expected_mac->x,
2716 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002717
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002718 /* Test a MAC that's too short, one-shot case. */
2719 TEST_EQUAL( psa_mac_verify( key, alg,
2720 input->x, input->len,
2721 expected_mac->x,
2722 expected_mac->len - 1 ),
2723 PSA_ERROR_INVALID_SIGNATURE );
2724
2725 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002726 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002727 PSA_ASSERT( psa_mac_update( &operation,
2728 input->x, input->len ) );
2729 TEST_EQUAL( psa_mac_verify_finish( &operation,
2730 expected_mac->x,
2731 expected_mac->len - 1 ),
2732 PSA_ERROR_INVALID_SIGNATURE );
2733
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002734 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002735 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2736 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002737 TEST_EQUAL( psa_mac_verify( key, alg,
2738 input->x, input->len,
2739 perturbed_mac, expected_mac->len + 1 ),
2740 PSA_ERROR_INVALID_SIGNATURE );
2741
2742 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002743 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002744 PSA_ASSERT( psa_mac_update( &operation,
2745 input->x, input->len ) );
2746 TEST_EQUAL( psa_mac_verify_finish( &operation,
2747 perturbed_mac,
2748 expected_mac->len + 1 ),
2749 PSA_ERROR_INVALID_SIGNATURE );
2750
2751 /* Test changing one byte. */
2752 for( size_t i = 0; i < expected_mac->len; i++ )
2753 {
Chris Jones9634bb12021-01-20 15:56:42 +00002754 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002755 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002756
2757 TEST_EQUAL( psa_mac_verify( key, alg,
2758 input->x, input->len,
2759 perturbed_mac, expected_mac->len ),
2760 PSA_ERROR_INVALID_SIGNATURE );
2761
Ronald Cron5425a212020-08-04 14:58:35 +02002762 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002763 PSA_ASSERT( psa_mac_update( &operation,
2764 input->x, input->len ) );
2765 TEST_EQUAL( psa_mac_verify_finish( &operation,
2766 perturbed_mac,
2767 expected_mac->len ),
2768 PSA_ERROR_INVALID_SIGNATURE );
2769 perturbed_mac[i] ^= 1;
2770 }
2771
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002773 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002774 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002775 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002776 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002777}
2778/* END_CASE */
2779
2780/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002781void cipher_operation_init( )
2782{
Jaeden Ameroab439972019-02-15 14:12:05 +00002783 const uint8_t input[1] = { 0 };
2784 unsigned char output[1] = { 0 };
2785 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002786 /* Test each valid way of initializing the object, except for `= {0}`, as
2787 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2788 * though it's OK by the C standard. We could test for this, but we'd need
2789 * to supress the Clang warning for the test. */
2790 psa_cipher_operation_t func = psa_cipher_operation_init( );
2791 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2792 psa_cipher_operation_t zero;
2793
2794 memset( &zero, 0, sizeof( zero ) );
2795
Jaeden Ameroab439972019-02-15 14:12:05 +00002796 /* A freshly-initialized cipher operation should not be usable. */
2797 TEST_EQUAL( psa_cipher_update( &func,
2798 input, sizeof( input ),
2799 output, sizeof( output ),
2800 &output_length ),
2801 PSA_ERROR_BAD_STATE );
2802 TEST_EQUAL( psa_cipher_update( &init,
2803 input, sizeof( input ),
2804 output, sizeof( output ),
2805 &output_length ),
2806 PSA_ERROR_BAD_STATE );
2807 TEST_EQUAL( psa_cipher_update( &zero,
2808 input, sizeof( input ),
2809 output, sizeof( output ),
2810 &output_length ),
2811 PSA_ERROR_BAD_STATE );
2812
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002813 /* A default cipher operation should be abortable without error. */
2814 PSA_ASSERT( psa_cipher_abort( &func ) );
2815 PSA_ASSERT( psa_cipher_abort( &init ) );
2816 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002817}
2818/* END_CASE */
2819
2820/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002821void cipher_setup( int key_type_arg,
2822 data_t *key,
2823 int alg_arg,
2824 int expected_status_arg )
2825{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002826 psa_key_type_t key_type = key_type_arg;
2827 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002828 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002829 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002830 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002831#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002832 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2833#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002834
Gilles Peskine8817f612018-12-18 00:18:46 +01002835 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002836
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002837 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2838 &operation, &status ) )
2839 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002840 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002841
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002842 /* The operation object should be reusable. */
2843#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2844 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2845 smoke_test_key_data,
2846 sizeof( smoke_test_key_data ),
2847 KNOWN_SUPPORTED_CIPHER_ALG,
2848 &operation, &status ) )
2849 goto exit;
2850 TEST_EQUAL( status, PSA_SUCCESS );
2851#endif
2852
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002853exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002854 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002855 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002856}
2857/* END_CASE */
2858
Ronald Cronee414c72021-03-18 18:50:08 +01002859/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002860void cipher_bad_order( )
2861{
Ronald Cron5425a212020-08-04 14:58:35 +02002862 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002863 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2864 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002866 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002867 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002868 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002869 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2870 0xaa, 0xaa, 0xaa, 0xaa };
2871 const uint8_t text[] = {
2872 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2873 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002874 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002875 size_t length = 0;
2876
2877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002878 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2879 psa_set_key_algorithm( &attributes, alg );
2880 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002881 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2882 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002883
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002884 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002885 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002886 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002887 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002888 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002889 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002890 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002891 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002892
2893 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002894 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002895 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002896 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002897 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002898 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002899 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002900 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002901
Jaeden Ameroab439972019-02-15 14:12:05 +00002902 /* Generate an IV without calling setup beforehand. */
2903 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2904 buffer, sizeof( buffer ),
2905 &length ),
2906 PSA_ERROR_BAD_STATE );
2907 PSA_ASSERT( psa_cipher_abort( &operation ) );
2908
2909 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002910 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002911 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2912 buffer, sizeof( buffer ),
2913 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002914 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002915 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2916 buffer, sizeof( buffer ),
2917 &length ),
2918 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002919 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002920 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002921 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002922
2923 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002924 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002925 PSA_ASSERT( psa_cipher_set_iv( &operation,
2926 iv, sizeof( iv ) ) );
2927 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2928 buffer, sizeof( buffer ),
2929 &length ),
2930 PSA_ERROR_BAD_STATE );
2931 PSA_ASSERT( psa_cipher_abort( &operation ) );
2932
2933 /* Set an IV without calling setup beforehand. */
2934 TEST_EQUAL( psa_cipher_set_iv( &operation,
2935 iv, sizeof( iv ) ),
2936 PSA_ERROR_BAD_STATE );
2937 PSA_ASSERT( psa_cipher_abort( &operation ) );
2938
2939 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002940 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002941 PSA_ASSERT( psa_cipher_set_iv( &operation,
2942 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002943 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002944 TEST_EQUAL( psa_cipher_set_iv( &operation,
2945 iv, sizeof( iv ) ),
2946 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002947 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002948 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002949 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002950
2951 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002952 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002953 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2954 buffer, sizeof( buffer ),
2955 &length ) );
2956 TEST_EQUAL( psa_cipher_set_iv( &operation,
2957 iv, sizeof( iv ) ),
2958 PSA_ERROR_BAD_STATE );
2959 PSA_ASSERT( psa_cipher_abort( &operation ) );
2960
2961 /* Call update without calling setup beforehand. */
2962 TEST_EQUAL( psa_cipher_update( &operation,
2963 text, sizeof( text ),
2964 buffer, sizeof( buffer ),
2965 &length ),
2966 PSA_ERROR_BAD_STATE );
2967 PSA_ASSERT( psa_cipher_abort( &operation ) );
2968
2969 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002970 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002971 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002972 TEST_EQUAL( psa_cipher_update( &operation,
2973 text, sizeof( text ),
2974 buffer, sizeof( buffer ),
2975 &length ),
2976 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002977 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002978 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002979 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002980
2981 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002982 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002983 PSA_ASSERT( psa_cipher_set_iv( &operation,
2984 iv, sizeof( iv ) ) );
2985 PSA_ASSERT( psa_cipher_finish( &operation,
2986 buffer, sizeof( buffer ), &length ) );
2987 TEST_EQUAL( psa_cipher_update( &operation,
2988 text, sizeof( text ),
2989 buffer, sizeof( buffer ),
2990 &length ),
2991 PSA_ERROR_BAD_STATE );
2992 PSA_ASSERT( psa_cipher_abort( &operation ) );
2993
2994 /* Call finish without calling setup beforehand. */
2995 TEST_EQUAL( psa_cipher_finish( &operation,
2996 buffer, sizeof( buffer ), &length ),
2997 PSA_ERROR_BAD_STATE );
2998 PSA_ASSERT( psa_cipher_abort( &operation ) );
2999
3000 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003001 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003002 /* Not calling update means we are encrypting an empty buffer, which is OK
3003 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003004 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003005 TEST_EQUAL( psa_cipher_finish( &operation,
3006 buffer, sizeof( buffer ), &length ),
3007 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003008 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003009 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003010 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003011
3012 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003013 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003014 PSA_ASSERT( psa_cipher_set_iv( &operation,
3015 iv, sizeof( iv ) ) );
3016 PSA_ASSERT( psa_cipher_finish( &operation,
3017 buffer, sizeof( buffer ), &length ) );
3018 TEST_EQUAL( psa_cipher_finish( &operation,
3019 buffer, sizeof( buffer ), &length ),
3020 PSA_ERROR_BAD_STATE );
3021 PSA_ASSERT( psa_cipher_abort( &operation ) );
3022
Ronald Cron5425a212020-08-04 14:58:35 +02003023 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003024
Jaeden Ameroab439972019-02-15 14:12:05 +00003025exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003026 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003027 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003028}
3029/* END_CASE */
3030
3031/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003032void cipher_encrypt_fail( int alg_arg,
3033 int key_type_arg,
3034 data_t *key_data,
3035 data_t *input,
3036 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003037{
Ronald Cron5425a212020-08-04 14:58:35 +02003038 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003039 psa_status_t status;
3040 psa_key_type_t key_type = key_type_arg;
3041 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003042 psa_status_t expected_status = expected_status_arg;
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;
3564 size_t output_buffer_size = 0;
3565 size_t output_length = 0;
3566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3567
3568 if ( PSA_ERROR_BAD_STATE != expected_status )
3569 {
3570 PSA_ASSERT( psa_crypto_init( ) );
3571
3572 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3573 psa_set_key_algorithm( &attributes, alg );
3574 psa_set_key_type( &attributes, key_type );
3575
3576 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3577 &key ) );
3578 }
3579
3580 /* Allocate input buffer and copy the iv and the plaintext */
3581 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3582 if ( input_buffer_size > 0 )
3583 {
3584 ASSERT_ALLOC( input, input_buffer_size );
3585 memcpy( input, iv->x, iv->len );
3586 memcpy( input + iv->len, input_arg->x, input_arg->len );
3587 }
3588
3589 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3590 ASSERT_ALLOC( output, output_buffer_size );
3591
3592 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3593 output_buffer_size, &output_length );
3594 TEST_EQUAL( status, expected_status );
3595
3596exit:
3597 mbedtls_free( input );
3598 mbedtls_free( output );
3599 psa_destroy_key( key );
3600 PSA_DONE( );
3601}
3602/* END_CASE */
3603
3604/* BEGIN_CASE */
3605void cipher_decrypt( int alg_arg,
3606 int key_type_arg,
3607 data_t *key_data,
3608 data_t *iv,
3609 data_t *input_arg,
3610 data_t *expected_output )
3611{
3612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3613 psa_key_type_t key_type = key_type_arg;
3614 psa_algorithm_t alg = alg_arg;
3615 unsigned char *input = NULL;
3616 size_t input_buffer_size = 0;
3617 unsigned char *output = NULL;
3618 size_t output_buffer_size = 0;
3619 size_t output_length = 0;
3620 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3621
3622 PSA_ASSERT( psa_crypto_init( ) );
3623
3624 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3625 psa_set_key_algorithm( &attributes, alg );
3626 psa_set_key_type( &attributes, key_type );
3627
3628 /* Allocate input buffer and copy the iv and the plaintext */
3629 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3630 if ( input_buffer_size > 0 )
3631 {
3632 ASSERT_ALLOC( input, input_buffer_size );
3633 memcpy( input, iv->x, iv->len );
3634 memcpy( input + iv->len, input_arg->x, input_arg->len );
3635 }
3636
3637 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3638 ASSERT_ALLOC( output, output_buffer_size );
3639
3640 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3641 &key ) );
3642
3643 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3644 output_buffer_size, &output_length ) );
3645 TEST_ASSERT( output_length <=
3646 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3647 TEST_ASSERT( output_length <=
3648 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3649
3650 ASSERT_COMPARE( expected_output->x, expected_output->len,
3651 output, output_length );
3652exit:
3653 mbedtls_free( input );
3654 mbedtls_free( output );
3655 psa_destroy_key( key );
3656 PSA_DONE( );
3657}
3658/* END_CASE */
3659
3660/* BEGIN_CASE */
3661void cipher_verify_output( int alg_arg,
3662 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003663 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003664 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003665{
Ronald Cron5425a212020-08-04 14:58:35 +02003666 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003667 psa_key_type_t key_type = key_type_arg;
3668 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003669 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003670 size_t output1_size = 0;
3671 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003672 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003673 size_t output2_size = 0;
3674 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003676
Gilles Peskine8817f612018-12-18 00:18:46 +01003677 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003678
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003679 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3680 psa_set_key_algorithm( &attributes, alg );
3681 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003682
Ronald Cron5425a212020-08-04 14:58:35 +02003683 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3684 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003685 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003686 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003687
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003688 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3689 output1, output1_size,
3690 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003691 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003692 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003693 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003694 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003695
3696 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003697 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003698
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003699 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3700 output2, output2_size,
3701 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003702 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003703 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003704 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003705 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003706
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003707 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003708
3709exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003710 mbedtls_free( output1 );
3711 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003712 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003713 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003714}
3715/* END_CASE */
3716
3717/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003718void cipher_verify_output_multipart( int alg_arg,
3719 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003720 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003721 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003722 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003723{
Ronald Cron5425a212020-08-04 14:58:35 +02003724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003725 psa_key_type_t key_type = key_type_arg;
3726 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003727 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003728 unsigned char iv[16] = {0};
3729 size_t iv_size = 16;
3730 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003731 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003732 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003733 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003734 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003735 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003736 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003737 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003738 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3739 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003740 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003741
Gilles Peskine8817f612018-12-18 00:18:46 +01003742 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003743
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003744 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3745 psa_set_key_algorithm( &attributes, alg );
3746 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003747
Ronald Cron5425a212020-08-04 14:58:35 +02003748 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3749 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003750
Ronald Cron5425a212020-08-04 14:58:35 +02003751 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3752 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003753
Steven Cooreman177deba2020-09-07 17:14:14 +02003754 if( alg != PSA_ALG_ECB_NO_PADDING )
3755 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003756 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3757 iv, iv_size,
3758 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003759 }
3760
gabor-mezei-armceface22021-01-21 12:26:17 +01003761 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3762 TEST_ASSERT( output1_buffer_size <=
3763 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003764 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003765
Gilles Peskinee0866522019-02-19 19:44:00 +01003766 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003767
Gilles Peskine8817f612018-12-18 00:18:46 +01003768 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3769 output1, output1_buffer_size,
3770 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003771 TEST_ASSERT( function_output_length <=
3772 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3773 TEST_ASSERT( function_output_length <=
3774 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003775 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003776
Gilles Peskine8817f612018-12-18 00:18:46 +01003777 PSA_ASSERT( psa_cipher_update( &operation1,
3778 input->x + first_part_size,
3779 input->len - first_part_size,
3780 output1, output1_buffer_size,
3781 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003782 TEST_ASSERT( function_output_length <=
3783 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3784 alg,
3785 input->len - first_part_size ) );
3786 TEST_ASSERT( function_output_length <=
3787 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003788 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003789
Gilles Peskine8817f612018-12-18 00:18:46 +01003790 PSA_ASSERT( psa_cipher_finish( &operation1,
3791 output1 + output1_length,
3792 output1_buffer_size - output1_length,
3793 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003794 TEST_ASSERT( function_output_length <=
3795 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3796 TEST_ASSERT( function_output_length <=
3797 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003798 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003799
Gilles Peskine8817f612018-12-18 00:18:46 +01003800 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003801
Gilles Peskine048b7f02018-06-08 14:20:49 +02003802 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003803 TEST_ASSERT( output2_buffer_size <=
3804 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3805 TEST_ASSERT( output2_buffer_size <=
3806 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003807 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003808
Steven Cooreman177deba2020-09-07 17:14:14 +02003809 if( iv_length > 0 )
3810 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003811 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3812 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003813 }
Moran Pekerded84402018-06-06 16:36:50 +03003814
Gilles Peskine8817f612018-12-18 00:18:46 +01003815 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3816 output2, output2_buffer_size,
3817 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003818 TEST_ASSERT( function_output_length <=
3819 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3820 TEST_ASSERT( function_output_length <=
3821 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003822 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003823
Gilles Peskine8817f612018-12-18 00:18:46 +01003824 PSA_ASSERT( psa_cipher_update( &operation2,
3825 output1 + first_part_size,
3826 output1_length - first_part_size,
3827 output2, output2_buffer_size,
3828 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003829 TEST_ASSERT( function_output_length <=
3830 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3831 alg,
3832 output1_length - first_part_size ) );
3833 TEST_ASSERT( function_output_length <=
3834 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003835 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003836
Gilles Peskine8817f612018-12-18 00:18:46 +01003837 PSA_ASSERT( psa_cipher_finish( &operation2,
3838 output2 + output2_length,
3839 output2_buffer_size - output2_length,
3840 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003841 TEST_ASSERT( function_output_length <=
3842 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3843 TEST_ASSERT( function_output_length <=
3844 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003845 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003846
Gilles Peskine8817f612018-12-18 00:18:46 +01003847 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003848
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003849 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003850
3851exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003852 psa_cipher_abort( &operation1 );
3853 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003854 mbedtls_free( output1 );
3855 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003856 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003857 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003858}
3859/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003860
Gilles Peskine20035e32018-02-03 22:44:14 +01003861/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003862void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003863 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003864 data_t *nonce,
3865 data_t *additional_data,
3866 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003867 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003868{
Ronald Cron5425a212020-08-04 14:58:35 +02003869 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003870 psa_key_type_t key_type = key_type_arg;
3871 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003872 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003873 unsigned char *output_data = NULL;
3874 size_t output_size = 0;
3875 size_t output_length = 0;
3876 unsigned char *output_data2 = NULL;
3877 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003878 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003879 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003881
Gilles Peskine8817f612018-12-18 00:18:46 +01003882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003883
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003884 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3885 psa_set_key_algorithm( &attributes, alg );
3886 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003887
Gilles Peskine049c7532019-05-15 20:22:09 +02003888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003889 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003890 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3891 key_bits = psa_get_key_bits( &attributes );
3892
3893 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3894 alg );
3895 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3896 * should be exact. */
3897 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3898 expected_result != PSA_ERROR_NOT_SUPPORTED )
3899 {
3900 TEST_EQUAL( output_size,
3901 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3902 TEST_ASSERT( output_size <=
3903 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3904 }
3905 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003906
Steven Cooremanf49478b2021-02-15 15:19:25 +01003907 status = psa_aead_encrypt( key, alg,
3908 nonce->x, nonce->len,
3909 additional_data->x,
3910 additional_data->len,
3911 input_data->x, input_data->len,
3912 output_data, output_size,
3913 &output_length );
3914
3915 /* If the operation is not supported, just skip and not fail in case the
3916 * encryption involves a common limitation of cryptography hardwares and
3917 * an alternative implementation. */
3918 if( status == PSA_ERROR_NOT_SUPPORTED )
3919 {
3920 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3921 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3922 }
3923
3924 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003925
3926 if( PSA_SUCCESS == expected_result )
3927 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003928 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003929
Gilles Peskine003a4a92019-05-14 16:09:40 +02003930 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3931 * should be exact. */
3932 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003933 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003934
gabor-mezei-armceface22021-01-21 12:26:17 +01003935 TEST_ASSERT( input_data->len <=
3936 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3937
Ronald Cron5425a212020-08-04 14:58:35 +02003938 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003939 nonce->x, nonce->len,
3940 additional_data->x,
3941 additional_data->len,
3942 output_data, output_length,
3943 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003944 &output_length2 ),
3945 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003946
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003947 ASSERT_COMPARE( input_data->x, input_data->len,
3948 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003949 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003950
Gilles Peskinea1cac842018-06-11 19:33:02 +02003951exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003952 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003953 mbedtls_free( output_data );
3954 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003955 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003956}
3957/* END_CASE */
3958
3959/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003960void aead_encrypt( int key_type_arg, data_t *key_data,
3961 int alg_arg,
3962 data_t *nonce,
3963 data_t *additional_data,
3964 data_t *input_data,
3965 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003966{
Ronald Cron5425a212020-08-04 14:58:35 +02003967 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003968 psa_key_type_t key_type = key_type_arg;
3969 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003970 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003971 unsigned char *output_data = NULL;
3972 size_t output_size = 0;
3973 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003975 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003976
Gilles Peskine8817f612018-12-18 00:18:46 +01003977 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003978
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003979 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3980 psa_set_key_algorithm( &attributes, alg );
3981 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003982
Gilles Peskine049c7532019-05-15 20:22:09 +02003983 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003984 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003985 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3986 key_bits = psa_get_key_bits( &attributes );
3987
3988 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3989 alg );
3990 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3991 * should be exact. */
3992 TEST_EQUAL( output_size,
3993 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3994 TEST_ASSERT( output_size <=
3995 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3996 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003997
Steven Cooremand588ea12021-01-11 19:36:04 +01003998 status = psa_aead_encrypt( key, alg,
3999 nonce->x, nonce->len,
4000 additional_data->x, additional_data->len,
4001 input_data->x, input_data->len,
4002 output_data, output_size,
4003 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004004
Ronald Cron28a45ed2021-02-09 20:35:42 +01004005 /* If the operation is not supported, just skip and not fail in case the
4006 * encryption involves a common limitation of cryptography hardwares and
4007 * an alternative implementation. */
4008 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004009 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004010 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4011 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004012 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004013
4014 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004015 ASSERT_COMPARE( expected_result->x, expected_result->len,
4016 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004017
Gilles Peskinea1cac842018-06-11 19:33:02 +02004018exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004019 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004020 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004021 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004022}
4023/* END_CASE */
4024
4025/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004026void aead_decrypt( int key_type_arg, data_t *key_data,
4027 int alg_arg,
4028 data_t *nonce,
4029 data_t *additional_data,
4030 data_t *input_data,
4031 data_t *expected_data,
4032 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004033{
Ronald Cron5425a212020-08-04 14:58:35 +02004034 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004035 psa_key_type_t key_type = key_type_arg;
4036 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004037 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004038 unsigned char *output_data = NULL;
4039 size_t output_size = 0;
4040 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004041 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004042 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004043 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004044
Gilles Peskine8817f612018-12-18 00:18:46 +01004045 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004046
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004047 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4048 psa_set_key_algorithm( &attributes, alg );
4049 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004050
Gilles Peskine049c7532019-05-15 20:22:09 +02004051 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004052 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004053 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4054 key_bits = psa_get_key_bits( &attributes );
4055
4056 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4057 alg );
4058 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4059 expected_result != PSA_ERROR_NOT_SUPPORTED )
4060 {
4061 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4062 * should be exact. */
4063 TEST_EQUAL( output_size,
4064 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
4065 TEST_ASSERT( output_size <=
4066 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4067 }
4068 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004069
Steven Cooremand588ea12021-01-11 19:36:04 +01004070 status = psa_aead_decrypt( key, alg,
4071 nonce->x, nonce->len,
4072 additional_data->x,
4073 additional_data->len,
4074 input_data->x, input_data->len,
4075 output_data, output_size,
4076 &output_length );
4077
Ronald Cron28a45ed2021-02-09 20:35:42 +01004078 /* If the operation is not supported, just skip and not fail in case the
4079 * decryption involves a common limitation of cryptography hardwares and
4080 * an alternative implementation. */
4081 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004082 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004083 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4084 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004085 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004086
4087 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004088
Gilles Peskine2d277862018-06-18 15:41:12 +02004089 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004090 ASSERT_COMPARE( expected_data->x, expected_data->len,
4091 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004092
Gilles Peskinea1cac842018-06-11 19:33:02 +02004093exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004094 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004095 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004096 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004097}
4098/* END_CASE */
4099
4100/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004101void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4102 int alg_arg,
4103 data_t *nonce,
4104 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004105 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004106 int do_set_lengths,
4107 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004108{
Paul Elliottd3f82412021-06-16 16:52:21 +01004109 size_t ad_part_len = 0;
4110 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004111 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004112
Paul Elliott32f46ba2021-09-23 18:24:36 +01004113 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004114 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004115 mbedtls_test_set_step( ad_part_len );
4116
4117 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004118 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004119 if( ad_part_len & 0x01 )
4120 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4121 else
4122 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004123 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004124
4125 /* Split ad into length(ad_part_len) parts. */
4126 if( !aead_multipart_internal_func( key_type_arg, key_data,
4127 alg_arg, nonce,
4128 additional_data,
4129 ad_part_len,
4130 input_data, -1,
4131 set_lengths_method,
4132 expected_output,
4133 1, 0 ) )
4134 break;
4135
4136 /* length(0) part, length(ad_part_len) part, length(0) part... */
4137 mbedtls_test_set_step( 1000 + ad_part_len );
4138
4139 if( !aead_multipart_internal_func( key_type_arg, key_data,
4140 alg_arg, nonce,
4141 additional_data,
4142 ad_part_len,
4143 input_data, -1,
4144 set_lengths_method,
4145 expected_output,
4146 1, 1 ) )
4147 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004148 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004149
Paul Elliott32f46ba2021-09-23 18:24:36 +01004150 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004151 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004152 /* Split data into length(data_part_len) parts. */
4153 mbedtls_test_set_step( 2000 + data_part_len );
4154
4155 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004156 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004157 if( data_part_len & 0x01 )
4158 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4159 else
4160 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004161 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004162
Paul Elliott32f46ba2021-09-23 18:24:36 +01004163 if( !aead_multipart_internal_func( key_type_arg, key_data,
4164 alg_arg, nonce,
4165 additional_data, -1,
4166 input_data, data_part_len,
4167 set_lengths_method,
4168 expected_output,
4169 1, 0 ) )
4170 break;
4171
4172 /* length(0) part, length(data_part_len) part, length(0) part... */
4173 mbedtls_test_set_step( 3000 + data_part_len );
4174
4175 if( !aead_multipart_internal_func( key_type_arg, key_data,
4176 alg_arg, nonce,
4177 additional_data, -1,
4178 input_data, data_part_len,
4179 set_lengths_method,
4180 expected_output,
4181 1, 1 ) )
4182 break;
4183 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004184
Paul Elliott8fc45162021-06-23 16:06:01 +01004185 /* Goto is required to silence warnings about unused labels, as we
4186 * don't actually do any test assertions in this function. */
4187 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004188}
4189/* END_CASE */
4190
4191/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004192void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4193 int alg_arg,
4194 data_t *nonce,
4195 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004196 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004197 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004198 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004199{
Paul Elliottd3f82412021-06-16 16:52:21 +01004200 size_t ad_part_len = 0;
4201 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004202 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004203
Paul Elliott32f46ba2021-09-23 18:24:36 +01004204 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004205 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004206 /* Split ad into length(ad_part_len) parts. */
4207 mbedtls_test_set_step( ad_part_len );
4208
4209 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004210 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004211 if( ad_part_len & 0x01 )
4212 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4213 else
4214 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004215 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004216
4217 if( !aead_multipart_internal_func( key_type_arg, key_data,
4218 alg_arg, nonce,
4219 additional_data,
4220 ad_part_len,
4221 input_data, -1,
4222 set_lengths_method,
4223 expected_output,
4224 0, 0 ) )
4225 break;
4226
4227 /* length(0) part, length(ad_part_len) part, length(0) part... */
4228 mbedtls_test_set_step( 1000 + ad_part_len );
4229
4230 if( !aead_multipart_internal_func( key_type_arg, key_data,
4231 alg_arg, nonce,
4232 additional_data,
4233 ad_part_len,
4234 input_data, -1,
4235 set_lengths_method,
4236 expected_output,
4237 0, 1 ) )
4238 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004239 }
4240
Paul Elliott32f46ba2021-09-23 18:24:36 +01004241 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004242 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004243 /* Split data into length(data_part_len) parts. */
4244 mbedtls_test_set_step( 2000 + data_part_len );
4245
4246 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004247 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004248 if( data_part_len & 0x01 )
4249 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4250 else
4251 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004252 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004253
4254 if( !aead_multipart_internal_func( key_type_arg, key_data,
4255 alg_arg, nonce,
4256 additional_data, -1,
4257 input_data, data_part_len,
4258 set_lengths_method,
4259 expected_output,
4260 0, 0 ) )
4261 break;
4262
4263 /* length(0) part, length(data_part_len) part, length(0) part... */
4264 mbedtls_test_set_step( 3000 + data_part_len );
4265
4266 if( !aead_multipart_internal_func( key_type_arg, key_data,
4267 alg_arg, nonce,
4268 additional_data, -1,
4269 input_data, data_part_len,
4270 set_lengths_method,
4271 expected_output,
4272 0, 1 ) )
4273 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004274 }
4275
Paul Elliott8fc45162021-06-23 16:06:01 +01004276 /* Goto is required to silence warnings about unused labels, as we
4277 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004278 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004279}
4280/* END_CASE */
4281
4282/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004283void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4284 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004285 int nonce_length,
4286 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004287 data_t *additional_data,
4288 data_t *input_data,
4289 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004290{
4291
4292 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4293 psa_key_type_t key_type = key_type_arg;
4294 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004295 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004296 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4297 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4298 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004299 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004300 size_t actual_nonce_length = 0;
4301 size_t expected_nonce_length = expected_nonce_length_arg;
4302 unsigned char *output = NULL;
4303 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004304 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004305 size_t ciphertext_size = 0;
4306 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004307 size_t tag_length = 0;
4308 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004309
4310 PSA_ASSERT( psa_crypto_init( ) );
4311
4312 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4313 psa_set_key_algorithm( & attributes, alg );
4314 psa_set_key_type( & attributes, key_type );
4315
4316 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4317 &key ) );
4318
4319 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4320
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004321 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4322
Paul Elliottf1277632021-08-24 18:11:37 +01004323 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004324
Paul Elliottf1277632021-08-24 18:11:37 +01004325 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004326
Paul Elliottf1277632021-08-24 18:11:37 +01004327 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004328
Paul Elliottf1277632021-08-24 18:11:37 +01004329 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004330
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004331 status = psa_aead_encrypt_setup( &operation, key, alg );
4332
4333 /* If the operation is not supported, just skip and not fail in case the
4334 * encryption involves a common limitation of cryptography hardwares and
4335 * an alternative implementation. */
4336 if( status == PSA_ERROR_NOT_SUPPORTED )
4337 {
4338 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004339 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004340 }
4341
4342 PSA_ASSERT( status );
4343
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004344 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004345 nonce_length,
4346 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004347
Paul Elliott693bf312021-07-23 17:40:41 +01004348 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004349
Paul Elliottf1277632021-08-24 18:11:37 +01004350 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004351
Paul Elliott88ecbe12021-09-22 17:23:03 +01004352 if( expected_status == PSA_SUCCESS )
4353 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4354 alg ) );
4355
Paul Elliottd79c5c52021-10-06 21:49:41 +01004356 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004357
Paul Elliott693bf312021-07-23 17:40:41 +01004358 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004359 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004360 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004361 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4362 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004363
4364 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4365 additional_data->len ) );
4366
4367 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004368 output, output_size,
4369 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004370
Paul Elliottf1277632021-08-24 18:11:37 +01004371 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4372 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004373 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4374 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004375
4376exit:
4377 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004378 mbedtls_free( output );
4379 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004380 psa_aead_abort( &operation );
4381 PSA_DONE( );
4382}
4383/* END_CASE */
4384
4385/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004386void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4387 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004388 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004389 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004390 data_t *additional_data,
4391 data_t *input_data,
4392 int expected_status_arg )
4393{
4394
4395 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4396 psa_key_type_t key_type = key_type_arg;
4397 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004398 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004399 uint8_t *nonce_buffer = NULL;
4400 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4401 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4402 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004403 unsigned char *output = NULL;
4404 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004405 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004406 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004407 size_t ciphertext_size = 0;
4408 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004409 size_t tag_length = 0;
4410 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004411 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004412 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004413
4414 PSA_ASSERT( psa_crypto_init( ) );
4415
4416 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4417 psa_set_key_algorithm( &attributes, alg );
4418 psa_set_key_type( &attributes, key_type );
4419
4420 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4421 &key ) );
4422
4423 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4424
4425 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4426
Paul Elliott6f0e7202021-08-25 12:57:18 +01004427 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004428
Paul Elliott6f0e7202021-08-25 12:57:18 +01004429 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004430
Paul Elliott6f0e7202021-08-25 12:57:18 +01004431 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004432
Paul Elliott6f0e7202021-08-25 12:57:18 +01004433 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004434
Paul Elliott863864a2021-07-23 17:28:31 +01004435 status = psa_aead_encrypt_setup( &operation, key, alg );
4436
4437 /* If the operation is not supported, just skip and not fail in case the
4438 * encryption involves a common limitation of cryptography hardwares and
4439 * an alternative implementation. */
4440 if( status == PSA_ERROR_NOT_SUPPORTED )
4441 {
4442 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004443 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004444 }
4445
4446 PSA_ASSERT( status );
4447
Paul Elliott4023ffd2021-09-10 16:21:22 +01004448 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4449 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004450 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004451 /* Arbitrary size buffer, to test zero length valid buffer. */
4452 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004453 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004454 }
4455 else
4456 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004457 /* If length is zero, then this will return NULL. */
4458 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004459 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004460
Paul Elliott4023ffd2021-09-10 16:21:22 +01004461 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004462 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004463 for( index = 0; index < nonce_length - 1; ++index )
4464 {
4465 nonce_buffer[index] = 'a' + index;
4466 }
Paul Elliott66696b52021-08-16 18:42:41 +01004467 }
Paul Elliott863864a2021-07-23 17:28:31 +01004468 }
4469
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004470 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4471 {
4472 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4473 input_data->len ) );
4474 }
4475
Paul Elliott6f0e7202021-08-25 12:57:18 +01004476 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004477
Paul Elliott693bf312021-07-23 17:40:41 +01004478 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004479
4480 if( expected_status == PSA_SUCCESS )
4481 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004482 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4483 {
4484 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4485 input_data->len ) );
4486 }
4487 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4488 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004489
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004490 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4491 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4492 additional_data->len ),
4493 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004494
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004495 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004496 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004497 &ciphertext_length ),
4498 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004499
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004500 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004501 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004502 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4503 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004504 }
4505
4506exit:
4507 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004508 mbedtls_free( output );
4509 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004510 mbedtls_free( nonce_buffer );
4511 psa_aead_abort( &operation );
4512 PSA_DONE( );
4513}
4514/* END_CASE */
4515
4516/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004517void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4518 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004519 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004520 data_t *nonce,
4521 data_t *additional_data,
4522 data_t *input_data,
4523 int expected_status_arg )
4524{
4525
4526 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4527 psa_key_type_t key_type = key_type_arg;
4528 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004529 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004530 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4531 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4532 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004533 unsigned char *output = NULL;
4534 unsigned char *ciphertext = NULL;
4535 size_t output_size = output_size_arg;
4536 size_t ciphertext_size = 0;
4537 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004538 size_t tag_length = 0;
4539 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4540
4541 PSA_ASSERT( psa_crypto_init( ) );
4542
4543 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4544 psa_set_key_algorithm( &attributes, alg );
4545 psa_set_key_type( &attributes, key_type );
4546
4547 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4548 &key ) );
4549
4550 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4551
Paul Elliottc6d11d02021-09-01 12:04:23 +01004552 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004553
Paul Elliottc6d11d02021-09-01 12:04:23 +01004554 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004555
Paul Elliottc6d11d02021-09-01 12:04:23 +01004556 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004557
Paul Elliott43fbda62021-07-23 18:30:59 +01004558 status = psa_aead_encrypt_setup( &operation, key, alg );
4559
4560 /* If the operation is not supported, just skip and not fail in case the
4561 * encryption involves a common limitation of cryptography hardwares and
4562 * an alternative implementation. */
4563 if( status == PSA_ERROR_NOT_SUPPORTED )
4564 {
4565 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4566 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4567 }
4568
4569 PSA_ASSERT( status );
4570
Paul Elliott47b9a142021-10-07 15:04:57 +01004571 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4572 input_data->len ) );
4573
Paul Elliott43fbda62021-07-23 18:30:59 +01004574 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4575
4576 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4577 additional_data->len ) );
4578
4579 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004580 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004581
4582 TEST_EQUAL( status, expected_status );
4583
4584 if( expected_status == PSA_SUCCESS )
4585 {
4586 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004587 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4588 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004589 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4590 }
4591
4592exit:
4593 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004594 mbedtls_free( output );
4595 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004596 psa_aead_abort( &operation );
4597 PSA_DONE( );
4598}
4599/* END_CASE */
4600
Paul Elliott91b021e2021-07-23 18:52:31 +01004601/* BEGIN_CASE */
4602void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4603 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004604 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004605 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004606 data_t *nonce,
4607 data_t *additional_data,
4608 data_t *input_data,
4609 int expected_status_arg )
4610{
4611
4612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4613 psa_key_type_t key_type = key_type_arg;
4614 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004615 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4617 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4618 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004619 unsigned char *ciphertext = NULL;
4620 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004621 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004622 size_t ciphertext_size = 0;
4623 size_t ciphertext_length = 0;
4624 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004625 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004626 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004627
4628 PSA_ASSERT( psa_crypto_init( ) );
4629
4630 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4631 psa_set_key_algorithm( &attributes, alg );
4632 psa_set_key_type( &attributes, key_type );
4633
4634 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4635 &key ) );
4636
4637 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4638
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004639 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004640
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004641 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004642
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004643 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004644
Paul Elliott719c1322021-09-13 18:27:22 +01004645 ASSERT_ALLOC( tag_buffer, tag_size );
4646
Paul Elliott91b021e2021-07-23 18:52:31 +01004647 status = psa_aead_encrypt_setup( &operation, key, alg );
4648
4649 /* If the operation is not supported, just skip and not fail in case the
4650 * encryption involves a common limitation of cryptography hardwares and
4651 * an alternative implementation. */
4652 if( status == PSA_ERROR_NOT_SUPPORTED )
4653 {
4654 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4655 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4656 }
4657
4658 PSA_ASSERT( status );
4659
4660 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4661
Paul Elliott76bda482021-10-07 17:07:23 +01004662 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4663 input_data->len ) );
4664
Paul Elliott91b021e2021-07-23 18:52:31 +01004665 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4666 additional_data->len ) );
4667
4668 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004669 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004670
4671 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004672 status = psa_aead_finish( &operation, finish_ciphertext,
4673 finish_ciphertext_size,
4674 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004675 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004676
4677 TEST_EQUAL( status, expected_status );
4678
4679exit:
4680 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004681 mbedtls_free( ciphertext );
4682 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004683 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004684 psa_aead_abort( &operation );
4685 PSA_DONE( );
4686}
4687/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004688
4689/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004690void aead_multipart_verify( int key_type_arg, data_t *key_data,
4691 int alg_arg,
4692 data_t *nonce,
4693 data_t *additional_data,
4694 data_t *input_data,
4695 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004696 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004697 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004698 int expected_status_arg )
4699{
4700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4701 psa_key_type_t key_type = key_type_arg;
4702 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004703 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4705 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4706 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004707 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004708 unsigned char *plaintext = NULL;
4709 unsigned char *finish_plaintext = NULL;
4710 size_t plaintext_size = 0;
4711 size_t plaintext_length = 0;
4712 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004713 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004714 unsigned char *tag_buffer = NULL;
4715 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004716
4717 PSA_ASSERT( psa_crypto_init( ) );
4718
4719 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4720 psa_set_key_algorithm( &attributes, alg );
4721 psa_set_key_type( &attributes, key_type );
4722
4723 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4724 &key ) );
4725
4726 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4727
4728 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4729 input_data->len );
4730
4731 ASSERT_ALLOC( plaintext, plaintext_size );
4732
4733 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4734
4735 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4736
Paul Elliott9961a662021-09-17 19:19:02 +01004737 status = psa_aead_decrypt_setup( &operation, key, alg );
4738
4739 /* If the operation is not supported, just skip and not fail in case the
4740 * encryption involves a common limitation of cryptography hardwares and
4741 * an alternative implementation. */
4742 if( status == PSA_ERROR_NOT_SUPPORTED )
4743 {
4744 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4745 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4746 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004747 TEST_EQUAL( status, expected_setup_status );
4748
4749 if( status != PSA_SUCCESS )
4750 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004751
4752 PSA_ASSERT( status );
4753
4754 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4755
Paul Elliottfec6f372021-10-06 17:15:02 +01004756 status = psa_aead_set_lengths( &operation, additional_data->len,
4757 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004758 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004759
Paul Elliott9961a662021-09-17 19:19:02 +01004760 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4761 additional_data->len ) );
4762
4763 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4764 input_data->len,
4765 plaintext, plaintext_size,
4766 &plaintext_length ) );
4767
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004768 if( tag_usage == USE_GIVEN_TAG )
4769 {
4770 tag_buffer = tag->x;
4771 tag_size = tag->len;
4772 }
4773
Paul Elliott9961a662021-09-17 19:19:02 +01004774 status = psa_aead_verify( &operation, finish_plaintext,
4775 verify_plaintext_size,
4776 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004777 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004778
4779 TEST_EQUAL( status, expected_status );
4780
4781exit:
4782 psa_destroy_key( key );
4783 mbedtls_free( plaintext );
4784 mbedtls_free( finish_plaintext );
4785 psa_aead_abort( &operation );
4786 PSA_DONE( );
4787}
4788/* END_CASE */
4789
Paul Elliott9961a662021-09-17 19:19:02 +01004790/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004791void aead_multipart_setup( int key_type_arg, data_t *key_data,
4792 int alg_arg, int expected_status_arg )
4793{
4794 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4795 psa_key_type_t key_type = key_type_arg;
4796 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004797 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4799 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4800 psa_status_t expected_status = expected_status_arg;
4801
4802 PSA_ASSERT( psa_crypto_init( ) );
4803
4804 psa_set_key_usage_flags( &attributes,
4805 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4806 psa_set_key_algorithm( &attributes, alg );
4807 psa_set_key_type( &attributes, key_type );
4808
4809 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4810 &key ) );
4811
Paul Elliott5221ef62021-09-19 17:33:03 +01004812 status = psa_aead_encrypt_setup( &operation, key, alg );
4813
4814 TEST_EQUAL( status, expected_status );
4815
4816 psa_aead_abort( &operation );
4817
Paul Elliott5221ef62021-09-19 17:33:03 +01004818 status = psa_aead_decrypt_setup( &operation, key, alg );
4819
4820 TEST_EQUAL(status, expected_status );
4821
4822exit:
4823 psa_destroy_key( key );
4824 psa_aead_abort( &operation );
4825 PSA_DONE( );
4826}
4827/* END_CASE */
4828
4829/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004830void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4831 int alg_arg,
4832 data_t *nonce,
4833 data_t *additional_data,
4834 data_t *input_data )
4835{
4836 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4837 psa_key_type_t key_type = key_type_arg;
4838 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004839 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004840 unsigned char *output_data = NULL;
4841 unsigned char *final_data = NULL;
4842 size_t output_size = 0;
4843 size_t finish_output_size = 0;
4844 size_t output_length = 0;
4845 size_t key_bits = 0;
4846 size_t tag_length = 0;
4847 size_t tag_size = 0;
4848 size_t nonce_length = 0;
4849 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4850 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4851 size_t output_part_length = 0;
4852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4853
4854 PSA_ASSERT( psa_crypto_init( ) );
4855
4856 psa_set_key_usage_flags( & attributes,
4857 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4858 psa_set_key_algorithm( & attributes, alg );
4859 psa_set_key_type( & attributes, key_type );
4860
4861 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4862 &key ) );
4863
4864 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4865 key_bits = psa_get_key_bits( &attributes );
4866
4867 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4868
4869 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4870
4871 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4872
4873 ASSERT_ALLOC( output_data, output_size );
4874
4875 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4876
4877 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4878
4879 ASSERT_ALLOC( final_data, finish_output_size );
4880
4881 /* Test all operations error without calling setup first. */
4882
Paul Elliottc23a9a02021-06-21 18:32:46 +01004883 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4884 PSA_ERROR_BAD_STATE );
4885
4886 psa_aead_abort( &operation );
4887
Paul Elliottc23a9a02021-06-21 18:32:46 +01004888 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4889 PSA_AEAD_NONCE_MAX_SIZE,
4890 &nonce_length ),
4891 PSA_ERROR_BAD_STATE );
4892
4893 psa_aead_abort( &operation );
4894
Paul Elliott481be342021-07-16 17:38:47 +01004895 /* ------------------------------------------------------- */
4896
Paul Elliottc23a9a02021-06-21 18:32:46 +01004897 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4898 input_data->len ),
4899 PSA_ERROR_BAD_STATE );
4900
4901 psa_aead_abort( &operation );
4902
Paul Elliott481be342021-07-16 17:38:47 +01004903 /* ------------------------------------------------------- */
4904
Paul Elliottc23a9a02021-06-21 18:32:46 +01004905 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4906 additional_data->len ),
4907 PSA_ERROR_BAD_STATE );
4908
4909 psa_aead_abort( &operation );
4910
Paul Elliott481be342021-07-16 17:38:47 +01004911 /* ------------------------------------------------------- */
4912
Paul Elliottc23a9a02021-06-21 18:32:46 +01004913 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4914 input_data->len, output_data,
4915 output_size, &output_length ),
4916 PSA_ERROR_BAD_STATE );
4917
4918 psa_aead_abort( &operation );
4919
Paul Elliott481be342021-07-16 17:38:47 +01004920 /* ------------------------------------------------------- */
4921
Paul Elliottc23a9a02021-06-21 18:32:46 +01004922 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4923 finish_output_size,
4924 &output_part_length,
4925 tag_buffer, tag_length,
4926 &tag_size ),
4927 PSA_ERROR_BAD_STATE );
4928
4929 psa_aead_abort( &operation );
4930
Paul Elliott481be342021-07-16 17:38:47 +01004931 /* ------------------------------------------------------- */
4932
Paul Elliottc23a9a02021-06-21 18:32:46 +01004933 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4934 finish_output_size,
4935 &output_part_length,
4936 tag_buffer,
4937 tag_length ),
4938 PSA_ERROR_BAD_STATE );
4939
4940 psa_aead_abort( &operation );
4941
4942 /* Test for double setups. */
4943
Paul Elliottc23a9a02021-06-21 18:32:46 +01004944 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4945
4946 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4947 PSA_ERROR_BAD_STATE );
4948
4949 psa_aead_abort( &operation );
4950
Paul Elliott481be342021-07-16 17:38:47 +01004951 /* ------------------------------------------------------- */
4952
Paul Elliottc23a9a02021-06-21 18:32:46 +01004953 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4954
4955 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4956 PSA_ERROR_BAD_STATE );
4957
4958 psa_aead_abort( &operation );
4959
Paul Elliott374a2be2021-07-16 17:53:40 +01004960 /* ------------------------------------------------------- */
4961
Paul Elliott374a2be2021-07-16 17:53:40 +01004962 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4963
4964 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4965 PSA_ERROR_BAD_STATE );
4966
4967 psa_aead_abort( &operation );
4968
4969 /* ------------------------------------------------------- */
4970
Paul Elliott374a2be2021-07-16 17:53:40 +01004971 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4972
4973 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4974 PSA_ERROR_BAD_STATE );
4975
4976 psa_aead_abort( &operation );
4977
Paul Elliottc23a9a02021-06-21 18:32:46 +01004978 /* Test for not setting a nonce. */
4979
Paul Elliottc23a9a02021-06-21 18:32:46 +01004980 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4981
4982 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4983 additional_data->len ),
4984 PSA_ERROR_BAD_STATE );
4985
4986 psa_aead_abort( &operation );
4987
Paul Elliott7f628422021-09-01 12:08:29 +01004988 /* ------------------------------------------------------- */
4989
Paul Elliott7f628422021-09-01 12:08:29 +01004990 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4991
4992 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4993 input_data->len, output_data,
4994 output_size, &output_length ),
4995 PSA_ERROR_BAD_STATE );
4996
4997 psa_aead_abort( &operation );
4998
Paul Elliottbdc2c682021-09-21 18:37:10 +01004999 /* ------------------------------------------------------- */
5000
Paul Elliottbdc2c682021-09-21 18:37:10 +01005001 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5002
5003 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5004 finish_output_size,
5005 &output_part_length,
5006 tag_buffer, tag_length,
5007 &tag_size ),
5008 PSA_ERROR_BAD_STATE );
5009
5010 psa_aead_abort( &operation );
5011
5012 /* ------------------------------------------------------- */
5013
Paul Elliottbdc2c682021-09-21 18:37:10 +01005014 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5015
5016 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5017 finish_output_size,
5018 &output_part_length,
5019 tag_buffer,
5020 tag_length ),
5021 PSA_ERROR_BAD_STATE );
5022
5023 psa_aead_abort( &operation );
5024
Paul Elliottc23a9a02021-06-21 18:32:46 +01005025 /* Test for double setting nonce. */
5026
Paul Elliottc23a9a02021-06-21 18:32:46 +01005027 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5028
5029 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5030
5031 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5032 PSA_ERROR_BAD_STATE );
5033
5034 psa_aead_abort( &operation );
5035
Paul Elliott374a2be2021-07-16 17:53:40 +01005036 /* Test for double generating nonce. */
5037
Paul Elliott374a2be2021-07-16 17:53:40 +01005038 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5039
5040 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5041 PSA_AEAD_NONCE_MAX_SIZE,
5042 &nonce_length ) );
5043
5044 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5045 PSA_AEAD_NONCE_MAX_SIZE,
5046 &nonce_length ),
5047 PSA_ERROR_BAD_STATE );
5048
5049
5050 psa_aead_abort( &operation );
5051
5052 /* Test for generate nonce then set and vice versa */
5053
Paul Elliott374a2be2021-07-16 17:53:40 +01005054 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5055
5056 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5057 PSA_AEAD_NONCE_MAX_SIZE,
5058 &nonce_length ) );
5059
5060 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5061 PSA_ERROR_BAD_STATE );
5062
5063 psa_aead_abort( &operation );
5064
Andrzej Kurekad837522021-12-15 15:28:49 +01005065 /* Test for generating nonce after calling set lengths */
5066
5067 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5068
5069 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5070 input_data->len ) );
5071
5072 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5073 PSA_AEAD_NONCE_MAX_SIZE,
5074 &nonce_length ) );
5075
5076 psa_aead_abort( &operation );
5077
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005078 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005079
5080 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5081
5082 if( operation.alg == PSA_ALG_CCM )
5083 {
5084 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5085 input_data->len ),
5086 PSA_ERROR_INVALID_ARGUMENT );
5087 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5088 PSA_AEAD_NONCE_MAX_SIZE,
5089 &nonce_length ),
5090 PSA_ERROR_BAD_STATE );
5091 }
5092 else
5093 {
5094 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5095 input_data->len ) );
5096 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5097 PSA_AEAD_NONCE_MAX_SIZE,
5098 &nonce_length ) );
5099 }
5100
5101 psa_aead_abort( &operation );
5102
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005103 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005104#if SIZE_MAX > UINT32_MAX
5105 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5106
5107 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5108 {
5109 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5110 input_data->len ),
5111 PSA_ERROR_INVALID_ARGUMENT );
5112 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5113 PSA_AEAD_NONCE_MAX_SIZE,
5114 &nonce_length ),
5115 PSA_ERROR_BAD_STATE );
5116 }
5117 else
5118 {
5119 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5120 input_data->len ) );
5121 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5122 PSA_AEAD_NONCE_MAX_SIZE,
5123 &nonce_length ) );
5124 }
5125
5126 psa_aead_abort( &operation );
5127#endif
5128
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005129 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005130
5131 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5132
5133 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5134 PSA_AEAD_NONCE_MAX_SIZE,
5135 &nonce_length ) );
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 }
5143 else
5144 {
5145 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5146 input_data->len ) );
5147 }
5148
5149 psa_aead_abort( &operation );
5150
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005151 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005152 /* Test for setting nonce after calling set lengths */
5153
5154 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5155
5156 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5157 input_data->len ) );
5158
5159 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5160
5161 psa_aead_abort( &operation );
5162
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005163 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005164
5165 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5166
5167 if( operation.alg == PSA_ALG_CCM )
5168 {
5169 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5170 input_data->len ),
5171 PSA_ERROR_INVALID_ARGUMENT );
5172 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5173 PSA_ERROR_BAD_STATE );
5174 }
5175 else
5176 {
5177 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5178 input_data->len ) );
5179 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5180 }
5181
5182 psa_aead_abort( &operation );
5183
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005184 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005185#if SIZE_MAX > UINT32_MAX
5186 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5187
5188 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5189 {
5190 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5191 input_data->len ),
5192 PSA_ERROR_INVALID_ARGUMENT );
5193 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5194 PSA_ERROR_BAD_STATE );
5195 }
5196 else
5197 {
5198 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5199 input_data->len ) );
5200 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5201 }
5202
5203 psa_aead_abort( &operation );
5204#endif
5205
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005206 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005207
5208 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5209
5210 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5211
5212 if( operation.alg == PSA_ALG_CCM )
5213 {
5214 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5215 input_data->len ),
5216 PSA_ERROR_INVALID_ARGUMENT );
5217 }
5218 else
5219 {
5220 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5221 input_data->len ) );
5222 }
5223
5224 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005225
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005226 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005227#if SIZE_MAX > UINT32_MAX
5228 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5229
5230 if( operation.alg == PSA_ALG_GCM )
5231 {
5232 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5233 SIZE_MAX ),
5234 PSA_ERROR_INVALID_ARGUMENT );
5235 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5236 PSA_ERROR_BAD_STATE );
5237 }
5238 else if ( operation.alg != PSA_ALG_CCM )
5239 {
5240 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5241 SIZE_MAX ) );
5242 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5243 }
5244
5245 psa_aead_abort( &operation );
5246
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005247 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005248 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5249
5250 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5251
5252 if( operation.alg == PSA_ALG_GCM )
5253 {
5254 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5255 SIZE_MAX ),
5256 PSA_ERROR_INVALID_ARGUMENT );
5257 }
5258 else if ( operation.alg != PSA_ALG_CCM )
5259 {
5260 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5261 SIZE_MAX ) );
5262 }
5263
5264 psa_aead_abort( &operation );
5265#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005266
5267 /* ------------------------------------------------------- */
5268
Paul Elliott374a2be2021-07-16 17:53:40 +01005269 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5270
5271 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5272
5273 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5274 PSA_AEAD_NONCE_MAX_SIZE,
5275 &nonce_length ),
5276 PSA_ERROR_BAD_STATE );
5277
5278 psa_aead_abort( &operation );
5279
Paul Elliott7220cae2021-06-22 17:25:57 +01005280 /* Test for generating nonce in decrypt setup. */
5281
Paul Elliott7220cae2021-06-22 17:25:57 +01005282 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5283
5284 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5285 PSA_AEAD_NONCE_MAX_SIZE,
5286 &nonce_length ),
5287 PSA_ERROR_BAD_STATE );
5288
5289 psa_aead_abort( &operation );
5290
Paul Elliottc23a9a02021-06-21 18:32:46 +01005291 /* Test for setting lengths twice. */
5292
Paul Elliottc23a9a02021-06-21 18:32:46 +01005293 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5294
5295 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5296
5297 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5298 input_data->len ) );
5299
5300 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5301 input_data->len ),
5302 PSA_ERROR_BAD_STATE );
5303
5304 psa_aead_abort( &operation );
5305
Andrzej Kurekad837522021-12-15 15:28:49 +01005306 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005307
Paul Elliottc23a9a02021-06-21 18:32:46 +01005308 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5309
5310 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5311
Andrzej Kurekad837522021-12-15 15:28:49 +01005312 if( operation.alg == PSA_ALG_CCM )
5313 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005314
Andrzej Kurekad837522021-12-15 15:28:49 +01005315 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5316 additional_data->len ),
5317 PSA_ERROR_BAD_STATE );
5318 }
5319 else
5320 {
5321 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5322 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005323
Andrzej Kurekad837522021-12-15 15:28:49 +01005324 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5325 input_data->len ),
5326 PSA_ERROR_BAD_STATE );
5327 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005328 psa_aead_abort( &operation );
5329
5330 /* ------------------------------------------------------- */
5331
Paul Elliottf94bd992021-09-19 18:15:59 +01005332 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5333
5334 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5335
Andrzej Kurekad837522021-12-15 15:28:49 +01005336 if( operation.alg == PSA_ALG_CCM )
5337 {
5338 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5339 input_data->len, output_data,
5340 output_size, &output_length ),
5341 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005342
Andrzej Kurekad837522021-12-15 15:28:49 +01005343 }
5344 else
5345 {
5346 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5347 input_data->len, output_data,
5348 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005349
Andrzej Kurekad837522021-12-15 15:28:49 +01005350 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5351 input_data->len ),
5352 PSA_ERROR_BAD_STATE );
5353 }
5354 psa_aead_abort( &operation );
5355
5356 /* ------------------------------------------------------- */
5357
5358 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5359
5360 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5361
5362 if( operation.alg == PSA_ALG_CCM )
5363 {
5364 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5365 finish_output_size,
5366 &output_part_length,
5367 tag_buffer, tag_length,
5368 &tag_size ) );
5369 }
5370 else
5371 {
5372 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5373 finish_output_size,
5374 &output_part_length,
5375 tag_buffer, tag_length,
5376 &tag_size ) );
5377
5378 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5379 input_data->len ),
5380 PSA_ERROR_BAD_STATE );
5381 }
5382 psa_aead_abort( &operation );
5383
5384 /* Test for setting lengths after generating nonce + already starting data. */
5385
5386 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5387
5388 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5389 PSA_AEAD_NONCE_MAX_SIZE,
5390 &nonce_length ) );
5391 if( operation.alg == PSA_ALG_CCM )
5392 {
5393
5394 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5395 additional_data->len ),
5396 PSA_ERROR_BAD_STATE );
5397 }
5398 else
5399 {
5400 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5401 additional_data->len ) );
5402
5403 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5404 input_data->len ),
5405 PSA_ERROR_BAD_STATE );
5406 }
5407 psa_aead_abort( &operation );
5408
5409 /* ------------------------------------------------------- */
5410
5411 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5412
5413 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5414 PSA_AEAD_NONCE_MAX_SIZE,
5415 &nonce_length ) );
5416 if( operation.alg == PSA_ALG_CCM )
5417 {
5418 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5419 input_data->len, output_data,
5420 output_size, &output_length ),
5421 PSA_ERROR_BAD_STATE );
5422
5423 }
5424 else
5425 {
5426 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5427 input_data->len, output_data,
5428 output_size, &output_length ) );
5429
5430 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5431 input_data->len ),
5432 PSA_ERROR_BAD_STATE );
5433 }
5434 psa_aead_abort( &operation );
5435
5436 /* ------------------------------------------------------- */
5437
5438 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5439
5440 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5441 PSA_AEAD_NONCE_MAX_SIZE,
5442 &nonce_length ) );
5443 if( operation.alg == PSA_ALG_CCM )
5444 {
5445 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5446 finish_output_size,
5447 &output_part_length,
5448 tag_buffer, tag_length,
5449 &tag_size ) );
5450 }
5451 else
5452 {
5453 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5454 finish_output_size,
5455 &output_part_length,
5456 tag_buffer, tag_length,
5457 &tag_size ) );
5458
5459 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5460 input_data->len ),
5461 PSA_ERROR_BAD_STATE );
5462 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005463 psa_aead_abort( &operation );
5464
Paul Elliott243080c2021-07-21 19:01:17 +01005465 /* Test for not sending any additional data or data after setting non zero
5466 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005467
Paul Elliottc23a9a02021-06-21 18:32:46 +01005468 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5469
5470 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5471
5472 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5473 input_data->len ) );
5474
5475 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5476 finish_output_size,
5477 &output_part_length,
5478 tag_buffer, tag_length,
5479 &tag_size ),
5480 PSA_ERROR_INVALID_ARGUMENT );
5481
5482 psa_aead_abort( &operation );
5483
Paul Elliott243080c2021-07-21 19:01:17 +01005484 /* Test for not sending any additional data or data after setting non-zero
5485 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005486
Paul Elliottc23a9a02021-06-21 18:32:46 +01005487 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5488
5489 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5490
5491 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5492 input_data->len ) );
5493
5494 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5495 finish_output_size,
5496 &output_part_length,
5497 tag_buffer,
5498 tag_length ),
5499 PSA_ERROR_INVALID_ARGUMENT );
5500
5501 psa_aead_abort( &operation );
5502
Paul Elliott243080c2021-07-21 19:01:17 +01005503 /* Test for not sending any additional data after setting a non-zero length
5504 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005505
Paul Elliottc23a9a02021-06-21 18:32:46 +01005506 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5507
5508 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5509
5510 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5511 input_data->len ) );
5512
5513 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5514 input_data->len, output_data,
5515 output_size, &output_length ),
5516 PSA_ERROR_INVALID_ARGUMENT );
5517
5518 psa_aead_abort( &operation );
5519
Paul Elliottf94bd992021-09-19 18:15:59 +01005520 /* Test for not sending any data after setting a non-zero length for it.*/
5521
Paul Elliottf94bd992021-09-19 18:15:59 +01005522 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5523
5524 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5525
5526 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5527 input_data->len ) );
5528
5529 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5530 additional_data->len ) );
5531
5532 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5533 finish_output_size,
5534 &output_part_length,
5535 tag_buffer, tag_length,
5536 &tag_size ),
5537 PSA_ERROR_INVALID_ARGUMENT );
5538
5539 psa_aead_abort( &operation );
5540
Paul Elliottb0450fe2021-09-01 15:06:26 +01005541 /* Test for sending too much additional data after setting lengths. */
5542
Paul Elliottb0450fe2021-09-01 15:06:26 +01005543 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5544
5545 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5546
5547 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5548
5549
5550 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5551 additional_data->len ),
5552 PSA_ERROR_INVALID_ARGUMENT );
5553
5554 psa_aead_abort( &operation );
5555
Paul Elliotta2a09b02021-09-22 14:56:40 +01005556 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005557
5558 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5559
5560 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5561
5562 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5563 input_data->len ) );
5564
5565 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5566 additional_data->len ) );
5567
5568 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5569 1 ),
5570 PSA_ERROR_INVALID_ARGUMENT );
5571
5572 psa_aead_abort( &operation );
5573
Paul Elliottb0450fe2021-09-01 15:06:26 +01005574 /* Test for sending too much data after setting lengths. */
5575
Paul Elliottb0450fe2021-09-01 15:06:26 +01005576 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5577
5578 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5579
5580 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5581
5582 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5583 input_data->len, output_data,
5584 output_size, &output_length ),
5585 PSA_ERROR_INVALID_ARGUMENT );
5586
5587 psa_aead_abort( &operation );
5588
Paul Elliotta2a09b02021-09-22 14:56:40 +01005589 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005590
5591 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5592
5593 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5594
5595 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5596 input_data->len ) );
5597
5598 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5599 additional_data->len ) );
5600
5601 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5602 input_data->len, output_data,
5603 output_size, &output_length ) );
5604
5605 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5606 1, output_data,
5607 output_size, &output_length ),
5608 PSA_ERROR_INVALID_ARGUMENT );
5609
5610 psa_aead_abort( &operation );
5611
Paul Elliottc23a9a02021-06-21 18:32:46 +01005612 /* Test sending additional data after data. */
5613
Paul Elliottc23a9a02021-06-21 18:32:46 +01005614 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5615
5616 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5617
Andrzej Kurekad837522021-12-15 15:28:49 +01005618 if( operation.alg != PSA_ALG_CCM )
5619 {
5620 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5621 input_data->len, output_data,
5622 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005623
Andrzej Kurekad837522021-12-15 15:28:49 +01005624 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5625 additional_data->len ),
5626 PSA_ERROR_BAD_STATE );
5627 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005628 psa_aead_abort( &operation );
5629
Paul Elliott534d0b42021-06-22 19:15:20 +01005630 /* Test calling finish on decryption. */
5631
Paul Elliott534d0b42021-06-22 19:15:20 +01005632 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5633
5634 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5635
5636 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5637 finish_output_size,
5638 &output_part_length,
5639 tag_buffer, tag_length,
5640 &tag_size ),
5641 PSA_ERROR_BAD_STATE );
5642
5643 psa_aead_abort( &operation );
5644
5645 /* Test calling verify on encryption. */
5646
Paul Elliott534d0b42021-06-22 19:15:20 +01005647 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5648
5649 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5650
5651 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5652 finish_output_size,
5653 &output_part_length,
5654 tag_buffer,
5655 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005656 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005657
5658 psa_aead_abort( &operation );
5659
5660
Paul Elliottc23a9a02021-06-21 18:32:46 +01005661exit:
5662 psa_destroy_key( key );
5663 psa_aead_abort( &operation );
5664 mbedtls_free( output_data );
5665 mbedtls_free( final_data );
5666 PSA_DONE( );
5667}
5668/* END_CASE */
5669
5670/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005671void signature_size( int type_arg,
5672 int bits,
5673 int alg_arg,
5674 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005675{
5676 psa_key_type_t type = type_arg;
5677 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005678 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005679
Gilles Peskinefe11b722018-12-18 00:24:04 +01005680 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005681
Gilles Peskinee59236f2018-01-27 23:32:46 +01005682exit:
5683 ;
5684}
5685/* END_CASE */
5686
5687/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005688void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5689 int alg_arg, data_t *input_data,
5690 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005691{
Ronald Cron5425a212020-08-04 14:58:35 +02005692 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005693 psa_key_type_t key_type = key_type_arg;
5694 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005695 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005696 unsigned char *signature = NULL;
5697 size_t signature_size;
5698 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005700
Gilles Peskine8817f612018-12-18 00:18:46 +01005701 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005702
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005703 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005704 psa_set_key_algorithm( &attributes, alg );
5705 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005706
Gilles Peskine049c7532019-05-15 20:22:09 +02005707 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005708 &key ) );
5709 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005710 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005711
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005712 /* Allocate a buffer which has the size advertized by the
5713 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005714 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005715 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005716 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005717 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005718 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005719
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005720 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005721 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005722 input_data->x, input_data->len,
5723 signature, signature_size,
5724 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005725 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005726 ASSERT_COMPARE( output_data->x, output_data->len,
5727 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005728
5729exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005730 /*
5731 * Key attributes may have been returned by psa_get_key_attributes()
5732 * thus reset them as required.
5733 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005734 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005735
Ronald Cron5425a212020-08-04 14:58:35 +02005736 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005737 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005738 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005739}
5740/* END_CASE */
5741
5742/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005743void sign_hash_fail( int key_type_arg, data_t *key_data,
5744 int alg_arg, data_t *input_data,
5745 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005746{
Ronald Cron5425a212020-08-04 14:58:35 +02005747 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005748 psa_key_type_t key_type = key_type_arg;
5749 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005750 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005751 psa_status_t actual_status;
5752 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005753 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005754 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005755 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005756
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005757 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005758
Gilles Peskine8817f612018-12-18 00:18:46 +01005759 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005760
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005761 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005762 psa_set_key_algorithm( &attributes, alg );
5763 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005764
Gilles Peskine049c7532019-05-15 20:22:09 +02005765 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005766 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005767
Ronald Cron5425a212020-08-04 14:58:35 +02005768 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005769 input_data->x, input_data->len,
5770 signature, signature_size,
5771 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005772 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005773 /* The value of *signature_length is unspecified on error, but
5774 * whatever it is, it should be less than signature_size, so that
5775 * if the caller tries to read *signature_length bytes without
5776 * checking the error code then they don't overflow a buffer. */
5777 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005778
5779exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005780 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005781 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005782 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005783 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005784}
5785/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005786
5787/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005788void sign_verify_hash( int key_type_arg, data_t *key_data,
5789 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005790{
Ronald Cron5425a212020-08-04 14:58:35 +02005791 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005792 psa_key_type_t key_type = key_type_arg;
5793 psa_algorithm_t alg = alg_arg;
5794 size_t key_bits;
5795 unsigned char *signature = NULL;
5796 size_t signature_size;
5797 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005799
Gilles Peskine8817f612018-12-18 00:18:46 +01005800 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005801
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005802 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005803 psa_set_key_algorithm( &attributes, alg );
5804 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005805
Gilles Peskine049c7532019-05-15 20:22:09 +02005806 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005807 &key ) );
5808 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005809 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005810
5811 /* Allocate a buffer which has the size advertized by the
5812 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005813 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005814 key_bits, alg );
5815 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005816 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005817 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005818
5819 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005820 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005821 input_data->x, input_data->len,
5822 signature, signature_size,
5823 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005824 /* Check that the signature length looks sensible. */
5825 TEST_ASSERT( signature_length <= signature_size );
5826 TEST_ASSERT( signature_length > 0 );
5827
5828 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005829 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005830 input_data->x, input_data->len,
5831 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005832
5833 if( input_data->len != 0 )
5834 {
5835 /* Flip a bit in the input and verify that the signature is now
5836 * detected as invalid. Flip a bit at the beginning, not at the end,
5837 * because ECDSA may ignore the last few bits of the input. */
5838 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005839 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005840 input_data->x, input_data->len,
5841 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005842 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005843 }
5844
5845exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005846 /*
5847 * Key attributes may have been returned by psa_get_key_attributes()
5848 * thus reset them as required.
5849 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005850 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005851
Ronald Cron5425a212020-08-04 14:58:35 +02005852 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005853 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005854 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005855}
5856/* END_CASE */
5857
5858/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005859void verify_hash( int key_type_arg, data_t *key_data,
5860 int alg_arg, data_t *hash_data,
5861 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005862{
Ronald Cron5425a212020-08-04 14:58:35 +02005863 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005864 psa_key_type_t key_type = key_type_arg;
5865 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005866 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005867
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005868 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005869
Gilles Peskine8817f612018-12-18 00:18:46 +01005870 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005871
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005872 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005873 psa_set_key_algorithm( &attributes, alg );
5874 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005875
Gilles Peskine049c7532019-05-15 20:22:09 +02005876 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005877 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005878
Ronald Cron5425a212020-08-04 14:58:35 +02005879 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005880 hash_data->x, hash_data->len,
5881 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005882
itayzafrir5c753392018-05-08 11:18:38 +03005883exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005884 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005885 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005886 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005887}
5888/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005889
5890/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005891void verify_hash_fail( int key_type_arg, data_t *key_data,
5892 int alg_arg, data_t *hash_data,
5893 data_t *signature_data,
5894 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005895{
Ronald Cron5425a212020-08-04 14:58:35 +02005896 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005897 psa_key_type_t key_type = key_type_arg;
5898 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005899 psa_status_t actual_status;
5900 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005902
Gilles Peskine8817f612018-12-18 00:18:46 +01005903 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005904
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005905 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005906 psa_set_key_algorithm( &attributes, alg );
5907 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005908
Gilles Peskine049c7532019-05-15 20:22:09 +02005909 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005910 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005911
Ronald Cron5425a212020-08-04 14:58:35 +02005912 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005913 hash_data->x, hash_data->len,
5914 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005915 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005916
5917exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005918 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005919 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005920 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005921}
5922/* END_CASE */
5923
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005924/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005925void sign_message_deterministic( int key_type_arg,
5926 data_t *key_data,
5927 int alg_arg,
5928 data_t *input_data,
5929 data_t *output_data )
5930{
5931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5932 psa_key_type_t key_type = key_type_arg;
5933 psa_algorithm_t alg = alg_arg;
5934 size_t key_bits;
5935 unsigned char *signature = NULL;
5936 size_t signature_size;
5937 size_t signature_length = 0xdeadbeef;
5938 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5939
5940 PSA_ASSERT( psa_crypto_init( ) );
5941
5942 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5943 psa_set_key_algorithm( &attributes, alg );
5944 psa_set_key_type( &attributes, key_type );
5945
5946 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5947 &key ) );
5948 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5949 key_bits = psa_get_key_bits( &attributes );
5950
5951 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5952 TEST_ASSERT( signature_size != 0 );
5953 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5954 ASSERT_ALLOC( signature, signature_size );
5955
5956 PSA_ASSERT( psa_sign_message( key, alg,
5957 input_data->x, input_data->len,
5958 signature, signature_size,
5959 &signature_length ) );
5960
5961 ASSERT_COMPARE( output_data->x, output_data->len,
5962 signature, signature_length );
5963
5964exit:
5965 psa_reset_key_attributes( &attributes );
5966
5967 psa_destroy_key( key );
5968 mbedtls_free( signature );
5969 PSA_DONE( );
5970
5971}
5972/* END_CASE */
5973
5974/* BEGIN_CASE */
5975void sign_message_fail( int key_type_arg,
5976 data_t *key_data,
5977 int alg_arg,
5978 data_t *input_data,
5979 int signature_size_arg,
5980 int expected_status_arg )
5981{
5982 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5983 psa_key_type_t key_type = key_type_arg;
5984 psa_algorithm_t alg = alg_arg;
5985 size_t signature_size = signature_size_arg;
5986 psa_status_t actual_status;
5987 psa_status_t expected_status = expected_status_arg;
5988 unsigned char *signature = NULL;
5989 size_t signature_length = 0xdeadbeef;
5990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5991
5992 ASSERT_ALLOC( signature, signature_size );
5993
5994 PSA_ASSERT( psa_crypto_init( ) );
5995
5996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5997 psa_set_key_algorithm( &attributes, alg );
5998 psa_set_key_type( &attributes, key_type );
5999
6000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6001 &key ) );
6002
6003 actual_status = psa_sign_message( key, alg,
6004 input_data->x, input_data->len,
6005 signature, signature_size,
6006 &signature_length );
6007 TEST_EQUAL( actual_status, expected_status );
6008 /* The value of *signature_length is unspecified on error, but
6009 * whatever it is, it should be less than signature_size, so that
6010 * if the caller tries to read *signature_length bytes without
6011 * checking the error code then they don't overflow a buffer. */
6012 TEST_ASSERT( signature_length <= signature_size );
6013
6014exit:
6015 psa_reset_key_attributes( &attributes );
6016 psa_destroy_key( key );
6017 mbedtls_free( signature );
6018 PSA_DONE( );
6019}
6020/* END_CASE */
6021
6022/* BEGIN_CASE */
6023void sign_verify_message( int key_type_arg,
6024 data_t *key_data,
6025 int alg_arg,
6026 data_t *input_data )
6027{
6028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6029 psa_key_type_t key_type = key_type_arg;
6030 psa_algorithm_t alg = alg_arg;
6031 size_t key_bits;
6032 unsigned char *signature = NULL;
6033 size_t signature_size;
6034 size_t signature_length = 0xdeadbeef;
6035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6036
6037 PSA_ASSERT( psa_crypto_init( ) );
6038
6039 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6040 PSA_KEY_USAGE_VERIFY_MESSAGE );
6041 psa_set_key_algorithm( &attributes, alg );
6042 psa_set_key_type( &attributes, key_type );
6043
6044 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6045 &key ) );
6046 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6047 key_bits = psa_get_key_bits( &attributes );
6048
6049 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6050 TEST_ASSERT( signature_size != 0 );
6051 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
6052 ASSERT_ALLOC( signature, signature_size );
6053
6054 PSA_ASSERT( psa_sign_message( key, alg,
6055 input_data->x, input_data->len,
6056 signature, signature_size,
6057 &signature_length ) );
6058 TEST_ASSERT( signature_length <= signature_size );
6059 TEST_ASSERT( signature_length > 0 );
6060
6061 PSA_ASSERT( psa_verify_message( key, alg,
6062 input_data->x, input_data->len,
6063 signature, signature_length ) );
6064
6065 if( input_data->len != 0 )
6066 {
6067 /* Flip a bit in the input and verify that the signature is now
6068 * detected as invalid. Flip a bit at the beginning, not at the end,
6069 * because ECDSA may ignore the last few bits of the input. */
6070 input_data->x[0] ^= 1;
6071 TEST_EQUAL( psa_verify_message( key, alg,
6072 input_data->x, input_data->len,
6073 signature, signature_length ),
6074 PSA_ERROR_INVALID_SIGNATURE );
6075 }
6076
6077exit:
6078 psa_reset_key_attributes( &attributes );
6079
6080 psa_destroy_key( key );
6081 mbedtls_free( signature );
6082 PSA_DONE( );
6083}
6084/* END_CASE */
6085
6086/* BEGIN_CASE */
6087void verify_message( int key_type_arg,
6088 data_t *key_data,
6089 int alg_arg,
6090 data_t *input_data,
6091 data_t *signature_data )
6092{
6093 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6094 psa_key_type_t key_type = key_type_arg;
6095 psa_algorithm_t alg = alg_arg;
6096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6097
6098 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
6099
6100 PSA_ASSERT( psa_crypto_init( ) );
6101
6102 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6103 psa_set_key_algorithm( &attributes, alg );
6104 psa_set_key_type( &attributes, key_type );
6105
6106 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6107 &key ) );
6108
6109 PSA_ASSERT( psa_verify_message( key, alg,
6110 input_data->x, input_data->len,
6111 signature_data->x, signature_data->len ) );
6112
6113exit:
6114 psa_reset_key_attributes( &attributes );
6115 psa_destroy_key( key );
6116 PSA_DONE( );
6117}
6118/* END_CASE */
6119
6120/* BEGIN_CASE */
6121void verify_message_fail( int key_type_arg,
6122 data_t *key_data,
6123 int alg_arg,
6124 data_t *hash_data,
6125 data_t *signature_data,
6126 int expected_status_arg )
6127{
6128 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6129 psa_key_type_t key_type = key_type_arg;
6130 psa_algorithm_t alg = alg_arg;
6131 psa_status_t actual_status;
6132 psa_status_t expected_status = expected_status_arg;
6133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6134
6135 PSA_ASSERT( psa_crypto_init( ) );
6136
6137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6138 psa_set_key_algorithm( &attributes, alg );
6139 psa_set_key_type( &attributes, key_type );
6140
6141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6142 &key ) );
6143
6144 actual_status = psa_verify_message( key, alg,
6145 hash_data->x, hash_data->len,
6146 signature_data->x,
6147 signature_data->len );
6148 TEST_EQUAL( actual_status, expected_status );
6149
6150exit:
6151 psa_reset_key_attributes( &attributes );
6152 psa_destroy_key( key );
6153 PSA_DONE( );
6154}
6155/* END_CASE */
6156
6157/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006158void asymmetric_encrypt( int key_type_arg,
6159 data_t *key_data,
6160 int alg_arg,
6161 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006162 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006163 int expected_output_length_arg,
6164 int expected_status_arg )
6165{
Ronald Cron5425a212020-08-04 14:58:35 +02006166 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006167 psa_key_type_t key_type = key_type_arg;
6168 psa_algorithm_t alg = alg_arg;
6169 size_t expected_output_length = expected_output_length_arg;
6170 size_t key_bits;
6171 unsigned char *output = NULL;
6172 size_t output_size;
6173 size_t output_length = ~0;
6174 psa_status_t actual_status;
6175 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006177
Gilles Peskine8817f612018-12-18 00:18:46 +01006178 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006179
Gilles Peskine656896e2018-06-29 19:12:28 +02006180 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006181 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6182 psa_set_key_algorithm( &attributes, alg );
6183 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006184 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006185 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006186
6187 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006188 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006189 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006190
Gilles Peskine656896e2018-06-29 19:12:28 +02006191 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006192 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006193 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006194
6195 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006196 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006197 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006198 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006199 output, output_size,
6200 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006201 TEST_EQUAL( actual_status, expected_status );
6202 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006203
Gilles Peskine68428122018-06-30 18:42:41 +02006204 /* If the label is empty, the test framework puts a non-null pointer
6205 * in label->x. Test that a null pointer works as well. */
6206 if( label->len == 0 )
6207 {
6208 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006209 if( output_size != 0 )
6210 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006211 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006212 input_data->x, input_data->len,
6213 NULL, label->len,
6214 output, output_size,
6215 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006216 TEST_EQUAL( actual_status, expected_status );
6217 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006218 }
6219
Gilles Peskine656896e2018-06-29 19:12:28 +02006220exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006221 /*
6222 * Key attributes may have been returned by psa_get_key_attributes()
6223 * thus reset them as required.
6224 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006225 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006226
Ronald Cron5425a212020-08-04 14:58:35 +02006227 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006228 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006229 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006230}
6231/* END_CASE */
6232
6233/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006234void asymmetric_encrypt_decrypt( int key_type_arg,
6235 data_t *key_data,
6236 int alg_arg,
6237 data_t *input_data,
6238 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006239{
Ronald Cron5425a212020-08-04 14:58:35 +02006240 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006241 psa_key_type_t key_type = key_type_arg;
6242 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006243 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006244 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006245 size_t output_size;
6246 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006247 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006248 size_t output2_size;
6249 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006251
Gilles Peskine8817f612018-12-18 00:18:46 +01006252 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006253
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006254 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6255 psa_set_key_algorithm( &attributes, alg );
6256 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006257
Gilles Peskine049c7532019-05-15 20:22:09 +02006258 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006259 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006260
6261 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006262 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006263 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006264
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006265 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006266 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006267 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006268
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006269 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006270 TEST_ASSERT( output2_size <=
6271 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6272 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006273 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006274
Gilles Peskineeebd7382018-06-08 18:11:54 +02006275 /* We test encryption by checking that encrypt-then-decrypt gives back
6276 * the original plaintext because of the non-optional random
6277 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006278 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006279 input_data->x, input_data->len,
6280 label->x, label->len,
6281 output, output_size,
6282 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006283 /* We don't know what ciphertext length to expect, but check that
6284 * it looks sensible. */
6285 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006286
Ronald Cron5425a212020-08-04 14:58:35 +02006287 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006288 output, output_length,
6289 label->x, label->len,
6290 output2, output2_size,
6291 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006292 ASSERT_COMPARE( input_data->x, input_data->len,
6293 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006294
6295exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006296 /*
6297 * Key attributes may have been returned by psa_get_key_attributes()
6298 * thus reset them as required.
6299 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006300 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006301
Ronald Cron5425a212020-08-04 14:58:35 +02006302 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006303 mbedtls_free( output );
6304 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006305 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006306}
6307/* END_CASE */
6308
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006309/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006310void asymmetric_decrypt( int key_type_arg,
6311 data_t *key_data,
6312 int alg_arg,
6313 data_t *input_data,
6314 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006315 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006316{
Ronald Cron5425a212020-08-04 14:58:35 +02006317 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006318 psa_key_type_t key_type = key_type_arg;
6319 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006320 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006321 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006322 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006323 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006325
Gilles Peskine8817f612018-12-18 00:18:46 +01006326 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006327
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006328 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6329 psa_set_key_algorithm( &attributes, alg );
6330 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006331
Gilles Peskine049c7532019-05-15 20:22:09 +02006332 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006333 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006334
gabor-mezei-armceface22021-01-21 12:26:17 +01006335 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6336 key_bits = psa_get_key_bits( &attributes );
6337
6338 /* Determine the maximum ciphertext length */
6339 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6340 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6341 ASSERT_ALLOC( output, output_size );
6342
Ronald Cron5425a212020-08-04 14:58:35 +02006343 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006344 input_data->x, input_data->len,
6345 label->x, label->len,
6346 output,
6347 output_size,
6348 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006349 ASSERT_COMPARE( expected_data->x, expected_data->len,
6350 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006351
Gilles Peskine68428122018-06-30 18:42:41 +02006352 /* If the label is empty, the test framework puts a non-null pointer
6353 * in label->x. Test that a null pointer works as well. */
6354 if( label->len == 0 )
6355 {
6356 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006357 if( output_size != 0 )
6358 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006359 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006360 input_data->x, input_data->len,
6361 NULL, label->len,
6362 output,
6363 output_size,
6364 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006365 ASSERT_COMPARE( expected_data->x, expected_data->len,
6366 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006367 }
6368
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006369exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006370 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006371 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006372 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006373 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006374}
6375/* END_CASE */
6376
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006377/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006378void asymmetric_decrypt_fail( int key_type_arg,
6379 data_t *key_data,
6380 int alg_arg,
6381 data_t *input_data,
6382 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006383 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006384 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006385{
Ronald Cron5425a212020-08-04 14:58:35 +02006386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006387 psa_key_type_t key_type = key_type_arg;
6388 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006389 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006390 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006391 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006392 psa_status_t actual_status;
6393 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006394 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006395
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006396 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006397
Gilles Peskine8817f612018-12-18 00:18:46 +01006398 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006399
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006400 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6401 psa_set_key_algorithm( &attributes, alg );
6402 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006403
Gilles Peskine049c7532019-05-15 20:22:09 +02006404 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006405 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006406
Ronald Cron5425a212020-08-04 14:58:35 +02006407 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006408 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006409 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006410 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006411 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006412 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006413 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006414
Gilles Peskine68428122018-06-30 18:42:41 +02006415 /* If the label is empty, the test framework puts a non-null pointer
6416 * in label->x. Test that a null pointer works as well. */
6417 if( label->len == 0 )
6418 {
6419 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006420 if( output_size != 0 )
6421 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006422 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006423 input_data->x, input_data->len,
6424 NULL, label->len,
6425 output, output_size,
6426 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006427 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006428 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006429 }
6430
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006431exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006432 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006433 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006434 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006435 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006436}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006437/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006438
6439/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006440void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006441{
6442 /* Test each valid way of initializing the object, except for `= {0}`, as
6443 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6444 * though it's OK by the C standard. We could test for this, but we'd need
6445 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006446 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006447 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6448 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6449 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006450
6451 memset( &zero, 0, sizeof( zero ) );
6452
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006453 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006454 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006455 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006456 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006457 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006458 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006459 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006460
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006461 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006462 PSA_ASSERT( psa_key_derivation_abort(&func) );
6463 PSA_ASSERT( psa_key_derivation_abort(&init) );
6464 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006465}
6466/* END_CASE */
6467
Janos Follath16de4a42019-06-13 16:32:24 +01006468/* BEGIN_CASE */
6469void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006470{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006471 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006472 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006473 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006474
Gilles Peskine8817f612018-12-18 00:18:46 +01006475 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006476
Janos Follath16de4a42019-06-13 16:32:24 +01006477 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006478 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006479
6480exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006481 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006482 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006483}
6484/* END_CASE */
6485
Janos Follathaf3c2a02019-06-12 12:34:34 +01006486/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006487void derive_set_capacity( int alg_arg, int capacity_arg,
6488 int expected_status_arg )
6489{
6490 psa_algorithm_t alg = alg_arg;
6491 size_t capacity = capacity_arg;
6492 psa_status_t expected_status = expected_status_arg;
6493 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6494
6495 PSA_ASSERT( psa_crypto_init( ) );
6496
6497 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6498
6499 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6500 expected_status );
6501
6502exit:
6503 psa_key_derivation_abort( &operation );
6504 PSA_DONE( );
6505}
6506/* END_CASE */
6507
6508/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006509void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006510 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006511 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006512 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006513 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006514 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006515 int expected_status_arg3,
6516 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006517{
6518 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006519 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6520 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006521 psa_status_t expected_statuses[] = {expected_status_arg1,
6522 expected_status_arg2,
6523 expected_status_arg3};
6524 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006525 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6526 MBEDTLS_SVC_KEY_ID_INIT,
6527 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006528 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6530 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006531 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006532 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006533 psa_status_t expected_output_status = expected_output_status_arg;
6534 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006535
6536 PSA_ASSERT( psa_crypto_init( ) );
6537
6538 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6539 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006540
6541 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6542
6543 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6544 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006545 mbedtls_test_set_step( i );
6546 if( steps[i] == 0 )
6547 {
6548 /* Skip this step */
6549 }
6550 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006551 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006552 psa_set_key_type( &attributes, key_types[i] );
6553 PSA_ASSERT( psa_import_key( &attributes,
6554 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006555 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006556 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6557 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6558 {
6559 // When taking a private key as secret input, use key agreement
6560 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006561 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6562 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006563 expected_statuses[i] );
6564 }
6565 else
6566 {
6567 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006568 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006569 expected_statuses[i] );
6570 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006571 }
6572 else
6573 {
6574 TEST_EQUAL( psa_key_derivation_input_bytes(
6575 &operation, steps[i],
6576 inputs[i]->x, inputs[i]->len ),
6577 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006578 }
6579 }
6580
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006581 if( output_key_type != PSA_KEY_TYPE_NONE )
6582 {
6583 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006584 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006585 psa_set_key_bits( &attributes, 8 );
6586 actual_output_status =
6587 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006588 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006589 }
6590 else
6591 {
6592 uint8_t buffer[1];
6593 actual_output_status =
6594 psa_key_derivation_output_bytes( &operation,
6595 buffer, sizeof( buffer ) );
6596 }
6597 TEST_EQUAL( actual_output_status, expected_output_status );
6598
Janos Follathaf3c2a02019-06-12 12:34:34 +01006599exit:
6600 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006601 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6602 psa_destroy_key( keys[i] );
6603 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006604 PSA_DONE( );
6605}
6606/* END_CASE */
6607
Janos Follathd958bb72019-07-03 15:02:16 +01006608/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006609void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006610{
Janos Follathd958bb72019-07-03 15:02:16 +01006611 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006613 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006614 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006615 unsigned char input1[] = "Input 1";
6616 size_t input1_length = sizeof( input1 );
6617 unsigned char input2[] = "Input 2";
6618 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006619 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006620 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006621 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6622 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6623 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006625
Gilles Peskine8817f612018-12-18 00:18:46 +01006626 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006627
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006628 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6629 psa_set_key_algorithm( &attributes, alg );
6630 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006631
Gilles Peskine73676cb2019-05-15 20:15:10 +02006632 PSA_ASSERT( psa_import_key( &attributes,
6633 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006634 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006635
6636 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006637 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6638 input1, input1_length,
6639 input2, input2_length,
6640 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006641 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006642
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006643 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006644 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006645 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006646
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006647 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006648
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006649 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006650 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006651
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006652exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006653 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006654 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006655 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006656}
6657/* END_CASE */
6658
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006659/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006660void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006661{
6662 uint8_t output_buffer[16];
6663 size_t buffer_size = 16;
6664 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006665 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006666
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006667 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6668 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006669 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006670
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006671 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006672 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006673
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006674 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006675
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006676 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6677 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006678 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006679
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006680 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006681 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006682
6683exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006684 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006685}
6686/* END_CASE */
6687
6688/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006689void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006690 int step1_arg, data_t *input1,
6691 int step2_arg, data_t *input2,
6692 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006693 int requested_capacity_arg,
6694 data_t *expected_output1,
6695 data_t *expected_output2 )
6696{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006697 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006698 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6699 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006700 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6701 MBEDTLS_SVC_KEY_ID_INIT,
6702 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006703 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006704 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006705 uint8_t *expected_outputs[2] =
6706 {expected_output1->x, expected_output2->x};
6707 size_t output_sizes[2] =
6708 {expected_output1->len, expected_output2->len};
6709 size_t output_buffer_size = 0;
6710 uint8_t *output_buffer = NULL;
6711 size_t expected_capacity;
6712 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006713 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006714 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006715 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006716
6717 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6718 {
6719 if( output_sizes[i] > output_buffer_size )
6720 output_buffer_size = output_sizes[i];
6721 if( output_sizes[i] == 0 )
6722 expected_outputs[i] = NULL;
6723 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006724 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006725 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006726
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006727 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6728 psa_set_key_algorithm( &attributes, alg );
6729 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006730
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006731 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006732 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6733 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6734 requested_capacity ) );
6735 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006736 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006737 switch( steps[i] )
6738 {
6739 case 0:
6740 break;
6741 case PSA_KEY_DERIVATION_INPUT_SECRET:
6742 PSA_ASSERT( psa_import_key( &attributes,
6743 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006744 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006745
6746 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6747 {
6748 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6749 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6750 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6751 }
6752
Gilles Peskine1468da72019-05-29 17:35:49 +02006753 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006754 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006755 break;
6756 default:
6757 PSA_ASSERT( psa_key_derivation_input_bytes(
6758 &operation, steps[i],
6759 inputs[i]->x, inputs[i]->len ) );
6760 break;
6761 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006762 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006763
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006764 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006765 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006766 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006767 expected_capacity = requested_capacity;
6768
6769 /* Expansion phase. */
6770 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6771 {
6772 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006773 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006774 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006775 if( expected_capacity == 0 && output_sizes[i] == 0 )
6776 {
6777 /* Reading 0 bytes when 0 bytes are available can go either way. */
6778 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006779 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006780 continue;
6781 }
6782 else if( expected_capacity == 0 ||
6783 output_sizes[i] > expected_capacity )
6784 {
6785 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006786 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006787 expected_capacity = 0;
6788 continue;
6789 }
6790 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006791 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006792 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006793 ASSERT_COMPARE( output_buffer, output_sizes[i],
6794 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006795 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006796 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006797 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006798 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006799 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006800 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006801 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006802
6803exit:
6804 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006805 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006806 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6807 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006808 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006809}
6810/* END_CASE */
6811
6812/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006813void derive_full( int alg_arg,
6814 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006815 data_t *input1,
6816 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006817 int requested_capacity_arg )
6818{
Ronald Cron5425a212020-08-04 14:58:35 +02006819 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006820 psa_algorithm_t alg = alg_arg;
6821 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006822 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006823 unsigned char output_buffer[16];
6824 size_t expected_capacity = requested_capacity;
6825 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006826 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006827
Gilles Peskine8817f612018-12-18 00:18:46 +01006828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006829
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006830 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6831 psa_set_key_algorithm( &attributes, alg );
6832 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006833
Gilles Peskine049c7532019-05-15 20:22:09 +02006834 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006835 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006836
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006837 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6838 input1->x, input1->len,
6839 input2->x, input2->len,
6840 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006841 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006842
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006843 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006844 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006845 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006846
6847 /* Expansion phase. */
6848 while( current_capacity > 0 )
6849 {
6850 size_t read_size = sizeof( output_buffer );
6851 if( read_size > current_capacity )
6852 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006853 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006854 output_buffer,
6855 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006856 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006857 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006858 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006859 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006860 }
6861
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006862 /* Check that the operation refuses to go over capacity. */
6863 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006864 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006865
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006866 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006867
6868exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006869 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006870 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006871 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006872}
6873/* END_CASE */
6874
Janos Follathe60c9052019-07-03 13:51:30 +01006875/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006876void derive_key_exercise( int alg_arg,
6877 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006878 data_t *input1,
6879 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006880 int derived_type_arg,
6881 int derived_bits_arg,
6882 int derived_usage_arg,
6883 int derived_alg_arg )
6884{
Ronald Cron5425a212020-08-04 14:58:35 +02006885 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6886 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006887 psa_algorithm_t alg = alg_arg;
6888 psa_key_type_t derived_type = derived_type_arg;
6889 size_t derived_bits = derived_bits_arg;
6890 psa_key_usage_t derived_usage = derived_usage_arg;
6891 psa_algorithm_t derived_alg = derived_alg_arg;
6892 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006893 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006895 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006896
Gilles Peskine8817f612018-12-18 00:18:46 +01006897 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006898
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006899 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6900 psa_set_key_algorithm( &attributes, alg );
6901 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006902 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006903 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006904
6905 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006906 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6907 input1->x, input1->len,
6908 input2->x, input2->len,
6909 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006910 goto exit;
6911
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006912 psa_set_key_usage_flags( &attributes, derived_usage );
6913 psa_set_key_algorithm( &attributes, derived_alg );
6914 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006915 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006916 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006917 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006918
6919 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006920 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006921 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6922 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006923
6924 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006925 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006926 goto exit;
6927
6928exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006929 /*
6930 * Key attributes may have been returned by psa_get_key_attributes()
6931 * thus reset them as required.
6932 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006933 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006934
6935 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006936 psa_destroy_key( base_key );
6937 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006938 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006939}
6940/* END_CASE */
6941
Janos Follath42fd8882019-07-03 14:17:09 +01006942/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006943void derive_key_export( int alg_arg,
6944 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006945 data_t *input1,
6946 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006947 int bytes1_arg,
6948 int bytes2_arg )
6949{
Ronald Cron5425a212020-08-04 14:58:35 +02006950 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6951 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006952 psa_algorithm_t alg = alg_arg;
6953 size_t bytes1 = bytes1_arg;
6954 size_t bytes2 = bytes2_arg;
6955 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006956 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006957 uint8_t *output_buffer = NULL;
6958 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006959 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6960 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006961 size_t length;
6962
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006963 ASSERT_ALLOC( output_buffer, capacity );
6964 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006965 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006966
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006967 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6968 psa_set_key_algorithm( &base_attributes, alg );
6969 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006970 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006971 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006972
6973 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006974 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6975 input1->x, input1->len,
6976 input2->x, input2->len,
6977 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006978 goto exit;
6979
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006980 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006981 output_buffer,
6982 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006983 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006984
6985 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006986 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6987 input1->x, input1->len,
6988 input2->x, input2->len,
6989 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006990 goto exit;
6991
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006992 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6993 psa_set_key_algorithm( &derived_attributes, 0 );
6994 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006995 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006996 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006997 &derived_key ) );
6998 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006999 export_buffer, bytes1,
7000 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007001 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007002 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007003 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007004 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007005 &derived_key ) );
7006 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007007 export_buffer + bytes1, bytes2,
7008 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007009 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007010
7011 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007012 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7013 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007014
7015exit:
7016 mbedtls_free( output_buffer );
7017 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007018 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007019 psa_destroy_key( base_key );
7020 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007021 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007022}
7023/* END_CASE */
7024
7025/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007026void derive_key( int alg_arg,
7027 data_t *key_data, data_t *input1, data_t *input2,
7028 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007029 int expected_status_arg,
7030 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02007031{
Ronald Cron5425a212020-08-04 14:58:35 +02007032 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7033 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02007034 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007035 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02007036 size_t bits = bits_arg;
7037 psa_status_t expected_status = expected_status_arg;
7038 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7039 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7040 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7041
7042 PSA_ASSERT( psa_crypto_init( ) );
7043
7044 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7045 psa_set_key_algorithm( &base_attributes, alg );
7046 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7047 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007048 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02007049
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007050 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7051 input1->x, input1->len,
7052 input2->x, input2->len,
7053 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02007054 goto exit;
7055
7056 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7057 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007058 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02007059 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01007060
7061 psa_status_t status =
7062 psa_key_derivation_output_key( &derived_attributes,
7063 &operation,
7064 &derived_key );
7065 if( is_large_output > 0 )
7066 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7067 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02007068
7069exit:
7070 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007071 psa_destroy_key( base_key );
7072 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02007073 PSA_DONE( );
7074}
7075/* END_CASE */
7076
7077/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02007078void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02007079 int our_key_type_arg, int our_key_alg_arg,
7080 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02007081 int expected_status_arg )
7082{
Ronald Cron5425a212020-08-04 14:58:35 +02007083 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007084 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007085 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007086 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007087 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007089 psa_status_t expected_status = expected_status_arg;
7090 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007091
Gilles Peskine8817f612018-12-18 00:18:46 +01007092 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007093
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007094 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007095 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007096 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007097 PSA_ASSERT( psa_import_key( &attributes,
7098 our_key_data->x, our_key_data->len,
7099 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007100
Gilles Peskine77f40d82019-04-11 21:27:06 +02007101 /* The tests currently include inputs that should fail at either step.
7102 * Test cases that fail at the setup step should be changed to call
7103 * key_derivation_setup instead, and this function should be renamed
7104 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007105 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02007106 if( status == PSA_SUCCESS )
7107 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007108 TEST_EQUAL( psa_key_derivation_key_agreement(
7109 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
7110 our_key,
7111 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02007112 expected_status );
7113 }
7114 else
7115 {
7116 TEST_ASSERT( status == expected_status );
7117 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007118
7119exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007120 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007121 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007122 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007123}
7124/* END_CASE */
7125
7126/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007127void raw_key_agreement( int alg_arg,
7128 int our_key_type_arg, data_t *our_key_data,
7129 data_t *peer_key_data,
7130 data_t *expected_output )
7131{
Ronald Cron5425a212020-08-04 14:58:35 +02007132 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007133 psa_algorithm_t alg = alg_arg;
7134 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007136 unsigned char *output = NULL;
7137 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007138 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007139
7140 ASSERT_ALLOC( output, expected_output->len );
7141 PSA_ASSERT( psa_crypto_init( ) );
7142
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007143 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7144 psa_set_key_algorithm( &attributes, alg );
7145 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007146 PSA_ASSERT( psa_import_key( &attributes,
7147 our_key_data->x, our_key_data->len,
7148 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007149
gabor-mezei-armceface22021-01-21 12:26:17 +01007150 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7151 key_bits = psa_get_key_bits( &attributes );
7152
Gilles Peskinebe697d82019-05-16 18:00:41 +02007153 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7154 peer_key_data->x, peer_key_data->len,
7155 output, expected_output->len,
7156 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007157 ASSERT_COMPARE( output, output_length,
7158 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01007159 TEST_ASSERT( output_length <=
7160 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7161 TEST_ASSERT( output_length <=
7162 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007163
7164exit:
7165 mbedtls_free( output );
7166 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007167 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007168}
7169/* END_CASE */
7170
7171/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007172void key_agreement_capacity( int alg_arg,
7173 int our_key_type_arg, data_t *our_key_data,
7174 data_t *peer_key_data,
7175 int expected_capacity_arg )
7176{
Ronald Cron5425a212020-08-04 14:58:35 +02007177 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007178 psa_algorithm_t alg = alg_arg;
7179 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007180 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007181 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007182 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007183 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007184
Gilles Peskine8817f612018-12-18 00:18:46 +01007185 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007186
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007187 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7188 psa_set_key_algorithm( &attributes, alg );
7189 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007190 PSA_ASSERT( psa_import_key( &attributes,
7191 our_key_data->x, our_key_data->len,
7192 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007193
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007194 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007195 PSA_ASSERT( psa_key_derivation_key_agreement(
7196 &operation,
7197 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7198 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007199 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7200 {
7201 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007202 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007203 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007204 NULL, 0 ) );
7205 }
Gilles Peskine59685592018-09-18 12:11:34 +02007206
Gilles Peskinebf491972018-10-25 22:36:12 +02007207 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007208 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007209 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007210 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007211
Gilles Peskinebf491972018-10-25 22:36:12 +02007212 /* Test the actual capacity by reading the output. */
7213 while( actual_capacity > sizeof( output ) )
7214 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007215 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007216 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007217 actual_capacity -= sizeof( output );
7218 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007219 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007220 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007221 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007222 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007223
Gilles Peskine59685592018-09-18 12:11:34 +02007224exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007225 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007226 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007227 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007228}
7229/* END_CASE */
7230
7231/* BEGIN_CASE */
7232void key_agreement_output( int alg_arg,
7233 int our_key_type_arg, data_t *our_key_data,
7234 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007235 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007236{
Ronald Cron5425a212020-08-04 14:58:35 +02007237 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007238 psa_algorithm_t alg = alg_arg;
7239 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007240 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007242 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007243
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007244 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7245 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007246
Gilles Peskine8817f612018-12-18 00:18:46 +01007247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007248
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007249 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7250 psa_set_key_algorithm( &attributes, alg );
7251 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007252 PSA_ASSERT( psa_import_key( &attributes,
7253 our_key_data->x, our_key_data->len,
7254 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007255
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007256 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007257 PSA_ASSERT( psa_key_derivation_key_agreement(
7258 &operation,
7259 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7260 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007261 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7262 {
7263 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007264 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007265 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007266 NULL, 0 ) );
7267 }
Gilles Peskine59685592018-09-18 12:11:34 +02007268
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007269 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007270 actual_output,
7271 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007272 ASSERT_COMPARE( actual_output, expected_output1->len,
7273 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007274 if( expected_output2->len != 0 )
7275 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007276 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007277 actual_output,
7278 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007279 ASSERT_COMPARE( actual_output, expected_output2->len,
7280 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007281 }
Gilles Peskine59685592018-09-18 12:11:34 +02007282
7283exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007284 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007285 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007286 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007287 mbedtls_free( actual_output );
7288}
7289/* END_CASE */
7290
7291/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007292void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007293{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007294 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007295 unsigned char *output = NULL;
7296 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007297 size_t i;
7298 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007299
Simon Butcher49f8e312020-03-03 15:51:50 +00007300 TEST_ASSERT( bytes_arg >= 0 );
7301
Gilles Peskine91892022021-02-08 19:50:26 +01007302 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007303 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007304
Gilles Peskine8817f612018-12-18 00:18:46 +01007305 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007306
Gilles Peskinea50d7392018-06-21 10:22:13 +02007307 /* Run several times, to ensure that every output byte will be
7308 * nonzero at least once with overwhelming probability
7309 * (2^(-8*number_of_runs)). */
7310 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007311 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007312 if( bytes != 0 )
7313 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007314 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007315
Gilles Peskinea50d7392018-06-21 10:22:13 +02007316 for( i = 0; i < bytes; i++ )
7317 {
7318 if( output[i] != 0 )
7319 ++changed[i];
7320 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007321 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007322
7323 /* Check that every byte was changed to nonzero at least once. This
7324 * validates that psa_generate_random is overwriting every byte of
7325 * the output buffer. */
7326 for( i = 0; i < bytes; i++ )
7327 {
7328 TEST_ASSERT( changed[i] != 0 );
7329 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007330
7331exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007332 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007333 mbedtls_free( output );
7334 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007335}
7336/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007337
7338/* BEGIN_CASE */
7339void generate_key( int type_arg,
7340 int bits_arg,
7341 int usage_arg,
7342 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007343 int expected_status_arg,
7344 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007345{
Ronald Cron5425a212020-08-04 14:58:35 +02007346 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007347 psa_key_type_t type = type_arg;
7348 psa_key_usage_t usage = usage_arg;
7349 size_t bits = bits_arg;
7350 psa_algorithm_t alg = alg_arg;
7351 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007353 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007354
Gilles Peskine8817f612018-12-18 00:18:46 +01007355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007356
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007357 psa_set_key_usage_flags( &attributes, usage );
7358 psa_set_key_algorithm( &attributes, alg );
7359 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007360 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007361
7362 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007363 psa_status_t status = psa_generate_key( &attributes, &key );
7364
7365 if( is_large_key > 0 )
7366 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7367 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007368 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007369 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007370
7371 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007372 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007373 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7374 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007375
Gilles Peskine818ca122018-06-20 18:16:48 +02007376 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007377 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007378 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007379
7380exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007381 /*
7382 * Key attributes may have been returned by psa_get_key_attributes()
7383 * thus reset them as required.
7384 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007385 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007386
Ronald Cron5425a212020-08-04 14:58:35 +02007387 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007388 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007389}
7390/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007391
Ronald Cronee414c72021-03-18 18:50:08 +01007392/* 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 +02007393void generate_key_rsa( int bits_arg,
7394 data_t *e_arg,
7395 int expected_status_arg )
7396{
Ronald Cron5425a212020-08-04 14:58:35 +02007397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007398 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007399 size_t bits = bits_arg;
7400 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7401 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7402 psa_status_t expected_status = expected_status_arg;
7403 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7404 uint8_t *exported = NULL;
7405 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007406 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007407 size_t exported_length = SIZE_MAX;
7408 uint8_t *e_read_buffer = NULL;
7409 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007410 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007411 size_t e_read_length = SIZE_MAX;
7412
7413 if( e_arg->len == 0 ||
7414 ( e_arg->len == 3 &&
7415 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7416 {
7417 is_default_public_exponent = 1;
7418 e_read_size = 0;
7419 }
7420 ASSERT_ALLOC( e_read_buffer, e_read_size );
7421 ASSERT_ALLOC( exported, exported_size );
7422
7423 PSA_ASSERT( psa_crypto_init( ) );
7424
7425 psa_set_key_usage_flags( &attributes, usage );
7426 psa_set_key_algorithm( &attributes, alg );
7427 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7428 e_arg->x, e_arg->len ) );
7429 psa_set_key_bits( &attributes, bits );
7430
7431 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007432 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007433 if( expected_status != PSA_SUCCESS )
7434 goto exit;
7435
7436 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007437 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007438 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7439 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7440 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7441 e_read_buffer, e_read_size,
7442 &e_read_length ) );
7443 if( is_default_public_exponent )
7444 TEST_EQUAL( e_read_length, 0 );
7445 else
7446 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7447
7448 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007449 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007450 goto exit;
7451
7452 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007453 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007454 exported, exported_size,
7455 &exported_length ) );
7456 {
7457 uint8_t *p = exported;
7458 uint8_t *end = exported + exported_length;
7459 size_t len;
7460 /* RSAPublicKey ::= SEQUENCE {
7461 * modulus INTEGER, -- n
7462 * publicExponent INTEGER } -- e
7463 */
7464 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007465 MBEDTLS_ASN1_SEQUENCE |
7466 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007467 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007468 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7469 MBEDTLS_ASN1_INTEGER ) );
7470 if( len >= 1 && p[0] == 0 )
7471 {
7472 ++p;
7473 --len;
7474 }
7475 if( e_arg->len == 0 )
7476 {
7477 TEST_EQUAL( len, 3 );
7478 TEST_EQUAL( p[0], 1 );
7479 TEST_EQUAL( p[1], 0 );
7480 TEST_EQUAL( p[2], 1 );
7481 }
7482 else
7483 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7484 }
7485
7486exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007487 /*
7488 * Key attributes may have been returned by psa_get_key_attributes() or
7489 * set by psa_set_key_domain_parameters() thus reset them as required.
7490 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007491 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007492
Ronald Cron5425a212020-08-04 14:58:35 +02007493 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007494 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007495 mbedtls_free( e_read_buffer );
7496 mbedtls_free( exported );
7497}
7498/* END_CASE */
7499
Darryl Greend49a4992018-06-18 17:27:26 +01007500/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007501void persistent_key_load_key_from_storage( data_t *data,
7502 int type_arg, int bits_arg,
7503 int usage_flags_arg, int alg_arg,
7504 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007505{
Ronald Cron71016a92020-08-28 19:01:50 +02007506 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007508 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7509 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007510 psa_key_type_t type = type_arg;
7511 size_t bits = bits_arg;
7512 psa_key_usage_t usage_flags = usage_flags_arg;
7513 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007514 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007515 unsigned char *first_export = NULL;
7516 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007517 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007518 size_t first_exported_length;
7519 size_t second_exported_length;
7520
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007521 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7522 {
7523 ASSERT_ALLOC( first_export, export_size );
7524 ASSERT_ALLOC( second_export, export_size );
7525 }
Darryl Greend49a4992018-06-18 17:27:26 +01007526
Gilles Peskine8817f612018-12-18 00:18:46 +01007527 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007528
Gilles Peskinec87af662019-05-15 16:12:22 +02007529 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007530 psa_set_key_usage_flags( &attributes, usage_flags );
7531 psa_set_key_algorithm( &attributes, alg );
7532 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007533 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007534
Darryl Green0c6575a2018-11-07 16:05:30 +00007535 switch( generation_method )
7536 {
7537 case IMPORT_KEY:
7538 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007539 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007540 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007541 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007542
Darryl Green0c6575a2018-11-07 16:05:30 +00007543 case GENERATE_KEY:
7544 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007545 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007546 break;
7547
7548 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007549#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007550 {
7551 /* Create base key */
7552 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7553 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7554 psa_set_key_usage_flags( &base_attributes,
7555 PSA_KEY_USAGE_DERIVE );
7556 psa_set_key_algorithm( &base_attributes, derive_alg );
7557 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007558 PSA_ASSERT( psa_import_key( &base_attributes,
7559 data->x, data->len,
7560 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007561 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007562 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007563 PSA_ASSERT( psa_key_derivation_input_key(
7564 &operation,
7565 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007566 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007567 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007568 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007569 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7570 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007571 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007572 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007573 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007574 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007575 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007576#else
7577 TEST_ASSUME( ! "KDF not supported in this configuration" );
7578#endif
7579 break;
7580
7581 default:
7582 TEST_ASSERT( ! "generation_method not implemented in test" );
7583 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007584 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007585 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007586
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007587 /* Export the key if permitted by the key policy. */
7588 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7589 {
Ronald Cron5425a212020-08-04 14:58:35 +02007590 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007591 first_export, export_size,
7592 &first_exported_length ) );
7593 if( generation_method == IMPORT_KEY )
7594 ASSERT_COMPARE( data->x, data->len,
7595 first_export, first_exported_length );
7596 }
Darryl Greend49a4992018-06-18 17:27:26 +01007597
7598 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007599 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007600 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007601 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007602
Darryl Greend49a4992018-06-18 17:27:26 +01007603 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007604 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007605 TEST_ASSERT( mbedtls_svc_key_id_equal(
7606 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007607 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7608 PSA_KEY_LIFETIME_PERSISTENT );
7609 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7610 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007611 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007612 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007613 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007614
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007615 /* Export the key again if permitted by the key policy. */
7616 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007617 {
Ronald Cron5425a212020-08-04 14:58:35 +02007618 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007619 second_export, export_size,
7620 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007621 ASSERT_COMPARE( first_export, first_exported_length,
7622 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007623 }
7624
7625 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007626 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007627 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007628
7629exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007630 /*
7631 * Key attributes may have been returned by psa_get_key_attributes()
7632 * thus reset them as required.
7633 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007634 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007635
Darryl Greend49a4992018-06-18 17:27:26 +01007636 mbedtls_free( first_export );
7637 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007638 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007639 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007640 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007641 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007642}
7643/* END_CASE */