blob: f21aa6e34a403895790ec3fc4bf65230af87ca18 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010024
Gilles Peskine4023c012021-05-27 13:21:20 +020025/* If this comes up, it's a bug in the test code or in the test data. */
26#define UNUSED 0xdeadbeef
27
Dave Rodgman647791d2021-06-23 12:49:59 +010028/* Assert that an operation is (not) active.
29 * This serves as a proxy for checking if the operation is aborted. */
30#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
31#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010032
33/** An invalid export length that will never be set by psa_export_key(). */
34static const size_t INVALID_EXPORT_LENGTH = ~0U;
35
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036/** Test if a buffer contains a constant byte value.
37 *
38 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 *
40 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042 * \param size Size of the buffer in bytes.
43 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 * \return 1 if the buffer is all-bits-zero.
45 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048{
49 size_t i;
50 for( i = 0; i < size; i++ )
51 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020052 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020055 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020056}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010057#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
59static int asn1_write_10x( unsigned char **p,
60 unsigned char *start,
61 size_t bits,
62 unsigned char x )
63{
64 int ret;
65 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020066 if( bits == 0 )
67 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
68 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020069 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030070 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020071 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
72 *p -= len;
73 ( *p )[len-1] = x;
74 if( bits % 8 == 0 )
75 ( *p )[1] |= 1;
76 else
77 ( *p )[0] |= 1 << ( bits % 8 );
78 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
80 MBEDTLS_ASN1_INTEGER ) );
81 return( len );
82}
83
84static int construct_fake_rsa_key( unsigned char *buffer,
85 size_t buffer_size,
86 unsigned char **p,
87 size_t bits,
88 int keypair )
89{
90 size_t half_bits = ( bits + 1 ) / 2;
91 int ret;
92 int len = 0;
93 /* Construct something that looks like a DER encoding of
94 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
95 * RSAPrivateKey ::= SEQUENCE {
96 * version Version,
97 * modulus INTEGER, -- n
98 * publicExponent INTEGER, -- e
99 * privateExponent INTEGER, -- d
100 * prime1 INTEGER, -- p
101 * prime2 INTEGER, -- q
102 * exponent1 INTEGER, -- d mod (p-1)
103 * exponent2 INTEGER, -- d mod (q-1)
104 * coefficient INTEGER, -- (inverse of q) mod p
105 * otherPrimeInfos OtherPrimeInfos OPTIONAL
106 * }
107 * Or, for a public key, the same structure with only
108 * version, modulus and publicExponent.
109 */
110 *p = buffer + buffer_size;
111 if( keypair )
112 {
113 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
116 asn1_write_10x( p, buffer, half_bits, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
118 asn1_write_10x( p, buffer, half_bits, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* q */
120 asn1_write_10x( p, buffer, half_bits, 1 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x( p, buffer, half_bits, 3 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* d */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 }
126 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
127 asn1_write_10x( p, buffer, 17, 1 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, /* n */
129 asn1_write_10x( p, buffer, bits, 1 ) );
130 if( keypair )
131 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
132 mbedtls_asn1_write_int( p, buffer, 0 ) );
133 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
134 {
135 const unsigned char tag =
136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
137 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
138 }
139 return( len );
140}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100141#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100143int exercise_mac_setup( psa_key_type_t key_type,
144 const unsigned char *key_bytes,
145 size_t key_length,
146 psa_algorithm_t alg,
147 psa_mac_operation_t *operation,
148 psa_status_t *status )
149{
Ronald Cron5425a212020-08-04 14:58:35 +0200150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200154 psa_set_key_algorithm( &attributes, alg );
155 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200156 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157
Ronald Cron5425a212020-08-04 14:58:35 +0200158 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100159 /* Whether setup succeeded or failed, abort must succeed. */
160 PSA_ASSERT( psa_mac_abort( operation ) );
161 /* If setup failed, reproduce the failure, so that the caller can
162 * test the resulting state of the operation object. */
163 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 {
Ronald Cron5425a212020-08-04 14:58:35 +0200165 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 }
167
Ronald Cron5425a212020-08-04 14:58:35 +0200168 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100169 return( 1 );
170
171exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200172 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173 return( 0 );
174}
175
176int exercise_cipher_setup( psa_key_type_t key_type,
177 const unsigned char *key_bytes,
178 size_t key_length,
179 psa_algorithm_t alg,
180 psa_cipher_operation_t *operation,
181 psa_status_t *status )
182{
Ronald Cron5425a212020-08-04 14:58:35 +0200183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
187 psa_set_key_algorithm( &attributes, alg );
188 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200189 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190
Ronald Cron5425a212020-08-04 14:58:35 +0200191 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* Whether setup succeeded or failed, abort must succeed. */
193 PSA_ASSERT( psa_cipher_abort( operation ) );
194 /* If setup failed, reproduce the failure, so that the caller can
195 * test the resulting state of the operation object. */
196 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 {
Ronald Cron5425a212020-08-04 14:58:35 +0200198 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100199 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 }
201
Ronald Cron5425a212020-08-04 14:58:35 +0200202 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203 return( 1 );
204
205exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200206 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207 return( 0 );
208}
209
Ronald Cron5425a212020-08-04 14:58:35 +0200210static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211{
212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 uint8_t buffer[1];
215 size_t length;
216 int ok = 0;
217
Ronald Cronecfb2372020-07-23 17:13:42 +0200218 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
220 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
221 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200224 TEST_EQUAL(
225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
226 TEST_EQUAL(
227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200228 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200229 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
233
Ronald Cron5425a212020-08-04 14:58:35 +0200234 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000235 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200236 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000238 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 ok = 1;
241
242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243 /*
244 * Key attributes may have been returned by psa_get_key_attributes()
245 * thus reset them as required.
246 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200247 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100248
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200249 return( ok );
250}
251
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252/* Assert that a key isn't reported as having a slot number. */
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
255 do \
256 { \
257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
258 TEST_EQUAL( psa_get_key_slot_number( \
259 attributes, \
260 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
261 PSA_ERROR_INVALID_ARGUMENT ); \
262 } \
263 while( 0 )
264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
265#define ASSERT_NO_SLOT_NUMBER( attributes ) \
266 ( (void) 0 )
267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100269/* An overapproximation of the amount of storage needed for a key of the
270 * given type and with the given content. The API doesn't make it easy
271 * to find a good value for the size. The current implementation doesn't
272 * care about the value anyway. */
273#define KEY_BITS_FROM_DATA( type, data ) \
274 ( data )->len
275
Darryl Green0c6575a2018-11-07 16:05:30 +0000276typedef enum {
277 IMPORT_KEY = 0,
278 GENERATE_KEY = 1,
279 DERIVE_KEY = 2
280} generate_method;
281
Paul Elliott33746aa2021-09-15 16:40:40 +0100282typedef enum
283{
284 DO_NOT_SET_LENGTHS = 0,
285 SET_LENGTHS_BEFORE_NONCE = 1,
286 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100287} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100288
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100289typedef enum
290{
291 USE_NULL_TAG = 0,
292 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100293} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100294
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100295/*!
296 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100297 * \param key_type_arg Type of key passed in
298 * \param key_data The encryption / decryption key data
299 * \param alg_arg The type of algorithm used
300 * \param nonce Nonce data
301 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100302 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 * feed additional data in to be encrypted /
304 * decrypted. If -1, no chunking.
305 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100306 * \param data_part_len_arg If not -1, the length of chunks to feed
307 * the data in to be encrypted / decrypted. If
308 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100309 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100310 * expected here, this controls whether or not
311 * to set lengths, and in what order with
312 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100315 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100316 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 */
319static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
320 int alg_arg,
321 data_t *nonce,
322 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100323 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100325 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100326 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100328 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100329 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100330{
331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
332 psa_key_type_t key_type = key_type_arg;
333 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100334 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100335 unsigned char *output_data = NULL;
336 unsigned char *part_data = NULL;
337 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100339 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 size_t output_size = 0;
341 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 size_t output_length = 0;
343 size_t key_bits = 0;
344 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100345 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100346 size_t part_length = 0;
347 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100349 size_t ad_part_len = 0;
350 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100351 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100356 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100357
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 PSA_ASSERT( psa_crypto_init( ) );
359
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 if( is_encrypt )
361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
362 else
363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
364
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 psa_set_key_algorithm( &attributes, alg );
366 psa_set_key_type( &attributes, key_type );
367
368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
369 &key ) );
370
371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
372 key_bits = psa_get_key_bits( &attributes );
373
374 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 if( is_encrypt )
377 {
378 /* Tag gets written at end of buffer. */
379 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
380 ( input_data->len +
381 tag_length ) );
382 data_true_size = input_data->len;
383 }
384 else
385 {
386 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
387 ( input_data->len -
388 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 /* Do not want to attempt to decrypt tag. */
391 data_true_size = input_data->len - tag_length;
392 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 ASSERT_ALLOC( output_data, output_size );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396 if( is_encrypt )
397 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100398 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
399 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 }
401 else
402 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100403 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
404 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100405 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100407 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100408
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100409 if( is_encrypt )
410 status = psa_aead_encrypt_setup( &operation, key, alg );
411 else
412 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
414 /* If the operation is not supported, just skip and not fail in case the
415 * encryption involves a common limitation of cryptography hardwares and
416 * an alternative implementation. */
417 if( status == PSA_ERROR_NOT_SUPPORTED )
418 {
419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
421 }
422
423 PSA_ASSERT( status );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 if( set_lengths_method == DO_NOT_SET_LENGTHS )
426 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
427 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100428 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100429 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
430 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
432 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100433 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100434 {
435 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
436
Paul Elliott33746aa2021-09-15 16:40:40 +0100437 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
438 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100441 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
443 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100444 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100446 for( part_offset = 0, part_count = 0;
447 part_offset < additional_data->len;
448 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100450 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 {
Paul Elliott329d5382021-07-22 17:10:45 +0100452 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100453 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100454 else if( additional_data->len - part_offset < ad_part_len )
455 {
456 part_length = additional_data->len - part_offset;
457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 else
459 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100460 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
462
463 PSA_ASSERT( psa_aead_update_ad( &operation,
464 additional_data->x + part_offset,
465 part_length ) );
466
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 }
468 }
469 else
470 {
471 /* Pass additional data in one go. */
472 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
473 additional_data->len ) );
474 }
475
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100476 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
478 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100479 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100481 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
483 ASSERT_ALLOC( part_data, part_data_size );
484
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100485 for( part_offset = 0, part_count = 0;
486 part_offset < data_true_size;
487 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100489 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 {
Paul Elliott329d5382021-07-22 17:10:45 +0100491 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100493 else if( ( data_true_size - part_offset ) < data_part_len )
494 {
495 part_length = ( data_true_size - part_offset );
496 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 else
498 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
502 PSA_ASSERT( psa_aead_update( &operation,
503 ( input_data->x + part_offset ),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length ) );
507
508 if( output_data && output_part_length )
509 {
510 memcpy( ( output_data + part_offset ), part_data,
511 output_part_length );
512 }
513
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 output_length += output_part_length;
515 }
516 }
517 else
518 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 output_size, &output_length ) );
523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( is_encrypt )
526 PSA_ASSERT( psa_aead_finish( &operation, final_data,
527 final_output_size,
528 &output_part_length,
529 tag_buffer, tag_length,
530 &tag_size ) );
531 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100532 {
Paul Elliott9961a662021-09-17 19:19:02 +0100533 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 final_output_size,
535 &output_part_length,
536 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100537 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100540 if( output_data && output_part_length )
541 memcpy( ( output_data + output_length ), final_data,
542 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
549 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551 TEST_EQUAL( tag_length, tag_size );
552
553 if( output_data && tag_length )
554 memcpy( ( output_data + output_length ), tag_buffer,
555 tag_length );
556
557 output_length += tag_length;
558
559 TEST_EQUAL( output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_ASSERT( output_length <=
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
570 TEST_ASSERT( output_length <=
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Gilles Peskinee59236f2018-01-27 23:32:46 +0100592/* END_HEADER */
593
594/* BEGIN_DEPENDENCIES
595 * depends_on:MBEDTLS_PSA_CRYPTO_C
596 * END_DEPENDENCIES
597 */
598
599/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200600void static_checks( )
601{
602 size_t max_truncated_mac_size =
603 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
604
605 /* Check that the length for a truncated MAC always fits in the algorithm
606 * encoding. The shifted mask is the maximum truncated value. The
607 * untruncated algorithm may be one byte larger. */
608 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
609}
610/* END_CASE */
611
612/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200613void import_with_policy( int type_arg,
614 int usage_arg, int alg_arg,
615 int expected_status_arg )
616{
617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
618 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200620 psa_key_type_t type = type_arg;
621 psa_key_usage_t usage = usage_arg;
622 psa_algorithm_t alg = alg_arg;
623 psa_status_t expected_status = expected_status_arg;
624 const uint8_t key_material[16] = {0};
625 psa_status_t status;
626
627 PSA_ASSERT( psa_crypto_init( ) );
628
629 psa_set_key_type( &attributes, type );
630 psa_set_key_usage_flags( &attributes, usage );
631 psa_set_key_algorithm( &attributes, alg );
632
633 status = psa_import_key( &attributes,
634 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200635 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636 TEST_EQUAL( status, expected_status );
637 if( status != PSA_SUCCESS )
638 goto exit;
639
Ronald Cron5425a212020-08-04 14:58:35 +0200640 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200642 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200643 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200644 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200645 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200646
Ronald Cron5425a212020-08-04 14:58:35 +0200647 PSA_ASSERT( psa_destroy_key( key ) );
648 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200649
650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100651 /*
652 * Key attributes may have been returned by psa_get_key_attributes()
653 * thus reset them as required.
654 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200655 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100656
657 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200658 PSA_DONE( );
659}
660/* END_CASE */
661
662/* BEGIN_CASE */
663void import_with_data( data_t *data, int type_arg,
664 int attr_bits_arg,
665 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200666{
667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
668 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200670 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200671 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200672 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
Gilles Peskine4747d192019-04-17 15:05:45 +0200677 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200678 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200679
Ronald Cron5425a212020-08-04 14:58:35 +0200680 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100681 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200682 if( status != PSA_SUCCESS )
683 goto exit;
684
Ronald Cron5425a212020-08-04 14:58:35 +0200685 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200686 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200687 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200688 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200689 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200690
Ronald Cron5425a212020-08-04 14:58:35 +0200691 PSA_ASSERT( psa_destroy_key( key ) );
692 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693
694exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100695 /*
696 * Key attributes may have been returned by psa_get_key_attributes()
697 * thus reset them as required.
698 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200699 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100700
701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200702 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703}
704/* END_CASE */
705
706/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200707void import_large_key( int type_arg, int byte_size_arg,
708 int expected_status_arg )
709{
710 psa_key_type_t type = type_arg;
711 size_t byte_size = byte_size_arg;
712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
713 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200715 psa_status_t status;
716 uint8_t *buffer = NULL;
717 size_t buffer_size = byte_size + 1;
718 size_t n;
719
Steven Cooreman69967ce2021-01-18 18:01:08 +0100720 /* Skip the test case if the target running the test cannot
721 * accomodate large keys due to heap size constraints */
722 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200723 memset( buffer, 'K', byte_size );
724
725 PSA_ASSERT( psa_crypto_init( ) );
726
727 /* Try importing the key */
728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
729 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200730 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100731 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200732 TEST_EQUAL( status, expected_status );
733
734 if( status == PSA_SUCCESS )
735 {
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200737 TEST_EQUAL( psa_get_key_type( &attributes ), type );
738 TEST_EQUAL( psa_get_key_bits( &attributes ),
739 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200740 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200742 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200743 for( n = 0; n < byte_size; n++ )
744 TEST_EQUAL( buffer[n], 'K' );
745 for( n = byte_size; n < buffer_size; n++ )
746 TEST_EQUAL( buffer[n], 0 );
747 }
748
749exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100750 /*
751 * Key attributes may have been returned by psa_get_key_attributes()
752 * thus reset them as required.
753 */
754 psa_reset_key_attributes( &attributes );
755
Ronald Cron5425a212020-08-04 14:58:35 +0200756 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200757 PSA_DONE( );
758 mbedtls_free( buffer );
759}
760/* END_CASE */
761
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100762/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
764{
Ronald Cron5425a212020-08-04 14:58:35 +0200765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200766 size_t bits = bits_arg;
767 psa_status_t expected_status = expected_status_arg;
768 psa_status_t status;
769 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200770 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200771 size_t buffer_size = /* Slight overapproximations */
772 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200773 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200774 unsigned char *p;
775 int ret;
776 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778
Gilles Peskine8817f612018-12-18 00:18:46 +0100779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200780 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200781
782 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
783 bits, keypair ) ) >= 0 );
784 length = ret;
785
786 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200787 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200788 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100789 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200790
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200791 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200792 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200793
794exit:
795 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200796 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200797}
798/* END_CASE */
799
800/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300801void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300802 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200803 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530804 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 int expected_bits,
806 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200807 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 int canonical_input )
809{
Ronald Cron5425a212020-08-04 14:58:35 +0200810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200812 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200813 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530815 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 unsigned char *exported = NULL;
817 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100819 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200822 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823
Moran Pekercb088e72018-07-17 17:36:59 +0300824 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200825 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200827 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829
Archana4d7ae1d2021-07-07 02:50:22 +0530830 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200831 psa_set_key_usage_flags( &attributes, usage_arg );
832 psa_set_key_algorithm( &attributes, alg );
833 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700834
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200836 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837
838 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200839 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200840 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
841 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200842 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843
844 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200845 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100846 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100847
848 /* The exported length must be set by psa_export_key() to a value between 0
849 * and export_size. On errors, the exported length must be 0. */
850 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
851 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
852 TEST_ASSERT( exported_length <= export_size );
853
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200854 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200855 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100856 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200857 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200860 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861
Gilles Peskineea38a922021-02-13 00:05:16 +0100862 /* Run sanity checks on the exported key. For non-canonical inputs,
863 * this validates the canonical representations. For canonical inputs,
864 * this doesn't directly validate the implementation, but it still helps
865 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530866 if( !psa_key_lifetime_is_external( lifetime ) )
867 {
868 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
869 goto exit;
870 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200873 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 else
875 {
Ronald Cron5425a212020-08-04 14:58:35 +0200876 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200877 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200878 &key2 ) );
879 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100880 reexported,
881 export_size,
882 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200883 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +0530884 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100887 TEST_ASSERT( exported_length <=
Archana4d7ae1d2021-07-07 02:50:22 +0530888 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +0530889 psa_get_key_bits( &got_attributes ) ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100890 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891
892destroy:
893 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200894 PSA_ASSERT( psa_destroy_key( key ) );
895 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896
897exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100898 /*
899 * Key attributes may have been returned by psa_get_key_attributes()
900 * thus reset them as required.
901 */
902 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +0530903 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300904 mbedtls_free( exported );
905 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200906 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907}
908/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100909
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300911void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200912 int type_arg,
913 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530914 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 int export_size_delta,
916 int expected_export_status_arg,
917 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918{
Ronald Cron5425a212020-08-04 14:58:35 +0200919 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300920 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200921 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200922 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300923 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530924 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300925 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100926 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100927 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929
Gilles Peskine8817f612018-12-18 00:18:46 +0100930 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300931
Archana4d7ae1d2021-07-07 02:50:22 +0530932 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
934 psa_set_key_algorithm( &attributes, alg );
935 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300936
937 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200938 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300939
Gilles Peskine49c25912018-10-29 15:15:31 +0100940 /* Export the public key */
941 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200942 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200943 exported, export_size,
944 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100946 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100947 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200948 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100949 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200950 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200951 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100952 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100953 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100954 TEST_ASSERT( expected_public_key->len <=
955 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
956 TEST_ASSERT( expected_public_key->len <=
957 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100958 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
959 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100960 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300961exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100962 /*
963 * Key attributes may have been returned by psa_get_key_attributes()
964 * thus reset them as required.
965 */
966 psa_reset_key_attributes( &attributes );
967
itayzafrir3e02b3b2018-06-12 17:06:52 +0300968 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200969 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200970 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300971}
972/* END_CASE */
973
Gilles Peskine20035e32018-02-03 22:44:14 +0100974/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975void import_and_exercise_key( data_t *data,
976 int type_arg,
977 int bits_arg,
978 int alg_arg )
979{
Ronald Cron5425a212020-08-04 14:58:35 +0200980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200981 psa_key_type_t type = type_arg;
982 size_t bits = bits_arg;
983 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100984 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200986 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200987
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
Gilles Peskine4747d192019-04-17 15:05:45 +0200990 psa_set_key_usage_flags( &attributes, usage );
991 psa_set_key_algorithm( &attributes, alg );
992 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993
994 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200995 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200996
997 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200998 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200999 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1000 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001001
1002 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001003 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001004 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001005
Ronald Cron5425a212020-08-04 14:58:35 +02001006 PSA_ASSERT( psa_destroy_key( key ) );
1007 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001008
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001009exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001010 /*
1011 * Key attributes may have been returned by psa_get_key_attributes()
1012 * thus reset them as required.
1013 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001014 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001015
1016 psa_reset_key_attributes( &attributes );
1017 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001018 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001019}
1020/* END_CASE */
1021
1022/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001023void effective_key_attributes( int type_arg, int expected_type_arg,
1024 int bits_arg, int expected_bits_arg,
1025 int usage_arg, int expected_usage_arg,
1026 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001027{
Ronald Cron5425a212020-08-04 14:58:35 +02001028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001029 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001030 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001031 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001032 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001033 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001034 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001035 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001036 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
Gilles Peskine8817f612018-12-18 00:18:46 +01001039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001040
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001041 psa_set_key_usage_flags( &attributes, usage );
1042 psa_set_key_algorithm( &attributes, alg );
1043 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001044 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001047 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048
Ronald Cron5425a212020-08-04 14:58:35 +02001049 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001050 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1051 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1052 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1053 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001054
1055exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001056 /*
1057 * Key attributes may have been returned by psa_get_key_attributes()
1058 * thus reset them as required.
1059 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001060 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001061
1062 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001063 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001064}
1065/* END_CASE */
1066
1067/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001068void check_key_policy( int type_arg, int bits_arg,
1069 int usage_arg, int alg_arg )
1070{
1071 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001072 usage_arg,
1073 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001074 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001075 goto exit;
1076}
1077/* END_CASE */
1078
1079/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001080void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001081{
1082 /* Test each valid way of initializing the object, except for `= {0}`, as
1083 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1084 * though it's OK by the C standard. We could test for this, but we'd need
1085 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001086 psa_key_attributes_t func = psa_key_attributes_init( );
1087 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1088 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001089
1090 memset( &zero, 0, sizeof( zero ) );
1091
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001092 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1093 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1094 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001096 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1097 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1098 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1099
1100 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1101 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1102 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1103
1104 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1105 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1106 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1107
1108 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1109 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1110 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001111}
1112/* END_CASE */
1113
1114/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001115void mac_key_policy( int policy_usage_arg,
1116 int policy_alg_arg,
1117 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001119 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001120 int expected_status_sign_arg,
1121 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001126 psa_key_type_t key_type = key_type_arg;
1127 psa_algorithm_t policy_alg = policy_alg_arg;
1128 psa_algorithm_t exercise_alg = exercise_alg_arg;
1129 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001130 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001131 psa_status_t expected_status_sign = expected_status_sign_arg;
1132 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001134
Gilles Peskine8817f612018-12-18 00:18:46 +01001135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001137 psa_set_key_usage_flags( &attributes, policy_usage );
1138 psa_set_key_algorithm( &attributes, policy_alg );
1139 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001140
Gilles Peskine049c7532019-05-15 20:22:09 +02001141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001142 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001143
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001144 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1145 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001146
Ronald Cron5425a212020-08-04 14:58:35 +02001147 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001148 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001149
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001150 /* Calculate the MAC, one-shot case. */
1151 uint8_t input[128] = {0};
1152 size_t mac_len;
1153 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1154 input, 128,
1155 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1156 expected_status_sign );
1157
1158 /* Verify correct MAC, one-shot case. */
1159 status = psa_mac_verify( key, exercise_alg, input, 128,
1160 mac, mac_len );
1161
1162 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1163 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001165 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001166
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001168
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001170 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001171 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
1173exit:
1174 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001176 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177}
1178/* END_CASE */
1179
1180/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001181void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182 int policy_alg,
1183 int key_type,
1184 data_t *key_data,
1185 int exercise_alg )
1186{
Ronald Cron5425a212020-08-04 14:58:35 +02001187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001189 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001190 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191 psa_status_t status;
1192
Gilles Peskine8817f612018-12-18 00:18:46 +01001193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001195 psa_set_key_usage_flags( &attributes, policy_usage );
1196 psa_set_key_algorithm( &attributes, policy_alg );
1197 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001198
Gilles Peskine049c7532019-05-15 20:22:09 +02001199 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001200 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001202 /* Check if no key usage flag implication is done */
1203 TEST_EQUAL( policy_usage,
1204 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001205
Ronald Cron5425a212020-08-04 14:58:35 +02001206 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207 if( policy_alg == exercise_alg &&
1208 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001209 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001211 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001212 psa_cipher_abort( &operation );
1213
Ronald Cron5425a212020-08-04 14:58:35 +02001214 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001215 if( policy_alg == exercise_alg &&
1216 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001217 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001219 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220
1221exit:
1222 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001223 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001224 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225}
1226/* END_CASE */
1227
1228/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001229void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 int policy_alg,
1231 int key_type,
1232 data_t *key_data,
1233 int nonce_length_arg,
1234 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001235 int exercise_alg,
1236 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001237{
Ronald Cron5425a212020-08-04 14:58:35 +02001238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001240 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001241 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001242 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001243 unsigned char nonce[16] = {0};
1244 size_t nonce_length = nonce_length_arg;
1245 unsigned char tag[16];
1246 size_t tag_length = tag_length_arg;
1247 size_t output_length;
1248
1249 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1250 TEST_ASSERT( tag_length <= sizeof( tag ) );
1251
Gilles Peskine8817f612018-12-18 00:18:46 +01001252 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001253
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001254 psa_set_key_usage_flags( &attributes, policy_usage );
1255 psa_set_key_algorithm( &attributes, policy_alg );
1256 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001257
Gilles Peskine049c7532019-05-15 20:22:09 +02001258 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001259 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001260
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001261 /* Check if no key usage implication is done */
1262 TEST_EQUAL( policy_usage,
1263 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001264
Ronald Cron5425a212020-08-04 14:58:35 +02001265 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266 nonce, nonce_length,
1267 NULL, 0,
1268 NULL, 0,
1269 tag, tag_length,
1270 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001271 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1272 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001273 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001274 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001275
1276 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001277 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001278 nonce, nonce_length,
1279 NULL, 0,
1280 tag, tag_length,
1281 NULL, 0,
1282 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001283 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1284 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1285 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001286 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001288 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001289
1290exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001291 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001292 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001293}
1294/* END_CASE */
1295
1296/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001297void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001298 int policy_alg,
1299 int key_type,
1300 data_t *key_data,
1301 int exercise_alg )
1302{
Ronald Cron5425a212020-08-04 14:58:35 +02001303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001305 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001306 psa_status_t status;
1307 size_t key_bits;
1308 size_t buffer_length;
1309 unsigned char *buffer = NULL;
1310 size_t output_length;
1311
Gilles Peskine8817f612018-12-18 00:18:46 +01001312 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001313
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001314 psa_set_key_usage_flags( &attributes, policy_usage );
1315 psa_set_key_algorithm( &attributes, policy_alg );
1316 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317
Gilles Peskine049c7532019-05-15 20:22:09 +02001318 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001319 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001321 /* Check if no key usage implication is done */
1322 TEST_EQUAL( policy_usage,
1323 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001324
Ronald Cron5425a212020-08-04 14:58:35 +02001325 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001326 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001327 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1328 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001329 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001330
Ronald Cron5425a212020-08-04 14:58:35 +02001331 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332 NULL, 0,
1333 NULL, 0,
1334 buffer, buffer_length,
1335 &output_length );
1336 if( policy_alg == exercise_alg &&
1337 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001338 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001340 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001341
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001342 if( buffer_length != 0 )
1343 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001344 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001345 buffer, buffer_length,
1346 NULL, 0,
1347 buffer, buffer_length,
1348 &output_length );
1349 if( policy_alg == exercise_alg &&
1350 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001351 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001352 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001353 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354
1355exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001356 /*
1357 * Key attributes may have been returned by psa_get_key_attributes()
1358 * thus reset them as required.
1359 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001360 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001361
1362 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001363 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364 mbedtls_free( buffer );
1365}
1366/* END_CASE */
1367
1368/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001369void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001370 int policy_alg,
1371 int key_type,
1372 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001373 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001374 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001375 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001376{
Ronald Cron5425a212020-08-04 14:58:35 +02001377 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001379 psa_key_usage_t policy_usage = policy_usage_arg;
1380 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001381 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001382 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1383 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1384 * compatible with the policy and `payload_length_arg` is supposed to be
1385 * a valid input length to sign. If `payload_length_arg <= 0`,
1386 * `exercise_alg` is supposed to be forbidden by the policy. */
1387 int compatible_alg = payload_length_arg > 0;
1388 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001389 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001390 size_t signature_length;
1391
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001392 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001393 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001394 TEST_EQUAL( expected_usage,
1395 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001396
Gilles Peskine8817f612018-12-18 00:18:46 +01001397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001398
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001399 psa_set_key_usage_flags( &attributes, policy_usage );
1400 psa_set_key_algorithm( &attributes, policy_alg );
1401 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001402
Gilles Peskine049c7532019-05-15 20:22:09 +02001403 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001404 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001405
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001406 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1407
Ronald Cron5425a212020-08-04 14:58:35 +02001408 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001409 payload, payload_length,
1410 signature, sizeof( signature ),
1411 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001412 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001413 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001414 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001415 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001416
1417 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001418 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001419 payload, payload_length,
1420 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001421 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001422 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001423 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001424 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001425
Gilles Peskinef7b41372021-09-22 16:15:05 +02001426 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001427 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001428 {
1429 status = psa_sign_message( key, exercise_alg,
1430 payload, payload_length,
1431 signature, sizeof( signature ),
1432 &signature_length );
1433 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1434 PSA_ASSERT( status );
1435 else
1436 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1437
1438 memset( signature, 0, sizeof( signature ) );
1439 status = psa_verify_message( key, exercise_alg,
1440 payload, payload_length,
1441 signature, sizeof( signature ) );
1442 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1443 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1444 else
1445 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1446 }
1447
Gilles Peskined5b33222018-06-18 22:20:03 +02001448exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001449 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001450 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001451}
1452/* END_CASE */
1453
Janos Follathba3fab92019-06-11 14:50:16 +01001454/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001455void derive_key_policy( int policy_usage,
1456 int policy_alg,
1457 int key_type,
1458 data_t *key_data,
1459 int exercise_alg )
1460{
Ronald Cron5425a212020-08-04 14:58:35 +02001461 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001463 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001464 psa_status_t status;
1465
Gilles Peskine8817f612018-12-18 00:18:46 +01001466 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001467
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001468 psa_set_key_usage_flags( &attributes, policy_usage );
1469 psa_set_key_algorithm( &attributes, policy_alg );
1470 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001471
Gilles Peskine049c7532019-05-15 20:22:09 +02001472 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001473 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001474
Janos Follathba3fab92019-06-11 14:50:16 +01001475 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1476
1477 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1478 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001479 {
Janos Follathba3fab92019-06-11 14:50:16 +01001480 PSA_ASSERT( psa_key_derivation_input_bytes(
1481 &operation,
1482 PSA_KEY_DERIVATION_INPUT_SEED,
1483 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001484 }
Janos Follathba3fab92019-06-11 14:50:16 +01001485
1486 status = psa_key_derivation_input_key( &operation,
1487 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001488 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001489
Gilles Peskineea0fb492018-07-12 17:17:20 +02001490 if( policy_alg == exercise_alg &&
1491 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001492 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001493 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001494 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001495
1496exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001497 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001498 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001499 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001500}
1501/* END_CASE */
1502
1503/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001504void agreement_key_policy( int policy_usage,
1505 int policy_alg,
1506 int key_type_arg,
1507 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001508 int exercise_alg,
1509 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001510{
Ronald Cron5425a212020-08-04 14:58:35 +02001511 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001513 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001514 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001515 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001516 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001517
Gilles Peskine8817f612018-12-18 00:18:46 +01001518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001519
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001520 psa_set_key_usage_flags( &attributes, policy_usage );
1521 psa_set_key_algorithm( &attributes, policy_alg );
1522 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001523
Gilles Peskine049c7532019-05-15 20:22:09 +02001524 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001525 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001526
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001527 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001528 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001529
Steven Cooremance48e852020-10-05 16:02:45 +02001530 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001531
1532exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001533 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001534 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001535 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001536}
1537/* END_CASE */
1538
1539/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001540void key_policy_alg2( int key_type_arg, data_t *key_data,
1541 int usage_arg, int alg_arg, int alg2_arg )
1542{
Ronald Cron5425a212020-08-04 14:58:35 +02001543 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001544 psa_key_type_t key_type = key_type_arg;
1545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1546 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1547 psa_key_usage_t usage = usage_arg;
1548 psa_algorithm_t alg = alg_arg;
1549 psa_algorithm_t alg2 = alg2_arg;
1550
1551 PSA_ASSERT( psa_crypto_init( ) );
1552
1553 psa_set_key_usage_flags( &attributes, usage );
1554 psa_set_key_algorithm( &attributes, alg );
1555 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1556 psa_set_key_type( &attributes, key_type );
1557 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001558 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001559
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001560 /* Update the usage flags to obtain implicit usage flags */
1561 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001562 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001563 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1564 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1565 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1566
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001567 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001568 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001569 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001570 goto exit;
1571
1572exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001573 /*
1574 * Key attributes may have been returned by psa_get_key_attributes()
1575 * thus reset them as required.
1576 */
1577 psa_reset_key_attributes( &got_attributes );
1578
Ronald Cron5425a212020-08-04 14:58:35 +02001579 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001580 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001581}
1582/* END_CASE */
1583
1584/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001585void raw_agreement_key_policy( int policy_usage,
1586 int policy_alg,
1587 int key_type_arg,
1588 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001589 int exercise_alg,
1590 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001591{
Ronald Cron5425a212020-08-04 14:58:35 +02001592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001594 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001595 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001596 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001597 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001598
1599 PSA_ASSERT( psa_crypto_init( ) );
1600
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001601 psa_set_key_usage_flags( &attributes, policy_usage );
1602 psa_set_key_algorithm( &attributes, policy_alg );
1603 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001604
Gilles Peskine049c7532019-05-15 20:22:09 +02001605 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001606 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001607
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001608 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001609
Steven Cooremance48e852020-10-05 16:02:45 +02001610 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001611
1612exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001613 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001614 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001615 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001616}
1617/* END_CASE */
1618
1619/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001620void copy_success( int source_usage_arg,
1621 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301622 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001623 int type_arg, data_t *material,
1624 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001625 int target_usage_arg,
1626 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301627 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001628 int expected_usage_arg,
1629 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001630{
Gilles Peskineca25db92019-04-19 11:43:08 +02001631 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1632 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001633 psa_key_usage_t expected_usage = expected_usage_arg;
1634 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001635 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301636 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1637 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001638 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1639 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001640 uint8_t *export_buffer = NULL;
1641
Gilles Peskine57ab7212019-01-28 13:03:09 +01001642 PSA_ASSERT( psa_crypto_init( ) );
1643
Gilles Peskineca25db92019-04-19 11:43:08 +02001644 /* Prepare the source key. */
1645 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1646 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001647 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001648 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301649 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001650 PSA_ASSERT( psa_import_key( &source_attributes,
1651 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001652 &source_key ) );
1653 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001654
Gilles Peskineca25db92019-04-19 11:43:08 +02001655 /* Prepare the target attributes. */
1656 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001657 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001658 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001659 }
Archana8a180362021-07-05 02:18:48 +05301660 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001661
Gilles Peskineca25db92019-04-19 11:43:08 +02001662 if( target_usage_arg != -1 )
1663 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1664 if( target_alg_arg != -1 )
1665 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001666 if( target_alg2_arg != -1 )
1667 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001668
Archana8a180362021-07-05 02:18:48 +05301669
Gilles Peskine57ab7212019-01-28 13:03:09 +01001670 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001671 PSA_ASSERT( psa_copy_key( source_key,
1672 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001673
1674 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001675 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001676
1677 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001678 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001679 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1680 psa_get_key_type( &target_attributes ) );
1681 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1682 psa_get_key_bits( &target_attributes ) );
1683 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1684 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001685 TEST_EQUAL( expected_alg2,
1686 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001687 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1688 {
1689 size_t length;
1690 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001691 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001692 material->len, &length ) );
1693 ASSERT_COMPARE( material->x, material->len,
1694 export_buffer, length );
1695 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001696
Archana8a180362021-07-05 02:18:48 +05301697 if( !psa_key_lifetime_is_external( target_lifetime ) )
1698 {
1699 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1700 goto exit;
1701 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1702 goto exit;
1703 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001704
Ronald Cron5425a212020-08-04 14:58:35 +02001705 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001706
1707exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001708 /*
1709 * Source and target key attributes may have been returned by
1710 * psa_get_key_attributes() thus reset them as required.
1711 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001712 psa_reset_key_attributes( &source_attributes );
1713 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001714
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001715 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001716 mbedtls_free( export_buffer );
1717}
1718/* END_CASE */
1719
1720/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001721void copy_fail( int source_usage_arg,
1722 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301723 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001724 int type_arg, data_t *material,
1725 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001726 int target_usage_arg,
1727 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001728 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001729 int expected_status_arg )
1730{
1731 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1732 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001733 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1734 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001735 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001736
1737 PSA_ASSERT( psa_crypto_init( ) );
1738
1739 /* Prepare the source key. */
1740 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1741 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001742 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001743 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301744 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001745 PSA_ASSERT( psa_import_key( &source_attributes,
1746 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001747 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001748
1749 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001750 psa_set_key_id( &target_attributes, key_id );
1751 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001752 psa_set_key_type( &target_attributes, target_type_arg );
1753 psa_set_key_bits( &target_attributes, target_bits_arg );
1754 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1755 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001756 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001757
1758 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001759 TEST_EQUAL( psa_copy_key( source_key,
1760 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001761 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001762
Ronald Cron5425a212020-08-04 14:58:35 +02001763 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001764
Gilles Peskine4a644642019-05-03 17:14:08 +02001765exit:
1766 psa_reset_key_attributes( &source_attributes );
1767 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001768 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001769}
1770/* END_CASE */
1771
1772/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001773void hash_operation_init( )
1774{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001775 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001776 /* Test each valid way of initializing the object, except for `= {0}`, as
1777 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1778 * though it's OK by the C standard. We could test for this, but we'd need
1779 * to supress the Clang warning for the test. */
1780 psa_hash_operation_t func = psa_hash_operation_init( );
1781 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1782 psa_hash_operation_t zero;
1783
1784 memset( &zero, 0, sizeof( zero ) );
1785
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001786 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001787 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1788 PSA_ERROR_BAD_STATE );
1789 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1790 PSA_ERROR_BAD_STATE );
1791 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1792 PSA_ERROR_BAD_STATE );
1793
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001794 /* A default hash operation should be abortable without error. */
1795 PSA_ASSERT( psa_hash_abort( &func ) );
1796 PSA_ASSERT( psa_hash_abort( &init ) );
1797 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001798}
1799/* END_CASE */
1800
1801/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001802void hash_setup( int alg_arg,
1803 int expected_status_arg )
1804{
1805 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001806 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001807 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001808 psa_status_t status;
1809
Gilles Peskine8817f612018-12-18 00:18:46 +01001810 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001811
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001812 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001813 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001814
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001815 /* Whether setup succeeded or failed, abort must succeed. */
1816 PSA_ASSERT( psa_hash_abort( &operation ) );
1817
1818 /* If setup failed, reproduce the failure, so as to
1819 * test the resulting state of the operation object. */
1820 if( status != PSA_SUCCESS )
1821 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1822
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001823 /* Now the operation object should be reusable. */
1824#if defined(KNOWN_SUPPORTED_HASH_ALG)
1825 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1826 PSA_ASSERT( psa_hash_abort( &operation ) );
1827#endif
1828
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001829exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001830 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001831}
1832/* END_CASE */
1833
1834/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001835void hash_compute_fail( int alg_arg, data_t *input,
1836 int output_size_arg, int expected_status_arg )
1837{
1838 psa_algorithm_t alg = alg_arg;
1839 uint8_t *output = NULL;
1840 size_t output_size = output_size_arg;
1841 size_t output_length = INVALID_EXPORT_LENGTH;
1842 psa_status_t expected_status = expected_status_arg;
1843 psa_status_t status;
1844
1845 ASSERT_ALLOC( output, output_size );
1846
1847 PSA_ASSERT( psa_crypto_init( ) );
1848
1849 status = psa_hash_compute( alg, input->x, input->len,
1850 output, output_size, &output_length );
1851 TEST_EQUAL( status, expected_status );
1852 TEST_ASSERT( output_length <= output_size );
1853
1854exit:
1855 mbedtls_free( output );
1856 PSA_DONE( );
1857}
1858/* END_CASE */
1859
1860/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001861void hash_compare_fail( int alg_arg, data_t *input,
1862 data_t *reference_hash,
1863 int expected_status_arg )
1864{
1865 psa_algorithm_t alg = alg_arg;
1866 psa_status_t expected_status = expected_status_arg;
1867 psa_status_t status;
1868
1869 PSA_ASSERT( psa_crypto_init( ) );
1870
1871 status = psa_hash_compare( alg, input->x, input->len,
1872 reference_hash->x, reference_hash->len );
1873 TEST_EQUAL( status, expected_status );
1874
1875exit:
1876 PSA_DONE( );
1877}
1878/* END_CASE */
1879
1880/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001881void hash_compute_compare( int alg_arg, data_t *input,
1882 data_t *expected_output )
1883{
1884 psa_algorithm_t alg = alg_arg;
1885 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1886 size_t output_length = INVALID_EXPORT_LENGTH;
1887 size_t i;
1888
1889 PSA_ASSERT( psa_crypto_init( ) );
1890
1891 /* Compute with tight buffer */
1892 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001893 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001894 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001895 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001896 ASSERT_COMPARE( output, output_length,
1897 expected_output->x, expected_output->len );
1898
1899 /* Compute with larger buffer */
1900 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1901 output, sizeof( output ),
1902 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001903 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001904 ASSERT_COMPARE( output, output_length,
1905 expected_output->x, expected_output->len );
1906
1907 /* Compare with correct hash */
1908 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1909 output, output_length ) );
1910
1911 /* Compare with trailing garbage */
1912 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1913 output, output_length + 1 ),
1914 PSA_ERROR_INVALID_SIGNATURE );
1915
1916 /* Compare with truncated hash */
1917 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1918 output, output_length - 1 ),
1919 PSA_ERROR_INVALID_SIGNATURE );
1920
1921 /* Compare with corrupted value */
1922 for( i = 0; i < output_length; i++ )
1923 {
Chris Jones9634bb12021-01-20 15:56:42 +00001924 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001925 output[i] ^= 1;
1926 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1927 output, output_length ),
1928 PSA_ERROR_INVALID_SIGNATURE );
1929 output[i] ^= 1;
1930 }
1931
1932exit:
1933 PSA_DONE( );
1934}
1935/* END_CASE */
1936
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001937/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001938void hash_bad_order( )
1939{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001940 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001941 unsigned char input[] = "";
1942 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001943 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001944 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1945 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1946 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001947 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001948 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001949 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001950
Gilles Peskine8817f612018-12-18 00:18:46 +01001951 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001952
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001953 /* Call setup twice in a row. */
1954 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001955 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001956 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1957 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001958 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001959 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001960 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001961
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001962 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001963 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001964 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001965 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001966
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001967 /* Check that update calls abort on error. */
1968 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001969 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001970 ASSERT_OPERATION_IS_ACTIVE( operation );
1971 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1972 PSA_ERROR_BAD_STATE );
1973 ASSERT_OPERATION_IS_INACTIVE( operation );
1974 PSA_ASSERT( psa_hash_abort( &operation ) );
1975 ASSERT_OPERATION_IS_INACTIVE( operation );
1976
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001977 /* Call update after finish. */
1978 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1979 PSA_ASSERT( psa_hash_finish( &operation,
1980 hash, sizeof( hash ), &hash_len ) );
1981 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001982 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001983 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001984
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001985 /* Call verify without calling setup beforehand. */
1986 TEST_EQUAL( psa_hash_verify( &operation,
1987 valid_hash, sizeof( valid_hash ) ),
1988 PSA_ERROR_BAD_STATE );
1989 PSA_ASSERT( psa_hash_abort( &operation ) );
1990
1991 /* Call verify after finish. */
1992 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1993 PSA_ASSERT( psa_hash_finish( &operation,
1994 hash, sizeof( hash ), &hash_len ) );
1995 TEST_EQUAL( psa_hash_verify( &operation,
1996 valid_hash, sizeof( valid_hash ) ),
1997 PSA_ERROR_BAD_STATE );
1998 PSA_ASSERT( psa_hash_abort( &operation ) );
1999
2000 /* Call verify twice in a row. */
2001 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002002 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002003 PSA_ASSERT( psa_hash_verify( &operation,
2004 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002005 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002006 TEST_EQUAL( psa_hash_verify( &operation,
2007 valid_hash, sizeof( valid_hash ) ),
2008 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002009 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002010 PSA_ASSERT( psa_hash_abort( &operation ) );
2011
2012 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002013 TEST_EQUAL( psa_hash_finish( &operation,
2014 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002015 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002016 PSA_ASSERT( psa_hash_abort( &operation ) );
2017
2018 /* Call finish twice in a row. */
2019 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2020 PSA_ASSERT( psa_hash_finish( &operation,
2021 hash, sizeof( hash ), &hash_len ) );
2022 TEST_EQUAL( psa_hash_finish( &operation,
2023 hash, sizeof( hash ), &hash_len ),
2024 PSA_ERROR_BAD_STATE );
2025 PSA_ASSERT( psa_hash_abort( &operation ) );
2026
2027 /* Call finish after calling verify. */
2028 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2029 PSA_ASSERT( psa_hash_verify( &operation,
2030 valid_hash, sizeof( valid_hash ) ) );
2031 TEST_EQUAL( psa_hash_finish( &operation,
2032 hash, sizeof( hash ), &hash_len ),
2033 PSA_ERROR_BAD_STATE );
2034 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002035
2036exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002037 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002038}
2039/* END_CASE */
2040
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002041/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002042void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002043{
2044 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002045 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2046 * appended to it */
2047 unsigned char hash[] = {
2048 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2049 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2050 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002051 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002052 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002053
Gilles Peskine8817f612018-12-18 00:18:46 +01002054 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002055
itayzafrir27e69452018-11-01 14:26:34 +02002056 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002057 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002058 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002059 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002060 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002061 ASSERT_OPERATION_IS_INACTIVE( operation );
2062 PSA_ASSERT( psa_hash_abort( &operation ) );
2063 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002064
itayzafrir27e69452018-11-01 14:26:34 +02002065 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002066 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002067 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002068 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002069
itayzafrir27e69452018-11-01 14:26:34 +02002070 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002071 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002072 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002073 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002074
itayzafrirec93d302018-10-18 18:01:10 +03002075exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002076 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002077}
2078/* END_CASE */
2079
Ronald Cronee414c72021-03-18 18:50:08 +01002080/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002081void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002082{
2083 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002084 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002085 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002086 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002087 size_t hash_len;
2088
Gilles Peskine8817f612018-12-18 00:18:46 +01002089 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002090
itayzafrir58028322018-10-25 10:22:01 +03002091 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002092 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002093 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002094 hash, expected_size - 1, &hash_len ),
2095 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002096
2097exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002098 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002099}
2100/* END_CASE */
2101
Ronald Cronee414c72021-03-18 18:50:08 +01002102/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002103void hash_clone_source_state( )
2104{
2105 psa_algorithm_t alg = PSA_ALG_SHA_256;
2106 unsigned char hash[PSA_HASH_MAX_SIZE];
2107 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2108 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2109 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2110 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2111 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2112 size_t hash_len;
2113
2114 PSA_ASSERT( psa_crypto_init( ) );
2115 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2116
2117 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2118 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2119 PSA_ASSERT( psa_hash_finish( &op_finished,
2120 hash, sizeof( hash ), &hash_len ) );
2121 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2122 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2123
2124 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2125 PSA_ERROR_BAD_STATE );
2126
2127 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2128 PSA_ASSERT( psa_hash_finish( &op_init,
2129 hash, sizeof( hash ), &hash_len ) );
2130 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2131 PSA_ASSERT( psa_hash_finish( &op_finished,
2132 hash, sizeof( hash ), &hash_len ) );
2133 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2134 PSA_ASSERT( psa_hash_finish( &op_aborted,
2135 hash, sizeof( hash ), &hash_len ) );
2136
2137exit:
2138 psa_hash_abort( &op_source );
2139 psa_hash_abort( &op_init );
2140 psa_hash_abort( &op_setup );
2141 psa_hash_abort( &op_finished );
2142 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002143 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002144}
2145/* END_CASE */
2146
Ronald Cronee414c72021-03-18 18:50:08 +01002147/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002148void hash_clone_target_state( )
2149{
2150 psa_algorithm_t alg = PSA_ALG_SHA_256;
2151 unsigned char hash[PSA_HASH_MAX_SIZE];
2152 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2153 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2154 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2155 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2156 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2157 size_t hash_len;
2158
2159 PSA_ASSERT( psa_crypto_init( ) );
2160
2161 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2162 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2163 PSA_ASSERT( psa_hash_finish( &op_finished,
2164 hash, sizeof( hash ), &hash_len ) );
2165 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2166 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2167
2168 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2169 PSA_ASSERT( psa_hash_finish( &op_target,
2170 hash, sizeof( hash ), &hash_len ) );
2171
2172 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2173 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2174 PSA_ERROR_BAD_STATE );
2175 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2176 PSA_ERROR_BAD_STATE );
2177
2178exit:
2179 psa_hash_abort( &op_target );
2180 psa_hash_abort( &op_init );
2181 psa_hash_abort( &op_setup );
2182 psa_hash_abort( &op_finished );
2183 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002184 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002185}
2186/* END_CASE */
2187
itayzafrir58028322018-10-25 10:22:01 +03002188/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002189void mac_operation_init( )
2190{
Jaeden Amero252ef282019-02-15 14:05:35 +00002191 const uint8_t input[1] = { 0 };
2192
Jaeden Amero769ce272019-01-04 11:48:03 +00002193 /* Test each valid way of initializing the object, except for `= {0}`, as
2194 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2195 * though it's OK by the C standard. We could test for this, but we'd need
2196 * to supress the Clang warning for the test. */
2197 psa_mac_operation_t func = psa_mac_operation_init( );
2198 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2199 psa_mac_operation_t zero;
2200
2201 memset( &zero, 0, sizeof( zero ) );
2202
Jaeden Amero252ef282019-02-15 14:05:35 +00002203 /* A freshly-initialized MAC operation should not be usable. */
2204 TEST_EQUAL( psa_mac_update( &func,
2205 input, sizeof( input ) ),
2206 PSA_ERROR_BAD_STATE );
2207 TEST_EQUAL( psa_mac_update( &init,
2208 input, sizeof( input ) ),
2209 PSA_ERROR_BAD_STATE );
2210 TEST_EQUAL( psa_mac_update( &zero,
2211 input, sizeof( input ) ),
2212 PSA_ERROR_BAD_STATE );
2213
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002214 /* A default MAC operation should be abortable without error. */
2215 PSA_ASSERT( psa_mac_abort( &func ) );
2216 PSA_ASSERT( psa_mac_abort( &init ) );
2217 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002218}
2219/* END_CASE */
2220
2221/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002222void mac_setup( int key_type_arg,
2223 data_t *key,
2224 int alg_arg,
2225 int expected_status_arg )
2226{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002227 psa_key_type_t key_type = key_type_arg;
2228 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002229 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002230 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002231 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2232#if defined(KNOWN_SUPPORTED_MAC_ALG)
2233 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2234#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002235
Gilles Peskine8817f612018-12-18 00:18:46 +01002236 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002237
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002238 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2239 &operation, &status ) )
2240 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002241 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002242
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002243 /* The operation object should be reusable. */
2244#if defined(KNOWN_SUPPORTED_MAC_ALG)
2245 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2246 smoke_test_key_data,
2247 sizeof( smoke_test_key_data ),
2248 KNOWN_SUPPORTED_MAC_ALG,
2249 &operation, &status ) )
2250 goto exit;
2251 TEST_EQUAL( status, PSA_SUCCESS );
2252#endif
2253
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002254exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002255 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002256}
2257/* END_CASE */
2258
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002259/* 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 +00002260void mac_bad_order( )
2261{
Ronald Cron5425a212020-08-04 14:58:35 +02002262 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002263 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2264 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002265 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002266 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2267 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2268 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002270 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2271 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2272 size_t sign_mac_length = 0;
2273 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2274 const uint8_t verify_mac[] = {
2275 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2276 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2277 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2278
2279 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002280 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002281 psa_set_key_algorithm( &attributes, alg );
2282 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002283
Ronald Cron5425a212020-08-04 14:58:35 +02002284 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2285 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002286
Jaeden Amero252ef282019-02-15 14:05:35 +00002287 /* Call update without calling setup beforehand. */
2288 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2289 PSA_ERROR_BAD_STATE );
2290 PSA_ASSERT( psa_mac_abort( &operation ) );
2291
2292 /* Call sign finish without calling setup beforehand. */
2293 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2294 &sign_mac_length),
2295 PSA_ERROR_BAD_STATE );
2296 PSA_ASSERT( psa_mac_abort( &operation ) );
2297
2298 /* Call verify finish without calling setup beforehand. */
2299 TEST_EQUAL( psa_mac_verify_finish( &operation,
2300 verify_mac, sizeof( verify_mac ) ),
2301 PSA_ERROR_BAD_STATE );
2302 PSA_ASSERT( psa_mac_abort( &operation ) );
2303
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002304 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002305 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002306 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002307 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002308 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002309 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002310 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002311 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002312
Jaeden Amero252ef282019-02-15 14:05:35 +00002313 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002314 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002315 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2316 PSA_ASSERT( psa_mac_sign_finish( &operation,
2317 sign_mac, sizeof( sign_mac ),
2318 &sign_mac_length ) );
2319 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2320 PSA_ERROR_BAD_STATE );
2321 PSA_ASSERT( psa_mac_abort( &operation ) );
2322
2323 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002324 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002325 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2326 PSA_ASSERT( psa_mac_verify_finish( &operation,
2327 verify_mac, sizeof( verify_mac ) ) );
2328 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2329 PSA_ERROR_BAD_STATE );
2330 PSA_ASSERT( psa_mac_abort( &operation ) );
2331
2332 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002333 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002334 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2335 PSA_ASSERT( psa_mac_sign_finish( &operation,
2336 sign_mac, sizeof( sign_mac ),
2337 &sign_mac_length ) );
2338 TEST_EQUAL( psa_mac_sign_finish( &operation,
2339 sign_mac, sizeof( sign_mac ),
2340 &sign_mac_length ),
2341 PSA_ERROR_BAD_STATE );
2342 PSA_ASSERT( psa_mac_abort( &operation ) );
2343
2344 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002345 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002346 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2347 PSA_ASSERT( psa_mac_verify_finish( &operation,
2348 verify_mac, sizeof( verify_mac ) ) );
2349 TEST_EQUAL( psa_mac_verify_finish( &operation,
2350 verify_mac, sizeof( verify_mac ) ),
2351 PSA_ERROR_BAD_STATE );
2352 PSA_ASSERT( psa_mac_abort( &operation ) );
2353
2354 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002355 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002356 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002357 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002358 TEST_EQUAL( psa_mac_verify_finish( &operation,
2359 verify_mac, sizeof( verify_mac ) ),
2360 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002361 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002362 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002363 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002364
2365 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002366 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002367 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002368 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002369 TEST_EQUAL( psa_mac_sign_finish( &operation,
2370 sign_mac, sizeof( sign_mac ),
2371 &sign_mac_length ),
2372 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002373 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002374 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002375 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002376
Ronald Cron5425a212020-08-04 14:58:35 +02002377 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002378
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002379exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002380 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002381}
2382/* END_CASE */
2383
2384/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002385void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002386 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002387 int alg_arg,
2388 data_t *input,
2389 data_t *expected_mac )
2390{
Ronald Cron5425a212020-08-04 14:58:35 +02002391 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002392 psa_key_type_t key_type = key_type_arg;
2393 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002394 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002396 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002397 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002398 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002399 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002400 const size_t output_sizes_to_test[] = {
2401 0,
2402 1,
2403 expected_mac->len - 1,
2404 expected_mac->len,
2405 expected_mac->len + 1,
2406 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002407
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002408 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002409 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002410 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002411
Gilles Peskine8817f612018-12-18 00:18:46 +01002412 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002413
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002414 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002415 psa_set_key_algorithm( &attributes, alg );
2416 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002417
Ronald Cron5425a212020-08-04 14:58:35 +02002418 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2419 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002420
Gilles Peskine8b356b52020-08-25 23:44:59 +02002421 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2422 {
2423 const size_t output_size = output_sizes_to_test[i];
2424 psa_status_t expected_status =
2425 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2426 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002427
Chris Jones9634bb12021-01-20 15:56:42 +00002428 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002429 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002430
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002431 /* Calculate the MAC, one-shot case. */
2432 TEST_EQUAL( psa_mac_compute( key, alg,
2433 input->x, input->len,
2434 actual_mac, output_size, &mac_length ),
2435 expected_status );
2436 if( expected_status == PSA_SUCCESS )
2437 {
2438 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2439 actual_mac, mac_length );
2440 }
2441
2442 if( output_size > 0 )
2443 memset( actual_mac, 0, output_size );
2444
2445 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002446 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002447 PSA_ASSERT( psa_mac_update( &operation,
2448 input->x, input->len ) );
2449 TEST_EQUAL( psa_mac_sign_finish( &operation,
2450 actual_mac, output_size,
2451 &mac_length ),
2452 expected_status );
2453 PSA_ASSERT( psa_mac_abort( &operation ) );
2454
2455 if( expected_status == PSA_SUCCESS )
2456 {
2457 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2458 actual_mac, mac_length );
2459 }
2460 mbedtls_free( actual_mac );
2461 actual_mac = NULL;
2462 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002463
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002464exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002465 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002466 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002467 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002468 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002469}
2470/* END_CASE */
2471
2472/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002473void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002474 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002475 int alg_arg,
2476 data_t *input,
2477 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478{
Ronald Cron5425a212020-08-04 14:58:35 +02002479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480 psa_key_type_t key_type = key_type_arg;
2481 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002482 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002483 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002484 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485
Gilles Peskine69c12672018-06-28 00:07:19 +02002486 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2487
Gilles Peskine8817f612018-12-18 00:18:46 +01002488 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002490 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002491 psa_set_key_algorithm( &attributes, alg );
2492 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002493
Ronald Cron5425a212020-08-04 14:58:35 +02002494 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2495 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002496
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002497 /* Verify correct MAC, one-shot case. */
2498 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2499 expected_mac->x, expected_mac->len ) );
2500
2501 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002502 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002503 PSA_ASSERT( psa_mac_update( &operation,
2504 input->x, input->len ) );
2505 PSA_ASSERT( psa_mac_verify_finish( &operation,
2506 expected_mac->x,
2507 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002509 /* Test a MAC that's too short, one-shot case. */
2510 TEST_EQUAL( psa_mac_verify( key, alg,
2511 input->x, input->len,
2512 expected_mac->x,
2513 expected_mac->len - 1 ),
2514 PSA_ERROR_INVALID_SIGNATURE );
2515
2516 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002517 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002518 PSA_ASSERT( psa_mac_update( &operation,
2519 input->x, input->len ) );
2520 TEST_EQUAL( psa_mac_verify_finish( &operation,
2521 expected_mac->x,
2522 expected_mac->len - 1 ),
2523 PSA_ERROR_INVALID_SIGNATURE );
2524
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002525 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002526 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2527 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002528 TEST_EQUAL( psa_mac_verify( key, alg,
2529 input->x, input->len,
2530 perturbed_mac, expected_mac->len + 1 ),
2531 PSA_ERROR_INVALID_SIGNATURE );
2532
2533 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002534 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002535 PSA_ASSERT( psa_mac_update( &operation,
2536 input->x, input->len ) );
2537 TEST_EQUAL( psa_mac_verify_finish( &operation,
2538 perturbed_mac,
2539 expected_mac->len + 1 ),
2540 PSA_ERROR_INVALID_SIGNATURE );
2541
2542 /* Test changing one byte. */
2543 for( size_t i = 0; i < expected_mac->len; i++ )
2544 {
Chris Jones9634bb12021-01-20 15:56:42 +00002545 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002546 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002547
2548 TEST_EQUAL( psa_mac_verify( key, alg,
2549 input->x, input->len,
2550 perturbed_mac, expected_mac->len ),
2551 PSA_ERROR_INVALID_SIGNATURE );
2552
Ronald Cron5425a212020-08-04 14:58:35 +02002553 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002554 PSA_ASSERT( psa_mac_update( &operation,
2555 input->x, input->len ) );
2556 TEST_EQUAL( psa_mac_verify_finish( &operation,
2557 perturbed_mac,
2558 expected_mac->len ),
2559 PSA_ERROR_INVALID_SIGNATURE );
2560 perturbed_mac[i] ^= 1;
2561 }
2562
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002564 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002565 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002566 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002567 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568}
2569/* END_CASE */
2570
2571/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002572void cipher_operation_init( )
2573{
Jaeden Ameroab439972019-02-15 14:12:05 +00002574 const uint8_t input[1] = { 0 };
2575 unsigned char output[1] = { 0 };
2576 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002577 /* Test each valid way of initializing the object, except for `= {0}`, as
2578 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2579 * though it's OK by the C standard. We could test for this, but we'd need
2580 * to supress the Clang warning for the test. */
2581 psa_cipher_operation_t func = psa_cipher_operation_init( );
2582 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2583 psa_cipher_operation_t zero;
2584
2585 memset( &zero, 0, sizeof( zero ) );
2586
Jaeden Ameroab439972019-02-15 14:12:05 +00002587 /* A freshly-initialized cipher operation should not be usable. */
2588 TEST_EQUAL( psa_cipher_update( &func,
2589 input, sizeof( input ),
2590 output, sizeof( output ),
2591 &output_length ),
2592 PSA_ERROR_BAD_STATE );
2593 TEST_EQUAL( psa_cipher_update( &init,
2594 input, sizeof( input ),
2595 output, sizeof( output ),
2596 &output_length ),
2597 PSA_ERROR_BAD_STATE );
2598 TEST_EQUAL( psa_cipher_update( &zero,
2599 input, sizeof( input ),
2600 output, sizeof( output ),
2601 &output_length ),
2602 PSA_ERROR_BAD_STATE );
2603
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002604 /* A default cipher operation should be abortable without error. */
2605 PSA_ASSERT( psa_cipher_abort( &func ) );
2606 PSA_ASSERT( psa_cipher_abort( &init ) );
2607 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002608}
2609/* END_CASE */
2610
2611/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002612void cipher_setup( int key_type_arg,
2613 data_t *key,
2614 int alg_arg,
2615 int expected_status_arg )
2616{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002617 psa_key_type_t key_type = key_type_arg;
2618 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002619 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002620 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002621 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002622#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002623 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2624#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002625
Gilles Peskine8817f612018-12-18 00:18:46 +01002626 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002627
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002628 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2629 &operation, &status ) )
2630 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002631 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002632
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002633 /* The operation object should be reusable. */
2634#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2635 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2636 smoke_test_key_data,
2637 sizeof( smoke_test_key_data ),
2638 KNOWN_SUPPORTED_CIPHER_ALG,
2639 &operation, &status ) )
2640 goto exit;
2641 TEST_EQUAL( status, PSA_SUCCESS );
2642#endif
2643
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002644exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002645 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002646 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002647}
2648/* END_CASE */
2649
Ronald Cronee414c72021-03-18 18:50:08 +01002650/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002651void cipher_bad_order( )
2652{
Ronald Cron5425a212020-08-04 14:58:35 +02002653 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002654 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2655 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002657 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002658 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002659 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002660 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2661 0xaa, 0xaa, 0xaa, 0xaa };
2662 const uint8_t text[] = {
2663 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2664 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002665 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002666 size_t length = 0;
2667
2668 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2670 psa_set_key_algorithm( &attributes, alg );
2671 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002672 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2673 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002674
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002675 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002676 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002677 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002678 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002679 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002680 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002681 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002682 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002683
2684 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002685 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002686 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002687 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002688 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002689 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002690 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002691 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002692
Jaeden Ameroab439972019-02-15 14:12:05 +00002693 /* Generate an IV without calling setup beforehand. */
2694 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2695 buffer, sizeof( buffer ),
2696 &length ),
2697 PSA_ERROR_BAD_STATE );
2698 PSA_ASSERT( psa_cipher_abort( &operation ) );
2699
2700 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002701 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002702 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2703 buffer, sizeof( buffer ),
2704 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002705 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002706 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2707 buffer, sizeof( buffer ),
2708 &length ),
2709 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002710 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002711 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002712 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002713
2714 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002715 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002716 PSA_ASSERT( psa_cipher_set_iv( &operation,
2717 iv, sizeof( iv ) ) );
2718 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2719 buffer, sizeof( buffer ),
2720 &length ),
2721 PSA_ERROR_BAD_STATE );
2722 PSA_ASSERT( psa_cipher_abort( &operation ) );
2723
2724 /* Set an IV without calling setup beforehand. */
2725 TEST_EQUAL( psa_cipher_set_iv( &operation,
2726 iv, sizeof( iv ) ),
2727 PSA_ERROR_BAD_STATE );
2728 PSA_ASSERT( psa_cipher_abort( &operation ) );
2729
2730 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002731 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002732 PSA_ASSERT( psa_cipher_set_iv( &operation,
2733 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002734 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002735 TEST_EQUAL( psa_cipher_set_iv( &operation,
2736 iv, sizeof( iv ) ),
2737 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002738 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002739 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002740 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002741
2742 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002743 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002744 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2745 buffer, sizeof( buffer ),
2746 &length ) );
2747 TEST_EQUAL( psa_cipher_set_iv( &operation,
2748 iv, sizeof( iv ) ),
2749 PSA_ERROR_BAD_STATE );
2750 PSA_ASSERT( psa_cipher_abort( &operation ) );
2751
2752 /* Call update without calling setup beforehand. */
2753 TEST_EQUAL( psa_cipher_update( &operation,
2754 text, sizeof( text ),
2755 buffer, sizeof( buffer ),
2756 &length ),
2757 PSA_ERROR_BAD_STATE );
2758 PSA_ASSERT( psa_cipher_abort( &operation ) );
2759
2760 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002761 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002762 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002763 TEST_EQUAL( psa_cipher_update( &operation,
2764 text, sizeof( text ),
2765 buffer, sizeof( buffer ),
2766 &length ),
2767 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002768 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002769 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002770 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002771
2772 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002773 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002774 PSA_ASSERT( psa_cipher_set_iv( &operation,
2775 iv, sizeof( iv ) ) );
2776 PSA_ASSERT( psa_cipher_finish( &operation,
2777 buffer, sizeof( buffer ), &length ) );
2778 TEST_EQUAL( psa_cipher_update( &operation,
2779 text, sizeof( text ),
2780 buffer, sizeof( buffer ),
2781 &length ),
2782 PSA_ERROR_BAD_STATE );
2783 PSA_ASSERT( psa_cipher_abort( &operation ) );
2784
2785 /* Call finish without calling setup beforehand. */
2786 TEST_EQUAL( psa_cipher_finish( &operation,
2787 buffer, sizeof( buffer ), &length ),
2788 PSA_ERROR_BAD_STATE );
2789 PSA_ASSERT( psa_cipher_abort( &operation ) );
2790
2791 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002792 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002793 /* Not calling update means we are encrypting an empty buffer, which is OK
2794 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002795 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002796 TEST_EQUAL( psa_cipher_finish( &operation,
2797 buffer, sizeof( buffer ), &length ),
2798 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002799 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002800 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002801 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002802
2803 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002804 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002805 PSA_ASSERT( psa_cipher_set_iv( &operation,
2806 iv, sizeof( iv ) ) );
2807 PSA_ASSERT( psa_cipher_finish( &operation,
2808 buffer, sizeof( buffer ), &length ) );
2809 TEST_EQUAL( psa_cipher_finish( &operation,
2810 buffer, sizeof( buffer ), &length ),
2811 PSA_ERROR_BAD_STATE );
2812 PSA_ASSERT( psa_cipher_abort( &operation ) );
2813
Ronald Cron5425a212020-08-04 14:58:35 +02002814 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002815
Jaeden Ameroab439972019-02-15 14:12:05 +00002816exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002817 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002818 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002819}
2820/* END_CASE */
2821
2822/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002823void cipher_encrypt_fail( int alg_arg,
2824 int key_type_arg,
2825 data_t *key_data,
2826 data_t *input,
2827 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828{
Ronald Cron5425a212020-08-04 14:58:35 +02002829 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830 psa_status_t status;
2831 psa_key_type_t key_type = key_type_arg;
2832 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002833 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002834 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002835 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002836 size_t output_length = 0;
2837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2838
2839 if ( PSA_ERROR_BAD_STATE != expected_status )
2840 {
2841 PSA_ASSERT( psa_crypto_init( ) );
2842
2843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2844 psa_set_key_algorithm( &attributes, alg );
2845 psa_set_key_type( &attributes, key_type );
2846
2847 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2848 input->len );
2849 ASSERT_ALLOC( output, output_buffer_size );
2850
2851 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2852 &key ) );
2853 }
2854
2855 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2856 output_buffer_size, &output_length );
2857
2858 TEST_EQUAL( status, expected_status );
2859
2860exit:
2861 mbedtls_free( output );
2862 psa_destroy_key( key );
2863 PSA_DONE( );
2864}
2865/* END_CASE */
2866
2867/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02002868void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
2869 data_t *input, int iv_length,
2870 int expected_result )
2871{
2872 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2873 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2874 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2875 size_t output_buffer_size = 0;
2876 unsigned char *output = NULL;
2877
2878 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2879 ASSERT_ALLOC( output, output_buffer_size );
2880
2881 PSA_ASSERT( psa_crypto_init( ) );
2882
2883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2884 psa_set_key_algorithm( &attributes, alg );
2885 psa_set_key_type( &attributes, key_type );
2886
2887 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2888 &key ) );
2889 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2890 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
2891 iv_length ) );
2892
2893exit:
2894 psa_cipher_abort( &operation );
2895 mbedtls_free( output );
2896 psa_destroy_key( key );
2897 PSA_DONE( );
2898}
2899/* END_CASE */
2900
2901/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002902void cipher_encrypt_alg_without_iv( int alg_arg,
2903 int key_type_arg,
2904 data_t *key_data,
2905 data_t *input,
2906 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002907{
2908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2909 psa_key_type_t key_type = key_type_arg;
2910 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02002911 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2912 uint8_t iv[1] = { 0x5a };
2913 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002914 unsigned char *output = NULL;
2915 size_t output_buffer_size = 0;
2916 size_t output_length = 0;
2917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2918
2919 PSA_ASSERT( psa_crypto_init( ) );
2920
2921 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2922 psa_set_key_algorithm( &attributes, alg );
2923 psa_set_key_type( &attributes, key_type );
2924
2925 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2926 ASSERT_ALLOC( output, output_buffer_size );
2927
2928 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2929 &key ) );
2930
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02002931 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2932 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
2933 PSA_ERROR_BAD_STATE );
2934 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2935 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
2936 &iv_length ),
2937 PSA_ERROR_BAD_STATE );
2938
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002939 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2940 output_buffer_size, &output_length ) );
2941 TEST_ASSERT( output_length <=
2942 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2943 TEST_ASSERT( output_length <=
2944 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2945
2946 ASSERT_COMPARE( expected_output->x, expected_output->len,
2947 output, output_length );
2948exit:
2949 mbedtls_free( output );
2950 psa_destroy_key( key );
2951 PSA_DONE( );
2952}
2953/* END_CASE */
2954
2955/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01002956void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2957{
2958 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2959 psa_algorithm_t alg = alg_arg;
2960 psa_key_type_t key_type = key_type_arg;
2961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2962 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2963 psa_status_t status;
2964
2965 PSA_ASSERT( psa_crypto_init( ) );
2966
2967 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2968 psa_set_key_algorithm( &attributes, alg );
2969 psa_set_key_type( &attributes, key_type );
2970
2971 /* Usage of either of these two size macros would cause divide by zero
2972 * with incorrect key types previously. Input length should be irrelevant
2973 * here. */
2974 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2975 0 );
2976 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2977
2978
2979 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2980 &key ) );
2981
2982 /* Should fail due to invalid alg type (to support invalid key type).
2983 * Encrypt or decrypt will end up in the same place. */
2984 status = psa_cipher_encrypt_setup( &operation, key, alg );
2985
2986 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2987
2988exit:
2989 psa_cipher_abort( &operation );
2990 psa_destroy_key( key );
2991 PSA_DONE( );
2992}
2993/* END_CASE */
2994
2995/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002996void cipher_encrypt_validation( int alg_arg,
2997 int key_type_arg,
2998 data_t *key_data,
2999 data_t *input )
3000{
3001 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3002 psa_key_type_t key_type = key_type_arg;
3003 psa_algorithm_t alg = alg_arg;
3004 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3005 unsigned char *output1 = NULL;
3006 size_t output1_buffer_size = 0;
3007 size_t output1_length = 0;
3008 unsigned char *output2 = NULL;
3009 size_t output2_buffer_size = 0;
3010 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003011 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003012 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003014
Gilles Peskine8817f612018-12-18 00:18:46 +01003015 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003016
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003017 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3018 psa_set_key_algorithm( &attributes, alg );
3019 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003020
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003021 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3022 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3023 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3024 ASSERT_ALLOC( output1, output1_buffer_size );
3025 ASSERT_ALLOC( output2, output2_buffer_size );
3026
Ronald Cron5425a212020-08-04 14:58:35 +02003027 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3028 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003029
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003030 /* The one-shot cipher encryption uses generated iv so validating
3031 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003032 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3033 output1_buffer_size, &output1_length ) );
3034 TEST_ASSERT( output1_length <=
3035 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3036 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003037 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003038
3039 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3040 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003041
Gilles Peskine8817f612018-12-18 00:18:46 +01003042 PSA_ASSERT( psa_cipher_update( &operation,
3043 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003044 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003045 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003046 TEST_ASSERT( function_output_length <=
3047 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3048 TEST_ASSERT( function_output_length <=
3049 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003050 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003051
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003052 PSA_ASSERT( psa_cipher_finish( &operation,
3053 output2 + output2_length,
3054 output2_buffer_size - output2_length,
3055 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003056 TEST_ASSERT( function_output_length <=
3057 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3058 TEST_ASSERT( function_output_length <=
3059 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003060 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003061
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003062 PSA_ASSERT( psa_cipher_abort( &operation ) );
3063 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3064 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003065
Gilles Peskine50e586b2018-06-08 14:28:46 +02003066exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003067 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003068 mbedtls_free( output1 );
3069 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003070 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003071 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003072}
3073/* END_CASE */
3074
3075/* BEGIN_CASE */
3076void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003077 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003078 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003079 int first_part_size_arg,
3080 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003081 data_t *expected_output,
3082 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003083{
Ronald Cron5425a212020-08-04 14:58:35 +02003084 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003085 psa_key_type_t key_type = key_type_arg;
3086 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003087 psa_status_t status;
3088 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003089 size_t first_part_size = first_part_size_arg;
3090 size_t output1_length = output1_length_arg;
3091 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003092 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003093 size_t output_buffer_size = 0;
3094 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003095 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003096 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003098
Gilles Peskine8817f612018-12-18 00:18:46 +01003099 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003100
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003101 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3102 psa_set_key_algorithm( &attributes, alg );
3103 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003104
Ronald Cron5425a212020-08-04 14:58:35 +02003105 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3106 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003107
Ronald Cron5425a212020-08-04 14:58:35 +02003108 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003109
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003110 if( iv->len > 0 )
3111 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003112 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003113 }
3114
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003115 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3116 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003117 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003118
Gilles Peskinee0866522019-02-19 19:44:00 +01003119 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003120 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3121 output, output_buffer_size,
3122 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003123 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003124 TEST_ASSERT( function_output_length <=
3125 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3126 TEST_ASSERT( function_output_length <=
3127 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003128 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003129
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003130 if( first_part_size < input->len )
3131 {
3132 PSA_ASSERT( psa_cipher_update( &operation,
3133 input->x + first_part_size,
3134 input->len - first_part_size,
3135 ( output_buffer_size == 0 ? NULL :
3136 output + total_output_length ),
3137 output_buffer_size - total_output_length,
3138 &function_output_length ) );
3139 TEST_ASSERT( function_output_length == output2_length );
3140 TEST_ASSERT( function_output_length <=
3141 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3142 alg,
3143 input->len - first_part_size ) );
3144 TEST_ASSERT( function_output_length <=
3145 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3146 total_output_length += function_output_length;
3147 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003148
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003149 status = psa_cipher_finish( &operation,
3150 ( output_buffer_size == 0 ? NULL :
3151 output + total_output_length ),
3152 output_buffer_size - total_output_length,
3153 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003154 TEST_ASSERT( function_output_length <=
3155 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3156 TEST_ASSERT( function_output_length <=
3157 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003158 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003159 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003160
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003161 if( expected_status == PSA_SUCCESS )
3162 {
3163 PSA_ASSERT( psa_cipher_abort( &operation ) );
3164
3165 ASSERT_COMPARE( expected_output->x, expected_output->len,
3166 output, total_output_length );
3167 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003168
3169exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003170 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003171 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003172 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003173 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003174}
3175/* END_CASE */
3176
3177/* BEGIN_CASE */
3178void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003179 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003180 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003181 int first_part_size_arg,
3182 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003183 data_t *expected_output,
3184 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003185{
Ronald Cron5425a212020-08-04 14:58:35 +02003186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003187 psa_key_type_t key_type = key_type_arg;
3188 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003189 psa_status_t status;
3190 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003191 size_t first_part_size = first_part_size_arg;
3192 size_t output1_length = output1_length_arg;
3193 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003194 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003195 size_t output_buffer_size = 0;
3196 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003197 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003198 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003199 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003200
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003202
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003203 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3204 psa_set_key_algorithm( &attributes, alg );
3205 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003206
Ronald Cron5425a212020-08-04 14:58:35 +02003207 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3208 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003209
Ronald Cron5425a212020-08-04 14:58:35 +02003210 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003211
Steven Cooreman177deba2020-09-07 17:14:14 +02003212 if( iv->len > 0 )
3213 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003214 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003215 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003216
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003217 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3218 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003219 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003220
Gilles Peskinee0866522019-02-19 19:44:00 +01003221 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003222 PSA_ASSERT( psa_cipher_update( &operation,
3223 input->x, first_part_size,
3224 output, output_buffer_size,
3225 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003226 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003227 TEST_ASSERT( function_output_length <=
3228 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3229 TEST_ASSERT( function_output_length <=
3230 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003231 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003232
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003233 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003234 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003235 PSA_ASSERT( psa_cipher_update( &operation,
3236 input->x + first_part_size,
3237 input->len - first_part_size,
3238 ( output_buffer_size == 0 ? NULL :
3239 output + total_output_length ),
3240 output_buffer_size - total_output_length,
3241 &function_output_length ) );
3242 TEST_ASSERT( function_output_length == output2_length );
3243 TEST_ASSERT( function_output_length <=
3244 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3245 alg,
3246 input->len - first_part_size ) );
3247 TEST_ASSERT( function_output_length <=
3248 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3249 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003250 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003251
Gilles Peskine50e586b2018-06-08 14:28:46 +02003252 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003253 ( output_buffer_size == 0 ? NULL :
3254 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003255 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003256 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003257 TEST_ASSERT( function_output_length <=
3258 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3259 TEST_ASSERT( function_output_length <=
3260 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003261 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003262 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003263
3264 if( expected_status == PSA_SUCCESS )
3265 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003266 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003267
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003268 ASSERT_COMPARE( expected_output->x, expected_output->len,
3269 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003270 }
3271
Gilles Peskine50e586b2018-06-08 14:28:46 +02003272exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003273 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003274 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003275 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003276 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003277}
3278/* END_CASE */
3279
Gilles Peskine50e586b2018-06-08 14:28:46 +02003280/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003281void cipher_decrypt_fail( int alg_arg,
3282 int key_type_arg,
3283 data_t *key_data,
3284 data_t *iv,
3285 data_t *input_arg,
3286 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003287{
3288 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3289 psa_status_t status;
3290 psa_key_type_t key_type = key_type_arg;
3291 psa_algorithm_t alg = alg_arg;
3292 psa_status_t expected_status = expected_status_arg;
3293 unsigned char *input = NULL;
3294 size_t input_buffer_size = 0;
3295 unsigned char *output = NULL;
3296 size_t output_buffer_size = 0;
3297 size_t output_length = 0;
3298 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3299
3300 if ( PSA_ERROR_BAD_STATE != expected_status )
3301 {
3302 PSA_ASSERT( psa_crypto_init( ) );
3303
3304 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3305 psa_set_key_algorithm( &attributes, alg );
3306 psa_set_key_type( &attributes, key_type );
3307
3308 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3309 &key ) );
3310 }
3311
3312 /* Allocate input buffer and copy the iv and the plaintext */
3313 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3314 if ( input_buffer_size > 0 )
3315 {
3316 ASSERT_ALLOC( input, input_buffer_size );
3317 memcpy( input, iv->x, iv->len );
3318 memcpy( input + iv->len, input_arg->x, input_arg->len );
3319 }
3320
3321 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3322 ASSERT_ALLOC( output, output_buffer_size );
3323
3324 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3325 output_buffer_size, &output_length );
3326 TEST_EQUAL( status, expected_status );
3327
3328exit:
3329 mbedtls_free( input );
3330 mbedtls_free( output );
3331 psa_destroy_key( key );
3332 PSA_DONE( );
3333}
3334/* END_CASE */
3335
3336/* BEGIN_CASE */
3337void cipher_decrypt( int alg_arg,
3338 int key_type_arg,
3339 data_t *key_data,
3340 data_t *iv,
3341 data_t *input_arg,
3342 data_t *expected_output )
3343{
3344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3345 psa_key_type_t key_type = key_type_arg;
3346 psa_algorithm_t alg = alg_arg;
3347 unsigned char *input = NULL;
3348 size_t input_buffer_size = 0;
3349 unsigned char *output = NULL;
3350 size_t output_buffer_size = 0;
3351 size_t output_length = 0;
3352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3353
3354 PSA_ASSERT( psa_crypto_init( ) );
3355
3356 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3357 psa_set_key_algorithm( &attributes, alg );
3358 psa_set_key_type( &attributes, key_type );
3359
3360 /* Allocate input buffer and copy the iv and the plaintext */
3361 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3362 if ( input_buffer_size > 0 )
3363 {
3364 ASSERT_ALLOC( input, input_buffer_size );
3365 memcpy( input, iv->x, iv->len );
3366 memcpy( input + iv->len, input_arg->x, input_arg->len );
3367 }
3368
3369 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3370 ASSERT_ALLOC( output, output_buffer_size );
3371
3372 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3373 &key ) );
3374
3375 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3376 output_buffer_size, &output_length ) );
3377 TEST_ASSERT( output_length <=
3378 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3379 TEST_ASSERT( output_length <=
3380 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3381
3382 ASSERT_COMPARE( expected_output->x, expected_output->len,
3383 output, output_length );
3384exit:
3385 mbedtls_free( input );
3386 mbedtls_free( output );
3387 psa_destroy_key( key );
3388 PSA_DONE( );
3389}
3390/* END_CASE */
3391
3392/* BEGIN_CASE */
3393void cipher_verify_output( int alg_arg,
3394 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003395 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003396 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003397{
Ronald Cron5425a212020-08-04 14:58:35 +02003398 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003399 psa_key_type_t key_type = key_type_arg;
3400 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003401 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003402 size_t output1_size = 0;
3403 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003404 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003405 size_t output2_size = 0;
3406 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003408
Gilles Peskine8817f612018-12-18 00:18:46 +01003409 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003410
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3412 psa_set_key_algorithm( &attributes, alg );
3413 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003414
Ronald Cron5425a212020-08-04 14:58:35 +02003415 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3416 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003417 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003418 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003419
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003420 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3421 output1, output1_size,
3422 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003423 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003424 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003425 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003426 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003427
3428 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003429 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003430
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003431 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3432 output2, output2_size,
3433 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003434 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003435 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003436 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003437 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003438
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003439 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003440
3441exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003442 mbedtls_free( output1 );
3443 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003444 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003445 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003446}
3447/* END_CASE */
3448
3449/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003450void cipher_verify_output_multipart( int alg_arg,
3451 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003452 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003453 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003454 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003455{
Ronald Cron5425a212020-08-04 14:58:35 +02003456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003457 psa_key_type_t key_type = key_type_arg;
3458 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003459 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003460 unsigned char iv[16] = {0};
3461 size_t iv_size = 16;
3462 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003463 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003464 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003465 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003466 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003467 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003468 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003469 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003470 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3471 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003472 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003473
Gilles Peskine8817f612018-12-18 00:18:46 +01003474 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003475
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003476 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3477 psa_set_key_algorithm( &attributes, alg );
3478 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003479
Ronald Cron5425a212020-08-04 14:58:35 +02003480 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3481 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003482
Ronald Cron5425a212020-08-04 14:58:35 +02003483 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3484 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003485
Steven Cooreman177deba2020-09-07 17:14:14 +02003486 if( alg != PSA_ALG_ECB_NO_PADDING )
3487 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003488 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3489 iv, iv_size,
3490 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003491 }
3492
gabor-mezei-armceface22021-01-21 12:26:17 +01003493 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3494 TEST_ASSERT( output1_buffer_size <=
3495 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003496 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003497
Gilles Peskinee0866522019-02-19 19:44:00 +01003498 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003499
Gilles Peskine8817f612018-12-18 00:18:46 +01003500 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3501 output1, output1_buffer_size,
3502 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003503 TEST_ASSERT( function_output_length <=
3504 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3505 TEST_ASSERT( function_output_length <=
3506 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003507 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003508
Gilles Peskine8817f612018-12-18 00:18:46 +01003509 PSA_ASSERT( psa_cipher_update( &operation1,
3510 input->x + first_part_size,
3511 input->len - first_part_size,
3512 output1, output1_buffer_size,
3513 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003514 TEST_ASSERT( function_output_length <=
3515 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3516 alg,
3517 input->len - first_part_size ) );
3518 TEST_ASSERT( function_output_length <=
3519 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003520 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003521
Gilles Peskine8817f612018-12-18 00:18:46 +01003522 PSA_ASSERT( psa_cipher_finish( &operation1,
3523 output1 + output1_length,
3524 output1_buffer_size - output1_length,
3525 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003526 TEST_ASSERT( function_output_length <=
3527 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3528 TEST_ASSERT( function_output_length <=
3529 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003530 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003531
Gilles Peskine8817f612018-12-18 00:18:46 +01003532 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003533
Gilles Peskine048b7f02018-06-08 14:20:49 +02003534 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003535 TEST_ASSERT( output2_buffer_size <=
3536 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3537 TEST_ASSERT( output2_buffer_size <=
3538 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003539 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003540
Steven Cooreman177deba2020-09-07 17:14:14 +02003541 if( iv_length > 0 )
3542 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003543 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3544 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003545 }
Moran Pekerded84402018-06-06 16:36:50 +03003546
Gilles Peskine8817f612018-12-18 00:18:46 +01003547 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3548 output2, output2_buffer_size,
3549 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003550 TEST_ASSERT( function_output_length <=
3551 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3552 TEST_ASSERT( function_output_length <=
3553 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003554 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003555
Gilles Peskine8817f612018-12-18 00:18:46 +01003556 PSA_ASSERT( psa_cipher_update( &operation2,
3557 output1 + first_part_size,
3558 output1_length - first_part_size,
3559 output2, output2_buffer_size,
3560 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003561 TEST_ASSERT( function_output_length <=
3562 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3563 alg,
3564 output1_length - first_part_size ) );
3565 TEST_ASSERT( function_output_length <=
3566 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003567 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003568
Gilles Peskine8817f612018-12-18 00:18:46 +01003569 PSA_ASSERT( psa_cipher_finish( &operation2,
3570 output2 + output2_length,
3571 output2_buffer_size - output2_length,
3572 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003573 TEST_ASSERT( function_output_length <=
3574 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3575 TEST_ASSERT( function_output_length <=
3576 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003577 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003578
Gilles Peskine8817f612018-12-18 00:18:46 +01003579 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003580
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003581 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003582
3583exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003584 psa_cipher_abort( &operation1 );
3585 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003586 mbedtls_free( output1 );
3587 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003588 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003589 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003590}
3591/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003592
Gilles Peskine20035e32018-02-03 22:44:14 +01003593/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003594void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003595 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003596 data_t *nonce,
3597 data_t *additional_data,
3598 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003599 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003600{
Ronald Cron5425a212020-08-04 14:58:35 +02003601 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003602 psa_key_type_t key_type = key_type_arg;
3603 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003604 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003605 unsigned char *output_data = NULL;
3606 size_t output_size = 0;
3607 size_t output_length = 0;
3608 unsigned char *output_data2 = NULL;
3609 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003610 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003611 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003613
Gilles Peskine8817f612018-12-18 00:18:46 +01003614 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003615
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003616 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3617 psa_set_key_algorithm( &attributes, alg );
3618 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003619
Gilles Peskine049c7532019-05-15 20:22:09 +02003620 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003621 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003622 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3623 key_bits = psa_get_key_bits( &attributes );
3624
3625 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3626 alg );
3627 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3628 * should be exact. */
3629 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3630 expected_result != PSA_ERROR_NOT_SUPPORTED )
3631 {
3632 TEST_EQUAL( output_size,
3633 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3634 TEST_ASSERT( output_size <=
3635 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3636 }
3637 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003638
Steven Cooremanf49478b2021-02-15 15:19:25 +01003639 status = psa_aead_encrypt( key, alg,
3640 nonce->x, nonce->len,
3641 additional_data->x,
3642 additional_data->len,
3643 input_data->x, input_data->len,
3644 output_data, output_size,
3645 &output_length );
3646
3647 /* If the operation is not supported, just skip and not fail in case the
3648 * encryption involves a common limitation of cryptography hardwares and
3649 * an alternative implementation. */
3650 if( status == PSA_ERROR_NOT_SUPPORTED )
3651 {
3652 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3653 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3654 }
3655
3656 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003657
3658 if( PSA_SUCCESS == expected_result )
3659 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003660 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003661
Gilles Peskine003a4a92019-05-14 16:09:40 +02003662 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3663 * should be exact. */
3664 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003665 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003666
gabor-mezei-armceface22021-01-21 12:26:17 +01003667 TEST_ASSERT( input_data->len <=
3668 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3669
Ronald Cron5425a212020-08-04 14:58:35 +02003670 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003671 nonce->x, nonce->len,
3672 additional_data->x,
3673 additional_data->len,
3674 output_data, output_length,
3675 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003676 &output_length2 ),
3677 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003678
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003679 ASSERT_COMPARE( input_data->x, input_data->len,
3680 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003681 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003682
Gilles Peskinea1cac842018-06-11 19:33:02 +02003683exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003684 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003685 mbedtls_free( output_data );
3686 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003687 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003688}
3689/* END_CASE */
3690
3691/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003692void aead_encrypt( int key_type_arg, data_t *key_data,
3693 int alg_arg,
3694 data_t *nonce,
3695 data_t *additional_data,
3696 data_t *input_data,
3697 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003698{
Ronald Cron5425a212020-08-04 14:58:35 +02003699 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003700 psa_key_type_t key_type = key_type_arg;
3701 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003702 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003703 unsigned char *output_data = NULL;
3704 size_t output_size = 0;
3705 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003706 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003707 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003708
Gilles Peskine8817f612018-12-18 00:18:46 +01003709 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003710
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003711 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3712 psa_set_key_algorithm( &attributes, alg );
3713 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003714
Gilles Peskine049c7532019-05-15 20:22:09 +02003715 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003716 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003717 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3718 key_bits = psa_get_key_bits( &attributes );
3719
3720 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3721 alg );
3722 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3723 * should be exact. */
3724 TEST_EQUAL( output_size,
3725 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3726 TEST_ASSERT( output_size <=
3727 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3728 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003729
Steven Cooremand588ea12021-01-11 19:36:04 +01003730 status = psa_aead_encrypt( key, alg,
3731 nonce->x, nonce->len,
3732 additional_data->x, additional_data->len,
3733 input_data->x, input_data->len,
3734 output_data, output_size,
3735 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003736
Ronald Cron28a45ed2021-02-09 20:35:42 +01003737 /* If the operation is not supported, just skip and not fail in case the
3738 * encryption involves a common limitation of cryptography hardwares and
3739 * an alternative implementation. */
3740 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003741 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003742 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3743 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003744 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003745
3746 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003747 ASSERT_COMPARE( expected_result->x, expected_result->len,
3748 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003749
Gilles Peskinea1cac842018-06-11 19:33:02 +02003750exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003751 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003752 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003753 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003754}
3755/* END_CASE */
3756
3757/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003758void aead_decrypt( int key_type_arg, data_t *key_data,
3759 int alg_arg,
3760 data_t *nonce,
3761 data_t *additional_data,
3762 data_t *input_data,
3763 data_t *expected_data,
3764 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003765{
Ronald Cron5425a212020-08-04 14:58:35 +02003766 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003767 psa_key_type_t key_type = key_type_arg;
3768 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003769 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003770 unsigned char *output_data = NULL;
3771 size_t output_size = 0;
3772 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003774 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003775 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003776
Gilles Peskine8817f612018-12-18 00:18:46 +01003777 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003778
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003779 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3780 psa_set_key_algorithm( &attributes, alg );
3781 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003782
Gilles Peskine049c7532019-05-15 20:22:09 +02003783 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003784 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003785 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3786 key_bits = psa_get_key_bits( &attributes );
3787
3788 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3789 alg );
3790 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3791 expected_result != PSA_ERROR_NOT_SUPPORTED )
3792 {
3793 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3794 * should be exact. */
3795 TEST_EQUAL( output_size,
3796 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3797 TEST_ASSERT( output_size <=
3798 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3799 }
3800 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003801
Steven Cooremand588ea12021-01-11 19:36:04 +01003802 status = psa_aead_decrypt( key, alg,
3803 nonce->x, nonce->len,
3804 additional_data->x,
3805 additional_data->len,
3806 input_data->x, input_data->len,
3807 output_data, output_size,
3808 &output_length );
3809
Ronald Cron28a45ed2021-02-09 20:35:42 +01003810 /* If the operation is not supported, just skip and not fail in case the
3811 * decryption involves a common limitation of cryptography hardwares and
3812 * an alternative implementation. */
3813 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003814 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003815 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3816 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003817 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003818
3819 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003820
Gilles Peskine2d277862018-06-18 15:41:12 +02003821 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003822 ASSERT_COMPARE( expected_data->x, expected_data->len,
3823 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003824
Gilles Peskinea1cac842018-06-11 19:33:02 +02003825exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003826 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003827 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003828 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003829}
3830/* END_CASE */
3831
3832/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003833void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3834 int alg_arg,
3835 data_t *nonce,
3836 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003837 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003838 int do_set_lengths,
3839 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003840{
Paul Elliottd3f82412021-06-16 16:52:21 +01003841 size_t ad_part_len = 0;
3842 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003843 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003844
Paul Elliott32f46ba2021-09-23 18:24:36 +01003845 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003846 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003847 mbedtls_test_set_step( ad_part_len );
3848
3849 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003850 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003851 if( ad_part_len & 0x01 )
3852 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3853 else
3854 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003855 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003856
3857 /* Split ad into length(ad_part_len) parts. */
3858 if( !aead_multipart_internal_func( key_type_arg, key_data,
3859 alg_arg, nonce,
3860 additional_data,
3861 ad_part_len,
3862 input_data, -1,
3863 set_lengths_method,
3864 expected_output,
3865 1, 0 ) )
3866 break;
3867
3868 /* length(0) part, length(ad_part_len) part, length(0) part... */
3869 mbedtls_test_set_step( 1000 + ad_part_len );
3870
3871 if( !aead_multipart_internal_func( key_type_arg, key_data,
3872 alg_arg, nonce,
3873 additional_data,
3874 ad_part_len,
3875 input_data, -1,
3876 set_lengths_method,
3877 expected_output,
3878 1, 1 ) )
3879 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003880 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003881
Paul Elliott32f46ba2021-09-23 18:24:36 +01003882 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003883 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003884 /* Split data into length(data_part_len) parts. */
3885 mbedtls_test_set_step( 2000 + data_part_len );
3886
3887 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003888 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003889 if( data_part_len & 0x01 )
3890 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3891 else
3892 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003893 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003894
Paul Elliott32f46ba2021-09-23 18:24:36 +01003895 if( !aead_multipart_internal_func( key_type_arg, key_data,
3896 alg_arg, nonce,
3897 additional_data, -1,
3898 input_data, data_part_len,
3899 set_lengths_method,
3900 expected_output,
3901 1, 0 ) )
3902 break;
3903
3904 /* length(0) part, length(data_part_len) part, length(0) part... */
3905 mbedtls_test_set_step( 3000 + data_part_len );
3906
3907 if( !aead_multipart_internal_func( key_type_arg, key_data,
3908 alg_arg, nonce,
3909 additional_data, -1,
3910 input_data, data_part_len,
3911 set_lengths_method,
3912 expected_output,
3913 1, 1 ) )
3914 break;
3915 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003916
Paul Elliott8fc45162021-06-23 16:06:01 +01003917 /* Goto is required to silence warnings about unused labels, as we
3918 * don't actually do any test assertions in this function. */
3919 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003920}
3921/* END_CASE */
3922
3923/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003924void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3925 int alg_arg,
3926 data_t *nonce,
3927 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003928 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003929 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003930 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003931{
Paul Elliottd3f82412021-06-16 16:52:21 +01003932 size_t ad_part_len = 0;
3933 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003934 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003935
Paul Elliott32f46ba2021-09-23 18:24:36 +01003936 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003937 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003938 /* Split ad into length(ad_part_len) parts. */
3939 mbedtls_test_set_step( ad_part_len );
3940
3941 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003942 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003943 if( ad_part_len & 0x01 )
3944 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3945 else
3946 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003947 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003948
3949 if( !aead_multipart_internal_func( key_type_arg, key_data,
3950 alg_arg, nonce,
3951 additional_data,
3952 ad_part_len,
3953 input_data, -1,
3954 set_lengths_method,
3955 expected_output,
3956 0, 0 ) )
3957 break;
3958
3959 /* length(0) part, length(ad_part_len) part, length(0) part... */
3960 mbedtls_test_set_step( 1000 + ad_part_len );
3961
3962 if( !aead_multipart_internal_func( key_type_arg, key_data,
3963 alg_arg, nonce,
3964 additional_data,
3965 ad_part_len,
3966 input_data, -1,
3967 set_lengths_method,
3968 expected_output,
3969 0, 1 ) )
3970 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003971 }
3972
Paul Elliott32f46ba2021-09-23 18:24:36 +01003973 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003974 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003975 /* Split data into length(data_part_len) parts. */
3976 mbedtls_test_set_step( 2000 + data_part_len );
3977
3978 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003979 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003980 if( data_part_len & 0x01 )
3981 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3982 else
3983 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003984 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003985
3986 if( !aead_multipart_internal_func( key_type_arg, key_data,
3987 alg_arg, nonce,
3988 additional_data, -1,
3989 input_data, data_part_len,
3990 set_lengths_method,
3991 expected_output,
3992 0, 0 ) )
3993 break;
3994
3995 /* length(0) part, length(data_part_len) part, length(0) part... */
3996 mbedtls_test_set_step( 3000 + data_part_len );
3997
3998 if( !aead_multipart_internal_func( key_type_arg, key_data,
3999 alg_arg, nonce,
4000 additional_data, -1,
4001 input_data, data_part_len,
4002 set_lengths_method,
4003 expected_output,
4004 0, 1 ) )
4005 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004006 }
4007
Paul Elliott8fc45162021-06-23 16:06:01 +01004008 /* Goto is required to silence warnings about unused labels, as we
4009 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004010 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004011}
4012/* END_CASE */
4013
4014/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004015void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4016 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004017 int nonce_length,
4018 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004019 data_t *additional_data,
4020 data_t *input_data,
4021 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004022{
4023
4024 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4025 psa_key_type_t key_type = key_type_arg;
4026 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004027 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004028 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4030 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004031 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004032 size_t actual_nonce_length = 0;
4033 size_t expected_nonce_length = expected_nonce_length_arg;
4034 unsigned char *output = NULL;
4035 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004036 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004037 size_t ciphertext_size = 0;
4038 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004039 size_t tag_length = 0;
4040 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004041
4042 PSA_ASSERT( psa_crypto_init( ) );
4043
4044 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4045 psa_set_key_algorithm( & attributes, alg );
4046 psa_set_key_type( & attributes, key_type );
4047
4048 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4049 &key ) );
4050
4051 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4052
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004053 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4054
Paul Elliottf1277632021-08-24 18:11:37 +01004055 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004056
Paul Elliottf1277632021-08-24 18:11:37 +01004057 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004058
Paul Elliottf1277632021-08-24 18:11:37 +01004059 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004060
Paul Elliottf1277632021-08-24 18:11:37 +01004061 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004062
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004063 status = psa_aead_encrypt_setup( &operation, key, alg );
4064
4065 /* If the operation is not supported, just skip and not fail in case the
4066 * encryption involves a common limitation of cryptography hardwares and
4067 * an alternative implementation. */
4068 if( status == PSA_ERROR_NOT_SUPPORTED )
4069 {
4070 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004071 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004072 }
4073
4074 PSA_ASSERT( status );
4075
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004076 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004077 nonce_length,
4078 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004079
Paul Elliott693bf312021-07-23 17:40:41 +01004080 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004081
Paul Elliottf1277632021-08-24 18:11:37 +01004082 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004083
Paul Elliott88ecbe12021-09-22 17:23:03 +01004084 if( expected_status == PSA_SUCCESS )
4085 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4086 alg ) );
4087
Paul Elliottd79c5c52021-10-06 21:49:41 +01004088 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004089
Paul Elliott693bf312021-07-23 17:40:41 +01004090 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004091 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004092 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004093 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4094 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004095
4096 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4097 additional_data->len ) );
4098
4099 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004100 output, output_size,
4101 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004102
Paul Elliottf1277632021-08-24 18:11:37 +01004103 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4104 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004105 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4106 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004107
4108exit:
4109 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004110 mbedtls_free( output );
4111 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004112 psa_aead_abort( &operation );
4113 PSA_DONE( );
4114}
4115/* END_CASE */
4116
4117/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004118void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4119 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004120 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004121 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004122 data_t *additional_data,
4123 data_t *input_data,
4124 int expected_status_arg )
4125{
4126
4127 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4128 psa_key_type_t key_type = key_type_arg;
4129 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004130 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004131 uint8_t *nonce_buffer = NULL;
4132 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4133 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4134 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004135 unsigned char *output = NULL;
4136 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004137 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004138 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004139 size_t ciphertext_size = 0;
4140 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004141 size_t tag_length = 0;
4142 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004143 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004144 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004145
4146 PSA_ASSERT( psa_crypto_init( ) );
4147
4148 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4149 psa_set_key_algorithm( &attributes, alg );
4150 psa_set_key_type( &attributes, key_type );
4151
4152 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4153 &key ) );
4154
4155 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4156
4157 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4158
Paul Elliott6f0e7202021-08-25 12:57:18 +01004159 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004160
Paul Elliott6f0e7202021-08-25 12:57:18 +01004161 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004162
Paul Elliott6f0e7202021-08-25 12:57:18 +01004163 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004164
Paul Elliott6f0e7202021-08-25 12:57:18 +01004165 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004166
Paul Elliott863864a2021-07-23 17:28:31 +01004167 status = psa_aead_encrypt_setup( &operation, key, alg );
4168
4169 /* If the operation is not supported, just skip and not fail in case the
4170 * encryption involves a common limitation of cryptography hardwares and
4171 * an alternative implementation. */
4172 if( status == PSA_ERROR_NOT_SUPPORTED )
4173 {
4174 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004175 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004176 }
4177
4178 PSA_ASSERT( status );
4179
Paul Elliott4023ffd2021-09-10 16:21:22 +01004180 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4181 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004182 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004183 /* Arbitrary size buffer, to test zero length valid buffer. */
4184 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004185 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004186 }
4187 else
4188 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004189 /* If length is zero, then this will return NULL. */
4190 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004191 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004192
Paul Elliott4023ffd2021-09-10 16:21:22 +01004193 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004194 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004195 for( index = 0; index < nonce_length - 1; ++index )
4196 {
4197 nonce_buffer[index] = 'a' + index;
4198 }
Paul Elliott66696b52021-08-16 18:42:41 +01004199 }
Paul Elliott863864a2021-07-23 17:28:31 +01004200 }
4201
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004202 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4203 {
4204 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4205 input_data->len ) );
4206 }
4207
Paul Elliott6f0e7202021-08-25 12:57:18 +01004208 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004209
Paul Elliott693bf312021-07-23 17:40:41 +01004210 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004211
4212 if( expected_status == PSA_SUCCESS )
4213 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004214 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4215 {
4216 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4217 input_data->len ) );
4218 }
4219 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4220 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004221
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004222 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4223 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4224 additional_data->len ),
4225 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004226
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004227 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004228 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004229 &ciphertext_length ),
4230 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004231
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004232 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004233 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004234 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4235 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004236 }
4237
4238exit:
4239 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004240 mbedtls_free( output );
4241 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004242 mbedtls_free( nonce_buffer );
4243 psa_aead_abort( &operation );
4244 PSA_DONE( );
4245}
4246/* END_CASE */
4247
4248/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004249void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4250 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004251 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004252 data_t *nonce,
4253 data_t *additional_data,
4254 data_t *input_data,
4255 int expected_status_arg )
4256{
4257
4258 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4259 psa_key_type_t key_type = key_type_arg;
4260 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004261 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004262 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4263 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4264 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004265 unsigned char *output = NULL;
4266 unsigned char *ciphertext = NULL;
4267 size_t output_size = output_size_arg;
4268 size_t ciphertext_size = 0;
4269 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004270 size_t tag_length = 0;
4271 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4272
4273 PSA_ASSERT( psa_crypto_init( ) );
4274
4275 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4276 psa_set_key_algorithm( &attributes, alg );
4277 psa_set_key_type( &attributes, key_type );
4278
4279 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4280 &key ) );
4281
4282 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4283
Paul Elliottc6d11d02021-09-01 12:04:23 +01004284 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004285
Paul Elliottc6d11d02021-09-01 12:04:23 +01004286 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004287
Paul Elliottc6d11d02021-09-01 12:04:23 +01004288 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004289
Paul Elliott43fbda62021-07-23 18:30:59 +01004290 status = psa_aead_encrypt_setup( &operation, key, alg );
4291
4292 /* If the operation is not supported, just skip and not fail in case the
4293 * encryption involves a common limitation of cryptography hardwares and
4294 * an alternative implementation. */
4295 if( status == PSA_ERROR_NOT_SUPPORTED )
4296 {
4297 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4298 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4299 }
4300
4301 PSA_ASSERT( status );
4302
Paul Elliott47b9a142021-10-07 15:04:57 +01004303 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4304 input_data->len ) );
4305
Paul Elliott43fbda62021-07-23 18:30:59 +01004306 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4307
4308 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4309 additional_data->len ) );
4310
4311 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004312 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004313
4314 TEST_EQUAL( status, expected_status );
4315
4316 if( expected_status == PSA_SUCCESS )
4317 {
4318 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004319 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4320 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004321 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4322 }
4323
4324exit:
4325 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004326 mbedtls_free( output );
4327 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004328 psa_aead_abort( &operation );
4329 PSA_DONE( );
4330}
4331/* END_CASE */
4332
Paul Elliott91b021e2021-07-23 18:52:31 +01004333/* BEGIN_CASE */
4334void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4335 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004336 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004337 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004338 data_t *nonce,
4339 data_t *additional_data,
4340 data_t *input_data,
4341 int expected_status_arg )
4342{
4343
4344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4345 psa_key_type_t key_type = key_type_arg;
4346 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004347 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4349 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4350 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004351 unsigned char *ciphertext = NULL;
4352 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004353 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004354 size_t ciphertext_size = 0;
4355 size_t ciphertext_length = 0;
4356 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004357 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004358 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004359
4360 PSA_ASSERT( psa_crypto_init( ) );
4361
4362 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4363 psa_set_key_algorithm( &attributes, alg );
4364 psa_set_key_type( &attributes, key_type );
4365
4366 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4367 &key ) );
4368
4369 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4370
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004371 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004372
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004373 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004374
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004375 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004376
Paul Elliott719c1322021-09-13 18:27:22 +01004377 ASSERT_ALLOC( tag_buffer, tag_size );
4378
Paul Elliott91b021e2021-07-23 18:52:31 +01004379 status = psa_aead_encrypt_setup( &operation, key, alg );
4380
4381 /* If the operation is not supported, just skip and not fail in case the
4382 * encryption involves a common limitation of cryptography hardwares and
4383 * an alternative implementation. */
4384 if( status == PSA_ERROR_NOT_SUPPORTED )
4385 {
4386 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4387 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4388 }
4389
4390 PSA_ASSERT( status );
4391
4392 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4393
Paul Elliott76bda482021-10-07 17:07:23 +01004394 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4395 input_data->len ) );
4396
Paul Elliott91b021e2021-07-23 18:52:31 +01004397 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4398 additional_data->len ) );
4399
4400 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004401 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004402
4403 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004404 status = psa_aead_finish( &operation, finish_ciphertext,
4405 finish_ciphertext_size,
4406 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004407 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004408
4409 TEST_EQUAL( status, expected_status );
4410
4411exit:
4412 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004413 mbedtls_free( ciphertext );
4414 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004415 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004416 psa_aead_abort( &operation );
4417 PSA_DONE( );
4418}
4419/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004420
4421/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004422void aead_multipart_verify( int key_type_arg, data_t *key_data,
4423 int alg_arg,
4424 data_t *nonce,
4425 data_t *additional_data,
4426 data_t *input_data,
4427 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004428 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004429 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004430 int expected_status_arg )
4431{
4432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4433 psa_key_type_t key_type = key_type_arg;
4434 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004435 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4437 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4438 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004439 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004440 unsigned char *plaintext = NULL;
4441 unsigned char *finish_plaintext = NULL;
4442 size_t plaintext_size = 0;
4443 size_t plaintext_length = 0;
4444 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004445 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004446 unsigned char *tag_buffer = NULL;
4447 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004448
4449 PSA_ASSERT( psa_crypto_init( ) );
4450
4451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4452 psa_set_key_algorithm( &attributes, alg );
4453 psa_set_key_type( &attributes, key_type );
4454
4455 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4456 &key ) );
4457
4458 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4459
4460 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4461 input_data->len );
4462
4463 ASSERT_ALLOC( plaintext, plaintext_size );
4464
4465 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4466
4467 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4468
Paul Elliott9961a662021-09-17 19:19:02 +01004469 status = psa_aead_decrypt_setup( &operation, key, alg );
4470
4471 /* If the operation is not supported, just skip and not fail in case the
4472 * encryption involves a common limitation of cryptography hardwares and
4473 * an alternative implementation. */
4474 if( status == PSA_ERROR_NOT_SUPPORTED )
4475 {
4476 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4477 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4478 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004479 TEST_EQUAL( status, expected_setup_status );
4480
4481 if( status != PSA_SUCCESS )
4482 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004483
4484 PSA_ASSERT( status );
4485
4486 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4487
Paul Elliottfec6f372021-10-06 17:15:02 +01004488 status = psa_aead_set_lengths( &operation, additional_data->len,
4489 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004490 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004491
Paul Elliott9961a662021-09-17 19:19:02 +01004492 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4493 additional_data->len ) );
4494
4495 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4496 input_data->len,
4497 plaintext, plaintext_size,
4498 &plaintext_length ) );
4499
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004500 if( tag_usage == USE_GIVEN_TAG )
4501 {
4502 tag_buffer = tag->x;
4503 tag_size = tag->len;
4504 }
4505
Paul Elliott9961a662021-09-17 19:19:02 +01004506 status = psa_aead_verify( &operation, finish_plaintext,
4507 verify_plaintext_size,
4508 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004509 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004510
4511 TEST_EQUAL( status, expected_status );
4512
4513exit:
4514 psa_destroy_key( key );
4515 mbedtls_free( plaintext );
4516 mbedtls_free( finish_plaintext );
4517 psa_aead_abort( &operation );
4518 PSA_DONE( );
4519}
4520/* END_CASE */
4521
Paul Elliott9961a662021-09-17 19:19:02 +01004522/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004523void aead_multipart_setup( int key_type_arg, data_t *key_data,
4524 int alg_arg, int expected_status_arg )
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 Elliott5221ef62021-09-19 17:33:03 +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;
4533
4534 PSA_ASSERT( psa_crypto_init( ) );
4535
4536 psa_set_key_usage_flags( &attributes,
4537 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4538 psa_set_key_algorithm( &attributes, alg );
4539 psa_set_key_type( &attributes, key_type );
4540
4541 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4542 &key ) );
4543
Paul Elliott5221ef62021-09-19 17:33:03 +01004544 status = psa_aead_encrypt_setup( &operation, key, alg );
4545
4546 TEST_EQUAL( status, expected_status );
4547
4548 psa_aead_abort( &operation );
4549
Paul Elliott5221ef62021-09-19 17:33:03 +01004550 status = psa_aead_decrypt_setup( &operation, key, alg );
4551
4552 TEST_EQUAL(status, expected_status );
4553
4554exit:
4555 psa_destroy_key( key );
4556 psa_aead_abort( &operation );
4557 PSA_DONE( );
4558}
4559/* END_CASE */
4560
4561/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004562void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4563 int alg_arg,
4564 data_t *nonce,
4565 data_t *additional_data,
4566 data_t *input_data )
4567{
4568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4569 psa_key_type_t key_type = key_type_arg;
4570 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004571 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004572 unsigned char *output_data = NULL;
4573 unsigned char *final_data = NULL;
4574 size_t output_size = 0;
4575 size_t finish_output_size = 0;
4576 size_t output_length = 0;
4577 size_t key_bits = 0;
4578 size_t tag_length = 0;
4579 size_t tag_size = 0;
4580 size_t nonce_length = 0;
4581 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4582 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4583 size_t output_part_length = 0;
4584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4585
4586 PSA_ASSERT( psa_crypto_init( ) );
4587
4588 psa_set_key_usage_flags( & attributes,
4589 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4590 psa_set_key_algorithm( & attributes, alg );
4591 psa_set_key_type( & attributes, key_type );
4592
4593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4594 &key ) );
4595
4596 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4597 key_bits = psa_get_key_bits( &attributes );
4598
4599 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4600
4601 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4602
4603 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4604
4605 ASSERT_ALLOC( output_data, output_size );
4606
4607 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4608
4609 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4610
4611 ASSERT_ALLOC( final_data, finish_output_size );
4612
4613 /* Test all operations error without calling setup first. */
4614
Paul Elliottc23a9a02021-06-21 18:32:46 +01004615 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4616 PSA_ERROR_BAD_STATE );
4617
4618 psa_aead_abort( &operation );
4619
Paul Elliottc23a9a02021-06-21 18:32:46 +01004620 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4621 PSA_AEAD_NONCE_MAX_SIZE,
4622 &nonce_length ),
4623 PSA_ERROR_BAD_STATE );
4624
4625 psa_aead_abort( &operation );
4626
Paul Elliott481be342021-07-16 17:38:47 +01004627 /* ------------------------------------------------------- */
4628
Paul Elliottc23a9a02021-06-21 18:32:46 +01004629 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4630 input_data->len ),
4631 PSA_ERROR_BAD_STATE );
4632
4633 psa_aead_abort( &operation );
4634
Paul Elliott481be342021-07-16 17:38:47 +01004635 /* ------------------------------------------------------- */
4636
Paul Elliottc23a9a02021-06-21 18:32:46 +01004637 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4638 additional_data->len ),
4639 PSA_ERROR_BAD_STATE );
4640
4641 psa_aead_abort( &operation );
4642
Paul Elliott481be342021-07-16 17:38:47 +01004643 /* ------------------------------------------------------- */
4644
Paul Elliottc23a9a02021-06-21 18:32:46 +01004645 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4646 input_data->len, output_data,
4647 output_size, &output_length ),
4648 PSA_ERROR_BAD_STATE );
4649
4650 psa_aead_abort( &operation );
4651
Paul Elliott481be342021-07-16 17:38:47 +01004652 /* ------------------------------------------------------- */
4653
Paul Elliottc23a9a02021-06-21 18:32:46 +01004654 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4655 finish_output_size,
4656 &output_part_length,
4657 tag_buffer, tag_length,
4658 &tag_size ),
4659 PSA_ERROR_BAD_STATE );
4660
4661 psa_aead_abort( &operation );
4662
Paul Elliott481be342021-07-16 17:38:47 +01004663 /* ------------------------------------------------------- */
4664
Paul Elliottc23a9a02021-06-21 18:32:46 +01004665 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4666 finish_output_size,
4667 &output_part_length,
4668 tag_buffer,
4669 tag_length ),
4670 PSA_ERROR_BAD_STATE );
4671
4672 psa_aead_abort( &operation );
4673
4674 /* Test for double setups. */
4675
Paul Elliottc23a9a02021-06-21 18:32:46 +01004676 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4677
4678 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4679 PSA_ERROR_BAD_STATE );
4680
4681 psa_aead_abort( &operation );
4682
Paul Elliott481be342021-07-16 17:38:47 +01004683 /* ------------------------------------------------------- */
4684
Paul Elliottc23a9a02021-06-21 18:32:46 +01004685 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4686
4687 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4688 PSA_ERROR_BAD_STATE );
4689
4690 psa_aead_abort( &operation );
4691
Paul Elliott374a2be2021-07-16 17:53:40 +01004692 /* ------------------------------------------------------- */
4693
Paul Elliott374a2be2021-07-16 17:53:40 +01004694 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4695
4696 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4697 PSA_ERROR_BAD_STATE );
4698
4699 psa_aead_abort( &operation );
4700
4701 /* ------------------------------------------------------- */
4702
Paul Elliott374a2be2021-07-16 17:53:40 +01004703 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4704
4705 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4706 PSA_ERROR_BAD_STATE );
4707
4708 psa_aead_abort( &operation );
4709
Paul Elliottc23a9a02021-06-21 18:32:46 +01004710 /* Test for not setting a nonce. */
4711
Paul Elliottc23a9a02021-06-21 18:32:46 +01004712 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4713
4714 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4715 additional_data->len ),
4716 PSA_ERROR_BAD_STATE );
4717
4718 psa_aead_abort( &operation );
4719
Paul Elliott7f628422021-09-01 12:08:29 +01004720 /* ------------------------------------------------------- */
4721
Paul Elliott7f628422021-09-01 12:08:29 +01004722 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4723
4724 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4725 input_data->len, output_data,
4726 output_size, &output_length ),
4727 PSA_ERROR_BAD_STATE );
4728
4729 psa_aead_abort( &operation );
4730
Paul Elliottbdc2c682021-09-21 18:37:10 +01004731 /* ------------------------------------------------------- */
4732
Paul Elliottbdc2c682021-09-21 18:37:10 +01004733 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4734
4735 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4736 finish_output_size,
4737 &output_part_length,
4738 tag_buffer, tag_length,
4739 &tag_size ),
4740 PSA_ERROR_BAD_STATE );
4741
4742 psa_aead_abort( &operation );
4743
4744 /* ------------------------------------------------------- */
4745
Paul Elliottbdc2c682021-09-21 18:37:10 +01004746 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4747
4748 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4749 finish_output_size,
4750 &output_part_length,
4751 tag_buffer,
4752 tag_length ),
4753 PSA_ERROR_BAD_STATE );
4754
4755 psa_aead_abort( &operation );
4756
Paul Elliottc23a9a02021-06-21 18:32:46 +01004757 /* Test for double setting nonce. */
4758
Paul Elliottc23a9a02021-06-21 18:32:46 +01004759 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4760
4761 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4762
4763 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4764 PSA_ERROR_BAD_STATE );
4765
4766 psa_aead_abort( &operation );
4767
Paul Elliott374a2be2021-07-16 17:53:40 +01004768 /* Test for double generating nonce. */
4769
Paul Elliott374a2be2021-07-16 17:53:40 +01004770 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4771
4772 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4773 PSA_AEAD_NONCE_MAX_SIZE,
4774 &nonce_length ) );
4775
4776 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4777 PSA_AEAD_NONCE_MAX_SIZE,
4778 &nonce_length ),
4779 PSA_ERROR_BAD_STATE );
4780
4781
4782 psa_aead_abort( &operation );
4783
4784 /* Test for generate nonce then set and vice versa */
4785
Paul Elliott374a2be2021-07-16 17:53:40 +01004786 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4787
4788 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4789 PSA_AEAD_NONCE_MAX_SIZE,
4790 &nonce_length ) );
4791
4792 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4793 PSA_ERROR_BAD_STATE );
4794
4795 psa_aead_abort( &operation );
4796
Andrzej Kurekad837522021-12-15 15:28:49 +01004797 /* Test for generating nonce after calling set lengths */
4798
4799 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4800
4801 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4802 input_data->len ) );
4803
4804 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4805 PSA_AEAD_NONCE_MAX_SIZE,
4806 &nonce_length ) );
4807
4808 psa_aead_abort( &operation );
4809
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004810 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004811
4812 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4813
4814 if( operation.alg == PSA_ALG_CCM )
4815 {
4816 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4817 input_data->len ),
4818 PSA_ERROR_INVALID_ARGUMENT );
4819 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4820 PSA_AEAD_NONCE_MAX_SIZE,
4821 &nonce_length ),
4822 PSA_ERROR_BAD_STATE );
4823 }
4824 else
4825 {
4826 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4827 input_data->len ) );
4828 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4829 PSA_AEAD_NONCE_MAX_SIZE,
4830 &nonce_length ) );
4831 }
4832
4833 psa_aead_abort( &operation );
4834
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004835 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004836#if SIZE_MAX > UINT32_MAX
4837 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4838
4839 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
4840 {
4841 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
4842 input_data->len ),
4843 PSA_ERROR_INVALID_ARGUMENT );
4844 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4845 PSA_AEAD_NONCE_MAX_SIZE,
4846 &nonce_length ),
4847 PSA_ERROR_BAD_STATE );
4848 }
4849 else
4850 {
4851 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
4852 input_data->len ) );
4853 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4854 PSA_AEAD_NONCE_MAX_SIZE,
4855 &nonce_length ) );
4856 }
4857
4858 psa_aead_abort( &operation );
4859#endif
4860
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004861 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01004862
4863 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4864
4865 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4866 PSA_AEAD_NONCE_MAX_SIZE,
4867 &nonce_length ) );
4868
4869 if( operation.alg == PSA_ALG_CCM )
4870 {
4871 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4872 input_data->len ),
4873 PSA_ERROR_INVALID_ARGUMENT );
4874 }
4875 else
4876 {
4877 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4878 input_data->len ) );
4879 }
4880
4881 psa_aead_abort( &operation );
4882
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004883 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004884 /* Test for setting nonce after calling set lengths */
4885
4886 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4887
4888 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4889 input_data->len ) );
4890
4891 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4892
4893 psa_aead_abort( &operation );
4894
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004895 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004896
4897 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4898
4899 if( operation.alg == PSA_ALG_CCM )
4900 {
4901 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4902 input_data->len ),
4903 PSA_ERROR_INVALID_ARGUMENT );
4904 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4905 PSA_ERROR_BAD_STATE );
4906 }
4907 else
4908 {
4909 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4910 input_data->len ) );
4911 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4912 }
4913
4914 psa_aead_abort( &operation );
4915
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004916 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004917#if SIZE_MAX > UINT32_MAX
4918 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4919
4920 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
4921 {
4922 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
4923 input_data->len ),
4924 PSA_ERROR_INVALID_ARGUMENT );
4925 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4926 PSA_ERROR_BAD_STATE );
4927 }
4928 else
4929 {
4930 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
4931 input_data->len ) );
4932 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4933 }
4934
4935 psa_aead_abort( &operation );
4936#endif
4937
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004938 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004939
4940 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4941
4942 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4943
4944 if( operation.alg == PSA_ALG_CCM )
4945 {
4946 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4947 input_data->len ),
4948 PSA_ERROR_INVALID_ARGUMENT );
4949 }
4950 else
4951 {
4952 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4953 input_data->len ) );
4954 }
4955
4956 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01004957
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004958 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004959#if SIZE_MAX > UINT32_MAX
4960 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4961
4962 if( operation.alg == PSA_ALG_GCM )
4963 {
4964 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4965 SIZE_MAX ),
4966 PSA_ERROR_INVALID_ARGUMENT );
4967 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4968 PSA_ERROR_BAD_STATE );
4969 }
4970 else if ( operation.alg != PSA_ALG_CCM )
4971 {
4972 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4973 SIZE_MAX ) );
4974 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4975 }
4976
4977 psa_aead_abort( &operation );
4978
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004979 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004980 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4981
4982 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4983
4984 if( operation.alg == PSA_ALG_GCM )
4985 {
4986 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4987 SIZE_MAX ),
4988 PSA_ERROR_INVALID_ARGUMENT );
4989 }
4990 else if ( operation.alg != PSA_ALG_CCM )
4991 {
4992 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4993 SIZE_MAX ) );
4994 }
4995
4996 psa_aead_abort( &operation );
4997#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01004998
4999 /* ------------------------------------------------------- */
5000
Paul Elliott374a2be2021-07-16 17:53:40 +01005001 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5002
5003 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5004
5005 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5006 PSA_AEAD_NONCE_MAX_SIZE,
5007 &nonce_length ),
5008 PSA_ERROR_BAD_STATE );
5009
5010 psa_aead_abort( &operation );
5011
Paul Elliott7220cae2021-06-22 17:25:57 +01005012 /* Test for generating nonce in decrypt setup. */
5013
Paul Elliott7220cae2021-06-22 17:25:57 +01005014 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5015
5016 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5017 PSA_AEAD_NONCE_MAX_SIZE,
5018 &nonce_length ),
5019 PSA_ERROR_BAD_STATE );
5020
5021 psa_aead_abort( &operation );
5022
Paul Elliottc23a9a02021-06-21 18:32:46 +01005023 /* Test for setting lengths twice. */
5024
Paul Elliottc23a9a02021-06-21 18:32:46 +01005025 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5026
5027 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5028
5029 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5030 input_data->len ) );
5031
5032 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5033 input_data->len ),
5034 PSA_ERROR_BAD_STATE );
5035
5036 psa_aead_abort( &operation );
5037
Andrzej Kurekad837522021-12-15 15:28:49 +01005038 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005039
Paul Elliottc23a9a02021-06-21 18:32:46 +01005040 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5041
5042 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5043
Andrzej Kurekad837522021-12-15 15:28:49 +01005044 if( operation.alg == PSA_ALG_CCM )
5045 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005046
Andrzej Kurekad837522021-12-15 15:28:49 +01005047 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5048 additional_data->len ),
5049 PSA_ERROR_BAD_STATE );
5050 }
5051 else
5052 {
5053 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5054 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005055
Andrzej Kurekad837522021-12-15 15:28:49 +01005056 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5057 input_data->len ),
5058 PSA_ERROR_BAD_STATE );
5059 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005060 psa_aead_abort( &operation );
5061
5062 /* ------------------------------------------------------- */
5063
Paul Elliottf94bd992021-09-19 18:15:59 +01005064 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5065
5066 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5067
Andrzej Kurekad837522021-12-15 15:28:49 +01005068 if( operation.alg == PSA_ALG_CCM )
5069 {
5070 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5071 input_data->len, output_data,
5072 output_size, &output_length ),
5073 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005074
Andrzej Kurekad837522021-12-15 15:28:49 +01005075 }
5076 else
5077 {
5078 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5079 input_data->len, output_data,
5080 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005081
Andrzej Kurekad837522021-12-15 15:28:49 +01005082 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5083 input_data->len ),
5084 PSA_ERROR_BAD_STATE );
5085 }
5086 psa_aead_abort( &operation );
5087
5088 /* ------------------------------------------------------- */
5089
5090 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5091
5092 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5093
5094 if( operation.alg == PSA_ALG_CCM )
5095 {
5096 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5097 finish_output_size,
5098 &output_part_length,
5099 tag_buffer, tag_length,
5100 &tag_size ) );
5101 }
5102 else
5103 {
5104 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5105 finish_output_size,
5106 &output_part_length,
5107 tag_buffer, tag_length,
5108 &tag_size ) );
5109
5110 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5111 input_data->len ),
5112 PSA_ERROR_BAD_STATE );
5113 }
5114 psa_aead_abort( &operation );
5115
5116 /* Test for setting lengths after generating nonce + already starting data. */
5117
5118 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5119
5120 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5121 PSA_AEAD_NONCE_MAX_SIZE,
5122 &nonce_length ) );
5123 if( operation.alg == PSA_ALG_CCM )
5124 {
5125
5126 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5127 additional_data->len ),
5128 PSA_ERROR_BAD_STATE );
5129 }
5130 else
5131 {
5132 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5133 additional_data->len ) );
5134
5135 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5136 input_data->len ),
5137 PSA_ERROR_BAD_STATE );
5138 }
5139 psa_aead_abort( &operation );
5140
5141 /* ------------------------------------------------------- */
5142
5143 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5144
5145 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5146 PSA_AEAD_NONCE_MAX_SIZE,
5147 &nonce_length ) );
5148 if( operation.alg == PSA_ALG_CCM )
5149 {
5150 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5151 input_data->len, output_data,
5152 output_size, &output_length ),
5153 PSA_ERROR_BAD_STATE );
5154
5155 }
5156 else
5157 {
5158 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5159 input_data->len, output_data,
5160 output_size, &output_length ) );
5161
5162 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5163 input_data->len ),
5164 PSA_ERROR_BAD_STATE );
5165 }
5166 psa_aead_abort( &operation );
5167
5168 /* ------------------------------------------------------- */
5169
5170 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5171
5172 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5173 PSA_AEAD_NONCE_MAX_SIZE,
5174 &nonce_length ) );
5175 if( operation.alg == PSA_ALG_CCM )
5176 {
5177 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5178 finish_output_size,
5179 &output_part_length,
5180 tag_buffer, tag_length,
5181 &tag_size ) );
5182 }
5183 else
5184 {
5185 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5186 finish_output_size,
5187 &output_part_length,
5188 tag_buffer, tag_length,
5189 &tag_size ) );
5190
5191 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5192 input_data->len ),
5193 PSA_ERROR_BAD_STATE );
5194 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005195 psa_aead_abort( &operation );
5196
Paul Elliott243080c2021-07-21 19:01:17 +01005197 /* Test for not sending any additional data or data after setting non zero
5198 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005199
Paul Elliottc23a9a02021-06-21 18:32:46 +01005200 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5201
5202 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5203
5204 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5205 input_data->len ) );
5206
5207 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5208 finish_output_size,
5209 &output_part_length,
5210 tag_buffer, tag_length,
5211 &tag_size ),
5212 PSA_ERROR_INVALID_ARGUMENT );
5213
5214 psa_aead_abort( &operation );
5215
Paul Elliott243080c2021-07-21 19:01:17 +01005216 /* Test for not sending any additional data or data after setting non-zero
5217 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005218
Paul Elliottc23a9a02021-06-21 18:32:46 +01005219 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5220
5221 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5222
5223 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5224 input_data->len ) );
5225
5226 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5227 finish_output_size,
5228 &output_part_length,
5229 tag_buffer,
5230 tag_length ),
5231 PSA_ERROR_INVALID_ARGUMENT );
5232
5233 psa_aead_abort( &operation );
5234
Paul Elliott243080c2021-07-21 19:01:17 +01005235 /* Test for not sending any additional data after setting a non-zero length
5236 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005237
Paul Elliottc23a9a02021-06-21 18:32:46 +01005238 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5239
5240 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5241
5242 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5243 input_data->len ) );
5244
5245 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5246 input_data->len, output_data,
5247 output_size, &output_length ),
5248 PSA_ERROR_INVALID_ARGUMENT );
5249
5250 psa_aead_abort( &operation );
5251
Paul Elliottf94bd992021-09-19 18:15:59 +01005252 /* Test for not sending any data after setting a non-zero length for it.*/
5253
Paul Elliottf94bd992021-09-19 18:15:59 +01005254 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5255
5256 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5257
5258 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5259 input_data->len ) );
5260
5261 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5262 additional_data->len ) );
5263
5264 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5265 finish_output_size,
5266 &output_part_length,
5267 tag_buffer, tag_length,
5268 &tag_size ),
5269 PSA_ERROR_INVALID_ARGUMENT );
5270
5271 psa_aead_abort( &operation );
5272
Paul Elliottb0450fe2021-09-01 15:06:26 +01005273 /* Test for sending too much additional data after setting lengths. */
5274
Paul Elliottb0450fe2021-09-01 15:06:26 +01005275 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5276
5277 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5278
5279 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5280
5281
5282 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5283 additional_data->len ),
5284 PSA_ERROR_INVALID_ARGUMENT );
5285
5286 psa_aead_abort( &operation );
5287
Paul Elliotta2a09b02021-09-22 14:56:40 +01005288 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005289
5290 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5291
5292 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5293
5294 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5295 input_data->len ) );
5296
5297 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5298 additional_data->len ) );
5299
5300 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5301 1 ),
5302 PSA_ERROR_INVALID_ARGUMENT );
5303
5304 psa_aead_abort( &operation );
5305
Paul Elliottb0450fe2021-09-01 15:06:26 +01005306 /* Test for sending too much data after setting lengths. */
5307
Paul Elliottb0450fe2021-09-01 15:06:26 +01005308 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5309
5310 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5311
5312 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5313
5314 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5315 input_data->len, output_data,
5316 output_size, &output_length ),
5317 PSA_ERROR_INVALID_ARGUMENT );
5318
5319 psa_aead_abort( &operation );
5320
Paul Elliotta2a09b02021-09-22 14:56:40 +01005321 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005322
5323 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5324
5325 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5326
5327 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5328 input_data->len ) );
5329
5330 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5331 additional_data->len ) );
5332
5333 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5334 input_data->len, output_data,
5335 output_size, &output_length ) );
5336
5337 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5338 1, output_data,
5339 output_size, &output_length ),
5340 PSA_ERROR_INVALID_ARGUMENT );
5341
5342 psa_aead_abort( &operation );
5343
Paul Elliottc23a9a02021-06-21 18:32:46 +01005344 /* Test sending additional data after data. */
5345
Paul Elliottc23a9a02021-06-21 18:32:46 +01005346 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5347
5348 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5349
Andrzej Kurekad837522021-12-15 15:28:49 +01005350 if( operation.alg != PSA_ALG_CCM )
5351 {
5352 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5353 input_data->len, output_data,
5354 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005355
Andrzej Kurekad837522021-12-15 15:28:49 +01005356 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5357 additional_data->len ),
5358 PSA_ERROR_BAD_STATE );
5359 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005360 psa_aead_abort( &operation );
5361
Paul Elliott534d0b42021-06-22 19:15:20 +01005362 /* Test calling finish on decryption. */
5363
Paul Elliott534d0b42021-06-22 19:15:20 +01005364 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5365
5366 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5367
5368 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5369 finish_output_size,
5370 &output_part_length,
5371 tag_buffer, tag_length,
5372 &tag_size ),
5373 PSA_ERROR_BAD_STATE );
5374
5375 psa_aead_abort( &operation );
5376
5377 /* Test calling verify on encryption. */
5378
Paul Elliott534d0b42021-06-22 19:15:20 +01005379 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5380
5381 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5382
5383 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5384 finish_output_size,
5385 &output_part_length,
5386 tag_buffer,
5387 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005388 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005389
5390 psa_aead_abort( &operation );
5391
5392
Paul Elliottc23a9a02021-06-21 18:32:46 +01005393exit:
5394 psa_destroy_key( key );
5395 psa_aead_abort( &operation );
5396 mbedtls_free( output_data );
5397 mbedtls_free( final_data );
5398 PSA_DONE( );
5399}
5400/* END_CASE */
5401
5402/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005403void signature_size( int type_arg,
5404 int bits,
5405 int alg_arg,
5406 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005407{
5408 psa_key_type_t type = type_arg;
5409 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005410 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005411
Gilles Peskinefe11b722018-12-18 00:24:04 +01005412 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005413
Gilles Peskinee59236f2018-01-27 23:32:46 +01005414exit:
5415 ;
5416}
5417/* END_CASE */
5418
5419/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005420void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5421 int alg_arg, data_t *input_data,
5422 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005423{
Ronald Cron5425a212020-08-04 14:58:35 +02005424 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005425 psa_key_type_t key_type = key_type_arg;
5426 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005427 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005428 unsigned char *signature = NULL;
5429 size_t signature_size;
5430 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005432
Gilles Peskine8817f612018-12-18 00:18:46 +01005433 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005434
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005435 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005436 psa_set_key_algorithm( &attributes, alg );
5437 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005438
Gilles Peskine049c7532019-05-15 20:22:09 +02005439 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005440 &key ) );
5441 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005442 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005443
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005444 /* Allocate a buffer which has the size advertized by the
5445 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005446 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005447 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005448 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005449 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005450 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005451
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005452 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005453 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005454 input_data->x, input_data->len,
5455 signature, signature_size,
5456 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005457 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005458 ASSERT_COMPARE( output_data->x, output_data->len,
5459 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005460
5461exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005462 /*
5463 * Key attributes may have been returned by psa_get_key_attributes()
5464 * thus reset them as required.
5465 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005466 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005467
Ronald Cron5425a212020-08-04 14:58:35 +02005468 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005469 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005470 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005471}
5472/* END_CASE */
5473
5474/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005475void sign_hash_fail( int key_type_arg, data_t *key_data,
5476 int alg_arg, data_t *input_data,
5477 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005478{
Ronald Cron5425a212020-08-04 14:58:35 +02005479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005480 psa_key_type_t key_type = key_type_arg;
5481 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005482 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005483 psa_status_t actual_status;
5484 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005485 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005486 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005488
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005489 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005490
Gilles Peskine8817f612018-12-18 00:18:46 +01005491 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005492
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005493 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005494 psa_set_key_algorithm( &attributes, alg );
5495 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005496
Gilles Peskine049c7532019-05-15 20:22:09 +02005497 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005498 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005499
Ronald Cron5425a212020-08-04 14:58:35 +02005500 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005501 input_data->x, input_data->len,
5502 signature, signature_size,
5503 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005504 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005505 /* The value of *signature_length is unspecified on error, but
5506 * whatever it is, it should be less than signature_size, so that
5507 * if the caller tries to read *signature_length bytes without
5508 * checking the error code then they don't overflow a buffer. */
5509 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005510
5511exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005512 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005513 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005514 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005515 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005516}
5517/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005518
5519/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005520void sign_verify_hash( int key_type_arg, data_t *key_data,
5521 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005522{
Ronald Cron5425a212020-08-04 14:58:35 +02005523 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005524 psa_key_type_t key_type = key_type_arg;
5525 psa_algorithm_t alg = alg_arg;
5526 size_t key_bits;
5527 unsigned char *signature = NULL;
5528 size_t signature_size;
5529 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005530 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005531
Gilles Peskine8817f612018-12-18 00:18:46 +01005532 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005533
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005534 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005535 psa_set_key_algorithm( &attributes, alg );
5536 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005537
Gilles Peskine049c7532019-05-15 20:22:09 +02005538 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005539 &key ) );
5540 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005541 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005542
5543 /* Allocate a buffer which has the size advertized by the
5544 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005545 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005546 key_bits, alg );
5547 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005548 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005549 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005550
5551 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005552 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005553 input_data->x, input_data->len,
5554 signature, signature_size,
5555 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005556 /* Check that the signature length looks sensible. */
5557 TEST_ASSERT( signature_length <= signature_size );
5558 TEST_ASSERT( signature_length > 0 );
5559
5560 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005561 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005562 input_data->x, input_data->len,
5563 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005564
5565 if( input_data->len != 0 )
5566 {
5567 /* Flip a bit in the input and verify that the signature is now
5568 * detected as invalid. Flip a bit at the beginning, not at the end,
5569 * because ECDSA may ignore the last few bits of the input. */
5570 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005571 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005572 input_data->x, input_data->len,
5573 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005574 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005575 }
5576
5577exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005578 /*
5579 * Key attributes may have been returned by psa_get_key_attributes()
5580 * thus reset them as required.
5581 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005582 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005583
Ronald Cron5425a212020-08-04 14:58:35 +02005584 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005585 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005586 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005587}
5588/* END_CASE */
5589
5590/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005591void verify_hash( int key_type_arg, data_t *key_data,
5592 int alg_arg, data_t *hash_data,
5593 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005594{
Ronald Cron5425a212020-08-04 14:58:35 +02005595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005596 psa_key_type_t key_type = key_type_arg;
5597 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005599
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005600 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005601
Gilles Peskine8817f612018-12-18 00:18:46 +01005602 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005603
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005604 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005605 psa_set_key_algorithm( &attributes, alg );
5606 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005607
Gilles Peskine049c7532019-05-15 20:22:09 +02005608 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005609 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005610
Ronald Cron5425a212020-08-04 14:58:35 +02005611 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005612 hash_data->x, hash_data->len,
5613 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005614
itayzafrir5c753392018-05-08 11:18:38 +03005615exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005616 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005617 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005618 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005619}
5620/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005621
5622/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005623void verify_hash_fail( int key_type_arg, data_t *key_data,
5624 int alg_arg, data_t *hash_data,
5625 data_t *signature_data,
5626 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005627{
Ronald Cron5425a212020-08-04 14:58:35 +02005628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005629 psa_key_type_t key_type = key_type_arg;
5630 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005631 psa_status_t actual_status;
5632 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005634
Gilles Peskine8817f612018-12-18 00:18:46 +01005635 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005636
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005637 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005638 psa_set_key_algorithm( &attributes, alg );
5639 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005640
Gilles Peskine049c7532019-05-15 20:22:09 +02005641 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005642 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005643
Ronald Cron5425a212020-08-04 14:58:35 +02005644 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005645 hash_data->x, hash_data->len,
5646 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005647 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005648
5649exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005650 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005651 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005652 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005653}
5654/* END_CASE */
5655
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005656/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005657void sign_message_deterministic( int key_type_arg,
5658 data_t *key_data,
5659 int alg_arg,
5660 data_t *input_data,
5661 data_t *output_data )
5662{
5663 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5664 psa_key_type_t key_type = key_type_arg;
5665 psa_algorithm_t alg = alg_arg;
5666 size_t key_bits;
5667 unsigned char *signature = NULL;
5668 size_t signature_size;
5669 size_t signature_length = 0xdeadbeef;
5670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5671
5672 PSA_ASSERT( psa_crypto_init( ) );
5673
5674 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5675 psa_set_key_algorithm( &attributes, alg );
5676 psa_set_key_type( &attributes, key_type );
5677
5678 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5679 &key ) );
5680 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5681 key_bits = psa_get_key_bits( &attributes );
5682
5683 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5684 TEST_ASSERT( signature_size != 0 );
5685 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5686 ASSERT_ALLOC( signature, signature_size );
5687
5688 PSA_ASSERT( psa_sign_message( key, alg,
5689 input_data->x, input_data->len,
5690 signature, signature_size,
5691 &signature_length ) );
5692
5693 ASSERT_COMPARE( output_data->x, output_data->len,
5694 signature, signature_length );
5695
5696exit:
5697 psa_reset_key_attributes( &attributes );
5698
5699 psa_destroy_key( key );
5700 mbedtls_free( signature );
5701 PSA_DONE( );
5702
5703}
5704/* END_CASE */
5705
5706/* BEGIN_CASE */
5707void sign_message_fail( int key_type_arg,
5708 data_t *key_data,
5709 int alg_arg,
5710 data_t *input_data,
5711 int signature_size_arg,
5712 int expected_status_arg )
5713{
5714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5715 psa_key_type_t key_type = key_type_arg;
5716 psa_algorithm_t alg = alg_arg;
5717 size_t signature_size = signature_size_arg;
5718 psa_status_t actual_status;
5719 psa_status_t expected_status = expected_status_arg;
5720 unsigned char *signature = NULL;
5721 size_t signature_length = 0xdeadbeef;
5722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5723
5724 ASSERT_ALLOC( signature, signature_size );
5725
5726 PSA_ASSERT( psa_crypto_init( ) );
5727
5728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5729 psa_set_key_algorithm( &attributes, alg );
5730 psa_set_key_type( &attributes, key_type );
5731
5732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5733 &key ) );
5734
5735 actual_status = psa_sign_message( key, alg,
5736 input_data->x, input_data->len,
5737 signature, signature_size,
5738 &signature_length );
5739 TEST_EQUAL( actual_status, expected_status );
5740 /* The value of *signature_length is unspecified on error, but
5741 * whatever it is, it should be less than signature_size, so that
5742 * if the caller tries to read *signature_length bytes without
5743 * checking the error code then they don't overflow a buffer. */
5744 TEST_ASSERT( signature_length <= signature_size );
5745
5746exit:
5747 psa_reset_key_attributes( &attributes );
5748 psa_destroy_key( key );
5749 mbedtls_free( signature );
5750 PSA_DONE( );
5751}
5752/* END_CASE */
5753
5754/* BEGIN_CASE */
5755void sign_verify_message( int key_type_arg,
5756 data_t *key_data,
5757 int alg_arg,
5758 data_t *input_data )
5759{
5760 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5761 psa_key_type_t key_type = key_type_arg;
5762 psa_algorithm_t alg = alg_arg;
5763 size_t key_bits;
5764 unsigned char *signature = NULL;
5765 size_t signature_size;
5766 size_t signature_length = 0xdeadbeef;
5767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5768
5769 PSA_ASSERT( psa_crypto_init( ) );
5770
5771 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5772 PSA_KEY_USAGE_VERIFY_MESSAGE );
5773 psa_set_key_algorithm( &attributes, alg );
5774 psa_set_key_type( &attributes, key_type );
5775
5776 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5777 &key ) );
5778 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5779 key_bits = psa_get_key_bits( &attributes );
5780
5781 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5782 TEST_ASSERT( signature_size != 0 );
5783 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5784 ASSERT_ALLOC( signature, signature_size );
5785
5786 PSA_ASSERT( psa_sign_message( key, alg,
5787 input_data->x, input_data->len,
5788 signature, signature_size,
5789 &signature_length ) );
5790 TEST_ASSERT( signature_length <= signature_size );
5791 TEST_ASSERT( signature_length > 0 );
5792
5793 PSA_ASSERT( psa_verify_message( key, alg,
5794 input_data->x, input_data->len,
5795 signature, signature_length ) );
5796
5797 if( input_data->len != 0 )
5798 {
5799 /* Flip a bit in the input and verify that the signature is now
5800 * detected as invalid. Flip a bit at the beginning, not at the end,
5801 * because ECDSA may ignore the last few bits of the input. */
5802 input_data->x[0] ^= 1;
5803 TEST_EQUAL( psa_verify_message( key, alg,
5804 input_data->x, input_data->len,
5805 signature, signature_length ),
5806 PSA_ERROR_INVALID_SIGNATURE );
5807 }
5808
5809exit:
5810 psa_reset_key_attributes( &attributes );
5811
5812 psa_destroy_key( key );
5813 mbedtls_free( signature );
5814 PSA_DONE( );
5815}
5816/* END_CASE */
5817
5818/* BEGIN_CASE */
5819void verify_message( int key_type_arg,
5820 data_t *key_data,
5821 int alg_arg,
5822 data_t *input_data,
5823 data_t *signature_data )
5824{
5825 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5826 psa_key_type_t key_type = key_type_arg;
5827 psa_algorithm_t alg = alg_arg;
5828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5829
5830 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5831
5832 PSA_ASSERT( psa_crypto_init( ) );
5833
5834 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5835 psa_set_key_algorithm( &attributes, alg );
5836 psa_set_key_type( &attributes, key_type );
5837
5838 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5839 &key ) );
5840
5841 PSA_ASSERT( psa_verify_message( key, alg,
5842 input_data->x, input_data->len,
5843 signature_data->x, signature_data->len ) );
5844
5845exit:
5846 psa_reset_key_attributes( &attributes );
5847 psa_destroy_key( key );
5848 PSA_DONE( );
5849}
5850/* END_CASE */
5851
5852/* BEGIN_CASE */
5853void verify_message_fail( int key_type_arg,
5854 data_t *key_data,
5855 int alg_arg,
5856 data_t *hash_data,
5857 data_t *signature_data,
5858 int expected_status_arg )
5859{
5860 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5861 psa_key_type_t key_type = key_type_arg;
5862 psa_algorithm_t alg = alg_arg;
5863 psa_status_t actual_status;
5864 psa_status_t expected_status = expected_status_arg;
5865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5866
5867 PSA_ASSERT( psa_crypto_init( ) );
5868
5869 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5870 psa_set_key_algorithm( &attributes, alg );
5871 psa_set_key_type( &attributes, key_type );
5872
5873 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5874 &key ) );
5875
5876 actual_status = psa_verify_message( key, alg,
5877 hash_data->x, hash_data->len,
5878 signature_data->x,
5879 signature_data->len );
5880 TEST_EQUAL( actual_status, expected_status );
5881
5882exit:
5883 psa_reset_key_attributes( &attributes );
5884 psa_destroy_key( key );
5885 PSA_DONE( );
5886}
5887/* END_CASE */
5888
5889/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005890void asymmetric_encrypt( int key_type_arg,
5891 data_t *key_data,
5892 int alg_arg,
5893 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005894 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005895 int expected_output_length_arg,
5896 int expected_status_arg )
5897{
Ronald Cron5425a212020-08-04 14:58:35 +02005898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005899 psa_key_type_t key_type = key_type_arg;
5900 psa_algorithm_t alg = alg_arg;
5901 size_t expected_output_length = expected_output_length_arg;
5902 size_t key_bits;
5903 unsigned char *output = NULL;
5904 size_t output_size;
5905 size_t output_length = ~0;
5906 psa_status_t actual_status;
5907 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005908 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005909
Gilles Peskine8817f612018-12-18 00:18:46 +01005910 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005911
Gilles Peskine656896e2018-06-29 19:12:28 +02005912 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005913 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5914 psa_set_key_algorithm( &attributes, alg );
5915 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005916 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005917 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005918
5919 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005920 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005921 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005922
Gilles Peskine656896e2018-06-29 19:12:28 +02005923 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005924 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005925 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005926
5927 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005928 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005929 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005930 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005931 output, output_size,
5932 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005933 TEST_EQUAL( actual_status, expected_status );
5934 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005935
Gilles Peskine68428122018-06-30 18:42:41 +02005936 /* If the label is empty, the test framework puts a non-null pointer
5937 * in label->x. Test that a null pointer works as well. */
5938 if( label->len == 0 )
5939 {
5940 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005941 if( output_size != 0 )
5942 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005943 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005944 input_data->x, input_data->len,
5945 NULL, label->len,
5946 output, output_size,
5947 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005948 TEST_EQUAL( actual_status, expected_status );
5949 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005950 }
5951
Gilles Peskine656896e2018-06-29 19:12:28 +02005952exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005953 /*
5954 * Key attributes may have been returned by psa_get_key_attributes()
5955 * thus reset them as required.
5956 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005957 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005958
Ronald Cron5425a212020-08-04 14:58:35 +02005959 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005960 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005961 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005962}
5963/* END_CASE */
5964
5965/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005966void asymmetric_encrypt_decrypt( int key_type_arg,
5967 data_t *key_data,
5968 int alg_arg,
5969 data_t *input_data,
5970 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005971{
Ronald Cron5425a212020-08-04 14:58:35 +02005972 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005973 psa_key_type_t key_type = key_type_arg;
5974 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005975 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005976 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005977 size_t output_size;
5978 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005979 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005980 size_t output2_size;
5981 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005983
Gilles Peskine8817f612018-12-18 00:18:46 +01005984 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005985
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005986 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5987 psa_set_key_algorithm( &attributes, alg );
5988 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005989
Gilles Peskine049c7532019-05-15 20:22:09 +02005990 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005991 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005992
5993 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005994 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005995 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005996
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005997 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005998 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005999 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006000
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006001 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006002 TEST_ASSERT( output2_size <=
6003 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6004 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006005 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006006
Gilles Peskineeebd7382018-06-08 18:11:54 +02006007 /* We test encryption by checking that encrypt-then-decrypt gives back
6008 * the original plaintext because of the non-optional random
6009 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006010 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006011 input_data->x, input_data->len,
6012 label->x, label->len,
6013 output, output_size,
6014 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006015 /* We don't know what ciphertext length to expect, but check that
6016 * it looks sensible. */
6017 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006018
Ronald Cron5425a212020-08-04 14:58:35 +02006019 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006020 output, output_length,
6021 label->x, label->len,
6022 output2, output2_size,
6023 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006024 ASSERT_COMPARE( input_data->x, input_data->len,
6025 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006026
6027exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006028 /*
6029 * Key attributes may have been returned by psa_get_key_attributes()
6030 * thus reset them as required.
6031 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006032 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006033
Ronald Cron5425a212020-08-04 14:58:35 +02006034 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006035 mbedtls_free( output );
6036 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006037 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006038}
6039/* END_CASE */
6040
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006041/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006042void asymmetric_decrypt( int key_type_arg,
6043 data_t *key_data,
6044 int alg_arg,
6045 data_t *input_data,
6046 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006047 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006048{
Ronald Cron5425a212020-08-04 14:58:35 +02006049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006050 psa_key_type_t key_type = key_type_arg;
6051 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006052 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006053 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006054 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006055 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006057
Gilles Peskine8817f612018-12-18 00:18:46 +01006058 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006059
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006060 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6061 psa_set_key_algorithm( &attributes, alg );
6062 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006063
Gilles Peskine049c7532019-05-15 20:22:09 +02006064 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006065 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006066
gabor-mezei-armceface22021-01-21 12:26:17 +01006067 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6068 key_bits = psa_get_key_bits( &attributes );
6069
6070 /* Determine the maximum ciphertext length */
6071 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6072 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6073 ASSERT_ALLOC( output, output_size );
6074
Ronald Cron5425a212020-08-04 14:58:35 +02006075 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006076 input_data->x, input_data->len,
6077 label->x, label->len,
6078 output,
6079 output_size,
6080 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006081 ASSERT_COMPARE( expected_data->x, expected_data->len,
6082 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006083
Gilles Peskine68428122018-06-30 18:42:41 +02006084 /* If the label is empty, the test framework puts a non-null pointer
6085 * in label->x. Test that a null pointer works as well. */
6086 if( label->len == 0 )
6087 {
6088 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006089 if( output_size != 0 )
6090 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006091 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006092 input_data->x, input_data->len,
6093 NULL, label->len,
6094 output,
6095 output_size,
6096 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006097 ASSERT_COMPARE( expected_data->x, expected_data->len,
6098 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006099 }
6100
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006101exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006102 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006103 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006104 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006105 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006106}
6107/* END_CASE */
6108
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006109/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006110void asymmetric_decrypt_fail( int key_type_arg,
6111 data_t *key_data,
6112 int alg_arg,
6113 data_t *input_data,
6114 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006115 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006116 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006117{
Ronald Cron5425a212020-08-04 14:58:35 +02006118 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006119 psa_key_type_t key_type = key_type_arg;
6120 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006121 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006122 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006123 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006124 psa_status_t actual_status;
6125 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006127
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006128 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006129
Gilles Peskine8817f612018-12-18 00:18:46 +01006130 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006131
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006132 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6133 psa_set_key_algorithm( &attributes, alg );
6134 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006135
Gilles Peskine049c7532019-05-15 20:22:09 +02006136 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006137 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006138
Ronald Cron5425a212020-08-04 14:58:35 +02006139 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006140 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006141 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006142 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006143 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006144 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006145 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006146
Gilles Peskine68428122018-06-30 18:42:41 +02006147 /* If the label is empty, the test framework puts a non-null pointer
6148 * in label->x. Test that a null pointer works as well. */
6149 if( label->len == 0 )
6150 {
6151 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006152 if( output_size != 0 )
6153 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006154 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006155 input_data->x, input_data->len,
6156 NULL, label->len,
6157 output, output_size,
6158 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006159 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006160 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006161 }
6162
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006163exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006164 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006165 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006166 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006167 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006168}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006169/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006170
6171/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006172void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006173{
6174 /* Test each valid way of initializing the object, except for `= {0}`, as
6175 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6176 * though it's OK by the C standard. We could test for this, but we'd need
6177 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006178 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006179 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6180 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6181 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006182
6183 memset( &zero, 0, sizeof( zero ) );
6184
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006185 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006186 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006187 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006188 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006189 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006190 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006191 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006192
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006193 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006194 PSA_ASSERT( psa_key_derivation_abort(&func) );
6195 PSA_ASSERT( psa_key_derivation_abort(&init) );
6196 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006197}
6198/* END_CASE */
6199
Janos Follath16de4a42019-06-13 16:32:24 +01006200/* BEGIN_CASE */
6201void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006202{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006203 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006204 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006205 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006206
Gilles Peskine8817f612018-12-18 00:18:46 +01006207 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006208
Janos Follath16de4a42019-06-13 16:32:24 +01006209 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006210 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006211
6212exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006213 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006214 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006215}
6216/* END_CASE */
6217
Janos Follathaf3c2a02019-06-12 12:34:34 +01006218/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006219void derive_set_capacity( int alg_arg, int capacity_arg,
6220 int expected_status_arg )
6221{
6222 psa_algorithm_t alg = alg_arg;
6223 size_t capacity = capacity_arg;
6224 psa_status_t expected_status = expected_status_arg;
6225 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6226
6227 PSA_ASSERT( psa_crypto_init( ) );
6228
6229 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6230
6231 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6232 expected_status );
6233
6234exit:
6235 psa_key_derivation_abort( &operation );
6236 PSA_DONE( );
6237}
6238/* END_CASE */
6239
6240/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006241void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006242 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006243 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006244 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006245 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006246 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006247 int expected_status_arg3,
6248 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006249{
6250 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006251 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6252 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006253 psa_status_t expected_statuses[] = {expected_status_arg1,
6254 expected_status_arg2,
6255 expected_status_arg3};
6256 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006257 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6258 MBEDTLS_SVC_KEY_ID_INIT,
6259 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006260 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6261 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6262 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006263 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006264 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006265 psa_status_t expected_output_status = expected_output_status_arg;
6266 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006267
6268 PSA_ASSERT( psa_crypto_init( ) );
6269
6270 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6271 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006272
6273 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6274
6275 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6276 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006277 mbedtls_test_set_step( i );
6278 if( steps[i] == 0 )
6279 {
6280 /* Skip this step */
6281 }
6282 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006283 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006284 psa_set_key_type( &attributes, key_types[i] );
6285 PSA_ASSERT( psa_import_key( &attributes,
6286 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006287 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006288 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6289 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6290 {
6291 // When taking a private key as secret input, use key agreement
6292 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006293 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6294 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006295 expected_statuses[i] );
6296 }
6297 else
6298 {
6299 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006300 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006301 expected_statuses[i] );
6302 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006303 }
6304 else
6305 {
6306 TEST_EQUAL( psa_key_derivation_input_bytes(
6307 &operation, steps[i],
6308 inputs[i]->x, inputs[i]->len ),
6309 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006310 }
6311 }
6312
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006313 if( output_key_type != PSA_KEY_TYPE_NONE )
6314 {
6315 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006316 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006317 psa_set_key_bits( &attributes, 8 );
6318 actual_output_status =
6319 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006320 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006321 }
6322 else
6323 {
6324 uint8_t buffer[1];
6325 actual_output_status =
6326 psa_key_derivation_output_bytes( &operation,
6327 buffer, sizeof( buffer ) );
6328 }
6329 TEST_EQUAL( actual_output_status, expected_output_status );
6330
Janos Follathaf3c2a02019-06-12 12:34:34 +01006331exit:
6332 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006333 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6334 psa_destroy_key( keys[i] );
6335 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006336 PSA_DONE( );
6337}
6338/* END_CASE */
6339
Janos Follathd958bb72019-07-03 15:02:16 +01006340/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006341void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006342{
Janos Follathd958bb72019-07-03 15:02:16 +01006343 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006345 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006346 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006347 unsigned char input1[] = "Input 1";
6348 size_t input1_length = sizeof( input1 );
6349 unsigned char input2[] = "Input 2";
6350 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006351 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006352 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006353 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6354 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6355 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006357
Gilles Peskine8817f612018-12-18 00:18:46 +01006358 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006359
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006360 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6361 psa_set_key_algorithm( &attributes, alg );
6362 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006363
Gilles Peskine73676cb2019-05-15 20:15:10 +02006364 PSA_ASSERT( psa_import_key( &attributes,
6365 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006366 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006367
6368 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006369 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6370 input1, input1_length,
6371 input2, input2_length,
6372 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006373 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006374
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006375 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006376 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006377 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006378
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006379 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006380
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006381 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006382 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006383
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006384exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006385 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006386 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006387 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006388}
6389/* END_CASE */
6390
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006391/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006392void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006393{
6394 uint8_t output_buffer[16];
6395 size_t buffer_size = 16;
6396 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006397 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006398
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006399 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6400 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006401 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006402
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006403 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006404 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006405
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006406 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006407
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006408 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6409 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006410 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006411
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006412 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006413 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006414
6415exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006416 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006417}
6418/* END_CASE */
6419
6420/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006421void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006422 int step1_arg, data_t *input1,
6423 int step2_arg, data_t *input2,
6424 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006425 int requested_capacity_arg,
6426 data_t *expected_output1,
6427 data_t *expected_output2 )
6428{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006429 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006430 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6431 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006432 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6433 MBEDTLS_SVC_KEY_ID_INIT,
6434 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006435 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006436 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006437 uint8_t *expected_outputs[2] =
6438 {expected_output1->x, expected_output2->x};
6439 size_t output_sizes[2] =
6440 {expected_output1->len, expected_output2->len};
6441 size_t output_buffer_size = 0;
6442 uint8_t *output_buffer = NULL;
6443 size_t expected_capacity;
6444 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006445 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006446 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006447 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006448
6449 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6450 {
6451 if( output_sizes[i] > output_buffer_size )
6452 output_buffer_size = output_sizes[i];
6453 if( output_sizes[i] == 0 )
6454 expected_outputs[i] = NULL;
6455 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006456 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006457 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006458
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006459 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6460 psa_set_key_algorithm( &attributes, alg );
6461 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006462
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006463 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006464 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6465 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6466 requested_capacity ) );
6467 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006468 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006469 switch( steps[i] )
6470 {
6471 case 0:
6472 break;
6473 case PSA_KEY_DERIVATION_INPUT_SECRET:
6474 PSA_ASSERT( psa_import_key( &attributes,
6475 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006476 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006477
6478 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6479 {
6480 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6481 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6482 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6483 }
6484
Gilles Peskine1468da72019-05-29 17:35:49 +02006485 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006486 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006487 break;
6488 default:
6489 PSA_ASSERT( psa_key_derivation_input_bytes(
6490 &operation, steps[i],
6491 inputs[i]->x, inputs[i]->len ) );
6492 break;
6493 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006494 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006495
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006496 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006497 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006498 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006499 expected_capacity = requested_capacity;
6500
6501 /* Expansion phase. */
6502 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6503 {
6504 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006505 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006506 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006507 if( expected_capacity == 0 && output_sizes[i] == 0 )
6508 {
6509 /* Reading 0 bytes when 0 bytes are available can go either way. */
6510 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006511 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006512 continue;
6513 }
6514 else if( expected_capacity == 0 ||
6515 output_sizes[i] > expected_capacity )
6516 {
6517 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006518 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006519 expected_capacity = 0;
6520 continue;
6521 }
6522 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006523 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006524 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006525 ASSERT_COMPARE( output_buffer, output_sizes[i],
6526 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006527 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006528 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006529 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006530 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006531 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006532 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006533 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006534
6535exit:
6536 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006537 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006538 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6539 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006540 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006541}
6542/* END_CASE */
6543
6544/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006545void derive_full( int alg_arg,
6546 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006547 data_t *input1,
6548 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006549 int requested_capacity_arg )
6550{
Ronald Cron5425a212020-08-04 14:58:35 +02006551 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006552 psa_algorithm_t alg = alg_arg;
6553 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006554 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006555 unsigned char output_buffer[16];
6556 size_t expected_capacity = requested_capacity;
6557 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006559
Gilles Peskine8817f612018-12-18 00:18:46 +01006560 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006561
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006562 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6563 psa_set_key_algorithm( &attributes, alg );
6564 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006565
Gilles Peskine049c7532019-05-15 20:22:09 +02006566 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006567 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006568
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006569 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6570 input1->x, input1->len,
6571 input2->x, input2->len,
6572 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006573 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006574
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006575 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006576 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006577 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006578
6579 /* Expansion phase. */
6580 while( current_capacity > 0 )
6581 {
6582 size_t read_size = sizeof( output_buffer );
6583 if( read_size > current_capacity )
6584 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006585 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006586 output_buffer,
6587 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006588 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006589 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006590 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006591 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006592 }
6593
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006594 /* Check that the operation refuses to go over capacity. */
6595 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006596 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006597
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006598 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006599
6600exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006601 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006602 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006603 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006604}
6605/* END_CASE */
6606
Janos Follathe60c9052019-07-03 13:51:30 +01006607/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006608void derive_key_exercise( int alg_arg,
6609 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006610 data_t *input1,
6611 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006612 int derived_type_arg,
6613 int derived_bits_arg,
6614 int derived_usage_arg,
6615 int derived_alg_arg )
6616{
Ronald Cron5425a212020-08-04 14:58:35 +02006617 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6618 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006619 psa_algorithm_t alg = alg_arg;
6620 psa_key_type_t derived_type = derived_type_arg;
6621 size_t derived_bits = derived_bits_arg;
6622 psa_key_usage_t derived_usage = derived_usage_arg;
6623 psa_algorithm_t derived_alg = derived_alg_arg;
6624 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006625 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006627 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006628
Gilles Peskine8817f612018-12-18 00:18:46 +01006629 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006630
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006631 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6632 psa_set_key_algorithm( &attributes, alg );
6633 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006634 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006635 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006636
6637 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006638 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6639 input1->x, input1->len,
6640 input2->x, input2->len,
6641 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006642 goto exit;
6643
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006644 psa_set_key_usage_flags( &attributes, derived_usage );
6645 psa_set_key_algorithm( &attributes, derived_alg );
6646 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006647 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006648 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006649 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006650
6651 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006652 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006653 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6654 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006655
6656 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006657 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006658 goto exit;
6659
6660exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006661 /*
6662 * Key attributes may have been returned by psa_get_key_attributes()
6663 * thus reset them as required.
6664 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006665 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006666
6667 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006668 psa_destroy_key( base_key );
6669 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006670 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006671}
6672/* END_CASE */
6673
Janos Follath42fd8882019-07-03 14:17:09 +01006674/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006675void derive_key_export( int alg_arg,
6676 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006677 data_t *input1,
6678 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006679 int bytes1_arg,
6680 int bytes2_arg )
6681{
Ronald Cron5425a212020-08-04 14:58:35 +02006682 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6683 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006684 psa_algorithm_t alg = alg_arg;
6685 size_t bytes1 = bytes1_arg;
6686 size_t bytes2 = bytes2_arg;
6687 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006688 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006689 uint8_t *output_buffer = NULL;
6690 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006691 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6692 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006693 size_t length;
6694
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006695 ASSERT_ALLOC( output_buffer, capacity );
6696 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006698
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006699 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6700 psa_set_key_algorithm( &base_attributes, alg );
6701 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006702 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006703 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006704
6705 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006706 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6707 input1->x, input1->len,
6708 input2->x, input2->len,
6709 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006710 goto exit;
6711
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006712 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006713 output_buffer,
6714 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006715 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006716
6717 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006718 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6719 input1->x, input1->len,
6720 input2->x, input2->len,
6721 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006722 goto exit;
6723
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006724 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6725 psa_set_key_algorithm( &derived_attributes, 0 );
6726 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006727 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006728 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006729 &derived_key ) );
6730 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006731 export_buffer, bytes1,
6732 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006733 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006734 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006735 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006736 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006737 &derived_key ) );
6738 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006739 export_buffer + bytes1, bytes2,
6740 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006741 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006742
6743 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006744 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6745 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006746
6747exit:
6748 mbedtls_free( output_buffer );
6749 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006750 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006751 psa_destroy_key( base_key );
6752 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006753 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006754}
6755/* END_CASE */
6756
6757/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01006758void derive_key_type( int alg_arg,
6759 data_t *key_data,
6760 data_t *input1,
6761 data_t *input2,
6762 int key_type_arg, int bits_arg,
6763 data_t *expected_export )
6764{
6765 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6766 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
6767 const psa_algorithm_t alg = alg_arg;
6768 const psa_key_type_t key_type = key_type_arg;
6769 const size_t bits = bits_arg;
6770 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6771 const size_t export_buffer_size =
6772 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
6773 uint8_t *export_buffer = NULL;
6774 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6775 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6776 size_t export_length;
6777
6778 ASSERT_ALLOC( export_buffer, export_buffer_size );
6779 PSA_ASSERT( psa_crypto_init( ) );
6780
6781 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6782 psa_set_key_algorithm( &base_attributes, alg );
6783 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6784 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
6785 &base_key ) );
6786
6787 if( !mbedtls_test_psa_setup_key_derivation_wrap(
6788 &operation, base_key, alg,
6789 input1->x, input1->len,
6790 input2->x, input2->len,
6791 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) )
6792 goto exit;
6793
6794 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6795 psa_set_key_algorithm( &derived_attributes, 0 );
6796 psa_set_key_type( &derived_attributes, key_type );
6797 psa_set_key_bits( &derived_attributes, bits );
6798 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
6799 &derived_key ) );
6800
6801 PSA_ASSERT( psa_export_key( derived_key,
6802 export_buffer, export_buffer_size,
6803 &export_length ) );
6804 ASSERT_COMPARE( export_buffer, export_length,
6805 expected_export->x, expected_export->len );
6806
6807exit:
6808 mbedtls_free( export_buffer );
6809 psa_key_derivation_abort( &operation );
6810 psa_destroy_key( base_key );
6811 psa_destroy_key( derived_key );
6812 PSA_DONE( );
6813}
6814/* END_CASE */
6815
6816/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006817void derive_key( int alg_arg,
6818 data_t *key_data, data_t *input1, data_t *input2,
6819 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006820 int expected_status_arg,
6821 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006822{
Ronald Cron5425a212020-08-04 14:58:35 +02006823 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6824 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006825 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006826 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006827 size_t bits = bits_arg;
6828 psa_status_t expected_status = expected_status_arg;
6829 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6830 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6831 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6832
6833 PSA_ASSERT( psa_crypto_init( ) );
6834
6835 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6836 psa_set_key_algorithm( &base_attributes, alg );
6837 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6838 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006839 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006840
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006841 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6842 input1->x, input1->len,
6843 input2->x, input2->len,
6844 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006845 goto exit;
6846
6847 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6848 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006849 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006850 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006851
6852 psa_status_t status =
6853 psa_key_derivation_output_key( &derived_attributes,
6854 &operation,
6855 &derived_key );
6856 if( is_large_output > 0 )
6857 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6858 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006859
6860exit:
6861 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006862 psa_destroy_key( base_key );
6863 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006864 PSA_DONE( );
6865}
6866/* END_CASE */
6867
6868/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006869void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006870 int our_key_type_arg, int our_key_alg_arg,
6871 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006872 int expected_status_arg )
6873{
Ronald Cron5425a212020-08-04 14:58:35 +02006874 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006875 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006876 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006877 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006878 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006880 psa_status_t expected_status = expected_status_arg;
6881 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006882
Gilles Peskine8817f612018-12-18 00:18:46 +01006883 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006884
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006885 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006886 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006887 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006888 PSA_ASSERT( psa_import_key( &attributes,
6889 our_key_data->x, our_key_data->len,
6890 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006891
Gilles Peskine77f40d82019-04-11 21:27:06 +02006892 /* The tests currently include inputs that should fail at either step.
6893 * Test cases that fail at the setup step should be changed to call
6894 * key_derivation_setup instead, and this function should be renamed
6895 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006896 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006897 if( status == PSA_SUCCESS )
6898 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006899 TEST_EQUAL( psa_key_derivation_key_agreement(
6900 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6901 our_key,
6902 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006903 expected_status );
6904 }
6905 else
6906 {
6907 TEST_ASSERT( status == expected_status );
6908 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006909
6910exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006911 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006912 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006913 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006914}
6915/* END_CASE */
6916
6917/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006918void raw_key_agreement( int alg_arg,
6919 int our_key_type_arg, data_t *our_key_data,
6920 data_t *peer_key_data,
6921 data_t *expected_output )
6922{
Ronald Cron5425a212020-08-04 14:58:35 +02006923 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006924 psa_algorithm_t alg = alg_arg;
6925 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006927 unsigned char *output = NULL;
6928 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006929 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006930
6931 ASSERT_ALLOC( output, expected_output->len );
6932 PSA_ASSERT( psa_crypto_init( ) );
6933
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6935 psa_set_key_algorithm( &attributes, alg );
6936 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006937 PSA_ASSERT( psa_import_key( &attributes,
6938 our_key_data->x, our_key_data->len,
6939 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006940
gabor-mezei-armceface22021-01-21 12:26:17 +01006941 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6942 key_bits = psa_get_key_bits( &attributes );
6943
Gilles Peskinebe697d82019-05-16 18:00:41 +02006944 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6945 peer_key_data->x, peer_key_data->len,
6946 output, expected_output->len,
6947 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006948 ASSERT_COMPARE( output, output_length,
6949 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006950 TEST_ASSERT( output_length <=
6951 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6952 TEST_ASSERT( output_length <=
6953 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006954
6955exit:
6956 mbedtls_free( output );
6957 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006958 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006959}
6960/* END_CASE */
6961
6962/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006963void key_agreement_capacity( int alg_arg,
6964 int our_key_type_arg, data_t *our_key_data,
6965 data_t *peer_key_data,
6966 int expected_capacity_arg )
6967{
Ronald Cron5425a212020-08-04 14:58:35 +02006968 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006969 psa_algorithm_t alg = alg_arg;
6970 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006971 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006972 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006973 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006974 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006975
Gilles Peskine8817f612018-12-18 00:18:46 +01006976 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006977
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006978 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6979 psa_set_key_algorithm( &attributes, alg );
6980 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006981 PSA_ASSERT( psa_import_key( &attributes,
6982 our_key_data->x, our_key_data->len,
6983 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006984
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006985 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006986 PSA_ASSERT( psa_key_derivation_key_agreement(
6987 &operation,
6988 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6989 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006990 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6991 {
6992 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006993 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006994 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006995 NULL, 0 ) );
6996 }
Gilles Peskine59685592018-09-18 12:11:34 +02006997
Gilles Peskinebf491972018-10-25 22:36:12 +02006998 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006999 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007000 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007001 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007002
Gilles Peskinebf491972018-10-25 22:36:12 +02007003 /* Test the actual capacity by reading the output. */
7004 while( actual_capacity > sizeof( output ) )
7005 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007006 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007007 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007008 actual_capacity -= sizeof( output );
7009 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007010 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007011 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007012 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007013 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007014
Gilles Peskine59685592018-09-18 12:11:34 +02007015exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007016 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007017 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007018 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007019}
7020/* END_CASE */
7021
7022/* BEGIN_CASE */
7023void key_agreement_output( int alg_arg,
7024 int our_key_type_arg, data_t *our_key_data,
7025 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007026 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007027{
Ronald Cron5425a212020-08-04 14:58:35 +02007028 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007029 psa_algorithm_t alg = alg_arg;
7030 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007031 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007033 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007034
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007035 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7036 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007037
Gilles Peskine8817f612018-12-18 00:18:46 +01007038 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007039
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007040 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7041 psa_set_key_algorithm( &attributes, alg );
7042 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007043 PSA_ASSERT( psa_import_key( &attributes,
7044 our_key_data->x, our_key_data->len,
7045 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007046
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007047 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007048 PSA_ASSERT( psa_key_derivation_key_agreement(
7049 &operation,
7050 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7051 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007052 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7053 {
7054 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007055 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007056 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007057 NULL, 0 ) );
7058 }
Gilles Peskine59685592018-09-18 12:11:34 +02007059
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007060 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007061 actual_output,
7062 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007063 ASSERT_COMPARE( actual_output, expected_output1->len,
7064 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007065 if( expected_output2->len != 0 )
7066 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007067 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007068 actual_output,
7069 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007070 ASSERT_COMPARE( actual_output, expected_output2->len,
7071 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007072 }
Gilles Peskine59685592018-09-18 12:11:34 +02007073
7074exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007075 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007076 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007077 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007078 mbedtls_free( actual_output );
7079}
7080/* END_CASE */
7081
7082/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007083void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007084{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007085 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007086 unsigned char *output = NULL;
7087 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007088 size_t i;
7089 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007090
Simon Butcher49f8e312020-03-03 15:51:50 +00007091 TEST_ASSERT( bytes_arg >= 0 );
7092
Gilles Peskine91892022021-02-08 19:50:26 +01007093 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007094 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007095
Gilles Peskine8817f612018-12-18 00:18:46 +01007096 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007097
Gilles Peskinea50d7392018-06-21 10:22:13 +02007098 /* Run several times, to ensure that every output byte will be
7099 * nonzero at least once with overwhelming probability
7100 * (2^(-8*number_of_runs)). */
7101 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007102 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007103 if( bytes != 0 )
7104 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007105 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007106
Gilles Peskinea50d7392018-06-21 10:22:13 +02007107 for( i = 0; i < bytes; i++ )
7108 {
7109 if( output[i] != 0 )
7110 ++changed[i];
7111 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007112 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007113
7114 /* Check that every byte was changed to nonzero at least once. This
7115 * validates that psa_generate_random is overwriting every byte of
7116 * the output buffer. */
7117 for( i = 0; i < bytes; i++ )
7118 {
7119 TEST_ASSERT( changed[i] != 0 );
7120 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007121
7122exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007123 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007124 mbedtls_free( output );
7125 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007126}
7127/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007128
7129/* BEGIN_CASE */
7130void generate_key( int type_arg,
7131 int bits_arg,
7132 int usage_arg,
7133 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007134 int expected_status_arg,
7135 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007136{
Ronald Cron5425a212020-08-04 14:58:35 +02007137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007138 psa_key_type_t type = type_arg;
7139 psa_key_usage_t usage = usage_arg;
7140 size_t bits = bits_arg;
7141 psa_algorithm_t alg = alg_arg;
7142 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007144 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007145
Gilles Peskine8817f612018-12-18 00:18:46 +01007146 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007147
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007148 psa_set_key_usage_flags( &attributes, usage );
7149 psa_set_key_algorithm( &attributes, alg );
7150 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007151 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007152
7153 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007154 psa_status_t status = psa_generate_key( &attributes, &key );
7155
7156 if( is_large_key > 0 )
7157 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7158 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007159 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007160 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007161
7162 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007163 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007164 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7165 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007166
Gilles Peskine818ca122018-06-20 18:16:48 +02007167 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007168 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007169 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007170
7171exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007172 /*
7173 * Key attributes may have been returned by psa_get_key_attributes()
7174 * thus reset them as required.
7175 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007176 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007177
Ronald Cron5425a212020-08-04 14:58:35 +02007178 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007179 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007180}
7181/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007182
Ronald Cronee414c72021-03-18 18:50:08 +01007183/* 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 +02007184void generate_key_rsa( int bits_arg,
7185 data_t *e_arg,
7186 int expected_status_arg )
7187{
Ronald Cron5425a212020-08-04 14:58:35 +02007188 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007189 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007190 size_t bits = bits_arg;
7191 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7192 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7193 psa_status_t expected_status = expected_status_arg;
7194 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7195 uint8_t *exported = NULL;
7196 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007197 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007198 size_t exported_length = SIZE_MAX;
7199 uint8_t *e_read_buffer = NULL;
7200 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007201 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007202 size_t e_read_length = SIZE_MAX;
7203
7204 if( e_arg->len == 0 ||
7205 ( e_arg->len == 3 &&
7206 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7207 {
7208 is_default_public_exponent = 1;
7209 e_read_size = 0;
7210 }
7211 ASSERT_ALLOC( e_read_buffer, e_read_size );
7212 ASSERT_ALLOC( exported, exported_size );
7213
7214 PSA_ASSERT( psa_crypto_init( ) );
7215
7216 psa_set_key_usage_flags( &attributes, usage );
7217 psa_set_key_algorithm( &attributes, alg );
7218 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7219 e_arg->x, e_arg->len ) );
7220 psa_set_key_bits( &attributes, bits );
7221
7222 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007223 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007224 if( expected_status != PSA_SUCCESS )
7225 goto exit;
7226
7227 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007228 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007229 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7230 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7231 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7232 e_read_buffer, e_read_size,
7233 &e_read_length ) );
7234 if( is_default_public_exponent )
7235 TEST_EQUAL( e_read_length, 0 );
7236 else
7237 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7238
7239 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007240 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007241 goto exit;
7242
7243 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007244 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007245 exported, exported_size,
7246 &exported_length ) );
7247 {
7248 uint8_t *p = exported;
7249 uint8_t *end = exported + exported_length;
7250 size_t len;
7251 /* RSAPublicKey ::= SEQUENCE {
7252 * modulus INTEGER, -- n
7253 * publicExponent INTEGER } -- e
7254 */
7255 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007256 MBEDTLS_ASN1_SEQUENCE |
7257 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007258 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007259 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7260 MBEDTLS_ASN1_INTEGER ) );
7261 if( len >= 1 && p[0] == 0 )
7262 {
7263 ++p;
7264 --len;
7265 }
7266 if( e_arg->len == 0 )
7267 {
7268 TEST_EQUAL( len, 3 );
7269 TEST_EQUAL( p[0], 1 );
7270 TEST_EQUAL( p[1], 0 );
7271 TEST_EQUAL( p[2], 1 );
7272 }
7273 else
7274 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7275 }
7276
7277exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007278 /*
7279 * Key attributes may have been returned by psa_get_key_attributes() or
7280 * set by psa_set_key_domain_parameters() thus reset them as required.
7281 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007282 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007283
Ronald Cron5425a212020-08-04 14:58:35 +02007284 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007285 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007286 mbedtls_free( e_read_buffer );
7287 mbedtls_free( exported );
7288}
7289/* END_CASE */
7290
Darryl Greend49a4992018-06-18 17:27:26 +01007291/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007292void persistent_key_load_key_from_storage( data_t *data,
7293 int type_arg, int bits_arg,
7294 int usage_flags_arg, int alg_arg,
7295 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007296{
Ronald Cron71016a92020-08-28 19:01:50 +02007297 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007298 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007299 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7300 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007301 psa_key_type_t type = type_arg;
7302 size_t bits = bits_arg;
7303 psa_key_usage_t usage_flags = usage_flags_arg;
7304 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007305 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007306 unsigned char *first_export = NULL;
7307 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007308 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007309 size_t first_exported_length;
7310 size_t second_exported_length;
7311
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007312 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7313 {
7314 ASSERT_ALLOC( first_export, export_size );
7315 ASSERT_ALLOC( second_export, export_size );
7316 }
Darryl Greend49a4992018-06-18 17:27:26 +01007317
Gilles Peskine8817f612018-12-18 00:18:46 +01007318 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007319
Gilles Peskinec87af662019-05-15 16:12:22 +02007320 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007321 psa_set_key_usage_flags( &attributes, usage_flags );
7322 psa_set_key_algorithm( &attributes, alg );
7323 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007324 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007325
Darryl Green0c6575a2018-11-07 16:05:30 +00007326 switch( generation_method )
7327 {
7328 case IMPORT_KEY:
7329 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007330 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007331 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007332 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007333
Darryl Green0c6575a2018-11-07 16:05:30 +00007334 case GENERATE_KEY:
7335 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007336 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007337 break;
7338
7339 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007340#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007341 {
7342 /* Create base key */
7343 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7344 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7345 psa_set_key_usage_flags( &base_attributes,
7346 PSA_KEY_USAGE_DERIVE );
7347 psa_set_key_algorithm( &base_attributes, derive_alg );
7348 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007349 PSA_ASSERT( psa_import_key( &base_attributes,
7350 data->x, data->len,
7351 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007352 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007353 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007354 PSA_ASSERT( psa_key_derivation_input_key(
7355 &operation,
7356 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007357 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007358 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007359 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007360 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7361 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007362 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007363 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007364 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007365 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007366 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007367#else
7368 TEST_ASSUME( ! "KDF not supported in this configuration" );
7369#endif
7370 break;
7371
7372 default:
7373 TEST_ASSERT( ! "generation_method not implemented in test" );
7374 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007375 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007376 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007377
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007378 /* Export the key if permitted by the key policy. */
7379 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7380 {
Ronald Cron5425a212020-08-04 14:58:35 +02007381 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007382 first_export, export_size,
7383 &first_exported_length ) );
7384 if( generation_method == IMPORT_KEY )
7385 ASSERT_COMPARE( data->x, data->len,
7386 first_export, first_exported_length );
7387 }
Darryl Greend49a4992018-06-18 17:27:26 +01007388
7389 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007390 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007391 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007392 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007393
Darryl Greend49a4992018-06-18 17:27:26 +01007394 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007395 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007396 TEST_ASSERT( mbedtls_svc_key_id_equal(
7397 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007398 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7399 PSA_KEY_LIFETIME_PERSISTENT );
7400 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7401 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007402 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007403 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007404 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007405
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007406 /* Export the key again if permitted by the key policy. */
7407 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007408 {
Ronald Cron5425a212020-08-04 14:58:35 +02007409 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007410 second_export, export_size,
7411 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007412 ASSERT_COMPARE( first_export, first_exported_length,
7413 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007414 }
7415
7416 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007417 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007418 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007419
7420exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007421 /*
7422 * Key attributes may have been returned by psa_get_key_attributes()
7423 * thus reset them as required.
7424 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007425 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007426
Darryl Greend49a4992018-06-18 17:27:26 +01007427 mbedtls_free( first_export );
7428 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007429 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007430 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007431 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007432 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007433}
7434/* END_CASE */